@phnx-labs/agents-cli 1.22.60 → 1.22.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/cli/command-registry.d.ts +1 -0
  3. package/dist/cli/command-registry.js +2 -0
  4. package/dist/commands/browser.js +9 -4
  5. package/dist/commands/doctor.js +1 -1
  6. package/dist/commands/exec.js +35 -1
  7. package/dist/commands/harness-hooks.d.ts +55 -0
  8. package/dist/commands/harness-hooks.js +104 -0
  9. package/dist/commands/harness-wizard.d.ts +33 -14
  10. package/dist/commands/harness-wizard.js +53 -23
  11. package/dist/commands/harness.d.ts +14 -0
  12. package/dist/commands/harness.js +86 -5
  13. package/dist/commands/reminders.d.ts +9 -0
  14. package/dist/commands/reminders.js +49 -0
  15. package/dist/commands/run-account-picker.d.ts +14 -0
  16. package/dist/commands/run-account-picker.js +13 -0
  17. package/dist/commands/teams.d.ts +1 -1
  18. package/dist/commands/teams.js +9 -3
  19. package/dist/index.js +9 -0
  20. package/dist/lib/accounting/rotate.d.ts +63 -0
  21. package/dist/lib/accounting/rotate.js +229 -13
  22. package/dist/lib/browser/drivers/local.d.ts +11 -0
  23. package/dist/lib/browser/drivers/local.js +26 -0
  24. package/dist/lib/browser/profiles.js +8 -6
  25. package/dist/lib/browser/service.d.ts +12 -8
  26. package/dist/lib/browser/service.js +38 -10
  27. package/dist/lib/claude-statusline.d.ts +14 -1
  28. package/dist/lib/claude-statusline.js +27 -2
  29. package/dist/lib/daemon/runner.js +17 -2
  30. package/dist/lib/devices/doctor-findings.d.ts +1 -1
  31. package/dist/lib/devices/doctor-findings.js +22 -4
  32. package/dist/lib/doctor-diff.d.ts +21 -5
  33. package/dist/lib/doctor-diff.js +242 -76
  34. package/dist/lib/feed/events.d.ts +1 -1
  35. package/dist/lib/feed/events.js +25 -16
  36. package/dist/lib/github/gh-overload.d.ts +58 -0
  37. package/dist/lib/github/gh-overload.js +246 -0
  38. package/dist/lib/github/rest.d.ts +64 -0
  39. package/dist/lib/github/rest.js +111 -0
  40. package/dist/lib/harness-connection-test.d.ts +57 -0
  41. package/dist/lib/harness-connection-test.js +80 -0
  42. package/dist/lib/heal.js +8 -3
  43. package/dist/lib/installations/shims.d.ts +22 -0
  44. package/dist/lib/installations/shims.js +104 -0
  45. package/dist/lib/linear-project-counts.js +8 -0
  46. package/dist/lib/linear-rate-limit.d.ts +26 -0
  47. package/dist/lib/linear-rate-limit.js +163 -0
  48. package/dist/lib/mcp.d.ts +9 -0
  49. package/dist/lib/mcp.js +37 -1
  50. package/dist/lib/open-url.js +5 -3
  51. package/dist/lib/permissions.d.ts +28 -0
  52. package/dist/lib/permissions.js +156 -1
  53. package/dist/lib/refresh.js +9 -1
  54. package/dist/lib/reminders.d.ts +29 -0
  55. package/dist/lib/reminders.js +88 -0
  56. package/dist/lib/resource-content-diff.d.ts +33 -0
  57. package/dist/lib/resource-content-diff.js +103 -0
  58. package/dist/lib/rules/compile.d.ts +7 -0
  59. package/dist/lib/rules/compile.js +7 -1
  60. package/dist/lib/session/active.d.ts +41 -4
  61. package/dist/lib/session/active.js +58 -7
  62. package/dist/lib/session/host-link.d.ts +22 -0
  63. package/dist/lib/session/host-link.js +40 -4
  64. package/dist/lib/session/trajectory.d.ts +42 -0
  65. package/dist/lib/session/trajectory.js +46 -27
  66. package/dist/lib/ssh-exec.d.ts +30 -0
  67. package/dist/lib/ssh-exec.js +37 -5
  68. package/dist/lib/startup/command-registry.js +1 -1
  69. package/dist/lib/subagents-registry.d.ts +18 -0
  70. package/dist/lib/subagents-registry.js +79 -0
  71. package/dist/lib/teams/agents.d.ts +12 -0
  72. package/dist/lib/teams/agents.js +51 -0
  73. package/dist/lib/traces/schema2-build.d.ts +85 -0
  74. package/dist/lib/traces/schema2-build.js +637 -0
  75. package/dist/lib/traces/schema2-danger.d.ts +36 -0
  76. package/dist/lib/traces/schema2-danger.js +185 -0
  77. package/dist/lib/traces/schema2.d.ts +149 -0
  78. package/dist/lib/traces/schema2.js +20 -0
  79. package/dist/lib/traces/sync.d.ts +93 -0
  80. package/dist/lib/traces/sync.js +75 -22
  81. package/dist/lib/traces/worker-template.js +5 -0
  82. package/dist/lib/uninstall.js +10 -1
  83. package/dist/lib/workflows.d.ts +11 -0
  84. package/dist/lib/workflows.js +67 -8
  85. package/package.json +1 -1
@@ -0,0 +1,29 @@
1
+ export interface Reminder {
2
+ /** Succinct form shown in the statusline (a few words). */
3
+ short: string;
4
+ /** Full principle, shown by `agents reminders`. Falls back to `short`. */
5
+ full: string;
6
+ }
7
+ /**
8
+ * Point the reminders file at a fixture for tests, mirroring
9
+ * `setClaudeUsageCachePathForTest` in accounting/usage.ts. Returns the prior
10
+ * override so a test can restore it. Pass `null` to clear.
11
+ */
12
+ export declare function setRemindersFilePathForTest(filePath: string | null): string | null;
13
+ export declare function remindersFilePath(): string;
14
+ /**
15
+ * Load reminders from disk.
16
+ *
17
+ * Returns `[]` when the file does not exist — the feature is simply not opted
18
+ * in. Throws on a present-but-malformed file so `agents reminders` can surface
19
+ * the problem; the statusline caller deliberately swallows that, because a
20
+ * broken prompt is worse than a missing reminder line.
21
+ */
22
+ export declare function loadReminders(filePath?: string): Reminder[];
23
+ /**
24
+ * Deterministically pick one reminder for a session. Same `sessionId` always
25
+ * maps to the same reminder, so it is stable within a session; different session
26
+ * ids spread across the list, so concurrent agents show different reminders.
27
+ * Returns `null` when there are no reminders.
28
+ */
29
+ export declare function pickReminderForSession(reminders: Reminder[], sessionId?: string): Reminder | null;
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Personal operating reminders.
3
+ *
4
+ * A small, user-owned list of principles kept in
5
+ * `~/.agents/reminders/reminders.yaml` and surfaced succinctly in the Claude
6
+ * statusline — one per session, chosen deterministically from the session id so
7
+ * concurrent agents each show a different one and it stays stable within a
8
+ * session. Presence of the file with at least one entry is the opt-in; there is
9
+ * no separate flag. The file syncs across the fleet via `agents repo push/pull`.
10
+ */
11
+ import * as fs from 'node:fs';
12
+ import * as path from 'node:path';
13
+ import { parse as parseYaml } from 'yaml';
14
+ import { getUserAgentsDir } from './state.js';
15
+ let remindersFilePathOverride = null;
16
+ /**
17
+ * Point the reminders file at a fixture for tests, mirroring
18
+ * `setClaudeUsageCachePathForTest` in accounting/usage.ts. Returns the prior
19
+ * override so a test can restore it. Pass `null` to clear.
20
+ */
21
+ export function setRemindersFilePathForTest(filePath) {
22
+ const prev = remindersFilePathOverride;
23
+ remindersFilePathOverride = filePath;
24
+ return prev;
25
+ }
26
+ export function remindersFilePath() {
27
+ return remindersFilePathOverride ?? path.join(getUserAgentsDir(), 'reminders', 'reminders.yaml');
28
+ }
29
+ function isRecord(value) {
30
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
31
+ }
32
+ /**
33
+ * Load reminders from disk.
34
+ *
35
+ * Returns `[]` when the file does not exist — the feature is simply not opted
36
+ * in. Throws on a present-but-malformed file so `agents reminders` can surface
37
+ * the problem; the statusline caller deliberately swallows that, because a
38
+ * broken prompt is worse than a missing reminder line.
39
+ */
40
+ export function loadReminders(filePath = remindersFilePath()) {
41
+ let raw;
42
+ try {
43
+ raw = fs.readFileSync(filePath, 'utf8');
44
+ }
45
+ catch (err) {
46
+ if (err?.code === 'ENOENT')
47
+ return [];
48
+ throw err;
49
+ }
50
+ const parsed = parseYaml(raw);
51
+ const list = isRecord(parsed) && Array.isArray(parsed.reminders) ? parsed.reminders : null;
52
+ if (!list) {
53
+ throw new Error(`reminders file has no 'reminders:' list: ${filePath}`);
54
+ }
55
+ const reminders = [];
56
+ for (const item of list) {
57
+ if (!isRecord(item))
58
+ continue;
59
+ const short = typeof item.short === 'string' ? item.short.trim() : '';
60
+ if (!short)
61
+ continue;
62
+ const full = typeof item.full === 'string' && item.full.trim() ? item.full.trim() : short;
63
+ reminders.push({ short, full });
64
+ }
65
+ return reminders;
66
+ }
67
+ /**
68
+ * Deterministically pick one reminder for a session. Same `sessionId` always
69
+ * maps to the same reminder, so it is stable within a session; different session
70
+ * ids spread across the list, so concurrent agents show different reminders.
71
+ * Returns `null` when there are no reminders.
72
+ */
73
+ export function pickReminderForSession(reminders, sessionId) {
74
+ if (reminders.length === 0)
75
+ return null;
76
+ const key = sessionId?.trim();
77
+ const index = key ? hashString(key) % reminders.length : 0;
78
+ return reminders[index];
79
+ }
80
+ /** FNV-1a 32-bit — stable, dependency-free, well-distributed for short ids. */
81
+ function hashString(value) {
82
+ let hash = 0x811c9dc5;
83
+ for (let i = 0; i < value.length; i++) {
84
+ hash ^= value.charCodeAt(i);
85
+ hash = Math.imul(hash, 0x01000193);
86
+ }
87
+ return hash >>> 0;
88
+ }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Shared content comparison for the resource-diff engine.
3
+ *
4
+ * `agents doctor` (and the plugin-drift describer) reconcile a version home's
5
+ * copy of a resource against its resolved source by comparing bytes. Two shapes
6
+ * of resource need it: a single file (a command, a rule, a transformed subagent)
7
+ * and a whole directory tree (a skill, a plugin mirror, a copied workflow).
8
+ * Both live here so every kind's differ shares ONE normalize rule, ONE ignore
9
+ * set, and ONE symlink-skip policy instead of re-deriving them (the duplication
10
+ * `doctor-diff.ts`'s `dirsContentMatch` had before PHNX-3504; the parallel
11
+ * `skillDirsMatch` copies in `versions.ts` / `detectors/skills.ts` are a separate
12
+ * follow-up cleanup so this change stays inside the fast-lane CI impact budget).
13
+ *
14
+ * Normalize: CRLF → LF and trim, so a trailing-newline / line-ending difference
15
+ * between two independently-written trees is not reported as content drift. This
16
+ * mirrors the byte compare `diffCommands` / `diffRules` already apply per file.
17
+ */
18
+ /** OS metadata / local tooling that is never synced into a version home. */
19
+ export declare const RESOURCE_CONTENT_IGNORE: Set<string>;
20
+ /** CRLF → LF and trim, so line-ending / trailing-newline skew is not drift. */
21
+ export declare function normalizeResourceContent(content: string): string;
22
+ /**
23
+ * True when two files exist and their normalized content is identical. A missing
24
+ * or unreadable file on either side is a mismatch (never a silent match).
25
+ */
26
+ export declare function filesContentMatch(a: string, b: string): boolean;
27
+ /**
28
+ * True when two directory trees hold the same set of names and every file under
29
+ * them matches by normalized content. Symlinks and ignored entries are skipped
30
+ * on both sides; a name present on one side only, or a file/dir type mismatch,
31
+ * is a mismatch. An unreadable directory on either side is a mismatch.
32
+ */
33
+ export declare function dirsContentMatch(src: string, dst: string): boolean;
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Shared content comparison for the resource-diff engine.
3
+ *
4
+ * `agents doctor` (and the plugin-drift describer) reconcile a version home's
5
+ * copy of a resource against its resolved source by comparing bytes. Two shapes
6
+ * of resource need it: a single file (a command, a rule, a transformed subagent)
7
+ * and a whole directory tree (a skill, a plugin mirror, a copied workflow).
8
+ * Both live here so every kind's differ shares ONE normalize rule, ONE ignore
9
+ * set, and ONE symlink-skip policy instead of re-deriving them (the duplication
10
+ * `doctor-diff.ts`'s `dirsContentMatch` had before PHNX-3504; the parallel
11
+ * `skillDirsMatch` copies in `versions.ts` / `detectors/skills.ts` are a separate
12
+ * follow-up cleanup so this change stays inside the fast-lane CI impact budget).
13
+ *
14
+ * Normalize: CRLF → LF and trim, so a trailing-newline / line-ending difference
15
+ * between two independently-written trees is not reported as content drift. This
16
+ * mirrors the byte compare `diffCommands` / `diffRules` already apply per file.
17
+ */
18
+ import * as fs from 'fs';
19
+ import * as path from 'path';
20
+ /** OS metadata / local tooling that is never synced into a version home. */
21
+ export const RESOURCE_CONTENT_IGNORE = new Set([
22
+ '.DS_Store',
23
+ '.git',
24
+ '.gitignore',
25
+ '.venv',
26
+ '__pycache__',
27
+ 'node_modules',
28
+ ]);
29
+ /** CRLF → LF and trim, so line-ending / trailing-newline skew is not drift. */
30
+ export function normalizeResourceContent(content) {
31
+ return content.replace(/\r\n/g, '\n').trim();
32
+ }
33
+ function readSafe(file) {
34
+ try {
35
+ return fs.readFileSync(file, 'utf-8');
36
+ }
37
+ catch {
38
+ return null;
39
+ }
40
+ }
41
+ /**
42
+ * True when two files exist and their normalized content is identical. A missing
43
+ * or unreadable file on either side is a mismatch (never a silent match).
44
+ */
45
+ export function filesContentMatch(a, b) {
46
+ const ac = readSafe(a);
47
+ const bc = readSafe(b);
48
+ if (ac == null || bc == null)
49
+ return false;
50
+ return normalizeResourceContent(ac) === normalizeResourceContent(bc);
51
+ }
52
+ /**
53
+ * True when two directory trees hold the same set of names and every file under
54
+ * them matches by normalized content. Symlinks and ignored entries are skipped
55
+ * on both sides; a name present on one side only, or a file/dir type mismatch,
56
+ * is a mismatch. An unreadable directory on either side is a mismatch.
57
+ */
58
+ export function dirsContentMatch(src, dst) {
59
+ const srcEntries = (() => {
60
+ try {
61
+ return fs.readdirSync(src, { withFileTypes: true });
62
+ }
63
+ catch {
64
+ return null;
65
+ }
66
+ })();
67
+ const dstEntries = (() => {
68
+ try {
69
+ return fs.readdirSync(dst, { withFileTypes: true });
70
+ }
71
+ catch {
72
+ return null;
73
+ }
74
+ })();
75
+ if (!srcEntries || !dstEntries)
76
+ return false;
77
+ const filter = (es) => es
78
+ .filter((e) => !e.isSymbolicLink() && !RESOURCE_CONTENT_IGNORE.has(e.name))
79
+ .sort((a, b) => a.name.localeCompare(b.name));
80
+ const srcF = filter(srcEntries);
81
+ const dstF = filter(dstEntries);
82
+ if (srcF.length !== dstF.length)
83
+ return false;
84
+ for (let i = 0; i < srcF.length; i++) {
85
+ if (srcF[i].name !== dstF[i].name)
86
+ return false;
87
+ const a = path.join(src, srcF[i].name);
88
+ const b = path.join(dst, dstF[i].name);
89
+ if (srcF[i].isDirectory()) {
90
+ if (!dstF[i].isDirectory())
91
+ return false;
92
+ if (!dirsContentMatch(a, b))
93
+ return false;
94
+ }
95
+ else if (srcF[i].isFile()) {
96
+ if (!dstF[i].isFile())
97
+ return false;
98
+ if (!filesContentMatch(a, b))
99
+ return false;
100
+ }
101
+ }
102
+ return true;
103
+ }
@@ -8,6 +8,13 @@
8
8
  */
9
9
  import type { AgentId } from '../types.js';
10
10
  import { type RulesLayer } from './compose.js';
11
+ /**
12
+ * Header the non-@-import compile path (`agents refresh-rules` → compileRulesForAgent)
13
+ * prepends to a compiled instruction file. Exported so `doctor-diff` can strip it
14
+ * before content-comparing a home rules file against the raw preset composition —
15
+ * a header-compiled home must still reconcile (the header is not source content).
16
+ */
17
+ export declare const COMPILED_HEADER: string;
11
18
  export declare const COMPILED_HEADER_PROJECT: string;
12
19
  /** Sidecar manifest recording source file hashes for staleness detection. */
13
20
  export interface CompileManifest {
@@ -18,7 +18,13 @@ import { composeRules, composeRulesFromState } from './compose.js';
18
18
  // whitespace (if any) is captured so we can preserve it in the output.
19
19
  const IMPORT_RE = /(^|\s)@(\S+)/g;
20
20
  const MAX_DEPTH = 5;
21
- const COMPILED_HEADER = '<!-- Auto-compiled by agents-cli from ~/.agents/rules/AGENTS.md + imports.\n' +
21
+ /**
22
+ * Header the non-@-import compile path (`agents refresh-rules` → compileRulesForAgent)
23
+ * prepends to a compiled instruction file. Exported so `doctor-diff` can strip it
24
+ * before content-comparing a home rules file against the raw preset composition —
25
+ * a header-compiled home must still reconcile (the header is not source content).
26
+ */
27
+ export const COMPILED_HEADER = '<!-- Auto-compiled by agents-cli from ~/.agents/rules/AGENTS.md + imports.\n' +
22
28
  ' Edit the source files under ~/.agents/rules/ — edits to this file will be overwritten on next sync. -->\n\n';
23
29
  export const COMPILED_HEADER_PROJECT = '<!-- Auto-compiled by agents-cli from .agents/rules/AGENTS.md + imports.\n' +
24
30
  ' Edit the source files under .agents/rules/ — edits to this file will be overwritten on next sync. -->\n\n';
@@ -93,6 +93,19 @@ export type ActiveStatus = 'running' | 'idle' | 'queued' | 'input_required' | 'c
93
93
  | 'orphaned'
94
94
  /** The host window died and took the agent with it — an unclean exit, not a normal close. */
95
95
  | 'crashed' | 'unknown';
96
+ /**
97
+ * Coarse lifecycle bucket a UI groups a row by, projected once at the source from
98
+ * {@link ActiveStatus} so consumers stop re-deriving it (PHNX-2484). The vocabulary
99
+ * is deliberately small — five terminal-agnostic buckets — so the AGI EXT Fleet panel
100
+ * reads `phase` straight off the `sessions watch --json` row instead of mirroring the
101
+ * CLI status word onto its own enum (the ext's `mapStatusToPhase`, now deleted):
102
+ * `running` — dispatched or actively working;
103
+ * `waiting` — blocked on the operator (needs-you);
104
+ * `failed` — dangling/dead and needing attention (crashed, orphaned, abandoned);
105
+ * `done` — the process exited cleanly;
106
+ * `idle` — alive but quiet, or an unclassified state.
107
+ */
108
+ export type SessionPhase = 'running' | 'waiting' | 'failed' | 'done' | 'idle';
96
109
  /**
97
110
  * Which rung of the recap ladder produced a row's shown {@link ActiveSession.title}
98
111
  * (RUSH-3011), best-first:
@@ -221,6 +234,13 @@ export interface ActiveSession {
221
234
  */
222
235
  lastActivityMs?: number;
223
236
  status: ActiveStatus;
237
+ /**
238
+ * Coarse lifecycle bucket derived once from {@link status} (see {@link SessionPhase}).
239
+ * Folded on at the end of {@link getActiveSessions} by {@link foldPhase}, AFTER
240
+ * {@link foldHostLink} has finalized `status` — so `orphaned`/`crashed` land in the
241
+ * `failed` bucket rather than being re-derived (and mis-bucketed as `idle`) downstream.
242
+ */
243
+ phase?: SessionPhase;
224
244
  /** Indexed launch origin, backfilled by the sessions command for JSON consumers. */
225
245
  origin?: 'cli' | 'routine';
226
246
  /** Routine definition name when origin is `routine`. */
@@ -776,10 +796,15 @@ export declare function foldTmuxClients(rows: ActiveSession[]): Promise<void>;
776
796
  * - `crashed` REPLACES `closed`. Both mean the process is gone, but `closed`
777
797
  * reads as a normal exit; `crashed` says the host window went down with it
778
798
  * and never cleaned up.
779
- * - `orphaned` replaces only `idle` / `input_required`. A session still WORKING
780
- * with nobody watching is a normal headless run, and flagging every one would
781
- * bury the real signal. A session sitting idle — or worse, waiting on a
782
- * question — with no client attached is the stranded case: nobody is coming.
799
+ * - `orphaned` replaces `idle` / `input_required` on ANY `no-client`, and
800
+ * `running` ONLY when the owning WINDOW was lost (`hostWindowLost`). A
801
+ * session still working with merely zero tmux clients is a normal headless
802
+ * run — since RUSH-3125 a detached remote pane is the steady state — so
803
+ * flagging every one would bury the real signal (the false positive reverted
804
+ * in 6d973b823). But a running agent whose IDE window stopped republishing
805
+ * its heartbeat outlived an unclean host death and is genuinely stranded, so
806
+ * it IS promoted. A session sitting idle — or worse, waiting on a question —
807
+ * with no client attached is the same stranded case: nobody is coming.
783
808
  */
784
809
  /**
785
810
  * Attribute each live row to the machine the session actually EXECUTES on.
@@ -845,6 +870,18 @@ export declare function deriveSessionRecap(row: Pick<ActiveSession, 'label' | 't
845
870
  };
846
871
  /** Fold the recap ladder onto every row (see {@link deriveSessionRecap}). */
847
872
  export declare function foldRecap(rows: ActiveSession[]): void;
873
+ /**
874
+ * Project a {@link SessionPhase} from the finalized {@link ActiveStatus} (PHNX-2484).
875
+ * This is the single source of truth the AGI EXT previously mirrored as
876
+ * `mapStatusToPhase` — the ext now reads the `phase` field instead of re-deriving it.
877
+ * `queued` counts as `running` (dispatched, in the pipeline); `abandoned`, `orphaned`,
878
+ * and `crashed` all bucket to `failed` (dangling/dead — needs attention), which is the
879
+ * drift this fixes: those three used to fall through a status-only map to `idle` and
880
+ * silently hide a dead agent.
881
+ */
882
+ export declare function derivePhase(status: ActiveStatus | undefined): SessionPhase;
883
+ /** Fold the coarse lifecycle {@link SessionPhase} onto every row (see {@link derivePhase}). */
884
+ export declare function foldPhase(rows: ActiveSession[]): void;
848
885
  /** Reap only stale sessions with proven-dead pids; unknown or live processes remain recoverable. */
849
886
  export declare function isReapableOrphan(row: Pick<ActiveSession, 'status' | 'pidAlive'>): boolean;
850
887
  /**
@@ -41,7 +41,7 @@ import { detectProvenance } from './provenance.js';
41
41
  import { loadDevices } from '../devices/registry.js';
42
42
  import { machineId, normalizeHost } from '../machine-id.js';
43
43
  import { presenceFromStore } from './detached.js';
44
- import { classifyHostLink, HOST_HEARTBEAT_STALE_MS } from './host-link.js';
44
+ import { classifyHostLink, hostWindowLost, HOST_HEARTBEAT_STALE_MS } from './host-link.js';
45
45
  import { mapBounded } from '../concurrency.js';
46
46
  import { linearIssueUrl } from './linear.js';
47
47
  import { viewingInLabel } from './viewing-in.js';
@@ -1810,6 +1810,10 @@ export async function getActiveSessions(opts = {}) {
1810
1810
  // Last: derive the shown title (recap ladder) from label/tail/topic now that
1811
1811
  // every row's live tail + label are resolved (RUSH-3011).
1812
1812
  foldRecap(merged);
1813
+ // Project the coarse lifecycle bucket once status is final (foldHostLink above may
1814
+ // have promoted a row to orphaned/crashed) so consumers read `phase` off the row
1815
+ // instead of re-deriving it (PHNX-2484).
1816
+ foldPhase(merged);
1813
1817
  return merged;
1814
1818
  }
1815
1819
  /**
@@ -1923,10 +1927,15 @@ export async function foldTmuxClients(rows) {
1923
1927
  * - `crashed` REPLACES `closed`. Both mean the process is gone, but `closed`
1924
1928
  * reads as a normal exit; `crashed` says the host window went down with it
1925
1929
  * and never cleaned up.
1926
- * - `orphaned` replaces only `idle` / `input_required`. A session still WORKING
1927
- * with nobody watching is a normal headless run, and flagging every one would
1928
- * bury the real signal. A session sitting idle — or worse, waiting on a
1929
- * question — with no client attached is the stranded case: nobody is coming.
1930
+ * - `orphaned` replaces `idle` / `input_required` on ANY `no-client`, and
1931
+ * `running` ONLY when the owning WINDOW was lost (`hostWindowLost`). A
1932
+ * session still working with merely zero tmux clients is a normal headless
1933
+ * run — since RUSH-3125 a detached remote pane is the steady state — so
1934
+ * flagging every one would bury the real signal (the false positive reverted
1935
+ * in 6d973b823). But a running agent whose IDE window stopped republishing
1936
+ * its heartbeat outlived an unclean host death and is genuinely stranded, so
1937
+ * it IS promoted. A session sitting idle — or worse, waiting on a question —
1938
+ * with no client attached is the same stranded case: nobody is coming.
1930
1939
  */
1931
1940
  /**
1932
1941
  * Attribute each live row to the machine the session actually EXECUTES on.
@@ -2032,12 +2041,13 @@ export function foldHostLink(rows) {
2032
2041
  continue;
2033
2042
  // `closed` IS the dead-pid status — `lifecycleStatus` assigns it from
2034
2043
  // `!pidAlive` and nothing else — so the status is the pid answer here.
2035
- const link = classifyHostLink({
2044
+ const signals = {
2036
2045
  pidAlive: s.status !== 'closed',
2037
2046
  windowHeartbeatMs: s.windowHeartbeatMs,
2038
2047
  tmuxClients: s.tmuxClients,
2039
2048
  deliberatelyDetached: s.presence === 'background' || s.presence === 'parked',
2040
- });
2049
+ };
2050
+ const link = classifyHostLink(signals);
2041
2051
  s.hostLink = link;
2042
2052
  // `foldPresence` gives every terminal row a DERIVED `attached` — "a live
2043
2053
  // interactive TUI you're watching" — which is exactly the claim a lost host
@@ -2057,6 +2067,14 @@ export function foldHostLink(rows) {
2057
2067
  else if (link === 'no-client' && (s.status === 'idle' || s.status === 'input_required')) {
2058
2068
  s.status = 'orphaned';
2059
2069
  }
2070
+ // A still-`running` agent is normally a healthy headless run, so 0 tmux
2071
+ // clients (a detached remote pane) MUST NOT flag it — that false positive was
2072
+ // reverted once already (6d973b823). The ONE exception is a lost WINDOW: its
2073
+ // owning IDE window stopped republishing, so it died uncleanly and the agent
2074
+ // outlived it — genuinely stranded (PHNX-3183, `hostWindowLost`, SES-18a).
2075
+ else if (s.status === 'running' && hostWindowLost(signals)) {
2076
+ s.status = 'orphaned';
2077
+ }
2060
2078
  }
2061
2079
  }
2062
2080
  /** One display line, trimmed and capped, or undefined when empty. */
@@ -2099,6 +2117,39 @@ export function foldRecap(rows) {
2099
2117
  s.lastAgentLine = recap.lastAgentLine;
2100
2118
  }
2101
2119
  }
2120
+ /**
2121
+ * Project a {@link SessionPhase} from the finalized {@link ActiveStatus} (PHNX-2484).
2122
+ * This is the single source of truth the AGI EXT previously mirrored as
2123
+ * `mapStatusToPhase` — the ext now reads the `phase` field instead of re-deriving it.
2124
+ * `queued` counts as `running` (dispatched, in the pipeline); `abandoned`, `orphaned`,
2125
+ * and `crashed` all bucket to `failed` (dangling/dead — needs attention), which is the
2126
+ * drift this fixes: those three used to fall through a status-only map to `idle` and
2127
+ * silently hide a dead agent.
2128
+ */
2129
+ export function derivePhase(status) {
2130
+ switch (status) {
2131
+ case 'running':
2132
+ case 'queued':
2133
+ return 'running';
2134
+ case 'input_required':
2135
+ return 'waiting';
2136
+ case 'abandoned':
2137
+ case 'orphaned':
2138
+ case 'crashed':
2139
+ return 'failed';
2140
+ case 'closed':
2141
+ return 'done';
2142
+ case 'idle':
2143
+ case 'unknown':
2144
+ default:
2145
+ return 'idle';
2146
+ }
2147
+ }
2148
+ /** Fold the coarse lifecycle {@link SessionPhase} onto every row (see {@link derivePhase}). */
2149
+ export function foldPhase(rows) {
2150
+ for (const s of rows)
2151
+ s.phase = derivePhase(s.status);
2152
+ }
2102
2153
  /** Reap only stale sessions with proven-dead pids; unknown or live processes remain recoverable. */
2103
2154
  export function isReapableOrphan(row) {
2104
2155
  return row.status === 'abandoned' && row.pidAlive === false;
@@ -80,3 +80,25 @@ export interface HostLinkInput {
80
80
  * or a running editor.
81
81
  */
82
82
  export declare function classifyHostLink(input: HostLinkInput): HostLink;
83
+ /**
84
+ * Did the owning IDE window die while the agent kept RUNNING? This is the ONE
85
+ * host-link loss that promotes a still-`running` session to `orphaned`
86
+ * (PHNX-3183): a window that WAS republishing its heartbeat every 4 min went
87
+ * stale for {@link HOST_HEARTBEAT_STALE_MS}, so it died uncleanly (a crash,
88
+ * reboot, or dropped SSH) and the agent it hosted outlived it — genuinely
89
+ * stranded, the "my remote agent is still alive after the laptop rebooted" case.
90
+ *
91
+ * Deliberately NARROWER than `no-client`. A tmux attached-client count of zero
92
+ * is also `no-client`, but for a detached remote pane (`agents run --device`,
93
+ * which RUSH-3125 wraps in a detached tmux session) that is the NORMAL steady
94
+ * state between check-ins — launch, detach, return with `agents focus` — not a
95
+ * loss. Promoting on it would relabel every unattended remote agent as
96
+ * `orphaned`, the over-reporting that makes the word worthless. Only a lost
97
+ * WINDOW is a positive "a client was expected and is now gone" for a running
98
+ * agent; mere absence is not.
99
+ *
100
+ * A deliberately-backgrounded session is excluded exactly as
101
+ * {@link classifyHostLink} excludes it: no client is the point of detaching.
102
+ * Pure — same injected signals as the classifier.
103
+ */
104
+ export declare function hostWindowLost(input: HostLinkInput): boolean;
@@ -33,22 +33,32 @@
33
33
  * the extension agree on when a window is dead.
34
34
  */
35
35
  export const HOST_HEARTBEAT_STALE_MS = 10 * 60_000;
36
+ /**
37
+ * Did the session's owning IDE window stop republishing its registry slice? A
38
+ * window republishes every 4 minutes (`HOST_HEARTBEAT_STALE_MS` is 10), so a
39
+ * slice this old means the window is gone, not merely quiet. Shared by
40
+ * {@link classifyHostLink} and {@link hostWindowLost} so the staleness rule has
41
+ * exactly one definition.
42
+ */
43
+ function windowGone(input) {
44
+ const now = input.nowMs ?? Date.now();
45
+ return input.windowHeartbeatMs !== undefined && now - input.windowHeartbeatMs >= HOST_HEARTBEAT_STALE_MS;
46
+ }
36
47
  /**
37
48
  * Classify a live row's host link. Pure — every signal is passed in, so the
38
49
  * whole decision table is unit-testable without a process table, a tmux server,
39
50
  * or a running editor.
40
51
  */
41
52
  export function classifyHostLink(input) {
42
- const now = input.nowMs ?? Date.now();
43
53
  // A session detached on purpose has no client BY DESIGN. It is the one case
44
54
  // that looks identical to an orphan from the outside, so it is excluded first —
45
55
  // otherwise every `agents sessions detach` would raise a false alarm.
46
56
  if (input.deliberatelyDetached)
47
57
  return 'connected';
48
- const windowGone = input.windowHeartbeatMs !== undefined && now - input.windowHeartbeatMs >= HOST_HEARTBEAT_STALE_MS;
58
+ const stale = windowGone(input);
49
59
  // The host window stopped keeping its registry slice alive AND the agent it
50
60
  // owned is dead: the pair went down together without teardown.
51
- if (windowGone && !input.pidAlive)
61
+ if (stale && !input.pidAlive)
52
62
  return 'host-gone';
53
63
  if (!input.pidAlive)
54
64
  return 'connected'; // a plain dead pid is `closed`, not an orphan
@@ -58,7 +68,7 @@ export function classifyHostLink(input) {
58
68
  return 'no-client';
59
69
  // Alive, not tmux-hosted (or tmux says someone is attached), but the window
60
70
  // that owned it is gone. The agent outlived its editor.
61
- if (windowGone)
71
+ if (stale)
62
72
  return 'no-client';
63
73
  // Positive evidence of a client: tmux counted at least one attached client, or
64
74
  // a window is refreshing its registry slice. Either is a real observation.
@@ -68,3 +78,29 @@ export function classifyHostLink(input) {
68
78
  // reporting the healthy answer by default — see {@link HostLink}'s `unknown`.
69
79
  return 'unknown';
70
80
  }
81
+ /**
82
+ * Did the owning IDE window die while the agent kept RUNNING? This is the ONE
83
+ * host-link loss that promotes a still-`running` session to `orphaned`
84
+ * (PHNX-3183): a window that WAS republishing its heartbeat every 4 min went
85
+ * stale for {@link HOST_HEARTBEAT_STALE_MS}, so it died uncleanly (a crash,
86
+ * reboot, or dropped SSH) and the agent it hosted outlived it — genuinely
87
+ * stranded, the "my remote agent is still alive after the laptop rebooted" case.
88
+ *
89
+ * Deliberately NARROWER than `no-client`. A tmux attached-client count of zero
90
+ * is also `no-client`, but for a detached remote pane (`agents run --device`,
91
+ * which RUSH-3125 wraps in a detached tmux session) that is the NORMAL steady
92
+ * state between check-ins — launch, detach, return with `agents focus` — not a
93
+ * loss. Promoting on it would relabel every unattended remote agent as
94
+ * `orphaned`, the over-reporting that makes the word worthless. Only a lost
95
+ * WINDOW is a positive "a client was expected and is now gone" for a running
96
+ * agent; mere absence is not.
97
+ *
98
+ * A deliberately-backgrounded session is excluded exactly as
99
+ * {@link classifyHostLink} excludes it: no client is the point of detaching.
100
+ * Pure — same injected signals as the classifier.
101
+ */
102
+ export function hostWindowLost(input) {
103
+ if (input.deliberatelyDetached)
104
+ return false;
105
+ return input.pidAlive && windowGone(input);
106
+ }
@@ -90,6 +90,48 @@ export interface BuildTrajectoryOptions {
90
90
  /** Cap on drawn steps; the tail is dropped and counted. Default 5000. */
91
91
  maxSteps?: number;
92
92
  }
93
+ /**
94
+ * The effective program a shell command ran, via the shared shell parser
95
+ * (`extractShellPrograms` — which unwraps the wrappers it knows: `env`, `sudo`,
96
+ * `agents ssh`; note `timeout` is deliberately NOT a wrapper there, per SES-37,
97
+ * so `timeout 30 npm test` resolves to `timeout`). On top of that this skips
98
+ * bare builtins/assignments (`cd`, `export`, …) when a real program follows. For
99
+ * a pipeline it takes the LEFTMOST effective program (`cat f | grep x` → `cat`),
100
+ * matching how the parser orders occurrences. Undefined when nothing static is
101
+ * identifiable. Never executes anything.
102
+ */
103
+ export declare function effectiveProgram(command: string | undefined): string | undefined;
104
+ /**
105
+ * One paired step: the drawn {@link TrajectoryStep}, the index of its originating
106
+ * `tool_use`/`thinking` event, and — once pairing completes — the index of the
107
+ * `tool_result`/`error` event that answered it. `buildSessionDetailV2` reads these
108
+ * triples (step, useEvent, resultEvent) to populate the schema-2 per-tool shapes
109
+ * WITHOUT re-running the callId pairing loop.
110
+ */
111
+ export interface StepDraft {
112
+ step: TrajectoryStep;
113
+ eventIndex: number;
114
+ callId?: string;
115
+ resultEventIndex?: number;
116
+ }
117
+ /** Absolute ms per event index (NaN when the timestamp is unparseable). */
118
+ export declare function eventTimestampsMs(events: SessionEvent[]): number[];
119
+ /**
120
+ * Draw a step for each thinking block and each non-local tool_use, in order, and
121
+ * pair each `tool_use` with its `tool_result`/`error` on `callId` (FIFO within a
122
+ * reused id — never across ids, and never by arrival order for concurrent calls,
123
+ * matching `ToolCallCollector.takePending`'s refusal to guess, `tool-calls.ts:509`).
124
+ *
125
+ * `_local` tool calls (Claude `!`-prefixed shell) are excluded to match the
126
+ * header's tool count (`computeSummaryStats` skips them, `render.ts:224`).
127
+ *
128
+ * The single source of the pairing: both {@link buildTrajectory} and
129
+ * `buildSessionDetailV2` consume the returned drafts (each draft's `eventIndex` +
130
+ * `resultEventIndex` are the use/result event indices) so the schema-2 producer
131
+ * never re-implements the loop. Durations/outcomes are still resolved by the
132
+ * caller — this only draws and pairs.
133
+ */
134
+ export declare function pairSteps(events: SessionEvent[], eventMs: number[], firstTs: number, redact: boolean, knownSecrets: readonly string[] | undefined): StepDraft[];
93
135
  /**
94
136
  * Build the derived trajectory for one session's normalized events.
95
137
  *