@shumkov/orchestra 0.10.19 → 0.10.21
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/docs/2026-08-17-001-u23-hook-trust-characterization-findings.md +964 -0
- package/docs/FOLDED_REPLY_ATTRIBUTION_SPEC.md +317 -0
- package/lib/codex/app-server-client.js +444 -125
- package/lib/codex/preflight.js +43 -0
- package/lib/codex/protocol-schema.json +8 -0
- package/lib/process/cli-process.js +134 -40
- package/lib/process/codex-process.js +34 -0
- package/lib/process/factory.js +5 -0
- package/package.json +1 -1
package/lib/codex/preflight.js
CHANGED
|
@@ -6,6 +6,7 @@ const path = require('node:path');
|
|
|
6
6
|
const {
|
|
7
7
|
CodexAppServerClient,
|
|
8
8
|
buildCodexAppServerEnv,
|
|
9
|
+
normalizeCodexHookManifest,
|
|
9
10
|
protocolSchema,
|
|
10
11
|
resolveCodexTargetPin,
|
|
11
12
|
} = require('./app-server-client');
|
|
@@ -64,10 +65,14 @@ const EXPECTED_PROFILE_KEYS = new Set([
|
|
|
64
65
|
'effort',
|
|
65
66
|
'sessionLauncher',
|
|
66
67
|
'sessionLauncherSha256',
|
|
68
|
+
'hookManifest',
|
|
69
|
+
'hookArtifactsSha256',
|
|
67
70
|
]);
|
|
68
71
|
const OPTIONAL_EXPECTED_PROFILE_KEYS = new Set([
|
|
69
72
|
'sessionLauncher',
|
|
70
73
|
'sessionLauncherSha256',
|
|
74
|
+
'hookManifest',
|
|
75
|
+
'hookArtifactsSha256',
|
|
71
76
|
]);
|
|
72
77
|
const PREFLIGHT_RESULT_KEYS = new Set([
|
|
73
78
|
'runtime',
|
|
@@ -301,6 +306,24 @@ function normalizeExpectedProfile(value) {
|
|
|
301
306
|
if (sessionLauncher !== null && !path.isAbsolute(sessionLauncher)) {
|
|
302
307
|
throw new TypeError('Codex preflight session launcher must be absolute');
|
|
303
308
|
}
|
|
309
|
+
const suppliedManifest = source.hookManifest ?? null;
|
|
310
|
+
const suppliedArtifactsSha256 = source.hookArtifactsSha256 ?? null;
|
|
311
|
+
if ((suppliedManifest === null) !== (suppliedArtifactsSha256 === null)) {
|
|
312
|
+
throw new TypeError(
|
|
313
|
+
'Codex preflight hook manifest and artifact digest must be paired',
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
const hookManifest = suppliedManifest === null
|
|
317
|
+
? null
|
|
318
|
+
: normalizeCodexHookManifest(suppliedManifest);
|
|
319
|
+
const hookArtifactsSha256 = suppliedArtifactsSha256 === null
|
|
320
|
+
? null
|
|
321
|
+
: requiredSha256(suppliedArtifactsSha256, 'hookArtifactsSha256');
|
|
322
|
+
if (hookManifest !== null && hookManifest.ownedCwd !== cwd) {
|
|
323
|
+
throw new TypeError(
|
|
324
|
+
'Codex preflight hook manifest must own the configured workspace',
|
|
325
|
+
);
|
|
326
|
+
}
|
|
304
327
|
if (
|
|
305
328
|
target !== targetReceipt.target
|
|
306
329
|
|| binarySha256 !== targetReceipt.binarySha256
|
|
@@ -409,6 +432,9 @@ function normalizeExpectedProfile(value) {
|
|
|
409
432
|
effort: requiredString(source.effort, 'effort'),
|
|
410
433
|
sessionLauncher,
|
|
411
434
|
sessionLauncherSha256,
|
|
435
|
+
// Absent, not null: a profile prepared without hooks keeps exactly the key
|
|
436
|
+
// set, object model and fingerprint it had before hooks existed.
|
|
437
|
+
...(hookManifest === null ? {} : { hookManifest, hookArtifactsSha256 }),
|
|
412
438
|
};
|
|
413
439
|
|
|
414
440
|
assertExpectedPolicy(profile);
|
|
@@ -700,6 +726,12 @@ function staticAttestation(profile, configRead, requirements, profiles) {
|
|
|
700
726
|
sessionLauncherPathSha256: digest(profile.sessionLauncher),
|
|
701
727
|
sessionLauncherSha256: profile.sessionLauncherSha256,
|
|
702
728
|
}),
|
|
729
|
+
// Hook material joins the identity only when a manifest is carried, so a
|
|
730
|
+
// profile prepared without hooks keeps the fingerprint it always had.
|
|
731
|
+
...(profile.hookManifest == null ? {} : {
|
|
732
|
+
hookManifestSha256: digest(profile.hookManifest),
|
|
733
|
+
hookArtifactsSha256: profile.hookArtifactsSha256,
|
|
734
|
+
}),
|
|
703
735
|
credentialHomeSha256: digest(profile.codexHome),
|
|
704
736
|
workspaceSha256: digest(profile.cwd),
|
|
705
737
|
allowlistedEnvironmentFingerprint:
|
|
@@ -942,6 +974,10 @@ async function runPreflight(profile, client) {
|
|
|
942
974
|
permissionProfiles,
|
|
943
975
|
} = await readAndAssertStaticPolicy(profile, client);
|
|
944
976
|
|
|
977
|
+
if (profile.hookManifest != null) {
|
|
978
|
+
await client.verifyHooks({ phase: 'trusted' });
|
|
979
|
+
}
|
|
980
|
+
|
|
945
981
|
const account = plainObject(
|
|
946
982
|
await client.request('account/read', { refreshToken: false }),
|
|
947
983
|
'account result',
|
|
@@ -1034,6 +1070,9 @@ async function preflightCodexRuntime(
|
|
|
1034
1070
|
cwd: profile.cwd,
|
|
1035
1071
|
codexHome: profile.codexHome,
|
|
1036
1072
|
env: profile.env,
|
|
1073
|
+
...(profile.hookManifest == null
|
|
1074
|
+
? {}
|
|
1075
|
+
: { hookManifest: profile.hookManifest }),
|
|
1037
1076
|
expectedConfigSha256: profile.ownedConfigSha256,
|
|
1038
1077
|
onNotification: async () => {
|
|
1039
1078
|
throw new CodexPreflightError(
|
|
@@ -1052,6 +1091,10 @@ async function preflightCodexRuntime(
|
|
|
1052
1091
|
|| typeof client.request !== 'function'
|
|
1053
1092
|
|| typeof client.close !== 'function'
|
|
1054
1093
|
|| typeof client.waitForFault !== 'function'
|
|
1094
|
+
|| (
|
|
1095
|
+
profile.hookManifest != null
|
|
1096
|
+
&& typeof client.verifyHooks !== 'function'
|
|
1097
|
+
)
|
|
1055
1098
|
) {
|
|
1056
1099
|
throw new TypeError('Codex preflight clientFactory returned an invalid client');
|
|
1057
1100
|
}
|
|
@@ -106,6 +106,14 @@
|
|
|
106
106
|
"optional": [],
|
|
107
107
|
"stateChanging": true
|
|
108
108
|
},
|
|
109
|
+
"hooks/list": {
|
|
110
|
+
"required": [
|
|
111
|
+
"cwds"
|
|
112
|
+
],
|
|
113
|
+
"optional": [],
|
|
114
|
+
"internal": true,
|
|
115
|
+
"stateChanging": false
|
|
116
|
+
},
|
|
109
117
|
"thread/backgroundTerminals/list": {
|
|
110
118
|
"required": [
|
|
111
119
|
"threadId"
|
|
@@ -1990,7 +1990,6 @@ class CliProcess extends Process {
|
|
|
1990
1990
|
if (chatIdMatches && Array.isArray(args.consumed_turn_ids) && args.consumed_turn_ids.length) {
|
|
1991
1991
|
this._ledgerAckConsumed(
|
|
1992
1992
|
args.consumed_turn_ids.filter((x) => typeof x === 'string'),
|
|
1993
|
-
typeof args.text === 'string' ? args.text : '',
|
|
1994
1993
|
);
|
|
1995
1994
|
} else if (chatIdMatches && msg.name === 'reply' && 'consumed_turn_ids' in args) {
|
|
1996
1995
|
this._lastAckFieldAt = Date.now(); // field present but empty — contract observed
|
|
@@ -2143,27 +2142,25 @@ class CliProcess extends Process {
|
|
|
2143
2142
|
return;
|
|
2144
2143
|
}
|
|
2145
2144
|
|
|
2146
|
-
//
|
|
2147
|
-
//
|
|
2148
|
-
//
|
|
2149
|
-
//
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2145
|
+
// Resolve ownership once so dispatch, quoting, and pending-turn bookkeeping
|
|
2146
|
+
// cannot disagree. A folded input can own the model's echoed turn_id while
|
|
2147
|
+
// the one live primary owns the combined answer; consumed_turn_ids is the
|
|
2148
|
+
// explicit proof that connects those two identities.
|
|
2149
|
+
const replyAttribution = this._resolveReplyAttribution(
|
|
2150
|
+
args.turn_id,
|
|
2151
|
+
args.consumed_turn_ids,
|
|
2152
|
+
);
|
|
2153
|
+
|
|
2154
|
+
// Resolve the reply's originating message so the dispatcher has a target
|
|
2155
|
+
// for reactions and reply-quoting. Anything unattributable stays null; it
|
|
2156
|
+
// must never react to or quote an unrelated message.
|
|
2154
2157
|
//
|
|
2155
2158
|
// Review F1: quote only the FIRST delivered reply per turn. On SDK,
|
|
2156
2159
|
// deliverReplies fires once per turn → one quote; the channels dispatcher
|
|
2157
2160
|
// fires per reply tool call, and an N-reply turn must not produce N
|
|
2158
2161
|
// bubbles all quoting the same user message.
|
|
2159
2162
|
let sourceMsgId = null;
|
|
2160
|
-
|
|
2161
|
-
if (args.turn_id && this.inputLedger.has(args.turn_id)) {
|
|
2162
|
-
sourceEntry = this.inputLedger.get(args.turn_id);
|
|
2163
|
-
} else if (this.pendingTurns.size === 1) {
|
|
2164
|
-
const [[onlyTurnId]] = this.pendingTurns;
|
|
2165
|
-
sourceEntry = this.inputLedger.get(onlyTurnId) || null;
|
|
2166
|
-
}
|
|
2163
|
+
const sourceEntry = replyAttribution.sourceEntry;
|
|
2167
2164
|
if (sourceEntry && !sourceEntry._quoteUsed) {
|
|
2168
2165
|
// Message ids may be numeric (Telegram) OR opaque strings (WhatsApp stanza ids).
|
|
2169
2166
|
// A purely-numeric id coerces to Number so number-based quote/reaction call sites
|
|
@@ -2194,6 +2191,12 @@ class CliProcess extends Process {
|
|
|
2194
2191
|
const attempt = argumentHash
|
|
2195
2192
|
? this._registerDeliveryAttempt(msg.tool_call_id, argumentHash)
|
|
2196
2193
|
: null;
|
|
2194
|
+
const trackedReplyDeliveries = msg.name === 'reply'
|
|
2195
|
+
? this._beginReplyDeliveries([
|
|
2196
|
+
replyAttribution.effectiveTurnId,
|
|
2197
|
+
...(Array.isArray(args.consumed_turn_ids) ? args.consumed_turn_ids : []),
|
|
2198
|
+
])
|
|
2199
|
+
: [];
|
|
2197
2200
|
const dispatchPromise = Promise.resolve().then(() => this.toolDispatcher({
|
|
2198
2201
|
sessionKey: this.sessionKey,
|
|
2199
2202
|
chatId: this.chatId,
|
|
@@ -2203,7 +2206,7 @@ class CliProcess extends Process {
|
|
|
2203
2206
|
// The turn this call names. A consumer holding per-turn state needs it
|
|
2204
2207
|
// to refuse a late reply from an earlier turn — without it, a delayed
|
|
2205
2208
|
// reply lands on whatever turn the session is holding NOW.
|
|
2206
|
-
turnId:
|
|
2209
|
+
turnId: replyAttribution.effectiveTurnId,
|
|
2207
2210
|
interim: args.interim === true, // status/progress reply — not the turn's answer
|
|
2208
2211
|
files: args.files,
|
|
2209
2212
|
messageId: args.message_id, // 0.13: edit_message target bubble
|
|
@@ -2234,6 +2237,7 @@ class CliProcess extends Process {
|
|
|
2234
2237
|
if (attempt) this._settleDeliveryAttempt(attempt, result?.ok ? 'sent' : 'failed');
|
|
2235
2238
|
} catch (err) {
|
|
2236
2239
|
if (attempt) this._settleDeliveryAttempt(attempt, 'failed');
|
|
2240
|
+
this._finishReplyDeliveries(trackedReplyDeliveries);
|
|
2237
2241
|
this._writeToBridge({
|
|
2238
2242
|
kind: 'tool_ack',
|
|
2239
2243
|
tool_call_id: msg.tool_call_id,
|
|
@@ -2301,6 +2305,10 @@ class CliProcess extends Process {
|
|
|
2301
2305
|
});
|
|
2302
2306
|
}
|
|
2303
2307
|
|
|
2308
|
+
if (result?.ok && msg.name === 'reply') {
|
|
2309
|
+
this._recordConsumedReplyDelivery(args.consumed_turn_ids, args.text);
|
|
2310
|
+
}
|
|
2311
|
+
|
|
2304
2312
|
// Review #16 + C9: only record the reply for pending-turn resolution when
|
|
2305
2313
|
// the dispatcher actually delivered AND the text is a non-empty string.
|
|
2306
2314
|
// Review P1 #4: route by turn_id (echoed from inbound <channel> meta) so
|
|
@@ -2309,8 +2317,21 @@ class CliProcess extends Process {
|
|
|
2309
2317
|
// fall back to the SINGLE pending turn if exactly one exists, else the
|
|
2310
2318
|
// oldest pending — log a warning either way so we can audit drift.
|
|
2311
2319
|
if (msg.name === 'reply' && result?.ok && typeof args.text === 'string' && args.text.length > 0) {
|
|
2312
|
-
this._recordReplyForPendingTurn(
|
|
2320
|
+
const recorded = this._recordReplyForPendingTurn(
|
|
2321
|
+
args.text,
|
|
2322
|
+
replyAttribution.effectiveTurnId,
|
|
2323
|
+
args.interim === true,
|
|
2324
|
+
);
|
|
2325
|
+
if (recorded && replyAttribution.foldSource) {
|
|
2326
|
+
this._logEvent('cli-fold-reply-attributed', {
|
|
2327
|
+
echoed_turn_id: replyAttribution.echoedTurnId,
|
|
2328
|
+
effective_turn_id: replyAttribution.effectiveTurnId,
|
|
2329
|
+
source: replyAttribution.foldSource,
|
|
2330
|
+
interim: args.interim === true,
|
|
2331
|
+
});
|
|
2332
|
+
}
|
|
2313
2333
|
}
|
|
2334
|
+
this._finishReplyDeliveries(trackedReplyDeliveries);
|
|
2314
2335
|
}
|
|
2315
2336
|
|
|
2316
2337
|
/**
|
|
@@ -2431,6 +2452,41 @@ class CliProcess extends Process {
|
|
|
2431
2452
|
ack();
|
|
2432
2453
|
}
|
|
2433
2454
|
|
|
2455
|
+
/**
|
|
2456
|
+
* Resolve the turn and ledger entry that own a reply without changing the
|
|
2457
|
+
* model-supplied arguments. Fold remapping requires both an echoed fold id
|
|
2458
|
+
* and exactly one live pending id in consumed_turn_ids; every other shape
|
|
2459
|
+
* preserves the existing late/unknown reply behavior.
|
|
2460
|
+
*/
|
|
2461
|
+
_resolveReplyAttribution(replyTurnId, consumedTurnIds) {
|
|
2462
|
+
const echoedTurnId = typeof replyTurnId === 'string' ? replyTurnId : null;
|
|
2463
|
+
let effectiveTurnId = echoedTurnId;
|
|
2464
|
+
let sourceEntry = echoedTurnId ? (this.inputLedger.get(echoedTurnId) || null) : null;
|
|
2465
|
+
let foldSource = null;
|
|
2466
|
+
|
|
2467
|
+
if (sourceEntry
|
|
2468
|
+
&& !this.pendingTurns.has(echoedTurnId)
|
|
2469
|
+
&& (sourceEntry.source === 'autosteer' || sourceEntry.source === 'edit-fold')
|
|
2470
|
+
&& Array.isArray(consumedTurnIds)
|
|
2471
|
+
&& consumedTurnIds.includes(echoedTurnId)) {
|
|
2472
|
+
const pendingIds = new Set(
|
|
2473
|
+
consumedTurnIds.filter(id => typeof id === 'string' && this.pendingTurns.has(id)),
|
|
2474
|
+
);
|
|
2475
|
+
if (pendingIds.size === 1) {
|
|
2476
|
+
[effectiveTurnId] = pendingIds;
|
|
2477
|
+
foldSource = sourceEntry.source;
|
|
2478
|
+
sourceEntry = this.inputLedger.get(effectiveTurnId) || null;
|
|
2479
|
+
}
|
|
2480
|
+
}
|
|
2481
|
+
|
|
2482
|
+
if (!sourceEntry && this.pendingTurns.size === 1) {
|
|
2483
|
+
const [[onlyTurnId]] = this.pendingTurns;
|
|
2484
|
+
sourceEntry = this.inputLedger.get(onlyTurnId) || null;
|
|
2485
|
+
}
|
|
2486
|
+
|
|
2487
|
+
return { echoedTurnId, effectiveTurnId, sourceEntry, foldSource };
|
|
2488
|
+
}
|
|
2489
|
+
|
|
2434
2490
|
/**
|
|
2435
2491
|
* Route a reply text to the pending turn it belongs to.
|
|
2436
2492
|
*
|
|
@@ -2456,7 +2512,7 @@ class CliProcess extends Process {
|
|
|
2456
2512
|
backend: this.backend,
|
|
2457
2513
|
alreadyDelivered: true,
|
|
2458
2514
|
});
|
|
2459
|
-
return;
|
|
2515
|
+
return false;
|
|
2460
2516
|
}
|
|
2461
2517
|
let target = null;
|
|
2462
2518
|
if (replyTurnId && this.pendingTurns.has(replyTurnId)) {
|
|
@@ -2498,7 +2554,7 @@ class CliProcess extends Process {
|
|
|
2498
2554
|
pending_turn_ids: pendingIds,
|
|
2499
2555
|
reply_len: text.length,
|
|
2500
2556
|
});
|
|
2501
|
-
return;
|
|
2557
|
+
return false;
|
|
2502
2558
|
}
|
|
2503
2559
|
// No pending turns at all → emit canonical 'autonomous-assistant-message'
|
|
2504
2560
|
// event so polygram's autonomous-msg path (sdk/callbacks.js
|
|
@@ -2520,7 +2576,7 @@ class CliProcess extends Process {
|
|
|
2520
2576
|
backend: this.backend,
|
|
2521
2577
|
alreadyDelivered: true,
|
|
2522
2578
|
});
|
|
2523
|
-
return;
|
|
2579
|
+
return false;
|
|
2524
2580
|
}
|
|
2525
2581
|
|
|
2526
2582
|
target.replies.push(text);
|
|
@@ -2546,7 +2602,7 @@ class CliProcess extends Process {
|
|
|
2546
2602
|
this._logEvent('cli-reply-cap-noted', { reply_count: target.replyCount });
|
|
2547
2603
|
}
|
|
2548
2604
|
this._noteActivity('reply');
|
|
2549
|
-
return;
|
|
2605
|
+
return true;
|
|
2550
2606
|
}
|
|
2551
2607
|
|
|
2552
2608
|
// ── Legacy (rung 3, hook stream never came up): pre-D1 path, byte-identical ──
|
|
@@ -2576,6 +2632,7 @@ class CliProcess extends Process {
|
|
|
2576
2632
|
} else {
|
|
2577
2633
|
target.quietTimer = setTimeout(() => this._resolveTurn(target._turnId), this.turnQuietMs);
|
|
2578
2634
|
}
|
|
2635
|
+
return true;
|
|
2579
2636
|
}
|
|
2580
2637
|
|
|
2581
2638
|
// ─── 0.13 D2: InputLedger ──────────────────────────────────────────
|
|
@@ -2624,16 +2681,8 @@ class CliProcess extends Process {
|
|
|
2624
2681
|
if (e._watchdogTimer) { clearTimeout(e._watchdogTimer); e._watchdogTimer = null; }
|
|
2625
2682
|
}
|
|
2626
2683
|
|
|
2627
|
-
/**
|
|
2628
|
-
|
|
2629
|
-
* `consumingText` is the text the consuming reply actually delivered; it is
|
|
2630
|
-
* recorded on each consumed pending so _finalizeTurn can tell a genuine fold
|
|
2631
|
-
* (the answer rode the sibling reply) from an early ack that DIDN'T carry the
|
|
2632
|
-
* answer (prod 2026-06-13: a 294-char "Researching now…" ack, then the real
|
|
2633
|
-
* answer arrived as Stop-fallback text — which must NOT be suppressed).
|
|
2634
|
-
* See docs/0.13-consumed-ack-stop-fallback-drop-spec.md.
|
|
2635
|
-
*/
|
|
2636
|
-
_ledgerAckConsumed(ids, consumingText = '') {
|
|
2684
|
+
/** A reply carried consumed_turn_ids — acknowledge every known input id. */
|
|
2685
|
+
_ledgerAckConsumed(ids) {
|
|
2637
2686
|
this._lastAckFieldAt = Date.now();
|
|
2638
2687
|
for (const id of ids) {
|
|
2639
2688
|
const e = this.inputLedger.get(id);
|
|
@@ -2650,14 +2699,6 @@ class CliProcess extends Process {
|
|
|
2650
2699
|
const pending = this.pendingTurns.get(id);
|
|
2651
2700
|
if (pending) {
|
|
2652
2701
|
pending._consumedAcked = true;
|
|
2653
|
-
// Remember WHAT the consuming reply delivered for this turn. The last
|
|
2654
|
-
// ack wins; a longer subsequent ack is the safer record (more likely to
|
|
2655
|
-
// actually contain the answer). _finalizeTurn only suppresses a
|
|
2656
|
-
// Stop-fallback finalize when its text matches this.
|
|
2657
|
-
if (typeof consumingText === 'string'
|
|
2658
|
-
&& consumingText.length > (pending._consumedByText?.length || 0)) {
|
|
2659
|
-
pending._consumedByText = consumingText;
|
|
2660
|
-
}
|
|
2661
2702
|
// The ack itself flips rung-2 eligibility on — arm now. (The turn's
|
|
2662
2703
|
// last _noteActivity ran BEFORE this flag was set, so without this
|
|
2663
2704
|
// a quiet tail would never re-arm and the turn would sit until a
|
|
@@ -2667,6 +2708,51 @@ class CliProcess extends Process {
|
|
|
2667
2708
|
}
|
|
2668
2709
|
}
|
|
2669
2710
|
|
|
2711
|
+
/** Record text only after the dispatcher confirms that the consuming reply arrived. */
|
|
2712
|
+
_recordConsumedReplyDelivery(ids, consumingText) {
|
|
2713
|
+
if (!Array.isArray(ids) || typeof consumingText !== 'string') return;
|
|
2714
|
+
for (const id of new Set(ids)) {
|
|
2715
|
+
const pending = this.pendingTurns.get(id);
|
|
2716
|
+
if (pending && consumingText.length > (pending._consumedByText?.length || 0)) {
|
|
2717
|
+
pending._consumedByText = consumingText;
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
}
|
|
2721
|
+
|
|
2722
|
+
/** Keep turn finalization behind the outcome of a consuming reply dispatch. */
|
|
2723
|
+
_beginReplyDeliveries(ids) {
|
|
2724
|
+
if (!Array.isArray(ids)) return [];
|
|
2725
|
+
const tracked = [];
|
|
2726
|
+
for (const turnId of new Set(ids)) {
|
|
2727
|
+
const pending = this.pendingTurns.get(turnId);
|
|
2728
|
+
if (!pending) continue;
|
|
2729
|
+
pending._replyDeliveriesInFlight = (pending._replyDeliveriesInFlight || 0) + 1;
|
|
2730
|
+
tracked.push({ turnId, pending });
|
|
2731
|
+
}
|
|
2732
|
+
return tracked;
|
|
2733
|
+
}
|
|
2734
|
+
|
|
2735
|
+
_finishReplyDeliveries(tracked) {
|
|
2736
|
+
for (const { turnId, pending } of tracked) {
|
|
2737
|
+
pending._replyDeliveriesInFlight = Math.max(
|
|
2738
|
+
0,
|
|
2739
|
+
(pending._replyDeliveriesInFlight || 0) - 1,
|
|
2740
|
+
);
|
|
2741
|
+
if (pending._replyDeliveriesInFlight > 0 || this.pendingTurns.get(turnId) !== pending) {
|
|
2742
|
+
continue;
|
|
2743
|
+
}
|
|
2744
|
+
if (pending._finalizeAfterReplyDelivery) {
|
|
2745
|
+
pending._finalizeAfterReplyDelivery = false;
|
|
2746
|
+
pending._timeoutAfterReplyDelivery = null;
|
|
2747
|
+
this._finalizeTurn(turnId);
|
|
2748
|
+
} else if (pending._timeoutAfterReplyDelivery) {
|
|
2749
|
+
const deferred = pending._timeoutAfterReplyDelivery;
|
|
2750
|
+
pending._timeoutAfterReplyDelivery = null;
|
|
2751
|
+
pending._fireTimeout?.(deferred.reason, deferred.probeResult);
|
|
2752
|
+
}
|
|
2753
|
+
}
|
|
2754
|
+
}
|
|
2755
|
+
|
|
2670
2756
|
_clearLedgerTimers() {
|
|
2671
2757
|
for (const e of this.inputLedger.values()) {
|
|
2672
2758
|
if (e._dropTimer) { clearTimeout(e._dropTimer); e._dropTimer = null; }
|
|
@@ -3157,6 +3243,10 @@ class CliProcess extends Process {
|
|
|
3157
3243
|
_finalizeTurn(turnId) {
|
|
3158
3244
|
const pending = this.pendingTurns.get(turnId);
|
|
3159
3245
|
if (!pending) return;
|
|
3246
|
+
if ((pending._replyDeliveriesInFlight || 0) > 0) {
|
|
3247
|
+
pending._finalizeAfterReplyDelivery = true;
|
|
3248
|
+
return;
|
|
3249
|
+
}
|
|
3160
3250
|
this.pendingTurns.delete(turnId);
|
|
3161
3251
|
// Review P1 #14: pop the matching pendingQueue entry too so downstream
|
|
3162
3252
|
// pm callbacks (sdk/callbacks.js context lookup) see a clean queue.
|
|
@@ -3328,6 +3418,10 @@ class CliProcess extends Process {
|
|
|
3328
3418
|
const fireTimeout = (reason, probeResult = null) => {
|
|
3329
3419
|
if (!this.pendingTurns.has(turnId)) return;
|
|
3330
3420
|
const pending = this.pendingTurns.get(turnId);
|
|
3421
|
+
if ((pending._replyDeliveriesInFlight || 0) > 0) {
|
|
3422
|
+
pending._timeoutAfterReplyDelivery = { reason, probeResult };
|
|
3423
|
+
return;
|
|
3424
|
+
}
|
|
3331
3425
|
// A question waits for the user: while an `ask` is open the turn must NOT
|
|
3332
3426
|
// time out and die mid-question. Defer — re-arm the absolute checkpoint and
|
|
3333
3427
|
// keep waiting; the question store's long safety backstop is the only bound
|
|
@@ -280,6 +280,7 @@ class CodexProcess extends Process {
|
|
|
280
280
|
cwd,
|
|
281
281
|
clientFactory,
|
|
282
282
|
staticPolicyAttestor = async () => {},
|
|
283
|
+
hookVerifier = null,
|
|
283
284
|
checkpointSink,
|
|
284
285
|
hostIdentity,
|
|
285
286
|
bootSessionIdentity,
|
|
@@ -318,6 +319,9 @@ class CodexProcess extends Process {
|
|
|
318
319
|
'CodexProcess: staticPolicyAttestor must be a function',
|
|
319
320
|
);
|
|
320
321
|
}
|
|
322
|
+
if (hookVerifier !== null && typeof hookVerifier !== 'function') {
|
|
323
|
+
throw new TypeError('CodexProcess: hookVerifier must be a function');
|
|
324
|
+
}
|
|
321
325
|
if (typeof generationIdFactory !== 'function') {
|
|
322
326
|
throw new TypeError('CodexProcess: generationIdFactory must be a function');
|
|
323
327
|
}
|
|
@@ -353,6 +357,7 @@ class CodexProcess extends Process {
|
|
|
353
357
|
|
|
354
358
|
this.clientFactory = clientFactory;
|
|
355
359
|
this.staticPolicyAttestor = staticPolicyAttestor;
|
|
360
|
+
this.hookVerifier = hookVerifier;
|
|
356
361
|
this.checkpointSink = checkpointSink;
|
|
357
362
|
this.hostIdentity = nonEmptyString(hostIdentity, 'hostIdentity');
|
|
358
363
|
this.bootSessionIdentity = nonEmptyString(
|
|
@@ -613,6 +618,10 @@ class CodexProcess extends Process {
|
|
|
613
618
|
|| typeof this.client.request !== 'function'
|
|
614
619
|
|| typeof this.client.close !== 'function'
|
|
615
620
|
|| typeof this.client.waitForFault !== 'function'
|
|
621
|
+
|| (
|
|
622
|
+
this.hookVerifier !== null
|
|
623
|
+
&& typeof this.client.verifyHooks !== 'function'
|
|
624
|
+
)
|
|
616
625
|
) {
|
|
617
626
|
throw new TypeError(
|
|
618
627
|
'CodexProcess: clientFactory returned an invalid client',
|
|
@@ -620,6 +629,10 @@ class CodexProcess extends Process {
|
|
|
620
629
|
}
|
|
621
630
|
await this.client.start();
|
|
622
631
|
this._assertLifecycle(lifecycle);
|
|
632
|
+
// A resumed thread executes hooks exactly as a fresh one does, so trust
|
|
633
|
+
// is proven before either kind of attachment.
|
|
634
|
+
await this._verifyHookTrust();
|
|
635
|
+
this._assertLifecycle(lifecycle);
|
|
623
636
|
this.state = 'AttachingThread';
|
|
624
637
|
this.attachingThreadId = existingSessionId;
|
|
625
638
|
const result = await this._withSettingsGate(async () => (
|
|
@@ -740,6 +753,19 @@ class CodexProcess extends Process {
|
|
|
740
753
|
}
|
|
741
754
|
}
|
|
742
755
|
|
|
756
|
+
async _verifyHookTrust() {
|
|
757
|
+
if (!this.hookVerifier) return;
|
|
758
|
+
try {
|
|
759
|
+
await this.hookVerifier(this.client, 'trusted');
|
|
760
|
+
} catch (cause) {
|
|
761
|
+
throw processError(
|
|
762
|
+
'Codex hook trust could not be verified',
|
|
763
|
+
cause?.code ?? 'CODEX_HOOK_TRUST_UNVERIFIED',
|
|
764
|
+
{ cause },
|
|
765
|
+
);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
743
769
|
async _verifyStartupRelease() {
|
|
744
770
|
if (!this.client) {
|
|
745
771
|
this.startupReleaseSafe = true;
|
|
@@ -1181,6 +1207,14 @@ class CodexProcess extends Process {
|
|
|
1181
1207
|
error.deliveryState = 'not-sent';
|
|
1182
1208
|
throw error;
|
|
1183
1209
|
}
|
|
1210
|
+
// A long-lived session must not drift between attachment and its
|
|
1211
|
+
// next turn, so trust is re-proven for every turn.
|
|
1212
|
+
try {
|
|
1213
|
+
await this._verifyHookTrust();
|
|
1214
|
+
} catch (cause) {
|
|
1215
|
+
cause.deliveryState = 'not-sent';
|
|
1216
|
+
throw cause;
|
|
1217
|
+
}
|
|
1184
1218
|
const assertTurnStartAdmission = () => {
|
|
1185
1219
|
if (
|
|
1186
1220
|
pending.startCancellationRequested
|
package/lib/process/factory.js
CHANGED
|
@@ -352,6 +352,11 @@ function createCodexProcess({
|
|
|
352
352
|
staticPolicyAttestor: (client) => (
|
|
353
353
|
reattestCodexStaticPolicy(profile, client)
|
|
354
354
|
),
|
|
355
|
+
// Only a profile that carries hook material asks for verification; without
|
|
356
|
+
// it the session runs the request sequence it always ran.
|
|
357
|
+
...(profile.hookManifest == null ? {} : {
|
|
358
|
+
hookVerifier: (client, phase) => client.verifyHooks({ phase }),
|
|
359
|
+
}),
|
|
355
360
|
checkpointSink,
|
|
356
361
|
hostIdentity,
|
|
357
362
|
bootSessionIdentity,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shumkov/orchestra",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.21",
|
|
4
4
|
"description": "Transport-agnostic session engine for Claude Code and the native Codex app-server: spawn, steer, supervise, observe turns, and recover.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "index.js",
|