jg-agent-hq 13.1.2 → 13.1.3

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 13.1.3
4
+
5
+ - Keep native execution status visible for sessions with uncertain control ownership, including CLI sessions already running at startup and status changes after connection loss. Session reads, discovery and active lists retain the managed identity and uncertain ownership without joining the session, replaying input or releasing queued work.
6
+ - Reread native status after a newer CLI turn prevents automatic reattachment, preserving the initial observation across the startup quarantine race. Keep the existing storage schema, action key and public request contracts unchanged.
7
+
3
8
  ## 13.1.2
4
9
 
5
10
  - Preserve native Codex item lifecycle timestamps in history, item details, evidence and live notifications. Operator messages use their own start time and replies their own completion time, retaining millisecond precision. Missing item times remain unknown instead of borrowing a turn time.
package/api/README.md CHANGED
@@ -379,6 +379,8 @@ Codex leaves `reasoning` items out as it reads them, so a Codex page can hold fe
379
379
 
380
380
  Every session has a live state: `running`, `waiting` (on the operator), `idle`, `closed`, `failed` or `unknown`. A managed session reports it from its managed record. A session another client drives is observed and listed with `ownership: 'external'`; the runtime sends it no input until `thread-adopt` records it as managed, and never stops, wakes or drives a session a terminal runs. A managed session the runtime closed is observed again, so a terminal that continues it shows its state.
381
381
 
382
+ A retained session with uncertain ownership also takes its live state from native observation, including loaded Codex sessions found at startup and later status changes. Thread reads, discovery and active-session summaries retain its managed ID and `ownership: 'uncertain'`. Observation never changes its control record, settles uncertain input, advances queued input or restores execution authority. Without a native observation its state remains unknown; explicit recovery is still required to regain control.
383
+
382
384
  A Claude session's summary carries `terminal`: `{ kind: 'interactive' }`, or `{ kind: 'background', attachId }` with the short ID `claude attach` takes, while the latest `claude agents --json` run lists it with a `pid`, and null otherwise; Codex summaries always carry null. While it is set, `thread-adopt` and `thread-resume` of the session, and input to it, are refused with JG_AGENT_HQ_ACTION_CONFLICT, so a session the runtime runs that a terminal resumed takes nothing from the runtime until the terminal closes it. An approval request of a Codex turn reaches every client the daemon serves; when another answers first, the runtime's prompt expires.
383
385
 
384
386
  Every summary carries `backgroundCommands`, the background tasks the session runs, after which Claude resumes it on its own: commands it runs or moved to the background, and agents it launched there. A session the runtime runs reports the count Claude last reported in `system/background_tasks_changed`, the same count its `live-items` window carries. A Claude session a terminal runs reports the tasks its transcript records since its listed process's `startedAt`, as the active-session poll below counts them. Codex sessions, and Claude sessions no run lists, report 0.
@@ -324,8 +324,8 @@ export const createEvents = (context, dispatcher, reconnect) => {
324
324
  turnId: null,
325
325
  });
326
326
  }
327
- return;
328
327
  }
328
+ // the store suppresses controlled observations and retains status for uncertain controls
329
329
  enqueue(provider, () => store.call('sessions', 'observe', provider, [{ nativeId, state: sessionState, thread }], false));
330
330
  },
331
331
  delta(provider, nativeId, turnId, itemId, text) {
@@ -37,6 +37,22 @@ export const createReattach = (context, dispatcher, events) => {
37
37
  },
38
38
  });
39
39
  waits.notifyThread(thread.id);
40
+ if (bound?.thread?.ownership === 'uncertain') {
41
+ // the initial loaded-thread observation may have preceded quarantine
42
+ const { thread: observed } = await control.inspectThread(nativeId, thread.workspaceRoot);
43
+ await store.call('sessions', 'observe', thread.provider, [
44
+ {
45
+ nativeId,
46
+ state: observed.state,
47
+ thread: {
48
+ workspaceRoot: observed.workspaceRoot,
49
+ name: observed.name,
50
+ createdAt: observed.createdAt,
51
+ },
52
+ },
53
+ ], false);
54
+ return;
55
+ }
40
56
  if (bound?.thread?.ownership !== 'managed' || bound.thread.runtimeEpoch !== epoch)
41
57
  return;
42
58
  own(thread.provider, nativeId);
@@ -1,2 +1,2 @@
1
1
  export declare const RETENTION_MS = 604800000;
2
- export declare const ACTIVE_SESSIONS = "WITH included(provider, native_id) AS (\n SELECT json_extract(value, '$[0]'), json_extract(value, '$[1]') FROM json_each(?)\n)\nSELECT provider, native_id, state, changed_at, thread, observed_thread, history FROM (\n SELECT agent_hq_thread.provider, agent_hq_thread.native_id,\n EXISTS (SELECT 1 FROM included WHERE included.provider = agent_hq_thread.provider AND included.native_id = agent_hq_thread.native_id) AS is_included,\n coalesce(map.session_state, (SELECT state FROM agent_hq_session_transition WHERE thread_id = agent_hq_thread.id ORDER BY sequence DESC LIMIT 1), 'unknown') AS state,\n coalesce((SELECT recorded_at FROM agent_hq_session_transition WHERE thread_id = agent_hq_thread.id ORDER BY sequence DESC LIMIT 1), agent_hq_thread.updated_at) AS changed_at,\n json_object('id', agent_hq_thread.id, 'ownership', agent_hq_thread.ownership, 'name', agent_hq_thread.name,\n 'workspaceRoot', agent_hq_thread.workspace_root, 'nativeLineage', json(agent_hq_thread.native_lineage),\n 'createdAt', agent_hq_thread.created_at, 'updatedAt', agent_hq_thread.updated_at) AS thread,\n NULL AS observed_thread, NULL AS history\n FROM agent_hq_thread INNER JOIN agent_hq_session_state_map AS map ON map.thread_state = agent_hq_thread.state\n WHERE agent_hq_thread.runtime_epoch IS NOT NULL AND agent_hq_thread.native_id IS NOT NULL AND (map.session_state IS NULL OR map.session_state IN ('running', 'waiting')\n OR EXISTS (SELECT 1 FROM included WHERE included.provider = agent_hq_thread.provider AND included.native_id = agent_hq_thread.native_id))\n UNION ALL\n SELECT observed.provider, observed.native_id,\n EXISTS (SELECT 1 FROM included WHERE included.provider = observed.provider AND included.native_id = observed.native_id),\n observed.state, observed.observed_at, NULL,\n CASE WHEN observed.provider = 'codex' AND observed.workspace_root IS NOT NULL THEN json_object('workspaceRoot', observed.workspace_root,\n 'name', observed.name, 'createdAt', observed.created_at) END,\n CASE WHEN observed.provider = 'claude' THEN (\n SELECT json_object('name', name, 'createdAt', created_at, 'updatedAt', updated_at,\n 'isSupported', json(CASE WHEN supported = 1 THEN 'true' ELSE 'false' END))\n FROM agent_hq_claude_file WHERE native_id = observed.native_id AND workspace_state = 'matching'\n ORDER BY updated_at DESC LIMIT 1\n ) END\n FROM agent_hq_observed_session AS observed WHERE observed.state IN ('running', 'waiting')\n OR EXISTS (SELECT 1 FROM included WHERE included.provider = observed.provider AND included.native_id = observed.native_id)\n) WHERE state IN ('running', 'waiting') OR (is_included AND state = 'idle')\nORDER BY changed_at DESC, native_id DESC LIMIT ?";
2
+ export declare const ACTIVE_SESSIONS = "WITH included(provider, native_id) AS (\n SELECT json_extract(value, '$[0]'), json_extract(value, '$[1]') FROM json_each(?)\n)\nSELECT provider, native_id, state, changed_at, thread, observed_thread, history FROM (\n SELECT agent_hq_thread.provider, agent_hq_thread.native_id,\n EXISTS (SELECT 1 FROM included WHERE included.provider = agent_hq_thread.provider AND included.native_id = agent_hq_thread.native_id) AS is_included,\n coalesce(map.session_state, (SELECT state FROM agent_hq_session_transition WHERE thread_id = agent_hq_thread.id ORDER BY sequence DESC LIMIT 1), 'unknown') AS state,\n coalesce((SELECT recorded_at FROM agent_hq_session_transition WHERE thread_id = agent_hq_thread.id ORDER BY sequence DESC LIMIT 1), agent_hq_thread.updated_at) AS changed_at,\n json_object('id', agent_hq_thread.id, 'ownership', agent_hq_thread.ownership, 'name', agent_hq_thread.name,\n 'workspaceRoot', agent_hq_thread.workspace_root, 'nativeLineage', json(agent_hq_thread.native_lineage),\n 'createdAt', agent_hq_thread.created_at, 'updatedAt', agent_hq_thread.updated_at) AS thread,\n NULL AS observed_thread, NULL AS history\n FROM agent_hq_thread INNER JOIN agent_hq_session_state_map AS map ON map.thread_state = agent_hq_thread.state\n WHERE agent_hq_thread.ownership = 'managed' AND agent_hq_thread.runtime_epoch IS NOT NULL AND agent_hq_thread.native_id IS NOT NULL AND (map.session_state IS NULL OR map.session_state IN ('running', 'waiting')\n OR EXISTS (SELECT 1 FROM included WHERE included.provider = agent_hq_thread.provider AND included.native_id = agent_hq_thread.native_id))\n UNION ALL\n SELECT observed.provider, observed.native_id,\n EXISTS (SELECT 1 FROM included WHERE included.provider = observed.provider AND included.native_id = observed.native_id),\n observed.state, observed.observed_at,\n CASE WHEN retained.id IS NOT NULL THEN json_object('id', retained.id, 'ownership', retained.ownership,\n 'name', retained.name, 'workspaceRoot', retained.workspace_root, 'nativeLineage', json(retained.native_lineage),\n 'createdAt', retained.created_at, 'updatedAt', retained.updated_at) END,\n CASE WHEN observed.provider = 'codex' AND observed.workspace_root IS NOT NULL THEN json_object('workspaceRoot', observed.workspace_root,\n 'name', observed.name, 'createdAt', observed.created_at) END,\n CASE WHEN observed.provider = 'claude' THEN (\n SELECT json_object('name', name, 'createdAt', created_at, 'updatedAt', updated_at,\n 'isSupported', json(CASE WHEN supported = 1 THEN 'true' ELSE 'false' END))\n FROM agent_hq_claude_file WHERE native_id = observed.native_id AND workspace_state = 'matching'\n ORDER BY updated_at DESC LIMIT 1\n ) END\n FROM agent_hq_observed_session AS observed\n LEFT JOIN agent_hq_thread AS retained ON retained.provider = observed.provider\n AND retained.native_id = observed.native_id AND retained.ownership = 'uncertain'\n WHERE (observed.state IN ('running', 'waiting')\n OR EXISTS (SELECT 1 FROM included WHERE included.provider = observed.provider AND included.native_id = observed.native_id))\n AND NOT EXISTS (SELECT 1 FROM agent_hq_thread AS controlled WHERE controlled.provider = observed.provider\n AND controlled.native_id = observed.native_id AND controlled.ownership = 'managed' AND controlled.state != 'inactive')\n) WHERE state IN ('running', 'waiting') OR (is_included AND state = 'idle')\nORDER BY changed_at DESC, native_id DESC LIMIT ?";
@@ -5,7 +5,8 @@ export const RETENTION_MS = 604_800_000;
5
5
  * sessions, which run background commands. A managed thread is active only while a runtime binds
6
6
  * it, which its runtime epoch index finds; a stopping one reports the state it stops, from its
7
7
  * last transition. An observed Codex session carries its latest confirmed metadata, and an
8
- * observed Claude session joins its indexed transcript, if any, by session ID. The included
8
+ * observed Claude session joins its indexed transcript, if any, by session ID. An uncertain
9
+ * control retains its identity while taking its live state from observation. The included
9
10
  * sessions are one JSON array of [provider, native ID] pairs.
10
11
  */
11
12
  export const ACTIVE_SESSIONS = `WITH included(provider, native_id) AS (
@@ -21,12 +22,15 @@ SELECT provider, native_id, state, changed_at, thread, observed_thread, history
21
22
  'createdAt', agent_hq_thread.created_at, 'updatedAt', agent_hq_thread.updated_at) AS thread,
22
23
  NULL AS observed_thread, NULL AS history
23
24
  FROM agent_hq_thread INNER JOIN agent_hq_session_state_map AS map ON map.thread_state = agent_hq_thread.state
24
- WHERE agent_hq_thread.runtime_epoch IS NOT NULL AND agent_hq_thread.native_id IS NOT NULL AND (map.session_state IS NULL OR map.session_state IN ('running', 'waiting')
25
+ WHERE agent_hq_thread.ownership = 'managed' AND agent_hq_thread.runtime_epoch IS NOT NULL AND agent_hq_thread.native_id IS NOT NULL AND (map.session_state IS NULL OR map.session_state IN ('running', 'waiting')
25
26
  OR EXISTS (SELECT 1 FROM included WHERE included.provider = agent_hq_thread.provider AND included.native_id = agent_hq_thread.native_id))
26
27
  UNION ALL
27
28
  SELECT observed.provider, observed.native_id,
28
29
  EXISTS (SELECT 1 FROM included WHERE included.provider = observed.provider AND included.native_id = observed.native_id),
29
- observed.state, observed.observed_at, NULL,
30
+ observed.state, observed.observed_at,
31
+ CASE WHEN retained.id IS NOT NULL THEN json_object('id', retained.id, 'ownership', retained.ownership,
32
+ 'name', retained.name, 'workspaceRoot', retained.workspace_root, 'nativeLineage', json(retained.native_lineage),
33
+ 'createdAt', retained.created_at, 'updatedAt', retained.updated_at) END,
30
34
  CASE WHEN observed.provider = 'codex' AND observed.workspace_root IS NOT NULL THEN json_object('workspaceRoot', observed.workspace_root,
31
35
  'name', observed.name, 'createdAt', observed.created_at) END,
32
36
  CASE WHEN observed.provider = 'claude' THEN (
@@ -35,7 +39,12 @@ SELECT provider, native_id, state, changed_at, thread, observed_thread, history
35
39
  FROM agent_hq_claude_file WHERE native_id = observed.native_id AND workspace_state = 'matching'
36
40
  ORDER BY updated_at DESC LIMIT 1
37
41
  ) END
38
- FROM agent_hq_observed_session AS observed WHERE observed.state IN ('running', 'waiting')
39
- OR EXISTS (SELECT 1 FROM included WHERE included.provider = observed.provider AND included.native_id = observed.native_id)
42
+ FROM agent_hq_observed_session AS observed
43
+ LEFT JOIN agent_hq_thread AS retained ON retained.provider = observed.provider
44
+ AND retained.native_id = observed.native_id AND retained.ownership = 'uncertain'
45
+ WHERE (observed.state IN ('running', 'waiting')
46
+ OR EXISTS (SELECT 1 FROM included WHERE included.provider = observed.provider AND included.native_id = observed.native_id))
47
+ AND NOT EXISTS (SELECT 1 FROM agent_hq_thread AS controlled WHERE controlled.provider = observed.provider
48
+ AND controlled.native_id = observed.native_id AND controlled.ownership = 'managed' AND controlled.state != 'inactive')
40
49
  ) WHERE state IN ('running', 'waiting') OR (is_included AND state = 'idle')
41
50
  ORDER BY changed_at DESC, native_id DESC LIMIT ?`;
@@ -19,10 +19,10 @@ export class SessionsModel {
19
19
  try {
20
20
  for (const session of sessions) {
21
21
  /*
22
- * A session HQ runs keeps its transitions on its managed record instead; one HQ closed is
23
- * observed again, so a terminal that continues it shows.
22
+ * A session HQ controls keeps its transitions on its managed record. Closed and uncertain
23
+ * controls can still be observed without granting control or changing their ledger.
24
24
  */
25
- if (this.__statements.get("SELECT 1 FROM agent_hq_thread WHERE provider = ? AND native_id = ? AND state != 'inactive'", provider, session.nativeId)) {
25
+ if (this.__statements.get("SELECT 1 FROM agent_hq_thread WHERE provider = ? AND native_id = ? AND ownership = 'managed' AND state != 'inactive'", provider, session.nativeId)) {
26
26
  this.__statements.run('DELETE FROM agent_hq_observed_session WHERE provider = ? AND native_id = ?', provider, session.nativeId);
27
27
  continue;
28
28
  }
@@ -67,10 +67,10 @@ export class SessionsModel {
67
67
  /** Reads the live states of selected native sessions, managed or observed. */
68
68
  states(provider, nativeIds) {
69
69
  return nativeIds.flatMap((nativeId) => {
70
- const record = this.__statements.get("SELECT coalesce(agent_hq_session_state_map.session_state, (SELECT state FROM agent_hq_session_transition WHERE thread_id = agent_hq_thread.id ORDER BY sequence DESC LIMIT 1), 'unknown') AS state, agent_hq_thread.state = 'inactive' AS is_closed FROM agent_hq_thread INNER JOIN agent_hq_session_state_map ON agent_hq_session_state_map.thread_state = agent_hq_thread.state WHERE agent_hq_thread.provider = ? AND agent_hq_thread.native_id = ?", provider, nativeId);
70
+ const record = this.__statements.get("SELECT coalesce(agent_hq_session_state_map.session_state, (SELECT state FROM agent_hq_session_transition WHERE thread_id = agent_hq_thread.id ORDER BY sequence DESC LIMIT 1), 'unknown') AS state, (agent_hq_thread.state = 'inactive' OR agent_hq_thread.ownership = 'uncertain') AS uses_observation FROM agent_hq_thread INNER JOIN agent_hq_session_state_map ON agent_hq_session_state_map.thread_state = agent_hq_thread.state WHERE agent_hq_thread.provider = ? AND agent_hq_thread.native_id = ?", provider, nativeId);
71
71
  const observed = () => this.__statements.get('SELECT state FROM agent_hq_observed_session WHERE provider = ? AND native_id = ?', provider, nativeId);
72
- // a session HQ closed takes the state observed since, while a terminal continues it
73
- const row = record?.is_closed ? (observed() ?? record) : (record ?? observed());
72
+ // native status remains readable even when HQ no longer controls the session
73
+ const row = record?.uses_observation ? (observed() ?? record) : (record ?? observed());
74
74
  return row ? [{ nativeId, state: row.state }] : [];
75
75
  });
76
76
  }
package/docs/recovery.md CHANGED
@@ -32,6 +32,8 @@ The expanded name contract is still a breaking change for older consumers: 12.x
32
32
 
33
33
  ## Codex connection recovery
34
34
 
35
+ A quarantined session can still be monitored while a CLI runs it. Native observations update its live state without recovering ownership; the summary keeps uncertain ownership and its existing managed identity. Monitoring sends no input and leaves the control ledger and earlier queued entries unchanged.
36
+
35
37
  A shared Codex daemon restart can interrupt every managed Codex session's live connection while HQ stays running. The runtime rechecks only its previously controlled sessions with no action in flight and rejoins matching native turns without changing settings or repeating input. Confirmed completions advance their waiting queues; unacknowledged actions and changed turns remain uncertain. Native command handles may have been lost, so rejoining establishes session control rather than successful command execution. See [runtime lifecycle](../api/README.md#runtime-lifecycle).
36
38
 
37
39
  For a session left uncertain, stop its producing HQ runtime and inspect the native identity and competing controllers before explicit recovery in the next runtime. `thread-recover` with `recovery: { kind: 'resume' }` can rejoin its already known Codex identity even when the daemon still holds it idle or active. The verified current turn is bound before joining and checked again afterward; no settings or input are sent. Reissued approvals can then be answered explicitly. Earlier queued entries stay paused, uncertain input receipts stay unresolved, and unknown state or a turn changing during the join leaves control uncertain. Never use attach/create to replace a known native identity or replay work to recover control.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jg-agent-hq",
3
- "version": "13.1.2",
3
+ "version": "13.1.3",
4
4
  "description": "Native Claude Code and Codex session control, history, usage and recoverable local storage for Node.js apps.",
5
5
  "type": "module",
6
6
  "license": "MIT",