jg-agent-hq 13.5.8 → 13.5.9

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.5.9
4
+
5
+ - Retain Codex control when the same controller records a valid native recovery turn while rejoining after a connection loss or runtime restart. Reconcile the observed turn instead of quarantining it against the pre-join snapshot.
6
+ - Keep unverified and overlapping turn changes quarantined, earlier receipts unchanged and queued input paused, without repeating input, settings or native joins.
7
+
3
8
  ## 13.5.8
4
9
 
5
10
  - Keep informational Codex messages in the transcript and out of question observers when their async delivery carries no questions. Preserve validation for question bundles.
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. 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.
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 turn that changes during the join is retained only when the same controller's ordered native events already recorded that exact turn and state, with no action in flight. This reconciliation sends no input, repeats no join or events and does not release earlier queued input. Unverified, unobserved or overlapping transitions stay 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
 
@@ -57,12 +57,38 @@ export const createReattach = (context, dispatcher, events) => {
57
57
  return;
58
58
  own(thread.provider, nativeId);
59
59
  isOwned = true;
60
+ const liveKey = key(thread.provider, nativeId);
61
+ const owner = context.controlled.get(liveKey);
60
62
  const joined = await control.reattachThread(`${randomUUID()}:reattach`, nativeId, thread.workspaceRoot);
61
63
  assertKnownTurn(joined);
62
64
  const turnId = bound.thread.activeTurnId;
63
65
  const latest = joined.latestTurn;
64
- if ((latest?.nativeId ?? null) !== (read.latestTurn?.nativeId ?? null))
65
- agentUnavailable();
66
+ if ((latest?.nativeId ?? null) !== (read.latestTurn?.nativeId ?? null)) {
67
+ if (!latest)
68
+ agentUnavailable();
69
+ // Codex can interrupt the old turn and start its recovery while the join is in flight.
70
+ // Accept only the transition this same controller already recorded, without replaying it.
71
+ await state.lane;
72
+ const current = await store.call('reads', 'getThread', thread.id);
73
+ const recorded = await store.call('reads', 'getTurnRecord', thread.provider, nativeId);
74
+ const hasObservedTurn = latest.state === 'running'
75
+ ? current?.activeTurnId === latest.nativeId &&
76
+ recorded?.activeTurnId === latest.nativeId
77
+ : current?.activeTurnId === null &&
78
+ current.state === latest.state &&
79
+ recorded?.lastTurnId === latest.nativeId;
80
+ if (state.isClosing ||
81
+ state.isUnavailable ||
82
+ context.blockedProviders.has(thread.provider) ||
83
+ context.controlled.get(liveKey) !== owner ||
84
+ current?.ownership !== 'managed' ||
85
+ current.runtimeEpoch !== epoch ||
86
+ current.nativeId !== nativeId ||
87
+ recorded?.hasPendingAction !== false ||
88
+ !hasObservedTurn)
89
+ agentUnavailable();
90
+ return;
91
+ }
66
92
  if (turnId && latest?.nativeId === turnId && latest.state !== 'running') {
67
93
  const { state: turnState } = latest;
68
94
  live.completeTurn(key(thread.provider, nativeId), turnId);
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 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).
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. A turn changing during the join retains control only when the same controller has already recorded that exact native transition and state, with no action in flight. This reconciliation repeats no native operation or events and leaves earlier queued input paused. Unknown, unobserved or overlapping transitions stay 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.8",
3
+ "version": "13.5.9",
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",