@nathanstephenson/flow 0.1.9 → 0.1.10-beta.10
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 +60 -21
- package/build/backend-worker-chunks/chunk-3KDHJA5P.mjs +162 -0
- package/build/backend-worker-chunks/{chunk-UQDH2COY.mjs → chunk-K774KOGL.mjs} +2 -7
- package/build/backend-worker-chunks/chunk-LAEUXDNH.mjs +130 -0
- package/build/backend-worker-chunks/{chunk-MLOUI3ZM.mjs → chunk-NZZME2I3.mjs} +6 -1
- package/build/backend-worker-chunks/claude-JG2FXYBQ.mjs +35859 -0
- package/build/backend-worker-chunks/{pi-M4OA2FGL.mjs → pi-KJSP23AO.mjs} +2138 -93
- package/build/backend-worker.mjs +77 -123
- package/build/workflow-runtime.cjs +12 -12
- package/dist/backend/claude/background-calls.js +7 -3
- package/dist/backend/claude/import.js +415 -0
- package/dist/backend/claude/index.js +201 -21
- package/dist/backend/claude/session-tools.js +14 -0
- package/dist/backend/claude/subagents.js +41 -9
- package/dist/backend/claude/todo-list.js +114 -0
- package/dist/backend/claude/workflow-parent.js +1 -1
- package/dist/backend/diff-excerpt-instructions.js +8 -0
- package/dist/backend/fake/index.js +54 -6
- package/dist/backend/mcp.js +4 -1
- package/dist/backend/pi/agent-permissions.js +1 -1
- package/dist/backend/pi/cache-warming.js +8 -0
- package/dist/backend/pi/canonical-json.js +120 -0
- package/dist/backend/pi/compaction-model.js +3 -2
- package/dist/backend/pi/host-auth.js +258 -0
- package/dist/backend/pi/import.js +269 -0
- package/dist/backend/pi/index.js +125 -21
- package/dist/backend/pi/model-auth.js +159 -0
- package/dist/backend/pi/model-catalog.js +120 -0
- package/dist/backend/pi/model-config.js +5 -0
- package/dist/backend/pi/model-selection.js +32 -0
- package/dist/backend/pi/plan-limits.js +41 -0
- package/dist/backend/pi/session-tools.js +21 -0
- package/dist/backend/pi/state.js +19 -1
- package/dist/backend/pi/subagents.js +7 -3
- package/dist/backend/pi/todo-list.js +23 -0
- package/dist/backend/pi/work.js +9 -1
- package/dist/backend/pi/workflow-parent.js +1 -1
- package/dist/backend/pi/workflow-subagent.js +4 -3
- package/dist/backend/plan-limits.js +17 -0
- package/dist/backend/session-tools.js +27 -0
- package/dist/backend/worker/claude-state.js +48 -0
- package/dist/backend/worker/entry.js +49 -8
- package/dist/backend/worker/index.js +155 -17
- package/dist/backend/worker/launcher.js +11 -4
- package/dist/backend/worker/protocol.js +2 -1
- package/dist/backend/worker/rpc.js +13 -0
- package/dist/backend/worker/sandbox-mcp.js +18 -7
- package/dist/backend/workflow-tools.js +9 -6
- package/dist/build-id +1 -1
- package/dist/cli/application.js +48 -42
- package/dist/cli/bootstrap.js +2 -2
- package/dist/cli/host-control.js +1 -1
- package/dist/cli/install-guard.js +2 -2
- package/dist/cli/main.js +4 -4
- package/dist/cli/systemd-update.js +3 -3
- package/dist/cli/update.js +6 -6
- package/dist/cli/web-update.js +6 -6
- package/dist/client/connection.js +46 -0
- package/dist/client/context-usage.js +3 -4
- package/dist/client/diff.js +40 -0
- package/dist/client/enquiry.js +0 -6
- package/dist/client/markdown.js +3 -2
- package/dist/client/model-choices.js +1 -1
- package/dist/client/permission.js +1 -2
- package/dist/client/project-name.js +3 -1
- package/dist/client/reduce.js +77 -3
- package/dist/client/relative-time.js +1 -1
- package/dist/client/scope-kind.js +6 -3
- package/dist/client/search.js +1 -1
- package/dist/client/status.js +1 -1
- package/dist/client/subagent-tree.js +49 -0
- package/dist/client/tool-chains.js +1 -1
- package/dist/client/tool-summary.js +28 -18
- package/dist/daemon/changes.js +128 -0
- package/dist/daemon/commit.js +84 -0
- package/dist/daemon/config-store.js +8 -3
- package/dist/daemon/config.js +105 -4
- package/dist/daemon/diff-excerpts.js +52 -0
- package/dist/daemon/git-state.js +197 -0
- package/dist/daemon/git.js +53 -10
- package/dist/daemon/github-image.js +18 -0
- package/dist/daemon/github.js +54 -0
- package/dist/daemon/host-control-routes.js +1 -1
- package/dist/daemon/host.js +1010 -116
- package/dist/daemon/http.js +4 -1
- package/dist/daemon/log.js +3 -0
- package/dist/daemon/mcp-auth.js +1 -1
- package/dist/daemon/models.js +2 -2
- package/dist/daemon/pi-model-cache.js +44 -0
- package/dist/daemon/pull-request.js +223 -138
- package/dist/daemon/push.js +43 -0
- package/dist/daemon/repository.js +123 -0
- package/dist/daemon/runtime.js +103 -22
- package/dist/daemon/secret-store.js +1 -1
- package/dist/daemon/server.js +111 -14
- package/dist/daemon/session-tool-views.js +22 -0
- package/dist/daemon/skills.js +24 -0
- package/dist/daemon/spend-ledger.js +118 -0
- package/dist/daemon/stack-metadata.js +12 -5
- package/dist/daemon/stack.js +162 -657
- package/dist/daemon/store.js +2 -0
- package/dist/daemon/summariser.js +30 -12
- package/dist/daemon/tour.js +56 -0
- package/dist/daemon/whisper.js +144 -0
- package/dist/daemon/workflow-authoring-scope-checks.js +1 -1
- package/dist/daemon/workflow-authoring-scope.js +1 -1
- package/dist/daemon/workflow-builder-files.js +2 -2
- package/dist/daemon/workflow-builder-guidance.js +1 -1
- package/dist/daemon/workflow-builder-routes.js +2 -2
- package/dist/daemon/workflow-builder.js +44 -17
- package/dist/daemon/workflow-environment.js +193 -0
- package/dist/daemon/workflow-execution-launch.js +77 -0
- package/dist/daemon/workflow-execution-resources.js +76 -0
- package/dist/daemon/workflow-execution-routes.js +17 -8
- package/dist/daemon/workflow-execution-values.js +128 -0
- package/dist/daemon/workflow-executions.js +434 -305
- package/dist/daemon/workflow-mcp-authoring-routes.js +2 -2
- package/dist/daemon/workflow-mcp-authoring.js +4 -4
- package/dist/daemon/workflow-mcp.js +1 -1
- package/dist/daemon/workflow-routes.js +60 -2
- package/dist/daemon/workflow-schedule-driver.js +251 -0
- package/dist/daemon/workflow-schedule-routes.js +137 -0
- package/dist/daemon/workflow-schedules.js +342 -0
- package/dist/isolation/filesystem.js +6 -2
- package/dist/isolation/sandbox.js +61 -18
- package/dist/isolation/shared-sandbox.js +3 -3
- package/dist/isolation/toolchain-assets.js +1 -1
- package/dist/protocol/events.js +3 -0
- package/dist/protocol/plan-limits.js +1 -0
- package/dist/protocol/settings.js +1 -0
- package/dist/protocol/spend.js +2 -0
- package/dist/protocol/stack.js +12 -39
- package/dist/protocol/workflow-schedules.js +1 -0
- package/dist/workflows/calls.js +53 -0
- package/dist/workflows/errors.js +2 -0
- package/dist/workflows/executors.js +6 -5
- package/dist/workflows/graph.js +83 -9
- package/dist/workflows/invocations.js +23 -0
- package/dist/workflows/mcp.js +1 -1
- package/dist/workflows/owner.js +45 -0
- package/dist/workflows/records.js +150 -10
- package/dist/workflows/recovery.js +87 -2
- package/dist/workflows/runtime-settings.js +16 -16
- package/dist/workflows/runtime.js +2 -2
- package/dist/workflows/schedule-errors.js +2 -0
- package/dist/workflows/schedule-store.js +385 -0
- package/dist/workflows/scheduler.js +389 -98
- package/dist/workflows/schedules.js +130 -0
- package/dist/workflows/schema.js +3 -1
- package/dist/workflows/store.js +126 -13
- package/package.json +10 -6
- package/web/dist/assets/index-C76t-z4w.css +2 -0
- package/web/dist/assets/{index-DRfeO6GJ.js → index-WPfbYVQ4.js} +39 -23
- package/web/dist/assets/{switch-DIogWclb.js → select-C5Ly2OpB.js} +1 -1
- package/web/dist/assets/{settings-mcp-Clp_jMWW.js → settings-mcp-BF-yqpeQ.js} +1 -1
- package/web/dist/assets/{settings-secrets-BIO_hXZ1.js → settings-secrets-WLarr2QP.js} +1 -1
- package/web/dist/assets/settings-workflows-DdcdRWFD.js +23 -0
- package/web/dist/assets/workflow-api-GjAmHc22.js +6 -0
- package/web/dist/assets/workflows-BhY3Uibo.js +149 -0
- package/web/dist/assets/workflows-pane-DElCcEpN.js +1 -0
- package/web/dist/index.html +6 -5
- package/build/backend-worker-chunks/chunk-J2DBNTL7.mjs +0 -94
- package/build/backend-worker-chunks/claude-GTW5KHY5.mjs +0 -30778
- package/dist/daemon/publish.js +0 -289
- package/dist/tui/app.js +0 -612
- package/dist/tui/keys.js +0 -90
- package/dist/tui/render.js +0 -411
- package/web/dist/assets/index-DgQFpKbn.css +0 -2
- package/web/dist/assets/settings-workflows-H-6eCff3.js +0 -23
- package/web/dist/assets/workflow-api-BfExddae.js +0 -6
- package/web/dist/assets/workflow-editors-CsbVx4WJ.js +0 -139
- package/web/dist/assets/workflows-Bv0sj7d0.js +0 -7
- package/web/dist/assets/workflows-pane-DHm44w7P.js +0 -1
- /package/dist/protocol/{publish.js → imports.js} +0 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { dirname, join } from 'node:path';
|
|
6
|
+
import { ScheduleValidationError } from './schedule-errors.js';
|
|
7
|
+
import { isActiveOccurrence, occurrenceValidator, scheduleValidator, SCHEDULE_RETENTION_MS, storageId, timestamp } from './schedules.js';
|
|
8
|
+
function sync(directory) {
|
|
9
|
+
const fd = openSync(directory, 'r');
|
|
10
|
+
try {
|
|
11
|
+
fsyncSync(fd);
|
|
12
|
+
}
|
|
13
|
+
finally {
|
|
14
|
+
closeSync(fd);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function durableDirectory(directory) {
|
|
18
|
+
if (existsSync(directory))
|
|
19
|
+
return;
|
|
20
|
+
const parent = dirname(directory);
|
|
21
|
+
durableDirectory(parent);
|
|
22
|
+
mkdirSync(directory, { mode: 0o700 });
|
|
23
|
+
sync(parent);
|
|
24
|
+
}
|
|
25
|
+
function atomicWrite(path, value) {
|
|
26
|
+
const directory = dirname(path);
|
|
27
|
+
durableDirectory(directory);
|
|
28
|
+
const temporary = join(directory, `.${randomUUID()}.tmp`);
|
|
29
|
+
try {
|
|
30
|
+
const fd = openSync(temporary, 'wx', 0o600);
|
|
31
|
+
try {
|
|
32
|
+
writeFileSync(fd, JSON.stringify(value));
|
|
33
|
+
fsyncSync(fd);
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
closeSync(fd);
|
|
37
|
+
}
|
|
38
|
+
renameSync(temporary, path);
|
|
39
|
+
sync(directory);
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
rmSync(temporary, { force: true });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function ids(directory, invalid) {
|
|
46
|
+
if (!existsSync(directory))
|
|
47
|
+
return [];
|
|
48
|
+
return readdirSync(directory).filter(name => name.endsWith('.json')).flatMap(name => {
|
|
49
|
+
const id = storageId.safeParse(name.slice(0, -5));
|
|
50
|
+
if (id.success)
|
|
51
|
+
return [id.data];
|
|
52
|
+
invalid?.();
|
|
53
|
+
console.error('Workflow Schedule record has an invalid filename. File retained for manual review.');
|
|
54
|
+
return [];
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const cleanupJournalValidator = z.object({ occurrenceId: storageId, status: z.enum(['pending', 'done']) }).strict();
|
|
58
|
+
function needsUnstartedCleanup(value) {
|
|
59
|
+
return value.executionId === undefined && ['interrupted', 'launch-failed', 'cancelled'].includes(value.status);
|
|
60
|
+
}
|
|
61
|
+
export class ScheduleRecordNotFound extends Error {
|
|
62
|
+
}
|
|
63
|
+
export class ScheduleRevisionConflict extends Error {
|
|
64
|
+
constructor() { super('Schedule changed; reload before editing'); }
|
|
65
|
+
}
|
|
66
|
+
/** Private, durable records independent of Agent Session lifecycle and transcript retention. */
|
|
67
|
+
export class WorkflowScheduleStore {
|
|
68
|
+
root;
|
|
69
|
+
schedules = new Map();
|
|
70
|
+
occurrences = new Map();
|
|
71
|
+
activeRecords = new Map();
|
|
72
|
+
history = [];
|
|
73
|
+
historyBySchedule = new Map();
|
|
74
|
+
active = new Set();
|
|
75
|
+
activeBySchedule = new Map();
|
|
76
|
+
projection;
|
|
77
|
+
pruning;
|
|
78
|
+
unknownOccurrences = false;
|
|
79
|
+
unstartedCleanup = new Map();
|
|
80
|
+
constructor(stateRoot) {
|
|
81
|
+
this.root = join(stateRoot, 'workflow-schedules');
|
|
82
|
+
for (const id of ids(join(this.root, 'unstarted-cleanup'))) {
|
|
83
|
+
try {
|
|
84
|
+
const value = cleanupJournalValidator.parse(JSON.parse(readFileSync(this.cleanupPath(id), 'utf8')));
|
|
85
|
+
if (value.occurrenceId !== id)
|
|
86
|
+
throw new Error('Invalid cleanup identity');
|
|
87
|
+
this.unstartedCleanup.set(id, value.status);
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
this.unstartedCleanup.set(id, 'unknown');
|
|
91
|
+
console.error('Workflow Occurrence cleanup journal is invalid. File retained for manual review.');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
for (const id of ids(join(this.root, 'definitions'))) {
|
|
95
|
+
try {
|
|
96
|
+
const value = scheduleValidator.parse(JSON.parse(readFileSync(this.schedulePath(id), 'utf8')));
|
|
97
|
+
if (value.id !== id)
|
|
98
|
+
throw new Error('Invalid schedule identity');
|
|
99
|
+
this.schedules.set(id, value);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
console.error('Workflow Schedule record is invalid. File retained for manual review.');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
for (const id of ids(join(this.root, 'occurrences'), () => { this.unknownOccurrences = true; })) {
|
|
106
|
+
try {
|
|
107
|
+
const value = occurrenceValidator.parse(JSON.parse(readFileSync(this.occurrencePath(id), 'utf8')));
|
|
108
|
+
if (value.id !== id)
|
|
109
|
+
throw new Error('Invalid occurrence identity');
|
|
110
|
+
this.indexOccurrence(value);
|
|
111
|
+
try {
|
|
112
|
+
this.queueUnstartedCleanup(value);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
this.unstartedCleanup.set(id, 'pending');
|
|
116
|
+
console.error('Workflow Occurrence cleanup journal could not be saved. Its record will be retained for retry.');
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
this.unknownOccurrences = true;
|
|
121
|
+
console.error('Workflow Occurrence record is invalid. File retained for manual review.');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
cleanupPath(id) { return join(this.root, 'unstarted-cleanup', `${storageId.parse(id)}.json`); }
|
|
126
|
+
queueUnstartedCleanup(value) {
|
|
127
|
+
if (!needsUnstartedCleanup(value) || this.unstartedCleanup.has(value.id))
|
|
128
|
+
return;
|
|
129
|
+
atomicWrite(this.cleanupPath(value.id), { occurrenceId: value.id, status: 'pending' });
|
|
130
|
+
this.unstartedCleanup.set(value.id, 'pending');
|
|
131
|
+
}
|
|
132
|
+
pendingUnstartedCleanup() { return [...this.unstartedCleanup].filter(([, status]) => status === 'pending').map(([id]) => id); }
|
|
133
|
+
prepareUnstartedCleanup(id) {
|
|
134
|
+
atomicWrite(this.cleanupPath(id), { occurrenceId: id, status: 'pending' });
|
|
135
|
+
}
|
|
136
|
+
completeUnstartedCleanup(id) {
|
|
137
|
+
atomicWrite(this.cleanupPath(id), { occurrenceId: id, status: 'done' });
|
|
138
|
+
this.unstartedCleanup.set(id, 'done');
|
|
139
|
+
}
|
|
140
|
+
setOccurrenceProjection(project) { this.projection = project; }
|
|
141
|
+
project(value) {
|
|
142
|
+
const copy = structuredClone(value);
|
|
143
|
+
return isActiveOccurrence(value) && this.projection ? this.projection(copy) : copy;
|
|
144
|
+
}
|
|
145
|
+
indexOccurrence(value) {
|
|
146
|
+
const fresh = !this.occurrences.has(value.id);
|
|
147
|
+
const { id, scheduleId, scheduledAt, status, finishedAt } = value;
|
|
148
|
+
this.occurrences.set(id, { id, scheduleId, scheduledAt, status, ...(finishedAt === undefined ? {} : { finishedAt }) });
|
|
149
|
+
if (isActiveOccurrence(value))
|
|
150
|
+
this.activeRecords.set(id, value);
|
|
151
|
+
else
|
|
152
|
+
this.activeRecords.delete(id);
|
|
153
|
+
const active = this.activeBySchedule.get(value.scheduleId) ?? new Set();
|
|
154
|
+
this.activeBySchedule.set(value.scheduleId, active);
|
|
155
|
+
if (isActiveOccurrence(value)) {
|
|
156
|
+
this.active.add(value.id);
|
|
157
|
+
active.add(value.id);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
this.active.delete(value.id);
|
|
161
|
+
active.delete(value.id);
|
|
162
|
+
}
|
|
163
|
+
if (!fresh)
|
|
164
|
+
return;
|
|
165
|
+
const history = this.historyBySchedule.get(value.scheduleId) ?? [];
|
|
166
|
+
this.historyBySchedule.set(value.scheduleId, history);
|
|
167
|
+
for (const list of [this.history, history]) {
|
|
168
|
+
let low = 0, high = list.length;
|
|
169
|
+
while (low < high) {
|
|
170
|
+
const mid = (low + high) >>> 1;
|
|
171
|
+
const other = this.occurrences.get(list[mid]);
|
|
172
|
+
if (other.scheduledAt > value.scheduledAt || (other.scheduledAt === value.scheduledAt && other.id > value.id))
|
|
173
|
+
low = mid + 1;
|
|
174
|
+
else
|
|
175
|
+
high = mid;
|
|
176
|
+
}
|
|
177
|
+
list.splice(low, 0, value.id);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
historyIds(scheduleId) {
|
|
181
|
+
if (scheduleId === undefined)
|
|
182
|
+
return this.history;
|
|
183
|
+
storageId.parse(scheduleId);
|
|
184
|
+
return this.historyBySchedule.get(scheduleId) ?? [];
|
|
185
|
+
}
|
|
186
|
+
listActiveOccurrences(scheduleId) {
|
|
187
|
+
if (scheduleId !== undefined)
|
|
188
|
+
storageId.parse(scheduleId);
|
|
189
|
+
const active = scheduleId === undefined ? this.active : this.activeBySchedule.get(scheduleId) ?? [];
|
|
190
|
+
return [...active].map(id => this.getOccurrence(id)).filter(isActiveOccurrence);
|
|
191
|
+
}
|
|
192
|
+
activeCount(scheduleId) { return this.listActiveOccurrences(scheduleId).length; }
|
|
193
|
+
schedulePath(id) { return join(this.root, 'definitions', `${storageId.parse(id)}.json`); }
|
|
194
|
+
occurrencePath(id) { return join(this.root, 'occurrences', `${storageId.parse(id)}.json`); }
|
|
195
|
+
getSchedule(id) {
|
|
196
|
+
storageId.parse(id);
|
|
197
|
+
const value = this.schedules.get(id);
|
|
198
|
+
if (!value)
|
|
199
|
+
throw new ScheduleRecordNotFound('Schedule not found');
|
|
200
|
+
return structuredClone(value);
|
|
201
|
+
}
|
|
202
|
+
listSchedules(includeDeleted = false) {
|
|
203
|
+
return [...this.schedules.values()].filter(value => includeDeleted || value.deletedAt === undefined).map(value => structuredClone(value));
|
|
204
|
+
}
|
|
205
|
+
saveSchedule(value, expectedRevision) {
|
|
206
|
+
const validated = scheduleValidator.parse(value);
|
|
207
|
+
const path = this.schedulePath(validated.id);
|
|
208
|
+
if (expectedRevision === undefined) {
|
|
209
|
+
if (existsSync(path))
|
|
210
|
+
throw new ScheduleRevisionConflict();
|
|
211
|
+
}
|
|
212
|
+
else if (this.getSchedule(validated.id).revision !== expectedRevision || validated.revision !== expectedRevision + 1)
|
|
213
|
+
throw new ScheduleRevisionConflict();
|
|
214
|
+
atomicWrite(path, validated);
|
|
215
|
+
this.schedules.set(validated.id, validated);
|
|
216
|
+
}
|
|
217
|
+
/** Cursor advancement is not a user edit and retains the activation/revision. */
|
|
218
|
+
advanceCursor(id, revision, due, nextDue) {
|
|
219
|
+
const saved = this.getSchedule(id);
|
|
220
|
+
if (!saved.enabled || saved.revision !== revision || saved.nextDue !== due)
|
|
221
|
+
throw new ScheduleRevisionConflict();
|
|
222
|
+
if (nextDue <= due)
|
|
223
|
+
throw new Error('Schedule cursor must advance');
|
|
224
|
+
const value = scheduleValidator.parse({ ...saved, nextDue });
|
|
225
|
+
atomicWrite(this.schedulePath(id), value);
|
|
226
|
+
this.schedules.set(id, value);
|
|
227
|
+
}
|
|
228
|
+
deleteSchedule(id, revision, now) {
|
|
229
|
+
const saved = this.getSchedule(id);
|
|
230
|
+
const { nextDue: _, ...rest } = saved;
|
|
231
|
+
this.saveSchedule({ ...rest, enabled: false, revision: saved.revision + 1, deletedAt: timestamp.parse(now), updatedAt: now }, revision);
|
|
232
|
+
}
|
|
233
|
+
readOccurrence(id) {
|
|
234
|
+
storageId.parse(id);
|
|
235
|
+
if (!this.occurrences.has(id))
|
|
236
|
+
throw new ScheduleRecordNotFound('Occurrence not found');
|
|
237
|
+
const value = this.activeRecords.get(id) ?? occurrenceValidator.parse(JSON.parse(readFileSync(this.occurrencePath(id), 'utf8')));
|
|
238
|
+
if (value.id !== id)
|
|
239
|
+
throw new Error('Invalid occurrence identity');
|
|
240
|
+
return value;
|
|
241
|
+
}
|
|
242
|
+
getStoredOccurrence(id) { return structuredClone(this.readOccurrence(id)); }
|
|
243
|
+
getOccurrence(id) { return this.project(this.readOccurrence(id)); }
|
|
244
|
+
listOccurrences(scheduleId) {
|
|
245
|
+
return this.historyIds(scheduleId).map(id => this.getOccurrence(id));
|
|
246
|
+
}
|
|
247
|
+
page(options = {}) {
|
|
248
|
+
const limit = options.limit ?? 50;
|
|
249
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 200)
|
|
250
|
+
throw new ScheduleValidationError('Invalid history limit');
|
|
251
|
+
const values = this.historyIds(options.scheduleId);
|
|
252
|
+
let start = 0;
|
|
253
|
+
if (options.before !== undefined) {
|
|
254
|
+
if (!storageId.safeParse(options.before).success)
|
|
255
|
+
throw new ScheduleValidationError('Invalid history cursor');
|
|
256
|
+
const cursor = /^(\d+)_([a-f0-9]{64})$/.exec(options.before);
|
|
257
|
+
if (cursor) {
|
|
258
|
+
const parsed = timestamp.safeParse(Number(cursor[1]));
|
|
259
|
+
if (!parsed.success)
|
|
260
|
+
throw new ScheduleValidationError('Invalid history cursor');
|
|
261
|
+
const due = parsed.data, id = cursor[2];
|
|
262
|
+
let high = values.length;
|
|
263
|
+
while (start < high) {
|
|
264
|
+
const mid = (start + high) >>> 1, other = this.occurrences.get(values[mid]);
|
|
265
|
+
if (other.scheduledAt > due || (other.scheduledAt === due && other.id >= id))
|
|
266
|
+
start = mid + 1;
|
|
267
|
+
else
|
|
268
|
+
high = mid;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
const index = values.indexOf(options.before);
|
|
273
|
+
if (index < 0)
|
|
274
|
+
throw new ScheduleValidationError('Invalid history cursor');
|
|
275
|
+
start = index + 1;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const last = this.occurrences.get(values[start + limit - 1]);
|
|
279
|
+
return { occurrences: values.slice(start, start + limit).map(id => this.getOccurrence(id)), ...(values.length > start + limit ? { next: `${last.scheduledAt}_${last.id}` } : {}) };
|
|
280
|
+
}
|
|
281
|
+
/** Durable admission claim. Call before provisioning or executable effects; never overwrite. */
|
|
282
|
+
claimOccurrence(value) {
|
|
283
|
+
const validated = occurrenceValidator.parse(value);
|
|
284
|
+
if (this.occurrences.has(value.id) || existsSync(this.occurrencePath(value.id)))
|
|
285
|
+
return false;
|
|
286
|
+
this.queueUnstartedCleanup(validated);
|
|
287
|
+
atomicWrite(this.occurrencePath(value.id), validated);
|
|
288
|
+
this.indexOccurrence(validated);
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
saveOccurrence(value) {
|
|
292
|
+
const validated = occurrenceValidator.parse(value);
|
|
293
|
+
const saved = this.readOccurrence(validated.id);
|
|
294
|
+
if (saved.scheduleId !== value.scheduleId || saved.activation !== value.activation || saved.scheduledAt !== value.scheduledAt || saved.revision !== value.revision)
|
|
295
|
+
throw new Error('Occurrence identity cannot change');
|
|
296
|
+
if (!isDeepStrictEqual(saved.settings, validated.settings))
|
|
297
|
+
throw new Error('Occurrence settings cannot change');
|
|
298
|
+
if (!isActiveOccurrence(saved) && !isDeepStrictEqual(saved, validated))
|
|
299
|
+
throw new Error('Terminal occurrence is immutable');
|
|
300
|
+
this.queueUnstartedCleanup(validated);
|
|
301
|
+
atomicWrite(this.occurrencePath(value.id), validated);
|
|
302
|
+
this.indexOccurrence(validated);
|
|
303
|
+
}
|
|
304
|
+
saveCleanupWarning(occurrence, warning) {
|
|
305
|
+
atomicWrite(join(this.root, 'cleanup', `${storageId.parse(occurrence.id)}.json`), { occurrenceId: occurrence.id, worktree: occurrence.worktree, warning });
|
|
306
|
+
}
|
|
307
|
+
clearCleanupWarning(id) {
|
|
308
|
+
const path = join(this.root, 'cleanup', `${storageId.parse(id)}.json`);
|
|
309
|
+
if (!existsSync(path))
|
|
310
|
+
return;
|
|
311
|
+
rmSync(path);
|
|
312
|
+
sync(dirname(path));
|
|
313
|
+
}
|
|
314
|
+
cleanupWarnings() {
|
|
315
|
+
return ids(join(this.root, 'cleanup')).flatMap(id => {
|
|
316
|
+
try {
|
|
317
|
+
return [JSON.parse(readFileSync(join(this.root, 'cleanup', `${id}.json`), 'utf8'))];
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
console.error('Workflow Occurrence cleanup warning is invalid. File retained for manual review.');
|
|
321
|
+
return [];
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
/** The callback owns execution cleanup; no Agent Session directories are ever removed here. */
|
|
326
|
+
prune(now, cleanup, canPrune = () => true) {
|
|
327
|
+
this.pruning ??= this.pruneExpired(now, cleanup, canPrune).finally(() => { this.pruning = undefined; });
|
|
328
|
+
return this.pruning;
|
|
329
|
+
}
|
|
330
|
+
async pruneExpired(now, cleanup, canPrune) {
|
|
331
|
+
timestamp.parse(now);
|
|
332
|
+
const removed = [];
|
|
333
|
+
for (const id of [...this.history]) {
|
|
334
|
+
if (!canPrune(id))
|
|
335
|
+
continue;
|
|
336
|
+
const indexed = this.occurrences.get(id);
|
|
337
|
+
if (!isActiveOccurrence(indexed) && (indexed.finishedAt === undefined || indexed.finishedAt > now - SCHEDULE_RETENTION_MS))
|
|
338
|
+
continue;
|
|
339
|
+
try {
|
|
340
|
+
const value = this.getOccurrence(id);
|
|
341
|
+
if (isActiveOccurrence(value) || value.finishedAt === undefined || value.finishedAt > now - SCHEDULE_RETENTION_MS)
|
|
342
|
+
continue;
|
|
343
|
+
if (this.active.has(value.id))
|
|
344
|
+
this.saveOccurrence(value);
|
|
345
|
+
await cleanup(value);
|
|
346
|
+
if (this.unstartedCleanup.has(id) && this.unstartedCleanup.get(id) !== 'unknown') {
|
|
347
|
+
rmSync(this.cleanupPath(id), { force: true });
|
|
348
|
+
sync(dirname(this.cleanupPath(id)));
|
|
349
|
+
this.unstartedCleanup.delete(id);
|
|
350
|
+
}
|
|
351
|
+
rmSync(this.occurrencePath(value.id));
|
|
352
|
+
sync(dirname(this.occurrencePath(value.id)));
|
|
353
|
+
this.occurrences.delete(value.id);
|
|
354
|
+
this.activeRecords.delete(value.id);
|
|
355
|
+
this.active.delete(value.id);
|
|
356
|
+
this.activeBySchedule.get(value.scheduleId)?.delete(value.id);
|
|
357
|
+
const history = this.historyBySchedule.get(value.scheduleId);
|
|
358
|
+
for (const list of [this.history, history])
|
|
359
|
+
list.splice(list.indexOf(value.id), 1);
|
|
360
|
+
if (!history.length) {
|
|
361
|
+
this.historyBySchedule.delete(value.scheduleId);
|
|
362
|
+
this.activeBySchedule.delete(value.scheduleId);
|
|
363
|
+
}
|
|
364
|
+
removed.push(value.id);
|
|
365
|
+
}
|
|
366
|
+
catch {
|
|
367
|
+
console.error('Workflow Occurrence retention failed. Its record and owned work will be retained for retry.');
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const retained = new Set(this.historyBySchedule.keys());
|
|
371
|
+
for (const schedule of this.listSchedules(true)) {
|
|
372
|
+
if (this.unknownOccurrences || schedule.deletedAt === undefined || retained.has(schedule.id) || schedule.deletedAt > now - SCHEDULE_RETENTION_MS)
|
|
373
|
+
continue;
|
|
374
|
+
try {
|
|
375
|
+
rmSync(this.schedulePath(schedule.id));
|
|
376
|
+
sync(dirname(this.schedulePath(schedule.id)));
|
|
377
|
+
this.schedules.delete(schedule.id);
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
console.error('Workflow Schedule retention failed. Its record will be retained for retry.');
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return removed;
|
|
384
|
+
}
|
|
385
|
+
}
|