@shumkov/orchestra 0.10.14 → 0.10.16
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/lib/codex/app-server-client.js +165 -9
- package/lib/codex/preflight.js +27 -0
- package/lib/process/codex-process.js +343 -14
- package/lib/process/process-manager.js +480 -105
- package/package.json +1 -1
|
@@ -29,13 +29,26 @@
|
|
|
29
29
|
|
|
30
30
|
'use strict';
|
|
31
31
|
|
|
32
|
-
const { createHash } = require('node:crypto');
|
|
32
|
+
const { createHash, hash } = require('node:crypto');
|
|
33
33
|
|
|
34
34
|
const DEFAULT_BUDGET = 10; // total Σ cost (SDK cost=1, tmux cost=3)
|
|
35
35
|
const DEFAULT_LRU_WAIT_MS = 300_000;
|
|
36
36
|
const DEFAULT_CODEX_RETIREMENT_TIMEOUT_MS = 30_000;
|
|
37
37
|
const MAX_RUNTIME_ID_BYTES = 512;
|
|
38
38
|
const CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/;
|
|
39
|
+
const QUALIFICATION_PROCESS_STATES = new Set([
|
|
40
|
+
'Spawning', 'Initializing', 'AttachingThread', 'Idle', 'StartingTurn',
|
|
41
|
+
'Active', 'BackgroundWorking', 'BackgroundSettling', 'Settling',
|
|
42
|
+
'Quiescing', 'Stopped', 'Closing', 'Closed', 'FailedAmbiguous',
|
|
43
|
+
'RecoveryConflict', 'DurabilityBlocked', 'ContainmentFailed', 'unknown',
|
|
44
|
+
]);
|
|
45
|
+
const QUALIFICATION_REASONS = new Set([
|
|
46
|
+
'eligible', 'active-turn', 'pending-delivery', 'background-owner',
|
|
47
|
+
'background-terminals', 'pagination-incomplete', 'process-not-idle',
|
|
48
|
+
'process-not-ready', 'no-codex-generation', 'unsupported', 'lease-drift',
|
|
49
|
+
'generation-drift', 'activity-epoch-drift', 'fence-drift',
|
|
50
|
+
'inspection-failed', 'invalid-expectation', 'incoherent',
|
|
51
|
+
]);
|
|
39
52
|
|
|
40
53
|
class ProcessManagerError extends Error {
|
|
41
54
|
constructor(message, code, options) {
|
|
@@ -62,6 +75,158 @@ function hintFingerprint(hint) {
|
|
|
62
75
|
.slice(0, 8);
|
|
63
76
|
}
|
|
64
77
|
|
|
78
|
+
function qualificationDigest(generationId) {
|
|
79
|
+
return typeof generationId === 'string'
|
|
80
|
+
? hash('sha256', generationId, 'hex')
|
|
81
|
+
: null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function qualificationResultFor(proc, reason, fence = {}) {
|
|
85
|
+
const observedAtMs = Object.hasOwn(fence, 'observedAtMs')
|
|
86
|
+
&& Number.isSafeInteger(fence.observedAtMs)
|
|
87
|
+
&& fence.observedAtMs >= 0
|
|
88
|
+
? fence.observedAtMs
|
|
89
|
+
: Date.now();
|
|
90
|
+
return Object.freeze({
|
|
91
|
+
outcome: 'mismatch',
|
|
92
|
+
reason: QUALIFICATION_REASONS.has(reason) ? reason : 'inspection-failed',
|
|
93
|
+
generationDigest: Object.hasOwn(fence, 'generationDigest')
|
|
94
|
+
? fence.generationDigest
|
|
95
|
+
: qualificationDigest(proc?.generationId),
|
|
96
|
+
activityEpoch: Object.hasOwn(fence, 'activityEpoch')
|
|
97
|
+
? fence.activityEpoch
|
|
98
|
+
: (Number.isSafeInteger(proc?.activityEpoch) ? proc.activityEpoch : null),
|
|
99
|
+
processState: QUALIFICATION_PROCESS_STATES.has(proc?.state)
|
|
100
|
+
? proc.state
|
|
101
|
+
: 'unknown',
|
|
102
|
+
activeTurnCount: proc?.current ? 1 : 0,
|
|
103
|
+
pendingDeliveryCount: typeof proc?.hasPendingDeliveryWork === 'function'
|
|
104
|
+
&& proc.hasPendingDeliveryWork() ? 1 : 0,
|
|
105
|
+
backgroundOwnerCount: typeof proc?.hasActiveBackgroundWork === 'function'
|
|
106
|
+
&& proc.hasActiveBackgroundWork() ? 1 : 0,
|
|
107
|
+
backgroundTerminalCount: 0,
|
|
108
|
+
backgroundTerminalRegistryComplete: false,
|
|
109
|
+
observedAtMs,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function normalizeQualificationExpectation(value, { requireFence = false } = {}) {
|
|
114
|
+
if (value === undefined && !requireFence) {
|
|
115
|
+
return { valid: true, expectedGenerationDigest: null, expectedActivityEpoch: null };
|
|
116
|
+
}
|
|
117
|
+
if (
|
|
118
|
+
value === null
|
|
119
|
+
|| typeof value !== 'object'
|
|
120
|
+
|| Array.isArray(value)
|
|
121
|
+
|| Object.getPrototypeOf(value) !== Object.prototype
|
|
122
|
+
) {
|
|
123
|
+
return { valid: false };
|
|
124
|
+
}
|
|
125
|
+
const keys = Reflect.ownKeys(value);
|
|
126
|
+
if (
|
|
127
|
+
keys.some((key) => (
|
|
128
|
+
typeof key !== 'string'
|
|
129
|
+
|| !['expectedGenerationDigest', 'expectedActivityEpoch'].includes(key)
|
|
130
|
+
))
|
|
131
|
+
|| (requireFence && keys.length !== 2)
|
|
132
|
+
|| (!requireFence && keys.length !== 0 && keys.length !== 2)
|
|
133
|
+
) {
|
|
134
|
+
return { valid: false };
|
|
135
|
+
}
|
|
136
|
+
const expectedGenerationDigest = value.expectedGenerationDigest;
|
|
137
|
+
const expectedActivityEpoch = value.expectedActivityEpoch;
|
|
138
|
+
if (
|
|
139
|
+
keys.length === 2
|
|
140
|
+
&& (
|
|
141
|
+
!/^[a-f0-9]{64}$/.test(expectedGenerationDigest)
|
|
142
|
+
|| !Number.isSafeInteger(expectedActivityEpoch)
|
|
143
|
+
|| expectedActivityEpoch < 0
|
|
144
|
+
)
|
|
145
|
+
) {
|
|
146
|
+
return { valid: false };
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
valid: true,
|
|
150
|
+
expectedGenerationDigest: keys.length === 2 ? expectedGenerationDigest : null,
|
|
151
|
+
expectedActivityEpoch: keys.length === 2 ? expectedActivityEpoch : null,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function normalizeQualificationResult(proc, result, expectedFence = {}) {
|
|
156
|
+
const expectedKeys = [
|
|
157
|
+
'outcome',
|
|
158
|
+
'reason',
|
|
159
|
+
'generationDigest',
|
|
160
|
+
'activityEpoch',
|
|
161
|
+
'processState',
|
|
162
|
+
'activeTurnCount',
|
|
163
|
+
'pendingDeliveryCount',
|
|
164
|
+
'backgroundOwnerCount',
|
|
165
|
+
'backgroundTerminalCount',
|
|
166
|
+
'backgroundTerminalRegistryComplete',
|
|
167
|
+
'observedAtMs',
|
|
168
|
+
];
|
|
169
|
+
const resultKeys = result && typeof result === 'object'
|
|
170
|
+
? Reflect.ownKeys(result)
|
|
171
|
+
: [];
|
|
172
|
+
if (
|
|
173
|
+
!result
|
|
174
|
+
|| typeof result !== 'object'
|
|
175
|
+
|| Object.getPrototypeOf(result) !== Object.prototype
|
|
176
|
+
|| resultKeys.length !== expectedKeys.length
|
|
177
|
+
|| expectedKeys.some((key) => !Object.hasOwn(result, key))
|
|
178
|
+
|| !['qualified', 'mismatch'].includes(result.outcome)
|
|
179
|
+
|| !QUALIFICATION_REASONS.has(result.reason)
|
|
180
|
+
|| !/^[a-f0-9]{64}$/.test(result.generationDigest)
|
|
181
|
+
|| !Number.isSafeInteger(result.activityEpoch)
|
|
182
|
+
|| result.activityEpoch < 0
|
|
183
|
+
|| !QUALIFICATION_PROCESS_STATES.has(result.processState)
|
|
184
|
+
|| ![0, 1].includes(result.activeTurnCount)
|
|
185
|
+
|| ![0, 1].includes(result.pendingDeliveryCount)
|
|
186
|
+
|| ![0, 1].includes(result.backgroundOwnerCount)
|
|
187
|
+
|| !Number.isSafeInteger(result.backgroundTerminalCount)
|
|
188
|
+
|| result.backgroundTerminalCount < 0
|
|
189
|
+
|| result.backgroundTerminalCount > 1_600
|
|
190
|
+
|| typeof result.backgroundTerminalRegistryComplete !== 'boolean'
|
|
191
|
+
|| !Number.isSafeInteger(result.observedAtMs)
|
|
192
|
+
|| result.observedAtMs < 0
|
|
193
|
+
) {
|
|
194
|
+
return qualificationResultFor(proc, 'inspection-failed');
|
|
195
|
+
}
|
|
196
|
+
if (
|
|
197
|
+
result.outcome === 'qualified'
|
|
198
|
+
&& (
|
|
199
|
+
result.reason !== 'eligible'
|
|
200
|
+
|| result.generationDigest !== expectedFence.generationDigest
|
|
201
|
+
|| result.activityEpoch !== expectedFence.activityEpoch
|
|
202
|
+
|| result.processState !== 'Idle'
|
|
203
|
+
|| result.activeTurnCount !== 0
|
|
204
|
+
|| result.pendingDeliveryCount !== 0
|
|
205
|
+
|| result.backgroundOwnerCount !== 0
|
|
206
|
+
|| result.backgroundTerminalCount !== 0
|
|
207
|
+
|| result.backgroundTerminalRegistryComplete !== true
|
|
208
|
+
)
|
|
209
|
+
) {
|
|
210
|
+
return qualificationResultFor(proc, 'incoherent', {
|
|
211
|
+
...expectedFence,
|
|
212
|
+
observedAtMs: result.observedAtMs,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return Object.freeze({
|
|
216
|
+
outcome: result.outcome,
|
|
217
|
+
reason: result.reason,
|
|
218
|
+
generationDigest: result.generationDigest,
|
|
219
|
+
activityEpoch: result.activityEpoch,
|
|
220
|
+
processState: result.processState,
|
|
221
|
+
activeTurnCount: result.activeTurnCount,
|
|
222
|
+
pendingDeliveryCount: result.pendingDeliveryCount,
|
|
223
|
+
backgroundOwnerCount: result.backgroundOwnerCount,
|
|
224
|
+
backgroundTerminalCount: result.backgroundTerminalCount,
|
|
225
|
+
backgroundTerminalRegistryComplete: result.backgroundTerminalRegistryComplete,
|
|
226
|
+
observedAtMs: result.observedAtMs,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
65
230
|
function findCodexRetirementVerificationError(error) {
|
|
66
231
|
let current = error;
|
|
67
232
|
for (let depth = 0; current && depth < 8; depth += 1) {
|
|
@@ -274,6 +439,8 @@ class ProcessManager {
|
|
|
274
439
|
this.procs = new Map(); // sessionKey → Process
|
|
275
440
|
this._lruWaiters = []; // [{ resolve, reject, timer }]
|
|
276
441
|
this._shuttingDown = false;
|
|
442
|
+
this._lifecycleAdmissionClosed = false;
|
|
443
|
+
this._cleanRetirementCandidates = new Map();
|
|
277
444
|
// sessionKey → in-flight start() Promise. Lets a concurrent
|
|
278
445
|
// getOrSpawn for the same key await the spawn instead of
|
|
279
446
|
// returning a proc whose start() hasn't resolved (see getOrSpawn).
|
|
@@ -323,6 +490,85 @@ class ProcessManager {
|
|
|
323
490
|
keys() { return [...this.procs.keys()]; }
|
|
324
491
|
get size() { return this.procs.size; }
|
|
325
492
|
|
|
493
|
+
async inspectCleanRestartQualification(expectation, { requireFence = false } = {}) {
|
|
494
|
+
const lease = this._codexLease;
|
|
495
|
+
const proc = lease?.proc ?? null;
|
|
496
|
+
const sessionKey = lease?.sessionKey ?? null;
|
|
497
|
+
const capturedFence = {
|
|
498
|
+
generationDigest: qualificationDigest(proc?.generationId),
|
|
499
|
+
activityEpoch: Number.isSafeInteger(proc?.activityEpoch)
|
|
500
|
+
? proc.activityEpoch
|
|
501
|
+
: null,
|
|
502
|
+
};
|
|
503
|
+
const parsedExpectation = normalizeQualificationExpectation(expectation, {
|
|
504
|
+
requireFence,
|
|
505
|
+
});
|
|
506
|
+
if (!parsedExpectation.valid) {
|
|
507
|
+
return qualificationResultFor(proc, 'invalid-expectation', capturedFence);
|
|
508
|
+
}
|
|
509
|
+
if (!proc || !this._isCodex(proc) || proc.closed || !this._codexLeaseMatches(proc)) {
|
|
510
|
+
return qualificationResultFor(proc, 'no-codex-generation', capturedFence);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const { expectedGenerationDigest, expectedActivityEpoch } = parsedExpectation;
|
|
514
|
+
if (
|
|
515
|
+
expectedGenerationDigest != null
|
|
516
|
+
&& expectedGenerationDigest !== capturedFence.generationDigest
|
|
517
|
+
) {
|
|
518
|
+
return qualificationResultFor(proc, 'generation-drift', capturedFence);
|
|
519
|
+
}
|
|
520
|
+
if (
|
|
521
|
+
expectedActivityEpoch != null
|
|
522
|
+
&& expectedActivityEpoch !== capturedFence.activityEpoch
|
|
523
|
+
) {
|
|
524
|
+
return qualificationResultFor(proc, 'activity-epoch-drift', capturedFence);
|
|
525
|
+
}
|
|
526
|
+
if (typeof proc.inspectCleanRestartQualification !== 'function') {
|
|
527
|
+
return qualificationResultFor(proc, 'unsupported', capturedFence);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
const assertLease = () => {
|
|
531
|
+
if (
|
|
532
|
+
this._codexLease !== lease
|
|
533
|
+
|| this.procs.get(sessionKey) !== proc
|
|
534
|
+
|| !this._codexLeaseMatches(proc)
|
|
535
|
+
|| proc.closed
|
|
536
|
+
) {
|
|
537
|
+
throw managerError(
|
|
538
|
+
'Codex qualification lease changed',
|
|
539
|
+
'CODEX_QUALIFICATION_LEASE_DRIFT',
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
if (
|
|
543
|
+
qualificationDigest(proc.generationId) !== capturedFence.generationDigest
|
|
544
|
+
|| proc.activityEpoch !== capturedFence.activityEpoch
|
|
545
|
+
) {
|
|
546
|
+
throw managerError(
|
|
547
|
+
'Codex qualification fence changed',
|
|
548
|
+
'CODEX_QUALIFICATION_FENCE',
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
return true;
|
|
552
|
+
};
|
|
553
|
+
try {
|
|
554
|
+
const result = await proc.inspectCleanRestartQualification({
|
|
555
|
+
expectedGenerationDigest: capturedFence.generationDigest,
|
|
556
|
+
expectedActivityEpoch: capturedFence.activityEpoch,
|
|
557
|
+
assertLease,
|
|
558
|
+
});
|
|
559
|
+
assertLease();
|
|
560
|
+
return normalizeQualificationResult(proc, result, capturedFence);
|
|
561
|
+
} catch (error) {
|
|
562
|
+
if (error?.code === 'CODEX_QUALIFICATION_LEASE_DRIFT') {
|
|
563
|
+
return qualificationResultFor(proc, 'lease-drift', capturedFence);
|
|
564
|
+
}
|
|
565
|
+
if (error?.code === 'CODEX_QUALIFICATION_FENCE') {
|
|
566
|
+
return qualificationResultFor(proc, 'fence-drift', capturedFence);
|
|
567
|
+
}
|
|
568
|
+
return qualificationResultFor(proc, 'inspection-failed', capturedFence);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
326
572
|
/**
|
|
327
573
|
* Current total cost across all live processes.
|
|
328
574
|
*/
|
|
@@ -497,6 +743,17 @@ class ProcessManager {
|
|
|
497
743
|
}
|
|
498
744
|
|
|
499
745
|
_withLifecycleGate(sessionKey, operation) {
|
|
746
|
+
if (this._lifecycleAdmissionClosed) {
|
|
747
|
+
return Promise.reject(new Error('shutdown'));
|
|
748
|
+
}
|
|
749
|
+
return this._enqueueLifecycleGate(sessionKey, operation);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
_withInternalLifecycleGate(sessionKey, operation) {
|
|
753
|
+
return this._enqueueLifecycleGate(sessionKey, operation);
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
_enqueueLifecycleGate(sessionKey, operation) {
|
|
500
757
|
const prior = this._lifecycleGates.get(sessionKey);
|
|
501
758
|
const run = Promise.resolve(prior)
|
|
502
759
|
.catch(() => {})
|
|
@@ -511,6 +768,24 @@ class ProcessManager {
|
|
|
511
768
|
return run;
|
|
512
769
|
}
|
|
513
770
|
|
|
771
|
+
async _drainLifecycleGatesToStableEmpty(gateErrors) {
|
|
772
|
+
while (true) {
|
|
773
|
+
const entries = [...this._lifecycleGates.entries()];
|
|
774
|
+
if (entries.length === 0) {
|
|
775
|
+
await Promise.resolve();
|
|
776
|
+
if (this._lifecycleGates.size === 0) return;
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
779
|
+
await Promise.allSettled(entries.map(async ([sessionKey, gate]) => {
|
|
780
|
+
try {
|
|
781
|
+
await gate;
|
|
782
|
+
} catch (error) {
|
|
783
|
+
gateErrors.set(sessionKey, error);
|
|
784
|
+
}
|
|
785
|
+
}));
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
514
789
|
_withRetirementIntent(sessionKey, operation) {
|
|
515
790
|
const count = (this._retirementIntents.get(sessionKey) ?? 0) + 1;
|
|
516
791
|
this._retirementIntents.set(sessionKey, count);
|
|
@@ -843,6 +1118,9 @@ class ProcessManager {
|
|
|
843
1118
|
if (this.procs.get(sessionKey) === newProc) {
|
|
844
1119
|
this.procs.delete(sessionKey);
|
|
845
1120
|
}
|
|
1121
|
+
if (this._lifecycleAdmissionClosed) {
|
|
1122
|
+
this._cleanRetirementCandidates.set(sessionKey, newProc);
|
|
1123
|
+
}
|
|
846
1124
|
this._maybeSignalLruWaiter();
|
|
847
1125
|
}
|
|
848
1126
|
throw err;
|
|
@@ -1231,7 +1509,7 @@ class ProcessManager {
|
|
|
1231
1509
|
});
|
|
1232
1510
|
this._retiring.add(oldest);
|
|
1233
1511
|
this.procs.delete(oldestKey);
|
|
1234
|
-
this.
|
|
1512
|
+
this._withInternalLifecycleGate(oldestKey, async () => {
|
|
1235
1513
|
try { await oldest.kill('evict'); } catch {}
|
|
1236
1514
|
finally { this._retiring.delete(oldest); }
|
|
1237
1515
|
}).catch(() => {});
|
|
@@ -1428,7 +1706,7 @@ class ProcessManager {
|
|
|
1428
1706
|
.filter(([, proc]) => !this._retiring.has(proc))
|
|
1429
1707
|
.map(([sessionKey]) => sessionKey);
|
|
1430
1708
|
const retirementResults = await Promise.allSettled(targets.map((sessionKey) => (
|
|
1431
|
-
this.
|
|
1709
|
+
this._withInternalLifecycleGate(sessionKey, () => (
|
|
1432
1710
|
this._killLocked(sessionKey, 'shutdown')
|
|
1433
1711
|
))
|
|
1434
1712
|
)));
|
|
@@ -1441,137 +1719,233 @@ class ProcessManager {
|
|
|
1441
1719
|
}
|
|
1442
1720
|
}
|
|
1443
1721
|
|
|
1444
|
-
async retireForCleanRestart({
|
|
1722
|
+
async retireForCleanRestart({
|
|
1723
|
+
getDeliveryEvidence,
|
|
1724
|
+
qualificationExpectation,
|
|
1725
|
+
} = {}) {
|
|
1445
1726
|
if (typeof getDeliveryEvidence !== 'function') {
|
|
1446
1727
|
throw new TypeError(
|
|
1447
1728
|
'ProcessManager.retireForCleanRestart: getDeliveryEvidence function required',
|
|
1448
1729
|
);
|
|
1449
1730
|
}
|
|
1450
1731
|
this._shuttingDown = true;
|
|
1732
|
+
this._lifecycleAdmissionClosed = true;
|
|
1451
1733
|
for (const w of this._lruWaiters) {
|
|
1452
1734
|
clearTimeout(w.timer);
|
|
1453
1735
|
w.reject(new Error('shutdown'));
|
|
1454
1736
|
}
|
|
1455
1737
|
this._lruWaiters.length = 0;
|
|
1456
1738
|
|
|
1457
|
-
const
|
|
1458
|
-
|
|
1459
|
-
);
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1739
|
+
const admittedGateErrors = new Map();
|
|
1740
|
+
const admittedGates = new Map(this._lifecycleGates);
|
|
1741
|
+
await this._drainLifecycleGatesToStableEmpty(admittedGateErrors);
|
|
1742
|
+
|
|
1743
|
+
const targets = new Set([
|
|
1744
|
+
...[...this.procs.entries()]
|
|
1745
|
+
.filter(([, proc]) => !this._retiring.has(proc))
|
|
1746
|
+
.map(([sessionKey]) => sessionKey),
|
|
1747
|
+
...this._cleanRetirementCandidates.keys(),
|
|
1748
|
+
...admittedGates.keys(),
|
|
1749
|
+
...admittedGateErrors.keys(),
|
|
1750
|
+
]);
|
|
1751
|
+
const retiredTargets = new Map();
|
|
1752
|
+
const qualificationLease = qualificationExpectation === undefined
|
|
1753
|
+
? null
|
|
1754
|
+
: this._codexLease;
|
|
1755
|
+
const qualificationProc = qualificationLease?.proc ?? null;
|
|
1756
|
+
let qualification = qualificationExpectation === undefined
|
|
1757
|
+
? null
|
|
1758
|
+
: await this.inspectCleanRestartQualification(
|
|
1759
|
+
qualificationExpectation,
|
|
1760
|
+
{ requireFence: true },
|
|
1761
|
+
);
|
|
1463
1762
|
|
|
1464
1763
|
let settled;
|
|
1465
1764
|
try {
|
|
1466
|
-
settled = await Promise.allSettled(targets.map(async (
|
|
1765
|
+
settled = await Promise.allSettled([...targets].map(async (sessionKey) => {
|
|
1766
|
+
const gateError = admittedGateErrors.get(sessionKey) ?? null;
|
|
1767
|
+
const proc = this.procs.get(sessionKey)
|
|
1768
|
+
?? this._cleanRetirementCandidates.get(sessionKey);
|
|
1769
|
+
if (!proc || this._retiring.has(proc)) {
|
|
1770
|
+
if (
|
|
1771
|
+
qualification?.outcome === 'qualified'
|
|
1772
|
+
&& sessionKey === qualificationLease?.sessionKey
|
|
1773
|
+
&& qualificationProc
|
|
1774
|
+
) {
|
|
1775
|
+
qualification = qualificationResultFor(
|
|
1776
|
+
qualificationProc,
|
|
1777
|
+
'lease-drift',
|
|
1778
|
+
{
|
|
1779
|
+
generationDigest: qualification.generationDigest,
|
|
1780
|
+
activityEpoch: qualification.activityEpoch,
|
|
1781
|
+
observedAtMs: qualification.observedAtMs,
|
|
1782
|
+
},
|
|
1783
|
+
);
|
|
1784
|
+
}
|
|
1785
|
+
if (gateError) throw gateError;
|
|
1786
|
+
return null;
|
|
1787
|
+
}
|
|
1788
|
+
this._retiring.add(proc);
|
|
1789
|
+
retiredTargets.set(sessionKey, proc);
|
|
1467
1790
|
let snapshot;
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1791
|
+
try {
|
|
1792
|
+
if (this._isCodex(proc)) {
|
|
1793
|
+
const candidate = typeof proc.captureCleanRestartCandidate === 'function'
|
|
1794
|
+
? proc.captureCleanRestartCandidate()
|
|
1795
|
+
: null;
|
|
1796
|
+
if (qualification?.outcome === 'qualified') {
|
|
1797
|
+
const leaseChanged = Boolean(
|
|
1798
|
+
this._codexLease !== qualificationLease
|
|
1799
|
+
|| qualificationProc !== proc
|
|
1800
|
+
|| this.procs.get(sessionKey) !== proc
|
|
1801
|
+
|| !this._codexLeaseMatches(proc)
|
|
1802
|
+
|| proc.closed
|
|
1803
|
+
);
|
|
1804
|
+
const fenceChanged = Boolean(
|
|
1805
|
+
qualificationDigest(proc.generationId) !== qualification.generationDigest
|
|
1806
|
+
|| proc.activityEpoch !== qualification.activityEpoch
|
|
1807
|
+
);
|
|
1808
|
+
if (leaseChanged || fenceChanged) {
|
|
1809
|
+
qualification = qualificationResultFor(
|
|
1810
|
+
proc,
|
|
1811
|
+
leaseChanged ? 'lease-drift' : 'fence-drift',
|
|
1812
|
+
{
|
|
1813
|
+
generationDigest: qualification.generationDigest,
|
|
1814
|
+
activityEpoch: qualification.activityEpoch,
|
|
1815
|
+
observedAtMs: qualification.observedAtMs,
|
|
1816
|
+
},
|
|
1817
|
+
);
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
const retirementPromise = this._strictRetire(proc, 'clean-restart');
|
|
1821
|
+
retirementPromise.catch(() => {});
|
|
1822
|
+
let deliveryEvidence = null;
|
|
1823
|
+
let deliveryEvidenceError = null;
|
|
1824
|
+
try {
|
|
1825
|
+
deliveryEvidence = await getDeliveryEvidence(
|
|
1826
|
+
sessionKey,
|
|
1827
|
+
candidate?.sourceMsgId ?? null,
|
|
1828
|
+
);
|
|
1829
|
+
} catch (error) {
|
|
1830
|
+
deliveryEvidenceError = error;
|
|
1831
|
+
}
|
|
1832
|
+
const retirement = await retirementPromise;
|
|
1833
|
+
const settledTerminalStatus = this._codexLease?.terminalStatus ?? null;
|
|
1834
|
+
const settledTurnId = this._codexLease?.turnId ?? null;
|
|
1835
|
+
const deliverySafe = Boolean(
|
|
1836
|
+
deliveryEvidence?.fenced === true
|
|
1837
|
+
&& deliveryEvidence.pending === 0
|
|
1838
|
+
&& deliveryEvidence.outputAttempted === false
|
|
1478
1839
|
);
|
|
1479
|
-
|
|
1480
|
-
|
|
1840
|
+
const exactRetirement = Boolean(
|
|
1841
|
+
candidate
|
|
1842
|
+
&& retirement?.committed === true
|
|
1843
|
+
&& retirement.disposition === 'stop-cancelled'
|
|
1844
|
+
&& retirement.sessionKey === candidate.sessionKey
|
|
1845
|
+
&& retirement.generationId === candidate.generationId
|
|
1846
|
+
&& retirement.attemptId === candidate.attemptId
|
|
1847
|
+
&& retirement.providerSessionId === candidate.providerSessionId
|
|
1848
|
+
&& retirement.providerTurnId === candidate.providerTurnId
|
|
1849
|
+
&& retirement.sourceMsgId != null
|
|
1850
|
+
&& String(retirement.sourceMsgId) === String(candidate.sourceMsgId)
|
|
1851
|
+
&& settledTerminalStatus === 'interrupted'
|
|
1852
|
+
&& settledTurnId === candidate.providerTurnId
|
|
1853
|
+
);
|
|
1854
|
+
if (this._codexLeaseMatches(proc)) this._codexLease = null;
|
|
1855
|
+
const eligible = deliverySafe && exactRetirement;
|
|
1856
|
+
let reason = 'output-evidence';
|
|
1857
|
+
if (deliverySafe) reason = 'retirement-binding-mismatch';
|
|
1858
|
+
if (deliveryEvidenceError) reason = 'delivery-evidence-failed';
|
|
1859
|
+
snapshot = candidate ? {
|
|
1860
|
+
runtime: 'codex',
|
|
1861
|
+
namespace: 'codex:app-server',
|
|
1862
|
+
sourceMsgId: candidate.sourceMsgId,
|
|
1863
|
+
providerSessionId: candidate.providerSessionId,
|
|
1864
|
+
providerTurnId: candidate.providerTurnId,
|
|
1865
|
+
cwd: candidate.cwd,
|
|
1866
|
+
model: candidate.model,
|
|
1867
|
+
effort: candidate.effort,
|
|
1868
|
+
spawnProfileId: candidate.spawnProfileId,
|
|
1869
|
+
eligible,
|
|
1870
|
+
reason: eligible ? 'eligible' : reason,
|
|
1871
|
+
} : {
|
|
1872
|
+
sourceMsgId: null,
|
|
1873
|
+
eligible: false,
|
|
1874
|
+
reason: 'no-active-turn',
|
|
1875
|
+
};
|
|
1876
|
+
if (deliveryEvidenceError) throw deliveryEvidenceError;
|
|
1877
|
+
} else {
|
|
1878
|
+
snapshot = await proc.retireForCleanRestart({
|
|
1879
|
+
getDeliveryEvidence: (_reportedSessionKey, sourceMsgId) => (
|
|
1880
|
+
getDeliveryEvidence(
|
|
1881
|
+
sessionKey,
|
|
1882
|
+
proc.backend === 'cli' ? sourceMsgId : null,
|
|
1883
|
+
)
|
|
1884
|
+
),
|
|
1885
|
+
});
|
|
1481
1886
|
}
|
|
1482
|
-
const
|
|
1483
|
-
const
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
&&
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
&& String(retirement.sourceMsgId) === String(candidate.sourceMsgId)
|
|
1501
|
-
&& settledTerminalStatus === 'interrupted'
|
|
1502
|
-
&& settledTurnId === candidate.providerTurnId
|
|
1503
|
-
);
|
|
1504
|
-
if (this._codexLeaseMatches(proc)) this._codexLease = null;
|
|
1505
|
-
const eligible = deliverySafe && exactRetirement;
|
|
1506
|
-
let reason = 'output-evidence';
|
|
1507
|
-
if (deliverySafe) reason = 'retirement-binding-mismatch';
|
|
1508
|
-
if (deliveryEvidenceError) reason = 'delivery-evidence-failed';
|
|
1509
|
-
snapshot = candidate ? {
|
|
1510
|
-
runtime: 'codex',
|
|
1511
|
-
namespace: 'codex:app-server',
|
|
1512
|
-
sourceMsgId: candidate.sourceMsgId,
|
|
1513
|
-
providerSessionId: candidate.providerSessionId,
|
|
1514
|
-
providerTurnId: candidate.providerTurnId,
|
|
1515
|
-
cwd: candidate.cwd,
|
|
1516
|
-
model: candidate.model,
|
|
1517
|
-
effort: candidate.effort,
|
|
1518
|
-
spawnProfileId: candidate.spawnProfileId,
|
|
1887
|
+
const supported = proc.backend === 'cli' || this._isCodex(proc);
|
|
1888
|
+
const eligible = supported && snapshot?.eligible === true;
|
|
1889
|
+
if (gateError) throw gateError;
|
|
1890
|
+
return Object.freeze({
|
|
1891
|
+
sessionKey,
|
|
1892
|
+
...(this._isCodex(proc) && snapshot.runtime ? {
|
|
1893
|
+
runtime: snapshot.runtime,
|
|
1894
|
+
namespace: snapshot.namespace,
|
|
1895
|
+
providerSessionId: snapshot.providerSessionId,
|
|
1896
|
+
providerTurnId: snapshot.providerTurnId,
|
|
1897
|
+
cwd: snapshot.cwd,
|
|
1898
|
+
model: snapshot.model,
|
|
1899
|
+
effort: snapshot.effort,
|
|
1900
|
+
spawnProfileId: snapshot.spawnProfileId,
|
|
1901
|
+
} : {}),
|
|
1902
|
+
sourceMsgId: supported
|
|
1903
|
+
? (snapshot?.sourceMsgId ?? null)
|
|
1904
|
+
: null,
|
|
1519
1905
|
eligible,
|
|
1520
|
-
reason:
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
eligible: false,
|
|
1524
|
-
reason: 'no-active-turn',
|
|
1525
|
-
};
|
|
1526
|
-
if (deliveryEvidenceError) throw deliveryEvidenceError;
|
|
1527
|
-
} else {
|
|
1528
|
-
snapshot = await proc.retireForCleanRestart({
|
|
1529
|
-
getDeliveryEvidence: (_reportedSessionKey, sourceMsgId) => (
|
|
1530
|
-
getDeliveryEvidence(
|
|
1531
|
-
sessionKey,
|
|
1532
|
-
proc.backend === 'cli' ? sourceMsgId : null,
|
|
1533
|
-
)
|
|
1534
|
-
),
|
|
1906
|
+
reason: supported
|
|
1907
|
+
? (snapshot?.reason || 'retirement-uncertain')
|
|
1908
|
+
: 'unsupported-backend',
|
|
1535
1909
|
});
|
|
1910
|
+
} catch (error) {
|
|
1911
|
+
if (gateError) throw gateError;
|
|
1912
|
+
throw error;
|
|
1536
1913
|
}
|
|
1537
|
-
const supported = proc.backend === 'cli' || this._isCodex(proc);
|
|
1538
|
-
const eligible = supported && snapshot?.eligible === true;
|
|
1539
|
-
return Object.freeze({
|
|
1540
|
-
sessionKey,
|
|
1541
|
-
...(this._isCodex(proc) && snapshot.runtime ? {
|
|
1542
|
-
runtime: snapshot.runtime,
|
|
1543
|
-
namespace: snapshot.namespace,
|
|
1544
|
-
providerSessionId: snapshot.providerSessionId,
|
|
1545
|
-
providerTurnId: snapshot.providerTurnId,
|
|
1546
|
-
cwd: snapshot.cwd,
|
|
1547
|
-
model: snapshot.model,
|
|
1548
|
-
effort: snapshot.effort,
|
|
1549
|
-
spawnProfileId: snapshot.spawnProfileId,
|
|
1550
|
-
} : {}),
|
|
1551
|
-
sourceMsgId: supported
|
|
1552
|
-
? (snapshot?.sourceMsgId ?? null)
|
|
1553
|
-
: null,
|
|
1554
|
-
eligible,
|
|
1555
|
-
reason: supported
|
|
1556
|
-
? (snapshot?.reason || 'retirement-uncertain')
|
|
1557
|
-
: 'unsupported-backend',
|
|
1558
|
-
});
|
|
1559
1914
|
}));
|
|
1560
1915
|
} finally {
|
|
1561
|
-
for (const [sessionKey, proc] of
|
|
1916
|
+
for (const [sessionKey, proc] of retiredTargets) {
|
|
1562
1917
|
if (proc.closed && this.procs.get(sessionKey) === proc) {
|
|
1563
1918
|
this.procs.delete(sessionKey);
|
|
1564
1919
|
}
|
|
1920
|
+
const candidateOwned = (
|
|
1921
|
+
this._cleanRetirementCandidates.get(sessionKey) === proc
|
|
1922
|
+
);
|
|
1923
|
+
if (!proc.closed && candidateOwned && !this.procs.has(sessionKey)) {
|
|
1924
|
+
this.procs.set(sessionKey, proc);
|
|
1925
|
+
}
|
|
1926
|
+
if (
|
|
1927
|
+
candidateOwned
|
|
1928
|
+
&& (proc.closed || this.procs.get(sessionKey) === proc)
|
|
1929
|
+
) {
|
|
1930
|
+
this._cleanRetirementCandidates.delete(sessionKey);
|
|
1931
|
+
}
|
|
1565
1932
|
this._retiring.delete(proc);
|
|
1566
1933
|
}
|
|
1567
1934
|
this._maybeSignalLruWaiter();
|
|
1568
1935
|
}
|
|
1569
|
-
const failure =
|
|
1570
|
-
...admittedResults,
|
|
1571
|
-
...settled,
|
|
1572
|
-
].find(result => result.status === 'rejected');
|
|
1936
|
+
const failure = settled.find(result => result.status === 'rejected');
|
|
1573
1937
|
if (failure) throw failure.reason;
|
|
1574
|
-
const results = settled
|
|
1938
|
+
const results = settled
|
|
1939
|
+
.filter(result => result.status === 'fulfilled' && result.value != null)
|
|
1940
|
+
.map(result => result.value);
|
|
1941
|
+
if (qualification) {
|
|
1942
|
+
Object.defineProperty(results, 'qualification', {
|
|
1943
|
+
value: qualification,
|
|
1944
|
+
enumerable: false,
|
|
1945
|
+
writable: false,
|
|
1946
|
+
configurable: false,
|
|
1947
|
+
});
|
|
1948
|
+
}
|
|
1575
1949
|
return Object.freeze(results);
|
|
1576
1950
|
}
|
|
1577
1951
|
|
|
@@ -1596,6 +1970,7 @@ class ProcessManager {
|
|
|
1596
1970
|
}
|
|
1597
1971
|
|
|
1598
1972
|
async interrupt(sessionKey) {
|
|
1973
|
+
if (this._lifecycleAdmissionClosed) throw new Error('shutdown');
|
|
1599
1974
|
const proc = this.procs.get(sessionKey);
|
|
1600
1975
|
if (!proc || proc.closed) return false;
|
|
1601
1976
|
if (
|
|
@@ -1982,7 +2357,7 @@ class ProcessManager {
|
|
|
1982
2357
|
}
|
|
1983
2358
|
const start = this._starting.get(proc.sessionKey) ?? null;
|
|
1984
2359
|
const lease = this._codexLease;
|
|
1985
|
-
this.
|
|
2360
|
+
this._withInternalLifecycleGate(proc.sessionKey, async () => {
|
|
1986
2361
|
if (start) await Promise.resolve(start).catch(() => {});
|
|
1987
2362
|
if (
|
|
1988
2363
|
this._starting.has(proc.sessionKey)
|
|
@@ -2020,7 +2395,7 @@ class ProcessManager {
|
|
|
2020
2395
|
// Serialize with start/replacement/kill. Re-check object identity after
|
|
2021
2396
|
// the gate so a late disconnect from an old generation cannot kill its
|
|
2022
2397
|
// replacement.
|
|
2023
|
-
this.
|
|
2398
|
+
this._withInternalLifecycleGate(proc.sessionKey, async () => {
|
|
2024
2399
|
if (this.procs.get(proc.sessionKey) !== proc) return;
|
|
2025
2400
|
await this._killLocked(proc.sessionKey, 'bridge-disconnected');
|
|
2026
2401
|
}).catch(err => {
|