@fitlab-ai/agent-infra 0.11.2 → 0.11.3
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/dist/bin/internal-cli.js +7 -2
- package/dist/lib/agent-clients/adapter.js +3 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/recovery.js +234 -0
- package/dist/lib/agent-clients/adapters/codex-lifecycle/sandbox-controller.js +8 -35
- package/dist/lib/agent-clients/adapters/codex-lifecycle/store.js +1 -34
- package/dist/lib/agent-clients/adapters/codex.js +10 -0
- package/dist/lib/internal/manual-validation.js +22 -27
- package/dist/lib/internal/sandbox-control.js +1 -43
- package/dist/lib/internal/task-lifecycle.js +1 -1
- package/dist/lib/internal/task-operation-registry.js +6 -2
- package/dist/lib/platform/issue-comments.js +2 -31
- package/dist/lib/platform/pr-summary.js +7 -2
- package/dist/lib/sandbox/control/client.js +3 -44
- package/dist/lib/sandbox/control/executor.js +14 -79
- package/dist/lib/sandbox/control/finalization-response.js +3 -5
- package/dist/lib/sandbox/control/protocol.js +5 -64
- package/dist/lib/sandbox/control/server.js +29 -206
- package/dist/lib/sandbox/control/state.js +1 -7
- package/dist/lib/sandbox/control/task-view.js +1 -2
- package/dist/lib/task/control-authority.js +34 -36
- package/dist/lib/task/control-recovery.js +3 -42
- package/dist/lib/task/delegation-receipts.js +2 -2
- package/dist/lib/task/finalization-artifacts.js +1 -3
- package/dist/lib/task/finalization-state.js +1 -1
- package/dist/lib/task/finalization.js +53 -126
- package/dist/lib/task/lifecycle.js +1 -1
- package/dist/lib/task/manual-validation-receipt.js +8 -3
- package/dist/lib/task/orchestration.js +12 -6
- package/dist/lifecycle-build-manifest.json +3 -3
- package/lib/agent-clients/adapter.ts +46 -0
- package/lib/agent-clients/adapters/codex-lifecycle/recovery.ts +297 -0
- package/lib/agent-clients/adapters/codex-lifecycle/sandbox-controller.ts +13 -33
- package/lib/agent-clients/adapters/codex-lifecycle/store.ts +1 -44
- package/lib/agent-clients/adapters/codex.ts +10 -0
- package/lib/internal/manual-validation.ts +22 -26
- package/lib/internal/sandbox-control.ts +0 -42
- package/lib/internal/task-lifecycle.ts +2 -2
- package/lib/internal/task-operation-registry.ts +7 -4
- package/lib/platform/issue-comments.ts +1 -35
- package/lib/platform/pr-summary.ts +10 -2
- package/lib/sandbox/control/client.ts +5 -68
- package/lib/sandbox/control/executor.ts +14 -81
- package/lib/sandbox/control/finalization-response.ts +3 -5
- package/lib/sandbox/control/protocol.ts +7 -83
- package/lib/sandbox/control/server.ts +30 -220
- package/lib/sandbox/control/state.ts +1 -9
- package/lib/sandbox/control/task-view.ts +1 -2
- package/lib/task/control-authority.ts +38 -47
- package/lib/task/control-recovery.ts +3 -43
- package/lib/task/delegation-receipts.ts +2 -2
- package/lib/task/finalization-artifacts.ts +1 -3
- package/lib/task/finalization-state.ts +1 -1
- package/lib/task/finalization.ts +57 -121
- package/lib/task/lifecycle.ts +2 -2
- package/lib/task/manual-validation-receipt.ts +8 -2
- package/lib/task/orchestration.ts +30 -19
- package/package.json +1 -1
- package/templates/.agents/rules/lifecycle-orchestration.en.md +19 -5
- package/templates/.agents/rules/lifecycle-orchestration.zh-CN.md +19 -5
- package/templates/.agents/skills/complete-manual-validation/SKILL.en.md +2 -2
- package/templates/.agents/skills/complete-manual-validation/SKILL.zh-CN.md +2 -2
- package/templates/.agents/skills/complete-task/SKILL.en.md +6 -7
- package/templates/.agents/skills/complete-task/SKILL.zh-CN.md +6 -7
- package/templates/.agents/skills/run-manual-validation/SKILL.en.md +2 -2
- package/templates/.agents/skills/run-manual-validation/SKILL.zh-CN.md +2 -2
- package/templates/.agents/skills/run-task/SKILL.en.md +9 -8
- package/templates/.agents/skills/run-task/SKILL.zh-CN.md +9 -8
- package/dist/lib/sandbox/control/task-workflow.js +0 -74
- package/dist/lib/sandbox/control/workflow-executor.js +0 -142
- package/dist/lib/task/lifecycle-recovery.js +0 -549
- package/dist/lib/task/recovery-warning.js +0 -19
- package/dist/lib/task/workflow-command.js +0 -21
- package/dist/lib/task/workflow-dispatch.js +0 -213
- package/lib/sandbox/control/task-workflow.ts +0 -95
- package/lib/sandbox/control/workflow-executor.ts +0 -156
- package/lib/task/lifecycle-recovery.ts +0 -691
- package/lib/task/recovery-warning.ts +0 -21
- package/lib/task/workflow-command.ts +0 -22
- package/lib/task/workflow-dispatch.ts +0 -214
|
@@ -1,549 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { createCodexLifecycleStore } from '../agent-clients/adapters/codex-lifecycle/store.js';
|
|
4
|
-
import { managedDelegationRole } from './delegation-receipts.js';
|
|
5
|
-
import { normalizeAgentToken } from '../agent-clients/tokens.js';
|
|
6
|
-
import { artifactName, parseArtifactName } from './artifact-name.js';
|
|
7
|
-
import { locateActivityLog, pairEntries } from './activity-log.js';
|
|
8
|
-
import { captureTaskWriteMetadata, writeTask } from './write.js';
|
|
9
|
-
import { parseTypedTaskFrontmatter } from './frontmatter.js';
|
|
10
|
-
import { resolveTaskRef, TASK_ID_RE } from './resolve-ref.js';
|
|
11
|
-
import { resolveAgentRuntimeStoreRoot } from '../runtime/agent-runtime.js';
|
|
12
|
-
import { recoverActivatedOrchestrationDelegationUnderLock, readRun } from './orchestration.js';
|
|
13
|
-
import { isRecoveryWarning, sameRecoveryWarning } from './recovery-warning.js';
|
|
14
|
-
const RECOVERY_NOTE_PREFIX = 'lifecycle-recovery:v1 ';
|
|
15
|
-
const RECOVERY_STAGES = ['analysis', 'review-analysis', 'plan', 'review-plan', 'code', 'review-code'];
|
|
16
|
-
const ACTIONS = {
|
|
17
|
-
analysis: 'Analyze Task',
|
|
18
|
-
'review-analysis': 'Review Analysis',
|
|
19
|
-
plan: 'Plan Task',
|
|
20
|
-
'review-plan': 'Review Plan',
|
|
21
|
-
code: 'Code Task',
|
|
22
|
-
'review-code': 'Review Code'
|
|
23
|
-
};
|
|
24
|
-
const RECOVERY_NOTE_KEYS = [
|
|
25
|
-
'version', 'taskId', 'stage', 'round', 'artifact', 'startedAgent', 'receiptId', 'childId',
|
|
26
|
-
'stopRevision', 'consumer', 'consumedAt', 'owner', 'reason'
|
|
27
|
-
];
|
|
28
|
-
const RECOVERY_RELEASE_RETRY_WARNING = Object.freeze({
|
|
29
|
-
code: 'RECOVERY_RELEASE_RETRY_REQUIRED',
|
|
30
|
-
message: 'recovery receipt and Activity Log are complete but the protected lifecycle claim could not be released',
|
|
31
|
-
action: 'retry recover-started with the same selector and reason'
|
|
32
|
-
});
|
|
33
|
-
function text(value) {
|
|
34
|
-
return typeof value === 'string' && value.length > 0 && value.trim() === value && !/[\r\n]/u.test(value);
|
|
35
|
-
}
|
|
36
|
-
function isReleaseRetryWarning(value) {
|
|
37
|
-
return sameRecoveryWarning(value, RECOVERY_RELEASE_RETRY_WARNING);
|
|
38
|
-
}
|
|
39
|
-
function timestamp(value) {
|
|
40
|
-
return text(value) && Number.isFinite(Date.parse(value));
|
|
41
|
-
}
|
|
42
|
-
function recoveryConsumer(taskId, receiptId) {
|
|
43
|
-
return `lifecycle-recovery:${taskId}:${receiptId}`;
|
|
44
|
-
}
|
|
45
|
-
function actionPattern(stage, round) {
|
|
46
|
-
const escaped = ACTIONS[stage].replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
|
|
47
|
-
return new RegExp(`^${escaped} \\(Round ${round}(?:, (?:fix for review-code(?:-r[2-9]|-r[1-9]\\d+)?.md|decision II-[1-9]\\d*))?\\)$`, 'u');
|
|
48
|
-
}
|
|
49
|
-
function baseStep(step) {
|
|
50
|
-
return step.replace(/\s*\[(?:started|aborted)\]\s*$/u, '');
|
|
51
|
-
}
|
|
52
|
-
function isRecoveryStage(value) {
|
|
53
|
-
return RECOVERY_STAGES.includes(value);
|
|
54
|
-
}
|
|
55
|
-
function parseRecoveryNote(note) {
|
|
56
|
-
if (!note.startsWith(RECOVERY_NOTE_PREFIX))
|
|
57
|
-
return null;
|
|
58
|
-
let value;
|
|
59
|
-
try {
|
|
60
|
-
value = JSON.parse(note.slice(RECOVERY_NOTE_PREFIX.length));
|
|
61
|
-
}
|
|
62
|
-
catch {
|
|
63
|
-
return null;
|
|
64
|
-
}
|
|
65
|
-
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
66
|
-
return null;
|
|
67
|
-
const record = value;
|
|
68
|
-
if (Object.keys(record).sort().join(',') !== [...RECOVERY_NOTE_KEYS].sort().join(','))
|
|
69
|
-
return null;
|
|
70
|
-
if (record.version !== 1
|
|
71
|
-
|| typeof record.taskId !== 'string' || !TASK_ID_RE.test(record.taskId)
|
|
72
|
-
|| !isRecoveryStage(record.stage)
|
|
73
|
-
|| !Number.isSafeInteger(record.round) || record.round < 1
|
|
74
|
-
|| !text(record.artifact)
|
|
75
|
-
|| !text(record.startedAgent)
|
|
76
|
-
|| !text(record.receiptId)
|
|
77
|
-
|| !text(record.childId)
|
|
78
|
-
|| !Number.isSafeInteger(record.stopRevision) || record.stopRevision < 1
|
|
79
|
-
|| !text(record.consumer)
|
|
80
|
-
|| !timestamp(record.consumedAt)
|
|
81
|
-
|| record.owner !== 'terminated'
|
|
82
|
-
|| !text(record.reason))
|
|
83
|
-
return null;
|
|
84
|
-
const parsedArtifact = parseArtifactName(record.artifact);
|
|
85
|
-
if (!parsedArtifact || parsedArtifact.family !== record.stage || parsedArtifact.round !== record.round)
|
|
86
|
-
return null;
|
|
87
|
-
if (record.consumer !== recoveryConsumer(record.taskId, record.receiptId))
|
|
88
|
-
return null;
|
|
89
|
-
return record;
|
|
90
|
-
}
|
|
91
|
-
function renderRecoveryNote(note) {
|
|
92
|
-
return `${RECOVERY_NOTE_PREFIX}${JSON.stringify(note)}`;
|
|
93
|
-
}
|
|
94
|
-
function result(request, status, extra = {}) {
|
|
95
|
-
return {
|
|
96
|
-
status,
|
|
97
|
-
changed: status === 'applied',
|
|
98
|
-
targetState: 'active',
|
|
99
|
-
requestRef: request.taskRef,
|
|
100
|
-
intent: request.intent,
|
|
101
|
-
taskId: null,
|
|
102
|
-
stage: request.stage,
|
|
103
|
-
round: request.round,
|
|
104
|
-
artifact: request.artifact,
|
|
105
|
-
receiptId: null,
|
|
106
|
-
childId: null,
|
|
107
|
-
warning: null,
|
|
108
|
-
error: null,
|
|
109
|
-
...extra
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
function failure(request, status, code, message, extra = {}) {
|
|
113
|
-
return result(request, status, { error: { code, message }, ...extra });
|
|
114
|
-
}
|
|
115
|
-
function recoveryFailure(request, status, code, message) {
|
|
116
|
-
return failure(request, status, code, message);
|
|
117
|
-
}
|
|
118
|
-
function normalizeRequest(request) {
|
|
119
|
-
if (!request || typeof request !== 'object'
|
|
120
|
-
|| request.intent !== 'recover-started'
|
|
121
|
-
|| typeof request.taskRef !== 'string' || !request.taskRef.trim()
|
|
122
|
-
|| typeof request.agent !== 'string'
|
|
123
|
-
|| !isRecoveryStage(request.stage)
|
|
124
|
-
|| !Number.isSafeInteger(request.round) || request.round < 1
|
|
125
|
-
|| typeof request.artifact !== 'string'
|
|
126
|
-
|| typeof request.reason !== 'string' || !request.reason.trim() || /[\r\n]/u.test(request.reason))
|
|
127
|
-
return { code: 'RECOVERY_PAYLOAD_INVALID', message: 'recover-started requires a task, agent, stage, round, artifact, and single-line reason' };
|
|
128
|
-
const agent = normalizeAgentToken(request.agent);
|
|
129
|
-
if (!agent)
|
|
130
|
-
return { code: 'RECOVERY_PAYLOAD_INVALID', message: 'recovery agent is not a recognized agent token' };
|
|
131
|
-
const expectedArtifact = artifactName(request.stage, request.round);
|
|
132
|
-
if (request.artifact !== expectedArtifact) {
|
|
133
|
-
return { code: 'RECOVERY_PAYLOAD_INVALID', message: `artifact must be ${expectedArtifact} for ${request.stage} round ${request.round}` };
|
|
134
|
-
}
|
|
135
|
-
return { ...request, agent, reason: request.reason.trim() };
|
|
136
|
-
}
|
|
137
|
-
function targetRows(sectionEntries, stage, round) {
|
|
138
|
-
const pattern = actionPattern(stage, round);
|
|
139
|
-
const rows = pairEntries([...sectionEntries]).filter((row) => pattern.test(row.step));
|
|
140
|
-
const recoveryEntries = sectionEntries.filter((entry) => entry.step.endsWith(' [aborted]') && pattern.test(baseStep(entry.step)));
|
|
141
|
-
return { rows, recoveryEntries };
|
|
142
|
-
}
|
|
143
|
-
function validateStopRecord(record, receipt, consumer) {
|
|
144
|
-
const start = record.state.startEvidence;
|
|
145
|
-
const stop = record.state.stopEvidence;
|
|
146
|
-
const provenance = receipt.lifecycleProvenance;
|
|
147
|
-
const host = receipt.hostEvidence;
|
|
148
|
-
if (receipt.client !== 'codex'
|
|
149
|
-
|| !provenance
|
|
150
|
-
|| host?.kind !== 'codex-lifecycle-v2'
|
|
151
|
-
|| !start
|
|
152
|
-
|| !stop
|
|
153
|
-
|| record.state.status !== 'stop-ready'
|
|
154
|
-
|| stop.terminalStatus !== 'completed'
|
|
155
|
-
|| stop.hookStopObserved !== true
|
|
156
|
-
|| start.childThreadId !== receipt.childId
|
|
157
|
-
|| start.parentThreadId !== receipt.parentId
|
|
158
|
-
|| managedDelegationRole(start.nativeAgent) !== receipt.role
|
|
159
|
-
|| start.hookDefinitionHash !== provenance.hookDefinitionHash
|
|
160
|
-
|| host.hookDefinitionHash !== provenance.hookDefinitionHash
|
|
161
|
-
|| host.capabilitySessionId !== provenance.capabilitySessionId
|
|
162
|
-
|| host.capabilityTurnId !== provenance.capabilityTurnId
|
|
163
|
-
|| host.capabilityToolUseId !== provenance.capabilityToolUseId
|
|
164
|
-
|| host.spawnToolUseId !== start.spawnToolUseId
|
|
165
|
-
|| !host.spawnObservedAt
|
|
166
|
-
|| !record.spawnObservedAt
|
|
167
|
-
|| host.spawnObservedAt !== record.spawnObservedAt
|
|
168
|
-
|| host.controllerInstanceDigest !== provenance.controllerInstanceDigest
|
|
169
|
-
|| host.controlGeneration !== provenance.controlGeneration
|
|
170
|
-
|| typeof host.controllerInstanceDigest !== 'string'
|
|
171
|
-
|| typeof host.controlGeneration !== 'string'
|
|
172
|
-
|| host.startRevision < 1
|
|
173
|
-
|| record.revision <= host.startRevision)
|
|
174
|
-
return { ok: false, code: 'RECOVERY_STOP_EVIDENCE_INVALID', message: 'Codex stop evidence does not match the managed activated receipt' };
|
|
175
|
-
if (record.state.child?.childThreadId !== receipt.childId || record.state.stop?.childThreadId !== receipt.childId) {
|
|
176
|
-
return { ok: false, code: 'RECOVERY_STOP_EVIDENCE_INVALID', message: 'Codex stop evidence child identity is inconsistent' };
|
|
177
|
-
}
|
|
178
|
-
if (record.consumer !== null && record.consumer !== consumer) {
|
|
179
|
-
return { ok: false, code: 'RECOVERY_CONSUMER_CONFLICT', message: `Codex lifecycle evidence was consumed by '${record.consumer}'` };
|
|
180
|
-
}
|
|
181
|
-
if (record.consumer === null)
|
|
182
|
-
return { ok: true, stopRevision: record.revision, consumedAt: '' };
|
|
183
|
-
if (receipt.status === 'activated' && record.consumer === consumer && record.consumedAt) {
|
|
184
|
-
return { ok: true, stopRevision: record.revision, consumedAt: record.consumedAt };
|
|
185
|
-
}
|
|
186
|
-
if (record.consumer !== consumer
|
|
187
|
-
|| !record.consumedAt
|
|
188
|
-
|| receipt.hostEvidence?.stopRevision !== record.revision
|
|
189
|
-
|| receipt.hostEvidence.consumer !== consumer
|
|
190
|
-
|| receipt.hostEvidence.consumedAt !== record.consumedAt)
|
|
191
|
-
return { ok: false, code: 'RECOVERY_REFERENCE_CONFLICT', message: 'recovery receipt references do not match the protected lifecycle claim' };
|
|
192
|
-
return { ok: true, stopRevision: record.revision, consumedAt: record.consumedAt };
|
|
193
|
-
}
|
|
194
|
-
function readLifecycleStore(options, repoRoot) {
|
|
195
|
-
if (options.lifecycleStore)
|
|
196
|
-
return options.lifecycleStore;
|
|
197
|
-
return createCodexLifecycleStore({
|
|
198
|
-
root: resolveAgentRuntimeStoreRoot({ repoRoot, store: 'lifecycle' }),
|
|
199
|
-
cliVersion: 'recovery',
|
|
200
|
-
now: options.now
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
function readStoredEvidence(store, childId) {
|
|
204
|
-
try {
|
|
205
|
-
return store.read(childId);
|
|
206
|
-
}
|
|
207
|
-
catch (error) {
|
|
208
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
209
|
-
if (message.includes('was not found uniquely'))
|
|
210
|
-
return { missing: true };
|
|
211
|
-
return { error: error instanceof Error ? error : new Error(message) };
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
function taskNote(taskId, request, receipt, stopRevision, consumer, consumedAt) {
|
|
215
|
-
return {
|
|
216
|
-
version: 1,
|
|
217
|
-
taskId,
|
|
218
|
-
stage: request.stage,
|
|
219
|
-
round: request.round,
|
|
220
|
-
artifact: request.artifact,
|
|
221
|
-
startedAgent: request.agent,
|
|
222
|
-
receiptId: receipt.id,
|
|
223
|
-
childId: receipt.childId,
|
|
224
|
-
stopRevision,
|
|
225
|
-
consumer,
|
|
226
|
-
consumedAt,
|
|
227
|
-
owner: 'terminated',
|
|
228
|
-
reason: request.reason
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
function releaseResult(request, taskId, receipt, released, previousChange) {
|
|
232
|
-
if (released)
|
|
233
|
-
return result(request, 'applied', { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
234
|
-
return result(request, 'applied', {
|
|
235
|
-
changed: previousChange,
|
|
236
|
-
taskId,
|
|
237
|
-
receiptId: receipt.id,
|
|
238
|
-
childId: receipt.childId,
|
|
239
|
-
warning: RECOVERY_RELEASE_RETRY_WARNING
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
function isPostActivationAbortedReceipt(receipt, taskId, request) {
|
|
243
|
-
return receipt.status === 'aborted'
|
|
244
|
-
&& receipt.activatedAt !== null
|
|
245
|
-
&& receipt.agent === null
|
|
246
|
-
&& receipt.taskId === taskId
|
|
247
|
-
&& receipt.stage === request.stage
|
|
248
|
-
&& receipt.round === request.round
|
|
249
|
-
&& receipt.artifact === request.artifact;
|
|
250
|
-
}
|
|
251
|
-
function matchingPostActivationAbortedReceipts(run, taskId, request) {
|
|
252
|
-
return run.receipts.filter((receipt) => isPostActivationAbortedReceipt(receipt, taskId, request));
|
|
253
|
-
}
|
|
254
|
-
function sameRecoveryNote(left, right) {
|
|
255
|
-
return left.version === right.version
|
|
256
|
-
&& left.taskId === right.taskId
|
|
257
|
-
&& left.stage === right.stage
|
|
258
|
-
&& left.round === right.round
|
|
259
|
-
&& left.artifact === right.artifact
|
|
260
|
-
&& left.startedAgent === right.startedAgent
|
|
261
|
-
&& left.receiptId === right.receiptId
|
|
262
|
-
&& left.childId === right.childId
|
|
263
|
-
&& left.stopRevision === right.stopRevision
|
|
264
|
-
&& left.consumer === right.consumer
|
|
265
|
-
&& left.consumedAt === right.consumedAt
|
|
266
|
-
&& left.owner === right.owner
|
|
267
|
-
&& left.reason === right.reason;
|
|
268
|
-
}
|
|
269
|
-
function recoveryNoteMatchesRequest(note, taskId, request) {
|
|
270
|
-
return note.taskId === taskId
|
|
271
|
-
&& note.stage === request.stage
|
|
272
|
-
&& note.round === request.round
|
|
273
|
-
&& note.artifact === request.artifact
|
|
274
|
-
&& note.startedAgent === request.agent
|
|
275
|
-
&& note.reason === request.reason;
|
|
276
|
-
}
|
|
277
|
-
function recoveryReferencesMatch(receipt, note, consumer) {
|
|
278
|
-
return receipt.childId === note.childId
|
|
279
|
-
&& note.consumer === consumer
|
|
280
|
-
&& receipt.hostEvidence?.stopRevision === note.stopRevision
|
|
281
|
-
&& receipt.hostEvidence.consumer === consumer
|
|
282
|
-
&& receipt.hostEvidence.consumedAt === note.consumedAt;
|
|
283
|
-
}
|
|
284
|
-
function recoveryRunIsStable(run) {
|
|
285
|
-
return run !== null && run.status === 'running' && run.pendingDelegation === null;
|
|
286
|
-
}
|
|
287
|
-
function recoveryTerminalFailureStatus(code) {
|
|
288
|
-
switch (code) {
|
|
289
|
-
case 'RECOVERY_LOG_CONFLICT':
|
|
290
|
-
case 'RECOVERY_NOTE_INVALID':
|
|
291
|
-
case 'RECOVERY_ORCHESTRATION_INVALID':
|
|
292
|
-
case 'RECOVERY_RECEIPT_INVALID':
|
|
293
|
-
case 'RECOVERY_REFERENCE_CONFLICT':
|
|
294
|
-
case 'RECOVERY_CONSUMER_CONFLICT':
|
|
295
|
-
return 'conflict';
|
|
296
|
-
default:
|
|
297
|
-
return 'owner-unknown';
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
function readRecoveryTerminalFacts(section, taskId, request, run, store) {
|
|
301
|
-
const targets = targetRows(section.entries, request.stage, request.round);
|
|
302
|
-
const paired = targets.rows.filter((row) => row.started !== '' && row.done !== '');
|
|
303
|
-
if (targets.recoveryEntries.length !== 1 || paired.length !== 1
|
|
304
|
-
|| paired[0].done !== targets.recoveryEntries[0].time) {
|
|
305
|
-
return { ok: false, code: 'RECOVERY_LOG_CONFLICT', message: 'recovery Activity Log does not contain one completed started/aborted pair' };
|
|
306
|
-
}
|
|
307
|
-
const note = parseRecoveryNote(targets.recoveryEntries[0].note);
|
|
308
|
-
if (!note || !recoveryNoteMatchesRequest(note, taskId, request)) {
|
|
309
|
-
return { ok: false, code: 'RECOVERY_NOTE_INVALID', message: 'recovery Activity Log note does not match the selector' };
|
|
310
|
-
}
|
|
311
|
-
if (!recoveryRunIsStable(run)) {
|
|
312
|
-
return { ok: false, code: 'RECOVERY_ORCHESTRATION_INVALID', message: 'completed recovery state must be running with no pending delegation' };
|
|
313
|
-
}
|
|
314
|
-
const receipts = matchingPostActivationAbortedReceipts(run, taskId, request);
|
|
315
|
-
if (receipts.length !== 1 || receipts[0].id !== note.receiptId) {
|
|
316
|
-
return { ok: false, code: 'RECOVERY_RECEIPT_INVALID', message: 'there is not one matching post-activation aborted receipt' };
|
|
317
|
-
}
|
|
318
|
-
const receipt = receipts[0];
|
|
319
|
-
const consumer = recoveryConsumer(taskId, receipt.id);
|
|
320
|
-
if (!recoveryReferencesMatch(receipt, note, consumer)) {
|
|
321
|
-
return { ok: false, code: 'RECOVERY_REFERENCE_CONFLICT', message: 'recovery receipt and Activity Log references do not match' };
|
|
322
|
-
}
|
|
323
|
-
const stored = readStoredEvidence(store, note.childId);
|
|
324
|
-
if ('error' in stored)
|
|
325
|
-
return { ok: false, code: 'RECOVERY_STORE_UNKNOWN', message: stored.error.message };
|
|
326
|
-
if ('missing' in stored)
|
|
327
|
-
return { ok: true, facts: { note, receipt, consumer, stored: null } };
|
|
328
|
-
const evidence = validateStopRecord(stored, receipt, consumer);
|
|
329
|
-
if (!evidence.ok)
|
|
330
|
-
return evidence;
|
|
331
|
-
if (stored.consumer !== consumer || !stored.consumedAt
|
|
332
|
-
|| evidence.stopRevision !== note.stopRevision || evidence.consumedAt !== note.consumedAt) {
|
|
333
|
-
return { ok: false, code: 'RECOVERY_REFERENCE_CONFLICT', message: 'complete recovery facts do not reference the protected recovery claim' };
|
|
334
|
-
}
|
|
335
|
-
return { ok: true, facts: { note, receipt, consumer, stored } };
|
|
336
|
-
}
|
|
337
|
-
function verifyRecoveryCommit(taskMdPath, taskDir, taskId, request, note, options) {
|
|
338
|
-
try {
|
|
339
|
-
const content = fs.readFileSync(taskMdPath, 'utf8');
|
|
340
|
-
parseTypedTaskFrontmatter(content);
|
|
341
|
-
const section = locateActivityLog(content);
|
|
342
|
-
if (!section)
|
|
343
|
-
return { ok: false, message: 'recovery Activity Log cannot be reread uniquely' };
|
|
344
|
-
const run = readRun(taskDir, options.orchestration);
|
|
345
|
-
const store = readLifecycleStore(options, options.repoRoot ?? path.resolve(taskDir, '../../../..'));
|
|
346
|
-
const facts = readRecoveryTerminalFacts(section, taskId, request, run, store);
|
|
347
|
-
if (!facts.ok)
|
|
348
|
-
return { ok: false, message: facts.message };
|
|
349
|
-
if (!sameRecoveryNote(facts.facts.note, note)) {
|
|
350
|
-
return { ok: false, message: 'recovery Activity Log note changed during commit verification' };
|
|
351
|
-
}
|
|
352
|
-
if (!facts.facts.stored)
|
|
353
|
-
return { ok: false, message: 'protected lifecycle claim disappeared before release' };
|
|
354
|
-
return { ok: true, receipt: facts.facts.receipt };
|
|
355
|
-
}
|
|
356
|
-
catch (error) {
|
|
357
|
-
return { ok: false, message: error instanceof Error ? error.message : String(error) };
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
function recoveryDomainFailure() {
|
|
361
|
-
return { consistent: false, recovery: true, targetState: 'active' };
|
|
362
|
-
}
|
|
363
|
-
export function readLifecycleRecoveryDomainEvidence(repoRoot, requestInput, terminalResult, options = {}) {
|
|
364
|
-
const terminalStateValid = terminalResult.status === 'no-op'
|
|
365
|
-
? terminalResult.changed === false
|
|
366
|
-
: terminalResult.status === 'applied'
|
|
367
|
-
&& (terminalResult.changed === true || (terminalResult.changed === false && isReleaseRetryWarning(terminalResult.warning)));
|
|
368
|
-
const normalized = normalizeRequest(requestInput);
|
|
369
|
-
if ('code' in normalized || terminalResult.targetState !== 'active'
|
|
370
|
-
|| !terminalStateValid)
|
|
371
|
-
return recoveryDomainFailure();
|
|
372
|
-
const request = normalized;
|
|
373
|
-
const resolved = resolveTaskRef(request.taskRef, { repoRoot });
|
|
374
|
-
if (!resolved.ok)
|
|
375
|
-
return recoveryDomainFailure();
|
|
376
|
-
try {
|
|
377
|
-
const content = fs.readFileSync(resolved.taskMdPath, 'utf8');
|
|
378
|
-
parseTypedTaskFrontmatter(content);
|
|
379
|
-
const section = locateActivityLog(content);
|
|
380
|
-
if (!section)
|
|
381
|
-
return recoveryDomainFailure();
|
|
382
|
-
const run = readRun(resolved.taskDir);
|
|
383
|
-
const store = readLifecycleStore(options, repoRoot);
|
|
384
|
-
const facts = readRecoveryTerminalFacts(section, resolved.taskId, request, run, store);
|
|
385
|
-
if (!facts.ok)
|
|
386
|
-
return recoveryDomainFailure();
|
|
387
|
-
if (!facts.facts.stored)
|
|
388
|
-
return { consistent: true, recovery: true, targetState: 'active', recoveryState: 'released' };
|
|
389
|
-
if (!isReleaseRetryWarning(terminalResult.warning))
|
|
390
|
-
return recoveryDomainFailure();
|
|
391
|
-
return {
|
|
392
|
-
consistent: true,
|
|
393
|
-
recovery: true,
|
|
394
|
-
targetState: 'active',
|
|
395
|
-
recoveryState: 'retry-required',
|
|
396
|
-
warning: terminalResult.warning
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
catch {
|
|
400
|
-
return recoveryDomainFailure();
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
function recoverStartedLifecycleUnderLock(requestInput, options = {}) {
|
|
404
|
-
const normalized = normalizeRequest(requestInput);
|
|
405
|
-
if ('code' in normalized)
|
|
406
|
-
return failure(requestInput, 'conflict', normalized.code, normalized.message);
|
|
407
|
-
const request = normalized;
|
|
408
|
-
const resolved = resolveTaskRef(request.taskRef, { repoRoot: options.repoRoot });
|
|
409
|
-
if (!resolved.ok)
|
|
410
|
-
return failure(request, 'conflict', resolved.code, resolved.message);
|
|
411
|
-
const taskId = resolved.taskId;
|
|
412
|
-
let content;
|
|
413
|
-
try {
|
|
414
|
-
content = fs.readFileSync(resolved.taskMdPath, 'utf8');
|
|
415
|
-
}
|
|
416
|
-
catch (error) {
|
|
417
|
-
return failure(request, 'owner-unknown', 'RECOVERY_TASK_READ_FAILED', String(error), { taskId });
|
|
418
|
-
}
|
|
419
|
-
try {
|
|
420
|
-
parseTypedTaskFrontmatter(content);
|
|
421
|
-
}
|
|
422
|
-
catch (error) {
|
|
423
|
-
return failure(request, 'conflict', 'RECOVERY_TASK_INVALID', error instanceof Error ? error.message : String(error), { taskId });
|
|
424
|
-
}
|
|
425
|
-
const section = locateActivityLog(content);
|
|
426
|
-
if (!section)
|
|
427
|
-
return failure(request, 'conflict', 'RECOVERY_LOG_INVALID', 'task has no unique Activity Log section', { taskId });
|
|
428
|
-
const targets = targetRows(section.entries, request.stage, request.round);
|
|
429
|
-
const open = targets.rows.filter((row) => row.started !== '' && row.done === '');
|
|
430
|
-
if (open.length > 1 || targets.recoveryEntries.length > 1) {
|
|
431
|
-
return failure(request, 'conflict', 'RECOVERY_SELECTOR_AMBIGUOUS', 'recovery selector does not identify one lifecycle attempt', { taskId });
|
|
432
|
-
}
|
|
433
|
-
let run;
|
|
434
|
-
try {
|
|
435
|
-
run = readRun(resolved.taskDir, options.orchestration);
|
|
436
|
-
}
|
|
437
|
-
catch (error) {
|
|
438
|
-
return failure(request, 'owner-unknown', 'RECOVERY_ORCHESTRATION_UNKNOWN', String(error), { taskId });
|
|
439
|
-
}
|
|
440
|
-
if (!run)
|
|
441
|
-
return failure(request, 'owner-unknown', 'RECOVERY_ORCHESTRATION_MISSING', 'no orchestration run exists for the open lifecycle execution', { taskId });
|
|
442
|
-
if (open.length === 0) {
|
|
443
|
-
if (targets.recoveryEntries.length !== 1) {
|
|
444
|
-
return failure(request, 'conflict', 'RECOVERY_TERMINAL_MISSING', 'there is no matching open or structured recovery lifecycle row', { taskId });
|
|
445
|
-
}
|
|
446
|
-
const store = readLifecycleStore(options, resolved.repoRoot);
|
|
447
|
-
const facts = readRecoveryTerminalFacts(section, taskId, request, run, store);
|
|
448
|
-
if (!facts.ok) {
|
|
449
|
-
return failure(request, recoveryTerminalFailureStatus(facts.code), facts.code, facts.message, { taskId });
|
|
450
|
-
}
|
|
451
|
-
const { note, receipt, consumer, stored } = facts.facts;
|
|
452
|
-
if (!stored)
|
|
453
|
-
return result(request, 'no-op', { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
454
|
-
let released = false;
|
|
455
|
-
try {
|
|
456
|
-
released = (options.releaseRecovery ?? store.releaseRecovery)(note.childId, consumer);
|
|
457
|
-
}
|
|
458
|
-
catch (error) {
|
|
459
|
-
return releaseResult(request, taskId, receipt, false, false);
|
|
460
|
-
}
|
|
461
|
-
return releaseResult(request, taskId, receipt, released, false);
|
|
462
|
-
}
|
|
463
|
-
const started = open[0];
|
|
464
|
-
if (normalizeAgentToken(started.agent) !== request.agent || started.note !== 'started') {
|
|
465
|
-
return failure(request, 'conflict', 'RECOVERY_SELECTOR_MISMATCH', 'open started lifecycle row does not match the recovery selector', { taskId });
|
|
466
|
-
}
|
|
467
|
-
if (targets.recoveryEntries.length > 0) {
|
|
468
|
-
return failure(request, 'conflict', 'RECOVERY_LOG_CONFLICT', 'open lifecycle execution already has a recovery terminal row', { taskId });
|
|
469
|
-
}
|
|
470
|
-
const pending = run.pendingDelegation;
|
|
471
|
-
const recovered = run.receipts.filter((candidate) => isPostActivationAbortedReceipt(candidate, taskId, request));
|
|
472
|
-
if (pending && recovered.length > 0) {
|
|
473
|
-
return failure(request, 'conflict', 'RECOVERY_RECEIPT_INVALID', 'open lifecycle execution has both a pending and an aborted matching delegation', { taskId });
|
|
474
|
-
}
|
|
475
|
-
const receipt = pending ?? (recovered.length === 1 ? recovered[0] : null);
|
|
476
|
-
if (!receipt || (pending && (receipt.status !== 'activated' || receipt.client !== 'codex')) || (!pending && receipt.status !== 'aborted')) {
|
|
477
|
-
return failure(request, 'owner-unknown', 'RECOVERY_DELEGATION_UNAVAILABLE', 'open lifecycle execution is not backed by one matching activated Codex delegation', { taskId });
|
|
478
|
-
}
|
|
479
|
-
if (normalizeAgentToken(started.agent) !== normalizeAgentToken(receipt.client)) {
|
|
480
|
-
return failure(request, 'conflict', 'RECOVERY_SELECTOR_MISMATCH', 'started agent does not match the delegation client', { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
481
|
-
}
|
|
482
|
-
const consumer = recoveryConsumer(taskId, receipt.id);
|
|
483
|
-
const store = readLifecycleStore(options, resolved.repoRoot);
|
|
484
|
-
const stored = readStoredEvidence(store, receipt.childId ?? '');
|
|
485
|
-
if ('error' in stored)
|
|
486
|
-
return failure(request, 'owner-unknown', 'RECOVERY_STORE_UNKNOWN', stored.error.message, { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
487
|
-
if ('missing' in stored)
|
|
488
|
-
return failure(request, 'owner-unknown', 'RECOVERY_STOP_EVIDENCE_MISSING', 'matching Codex lifecycle stop evidence is missing', { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
489
|
-
const evidence = validateStopRecord(stored, receipt, consumer);
|
|
490
|
-
if (!evidence.ok)
|
|
491
|
-
return failure(request, evidence.code === 'RECOVERY_CONSUMER_CONFLICT' ? 'conflict' : 'owner-unknown', evidence.code, evidence.message, { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
492
|
-
let claimed = stored;
|
|
493
|
-
if (stored.consumer === null) {
|
|
494
|
-
try {
|
|
495
|
-
claimed = store.claimRecovery(receipt.childId, taskId, receipt.id, receipt.hostEvidence?.hookDefinitionHash);
|
|
496
|
-
}
|
|
497
|
-
catch (error) {
|
|
498
|
-
return failure(request, 'owner-unknown', 'RECOVERY_CLAIM_FAILED', error instanceof Error ? error.message : String(error), { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
const stopRevision = claimed.revision;
|
|
502
|
-
const consumedAt = claimed.consumedAt;
|
|
503
|
-
if (!consumedAt)
|
|
504
|
-
return failure(request, 'owner-unknown', 'RECOVERY_CLAIM_INVALID', 'protected lifecycle claim has no consumed timestamp', { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
505
|
-
if (receipt.status === 'activated') {
|
|
506
|
-
const recoveredRun = recoverActivatedOrchestrationDelegationUnderLock(taskId, {
|
|
507
|
-
receiptId: receipt.id,
|
|
508
|
-
stage: request.stage,
|
|
509
|
-
round: request.round,
|
|
510
|
-
artifact: request.artifact,
|
|
511
|
-
startedAgent: request.agent,
|
|
512
|
-
childId: receipt.childId,
|
|
513
|
-
stopRevision,
|
|
514
|
-
consumer,
|
|
515
|
-
consumedAt
|
|
516
|
-
}, { ...options.orchestration, repoRoot: resolved.repoRoot, now: options.now });
|
|
517
|
-
if (recoveredRun.status === 'failed' || !recoveredRun.run) {
|
|
518
|
-
return failure(request, 'owner-unknown', recoveredRun.error?.code ?? 'RECOVERY_ORCHESTRATION_FAILED', recoveredRun.error?.message ?? 'orchestration recovery failed', { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
const note = taskNote(taskId, request, receipt, stopRevision, consumer, consumedAt);
|
|
522
|
-
const metadata = captureTaskWriteMetadata();
|
|
523
|
-
const updatedBody = `${section.body ? `${section.body}\n` : ''}- ${metadata.timestamp} — **${baseStep(started.step)} [aborted]** by ${request.agent} — ${renderRecoveryNote(note)}`;
|
|
524
|
-
const written = (options.writeTask ?? writeTask)({
|
|
525
|
-
taskRef: taskId,
|
|
526
|
-
expectedState: 'active',
|
|
527
|
-
mutations: [{ kind: 'section', aliases: ['活动日志', 'Activity Log'], heading: section.heading, body: updatedBody }]
|
|
528
|
-
}, { repoRoot: resolved.repoRoot, metadataProvider: () => metadata });
|
|
529
|
-
if (written.status === 'failed') {
|
|
530
|
-
return failure(request, 'owner-unknown', 'RECOVERY_LOG_WRITE_FAILED', written.error.message, { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
531
|
-
}
|
|
532
|
-
const verified = (options.verifyRecoveryCommit ?? verifyRecoveryCommit)(resolved.taskMdPath, resolved.taskDir, taskId, request, note, options);
|
|
533
|
-
if (!verified.ok) {
|
|
534
|
-
return failure(request, 'owner-unknown', 'RECOVERY_COMMIT_VERIFY_FAILED', verified.message, { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
535
|
-
}
|
|
536
|
-
let released = false;
|
|
537
|
-
try {
|
|
538
|
-
released = (options.releaseRecovery ?? store.releaseRecovery)(receipt.childId, consumer);
|
|
539
|
-
}
|
|
540
|
-
catch {
|
|
541
|
-
released = false;
|
|
542
|
-
}
|
|
543
|
-
if (!released) {
|
|
544
|
-
return releaseResult(request, taskId, receipt, false, true);
|
|
545
|
-
}
|
|
546
|
-
return result(request, 'applied', { taskId, receiptId: receipt.id, childId: receipt.childId });
|
|
547
|
-
}
|
|
548
|
-
export { RECOVERY_NOTE_PREFIX, parseRecoveryNote, recoveryFailure, recoveryConsumer, recoverStartedLifecycleUnderLock, renderRecoveryNote };
|
|
549
|
-
//# sourceMappingURL=lifecycle-recovery.js.map
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
function warningText(value) {
|
|
2
|
-
return typeof value === 'string' && value.length > 0 && value.trim() === value && !/[\r\n]/u.test(value);
|
|
3
|
-
}
|
|
4
|
-
export function isRecoveryWarning(value) {
|
|
5
|
-
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
6
|
-
return false;
|
|
7
|
-
const warning = value;
|
|
8
|
-
return Object.keys(warning).sort().join(',') === 'action,code,message'
|
|
9
|
-
&& warningText(warning.code)
|
|
10
|
-
&& warningText(warning.message)
|
|
11
|
-
&& warningText(warning.action);
|
|
12
|
-
}
|
|
13
|
-
export function sameRecoveryWarning(left, right) {
|
|
14
|
-
return isRecoveryWarning(left) && isRecoveryWarning(right)
|
|
15
|
-
&& left.code === right.code
|
|
16
|
-
&& left.message === right.message
|
|
17
|
-
&& left.action === right.action;
|
|
18
|
-
}
|
|
19
|
-
//# sourceMappingURL=recovery-warning.js.map
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export const TASK_WORKFLOW_COMMANDS = Object.freeze({
|
|
2
|
-
'artifact-inspect': ['task-artifact', 'inspect'],
|
|
3
|
-
'artifact-init': ['task-artifact', 'init'],
|
|
4
|
-
'artifact-preflight': ['task-artifact', 'preflight'],
|
|
5
|
-
'artifact-finalize-local': ['task-artifact', 'finalize-local'],
|
|
6
|
-
'review-preflight': ['task-review', 'preflight'],
|
|
7
|
-
'review-finalize-summary': ['task-review', 'finalize-summary'],
|
|
8
|
-
event: ['task-event', null],
|
|
9
|
-
'ledger-finding-response': ['task-ledger', 'finding-respond'],
|
|
10
|
-
'ledger-finding-review': ['task-ledger', 'finding-review'],
|
|
11
|
-
'ledger-finding-upsert': ['task-ledger', 'finding-upsert'],
|
|
12
|
-
'ledger-stage-status': ['task-ledger', 'stage-status'],
|
|
13
|
-
'decision-next-id': ['task-ledger', 'decision-next-id'],
|
|
14
|
-
'decision-upsert': ['task-ledger', 'decision-upsert'],
|
|
15
|
-
'invalidation-reconcile': ['task-invalidation', 'reconcile'],
|
|
16
|
-
'warning-add': ['task-warning', 'add'],
|
|
17
|
-
'warning-list': ['task-warning', 'list'],
|
|
18
|
-
'warning-set-status': ['task-warning', 'set-status']
|
|
19
|
-
});
|
|
20
|
-
export const TASK_WORKFLOW_OPERATIONS = Object.freeze(Object.keys(TASK_WORKFLOW_COMMANDS));
|
|
21
|
-
//# sourceMappingURL=workflow-command.js.map
|