@litfamily/litcodex 1.0.36 → 1.0.38
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-Ko-KR.md +9 -9
- package/README.md +9 -9
- package/dist/install/codex-version.js +1 -1
- package/marketplace/plugins/litcodex/.codex-plugin/plugin.json +1 -1
- package/marketplace/plugins/litcodex/components/auto-update/package.json +1 -1
- package/marketplace/plugins/litcodex/components/comment-checker/package.json +1 -1
- package/marketplace/plugins/litcodex/components/git-bash/package.json +1 -1
- package/marketplace/plugins/litcodex/components/lit-loop/directives/lit-adhd.md +32 -13
- package/marketplace/plugins/litcodex/components/lit-loop/dist/hook-cli.js +2 -1
- package/marketplace/plugins/litcodex/components/lit-loop/dist/rewind-board.d.ts +192 -0
- package/marketplace/plugins/litcodex/components/lit-loop/dist/rewind-board.js +437 -0
- package/marketplace/plugins/litcodex/components/lit-loop/dist/rewind-cli.js +17 -3
- package/marketplace/plugins/litcodex/components/lit-loop/dist/rewind-go.d.ts +3 -2
- package/marketplace/plugins/litcodex/components/lit-loop/dist/rewind-go.js +6 -4
- package/marketplace/plugins/litcodex/components/lit-loop/dist/rewind.d.ts +9 -2
- package/marketplace/plugins/litcodex/components/lit-loop/dist/rewind.js +245 -30
- package/marketplace/plugins/litcodex/components/lit-loop/dist/route-heads.js +3 -3
- package/marketplace/plugins/litcodex/components/lit-loop/dist/state-types.d.ts +1 -1
- package/marketplace/plugins/litcodex/components/lit-loop/dist/state-types.js +2 -0
- package/marketplace/plugins/litcodex/components/lit-loop/dist/workflow-map.js +5 -1
- package/marketplace/plugins/litcodex/components/lit-loop/dist/workflow-stack.js +1 -1
- package/marketplace/plugins/litcodex/components/lit-loop/package.json +1 -1
- package/marketplace/plugins/litcodex/components/lsp/package.json +1 -1
- package/marketplace/plugins/litcodex/components/rules/package.json +1 -1
- package/marketplace/plugins/litcodex/components/start-work-continuation/directive.md +5 -5
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/cli.js +30 -5
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/codex-hook.d.ts +6 -5
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/codex-hook.js +184 -62
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/host-reads.d.ts +7 -0
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/host-reads.js +30 -0
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/index.d.ts +9 -4
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/index.js +5 -2
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/lifecycle-root.d.ts +9 -0
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/lifecycle-root.js +117 -0
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/lifecycle-store.d.ts +72 -1
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/lifecycle-store.js +525 -103
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/pre-tool-hook.d.ts +8 -0
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/pre-tool-hook.js +131 -0
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/protected-paths.d.ts +13 -0
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/protected-paths.js +103 -0
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/work-state-reader.d.ts +14 -2
- package/marketplace/plugins/litcodex/components/start-work-continuation/dist/work-state-reader.js +14 -4
- package/marketplace/plugins/litcodex/components/start-work-continuation/package.json +1 -1
- package/marketplace/plugins/litcodex/components/telemetry/package.json +1 -1
- package/marketplace/plugins/litcodex/components/wikify-knowledge/package.json +1 -1
- package/marketplace/plugins/litcodex/hooks/hooks.json +11 -0
- package/marketplace/plugins/litcodex/skills/lit-adhd/SKILL.md +43 -21
- package/marketplace/plugins/litcodex/skills/lit-adhd/agents/openai.yaml +1 -1
- package/marketplace/plugins/litcodex/skills/lit-adhd/references/card-examples.md +103 -3
- package/marketplace/plugins/litcodex/skills/lit-adhd/references/status-taxonomy.md +19 -1
- package/marketplace/plugins/litcodex/skills/start-work/SKILL.md +18 -4
- package/node_modules/@litcodex/lit-loop/directives/lit-adhd.md +32 -13
- package/node_modules/@litcodex/lit-loop/dist/hook-cli.js +2 -1
- package/node_modules/@litcodex/lit-loop/dist/rewind-board.d.ts +192 -0
- package/node_modules/@litcodex/lit-loop/dist/rewind-board.js +437 -0
- package/node_modules/@litcodex/lit-loop/dist/rewind-cli.js +17 -3
- package/node_modules/@litcodex/lit-loop/dist/rewind-go.d.ts +3 -2
- package/node_modules/@litcodex/lit-loop/dist/rewind-go.js +6 -4
- package/node_modules/@litcodex/lit-loop/dist/rewind.d.ts +9 -2
- package/node_modules/@litcodex/lit-loop/dist/rewind.js +245 -30
- package/node_modules/@litcodex/lit-loop/dist/route-heads.js +3 -3
- package/node_modules/@litcodex/lit-loop/dist/state-types.d.ts +1 -1
- package/node_modules/@litcodex/lit-loop/dist/state-types.js +2 -0
- package/node_modules/@litcodex/lit-loop/dist/workflow-map.js +5 -1
- package/node_modules/@litcodex/lit-loop/dist/workflow-stack.js +1 -1
- package/node_modules/@litcodex/lit-loop/package.json +1 -1
- package/node_modules/@litcodex/lit-loop/skills/lit-adhd/SKILL.md +43 -21
- package/node_modules/@litcodex/lit-loop/skills/start-work/SKILL.md +18 -4
- package/package.json +2 -2
- package/readme-assets/badge-version.svg +1 -1
package/marketplace/plugins/litcodex/components/start-work-continuation/dist/lifecycle-store.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { closeSync, constants, fstatSync, fsyncSync, linkSync, lstatSync, mkdirSync, openSync, readSync, realpathSync, renameSync, unlinkSync, writeSync, } from "node:fs";
|
|
3
3
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { isHostOwnedRead } from "./host-reads.js";
|
|
4
5
|
import { analyzePlanProgress } from "./plan-progress.js";
|
|
6
|
+
import { protectedReason } from "./protected-paths.js";
|
|
5
7
|
export const START_WORK_STATE_MAX_BYTES = 1_000_000;
|
|
6
8
|
export const START_WORK_PLAN_MAX_BYTES = 1_000_000;
|
|
7
9
|
export const START_WORK_LEDGER_TAIL_BYTES = 2_000_000;
|
|
@@ -13,6 +15,30 @@ export const START_WORK_MAX_AUTHORITY_BYTES = 64_000;
|
|
|
13
15
|
export const START_WORK_MAX_INIT_BYTES = 256_000;
|
|
14
16
|
const MAX_AUTHORITY_ITEMS = 64;
|
|
15
17
|
const MAX_AUTHORITY_GRANTS = 64;
|
|
18
|
+
export const MAX_PARKED_BOUNDARIES = 16;
|
|
19
|
+
export const MAX_DENIED_BOUNDARIES = 64;
|
|
20
|
+
export const MAX_CANCEL_NOTICES = 16;
|
|
21
|
+
/**
|
|
22
|
+
* Release-class actions are never parked and never granted, whatever the plan's `forbidden_actions` lists: a typed
|
|
23
|
+
* `lit approve` settles every parked step at once, so these stay with the user outside the run.
|
|
24
|
+
*/
|
|
25
|
+
const RELEASE_CLASS_ACTIONS = new Set([
|
|
26
|
+
"commit",
|
|
27
|
+
"push",
|
|
28
|
+
"publish",
|
|
29
|
+
"tag",
|
|
30
|
+
"release",
|
|
31
|
+
"version-bump",
|
|
32
|
+
"vcs-destructive",
|
|
33
|
+
"git-commit",
|
|
34
|
+
"git-push",
|
|
35
|
+
"git-tag",
|
|
36
|
+
"git-reset",
|
|
37
|
+
"git-rebase",
|
|
38
|
+
"force-push",
|
|
39
|
+
"host-config",
|
|
40
|
+
"host-config-write",
|
|
41
|
+
]);
|
|
16
42
|
const LOCK_ATTEMPTS = 8;
|
|
17
43
|
const LOCK_STALE_MS = 30_000;
|
|
18
44
|
const O_NOFOLLOW = typeof constants.O_NOFOLLOW === "number" ? constants.O_NOFOLLOW : 0;
|
|
@@ -156,108 +182,316 @@ export function transitionWork(cwd, request, options = {}) {
|
|
|
156
182
|
return withStateLock(paths, options, () => {
|
|
157
183
|
const loaded = loadState(paths);
|
|
158
184
|
reconcileAllLastTransitions(paths, loaded.state, options);
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
185
|
+
return applyTransitionLocked(paths, loaded, request, options);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/** The body of a model-callable transition, run while the state lock is held. */
|
|
189
|
+
function applyTransitionLocked(paths, loaded, request, options) {
|
|
190
|
+
const env = options.env ?? process.env;
|
|
191
|
+
const existing = findCanonicalEvent(paths, loaded.state, request.transitionId, options);
|
|
192
|
+
if (existing !== undefined) {
|
|
193
|
+
if (!requestMatchesEvent(request, existing)) {
|
|
194
|
+
throw new LifecycleStoreError("TRANSITION_ID_CONFLICT", "transition id has a conflicting canonical event");
|
|
166
195
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
196
|
+
ensureLedgerEvent(paths, existing, options);
|
|
197
|
+
return { changed: false, state: loaded.state, event: existing };
|
|
198
|
+
}
|
|
199
|
+
assertReplayAvailable(loaded.state, request.expectedRevision);
|
|
200
|
+
const migrationAuthority = request.authority === undefined ? undefined : normalizeAuthority(paths, request.authority);
|
|
201
|
+
const state = normalizeStateForMutation(paths, loaded, migrationAuthority);
|
|
202
|
+
const work = state.works[request.workId];
|
|
203
|
+
if (work === undefined ||
|
|
204
|
+
work.work_id !== request.workId ||
|
|
205
|
+
!work.session_ids.includes(`codex:${request.sessionId}`) ||
|
|
206
|
+
state.active_work_id !== request.workId) {
|
|
207
|
+
throw new LifecycleStoreError("WORK_SELECTION_INVALID", "workId did not select the active matching work");
|
|
208
|
+
}
|
|
209
|
+
const authority = work.authority;
|
|
210
|
+
if (authority === undefined) {
|
|
211
|
+
throw new LifecycleStoreError("AUTHORITY_INVALID", "nonterminal work lacks authority");
|
|
212
|
+
}
|
|
213
|
+
const boundary = request.boundary === undefined ? undefined : normalizeBoundary(request.boundary, authority);
|
|
214
|
+
const parked = work.parked_boundaries ?? [];
|
|
215
|
+
if (boundary !== undefined && (request.action === "pause" || request.action === "resume")) {
|
|
216
|
+
if (isReleaseClass(boundary.action)) {
|
|
217
|
+
throw new LifecycleStoreError("BOUNDARY_MISMATCH", "boundary action is permanently forbidden");
|
|
176
218
|
}
|
|
177
|
-
const
|
|
178
|
-
if (
|
|
179
|
-
throw new LifecycleStoreError("
|
|
219
|
+
const reason = protectedReason(boundary.root, accessKindOf(boundary.action), env);
|
|
220
|
+
if (reason !== null) {
|
|
221
|
+
throw new LifecycleStoreError("BOUNDARY_PROTECTED", `${boundary.root} is protected for this run (${reason}); a start-work never opens or changes secrets or host settings, and this is not an approval prompt`);
|
|
180
222
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
boundary
|
|
185
|
-
|
|
186
|
-
boundariesEquivalent(boundary, work.pending_boundary)) {
|
|
223
|
+
}
|
|
224
|
+
if (request.action === "pause" && work.status === "active" && boundary !== undefined) {
|
|
225
|
+
if (authorityAuthorizesBoundary(authority, boundary) ||
|
|
226
|
+
work.authority_grants?.some((grant) => grantAuthorizesBoundary(grant, boundary)) === true ||
|
|
227
|
+
(isReadAction(boundary.action) && isHostOwnedRead(boundary.root, request.sessionId, env))) {
|
|
187
228
|
const prior = work.last_transition;
|
|
188
|
-
if (prior === undefined
|
|
189
|
-
throw new LifecycleStoreError("BOUNDARY_STATE_INVALID", "
|
|
229
|
+
if (prior === undefined) {
|
|
230
|
+
throw new LifecycleStoreError("BOUNDARY_STATE_INVALID", "authorized work lacks its canonical event");
|
|
190
231
|
}
|
|
191
232
|
ensureLedgerEvent(paths, prior, options);
|
|
192
|
-
return { changed: false, state, event: prior };
|
|
193
|
-
}
|
|
194
|
-
if (request.action === "pause" && work.status === "active" && boundary !== undefined) {
|
|
195
|
-
if (authorityAuthorizesBoundary(authority, boundary)) {
|
|
196
|
-
const prior = work.last_transition;
|
|
197
|
-
if (prior === undefined) {
|
|
198
|
-
throw new LifecycleStoreError("BOUNDARY_STATE_INVALID", "authorized work lacks its canonical event");
|
|
199
|
-
}
|
|
200
|
-
ensureLedgerEvent(paths, prior, options);
|
|
201
|
-
return { changed: false, state, event: prior };
|
|
202
|
-
}
|
|
203
|
-
const consumed = work.authority_grants?.find((grant) => grantAuthorizesBoundary(grant, boundary));
|
|
204
|
-
if (consumed !== undefined) {
|
|
205
|
-
const prior = work.last_transition;
|
|
206
|
-
if (prior === undefined) {
|
|
207
|
-
throw new LifecycleStoreError("BOUNDARY_STATE_INVALID", "authorized work lacks its canonical event");
|
|
208
|
-
}
|
|
209
|
-
ensureLedgerEvent(paths, prior, options);
|
|
210
|
-
return { changed: false, state, event: prior };
|
|
211
|
-
}
|
|
233
|
+
return { changed: false, state, event: prior, outcome: "authorized" };
|
|
212
234
|
}
|
|
213
|
-
if (
|
|
214
|
-
throw new LifecycleStoreError("
|
|
235
|
+
if ((work.denied_boundaries ?? []).some((denied) => denied.action === boundary.action && isWithin(denied.root, boundary.root))) {
|
|
236
|
+
throw new LifecycleStoreError("BOUNDARY_DENIED", "the user denied this step for this run; carry on without it");
|
|
215
237
|
}
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
238
|
+
const same = parked.find((entry) => entry.action === boundary.action && entry.root === boundary.root);
|
|
239
|
+
if (same !== undefined) {
|
|
240
|
+
const prior = state.transition_history?.[same.transition_id] ?? work.last_transition;
|
|
241
|
+
if (prior === undefined) {
|
|
242
|
+
throw new LifecycleStoreError("BOUNDARY_STATE_INVALID", "parked boundary lacks its canonical event");
|
|
243
|
+
}
|
|
244
|
+
return { changed: false, state, event: prior, outcome: "already_parked" };
|
|
223
245
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
246
|
+
}
|
|
247
|
+
if (state.revision !== request.expectedRevision) {
|
|
248
|
+
throw new LifecycleStoreError("REVISION_CONFLICT", "expected revision does not match current revision");
|
|
249
|
+
}
|
|
250
|
+
const plan = canonicalPlan(paths, work.active_plan, false);
|
|
251
|
+
// Fresh init enforces the planner contract; persisted work retains legacy progress compatibility.
|
|
252
|
+
if (plan.progress.total === 0)
|
|
253
|
+
throw new LifecycleStoreError("PLAN_EMPTY", "start-work plan has no executable checkbox");
|
|
254
|
+
const target = targetStatus(request.action, work.status, request.reasonCode);
|
|
255
|
+
if (request.action === "complete" && plan.progress.remaining !== 0) {
|
|
256
|
+
throw new LifecycleStoreError("PLAN_INCOMPLETE", "complete requires zero remaining executable checkboxes");
|
|
257
|
+
}
|
|
258
|
+
const grant = request.grant === undefined ? undefined : normalizeGrant(request.grant, authority, boundary);
|
|
259
|
+
if (request.action === "pause" && boundary === undefined) {
|
|
260
|
+
throw new LifecycleStoreError("BOUNDARY_REQUIRED", "pause requires an explicit authority boundary");
|
|
261
|
+
}
|
|
262
|
+
if (request.action === "pause" && parked.length >= MAX_PARKED_BOUNDARIES) {
|
|
263
|
+
throw new LifecycleStoreError("PARKED_LIMIT", "16 steps are already parked; finish what the plan allows, list the rest on BLOCKED: lines in your final message and end the turn; the user decides with lit approve or lit cancel");
|
|
264
|
+
}
|
|
265
|
+
let resumed;
|
|
266
|
+
if (request.action === "resume") {
|
|
267
|
+
if (boundary === undefined || grant === undefined) {
|
|
268
|
+
throw new LifecycleStoreError("GRANT_REQUIRED", "resume requires matching boundary and grant");
|
|
227
269
|
}
|
|
228
|
-
if (
|
|
229
|
-
|
|
230
|
-
|
|
270
|
+
if (work.status === "active") {
|
|
271
|
+
resumed = parked.find((entry) => entry.boundary_id === boundary.boundary_id && boundariesEquivalent(boundaryFields(entry), boundary));
|
|
272
|
+
if (resumed === undefined) {
|
|
273
|
+
throw new LifecycleStoreError("BOUNDARY_MISMATCH", "resume boundary does not name a parked step");
|
|
231
274
|
}
|
|
232
|
-
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
const pending = loaded.sourceSchema === 2 ? boundary : undefined;
|
|
233
278
|
if (pending === undefined || !boundariesEquivalent(boundary, pending)) {
|
|
234
279
|
throw new LifecycleStoreError("BOUNDARY_MISMATCH", "resume boundary does not match pending boundary");
|
|
235
280
|
}
|
|
236
|
-
const conflictingGrant = work.authority_grants?.find((item) => item.grant_id === grant.grant_id && !grantMatchesBoundary(item, boundary));
|
|
237
|
-
if (conflictingGrant !== undefined) {
|
|
238
|
-
throw new LifecycleStoreError("GRANT_CONFLICT", "grant id is already bound to a different boundary");
|
|
239
|
-
}
|
|
240
281
|
}
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
const
|
|
282
|
+
const conflictingGrant = work.authority_grants?.find((item) => item.grant_id === grant.grant_id && !grantMatchesBoundary(item, boundary));
|
|
283
|
+
if (conflictingGrant !== undefined) {
|
|
284
|
+
throw new LifecycleStoreError("GRANT_CONFLICT", "grant id is already bound to a different boundary");
|
|
285
|
+
}
|
|
286
|
+
const held = work.authority_grants ?? [];
|
|
287
|
+
if (!held.some((item) => item.grant_id === grant.grant_id) && held.length >= MAX_AUTHORITY_GRANTS) {
|
|
288
|
+
throw new LifecycleStoreError("GRANT_LIMIT", `this run already holds ${MAX_AUTHORITY_GRANTS} grants, so no further step can be granted`);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const event = transitionEvent(state, work, request, target, plan.relativePath, plan.progress.progressToken, options.now?.() ?? new Date().toISOString(), boundary, grant);
|
|
292
|
+
const grants = request.action === "resume" && grant !== undefined
|
|
293
|
+
? [...(work.authority_grants ?? []).filter((item) => item.grant_id !== grant.grant_id), grant].slice(-MAX_AUTHORITY_GRANTS)
|
|
294
|
+
: work.authority_grants;
|
|
295
|
+
const nextParked = request.action === "pause" && boundary !== undefined
|
|
296
|
+
? [
|
|
297
|
+
...parked,
|
|
298
|
+
{
|
|
299
|
+
...boundary,
|
|
300
|
+
parked_at: event.at,
|
|
301
|
+
session_id: event.sessionId,
|
|
302
|
+
transition_id: event.transitionId,
|
|
303
|
+
},
|
|
304
|
+
]
|
|
305
|
+
: resumed !== undefined
|
|
306
|
+
? parked.filter((entry) => entry !== resumed)
|
|
307
|
+
: parked;
|
|
308
|
+
const updatedWork = {
|
|
309
|
+
...work,
|
|
310
|
+
status: target,
|
|
311
|
+
...(request.action === "resume" && grants !== undefined ? { authority_grants: grants } : {}),
|
|
312
|
+
last_transition: event,
|
|
313
|
+
};
|
|
314
|
+
const terminal = target === "abandoned" || target === "completed";
|
|
315
|
+
const nextWork = withParked(omitPendingBoundary(updatedWork), terminal ? [] : nextParked);
|
|
316
|
+
const history = appendHistory(state, event);
|
|
317
|
+
const nextState = compactTerminalWorks({
|
|
318
|
+
...state,
|
|
319
|
+
schema_version: 3,
|
|
320
|
+
revision: event.revision,
|
|
321
|
+
active_work_id: terminal ? null : request.workId,
|
|
322
|
+
works: { ...state.works, [request.workId]: nextWork },
|
|
323
|
+
transition_history: history.events,
|
|
324
|
+
history_floor_revision: history.floorRevision,
|
|
325
|
+
});
|
|
326
|
+
validateStateConsistency(nextState);
|
|
327
|
+
writeStateAtomic(paths, nextState, false);
|
|
328
|
+
options.afterStateWrite?.();
|
|
329
|
+
ensureLedgerEvent(paths, event, options);
|
|
330
|
+
return {
|
|
331
|
+
changed: true,
|
|
332
|
+
state: nextState,
|
|
333
|
+
event,
|
|
334
|
+
...(request.action === "pause" ? { outcome: "parked" } : {}),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* The typed `lit cancel`: ends the project's nonterminal run whichever Codex session of the project typed it. It
|
|
339
|
+
* checks neither session membership nor an expected revision and never reads the plan, so a deleted or emptied
|
|
340
|
+
* plan cannot keep a user from ending the run. Every other session of the run is left a notice for its next prompt.
|
|
341
|
+
*/
|
|
342
|
+
export function cancelWorkFromUserPrompt(cwd, request, options = {}) {
|
|
343
|
+
if (!isBoundedLifecycleText(cwd) ||
|
|
344
|
+
!isBoundedLifecycleText(request.sessionId) ||
|
|
345
|
+
!isBoundedLifecycleText(request.turnId)) {
|
|
346
|
+
throw new LifecycleStoreError("CANCEL_INPUT_INVALID", "cancel input is malformed or oversized");
|
|
347
|
+
}
|
|
348
|
+
const paths = managedPaths(cwd, true);
|
|
349
|
+
if (!pathExists(paths.state))
|
|
350
|
+
return { outcome: "nothing" };
|
|
351
|
+
return withStateLock(paths, options, () => {
|
|
352
|
+
const loaded = loadState(paths);
|
|
353
|
+
reconcileAllLastTransitions(paths, loaded.state, options);
|
|
354
|
+
const activeId = loaded.state.active_work_id;
|
|
355
|
+
const current = activeId === null ? undefined : loaded.state.works[activeId];
|
|
356
|
+
if (activeId === null || current === undefined || !isNonterminal(current.status))
|
|
357
|
+
return { outcome: "nothing" };
|
|
358
|
+
const transitionId = `prompt-cancel:${digest([activeId, request.sessionId, request.turnId].join("\0")).slice(0, 32)}`;
|
|
359
|
+
const existing = findCanonicalEvent(paths, loaded.state, transitionId, options);
|
|
360
|
+
if (existing !== undefined) {
|
|
361
|
+
ensureLedgerEvent(paths, existing, options);
|
|
362
|
+
return { outcome: "replay", workId: activeId, plan: current.plan_name };
|
|
363
|
+
}
|
|
364
|
+
const state = normalizeStateForMutation(paths, loaded, undefined, activeId);
|
|
365
|
+
const work = state.works[activeId];
|
|
366
|
+
if (work === undefined)
|
|
367
|
+
throw new LifecycleStoreError("STATE_INVALID", "active work disappeared");
|
|
368
|
+
const typer = `codex:${request.sessionId}`;
|
|
369
|
+
const at = options.now?.() ?? new Date().toISOString();
|
|
370
|
+
const event = {
|
|
371
|
+
kind: "start_work_cancelled",
|
|
372
|
+
at,
|
|
373
|
+
workId: activeId,
|
|
374
|
+
transitionId,
|
|
375
|
+
revision: state.revision + 1,
|
|
376
|
+
fromWorkStatus: work.status,
|
|
377
|
+
toWorkStatus: "abandoned",
|
|
378
|
+
plan: work.active_plan,
|
|
379
|
+
sessionId: typer,
|
|
380
|
+
reasonCode: "user_cancelled",
|
|
381
|
+
turnId: request.turnId,
|
|
382
|
+
};
|
|
383
|
+
const nextWork = withParked(omitPendingBoundary({ ...work, status: "abandoned", last_transition: event }), []);
|
|
384
|
+
const notices = { ...(state.cancel_notices ?? {}) };
|
|
385
|
+
for (const owner of work.session_ids) {
|
|
386
|
+
if (!owner.startsWith("codex:") || owner === typer)
|
|
387
|
+
continue;
|
|
388
|
+
delete notices[owner];
|
|
389
|
+
notices[owner] = { work_id: activeId, plan_name: work.plan_name, by_session_id: typer, at };
|
|
390
|
+
}
|
|
391
|
+
const kept = Object.entries(notices).slice(-MAX_CANCEL_NOTICES);
|
|
392
|
+
const history = appendHistory(state, event);
|
|
393
|
+
const nextState = compactTerminalWorks({
|
|
394
|
+
...state,
|
|
395
|
+
schema_version: 3,
|
|
396
|
+
revision: event.revision,
|
|
397
|
+
active_work_id: null,
|
|
398
|
+
works: { ...state.works, [activeId]: nextWork },
|
|
399
|
+
transition_history: history.events,
|
|
400
|
+
history_floor_revision: history.floorRevision,
|
|
401
|
+
...(kept.length === 0 && state.cancel_notices === undefined
|
|
402
|
+
? {}
|
|
403
|
+
: { cancel_notices: Object.fromEntries(kept) }),
|
|
404
|
+
});
|
|
405
|
+
validateStateConsistency(nextState);
|
|
406
|
+
writeStateAtomic(paths, nextState, false);
|
|
407
|
+
options.afterStateWrite?.();
|
|
408
|
+
ensureLedgerEvent(paths, event, options);
|
|
409
|
+
return {
|
|
410
|
+
outcome: "cancelled",
|
|
411
|
+
workId: activeId,
|
|
412
|
+
plan: work.plan_name,
|
|
413
|
+
otherSession: !work.session_ids.includes(typer),
|
|
414
|
+
};
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Remove and return this session's cancel notice. It is bookkeeping, not a lifecycle step: no event is recorded and
|
|
419
|
+
* the revision does not move, so a lane still holding the old revision is not turned away.
|
|
420
|
+
*/
|
|
421
|
+
export function takeCancelNotice(cwd, sessionId, options = {}) {
|
|
422
|
+
const key = `codex:${sessionId}`;
|
|
423
|
+
const peek = inspectLifecycleState(cwd);
|
|
424
|
+
if (!peek.ok || peek.state.cancel_notices?.[key] === undefined)
|
|
425
|
+
return null;
|
|
426
|
+
const paths = managedPaths(cwd, true);
|
|
427
|
+
return withStateLock(paths, options, () => {
|
|
428
|
+
const loaded = loadState(paths);
|
|
429
|
+
const notice = loaded.state.cancel_notices?.[key];
|
|
430
|
+
if (loaded.sourceSchema !== 3 || notice === undefined)
|
|
431
|
+
return null;
|
|
432
|
+
const notices = { ...loaded.state.cancel_notices };
|
|
433
|
+
delete notices[key];
|
|
434
|
+
const nextState = { ...loaded.state, cancel_notices: notices };
|
|
435
|
+
validateStateConsistency(nextState);
|
|
436
|
+
writeStateAtomic(paths, nextState, false);
|
|
437
|
+
options.afterStateWrite?.();
|
|
438
|
+
return notice;
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
/** The typed `lit deny`: every parked step of this session's run moves to the refused list for the rest of the run. */
|
|
442
|
+
export function denyParkedBoundaries(cwd, request, options = {}) {
|
|
443
|
+
if (!isBoundedLifecycleText(cwd) ||
|
|
444
|
+
!isBoundedLifecycleText(request.sessionId) ||
|
|
445
|
+
!isBoundedLifecycleText(request.turnId)) {
|
|
446
|
+
throw new LifecycleStoreError("DENY_INPUT_INVALID", "deny input is malformed or oversized");
|
|
447
|
+
}
|
|
448
|
+
const paths = managedPaths(cwd, true);
|
|
449
|
+
return withStateLock(paths, options, () => {
|
|
450
|
+
const loaded = loadState(paths);
|
|
451
|
+
reconcileAllLastTransitions(paths, loaded.state, options);
|
|
452
|
+
const state = normalizeStateForMutation(paths, loaded);
|
|
453
|
+
const work = selectWorkForSession(state, request.sessionId, ["active"]);
|
|
454
|
+
if (work === null)
|
|
455
|
+
return { outcome: "nothing" };
|
|
456
|
+
const transitionId = `prompt-deny:${digest([work.work_id, request.sessionId, request.turnId].join("\0")).slice(0, 32)}`;
|
|
457
|
+
const existing = findCanonicalEvent(paths, state, transitionId, options);
|
|
458
|
+
if (existing !== undefined) {
|
|
459
|
+
ensureLedgerEvent(paths, existing, options);
|
|
460
|
+
const ids = new Set(existing.boundaryIds ?? []);
|
|
461
|
+
return {
|
|
462
|
+
outcome: "replay",
|
|
463
|
+
workId: work.work_id,
|
|
464
|
+
boundaries: (work.denied_boundaries ?? []).filter((entry) => ids.has(entry.boundary_id)),
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
const parked = work.parked_boundaries ?? [];
|
|
468
|
+
if (parked.length === 0)
|
|
469
|
+
return { outcome: "nothing" };
|
|
470
|
+
const refused = parked.map(boundaryFields);
|
|
471
|
+
const event = {
|
|
472
|
+
kind: "start_work_boundary_denied",
|
|
473
|
+
at: options.now?.() ?? new Date().toISOString(),
|
|
474
|
+
workId: work.work_id,
|
|
475
|
+
transitionId,
|
|
476
|
+
revision: state.revision + 1,
|
|
477
|
+
fromWorkStatus: "active",
|
|
478
|
+
toWorkStatus: "active",
|
|
479
|
+
plan: work.active_plan,
|
|
480
|
+
sessionId: `codex:${request.sessionId}`,
|
|
481
|
+
turnId: request.turnId,
|
|
482
|
+
boundaryIds: refused.map((entry) => entry.boundary_id),
|
|
483
|
+
};
|
|
484
|
+
const nextWork = withParked(omitPendingBoundary({
|
|
246
485
|
...work,
|
|
247
|
-
|
|
248
|
-
...(request.action === "pause" && boundary !== undefined ? { pending_boundary: boundary } : {}),
|
|
249
|
-
...(request.action === "resume" && grants !== undefined ? { authority_grants: grants } : {}),
|
|
486
|
+
denied_boundaries: [...(work.denied_boundaries ?? []), ...refused].slice(-MAX_DENIED_BOUNDARIES),
|
|
250
487
|
last_transition: event,
|
|
251
|
-
};
|
|
252
|
-
const nextWork = request.action === "resume" || terminalAction(request.action) ? omitPendingBoundary(updatedWork) : updatedWork;
|
|
253
|
-
const terminal = target === "abandoned" || target === "completed";
|
|
488
|
+
}), []);
|
|
254
489
|
const history = appendHistory(state, event);
|
|
255
490
|
const nextState = compactTerminalWorks({
|
|
256
491
|
...state,
|
|
257
492
|
schema_version: 3,
|
|
258
493
|
revision: event.revision,
|
|
259
|
-
|
|
260
|
-
works: { ...state.works, [request.workId]: nextWork },
|
|
494
|
+
works: { ...state.works, [work.work_id]: nextWork },
|
|
261
495
|
transition_history: history.events,
|
|
262
496
|
history_floor_revision: history.floorRevision,
|
|
263
497
|
});
|
|
@@ -265,7 +499,29 @@ export function transitionWork(cwd, request, options = {}) {
|
|
|
265
499
|
writeStateAtomic(paths, nextState, false);
|
|
266
500
|
options.afterStateWrite?.();
|
|
267
501
|
ensureLedgerEvent(paths, event, options);
|
|
268
|
-
return {
|
|
502
|
+
return { outcome: "denied", workId: work.work_id, boundaries: refused };
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Drop a parked step whose root no longer exists, without an event or a revision move. Returns false and changes
|
|
507
|
+
* nothing when the root is still there or the step is not parked for this session.
|
|
508
|
+
*/
|
|
509
|
+
export function dropVanishedParkedBoundary(cwd, sessionId, boundaryId, options = {}) {
|
|
510
|
+
const paths = managedPaths(cwd, true);
|
|
511
|
+
return withStateLock(paths, options, () => {
|
|
512
|
+
const loaded = loadState(paths);
|
|
513
|
+
if (loaded.sourceSchema !== 3)
|
|
514
|
+
return false;
|
|
515
|
+
const work = selectWorkForSession(loaded.state, sessionId, ["active"]);
|
|
516
|
+
const entry = work?.parked_boundaries?.find((item) => item.boundary_id === boundaryId);
|
|
517
|
+
if (work === null || entry === undefined || pathExists(entry.root))
|
|
518
|
+
return false;
|
|
519
|
+
const nextWork = withParked(omitPendingBoundary(work), (work.parked_boundaries ?? []).filter((item) => item !== entry));
|
|
520
|
+
const nextState = { ...loaded.state, works: { ...loaded.state.works, [work.work_id]: nextWork } };
|
|
521
|
+
validateStateConsistency(nextState);
|
|
522
|
+
writeStateAtomic(paths, nextState, false);
|
|
523
|
+
options.afterStateWrite?.();
|
|
524
|
+
return true;
|
|
269
525
|
});
|
|
270
526
|
}
|
|
271
527
|
export function advanceStopLease(cwd, request, options = {}) {
|
|
@@ -296,17 +552,20 @@ export function advanceStopLease(cwd, request, options = {}) {
|
|
|
296
552
|
throw new LifecycleStoreError("PROGRESS_TOKEN_MISMATCH", "continuation progress token does not match the plan");
|
|
297
553
|
}
|
|
298
554
|
const lease = work.continuation_lease;
|
|
299
|
-
|
|
555
|
+
const parkedDigest = parkedDigestOf(work);
|
|
556
|
+
const sameQueue = (lease?.parked_digest ?? "") === parkedDigest;
|
|
557
|
+
if (lease?.progress_token === request.progressToken && lease.turn_id === request.turnId && sameQueue) {
|
|
300
558
|
const event = findCanonicalEvent(paths, state, lease.transition_id, options);
|
|
301
559
|
if (event === undefined || event.kind !== "start_work_continuation_issued") {
|
|
302
560
|
throw new LifecycleStoreError("LEASE_STATE_INVALID", "continuation lease lacks its canonical event");
|
|
303
561
|
}
|
|
304
562
|
return { outcome: "replay", state, event };
|
|
305
563
|
}
|
|
306
|
-
if (lease?.progress_token === request.progressToken)
|
|
564
|
+
if (lease?.progress_token === request.progressToken && sameQueue)
|
|
307
565
|
return { outcome: "silent", state };
|
|
308
566
|
const at = options.now?.() ?? new Date().toISOString();
|
|
309
|
-
|
|
567
|
+
// A newly parked step earns one more continuation; with nothing parked the id is the same as before.
|
|
568
|
+
const transitionId = `stop-continue:${shortDigest(request.sessionId)}:${shortDigest(request.turnId)}:${request.progressToken}${parkedDigest === "" ? "" : `:${parkedDigest}`}`;
|
|
310
569
|
const existing = findCanonicalEvent(paths, state, transitionId, options);
|
|
311
570
|
if (existing !== undefined) {
|
|
312
571
|
ensureLedgerEvent(paths, existing, options);
|
|
@@ -332,6 +591,7 @@ export function advanceStopLease(cwd, request, options = {}) {
|
|
|
332
591
|
progress_token: request.progressToken,
|
|
333
592
|
turn_id: request.turnId,
|
|
334
593
|
transition_id: transitionId,
|
|
594
|
+
...(parkedDigest === "" ? {} : { parked_digest: parkedDigest }),
|
|
335
595
|
},
|
|
336
596
|
last_transition: event,
|
|
337
597
|
};
|
|
@@ -534,6 +794,7 @@ function loadState(paths) {
|
|
|
534
794
|
historyFloor > revision) {
|
|
535
795
|
throw new LifecycleStoreError("STATE_REVISION_INVALID", "history floor is inconsistent with state revision");
|
|
536
796
|
}
|
|
797
|
+
const notices = parseCancelNotices(parsed["cancel_notices"]);
|
|
537
798
|
const state = {
|
|
538
799
|
...parsed,
|
|
539
800
|
schema_version: 3,
|
|
@@ -542,6 +803,7 @@ function loadState(paths) {
|
|
|
542
803
|
works,
|
|
543
804
|
...(history === undefined ? {} : { transition_history: history }),
|
|
544
805
|
...(schema === 2 ? {} : { history_floor_revision: historyFloor }),
|
|
806
|
+
...(notices === undefined ? {} : { cancel_notices: notices }),
|
|
545
807
|
};
|
|
546
808
|
if (schema === 3)
|
|
547
809
|
validateStateConsistency(state);
|
|
@@ -574,7 +836,10 @@ function parseWork(paths, id, value, schema) {
|
|
|
574
836
|
const last = parseLifecycleEvent(value["last_transition"]);
|
|
575
837
|
if (value["last_transition"] !== undefined && last === undefined)
|
|
576
838
|
return null;
|
|
577
|
-
|
|
839
|
+
// A work written after migrating an old pause is active while its last event still records the pause.
|
|
840
|
+
const migratedPause = status === "active" && last?.kind === "start_work_paused" && last.toWorkStatus === "paused";
|
|
841
|
+
if (last !== undefined &&
|
|
842
|
+
(last.workId !== id || last.plan !== activePlan || (last.toWorkStatus !== status && !migratedPause)))
|
|
578
843
|
return null;
|
|
579
844
|
let authority;
|
|
580
845
|
try {
|
|
@@ -591,8 +856,21 @@ function parseWork(paths, id, value, schema) {
|
|
|
591
856
|
if (pending !== undefined) {
|
|
592
857
|
if (status !== "paused" || authority === undefined)
|
|
593
858
|
return null;
|
|
594
|
-
validateBoundaryAgainstAuthority(pending, authority, true);
|
|
859
|
+
validateBoundaryAgainstAuthority(pending, authority, true, true);
|
|
860
|
+
}
|
|
861
|
+
const parked = parseParkedBoundaries(value["parked_boundaries"]);
|
|
862
|
+
if (value["parked_boundaries"] !== undefined && parked === undefined)
|
|
863
|
+
return null;
|
|
864
|
+
const denied = parseDeniedBoundaries(value["denied_boundaries"]);
|
|
865
|
+
if (value["denied_boundaries"] !== undefined && denied === undefined)
|
|
866
|
+
return null;
|
|
867
|
+
for (const entry of [...(parked ?? []), ...(denied ?? [])]) {
|
|
868
|
+
if (authority === undefined)
|
|
869
|
+
return null;
|
|
870
|
+
validateBoundaryAgainstAuthority(entry, authority, true, true);
|
|
595
871
|
}
|
|
872
|
+
if (parked !== undefined && parked.length > 0 && status !== "active")
|
|
873
|
+
return null;
|
|
596
874
|
const grants = parseGrants(value["authority_grants"]);
|
|
597
875
|
if (value["authority_grants"] !== undefined && grants === undefined)
|
|
598
876
|
return null;
|
|
@@ -628,27 +906,109 @@ function parseWork(paths, id, value, schema) {
|
|
|
628
906
|
}
|
|
629
907
|
}
|
|
630
908
|
}
|
|
631
|
-
|
|
909
|
+
// Read-time migration: a schema-3 run an older version paused at one boundary is now active with that step
|
|
910
|
+
// parked. The raw `pending_boundary` key is dropped here so no later write carries it back.
|
|
911
|
+
const migrate = schema === 3 && status === "paused" && pending !== undefined && last !== undefined;
|
|
912
|
+
const parsed = {
|
|
632
913
|
...value,
|
|
633
914
|
work_id: id,
|
|
634
915
|
active_plan: activePlan,
|
|
635
916
|
plan_name: value["plan_name"],
|
|
636
917
|
session_ids: sessionIds,
|
|
637
|
-
status,
|
|
918
|
+
status: migrate ? "active" : status,
|
|
638
919
|
worktree_path: normalizedWorktree,
|
|
639
920
|
...(authority === undefined ? {} : { authority }),
|
|
640
|
-
...(pending === undefined ? {} : { pending_boundary: pending }),
|
|
921
|
+
...(pending === undefined || migrate ? {} : { pending_boundary: pending }),
|
|
641
922
|
...(grants === undefined ? {} : { authority_grants: grants }),
|
|
642
923
|
...(lease === undefined ? {} : { continuation_lease: lease }),
|
|
643
924
|
...(last === undefined ? {} : { last_transition: last }),
|
|
925
|
+
...(denied === undefined ? {} : { denied_boundaries: denied }),
|
|
644
926
|
};
|
|
927
|
+
delete parsed["parked_boundaries"];
|
|
928
|
+
if (migrate)
|
|
929
|
+
delete parsed["pending_boundary"];
|
|
930
|
+
const queue = migrate && pending !== undefined && last !== undefined
|
|
931
|
+
? [{ ...pending, parked_at: last.at, session_id: last.sessionId, transition_id: last.transitionId }]
|
|
932
|
+
: (parked ?? []);
|
|
933
|
+
if (queue.length > 0)
|
|
934
|
+
parsed["parked_boundaries"] = queue;
|
|
935
|
+
return parsed;
|
|
936
|
+
}
|
|
937
|
+
function parseParkedBoundaries(value) {
|
|
938
|
+
if (value === undefined)
|
|
939
|
+
return undefined;
|
|
940
|
+
if (!Array.isArray(value) || value.length > MAX_PARKED_BOUNDARIES)
|
|
941
|
+
return undefined;
|
|
942
|
+
const entries = [];
|
|
943
|
+
for (const item of value) {
|
|
944
|
+
if (!isRecord(item))
|
|
945
|
+
return undefined;
|
|
946
|
+
const { parked_at: at, session_id: session, transition_id: transition, ...rest } = item;
|
|
947
|
+
const boundary = parseBoundary(rest);
|
|
948
|
+
if (boundary === undefined ||
|
|
949
|
+
!isBoundedLifecycleText(at) ||
|
|
950
|
+
!isBoundedLifecycleText(session) ||
|
|
951
|
+
!isBoundedLifecycleText(transition))
|
|
952
|
+
return undefined;
|
|
953
|
+
entries.push({ ...boundary, parked_at: at, session_id: session, transition_id: transition });
|
|
954
|
+
}
|
|
955
|
+
if (new Set(entries.map((entry) => entry.boundary_id)).size !== entries.length)
|
|
956
|
+
return undefined;
|
|
957
|
+
if (new Set(entries.map((entry) => `${entry.action}\0${entry.root}`)).size !== entries.length)
|
|
958
|
+
return undefined;
|
|
959
|
+
return entries;
|
|
645
960
|
}
|
|
646
|
-
function
|
|
961
|
+
function parseDeniedBoundaries(value) {
|
|
962
|
+
if (value === undefined)
|
|
963
|
+
return undefined;
|
|
964
|
+
if (!Array.isArray(value) || value.length > MAX_DENIED_BOUNDARIES)
|
|
965
|
+
return undefined;
|
|
966
|
+
const entries = value.map(parseBoundary);
|
|
967
|
+
return entries.every((entry) => entry !== undefined) ? entries : undefined;
|
|
968
|
+
}
|
|
969
|
+
function parseCancelNotices(value) {
|
|
970
|
+
if (value === undefined)
|
|
971
|
+
return undefined;
|
|
972
|
+
if (!isRecord(value))
|
|
973
|
+
throw new LifecycleStoreError("STATE_INVALID", "cancel_notices must be an object");
|
|
974
|
+
const entries = Object.entries(value);
|
|
975
|
+
if (entries.length > MAX_CANCEL_NOTICES) {
|
|
976
|
+
throw new LifecycleStoreError("STATE_INVALID", "cancel_notices exceeds its limit");
|
|
977
|
+
}
|
|
978
|
+
const notices = [];
|
|
979
|
+
for (const [key, notice] of entries) {
|
|
980
|
+
if (!key.startsWith("codex:") ||
|
|
981
|
+
!isBoundedLifecycleText(key) ||
|
|
982
|
+
!isRecord(notice) ||
|
|
983
|
+
Object.keys(notice).some((field) => !["work_id", "plan_name", "by_session_id", "at"].includes(field)) ||
|
|
984
|
+
!isBoundedLifecycleText(notice["work_id"]) ||
|
|
985
|
+
!isBoundedLifecycleText(notice["plan_name"], START_WORK_MAX_DISPLAY_BYTES) ||
|
|
986
|
+
!isBoundedLifecycleText(notice["by_session_id"]) ||
|
|
987
|
+
!isBoundedLifecycleText(notice["at"])) {
|
|
988
|
+
throw new LifecycleStoreError("STATE_INVALID", `invalid cancel notice ${key}`);
|
|
989
|
+
}
|
|
990
|
+
notices.push([
|
|
991
|
+
key,
|
|
992
|
+
{
|
|
993
|
+
work_id: notice["work_id"],
|
|
994
|
+
plan_name: notice["plan_name"],
|
|
995
|
+
by_session_id: notice["by_session_id"],
|
|
996
|
+
at: notice["at"],
|
|
997
|
+
},
|
|
998
|
+
]);
|
|
999
|
+
}
|
|
1000
|
+
return Object.fromEntries(notices);
|
|
1001
|
+
}
|
|
1002
|
+
/**
|
|
1003
|
+
* Lazily migrate a schema-2 state for a write. `cancelWorkId` names the work a typed cancel is about to end: it
|
|
1004
|
+
* becomes terminal, and a terminal work needs no authority, so the cancel can migrate without one.
|
|
1005
|
+
*/
|
|
1006
|
+
function normalizeStateForMutation(paths, loaded, migrationAuthority, cancelWorkId) {
|
|
647
1007
|
if (loaded.sourceSchema === 3)
|
|
648
1008
|
return loaded.state;
|
|
649
1009
|
const works = Object.fromEntries(Object.entries(loaded.state.works).map(([id, work]) => {
|
|
650
1010
|
const plan = canonicalPlan(paths, work.active_plan, true);
|
|
651
|
-
if (isNonterminal(work.status) && migrationAuthority === undefined) {
|
|
1011
|
+
if (isNonterminal(work.status) && migrationAuthority === undefined && id !== cancelWorkId) {
|
|
652
1012
|
throw new LifecycleStoreError("MIGRATION_AUTHORITY_REQUIRED", "schema-2 migration authority is required");
|
|
653
1013
|
}
|
|
654
1014
|
const authority = isNonterminal(work.status) ? migrationAuthority : work.authority;
|
|
@@ -751,7 +1111,11 @@ function validateStateConsistency(state) {
|
|
|
751
1111
|
}
|
|
752
1112
|
if (work.authority !== undefined && event.boundary !== undefined) {
|
|
753
1113
|
try {
|
|
754
|
-
|
|
1114
|
+
// A run keeps going past a parked step, so its root may be gone by the time the user answers.
|
|
1115
|
+
const mayBeGone = !isNonterminal(work.status) ||
|
|
1116
|
+
event.kind === "start_work_boundary_parked" ||
|
|
1117
|
+
event.kind === "start_work_paused";
|
|
1118
|
+
validateBoundaryAgainstAuthority(event.boundary, work.authority, true, mayBeGone);
|
|
755
1119
|
}
|
|
756
1120
|
catch {
|
|
757
1121
|
throw new LifecycleStoreError("AUTHORITY_INVALID", "history boundary is outside stored authority");
|
|
@@ -785,6 +1149,17 @@ function validateStateConsistency(state) {
|
|
|
785
1149
|
else if (state.revision !== 0) {
|
|
786
1150
|
throw new LifecycleStoreError("STATE_REVISION_INVALID", "nonzero revision requires retained history");
|
|
787
1151
|
}
|
|
1152
|
+
if (Object.keys(state.cancel_notices ?? {}).length > MAX_CANCEL_NOTICES) {
|
|
1153
|
+
throw new LifecycleStoreError("STATE_INVALID", "cancel_notices exceeds its limit");
|
|
1154
|
+
}
|
|
1155
|
+
for (const work of Object.values(state.works)) {
|
|
1156
|
+
const parked = work.parked_boundaries ?? [];
|
|
1157
|
+
if (parked.length > MAX_PARKED_BOUNDARIES ||
|
|
1158
|
+
(parked.length > 0 && (work.status !== "active" || state.active_work_id !== work.work_id)) ||
|
|
1159
|
+
new Set(parked.map((entry) => entry.boundary_id)).size !== parked.length) {
|
|
1160
|
+
throw new LifecycleStoreError("BOUNDARY_STATE_INVALID", "parked steps belong only to the active work");
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
788
1163
|
for (const work of Object.values(state.works)) {
|
|
789
1164
|
const last = work.last_transition;
|
|
790
1165
|
if (work.status === "paused" && last?.revision !== state.revision) {
|
|
@@ -828,6 +1203,9 @@ function parseLease(value) {
|
|
|
828
1203
|
return undefined;
|
|
829
1204
|
if (!isRecord(value))
|
|
830
1205
|
return undefined;
|
|
1206
|
+
const parkedDigest = value["parked_digest"];
|
|
1207
|
+
if (parkedDigest !== undefined && !isShortHash(parkedDigest))
|
|
1208
|
+
return undefined;
|
|
831
1209
|
return isHash(value["progress_token"]) &&
|
|
832
1210
|
isBoundedLifecycleText(value["turn_id"]) &&
|
|
833
1211
|
isBoundedLifecycleText(value["transition_id"])
|
|
@@ -835,6 +1213,7 @@ function parseLease(value) {
|
|
|
835
1213
|
progress_token: value["progress_token"],
|
|
836
1214
|
turn_id: value["turn_id"],
|
|
837
1215
|
transition_id: value["transition_id"],
|
|
1216
|
+
...(parkedDigest === undefined ? {} : { parked_digest: parkedDigest }),
|
|
838
1217
|
}
|
|
839
1218
|
: undefined;
|
|
840
1219
|
}
|
|
@@ -872,6 +1251,7 @@ function parseLifecycleEvent(value) {
|
|
|
872
1251
|
"initFingerprint",
|
|
873
1252
|
"boundary",
|
|
874
1253
|
"grant",
|
|
1254
|
+
"boundaryIds",
|
|
875
1255
|
]);
|
|
876
1256
|
if (Object.keys(value).some((key) => !allowed.has(key)))
|
|
877
1257
|
return undefined;
|
|
@@ -901,13 +1281,17 @@ function parseLifecycleEvent(value) {
|
|
|
901
1281
|
const grant = parseGrant(value["grant"]);
|
|
902
1282
|
if (value["grant"] !== undefined && grant === undefined)
|
|
903
1283
|
return undefined;
|
|
1284
|
+
const ids = value["boundaryIds"];
|
|
1285
|
+
if (ids !== undefined &&
|
|
1286
|
+
!(Array.isArray(ids) && ids.length <= MAX_PARKED_BOUNDARIES && ids.every((id) => isBoundedLifecycleText(id))))
|
|
1287
|
+
return undefined;
|
|
904
1288
|
return {
|
|
905
1289
|
...value,
|
|
906
1290
|
...(boundary === undefined ? {} : { boundary }),
|
|
907
1291
|
...(grant === undefined ? {} : { grant }),
|
|
908
1292
|
};
|
|
909
1293
|
}
|
|
910
|
-
function transitionEvent(state, work, request, target,
|
|
1294
|
+
function transitionEvent(state, work, request, target, planPath, progressToken, at, boundary, grant) {
|
|
911
1295
|
return {
|
|
912
1296
|
kind: kindForAction(request.action),
|
|
913
1297
|
at,
|
|
@@ -916,20 +1300,22 @@ function transitionEvent(state, work, request, target, plan, at, boundary, grant
|
|
|
916
1300
|
revision: state.revision + 1,
|
|
917
1301
|
fromWorkStatus: work.status,
|
|
918
1302
|
toWorkStatus: target,
|
|
919
|
-
plan:
|
|
1303
|
+
plan: planPath,
|
|
920
1304
|
sessionId: `codex:${request.sessionId}`,
|
|
921
1305
|
reasonCode: request.reasonCode,
|
|
922
1306
|
...(request.turnId === undefined ? {} : { turnId: request.turnId }),
|
|
923
|
-
progressToken
|
|
1307
|
+
progressToken,
|
|
924
1308
|
...(boundary === undefined ? {} : { boundary }),
|
|
925
1309
|
...(grant === undefined ? {} : { grant }),
|
|
926
1310
|
};
|
|
927
1311
|
}
|
|
928
1312
|
function targetStatus(action, status, reasonCode) {
|
|
1313
|
+
// A pause parks the step and the run keeps going; a resume of a parked step keeps it going too.
|
|
929
1314
|
if (action === "pause" && status === "active" && isPauseReason(reasonCode))
|
|
930
|
-
return "paused";
|
|
931
|
-
if (action === "resume" && status === "paused" && reasonCode === "explicit_user_resume")
|
|
932
1315
|
return "active";
|
|
1316
|
+
if (action === "resume" && (status === "paused" || status === "active") && reasonCode === "explicit_user_resume") {
|
|
1317
|
+
return "active";
|
|
1318
|
+
}
|
|
933
1319
|
if (action === "cancel" && (status === "active" || status === "paused") && reasonCode === "user_cancelled") {
|
|
934
1320
|
return "abandoned";
|
|
935
1321
|
}
|
|
@@ -940,7 +1326,7 @@ function targetStatus(action, status, reasonCode) {
|
|
|
940
1326
|
}
|
|
941
1327
|
function kindForAction(action) {
|
|
942
1328
|
if (action === "pause")
|
|
943
|
-
return "
|
|
1329
|
+
return "start_work_boundary_parked";
|
|
944
1330
|
if (action === "resume")
|
|
945
1331
|
return "start_work_resumed";
|
|
946
1332
|
if (action === "cancel")
|
|
@@ -948,7 +1334,10 @@ function kindForAction(action) {
|
|
|
948
1334
|
return "start_work_completed";
|
|
949
1335
|
}
|
|
950
1336
|
function requestMatchesEvent(request, event) {
|
|
951
|
-
|
|
1337
|
+
// An old pause transition id still replays: older versions recorded it as `start_work_paused`.
|
|
1338
|
+
const kindMatches = event.kind === kindForAction(request.action) ||
|
|
1339
|
+
(request.action === "pause" && event.kind === "start_work_paused");
|
|
1340
|
+
return (kindMatches &&
|
|
952
1341
|
event.workId === request.workId &&
|
|
953
1342
|
event.sessionId === `codex:${request.sessionId}` &&
|
|
954
1343
|
event.reasonCode === request.reasonCode &&
|
|
@@ -1577,6 +1966,7 @@ function stableEventJson(event) {
|
|
|
1577
1966
|
...(event.initFingerprint === undefined ? {} : { initFingerprint: event.initFingerprint }),
|
|
1578
1967
|
...(event.boundary === undefined ? {} : { boundary: event.boundary }),
|
|
1579
1968
|
...(event.grant === undefined ? {} : { grant: event.grant }),
|
|
1969
|
+
...(event.boundaryIds === undefined ? {} : { boundaryIds: event.boundaryIds }),
|
|
1580
1970
|
});
|
|
1581
1971
|
}
|
|
1582
1972
|
function optionalStableEqual(left, right) {
|
|
@@ -1639,6 +2029,9 @@ function shortDigest(value) {
|
|
|
1639
2029
|
function isHash(value) {
|
|
1640
2030
|
return typeof value === "string" && /^[a-f0-9]{64}$/.test(value);
|
|
1641
2031
|
}
|
|
2032
|
+
function isShortHash(value) {
|
|
2033
|
+
return typeof value === "string" && /^[a-f0-9]{16}$/.test(value);
|
|
2034
|
+
}
|
|
1642
2035
|
function isPauseReason(value) {
|
|
1643
2036
|
return value === "authorization_required" || value === "credential_required" || value === "host_capability_required";
|
|
1644
2037
|
}
|
|
@@ -1656,7 +2049,9 @@ function isLifecycleKind(value) {
|
|
|
1656
2049
|
value === "start_work_paused" ||
|
|
1657
2050
|
value === "start_work_resumed" ||
|
|
1658
2051
|
value === "start_work_cancelled" ||
|
|
1659
|
-
value === "start_work_completed"
|
|
2052
|
+
value === "start_work_completed" ||
|
|
2053
|
+
value === "start_work_boundary_parked" ||
|
|
2054
|
+
value === "start_work_boundary_denied");
|
|
1660
2055
|
}
|
|
1661
2056
|
function isWorkStatus(value) {
|
|
1662
2057
|
return value === "active" || value === "paused" || value === "abandoned" || value === "completed";
|
|
@@ -1667,14 +2062,41 @@ function isNonterminal(value) {
|
|
|
1667
2062
|
function isTransitionAction(value) {
|
|
1668
2063
|
return value === "pause" || value === "resume" || value === "cancel" || value === "complete";
|
|
1669
2064
|
}
|
|
1670
|
-
function terminalAction(value) {
|
|
1671
|
-
return value === "cancel" || value === "complete";
|
|
1672
|
-
}
|
|
1673
2065
|
function omitPendingBoundary(work) {
|
|
1674
2066
|
const copy = { ...work };
|
|
1675
2067
|
delete copy.pending_boundary;
|
|
1676
2068
|
return copy;
|
|
1677
2069
|
}
|
|
2070
|
+
/** Set the parked queue; an empty queue removes the key so terminal and quiet works keep their old shape. */
|
|
2071
|
+
function withParked(work, parked) {
|
|
2072
|
+
const copy = { ...work };
|
|
2073
|
+
if (parked.length === 0)
|
|
2074
|
+
delete copy["parked_boundaries"];
|
|
2075
|
+
else
|
|
2076
|
+
copy["parked_boundaries"] = parked;
|
|
2077
|
+
return copy;
|
|
2078
|
+
}
|
|
2079
|
+
function boundaryFields(entry) {
|
|
2080
|
+
return {
|
|
2081
|
+
boundary_id: entry.boundary_id,
|
|
2082
|
+
authority_id: entry.authority_id,
|
|
2083
|
+
action: entry.action,
|
|
2084
|
+
root: entry.root,
|
|
2085
|
+
};
|
|
2086
|
+
}
|
|
2087
|
+
export function isReleaseClass(action) {
|
|
2088
|
+
return RELEASE_CLASS_ACTIONS.has(action.toLowerCase().replace(/[_ .]+/gu, "-"));
|
|
2089
|
+
}
|
|
2090
|
+
function isReadAction(action) {
|
|
2091
|
+
return action === "read" || action === "inspect";
|
|
2092
|
+
}
|
|
2093
|
+
function accessKindOf(action) {
|
|
2094
|
+
return isReadAction(action) ? "read" : "write";
|
|
2095
|
+
}
|
|
2096
|
+
function parkedDigestOf(work) {
|
|
2097
|
+
const ids = (work.parked_boundaries ?? []).map((entry) => entry.boundary_id).sort();
|
|
2098
|
+
return ids.length === 0 ? "" : shortDigest(ids.join("\0"));
|
|
2099
|
+
}
|
|
1678
2100
|
function isCode(error, code) {
|
|
1679
2101
|
return isRecord(error) && error["code"] === code;
|
|
1680
2102
|
}
|