agent-orchestrator/packages/core/src
Harshit Singh Bhandari a610601158
refactor(agent-claude-code): split activity detection; remove dead JSONL cases; detect blocked from api_error (#1927)
* refactor(agent-claude-code): split activity detection into its own module; remove dead JSONL cases

Move `getActivityState`, `findClaudeProcess`, the ps-cache, `classifyTerminalOutput`, `findLatestSessionFile`, and `toClaudeProjectPath` into a new `activity-detection.ts`. `index.ts` shrinks by ~190 lines and now delegates via thin wrappers; `toClaudeProjectPath` and `resetPsCache` are re-exported so existing unit-test and integration-test import sites keep working.

Drop two switch branches that could never fire: `case "permission_request"` → `waiting_input` and `case "error"` → `blocked`. Verified by reading every JSONL under `~/.claude/projects/`: Claude emits `agent-color, agent-name, ai-title, assistant, attachment, custom-title, file-history-snapshot, last-prompt, permission-mode, pr-link, progress, queue-operation, summary, system, user` — but never a top-level `permission_request` or `error`. Permission prompts have no native-JSONL signal at all (they only sit in the terminal until the user answers); API errors arrive under `type:"system"` with `subtype:"api_error"` / `level:"error"`, not as a top-level `error` type. `waiting_input` and `blocked` continue to flow through the terminal-regex → AO activity-JSONL path that was added upstream — that path is now the only source, which matches reality.

Behavior preserved: the cascade (process check → native JSONL → AO actionable → AO age-decay fallback → stale-native) is byte-equivalent for every type Claude actually emits. Four tests that wrote fake `permission_request` / `error` JSONL deleted.

* feat(agent-claude-code,core): detect blocked from Claude api_error JSONL entries

Extend `readLastJsonlEntry` in core to also surface top-level `subtype` and `level` fields (additive — existing return shape unchanged). Map `{type:"system", level:"error"}` to `blocked` in `getClaudeActivityState`.

Claude writes API errors as `{type:"system", subtype:"api_error", level:"error", cause:{code:"ConnectionRefused"|"FailedToOpenSocket"|...}, retryAttempt:N, maxRetries:10}`. When the LAST JSONL entry has that shape, Claude is mid-retry-loop or has exhausted retries — surfacing as `blocked` lets stuck-detection fire. Other `system` subtypes (`compact_boundary`, `local_command`, `turn_duration`, `away_summary`, etc.) continue to map to `ready`/`idle` by age via the `entry.lastLevel !== "error"` branch.

Closes the only remaining "no live producer" gap from the PR description: previously `blocked` had a slot in the cascade but no writer. Now native JSONL fills it directly, no AO activity-log roundtrip needed.

Tests:
- "blocked for 'system' api_error (level: error)"
- "ready for non-error 'system' subtypes (compact_boundary)"
- "'system' api_error ignores staleness (always blocked)"

Existing `system` test (no level field) continues to map to `ready` — verified.

* fix(agent-claude-code): require api_error subtype AND error level for blocked

Tighten the system-entry gate per @greptile-apps review on #1927. Today only `api_error` carries `level:"error"` under `type:"system"`, but pinning both fields makes intent explicit and prevents silent drift if Claude ever adds a non-fatal error-level diagnostic later.

Test locks the tightened gate: `{type:"system", subtype:"future_diagnostic", level:"error"}` → `ready` (not `blocked`).

* test(core): cover lastSubtype/lastLevel extraction in readLastJsonlEntry

Per reviewer note on #1927: the consumer side (claude-code plugin) tested the new fields, but the producer side in core didn't. Lock the extraction behavior so future refactors of `readLastJsonlEntry` can't regress silently.

Covers: real Claude api_error shape, absent fields, non-string field types.
2026-05-19 18:00:54 +05:30
..
__tests__ refactor(agent-claude-code): split activity detection; remove dead JSONL cases; detect blocked from api_error (#1927) 2026-05-19 18:00:54 +05:30
migration feat(core): record activity events for config, plugin-registry, and storage migration (#1696) 2026-05-18 19:16:34 +05:30
prompts Add orchestrator-driven code review board (#1871) 2026-05-19 11:47:57 +05:30
recovery feat(core): activity events for recovery, metadata corruption, agent-report (#1692) 2026-05-18 17:27:36 +05:30
utils fix: recover native session restore fallback (#1797) 2026-05-18 01:37:39 +05:30
activity-events.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
activity-log.ts fix(core): keep actionable activity sticky (#1902) 2026-05-17 20:03:56 +05:30
activity-signal.ts fix: address lifecycle review feedback (#122) 2026-04-17 19:38:32 +05:30
agent-report.ts feat(core): record activity events for config, plugin-registry, and storage migration (#1696) 2026-05-18 19:16:34 +05:30
agent-selection.ts fix(core): forward allSessionPrefixes in resolveSessionRole 2026-04-06 23:11:59 -07:00
agent-workspace-hooks.ts feat(windows): complete Windows support (#1025) 2026-05-09 00:10:53 +05:30
atomic-write.ts feat(windows): complete Windows support (#1025) 2026-05-09 00:10:53 +05:30
cleanup-stack.ts refactor(core): replace spawn rollback ladder with CleanupStack (#1616) 2026-05-03 22:43:21 +05:30
code-review-manager.ts Add orchestrator-driven code review board (#1871) 2026-05-19 11:47:57 +05:30
code-review-store.ts Add orchestrator-driven code review board (#1871) 2026-05-19 11:47:57 +05:30
config-generator.ts feat(windows): complete Windows support (#1025) 2026-05-09 00:10:53 +05:30
config.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
daemon-children.ts fix(cli): reap daemon children on stop+SIGINT, sweep orphans on start (closes #1848) (#1849) 2026-05-15 03:38:09 +05:30
dashboard-notifications.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
events-db.ts fix(cli): rebuild better-sqlite3 on install + quieter ABI-mismatch warning (closes #1822) (#1824) 2026-05-18 04:02:26 +05:30
feature-flags.ts Add multi-project storage, resolution, and project settings support (#1343) 2026-04-21 17:45:55 +05:30
feedback-tools.ts feat(core): add feedback tools contracts, validation, storage, and dedupe 2026-03-10 22:31:39 +05:30
file-lock.ts Add multi-project storage, resolution, and project settings support (#1343) 2026-04-21 17:45:55 +05:30
format-automated-comments.ts fix(core): dispatch detailed bugbot review comments to agents (#1334) 2026-04-22 22:18:13 +05:30
gh-trace.ts feat(windows): complete Windows support (#1025) 2026-05-09 00:10:53 +05:30
git-activity.ts refactor(core): extract hasRecentCommits helper into @aoagents/ao-core (#1437) 2026-05-05 18:58:53 +05:30
global-config.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
index.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
key-value.ts feat(core): add feedback tools contracts, validation, storage, and dedupe 2026-03-10 22:31:39 +05:30
lifecycle-manager.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
lifecycle-state.ts fix(core): clear terminatedAt/completedAt when restoring a session out of terminal state (closes #1831) (#1834) 2026-05-15 04:46:35 +05:30
lifecycle-status-decisions.ts refactor(core): storage redesign — projectId-based paths, JSON metadata (#1466) 2026-04-28 17:55:53 +05:30
lifecycle-transition.ts fix(core): clear terminatedAt/completedAt when restoring a session out of terminal state (closes #1831) (#1834) 2026-05-15 04:46:35 +05:30
markdown.d.ts build(core): bundle prompt templates with rollup 2026-04-13 14:55:01 +05:30
metadata.ts feat(core): round out session-manager activity events (#1657) (#1697) 2026-05-18 19:53:55 +05:30
notification-data.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
notification-observability.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
notifier-resolution.ts fix: merge upstream main and resolve conflicts for cursor agent 2026-04-10 11:48:27 +05:30
observability.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
opencode-agents-md.ts feat(opencode): enhance AGENTS.md handling 2026-04-11 18:58:04 +05:30
opencode-config.ts feat (core): inject worker prompt as instructions file (#1302) 2026-04-26 15:44:39 +05:30
opencode-session-id.ts feat: OpenCode session lifecycle and CLI controls (#315) 2026-03-08 09:55:44 +05:30
opencode-shared.ts feat(windows): complete Windows support (#1025) 2026-05-09 00:10:53 +05:30
orchestrator-prompt.ts fix(core): validate template placeholders before render 2026-04-16 20:13:58 +05:30
orchestrator-session-strategy.ts Fix orchestrator identity to use one canonical session per project (#1487) 2026-04-25 18:57:31 +05:30
paths.ts Add orchestrator-driven code review board (#1871) 2026-05-19 11:47:57 +05:30
platform.ts fix(cli): reap daemon children on stop+SIGINT, sweep orphans on start (closes #1848) (#1849) 2026-05-15 03:38:09 +05:30
plugin-registry.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
portfolio-projects.ts Add multi-project storage, resolution, and project settings support (#1343) 2026-04-21 17:45:55 +05:30
portfolio-registry.ts refactor(core): storage redesign — projectId-based paths, JSON metadata (#1466) 2026-04-28 17:55:53 +05:30
portfolio-routing.ts Add multi-project storage, resolution, and project settings support (#1343) 2026-04-21 17:45:55 +05:30
portfolio-session-service.ts fix: recover native session restore fallback (#1797) 2026-05-18 01:37:39 +05:30
process-cache.ts refactor(spawn): plugin-owned preflight + collapse project resolution (#1622) 2026-05-04 14:03:26 +05:30
project-resolver.ts feat(core): record activity events for config, plugin-registry, and storage migration (#1696) 2026-05-18 19:16:34 +05:30
prompt-builder.ts feat: enable worker→orchestrator dialogue via `ao send` with auto-sender prefix (#1787) 2026-05-10 21:50:43 +05:30
query-activity-events.ts feat: add SQLite-backed activity event logging layer (#1528) 2026-05-01 21:13:20 +05:30
report-watcher.ts fix: address PR #1300 requested changes 2026-04-18 11:13:53 +05:30
scm-webhook-utils.ts feat: add SCM webhook lifecycle triggers (#394) 2026-03-11 10:34:41 +05:30
session-manager.ts feat(core): round out session-manager activity events (#1657) (#1697) 2026-05-18 19:53:55 +05:30
spawn-target.ts fix(cli): derive projectId from prefixed issue id on spawn (#1330) 2026-04-22 22:18:39 +05:30
storage-key.ts feat(windows): complete Windows support (#1025) 2026-05-09 00:10:53 +05:30
tmux.ts feat(windows): complete Windows support (#1025) 2026-05-09 00:10:53 +05:30
types.ts feat(notifier): make notifier system robust with manual harness and desktop setup (#1736) 2026-05-19 14:48:40 +05:30
update-cache.ts feat(release): weekly release train — channels, onboarding, dashboard banner, cron (#1781) 2026-05-12 23:11:09 +05:30
utils.ts refactor(agent-claude-code): split activity detection; remove dead JSONL cases; detect blocked from api_error (#1927) 2026-05-19 18:00:54 +05:30
version-compare.ts feat(release): weekly release train — channels, onboarding, dashboard banner, cron (#1781) 2026-05-12 23:11:09 +05:30
windows-pty-registry.ts feat(windows): complete Windows support (#1025) 2026-05-09 00:10:53 +05:30