@shumkov/orchestra 0.10.18 → 0.10.20
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/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/codex-process.js +231 -29
- 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"
|
|
@@ -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(
|
|
@@ -469,6 +474,7 @@ class CodexProcess extends Process {
|
|
|
469
474
|
this.killPromise = null;
|
|
470
475
|
this.interruptPromise = null;
|
|
471
476
|
this.backgroundSettlementPromise = null;
|
|
477
|
+
this.backgroundOwner = null;
|
|
472
478
|
this.backgroundWatchdog = null;
|
|
473
479
|
this.backgroundWatchdogDeadlineAt = null;
|
|
474
480
|
this.backgroundWatchdogPromise = null;
|
|
@@ -612,6 +618,10 @@ class CodexProcess extends Process {
|
|
|
612
618
|
|| typeof this.client.request !== 'function'
|
|
613
619
|
|| typeof this.client.close !== 'function'
|
|
614
620
|
|| typeof this.client.waitForFault !== 'function'
|
|
621
|
+
|| (
|
|
622
|
+
this.hookVerifier !== null
|
|
623
|
+
&& typeof this.client.verifyHooks !== 'function'
|
|
624
|
+
)
|
|
615
625
|
) {
|
|
616
626
|
throw new TypeError(
|
|
617
627
|
'CodexProcess: clientFactory returned an invalid client',
|
|
@@ -619,6 +629,10 @@ class CodexProcess extends Process {
|
|
|
619
629
|
}
|
|
620
630
|
await this.client.start();
|
|
621
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);
|
|
622
636
|
this.state = 'AttachingThread';
|
|
623
637
|
this.attachingThreadId = existingSessionId;
|
|
624
638
|
const result = await this._withSettingsGate(async () => (
|
|
@@ -739,6 +753,19 @@ class CodexProcess extends Process {
|
|
|
739
753
|
}
|
|
740
754
|
}
|
|
741
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
|
+
|
|
742
769
|
async _verifyStartupRelease() {
|
|
743
770
|
if (!this.client) {
|
|
744
771
|
this.startupReleaseSafe = true;
|
|
@@ -1180,6 +1207,14 @@ class CodexProcess extends Process {
|
|
|
1180
1207
|
error.deliveryState = 'not-sent';
|
|
1181
1208
|
throw error;
|
|
1182
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
|
+
}
|
|
1183
1218
|
const assertTurnStartAdmission = () => {
|
|
1184
1219
|
if (
|
|
1185
1220
|
pending.startCancellationRequested
|
|
@@ -1796,20 +1831,29 @@ class CodexProcess extends Process {
|
|
|
1796
1831
|
this._clearBackgroundWatchdog();
|
|
1797
1832
|
const backgroundSettlement = this.backgroundSettlementPromise;
|
|
1798
1833
|
const pending = this.current;
|
|
1834
|
+
let backgroundOwnership = null;
|
|
1835
|
+
if (
|
|
1836
|
+
priorState === 'BackgroundWorking'
|
|
1837
|
+
|| priorState === 'BackgroundSettling'
|
|
1838
|
+
) {
|
|
1839
|
+
try {
|
|
1840
|
+
backgroundOwnership = this._requireBackgroundOwner();
|
|
1841
|
+
} catch (error) {
|
|
1842
|
+
await this._enterContainment(
|
|
1843
|
+
'background-ownership-unknown',
|
|
1844
|
+
error,
|
|
1845
|
+
);
|
|
1846
|
+
throw error;
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1799
1849
|
const controlDeadline = controlDeadlineOverride
|
|
1800
1850
|
?? pending?.deadlineAt
|
|
1801
|
-
??
|
|
1851
|
+
?? backgroundOwnership?.owner.deadlineAt
|
|
1802
1852
|
?? (Date.now() + this.interruptTimeoutMs + this.cleanupTimeoutMs);
|
|
1803
1853
|
let turnId = this.activeTurnId
|
|
1804
1854
|
?? pending?.turnId
|
|
1805
|
-
??
|
|
1806
|
-
|
|
1807
|
-
priorState === 'BackgroundWorking'
|
|
1808
|
-
|| priorState === 'BackgroundSettling'
|
|
1809
|
-
)
|
|
1810
|
-
? this.lastTerminal?.turnId
|
|
1811
|
-
: null
|
|
1812
|
-
);
|
|
1855
|
+
?? backgroundOwnership?.owner.turnId
|
|
1856
|
+
?? null;
|
|
1813
1857
|
if (pending && priorState === 'StartingTurn') {
|
|
1814
1858
|
pending.startCancellationRequested = true;
|
|
1815
1859
|
}
|
|
@@ -1899,6 +1943,7 @@ class CodexProcess extends Process {
|
|
|
1899
1943
|
const reuseBackgroundCleanup = (
|
|
1900
1944
|
cleanupProof?.turnId != null
|
|
1901
1945
|
&& cleanupProof.turnId === turnId
|
|
1946
|
+
&& cleanupProof.deadlineAt === backgroundOwnership?.owner.deadlineAt
|
|
1902
1947
|
);
|
|
1903
1948
|
let terminal = pending?.terminal
|
|
1904
1949
|
?? (
|
|
@@ -1993,6 +2038,7 @@ class CodexProcess extends Process {
|
|
|
1993
2038
|
turnId,
|
|
1994
2039
|
});
|
|
1995
2040
|
}
|
|
2041
|
+
this.backgroundOwner = null;
|
|
1996
2042
|
this.state = 'Stopped';
|
|
1997
2043
|
this.inFlight = false;
|
|
1998
2044
|
this.emit('codex-settled', this._lifecyclePayload('stopped', {
|
|
@@ -2055,6 +2101,8 @@ class CodexProcess extends Process {
|
|
|
2055
2101
|
|| this.state === 'FailedAmbiguous'
|
|
2056
2102
|
|| this.state === 'DurabilityBlocked'
|
|
2057
2103
|
) return;
|
|
2104
|
+
if (this.state !== 'Settling' || this.current !== pending) return;
|
|
2105
|
+
const probeState = this.state;
|
|
2058
2106
|
const page = await this.client.request(
|
|
2059
2107
|
'thread/backgroundTerminals/list',
|
|
2060
2108
|
{ threadId: this.providerSessionId, limit: 100 },
|
|
@@ -2063,9 +2111,11 @@ class CodexProcess extends Process {
|
|
|
2063
2111
|
},
|
|
2064
2112
|
);
|
|
2065
2113
|
this._assertLifecycle(lifecycle);
|
|
2114
|
+
if (this.state !== probeState || this.current !== pending) return;
|
|
2066
2115
|
if (page.count > 0 || page.nextCursor != null) {
|
|
2116
|
+
this._bindBackgroundOwnerFromPending(pending);
|
|
2067
2117
|
this._activateBackgroundOwnership();
|
|
2068
|
-
this._armBackgroundWatchdog(
|
|
2118
|
+
this._armBackgroundWatchdog();
|
|
2069
2119
|
if (this.threadStatusType === 'idle') {
|
|
2070
2120
|
this._scheduleBackgroundSettlement();
|
|
2071
2121
|
}
|
|
@@ -2073,12 +2123,130 @@ class CodexProcess extends Process {
|
|
|
2073
2123
|
}
|
|
2074
2124
|
|
|
2075
2125
|
_activateBackgroundOwnership() {
|
|
2126
|
+
this._requireBackgroundOwner();
|
|
2076
2127
|
if (!this.hasActiveBackgroundWork()) {
|
|
2077
2128
|
this._advanceActivityEpoch();
|
|
2078
2129
|
this.state = 'BackgroundWorking';
|
|
2079
2130
|
}
|
|
2080
2131
|
}
|
|
2081
2132
|
|
|
2133
|
+
_backgroundOwnershipError(message) {
|
|
2134
|
+
return processError(message, 'CODEX_BACKGROUND_OWNERSHIP_UNKNOWN');
|
|
2135
|
+
}
|
|
2136
|
+
|
|
2137
|
+
_ownerFromTerminal(terminal, { requireLive = false } = {}) {
|
|
2138
|
+
if (
|
|
2139
|
+
!terminal
|
|
2140
|
+
|| !Number.isSafeInteger(terminal.deadlineAt)
|
|
2141
|
+
|| terminal.deadlineAt < 1
|
|
2142
|
+
|| !['completed', 'interrupted', 'failed'].includes(terminal.status)
|
|
2143
|
+
|| (requireLive && terminal.deadlineAt <= Date.now())
|
|
2144
|
+
) {
|
|
2145
|
+
throw this._backgroundOwnershipError(
|
|
2146
|
+
'Codex background work had no exact live terminal owner',
|
|
2147
|
+
);
|
|
2148
|
+
}
|
|
2149
|
+
let turnId;
|
|
2150
|
+
try {
|
|
2151
|
+
turnId = boundedOpaqueId(terminal.turnId, 'background owner turnId');
|
|
2152
|
+
} catch {
|
|
2153
|
+
throw this._backgroundOwnershipError(
|
|
2154
|
+
'Codex background work had no exact live terminal owner',
|
|
2155
|
+
);
|
|
2156
|
+
}
|
|
2157
|
+
return Object.freeze({
|
|
2158
|
+
turnId,
|
|
2159
|
+
deadlineAt: terminal.deadlineAt,
|
|
2160
|
+
});
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
_bindBackgroundOwner(terminal, options = {}) {
|
|
2164
|
+
const owner = this._ownerFromTerminal(terminal, options);
|
|
2165
|
+
if (this.backgroundOwner) {
|
|
2166
|
+
if (
|
|
2167
|
+
!Object.isFrozen(this.backgroundOwner)
|
|
2168
|
+
|| this.backgroundOwner.turnId !== owner.turnId
|
|
2169
|
+
|| this.backgroundOwner.deadlineAt !== owner.deadlineAt
|
|
2170
|
+
) {
|
|
2171
|
+
throw this._backgroundOwnershipError(
|
|
2172
|
+
'Codex background owner changed before cleanup',
|
|
2173
|
+
);
|
|
2174
|
+
}
|
|
2175
|
+
return this.backgroundOwner;
|
|
2176
|
+
}
|
|
2177
|
+
this.backgroundCleanupProof = null;
|
|
2178
|
+
this.backgroundOwner = owner;
|
|
2179
|
+
return owner;
|
|
2180
|
+
}
|
|
2181
|
+
|
|
2182
|
+
_bindBackgroundOwnerFromPending(pending) {
|
|
2183
|
+
const terminal = pending?.terminal;
|
|
2184
|
+
if (
|
|
2185
|
+
pending !== this.current
|
|
2186
|
+
|| !terminal
|
|
2187
|
+
|| terminal !== this.lastTerminal
|
|
2188
|
+
|| terminal.turnId !== pending.turnId
|
|
2189
|
+
|| terminal.deadlineAt !== pending.deadlineAt
|
|
2190
|
+
) {
|
|
2191
|
+
throw this._backgroundOwnershipError(
|
|
2192
|
+
'Codex background registry had no exact pending terminal owner',
|
|
2193
|
+
);
|
|
2194
|
+
}
|
|
2195
|
+
return this._bindBackgroundOwner(terminal);
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
_captureBackgroundPromotionCandidate() {
|
|
2199
|
+
const terminal = this.lastTerminal;
|
|
2200
|
+
if (!terminal) return null;
|
|
2201
|
+
return Object.freeze({
|
|
2202
|
+
terminal,
|
|
2203
|
+
turnId: terminal.turnId,
|
|
2204
|
+
status: terminal.status,
|
|
2205
|
+
deadlineAt: terminal.deadlineAt,
|
|
2206
|
+
});
|
|
2207
|
+
}
|
|
2208
|
+
|
|
2209
|
+
_promoteBackgroundCandidateToOwner(admission) {
|
|
2210
|
+
const candidate = admission?.candidate;
|
|
2211
|
+
const terminal = this.lastTerminal;
|
|
2212
|
+
if (
|
|
2213
|
+
!admission
|
|
2214
|
+
|| this.activityEpoch !== admission.activityEpoch
|
|
2215
|
+
|| this.state !== admission.state
|
|
2216
|
+
|| this.current !== admission.current
|
|
2217
|
+
|| !candidate
|
|
2218
|
+
|| candidate.terminal !== terminal
|
|
2219
|
+
|| candidate.turnId !== terminal?.turnId
|
|
2220
|
+
|| candidate.status !== terminal?.status
|
|
2221
|
+
|| candidate.deadlineAt !== terminal?.deadlineAt
|
|
2222
|
+
) {
|
|
2223
|
+
throw this._backgroundOwnershipError(
|
|
2224
|
+
'Codex background status candidate changed before ownership',
|
|
2225
|
+
);
|
|
2226
|
+
}
|
|
2227
|
+
return this._bindBackgroundOwner(terminal, {
|
|
2228
|
+
requireLive: true,
|
|
2229
|
+
});
|
|
2230
|
+
}
|
|
2231
|
+
|
|
2232
|
+
_requireBackgroundOwner() {
|
|
2233
|
+
const owner = this.backgroundOwner;
|
|
2234
|
+
const terminal = this.lastTerminal;
|
|
2235
|
+
if (
|
|
2236
|
+
!owner
|
|
2237
|
+
|| !Object.isFrozen(owner)
|
|
2238
|
+
|| !terminal
|
|
2239
|
+
|| owner.turnId !== terminal.turnId
|
|
2240
|
+
|| owner.deadlineAt !== terminal.deadlineAt
|
|
2241
|
+
) {
|
|
2242
|
+
throw this._backgroundOwnershipError(
|
|
2243
|
+
'Codex background work had missing or mismatched ownership',
|
|
2244
|
+
);
|
|
2245
|
+
}
|
|
2246
|
+
this._ownerFromTerminal(terminal);
|
|
2247
|
+
return { owner, terminal };
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2082
2250
|
_qualificationFenceError() {
|
|
2083
2251
|
return processError(
|
|
2084
2252
|
'Codex clean-restart qualification fence changed',
|
|
@@ -2517,15 +2685,38 @@ class CodexProcess extends Process {
|
|
|
2517
2685
|
&& !this.current
|
|
2518
2686
|
&& this.state === 'Idle'
|
|
2519
2687
|
);
|
|
2688
|
+
const backgroundCandidate = activateBackground
|
|
2689
|
+
? this._captureBackgroundPromotionCandidate()
|
|
2690
|
+
: null;
|
|
2520
2691
|
if (activateBackground) this._advanceActivityEpoch();
|
|
2692
|
+
const backgroundAdmission = activateBackground
|
|
2693
|
+
? Object.freeze({
|
|
2694
|
+
candidate: backgroundCandidate,
|
|
2695
|
+
activityEpoch: this.activityEpoch,
|
|
2696
|
+
state: this.state,
|
|
2697
|
+
current: this.current,
|
|
2698
|
+
})
|
|
2699
|
+
: null;
|
|
2521
2700
|
await this._checkpoint('thread-status-changed', {
|
|
2522
2701
|
threadId: ownedThreadId,
|
|
2523
2702
|
statusType,
|
|
2524
2703
|
}, fence);
|
|
2525
2704
|
fence.assertActive?.();
|
|
2526
2705
|
this.threadStatusType = statusType;
|
|
2527
|
-
if (activateBackground
|
|
2706
|
+
if (activateBackground) {
|
|
2707
|
+
try {
|
|
2708
|
+
this._promoteBackgroundCandidateToOwner(backgroundAdmission);
|
|
2709
|
+
} catch (error) {
|
|
2710
|
+
await this._enterContainment(
|
|
2711
|
+
'background-ownership-unknown',
|
|
2712
|
+
error,
|
|
2713
|
+
null,
|
|
2714
|
+
fence,
|
|
2715
|
+
);
|
|
2716
|
+
return;
|
|
2717
|
+
}
|
|
2528
2718
|
this.state = 'BackgroundWorking';
|
|
2719
|
+
this._armBackgroundWatchdog();
|
|
2529
2720
|
} else if (
|
|
2530
2721
|
statusType === 'idle'
|
|
2531
2722
|
&& this.state === 'BackgroundWorking'
|
|
@@ -2827,19 +3018,13 @@ class CodexProcess extends Process {
|
|
|
2827
3018
|
|
|
2828
3019
|
async _settleNaturalBackgroundWork() {
|
|
2829
3020
|
const lifecycle = this._captureLifecycle();
|
|
2830
|
-
const terminal = this.lastTerminal;
|
|
2831
|
-
const deadline = terminal?.deadlineAt ?? Date.now();
|
|
2832
3021
|
try {
|
|
2833
3022
|
this._assertLifecycle(lifecycle);
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
'Codex background work had no owned terminal turn',
|
|
2837
|
-
'CODEX_BACKGROUND_OWNERSHIP_UNKNOWN',
|
|
2838
|
-
);
|
|
2839
|
-
}
|
|
3023
|
+
const { owner, terminal } = this._requireBackgroundOwner();
|
|
3024
|
+
const deadline = owner.deadlineAt;
|
|
2840
3025
|
await this._checkpoint('background-terminal-reconciled', {
|
|
2841
3026
|
threadId: this.providerSessionId,
|
|
2842
|
-
turnId:
|
|
3027
|
+
turnId: owner.turnId,
|
|
2843
3028
|
terminalStatus: terminal.status,
|
|
2844
3029
|
});
|
|
2845
3030
|
this._assertLifecycle(lifecycle);
|
|
@@ -2847,35 +3032,36 @@ class CodexProcess extends Process {
|
|
|
2847
3032
|
threadId: this.providerSessionId,
|
|
2848
3033
|
}, {
|
|
2849
3034
|
source: null,
|
|
2850
|
-
turnId:
|
|
3035
|
+
turnId: owner.turnId,
|
|
2851
3036
|
timeoutMs: Math.max(1, deadline - Date.now()),
|
|
2852
3037
|
});
|
|
2853
3038
|
this._assertLifecycle(lifecycle);
|
|
2854
3039
|
await this._checkpoint('background-clean-accepted', {
|
|
2855
3040
|
threadId: this.providerSessionId,
|
|
2856
|
-
turnId:
|
|
3041
|
+
turnId: owner.turnId,
|
|
2857
3042
|
});
|
|
2858
3043
|
this._assertLifecycle(lifecycle);
|
|
2859
3044
|
await this._waitForFreshEmptyRegistry(deadline, lifecycle);
|
|
2860
3045
|
this._assertLifecycle(lifecycle);
|
|
2861
3046
|
await this._checkpoint('background-empty-registry-observed', {
|
|
2862
3047
|
threadId: this.providerSessionId,
|
|
2863
|
-
turnId:
|
|
3048
|
+
turnId: owner.turnId,
|
|
2864
3049
|
});
|
|
2865
3050
|
this._assertLifecycle(lifecycle);
|
|
2866
3051
|
this.backgroundCleanupProof = Object.freeze({
|
|
2867
|
-
turnId:
|
|
3052
|
+
turnId: owner.turnId,
|
|
2868
3053
|
terminalStatus: terminal.status,
|
|
2869
3054
|
deadlineAt: deadline,
|
|
2870
3055
|
});
|
|
2871
3056
|
this._clearBackgroundWatchdog();
|
|
3057
|
+
this.backgroundOwner = null;
|
|
2872
3058
|
if (this.state === 'Quiescing') return this.backgroundCleanupProof;
|
|
2873
3059
|
this.state = 'Idle';
|
|
2874
3060
|
this.inFlight = false;
|
|
2875
3061
|
this.emit('codex-settled', this._lifecyclePayload(
|
|
2876
3062
|
'background-settled',
|
|
2877
3063
|
{
|
|
2878
|
-
turnId:
|
|
3064
|
+
turnId: owner.turnId,
|
|
2879
3065
|
terminalStatus: terminal.status,
|
|
2880
3066
|
trackedTerminalCleanupAccepted: true,
|
|
2881
3067
|
freshRegistryObservedEmpty: true,
|
|
@@ -2910,8 +3096,12 @@ class CodexProcess extends Process {
|
|
|
2910
3096
|
this.backgroundSettlementPromise.catch(() => {});
|
|
2911
3097
|
}
|
|
2912
3098
|
|
|
2913
|
-
_armBackgroundWatchdog(
|
|
2914
|
-
|
|
3099
|
+
_armBackgroundWatchdog() {
|
|
3100
|
+
const { owner } = this._requireBackgroundOwner();
|
|
3101
|
+
const { deadlineAt } = owner;
|
|
3102
|
+
const retirementReserveMs = (
|
|
3103
|
+
this.interruptTimeoutMs + this.cleanupTimeoutMs
|
|
3104
|
+
);
|
|
2915
3105
|
if (
|
|
2916
3106
|
this.backgroundWatchdog
|
|
2917
3107
|
&& this.backgroundWatchdogDeadlineAt === deadlineAt
|
|
@@ -2925,10 +3115,22 @@ class CodexProcess extends Process {
|
|
|
2925
3115
|
this.state !== 'BackgroundWorking'
|
|
2926
3116
|
&& this.state !== 'BackgroundSettling'
|
|
2927
3117
|
) return;
|
|
3118
|
+
let currentOwner;
|
|
3119
|
+
try {
|
|
3120
|
+
currentOwner = this._requireBackgroundOwner().owner;
|
|
3121
|
+
} catch (error) {
|
|
3122
|
+
this.backgroundWatchdogPromise = this._enterContainment(
|
|
3123
|
+
'background-ownership-unknown',
|
|
3124
|
+
error,
|
|
3125
|
+
).finally(() => {
|
|
3126
|
+
this.backgroundWatchdogPromise = null;
|
|
3127
|
+
});
|
|
3128
|
+
return;
|
|
3129
|
+
}
|
|
2928
3130
|
if (!this.interruptPromise) {
|
|
2929
3131
|
this.interruptPromise = this._interrupt(
|
|
2930
3132
|
'timeout',
|
|
2931
|
-
|
|
3133
|
+
currentOwner.deadlineAt,
|
|
2932
3134
|
);
|
|
2933
3135
|
}
|
|
2934
3136
|
this.backgroundWatchdogPromise = this.interruptPromise
|
|
@@ -2946,7 +3148,7 @@ class CodexProcess extends Process {
|
|
|
2946
3148
|
.finally(() => {
|
|
2947
3149
|
this.backgroundWatchdogPromise = null;
|
|
2948
3150
|
});
|
|
2949
|
-
}, Math.max(1, deadlineAt - Date.now()));
|
|
3151
|
+
}, Math.max(1, deadlineAt - retirementReserveMs - Date.now()));
|
|
2950
3152
|
this.backgroundWatchdog.unref?.();
|
|
2951
3153
|
}
|
|
2952
3154
|
|
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.20",
|
|
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",
|