jg-agent-hq 13.5.1 → 13.5.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,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 13.5.3
4
+
5
+ - Keep controller creation, adoption, resume and recovery in the existing preparation state until native readiness. Opening a session without input no longer reports a turn starting or completing, preventing false completion notifications. Genuine input and autonomous native turns retain their lifecycle events.
6
+ - Preserve the storage schema, action key, input receipts and queue admission without migration or replay.
7
+
8
+ ## 13.5.2
9
+
10
+ - Automatically rejoin previously managed Codex sessions after a shared connection loss or HQ restart when their verified latest native turn changed. Joining sends no input or settings, leaves earlier receipts unresolved and queues paused, and still refuses unverified state, pending submissions and turn changes during the join. Existing uncertain/external sessions still require explicit recovery.
11
+
3
12
  ## 13.5.1
4
13
 
5
14
  - Allow confirmed recovery of a known quarantined Codex identity in the current HQ runtime, including loaded active, idle and unloaded sessions, without restarting HQ. Verify the native turn before and after joining, retain paused queues and unresolved receipts, and send no settings or input. Healthy controls, pending dispatches and unverified identities remain refused.
package/README.md CHANGED
@@ -77,7 +77,7 @@ Native execution settings, instructions, skills and tool permissions remain with
77
77
 
78
78
  When Claude sends structurally valid evidence HQ cannot interpret, HQ suspends that session's controls and live interpretation while its native work continues. It closes the quarantined controller automatically only after fresh native idle, a known empty task snapshot and no unresolved requests establish that work has finished. Unknown evidence or an unanswered approval keeps it alive; confirmed `thread-close` with `interrupt: true` explicitly stops its owned process group, including unfinished background work. Identity and transport-integrity failures retain their shutdown handling.
79
79
 
80
- A quarantined known Codex identity offers confirmed recovery in the current runtime without an HQ restart. Recovery verifies the native turn before and after joining, sends no input or settings, keeps earlier queues paused and leaves uncertain receipts unresolved. Automatic reconnect still refuses changed turns; inspect the session and close competing clients before confirming recovery.
80
+ A quarantined known Codex identity offers confirmed recovery in the current runtime without an HQ restart. Recovery verifies the native turn before and after joining, sends no input or settings, keeps earlier queues paused and leaves uncertain receipts unresolved. Previously managed sessions also rejoin automatically after connection loss or runtime restart when their verified latest turn changed, without resolving earlier receipts or releasing old queues. Unknown state, pending submissions and a turn changing during the join still require inspection and explicit recovery. Native turn IDs do not prove exclusive control; keep other controlling clients closed.
81
81
 
82
82
  After verified owned-group shutdown and controller release, that conversation becomes managed and inactive without restarting HQ. Resume retains its native identity and history. Unacknowledged input stays uncertain, earlier queued work stays paused, and pending approvals expire. Unconfirmed shutdown or a competing native terminal keeps ownership uncertain; another controlled conversation remains usable. See [Claude connection recovery](docs/recovery.md#claude-connection-recovery).
83
83
 
package/api/README.md CHANGED
@@ -93,7 +93,7 @@ After optional `initializeHostStorage` completes, the runtime connects both nati
93
93
 
94
94
  The controller holds the state root's `runtime` lock for its lifetime, as [lock lifecycle](#lock-lifecycle) describes. A live, dead, incomplete or unverifiable existing holder prevents another controller from starting, apart from a dead holder the host's `isPreviousRuntimeStopped` check proves stopped. Before deliberate recovery, establish that the inspected host, its worker and native process group have stopped. Runtime shutdown sends interruption requests across the bounded Claude session cohort concurrently, then closes IPC connections, SQLite and owned native processes before releasing the `runtime` lock. Failed cleanup keeps the lock held. A new runtime receives a new epoch.
95
95
 
96
- Codex sessions outlive the runtime, because they run in Codex's shared app-server daemon. Shutdown leaves them running: it doesn't interrupt their turns, clean their background terminals or answer a request still waiting on one, and their managed records stay bound to the stopped runtime. At startup, the next runtime rejoins each one that had no action in flight, one at a time in the background. It reads the session's state and latest turn from the daemon, binds the record to its own epoch, then joins with `thread/resume` without settings. A turn still running stays the session's active turn. A turn that finished meanwhile completes its action's receipt, without a `result`, and lets a queued entry waiting on it advance. The daemon sends a still-waiting request again on joining, and it opens as a new approval in the new epoch. A session whose latest turn isn't the one the runtime recorded, because another client ran one, or that can't be read or joined, has its ownership marked uncertain for explicit recovery. A crash leaves the same bound records, so those sessions are rejoined too. A restored snapshot marks every session uncertain, so it never rejoins one. Claude sessions are processes the runtime owns: their turns stop with it, and they wait inactive for an explicit resume.
96
+ Codex sessions outlive the runtime, because they run in Codex's shared app-server daemon. Shutdown leaves them running: it doesn't interrupt their turns, clean their background terminals or answer a request still waiting on one, and their managed records stay bound to the stopped runtime. At startup, the next runtime rejoins each one that had no action in flight, one at a time in the background. It reads the session's state and latest turn from the daemon, binds the record to its own epoch, then joins with `thread/resume` without settings. A turn still running stays the session's active turn. A turn that finished meanwhile completes its action's receipt, without a `result`, and lets a queued entry waiting on it advance. The daemon sends a still-waiting request again on joining, and it opens as a new approval in the new epoch. If the verified latest turn changed, the runtime restores control of that previously managed identity without assigning its completion to the earlier action or releasing earlier queued input. This also covers native daemon recovery continuations. Native turn IDs do not establish exclusive control or distinguish another client's turn, so competing controllers must remain closed. A session that cannot be verified or whose turn changes during the join stays uncertain for explicit recovery. A crash leaves the same bound records, so those sessions are rejoined too. A restored snapshot marks every session uncertain, so it never rejoins one. Claude sessions are processes the runtime owns: their turns stop with it, and they wait inactive for an explicit resume.
97
97
 
98
98
  One Codex connection carries every managed Codex session, so a message the runtime can't validate never fails it. A notification about items, text deltas, token usage or a session's status that fails validation is dropped, and a session the runtime controls reports a gap in its live window; a status newer than the runtime knows makes the session `unknown`. A message over 33,554,432 bytes, including its continuation fragments, is skipped through its final fragment without being buffered; a request whose response was skipped fails at its 30-second deadline. A turn start, turn end or approval resolution that fails validation, or a skipped message, whose session can't be known so every session the runtime controls is affected, is made up for by reading the session from the daemon, one session at a time and outside the order other sessions' events keep:
99
99
 
@@ -405,6 +405,8 @@ Codex leaves `reasoning` items out as it reads them, so a Codex page can hold fe
405
405
 
406
406
  ## Session states
407
407
 
408
+ Controller setup uses the managed record's existing `creating` preparation state for creation, adoption, resume and recovery. Until native readiness its session state is unknown, rather than a running inference turn. Readiness alone records `opened`, never `turn-completed`; `starting` is reserved for an action that actually submits turn input. Verified existing turns and autonomous native output still record their actual lifecycle.
409
+
408
410
  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.
409
411
 
410
412
  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.
@@ -242,7 +242,7 @@ export class ControlAdmission extends ControlModel {
242
242
  }
243
243
  if (isCreating)
244
244
  phase = 'create';
245
- this._statements.run("UPDATE agent_hq_thread SET ownership = 'managed', runtime_epoch = ?, pending_action_id = ?, active_turn_id = NULL, state = ?, updated_at = ? WHERE id = ?", this._epoch, action.id, isCreating ? 'creating' : 'starting', now, threadId);
245
+ this._statements.run("UPDATE agent_hq_thread SET ownership = 'managed', runtime_epoch = ?, pending_action_id = ?, active_turn_id = NULL, state = 'creating', updated_at = ? WHERE id = ?", this._epoch, action.id, now, threadId);
246
246
  if (prepared.recoveryTurn !== undefined) {
247
247
  if (command.kind !== 'thread-recover' ||
248
248
  command.recovery.kind !== 'resume' ||
@@ -359,7 +359,7 @@ export class ControlAdmission extends ControlModel {
359
359
  this._conflict();
360
360
  const threadId = randomUUID();
361
361
  this._statements.run(`INSERT INTO agent_hq_thread (id, provider, native_id, name, workspace_root, ownership, runtime_epoch, pending_action_id, state, registration_action_id, created_at, updated_at)
362
- VALUES (?, ?, ?, ?, ?, 'managed', ?, ?, ?, ?, ?, ?)`, threadId, provider, nativeId ?? null, name, prepared.workspaceRoot, this._epoch, actionId, nativeId ? 'starting' : 'creating', actionId, now, now);
362
+ VALUES (?, ?, ?, ?, ?, 'managed', ?, ?, ?, ?, ?, ?)`, threadId, provider, nativeId ?? null, name, prepared.workspaceRoot, this._epoch, actionId, 'creating', actionId, now, now);
363
363
  this._statements.run('UPDATE agent_hq_action SET thread_id = ?, requested_name = ?, provider = ? WHERE id = ?', threadId, name, provider, actionId);
364
364
  if (prepared.input) {
365
365
  this._statements.run('UPDATE agent_hq_action SET queued_input = ? WHERE id = ?', JSON.stringify(prepared.input), actionId);
@@ -46,9 +46,9 @@ export declare class ControlLifecycle extends ControlModel {
46
46
  /**
47
47
  * Binds a kept or reconnecting session to this runtime, as its native process reports
48
48
  * it before the runtime rejoins it. A turn that finished meanwhile completes its action and lets
49
- * the session's queued input advance, which this runtime then delivers. A session whose latest
50
- * turn isn't the one HQ recorded, because another client started one, has its ownership marked
51
- * uncertain instead.
49
+ * the session's queued input advance, which this runtime then delivers. A different verified turn
50
+ * restores control without completing the earlier action or releasing its queued input.
51
+ * Unverifiable native state stays uncertain.
52
52
  * @returns The thread and whether its queue may advance, or null when it is no longer one to rejoin.
53
53
  * @throws
54
54
  * - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
@@ -107,9 +107,9 @@ export class ControlLifecycle extends ControlModel {
107
107
  /**
108
108
  * Binds a kept or reconnecting session to this runtime, as its native process reports
109
109
  * it before the runtime rejoins it. A turn that finished meanwhile completes its action and lets
110
- * the session's queued input advance, which this runtime then delivers. A session whose latest
111
- * turn isn't the one HQ recorded, because another client started one, has its ownership marked
112
- * uncertain instead.
110
+ * the session's queued input advance, which this runtime then delivers. A different verified turn
111
+ * restores control without completing the earlier action or releasing its queued input.
112
+ * Unverifiable native state stays uncertain.
113
113
  * @returns The thread and whether its queue may advance, or null when it is no longer one to rejoin.
114
114
  * @throws
115
115
  * - JG_AGENT_HQ_ACTION_CONFLICT: The HQ action conflicts with its recorded state
@@ -124,25 +124,34 @@ export class ControlLifecycle extends ControlModel {
124
124
  const now = Date.now();
125
125
  const latest = input.latestTurn;
126
126
  const isRunning = latest?.state === 'running';
127
- if ((latest?.nativeTurnId ?? null) !== (thread.active_turn_id ?? thread.last_turn_id) ||
128
- (isRunning && thread.active_turn_id === null)) {
127
+ const hasChangedTurn = (latest?.nativeTurnId ?? null) !== (thread.active_turn_id ?? thread.last_turn_id) ||
128
+ (isRunning && thread.active_turn_id === null);
129
+ if (hasChangedTurn && latest === null) {
129
130
  if (!input.isReconnect)
130
131
  this.__quarantine(thread.id, now);
131
132
  return { thread: this._managedThread(thread.id), canDispatchQueued: false };
132
133
  }
133
134
  this._availableSlot();
134
135
  // the turn HQ left running, when it has finished since
135
- const finished = thread.active_turn_id !== null && latest && latest.state !== 'running'
136
+ const finished = !hasChangedTurn && thread.active_turn_id !== null && latest && latest.state !== 'running'
136
137
  ? { nativeTurnId: latest.nativeTurnId, state: latest.state }
137
138
  : null;
138
- const state = isRunning
139
- ? thread.state === 'stopping'
140
- ? 'stopping'
141
- : 'running'
142
- : (finished?.state ??
143
- (input.isReconnect ? (thread.last_turn_state ?? 'idle') : thread.state));
144
- this._statements.run("UPDATE agent_hq_thread SET ownership = 'managed', runtime_epoch = ?, active_turn_id = ?, last_turn_id = ?, last_turn_state = ?, state = ?, updated_at = ? WHERE id = ?", this._epoch, isRunning ? latest.nativeTurnId : null, finished?.nativeTurnId ?? thread.last_turn_id, finished?.state ?? thread.last_turn_state, state, now, thread.id);
139
+ const state = hasChangedTurn
140
+ ? latest.state
141
+ : isRunning
142
+ ? thread.state === 'stopping'
143
+ ? 'stopping'
144
+ : 'running'
145
+ : (finished?.state ??
146
+ (input.isReconnect ? (thread.last_turn_state ?? 'idle') : thread.state));
147
+ this._statements.run("UPDATE agent_hq_thread SET ownership = 'managed', runtime_epoch = ?, active_turn_id = ?, last_turn_id = ?, last_turn_state = ?, state = ?, updated_at = ? WHERE id = ?", this._epoch, isRunning ? latest.nativeTurnId : null, hasChangedTurn && latest && !isRunning
148
+ ? latest.nativeTurnId
149
+ : (finished?.nativeTurnId ?? thread.last_turn_id), hasChangedTurn && latest && !isRunning
150
+ ? latest.state
151
+ : (finished?.state ?? thread.last_turn_state), state, now, thread.id);
145
152
  this._statements.run("UPDATE agent_hq_action SET runtime_epoch = ?, updated_at = ? WHERE thread_id = ? AND delivery = 'queued'", this._epoch, now, thread.id);
153
+ if (hasChangedTurn)
154
+ this._statements.run("UPDATE agent_hq_action SET queue_ready = 0 WHERE thread_id = ? AND delivery = 'queued'", thread.id);
146
155
  const canDispatchQueued = finished?.state === 'completed' && thread.state !== 'stopping';
147
156
  if (finished) {
148
157
  this._statements.run('UPDATE agent_hq_action SET completion = ?, updated_at = ? WHERE thread_id = ? AND native_turn_id = ? AND completion IS NULL', finished.state, now, thread.id, finished.nativeTurnId);
package/docs/recovery.md CHANGED
@@ -54,7 +54,7 @@ Async question subscriptions are ephemeral. Recreate the host's `observeAsyncInp
54
54
 
55
55
  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.
56
56
 
57
- 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).
57
+ 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 verified native turns without changing settings or repeating input. Matching-turn completions advance their waiting queues. A stable changed turn restores control while earlier receipts remain unresolved and earlier queues stay paused, including through later reconnects. Pending submissions are excluded, and unknown state or a turn changing during the join stays uncertain. The same policy applies to previously managed sessions retained at runtime restart; existing uncertain or externally discovered identities are never automatically adopted. Native turn IDs cannot distinguish a daemon continuation from another client's work or establish exclusive control, so keep competing controllers closed. 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).
58
58
 
59
59
  For a known Codex session left uncertain, inspect its native identity and close competing controllers before explicit recovery. The current HQ runtime can recover it once no action remains in flight; restarting HQ is unnecessary. Other recovery modes and providers retain their stopped-producing-runtime requirement. `thread-recover` with `recovery: { kind: 'resume' }` can rejoin its already known Codex identity whether unloaded or still held 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.
60
60
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jg-agent-hq",
3
- "version": "13.5.1",
3
+ "version": "13.5.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",