@shumkov/orchestra 0.10.18 → 0.10.19
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/process/codex-process.js +197 -29
- package/package.json +1 -1
|
@@ -469,6 +469,7 @@ class CodexProcess extends Process {
|
|
|
469
469
|
this.killPromise = null;
|
|
470
470
|
this.interruptPromise = null;
|
|
471
471
|
this.backgroundSettlementPromise = null;
|
|
472
|
+
this.backgroundOwner = null;
|
|
472
473
|
this.backgroundWatchdog = null;
|
|
473
474
|
this.backgroundWatchdogDeadlineAt = null;
|
|
474
475
|
this.backgroundWatchdogPromise = null;
|
|
@@ -1796,20 +1797,29 @@ class CodexProcess extends Process {
|
|
|
1796
1797
|
this._clearBackgroundWatchdog();
|
|
1797
1798
|
const backgroundSettlement = this.backgroundSettlementPromise;
|
|
1798
1799
|
const pending = this.current;
|
|
1800
|
+
let backgroundOwnership = null;
|
|
1801
|
+
if (
|
|
1802
|
+
priorState === 'BackgroundWorking'
|
|
1803
|
+
|| priorState === 'BackgroundSettling'
|
|
1804
|
+
) {
|
|
1805
|
+
try {
|
|
1806
|
+
backgroundOwnership = this._requireBackgroundOwner();
|
|
1807
|
+
} catch (error) {
|
|
1808
|
+
await this._enterContainment(
|
|
1809
|
+
'background-ownership-unknown',
|
|
1810
|
+
error,
|
|
1811
|
+
);
|
|
1812
|
+
throw error;
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1799
1815
|
const controlDeadline = controlDeadlineOverride
|
|
1800
1816
|
?? pending?.deadlineAt
|
|
1801
|
-
??
|
|
1817
|
+
?? backgroundOwnership?.owner.deadlineAt
|
|
1802
1818
|
?? (Date.now() + this.interruptTimeoutMs + this.cleanupTimeoutMs);
|
|
1803
1819
|
let turnId = this.activeTurnId
|
|
1804
1820
|
?? pending?.turnId
|
|
1805
|
-
??
|
|
1806
|
-
|
|
1807
|
-
priorState === 'BackgroundWorking'
|
|
1808
|
-
|| priorState === 'BackgroundSettling'
|
|
1809
|
-
)
|
|
1810
|
-
? this.lastTerminal?.turnId
|
|
1811
|
-
: null
|
|
1812
|
-
);
|
|
1821
|
+
?? backgroundOwnership?.owner.turnId
|
|
1822
|
+
?? null;
|
|
1813
1823
|
if (pending && priorState === 'StartingTurn') {
|
|
1814
1824
|
pending.startCancellationRequested = true;
|
|
1815
1825
|
}
|
|
@@ -1899,6 +1909,7 @@ class CodexProcess extends Process {
|
|
|
1899
1909
|
const reuseBackgroundCleanup = (
|
|
1900
1910
|
cleanupProof?.turnId != null
|
|
1901
1911
|
&& cleanupProof.turnId === turnId
|
|
1912
|
+
&& cleanupProof.deadlineAt === backgroundOwnership?.owner.deadlineAt
|
|
1902
1913
|
);
|
|
1903
1914
|
let terminal = pending?.terminal
|
|
1904
1915
|
?? (
|
|
@@ -1993,6 +2004,7 @@ class CodexProcess extends Process {
|
|
|
1993
2004
|
turnId,
|
|
1994
2005
|
});
|
|
1995
2006
|
}
|
|
2007
|
+
this.backgroundOwner = null;
|
|
1996
2008
|
this.state = 'Stopped';
|
|
1997
2009
|
this.inFlight = false;
|
|
1998
2010
|
this.emit('codex-settled', this._lifecyclePayload('stopped', {
|
|
@@ -2055,6 +2067,8 @@ class CodexProcess extends Process {
|
|
|
2055
2067
|
|| this.state === 'FailedAmbiguous'
|
|
2056
2068
|
|| this.state === 'DurabilityBlocked'
|
|
2057
2069
|
) return;
|
|
2070
|
+
if (this.state !== 'Settling' || this.current !== pending) return;
|
|
2071
|
+
const probeState = this.state;
|
|
2058
2072
|
const page = await this.client.request(
|
|
2059
2073
|
'thread/backgroundTerminals/list',
|
|
2060
2074
|
{ threadId: this.providerSessionId, limit: 100 },
|
|
@@ -2063,9 +2077,11 @@ class CodexProcess extends Process {
|
|
|
2063
2077
|
},
|
|
2064
2078
|
);
|
|
2065
2079
|
this._assertLifecycle(lifecycle);
|
|
2080
|
+
if (this.state !== probeState || this.current !== pending) return;
|
|
2066
2081
|
if (page.count > 0 || page.nextCursor != null) {
|
|
2082
|
+
this._bindBackgroundOwnerFromPending(pending);
|
|
2067
2083
|
this._activateBackgroundOwnership();
|
|
2068
|
-
this._armBackgroundWatchdog(
|
|
2084
|
+
this._armBackgroundWatchdog();
|
|
2069
2085
|
if (this.threadStatusType === 'idle') {
|
|
2070
2086
|
this._scheduleBackgroundSettlement();
|
|
2071
2087
|
}
|
|
@@ -2073,12 +2089,130 @@ class CodexProcess extends Process {
|
|
|
2073
2089
|
}
|
|
2074
2090
|
|
|
2075
2091
|
_activateBackgroundOwnership() {
|
|
2092
|
+
this._requireBackgroundOwner();
|
|
2076
2093
|
if (!this.hasActiveBackgroundWork()) {
|
|
2077
2094
|
this._advanceActivityEpoch();
|
|
2078
2095
|
this.state = 'BackgroundWorking';
|
|
2079
2096
|
}
|
|
2080
2097
|
}
|
|
2081
2098
|
|
|
2099
|
+
_backgroundOwnershipError(message) {
|
|
2100
|
+
return processError(message, 'CODEX_BACKGROUND_OWNERSHIP_UNKNOWN');
|
|
2101
|
+
}
|
|
2102
|
+
|
|
2103
|
+
_ownerFromTerminal(terminal, { requireLive = false } = {}) {
|
|
2104
|
+
if (
|
|
2105
|
+
!terminal
|
|
2106
|
+
|| !Number.isSafeInteger(terminal.deadlineAt)
|
|
2107
|
+
|| terminal.deadlineAt < 1
|
|
2108
|
+
|| !['completed', 'interrupted', 'failed'].includes(terminal.status)
|
|
2109
|
+
|| (requireLive && terminal.deadlineAt <= Date.now())
|
|
2110
|
+
) {
|
|
2111
|
+
throw this._backgroundOwnershipError(
|
|
2112
|
+
'Codex background work had no exact live terminal owner',
|
|
2113
|
+
);
|
|
2114
|
+
}
|
|
2115
|
+
let turnId;
|
|
2116
|
+
try {
|
|
2117
|
+
turnId = boundedOpaqueId(terminal.turnId, 'background owner turnId');
|
|
2118
|
+
} catch {
|
|
2119
|
+
throw this._backgroundOwnershipError(
|
|
2120
|
+
'Codex background work had no exact live terminal owner',
|
|
2121
|
+
);
|
|
2122
|
+
}
|
|
2123
|
+
return Object.freeze({
|
|
2124
|
+
turnId,
|
|
2125
|
+
deadlineAt: terminal.deadlineAt,
|
|
2126
|
+
});
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
_bindBackgroundOwner(terminal, options = {}) {
|
|
2130
|
+
const owner = this._ownerFromTerminal(terminal, options);
|
|
2131
|
+
if (this.backgroundOwner) {
|
|
2132
|
+
if (
|
|
2133
|
+
!Object.isFrozen(this.backgroundOwner)
|
|
2134
|
+
|| this.backgroundOwner.turnId !== owner.turnId
|
|
2135
|
+
|| this.backgroundOwner.deadlineAt !== owner.deadlineAt
|
|
2136
|
+
) {
|
|
2137
|
+
throw this._backgroundOwnershipError(
|
|
2138
|
+
'Codex background owner changed before cleanup',
|
|
2139
|
+
);
|
|
2140
|
+
}
|
|
2141
|
+
return this.backgroundOwner;
|
|
2142
|
+
}
|
|
2143
|
+
this.backgroundCleanupProof = null;
|
|
2144
|
+
this.backgroundOwner = owner;
|
|
2145
|
+
return owner;
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
_bindBackgroundOwnerFromPending(pending) {
|
|
2149
|
+
const terminal = pending?.terminal;
|
|
2150
|
+
if (
|
|
2151
|
+
pending !== this.current
|
|
2152
|
+
|| !terminal
|
|
2153
|
+
|| terminal !== this.lastTerminal
|
|
2154
|
+
|| terminal.turnId !== pending.turnId
|
|
2155
|
+
|| terminal.deadlineAt !== pending.deadlineAt
|
|
2156
|
+
) {
|
|
2157
|
+
throw this._backgroundOwnershipError(
|
|
2158
|
+
'Codex background registry had no exact pending terminal owner',
|
|
2159
|
+
);
|
|
2160
|
+
}
|
|
2161
|
+
return this._bindBackgroundOwner(terminal);
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
_captureBackgroundPromotionCandidate() {
|
|
2165
|
+
const terminal = this.lastTerminal;
|
|
2166
|
+
if (!terminal) return null;
|
|
2167
|
+
return Object.freeze({
|
|
2168
|
+
terminal,
|
|
2169
|
+
turnId: terminal.turnId,
|
|
2170
|
+
status: terminal.status,
|
|
2171
|
+
deadlineAt: terminal.deadlineAt,
|
|
2172
|
+
});
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
_promoteBackgroundCandidateToOwner(admission) {
|
|
2176
|
+
const candidate = admission?.candidate;
|
|
2177
|
+
const terminal = this.lastTerminal;
|
|
2178
|
+
if (
|
|
2179
|
+
!admission
|
|
2180
|
+
|| this.activityEpoch !== admission.activityEpoch
|
|
2181
|
+
|| this.state !== admission.state
|
|
2182
|
+
|| this.current !== admission.current
|
|
2183
|
+
|| !candidate
|
|
2184
|
+
|| candidate.terminal !== terminal
|
|
2185
|
+
|| candidate.turnId !== terminal?.turnId
|
|
2186
|
+
|| candidate.status !== terminal?.status
|
|
2187
|
+
|| candidate.deadlineAt !== terminal?.deadlineAt
|
|
2188
|
+
) {
|
|
2189
|
+
throw this._backgroundOwnershipError(
|
|
2190
|
+
'Codex background status candidate changed before ownership',
|
|
2191
|
+
);
|
|
2192
|
+
}
|
|
2193
|
+
return this._bindBackgroundOwner(terminal, {
|
|
2194
|
+
requireLive: true,
|
|
2195
|
+
});
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
_requireBackgroundOwner() {
|
|
2199
|
+
const owner = this.backgroundOwner;
|
|
2200
|
+
const terminal = this.lastTerminal;
|
|
2201
|
+
if (
|
|
2202
|
+
!owner
|
|
2203
|
+
|| !Object.isFrozen(owner)
|
|
2204
|
+
|| !terminal
|
|
2205
|
+
|| owner.turnId !== terminal.turnId
|
|
2206
|
+
|| owner.deadlineAt !== terminal.deadlineAt
|
|
2207
|
+
) {
|
|
2208
|
+
throw this._backgroundOwnershipError(
|
|
2209
|
+
'Codex background work had missing or mismatched ownership',
|
|
2210
|
+
);
|
|
2211
|
+
}
|
|
2212
|
+
this._ownerFromTerminal(terminal);
|
|
2213
|
+
return { owner, terminal };
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2082
2216
|
_qualificationFenceError() {
|
|
2083
2217
|
return processError(
|
|
2084
2218
|
'Codex clean-restart qualification fence changed',
|
|
@@ -2517,15 +2651,38 @@ class CodexProcess extends Process {
|
|
|
2517
2651
|
&& !this.current
|
|
2518
2652
|
&& this.state === 'Idle'
|
|
2519
2653
|
);
|
|
2654
|
+
const backgroundCandidate = activateBackground
|
|
2655
|
+
? this._captureBackgroundPromotionCandidate()
|
|
2656
|
+
: null;
|
|
2520
2657
|
if (activateBackground) this._advanceActivityEpoch();
|
|
2658
|
+
const backgroundAdmission = activateBackground
|
|
2659
|
+
? Object.freeze({
|
|
2660
|
+
candidate: backgroundCandidate,
|
|
2661
|
+
activityEpoch: this.activityEpoch,
|
|
2662
|
+
state: this.state,
|
|
2663
|
+
current: this.current,
|
|
2664
|
+
})
|
|
2665
|
+
: null;
|
|
2521
2666
|
await this._checkpoint('thread-status-changed', {
|
|
2522
2667
|
threadId: ownedThreadId,
|
|
2523
2668
|
statusType,
|
|
2524
2669
|
}, fence);
|
|
2525
2670
|
fence.assertActive?.();
|
|
2526
2671
|
this.threadStatusType = statusType;
|
|
2527
|
-
if (activateBackground
|
|
2672
|
+
if (activateBackground) {
|
|
2673
|
+
try {
|
|
2674
|
+
this._promoteBackgroundCandidateToOwner(backgroundAdmission);
|
|
2675
|
+
} catch (error) {
|
|
2676
|
+
await this._enterContainment(
|
|
2677
|
+
'background-ownership-unknown',
|
|
2678
|
+
error,
|
|
2679
|
+
null,
|
|
2680
|
+
fence,
|
|
2681
|
+
);
|
|
2682
|
+
return;
|
|
2683
|
+
}
|
|
2528
2684
|
this.state = 'BackgroundWorking';
|
|
2685
|
+
this._armBackgroundWatchdog();
|
|
2529
2686
|
} else if (
|
|
2530
2687
|
statusType === 'idle'
|
|
2531
2688
|
&& this.state === 'BackgroundWorking'
|
|
@@ -2827,19 +2984,13 @@ class CodexProcess extends Process {
|
|
|
2827
2984
|
|
|
2828
2985
|
async _settleNaturalBackgroundWork() {
|
|
2829
2986
|
const lifecycle = this._captureLifecycle();
|
|
2830
|
-
const terminal = this.lastTerminal;
|
|
2831
|
-
const deadline = terminal?.deadlineAt ?? Date.now();
|
|
2832
2987
|
try {
|
|
2833
2988
|
this._assertLifecycle(lifecycle);
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
'Codex background work had no owned terminal turn',
|
|
2837
|
-
'CODEX_BACKGROUND_OWNERSHIP_UNKNOWN',
|
|
2838
|
-
);
|
|
2839
|
-
}
|
|
2989
|
+
const { owner, terminal } = this._requireBackgroundOwner();
|
|
2990
|
+
const deadline = owner.deadlineAt;
|
|
2840
2991
|
await this._checkpoint('background-terminal-reconciled', {
|
|
2841
2992
|
threadId: this.providerSessionId,
|
|
2842
|
-
turnId:
|
|
2993
|
+
turnId: owner.turnId,
|
|
2843
2994
|
terminalStatus: terminal.status,
|
|
2844
2995
|
});
|
|
2845
2996
|
this._assertLifecycle(lifecycle);
|
|
@@ -2847,35 +2998,36 @@ class CodexProcess extends Process {
|
|
|
2847
2998
|
threadId: this.providerSessionId,
|
|
2848
2999
|
}, {
|
|
2849
3000
|
source: null,
|
|
2850
|
-
turnId:
|
|
3001
|
+
turnId: owner.turnId,
|
|
2851
3002
|
timeoutMs: Math.max(1, deadline - Date.now()),
|
|
2852
3003
|
});
|
|
2853
3004
|
this._assertLifecycle(lifecycle);
|
|
2854
3005
|
await this._checkpoint('background-clean-accepted', {
|
|
2855
3006
|
threadId: this.providerSessionId,
|
|
2856
|
-
turnId:
|
|
3007
|
+
turnId: owner.turnId,
|
|
2857
3008
|
});
|
|
2858
3009
|
this._assertLifecycle(lifecycle);
|
|
2859
3010
|
await this._waitForFreshEmptyRegistry(deadline, lifecycle);
|
|
2860
3011
|
this._assertLifecycle(lifecycle);
|
|
2861
3012
|
await this._checkpoint('background-empty-registry-observed', {
|
|
2862
3013
|
threadId: this.providerSessionId,
|
|
2863
|
-
turnId:
|
|
3014
|
+
turnId: owner.turnId,
|
|
2864
3015
|
});
|
|
2865
3016
|
this._assertLifecycle(lifecycle);
|
|
2866
3017
|
this.backgroundCleanupProof = Object.freeze({
|
|
2867
|
-
turnId:
|
|
3018
|
+
turnId: owner.turnId,
|
|
2868
3019
|
terminalStatus: terminal.status,
|
|
2869
3020
|
deadlineAt: deadline,
|
|
2870
3021
|
});
|
|
2871
3022
|
this._clearBackgroundWatchdog();
|
|
3023
|
+
this.backgroundOwner = null;
|
|
2872
3024
|
if (this.state === 'Quiescing') return this.backgroundCleanupProof;
|
|
2873
3025
|
this.state = 'Idle';
|
|
2874
3026
|
this.inFlight = false;
|
|
2875
3027
|
this.emit('codex-settled', this._lifecyclePayload(
|
|
2876
3028
|
'background-settled',
|
|
2877
3029
|
{
|
|
2878
|
-
turnId:
|
|
3030
|
+
turnId: owner.turnId,
|
|
2879
3031
|
terminalStatus: terminal.status,
|
|
2880
3032
|
trackedTerminalCleanupAccepted: true,
|
|
2881
3033
|
freshRegistryObservedEmpty: true,
|
|
@@ -2910,8 +3062,12 @@ class CodexProcess extends Process {
|
|
|
2910
3062
|
this.backgroundSettlementPromise.catch(() => {});
|
|
2911
3063
|
}
|
|
2912
3064
|
|
|
2913
|
-
_armBackgroundWatchdog(
|
|
2914
|
-
|
|
3065
|
+
_armBackgroundWatchdog() {
|
|
3066
|
+
const { owner } = this._requireBackgroundOwner();
|
|
3067
|
+
const { deadlineAt } = owner;
|
|
3068
|
+
const retirementReserveMs = (
|
|
3069
|
+
this.interruptTimeoutMs + this.cleanupTimeoutMs
|
|
3070
|
+
);
|
|
2915
3071
|
if (
|
|
2916
3072
|
this.backgroundWatchdog
|
|
2917
3073
|
&& this.backgroundWatchdogDeadlineAt === deadlineAt
|
|
@@ -2925,10 +3081,22 @@ class CodexProcess extends Process {
|
|
|
2925
3081
|
this.state !== 'BackgroundWorking'
|
|
2926
3082
|
&& this.state !== 'BackgroundSettling'
|
|
2927
3083
|
) return;
|
|
3084
|
+
let currentOwner;
|
|
3085
|
+
try {
|
|
3086
|
+
currentOwner = this._requireBackgroundOwner().owner;
|
|
3087
|
+
} catch (error) {
|
|
3088
|
+
this.backgroundWatchdogPromise = this._enterContainment(
|
|
3089
|
+
'background-ownership-unknown',
|
|
3090
|
+
error,
|
|
3091
|
+
).finally(() => {
|
|
3092
|
+
this.backgroundWatchdogPromise = null;
|
|
3093
|
+
});
|
|
3094
|
+
return;
|
|
3095
|
+
}
|
|
2928
3096
|
if (!this.interruptPromise) {
|
|
2929
3097
|
this.interruptPromise = this._interrupt(
|
|
2930
3098
|
'timeout',
|
|
2931
|
-
|
|
3099
|
+
currentOwner.deadlineAt,
|
|
2932
3100
|
);
|
|
2933
3101
|
}
|
|
2934
3102
|
this.backgroundWatchdogPromise = this.interruptPromise
|
|
@@ -2946,7 +3114,7 @@ class CodexProcess extends Process {
|
|
|
2946
3114
|
.finally(() => {
|
|
2947
3115
|
this.backgroundWatchdogPromise = null;
|
|
2948
3116
|
});
|
|
2949
|
-
}, Math.max(1, deadlineAt - Date.now()));
|
|
3117
|
+
}, Math.max(1, deadlineAt - retirementReserveMs - Date.now()));
|
|
2950
3118
|
this.backgroundWatchdog.unref?.();
|
|
2951
3119
|
}
|
|
2952
3120
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shumkov/orchestra",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.19",
|
|
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",
|