@fitlab-ai/agent-infra 0.9.4 → 0.9.5
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/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/dist/bin/internal-cli.js +10 -0
- package/dist/lib/agent-clients/adapter.js +2 -2
- package/dist/lib/agent-clients/adapters/codex-lifecycle/app-server.js +382 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/evidence.js +163 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/store.js +211 -0
- package/dist/lib/agent-clients/adapters/codex.js +2 -2
- package/dist/lib/git/worktree-identity.js +64 -0
- package/dist/lib/internal/codex-lifecycle.js +202 -0
- package/dist/lib/internal/task-create.js +48 -0
- package/dist/lib/internal/task-orchestration.js +68 -29
- package/dist/lib/sandbox/commands/create.js +1 -0
- package/dist/lib/sandbox/control/client.js +29 -15
- package/dist/lib/sandbox/control/protocol.js +23 -4
- package/dist/lib/sandbox/control/server.js +53 -18
- package/dist/lib/sandbox/workspace-view.js +8 -2
- package/dist/lib/task/activity-log.js +32 -5
- package/dist/lib/task/commit-finalization.js +14 -3
- package/dist/lib/task/create-service.js +137 -0
- package/dist/lib/task/create.js +333 -0
- package/dist/lib/task/orchestration.js +151 -15
- package/dist/lib/task/review-fingerprint.js +4 -0
- package/dist/lib/task/workspace-snapshot.js +78 -3
- package/lib/agent-clients/adapter.ts +4 -4
- package/lib/agent-clients/adapters/codex-lifecycle/app-server.ts +442 -0
- package/lib/agent-clients/adapters/codex-lifecycle/evidence.ts +307 -0
- package/lib/agent-clients/adapters/codex-lifecycle/store.ts +250 -0
- package/lib/agent-clients/adapters/codex.ts +2 -2
- package/lib/git/worktree-identity.ts +86 -0
- package/lib/internal/codex-lifecycle.ts +204 -0
- package/lib/internal/task-create.ts +49 -0
- package/lib/internal/task-orchestration.ts +54 -28
- package/lib/sandbox/commands/create.ts +1 -0
- package/lib/sandbox/control/client.ts +42 -18
- package/lib/sandbox/control/protocol.ts +43 -8
- package/lib/sandbox/control/server.ts +60 -22
- package/lib/sandbox/workspace-view.ts +9 -2
- package/lib/task/activity-log.ts +46 -7
- package/lib/task/commit-finalization.ts +23 -3
- package/lib/task/create-service.ts +171 -0
- package/lib/task/create.ts +372 -0
- package/lib/task/orchestration.ts +182 -18
- package/lib/task/review-fingerprint.ts +9 -0
- package/lib/task/workspace-snapshot.ts +98 -4
- package/package.json +7 -7
- package/templates/.agents/hooks/lifecycle-delegation.js +47 -3
- package/templates/.agents/rules/task-management.en.md +1 -0
- package/templates/.agents/rules/task-management.zh-CN.md +1 -0
- package/templates/.agents/skills/commit/SKILL.en.md +3 -9
- package/templates/.agents/skills/commit/SKILL.zh-CN.md +6 -10
- package/templates/.agents/skills/commit/reference/commit-orchestration.en.md +4 -3
- package/templates/.agents/skills/commit/reference/commit-orchestration.zh-CN.md +5 -4
- package/templates/.agents/skills/create-task/SKILL.en.md +26 -43
- package/templates/.agents/skills/create-task/SKILL.zh-CN.md +26 -43
- package/templates/.codex/hooks.json +50 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
type CodexEvidenceSource =
|
|
2
|
+
| 'codex-hook'
|
|
3
|
+
| 'codex-app-server-thread'
|
|
4
|
+
| 'codex-app-server-settings'
|
|
5
|
+
| 'codex-app-server-turn'
|
|
6
|
+
| 'codex-app-server-reroute';
|
|
7
|
+
|
|
8
|
+
type CodexLifecycleEvent =
|
|
9
|
+
| Readonly<{
|
|
10
|
+
type: 'hook-spawn';
|
|
11
|
+
sessionId: string;
|
|
12
|
+
turnId: string;
|
|
13
|
+
toolUseId: string;
|
|
14
|
+
nativeAgent: string;
|
|
15
|
+
requestedModel?: string;
|
|
16
|
+
requestedReasoningEffort?: string;
|
|
17
|
+
hookDefinitionHash: string;
|
|
18
|
+
}>
|
|
19
|
+
| Readonly<{
|
|
20
|
+
type: 'hook-child';
|
|
21
|
+
sessionId: string;
|
|
22
|
+
turnId: string;
|
|
23
|
+
childThreadId: string;
|
|
24
|
+
nativeAgent: string;
|
|
25
|
+
}>
|
|
26
|
+
| Readonly<{
|
|
27
|
+
type: 'app-thread';
|
|
28
|
+
childThreadId: string;
|
|
29
|
+
parentThreadId: string;
|
|
30
|
+
forkedFromId: string | null;
|
|
31
|
+
sourceParentThreadId: string;
|
|
32
|
+
}>
|
|
33
|
+
| Readonly<{
|
|
34
|
+
type: 'app-settings';
|
|
35
|
+
childThreadId: string;
|
|
36
|
+
model: string;
|
|
37
|
+
reasoningEffort: string;
|
|
38
|
+
}>
|
|
39
|
+
| Readonly<{
|
|
40
|
+
type: 'app-reroute';
|
|
41
|
+
childThreadId: string;
|
|
42
|
+
turnId: string;
|
|
43
|
+
fromModel: string;
|
|
44
|
+
toModel: string;
|
|
45
|
+
reason: string;
|
|
46
|
+
}>
|
|
47
|
+
| Readonly<{
|
|
48
|
+
type: 'app-terminal';
|
|
49
|
+
childThreadId: string;
|
|
50
|
+
turnId: string;
|
|
51
|
+
status: 'completed' | 'interrupted' | 'failed';
|
|
52
|
+
}>
|
|
53
|
+
| Readonly<{
|
|
54
|
+
type: 'hook-stop';
|
|
55
|
+
sessionId: string;
|
|
56
|
+
turnId: string;
|
|
57
|
+
childThreadId: string;
|
|
58
|
+
nativeAgent: string;
|
|
59
|
+
}>;
|
|
60
|
+
|
|
61
|
+
type CodexStartEvidence = Readonly<{
|
|
62
|
+
schemaVersion: 1;
|
|
63
|
+
cliVersion: string;
|
|
64
|
+
parentThreadId: string;
|
|
65
|
+
parentTurnId: string;
|
|
66
|
+
spawnToolUseId: string;
|
|
67
|
+
childThreadId: string;
|
|
68
|
+
nativeAgent: string;
|
|
69
|
+
spawnMode: 'fresh';
|
|
70
|
+
actualModel: Readonly<{ value: string; source: CodexEvidenceSource }>;
|
|
71
|
+
actualReasoningEffort: Readonly<{ value: string; source: CodexEvidenceSource }>;
|
|
72
|
+
modelFallbackReason: string | null;
|
|
73
|
+
reasoningEffortFallbackReason: string | null;
|
|
74
|
+
hookDefinitionHash: string;
|
|
75
|
+
}>;
|
|
76
|
+
|
|
77
|
+
type CodexStopEvidence = Readonly<{
|
|
78
|
+
schemaVersion: 1;
|
|
79
|
+
childThreadId: string;
|
|
80
|
+
turnId: string;
|
|
81
|
+
terminalStatus: 'completed';
|
|
82
|
+
hookStopObserved: true;
|
|
83
|
+
}>;
|
|
84
|
+
|
|
85
|
+
type CodexLifecycleStatus =
|
|
86
|
+
| 'empty'
|
|
87
|
+
| 'observed-spawn'
|
|
88
|
+
| 'observed-child'
|
|
89
|
+
| 'start-ready'
|
|
90
|
+
| 'observed-terminal'
|
|
91
|
+
| 'stop-ready'
|
|
92
|
+
| 'invalid'
|
|
93
|
+
| 'expired';
|
|
94
|
+
|
|
95
|
+
type CodexEvidenceError = Readonly<{ code: string; message: string }>;
|
|
96
|
+
|
|
97
|
+
type CodexLifecycleState = Readonly<{
|
|
98
|
+
schemaVersion: 1;
|
|
99
|
+
cliVersion: string;
|
|
100
|
+
status: CodexLifecycleStatus;
|
|
101
|
+
spawn: Extract<CodexLifecycleEvent, { type: 'hook-spawn' }> | null;
|
|
102
|
+
child: Extract<CodexLifecycleEvent, { type: 'hook-child' }> | null;
|
|
103
|
+
thread: Extract<CodexLifecycleEvent, { type: 'app-thread' }> | null;
|
|
104
|
+
settings: Extract<CodexLifecycleEvent, { type: 'app-settings' }> | null;
|
|
105
|
+
reroute: Extract<CodexLifecycleEvent, { type: 'app-reroute' }> | null;
|
|
106
|
+
terminal: Extract<CodexLifecycleEvent, { type: 'app-terminal' }> | null;
|
|
107
|
+
stop: Extract<CodexLifecycleEvent, { type: 'hook-stop' }> | null;
|
|
108
|
+
startEvidence: CodexStartEvidence | null;
|
|
109
|
+
stopEvidence: CodexStopEvidence | null;
|
|
110
|
+
error: CodexEvidenceError | null;
|
|
111
|
+
}>;
|
|
112
|
+
|
|
113
|
+
function createCodexLifecycleState(cliVersion: string): CodexLifecycleState {
|
|
114
|
+
if (!cliVersion.trim()) throw new Error('Codex CLI version is required');
|
|
115
|
+
return Object.freeze({
|
|
116
|
+
schemaVersion: 1,
|
|
117
|
+
cliVersion,
|
|
118
|
+
status: 'empty',
|
|
119
|
+
spawn: null,
|
|
120
|
+
child: null,
|
|
121
|
+
thread: null,
|
|
122
|
+
settings: null,
|
|
123
|
+
reroute: null,
|
|
124
|
+
terminal: null,
|
|
125
|
+
stop: null,
|
|
126
|
+
startEvidence: null,
|
|
127
|
+
stopEvidence: null,
|
|
128
|
+
error: null
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function invalid(state: CodexLifecycleState, code: string, message: string): CodexLifecycleState {
|
|
133
|
+
return Object.freeze({
|
|
134
|
+
...state,
|
|
135
|
+
status: 'invalid',
|
|
136
|
+
startEvidence: null,
|
|
137
|
+
stopEvidence: null,
|
|
138
|
+
error: Object.freeze({ code, message })
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function same(left: unknown, right: unknown): boolean {
|
|
143
|
+
return JSON.stringify(left) === JSON.stringify(right);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function requiredStrings(event: CodexLifecycleEvent): boolean {
|
|
147
|
+
return Object.entries(event).every(([key, value]) =>
|
|
148
|
+
key === 'forkedFromId'
|
|
149
|
+
|| (typeof value !== 'string' || value.trim() !== '')
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function derive(state: CodexLifecycleState): CodexLifecycleState {
|
|
154
|
+
const { spawn, child, thread, settings, reroute, terminal, stop } = state;
|
|
155
|
+
if (spawn && child && (
|
|
156
|
+
spawn.sessionId !== child.sessionId
|
|
157
|
+
|| spawn.nativeAgent !== child.nativeAgent
|
|
158
|
+
)) {
|
|
159
|
+
return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'hook spawn and child identity do not match');
|
|
160
|
+
}
|
|
161
|
+
if (child && thread && child.childThreadId !== thread.childThreadId) {
|
|
162
|
+
return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'hook and App Server child identity do not match');
|
|
163
|
+
}
|
|
164
|
+
if (spawn && thread && (
|
|
165
|
+
thread.parentThreadId !== spawn.sessionId
|
|
166
|
+
|| thread.sourceParentThreadId !== spawn.sessionId
|
|
167
|
+
)) {
|
|
168
|
+
return invalid(state, 'CODEX_EVIDENCE_PARENT_MISMATCH', 'App Server parent identity does not match the spawning session');
|
|
169
|
+
}
|
|
170
|
+
if (thread?.forkedFromId !== null && thread?.forkedFromId !== undefined) {
|
|
171
|
+
return invalid(state, 'CODEX_EVIDENCE_FORK_FORBIDDEN', 'forked child threads are not fresh lifecycle evidence');
|
|
172
|
+
}
|
|
173
|
+
if (settings && child && settings.childThreadId !== child.childThreadId) {
|
|
174
|
+
return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'resolved settings belong to a different child');
|
|
175
|
+
}
|
|
176
|
+
if (reroute && child && reroute.childThreadId !== child.childThreadId) {
|
|
177
|
+
return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'model reroute belongs to a different child');
|
|
178
|
+
}
|
|
179
|
+
if (spawn?.requestedModel && settings && spawn.requestedModel !== settings.model) {
|
|
180
|
+
if (
|
|
181
|
+
!reroute
|
|
182
|
+
|| reroute.fromModel !== spawn.requestedModel
|
|
183
|
+
|| reroute.toModel !== settings.model
|
|
184
|
+
|| !reroute.reason.trim()
|
|
185
|
+
) {
|
|
186
|
+
return invalid(state, 'CODEX_EVIDENCE_MODEL_UNEXPLAINED', 'resolved model differs without a matching host reroute');
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (
|
|
190
|
+
spawn?.requestedReasoningEffort
|
|
191
|
+
&& settings
|
|
192
|
+
&& spawn.requestedReasoningEffort !== settings.reasoningEffort
|
|
193
|
+
) {
|
|
194
|
+
return invalid(state, 'CODEX_EVIDENCE_EFFORT_UNEXPLAINED', 'resolved reasoning effort differs without structured host evidence');
|
|
195
|
+
}
|
|
196
|
+
if (terminal && terminal.status !== 'completed') {
|
|
197
|
+
return invalid(state, 'CODEX_EVIDENCE_TERMINAL_INVALID', `child turn ended as '${terminal.status}'`);
|
|
198
|
+
}
|
|
199
|
+
if (terminal && child && terminal.childThreadId !== child.childThreadId) {
|
|
200
|
+
return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'terminal event belongs to a different child');
|
|
201
|
+
}
|
|
202
|
+
if (stop && child && (
|
|
203
|
+
stop.sessionId !== child.sessionId
|
|
204
|
+
|| stop.turnId !== child.turnId
|
|
205
|
+
|| stop.childThreadId !== child.childThreadId
|
|
206
|
+
|| stop.nativeAgent !== child.nativeAgent
|
|
207
|
+
)) {
|
|
208
|
+
return invalid(state, 'CODEX_EVIDENCE_IDENTITY_MISMATCH', 'hook stop identity does not match the child');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
let startEvidence: CodexStartEvidence | null = null;
|
|
212
|
+
if (spawn && child && thread && settings) {
|
|
213
|
+
startEvidence = Object.freeze({
|
|
214
|
+
schemaVersion: 1,
|
|
215
|
+
cliVersion: state.cliVersion,
|
|
216
|
+
parentThreadId: spawn.sessionId,
|
|
217
|
+
parentTurnId: spawn.turnId,
|
|
218
|
+
spawnToolUseId: spawn.toolUseId,
|
|
219
|
+
childThreadId: child.childThreadId,
|
|
220
|
+
nativeAgent: child.nativeAgent,
|
|
221
|
+
spawnMode: 'fresh',
|
|
222
|
+
actualModel: Object.freeze({ value: settings.model, source: 'codex-app-server-settings' }),
|
|
223
|
+
actualReasoningEffort: Object.freeze({ value: settings.reasoningEffort, source: 'codex-app-server-settings' }),
|
|
224
|
+
modelFallbackReason: reroute?.reason ?? null,
|
|
225
|
+
reasoningEffortFallbackReason: null,
|
|
226
|
+
hookDefinitionHash: spawn.hookDefinitionHash
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
let stopEvidence: CodexStopEvidence | null = null;
|
|
230
|
+
if (startEvidence && terminal?.status === 'completed' && stop) {
|
|
231
|
+
stopEvidence = Object.freeze({
|
|
232
|
+
schemaVersion: 1,
|
|
233
|
+
childThreadId: startEvidence.childThreadId,
|
|
234
|
+
turnId: terminal.turnId,
|
|
235
|
+
terminalStatus: 'completed',
|
|
236
|
+
hookStopObserved: true
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
const status: CodexLifecycleStatus = stopEvidence
|
|
240
|
+
? 'stop-ready'
|
|
241
|
+
: terminal
|
|
242
|
+
? 'observed-terminal'
|
|
243
|
+
: startEvidence
|
|
244
|
+
? 'start-ready'
|
|
245
|
+
: child
|
|
246
|
+
? 'observed-child'
|
|
247
|
+
: spawn
|
|
248
|
+
? 'observed-spawn'
|
|
249
|
+
: 'empty';
|
|
250
|
+
return Object.freeze({ ...state, status, startEvidence, stopEvidence, error: null });
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function reduceCodexLifecycleEvent(
|
|
254
|
+
state: CodexLifecycleState,
|
|
255
|
+
event: CodexLifecycleEvent
|
|
256
|
+
): CodexLifecycleState {
|
|
257
|
+
if (state.status === 'invalid' || state.status === 'expired') return state;
|
|
258
|
+
if (!requiredStrings(event)) {
|
|
259
|
+
return invalid(state, 'CODEX_EVIDENCE_FIELD_MISSING', 'lifecycle event contains an empty required field');
|
|
260
|
+
}
|
|
261
|
+
const key = ({
|
|
262
|
+
'hook-spawn': 'spawn',
|
|
263
|
+
'hook-child': 'child',
|
|
264
|
+
'app-thread': 'thread',
|
|
265
|
+
'app-settings': 'settings',
|
|
266
|
+
'app-reroute': 'reroute',
|
|
267
|
+
'app-terminal': 'terminal',
|
|
268
|
+
'hook-stop': 'stop'
|
|
269
|
+
} as const)[event.type];
|
|
270
|
+
const existing = state[key];
|
|
271
|
+
if (existing) {
|
|
272
|
+
if (same(existing, event)) return state;
|
|
273
|
+
return invalid(state, 'CODEX_EVIDENCE_REPLAY_CONFLICT', `conflicting '${event.type}' event`);
|
|
274
|
+
}
|
|
275
|
+
return derive(Object.freeze({ ...state, [key]: Object.freeze({ ...event }) }));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function expireCodexLifecycleState(state: CodexLifecycleState): CodexLifecycleState {
|
|
279
|
+
if (!['observed-spawn', 'observed-child', 'start-ready', 'observed-terminal'].includes(state.status)) {
|
|
280
|
+
return state;
|
|
281
|
+
}
|
|
282
|
+
return Object.freeze({
|
|
283
|
+
...state,
|
|
284
|
+
status: 'expired',
|
|
285
|
+
startEvidence: null,
|
|
286
|
+
stopEvidence: null,
|
|
287
|
+
error: Object.freeze({
|
|
288
|
+
code: 'CODEX_EVIDENCE_EXPIRED',
|
|
289
|
+
message: 'Codex lifecycle evidence expired before completion'
|
|
290
|
+
})
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export {
|
|
295
|
+
createCodexLifecycleState,
|
|
296
|
+
expireCodexLifecycleState,
|
|
297
|
+
reduceCodexLifecycleEvent
|
|
298
|
+
};
|
|
299
|
+
export type {
|
|
300
|
+
CodexEvidenceError,
|
|
301
|
+
CodexEvidenceSource,
|
|
302
|
+
CodexLifecycleEvent,
|
|
303
|
+
CodexLifecycleState,
|
|
304
|
+
CodexLifecycleStatus,
|
|
305
|
+
CodexStartEvidence,
|
|
306
|
+
CodexStopEvidence
|
|
307
|
+
};
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
createCodexLifecycleState,
|
|
7
|
+
expireCodexLifecycleState,
|
|
8
|
+
reduceCodexLifecycleEvent
|
|
9
|
+
} from './evidence.ts';
|
|
10
|
+
import type {
|
|
11
|
+
CodexLifecycleEvent,
|
|
12
|
+
CodexLifecycleState
|
|
13
|
+
} from './evidence.ts';
|
|
14
|
+
|
|
15
|
+
type StoredCodexLifecycle = Readonly<{
|
|
16
|
+
schemaVersion: 1;
|
|
17
|
+
revision: number;
|
|
18
|
+
state: CodexLifecycleState;
|
|
19
|
+
consumer: string | null;
|
|
20
|
+
consumedAt: string | null;
|
|
21
|
+
updatedAt: string;
|
|
22
|
+
}>;
|
|
23
|
+
|
|
24
|
+
type CodexLifecycleStoreOptions = Readonly<{
|
|
25
|
+
root?: string;
|
|
26
|
+
cliVersion: string;
|
|
27
|
+
now?: () => string;
|
|
28
|
+
}>;
|
|
29
|
+
|
|
30
|
+
type CodexLifecycleStoreResult = Readonly<{
|
|
31
|
+
path: string;
|
|
32
|
+
revision: number;
|
|
33
|
+
state: CodexLifecycleState;
|
|
34
|
+
}>;
|
|
35
|
+
|
|
36
|
+
const LOCK_RETRY_MS = 10;
|
|
37
|
+
const LOCK_TIMEOUT_MS = 1_000;
|
|
38
|
+
const LOCK_STALE_MS = 30_000;
|
|
39
|
+
|
|
40
|
+
function digest(value: string): string {
|
|
41
|
+
return crypto.createHash('sha256').update(value).digest('hex');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function readRecord(file: string): StoredCodexLifecycle {
|
|
45
|
+
const value = JSON.parse(fs.readFileSync(file, 'utf8')) as StoredCodexLifecycle;
|
|
46
|
+
if (
|
|
47
|
+
value.schemaVersion !== 1
|
|
48
|
+
|| !Number.isSafeInteger(value.revision)
|
|
49
|
+
|| value.revision < 1
|
|
50
|
+
|| !value.state
|
|
51
|
+
|| value.state.schemaVersion !== 1
|
|
52
|
+
) {
|
|
53
|
+
throw new Error(`Codex lifecycle record '${path.basename(file)}' is invalid`);
|
|
54
|
+
}
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function recordFiles(root: string): string[] {
|
|
59
|
+
if (!fs.existsSync(root)) return [];
|
|
60
|
+
return fs.readdirSync(root)
|
|
61
|
+
.filter((name) => /^[a-f0-9]{64}\.json$/.test(name))
|
|
62
|
+
.map((name) => path.join(root, name));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function writeRecord(file: string, record: StoredCodexLifecycle, expectedRevision: number): void {
|
|
66
|
+
const temp = `${file}.${process.pid}.${crypto.randomUUID()}.tmp`;
|
|
67
|
+
fs.writeFileSync(temp, `${JSON.stringify(record, null, 2)}\n`, {
|
|
68
|
+
encoding: 'utf8',
|
|
69
|
+
flag: 'wx',
|
|
70
|
+
mode: 0o600
|
|
71
|
+
});
|
|
72
|
+
try {
|
|
73
|
+
const actualRevision = fs.existsSync(file) ? readRecord(file).revision : 0;
|
|
74
|
+
if (actualRevision !== expectedRevision) {
|
|
75
|
+
throw new Error(`Codex lifecycle revision changed from ${expectedRevision} to ${actualRevision}`);
|
|
76
|
+
}
|
|
77
|
+
fs.renameSync(temp, file);
|
|
78
|
+
fs.chmodSync(file, 0o600);
|
|
79
|
+
} finally {
|
|
80
|
+
if (fs.existsSync(temp)) fs.unlinkSync(temp);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function createCodexLifecycleStore(options: CodexLifecycleStoreOptions) {
|
|
85
|
+
const root = options.root ?? path.join(process.cwd(), '.agents', 'workspace', '.runtime', 'codex-lifecycle');
|
|
86
|
+
const now = options.now ?? (() => new Date().toISOString());
|
|
87
|
+
fs.mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
88
|
+
fs.chmodSync(root, 0o700);
|
|
89
|
+
|
|
90
|
+
function withWriteLock<T>(operation: () => T): T {
|
|
91
|
+
const lock = path.join(root, '.write.lock');
|
|
92
|
+
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
93
|
+
let descriptor: number | null = null;
|
|
94
|
+
while (descriptor === null) {
|
|
95
|
+
try {
|
|
96
|
+
descriptor = fs.openSync(lock, 'wx', 0o600);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error;
|
|
99
|
+
try {
|
|
100
|
+
if (Date.now() - fs.statSync(lock).mtimeMs > LOCK_STALE_MS) {
|
|
101
|
+
fs.unlinkSync(lock);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
} catch (lockError) {
|
|
105
|
+
if ((lockError as NodeJS.ErrnoException).code === 'ENOENT') continue;
|
|
106
|
+
throw lockError;
|
|
107
|
+
}
|
|
108
|
+
if (Date.now() >= deadline) throw new Error('Codex lifecycle store is busy');
|
|
109
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, LOCK_RETRY_MS);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
return operation();
|
|
114
|
+
} finally {
|
|
115
|
+
fs.closeSync(descriptor);
|
|
116
|
+
fs.unlinkSync(lock);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function findByChild(childThreadId: string): string[] {
|
|
121
|
+
return recordFiles(root).filter((file) => {
|
|
122
|
+
const child = readRecord(file).state.child;
|
|
123
|
+
return child?.childThreadId === childThreadId;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function locate(event: CodexLifecycleEvent): string {
|
|
128
|
+
if (event.type === 'hook-spawn') {
|
|
129
|
+
return path.join(root, `${digest(`${event.sessionId}\0${event.turnId}\0${event.toolUseId}`)}.json`);
|
|
130
|
+
}
|
|
131
|
+
if (event.type === 'hook-child') {
|
|
132
|
+
const matches = recordFiles(root).filter((file) => {
|
|
133
|
+
const state = readRecord(file).state;
|
|
134
|
+
return state.spawn?.sessionId === event.sessionId
|
|
135
|
+
&& state.spawn.nativeAgent === event.nativeAgent
|
|
136
|
+
&& (!state.child || state.child.childThreadId === event.childThreadId);
|
|
137
|
+
});
|
|
138
|
+
if (matches.length !== 1) {
|
|
139
|
+
throw new Error(matches.length === 0
|
|
140
|
+
? 'Codex lifecycle parent session and agent correlation was not found'
|
|
141
|
+
: 'Codex lifecycle parent session and agent correlation is ambiguous');
|
|
142
|
+
}
|
|
143
|
+
return matches[0]!;
|
|
144
|
+
}
|
|
145
|
+
const matches = findByChild(event.childThreadId);
|
|
146
|
+
if (matches.length !== 1) {
|
|
147
|
+
throw new Error(matches.length === 0
|
|
148
|
+
? `Codex lifecycle child '${event.childThreadId}' was not found`
|
|
149
|
+
: `Codex lifecycle child '${event.childThreadId}' is ambiguous`);
|
|
150
|
+
}
|
|
151
|
+
return matches[0]!;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function apply(event: CodexLifecycleEvent): CodexLifecycleStoreResult {
|
|
155
|
+
return withWriteLock(() => {
|
|
156
|
+
const file = locate(event);
|
|
157
|
+
const current = fs.existsSync(file)
|
|
158
|
+
? readRecord(file)
|
|
159
|
+
: Object.freeze({
|
|
160
|
+
schemaVersion: 1 as const,
|
|
161
|
+
revision: 0,
|
|
162
|
+
state: createCodexLifecycleState(options.cliVersion),
|
|
163
|
+
consumer: null,
|
|
164
|
+
consumedAt: null,
|
|
165
|
+
updatedAt: now()
|
|
166
|
+
});
|
|
167
|
+
if (current.consumer) throw new Error(`Codex lifecycle evidence was already consumed by '${current.consumer}'`);
|
|
168
|
+
const nextState = reduceCodexLifecycleEvent(current.state, event);
|
|
169
|
+
const next = Object.freeze({
|
|
170
|
+
schemaVersion: 1 as const,
|
|
171
|
+
revision: current.revision + 1,
|
|
172
|
+
state: nextState,
|
|
173
|
+
consumer: null,
|
|
174
|
+
consumedAt: null,
|
|
175
|
+
updatedAt: now()
|
|
176
|
+
});
|
|
177
|
+
writeRecord(file, next, current.revision);
|
|
178
|
+
return Object.freeze({ path: file, revision: next.revision, state: next.state });
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function read(childThreadId: string): StoredCodexLifecycle {
|
|
183
|
+
const matches = findByChild(childThreadId);
|
|
184
|
+
if (matches.length !== 1) throw new Error(`Codex lifecycle child '${childThreadId}' was not found uniquely`);
|
|
185
|
+
return readRecord(matches[0]!);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function consume(
|
|
189
|
+
childThreadId: string,
|
|
190
|
+
consumer: string,
|
|
191
|
+
expectedHookDefinitionHash?: string
|
|
192
|
+
): StoredCodexLifecycle {
|
|
193
|
+
if (!consumer.trim()) throw new Error('Codex lifecycle consumer is required');
|
|
194
|
+
return withWriteLock(() => {
|
|
195
|
+
const matches = findByChild(childThreadId);
|
|
196
|
+
if (matches.length !== 1) throw new Error(`Codex lifecycle child '${childThreadId}' was not found uniquely`);
|
|
197
|
+
const file = matches[0]!;
|
|
198
|
+
const current = readRecord(file);
|
|
199
|
+
if (current.consumer) throw new Error(`Codex lifecycle evidence was already consumed by '${current.consumer}'`);
|
|
200
|
+
if (current.state.status !== 'stop-ready') throw new Error('Codex lifecycle evidence is not stop-ready');
|
|
201
|
+
if (
|
|
202
|
+
expectedHookDefinitionHash
|
|
203
|
+
&& current.state.startEvidence?.hookDefinitionHash !== expectedHookDefinitionHash
|
|
204
|
+
) throw new Error('Codex lifecycle hook definition hash is stale');
|
|
205
|
+
const next = Object.freeze({
|
|
206
|
+
...current,
|
|
207
|
+
revision: current.revision + 1,
|
|
208
|
+
consumer,
|
|
209
|
+
consumedAt: now(),
|
|
210
|
+
updatedAt: now()
|
|
211
|
+
});
|
|
212
|
+
writeRecord(file, next, current.revision);
|
|
213
|
+
return next;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function expireBefore(cutoff: string): number {
|
|
218
|
+
return withWriteLock(() => {
|
|
219
|
+
let changed = 0;
|
|
220
|
+
for (const file of recordFiles(root)) {
|
|
221
|
+
const current = readRecord(file);
|
|
222
|
+
if (current.updatedAt >= cutoff) continue;
|
|
223
|
+
if (current.consumer || ['invalid', 'expired', 'stop-ready'].includes(current.state.status)) {
|
|
224
|
+
fs.unlinkSync(file);
|
|
225
|
+
changed += 1;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const expiredState = expireCodexLifecycleState(current.state);
|
|
229
|
+
if (expiredState === current.state) continue;
|
|
230
|
+
writeRecord(file, Object.freeze({
|
|
231
|
+
...current,
|
|
232
|
+
revision: current.revision + 1,
|
|
233
|
+
state: expiredState,
|
|
234
|
+
updatedAt: now()
|
|
235
|
+
}), current.revision);
|
|
236
|
+
changed += 1;
|
|
237
|
+
}
|
|
238
|
+
return changed;
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return Object.freeze({ root, apply, consume, expireBefore, read });
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export { createCodexLifecycleStore };
|
|
246
|
+
export type {
|
|
247
|
+
CodexLifecycleStoreOptions,
|
|
248
|
+
CodexLifecycleStoreResult,
|
|
249
|
+
StoredCodexLifecycle
|
|
250
|
+
};
|
|
@@ -25,8 +25,8 @@ const codexAdapter = defineAgentClientAdapter({
|
|
|
25
25
|
guidance: 'Use the host model picker for the complete model and reasoning-effort catalog.'
|
|
26
26
|
},
|
|
27
27
|
delegationEvidence: {
|
|
28
|
-
actualModel: '
|
|
29
|
-
actualReasoningEffort: '
|
|
28
|
+
actualModel: 'app-server',
|
|
29
|
+
actualReasoningEffort: 'app-server'
|
|
30
30
|
},
|
|
31
31
|
project: {
|
|
32
32
|
ownedPathPrefixes: ['.codex/'],
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
type GitWorktreeIdentity = Readonly<{
|
|
6
|
+
worktreeRoot: string;
|
|
7
|
+
commonDir: string;
|
|
8
|
+
branch: string;
|
|
9
|
+
}>;
|
|
10
|
+
|
|
11
|
+
function normalizeCanonicalPath(value: string, platform: NodeJS.Platform = process.platform): string {
|
|
12
|
+
return platform === 'win32'
|
|
13
|
+
? path.win32.normalize(value).replaceAll('/', '\\').toLowerCase()
|
|
14
|
+
: path.posix.normalize(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function sameFilesystemEntry(left: string, right: string, platform: NodeJS.Platform = process.platform): boolean {
|
|
18
|
+
const leftReal = fs.realpathSync.native(left);
|
|
19
|
+
const rightReal = fs.realpathSync.native(right);
|
|
20
|
+
const leftStat = fs.statSync(leftReal, { bigint: true });
|
|
21
|
+
const rightStat = fs.statSync(rightReal, { bigint: true });
|
|
22
|
+
if (leftStat.ino !== 0n && rightStat.ino !== 0n) {
|
|
23
|
+
return leftStat.dev === rightStat.dev && leftStat.ino === rightStat.ino;
|
|
24
|
+
}
|
|
25
|
+
return normalizeCanonicalPath(leftReal, platform) === normalizeCanonicalPath(rightReal, platform);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function git(root: string, args: string[]): string {
|
|
29
|
+
return execFileSync('git', args, {
|
|
30
|
+
cwd: root,
|
|
31
|
+
encoding: 'utf8',
|
|
32
|
+
stdio: ['ignore', 'pipe', 'pipe']
|
|
33
|
+
}).trim();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function resolveGitWorktreeIdentity(worktreeRoot: string): GitWorktreeIdentity {
|
|
37
|
+
try {
|
|
38
|
+
const canonical = fs.realpathSync.native(worktreeRoot);
|
|
39
|
+
const topLevel = fs.realpathSync.native(git(canonical, ['rev-parse', '--show-toplevel']));
|
|
40
|
+
if (!sameFilesystemEntry(canonical, topLevel)) {
|
|
41
|
+
throw new Error('worktree root is not the Git top-level directory');
|
|
42
|
+
}
|
|
43
|
+
const commonDir = fs.realpathSync.native(git(canonical, [
|
|
44
|
+
'rev-parse', '--path-format=absolute', '--git-common-dir'
|
|
45
|
+
]));
|
|
46
|
+
const branch = git(canonical, ['symbolic-ref', '--short', 'HEAD']);
|
|
47
|
+
return { worktreeRoot: canonical, commonDir, branch };
|
|
48
|
+
} catch (error) {
|
|
49
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
50
|
+
throw new Error(`SANDBOX_CONTROL_WORKTREE_INVALID: ${message}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function assertGitWorktreeBinding(
|
|
55
|
+
stateRoot: string,
|
|
56
|
+
worktreeRoot: string,
|
|
57
|
+
expectedBranch: string
|
|
58
|
+
): GitWorktreeIdentity {
|
|
59
|
+
const state = resolveGitWorktreeIdentity(stateRoot);
|
|
60
|
+
const worktree = resolveGitWorktreeIdentity(worktreeRoot);
|
|
61
|
+
if (!sameFilesystemEntry(state.commonDir, worktree.commonDir)) {
|
|
62
|
+
throw new Error('SANDBOX_CONTROL_WORKTREE_INVALID: state and worktree roots have different Git common directories');
|
|
63
|
+
}
|
|
64
|
+
if (worktree.branch !== expectedBranch) {
|
|
65
|
+
throw new Error(`SANDBOX_CONTROL_WORKTREE_INVALID: expected branch '${expectedBranch}', got '${worktree.branch}'`);
|
|
66
|
+
}
|
|
67
|
+
return worktree;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function assertGitRepositoryBinding(stateRoot: string, worktreeRoot: string): GitWorktreeIdentity {
|
|
71
|
+
const state = resolveGitWorktreeIdentity(stateRoot);
|
|
72
|
+
const worktree = resolveGitWorktreeIdentity(worktreeRoot);
|
|
73
|
+
if (!sameFilesystemEntry(state.commonDir, worktree.commonDir)) {
|
|
74
|
+
throw new Error('SANDBOX_CONTROL_WORKTREE_INVALID: state and worktree roots have different Git common directories');
|
|
75
|
+
}
|
|
76
|
+
return worktree;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export {
|
|
80
|
+
assertGitWorktreeBinding,
|
|
81
|
+
assertGitRepositoryBinding,
|
|
82
|
+
normalizeCanonicalPath,
|
|
83
|
+
resolveGitWorktreeIdentity,
|
|
84
|
+
sameFilesystemEntry
|
|
85
|
+
};
|
|
86
|
+
export type { GitWorktreeIdentity };
|