dsh-taskbridge 1.7.1 → 1.8.1
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/CHANGELOG.md +24 -0
- package/LICENSE +21 -21
- package/README.md +733 -877
- package/THIRD_PARTY_NOTICES.txt +23 -23
- package/antigravity/agents/dsh-taskbridge.md +32 -0
- package/antigravity/skills/dsh-taskbridge/SKILL.md +51 -0
- package/codex/dsh-taskbridge.toml +181 -631
- package/cordis.patch.yml +6 -6
- package/docs/INTRODUCTION.md +122 -295
- package/docs/INTRODUCTION.zh.md +134 -279
- package/docs/antigravity-integration.md +42 -0
- package/docs/auth-contract.md +138 -138
- package/docs/claude-code.md +6 -11
- package/docs/codex-integration.md +2 -2
- package/docs/contracts-stage-b.md +443 -443
- package/docs/conversation-placement.md +7 -6
- package/docs/plugin-market.md +30 -30
- package/docs/redaction-contract.md +170 -170
- package/docs/releases/1.7.0.zh.md +11 -11
- package/docs/releases/1.8.0.zh.md +16 -0
- package/docs/releases/1.8.1.zh.md +24 -0
- package/docs/runtime-compatibility.md +1 -1
- package/docs/taskbridge-upgrade.md +28 -28
- package/docs/taskdispatch-upgrade.md +25 -25
- package/docs/workspace-contract.md +538 -538
- package/docs/workspace-grouping.md +126 -126
- package/icon.svg +1 -1
- package/lib/client.d.ts +804 -699
- package/lib/client.js +7 -7
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +150 -144
- package/src/antigravity/installer.js +1068 -0
- package/src/antigravity/seam.js +142 -0
- package/src/auth/fence.js +361 -361
- package/src/auth/grants.js +789 -789
- package/src/client/api.js +266 -3
- package/src/client/index.jsx +224 -11
- package/src/client/sidebar.jsx +862 -862
- package/src/client/styles.js +624 -624
- package/src/client/task-cleanup.js +429 -304
- package/src/client/task-records-table.jsx +217 -217
- package/src/client/task-view.js +307 -307
- package/src/client/workspace-groups.js +482 -482
- package/src/client/workspaces-browser.jsx +770 -770
- package/src/codex/agent-ownership.js +270 -270
- package/src/codex/installer.js +6 -0
- package/src/codex/toml.js +784 -784
- package/src/config/defaults.js +11 -15
- package/src/config/redact.js +245 -245
- package/src/config/store.js +186 -186
- package/src/credentials/seam.js +202 -202
- package/src/http/respond.js +6 -5
- package/src/http/user-plane.js +83 -1
- package/src/index.js +25 -0
- package/src/jobs/diagnostics.js +15 -15
- package/src/jobs/engine.js +1 -0
- package/src/jobs/history.js +1 -1
- package/src/jobs/plan-store.js +1103 -1020
- package/src/jobs/record-cleanup.js +452 -7
- package/src/jobs/review-note.js +63 -63
- package/src/jobs/task-descriptions.js +82 -82
- package/src/jobs/task-display.js +73 -73
- package/src/jobs/task-tree.js +786 -786
- package/src/jobs/title.js +134 -134
- package/src/mcp/continuation.js +317 -317
- package/src/mcp/server.js +34 -27
- package/src/mcp/wait.js +34 -15
- package/src/model/connectivity.js +371 -371
- package/src/model/discovery.js +582 -582
- package/src/model/native-catalog.js +68 -68
- package/src/model/openai-adapter.js +671 -671
- package/src/model/probe.js +264 -264
- package/src/model/providers.js +756 -756
- package/src/model/routing.js +321 -321
- package/src/runtime/agent-runner.js +1 -0
- package/src/runtime/permissions.js +1619 -1619
- package/src/runtime/question-call.js +162 -162
- package/src/runtime/questions.js +909 -909
- package/src/runtime/session-placement.js +36 -1
- package/src/runtime/worker-prompt.js +61 -55
- package/src/workspace/audit.js +775 -775
- package/src/workspace/preflight.js +424 -424
- package/src/workspace/worktrees.js +833 -679
package/src/jobs/plan-store.js
CHANGED
|
@@ -1,1020 +1,1103 @@
|
|
|
1
|
-
// Bounded durable planning-node store (`plan.json`).
|
|
2
|
-
//
|
|
3
|
-
// Planning nodes exist independently of execution records (`history.json`) so a
|
|
4
|
-
// root goal or intermediate breakdown stage can exist before any worker runs and
|
|
5
|
-
// is never evicted by the 200-entry execution history rotation.
|
|
6
|
-
|
|
7
|
-
import { randomBytes, randomUUID } from 'node:crypto';
|
|
8
|
-
import * as nodeFs from 'node:fs/promises';
|
|
9
|
-
import { dirname, isAbsolute, join } from 'node:path';
|
|
10
|
-
|
|
11
|
-
import { redactDeep, redactText } from '../config/redact.js';
|
|
12
|
-
import {
|
|
13
|
-
REVIEW_NOTE_MAX_CHARS,
|
|
14
|
-
normalizeReviewNote,
|
|
15
|
-
reviewNoteLength,
|
|
16
|
-
reviewNoteOverflow,
|
|
17
|
-
truncateCodePoints,
|
|
18
|
-
} from './review-note.js';
|
|
19
|
-
import {
|
|
20
|
-
TASK_DESCRIPTION_FIELDS,
|
|
21
|
-
TASK_DESCRIPTION_MAX_CHARS,
|
|
22
|
-
canonicalTaskDescription,
|
|
23
|
-
truncateTaskDescription,
|
|
24
|
-
} from './task-descriptions.js';
|
|
25
|
-
import {
|
|
26
|
-
isPathDigest,
|
|
27
|
-
pathDigest,
|
|
28
|
-
readPathIdentity,
|
|
29
|
-
recordedDirectoryIs,
|
|
30
|
-
recordedPathIsMasked,
|
|
31
|
-
} from './continuation-identity.js';
|
|
32
|
-
|
|
33
|
-
export const PLAN_SCHEMA_VERSION = 1;
|
|
34
|
-
export const PLAN_FILE = 'plan.json';
|
|
35
|
-
export const PLAN_TITLE_MAX_CHARS = 120;
|
|
36
|
-
export const MAX_PLAN_DEPTH = 16;
|
|
37
|
-
export const MAX_PLAN_BATCH = 32;
|
|
38
|
-
export const MAX_PLAN_REVIEW_HISTORY = 16;
|
|
39
|
-
|
|
40
|
-
const DEFAULT_LIMITS = Object.freeze({
|
|
41
|
-
maxNodes: 500,
|
|
42
|
-
maxBytes: 2 * 1024 * 1024,
|
|
43
|
-
maxNodeBytes: 16 * 1024,
|
|
44
|
-
maxDepth: MAX_PLAN_DEPTH,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const MIN_NODE_BYTES = 1024;
|
|
48
|
-
const MIN_DOCUMENT_BYTES = 64 * 1024;
|
|
49
|
-
const MAX_ID_CHARS = 256;
|
|
50
|
-
const MAX_KEY_CHARS = 64;
|
|
51
|
-
const MAX_PATH_CHARS = 4096;
|
|
52
|
-
const MAX_SAFE_TIMESTAMP = 8.64e15;
|
|
53
|
-
const WINDOWS_TRANSIENT_RENAME = new Set(['EACCES', 'EBUSY', 'EPERM']);
|
|
54
|
-
|
|
55
|
-
export const PLAN_CODES = Object.freeze({
|
|
56
|
-
unreadable: 'PLAN_UNREADABLE',
|
|
57
|
-
fileTooLarge: 'PLAN_FILE_TOO_LARGE',
|
|
58
|
-
malformed: 'PLAN_MALFORMED',
|
|
59
|
-
schemaUnsupported: 'PLAN_SCHEMA_UNSUPPORTED',
|
|
60
|
-
invalid: 'PLAN_INVALID',
|
|
61
|
-
titleInvalid: 'PLAN_TITLE_INVALID',
|
|
62
|
-
descriptionInvalid: 'PLAN_DESCRIPTION_INVALID',
|
|
63
|
-
nodeNotFound: 'PLAN_NODE_NOT_FOUND',
|
|
64
|
-
cycleDetected: 'PLAN_CYCLE_DETECTED',
|
|
65
|
-
depthExceeded: 'PLAN_DEPTH_EXCEEDED',
|
|
66
|
-
sourceMismatch: 'PLAN_SOURCE_MISMATCH',
|
|
67
|
-
limitExceeded: 'PLAN_LIMIT_EXCEEDED',
|
|
68
|
-
reviewInvalid: 'PLAN_REVIEW_INVALID',
|
|
69
|
-
reviewConflict: 'PLAN_REVIEW_CONFLICT',
|
|
70
|
-
writeFailed: 'PLAN_WRITE_FAILED',
|
|
71
|
-
disposed: 'PLAN_DISPOSED',
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
export class PlanError extends Error {
|
|
75
|
-
constructor(code, message, details = {}) {
|
|
76
|
-
super(message);
|
|
77
|
-
this.name = 'PlanError';
|
|
78
|
-
this.code = code;
|
|
79
|
-
this.details = details;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export class PlanStore {
|
|
84
|
-
constructor(dataDir, options = {}) {
|
|
85
|
-
if (typeof dataDir !== 'string' || dataDir.trim() === '') {
|
|
86
|
-
throw new TypeError('PlanStore needs a data directory');
|
|
87
|
-
}
|
|
88
|
-
const limits = { ...DEFAULT_LIMITS };
|
|
89
|
-
for (const key of Object.keys(DEFAULT_LIMITS)) {
|
|
90
|
-
const value = options[key];
|
|
91
|
-
if (value === undefined) continue;
|
|
92
|
-
if (!Number.isSafeInteger(value) || value <= 0) throw new TypeError(`${key} must be a positive integer`);
|
|
93
|
-
limits[key] = value;
|
|
94
|
-
}
|
|
95
|
-
if (limits.maxNodeBytes < MIN_NODE_BYTES) {
|
|
96
|
-
throw new TypeError(`maxNodeBytes must be at least ${MIN_NODE_BYTES}`);
|
|
97
|
-
}
|
|
98
|
-
if (limits.maxBytes < limits.maxNodeBytes) {
|
|
99
|
-
throw new TypeError('maxBytes must be at least maxNodeBytes');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
this.dir = dataDir;
|
|
103
|
-
this.path = join(dataDir, PLAN_FILE);
|
|
104
|
-
this.maxNodes = limits.maxNodes;
|
|
105
|
-
this.maxBytes = limits.maxBytes;
|
|
106
|
-
this.maxNodeBytes = limits.maxNodeBytes;
|
|
107
|
-
this.maxDepth = limits.maxDepth;
|
|
108
|
-
this.maxDocumentBytes = Math.max(limits.maxBytes * 2, MIN_DOCUMENT_BYTES);
|
|
109
|
-
this.fs = options.fs ?? nodeFs;
|
|
110
|
-
|
|
111
|
-
this.nodes = null;
|
|
112
|
-
this.lastSequence = 0;
|
|
113
|
-
this.loading = null;
|
|
114
|
-
this.refusal = null;
|
|
115
|
-
this.queue = Promise.resolve();
|
|
116
|
-
this.disposed = false;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async load(options = {}) {
|
|
120
|
-
const state = await this.ensureLoaded();
|
|
121
|
-
const filterConn = options?.connectionId;
|
|
122
|
-
const filtered = typeof filterConn === 'string' && filterConn !== ''
|
|
123
|
-
? state.nodes.filter((node) => node.connectionId === filterConn)
|
|
124
|
-
: state.nodes;
|
|
125
|
-
return filtered.map((node) => structuredClone(node));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Let cleanup validate relationships while queued plan mutations are held.
|
|
129
|
-
async withStableNodes(action) {
|
|
130
|
-
return await this.enqueue(async () => action(await this.load()));
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
async
|
|
144
|
-
if (this.disposed) throw this.disposedError();
|
|
145
|
-
this.throwIfRefused();
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
const
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
});
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
this.
|
|
637
|
-
this.
|
|
638
|
-
return { nodes: this.nodes, lastSequence: this.lastSequence };
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
if (
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
)
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
});
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1
|
+
// Bounded durable planning-node store (`plan.json`).
|
|
2
|
+
//
|
|
3
|
+
// Planning nodes exist independently of execution records (`history.json`) so a
|
|
4
|
+
// root goal or intermediate breakdown stage can exist before any worker runs and
|
|
5
|
+
// is never evicted by the 200-entry execution history rotation.
|
|
6
|
+
|
|
7
|
+
import { randomBytes, randomUUID } from 'node:crypto';
|
|
8
|
+
import * as nodeFs from 'node:fs/promises';
|
|
9
|
+
import { dirname, isAbsolute, join } from 'node:path';
|
|
10
|
+
|
|
11
|
+
import { redactDeep, redactText } from '../config/redact.js';
|
|
12
|
+
import {
|
|
13
|
+
REVIEW_NOTE_MAX_CHARS,
|
|
14
|
+
normalizeReviewNote,
|
|
15
|
+
reviewNoteLength,
|
|
16
|
+
reviewNoteOverflow,
|
|
17
|
+
truncateCodePoints,
|
|
18
|
+
} from './review-note.js';
|
|
19
|
+
import {
|
|
20
|
+
TASK_DESCRIPTION_FIELDS,
|
|
21
|
+
TASK_DESCRIPTION_MAX_CHARS,
|
|
22
|
+
canonicalTaskDescription,
|
|
23
|
+
truncateTaskDescription,
|
|
24
|
+
} from './task-descriptions.js';
|
|
25
|
+
import {
|
|
26
|
+
isPathDigest,
|
|
27
|
+
pathDigest,
|
|
28
|
+
readPathIdentity,
|
|
29
|
+
recordedDirectoryIs,
|
|
30
|
+
recordedPathIsMasked,
|
|
31
|
+
} from './continuation-identity.js';
|
|
32
|
+
|
|
33
|
+
export const PLAN_SCHEMA_VERSION = 1;
|
|
34
|
+
export const PLAN_FILE = 'plan.json';
|
|
35
|
+
export const PLAN_TITLE_MAX_CHARS = 120;
|
|
36
|
+
export const MAX_PLAN_DEPTH = 16;
|
|
37
|
+
export const MAX_PLAN_BATCH = 32;
|
|
38
|
+
export const MAX_PLAN_REVIEW_HISTORY = 16;
|
|
39
|
+
|
|
40
|
+
const DEFAULT_LIMITS = Object.freeze({
|
|
41
|
+
maxNodes: 500,
|
|
42
|
+
maxBytes: 2 * 1024 * 1024,
|
|
43
|
+
maxNodeBytes: 16 * 1024,
|
|
44
|
+
maxDepth: MAX_PLAN_DEPTH,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const MIN_NODE_BYTES = 1024;
|
|
48
|
+
const MIN_DOCUMENT_BYTES = 64 * 1024;
|
|
49
|
+
const MAX_ID_CHARS = 256;
|
|
50
|
+
const MAX_KEY_CHARS = 64;
|
|
51
|
+
const MAX_PATH_CHARS = 4096;
|
|
52
|
+
const MAX_SAFE_TIMESTAMP = 8.64e15;
|
|
53
|
+
const WINDOWS_TRANSIENT_RENAME = new Set(['EACCES', 'EBUSY', 'EPERM']);
|
|
54
|
+
|
|
55
|
+
export const PLAN_CODES = Object.freeze({
|
|
56
|
+
unreadable: 'PLAN_UNREADABLE',
|
|
57
|
+
fileTooLarge: 'PLAN_FILE_TOO_LARGE',
|
|
58
|
+
malformed: 'PLAN_MALFORMED',
|
|
59
|
+
schemaUnsupported: 'PLAN_SCHEMA_UNSUPPORTED',
|
|
60
|
+
invalid: 'PLAN_INVALID',
|
|
61
|
+
titleInvalid: 'PLAN_TITLE_INVALID',
|
|
62
|
+
descriptionInvalid: 'PLAN_DESCRIPTION_INVALID',
|
|
63
|
+
nodeNotFound: 'PLAN_NODE_NOT_FOUND',
|
|
64
|
+
cycleDetected: 'PLAN_CYCLE_DETECTED',
|
|
65
|
+
depthExceeded: 'PLAN_DEPTH_EXCEEDED',
|
|
66
|
+
sourceMismatch: 'PLAN_SOURCE_MISMATCH',
|
|
67
|
+
limitExceeded: 'PLAN_LIMIT_EXCEEDED',
|
|
68
|
+
reviewInvalid: 'PLAN_REVIEW_INVALID',
|
|
69
|
+
reviewConflict: 'PLAN_REVIEW_CONFLICT',
|
|
70
|
+
writeFailed: 'PLAN_WRITE_FAILED',
|
|
71
|
+
disposed: 'PLAN_DISPOSED',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export class PlanError extends Error {
|
|
75
|
+
constructor(code, message, details = {}) {
|
|
76
|
+
super(message);
|
|
77
|
+
this.name = 'PlanError';
|
|
78
|
+
this.code = code;
|
|
79
|
+
this.details = details;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export class PlanStore {
|
|
84
|
+
constructor(dataDir, options = {}) {
|
|
85
|
+
if (typeof dataDir !== 'string' || dataDir.trim() === '') {
|
|
86
|
+
throw new TypeError('PlanStore needs a data directory');
|
|
87
|
+
}
|
|
88
|
+
const limits = { ...DEFAULT_LIMITS };
|
|
89
|
+
for (const key of Object.keys(DEFAULT_LIMITS)) {
|
|
90
|
+
const value = options[key];
|
|
91
|
+
if (value === undefined) continue;
|
|
92
|
+
if (!Number.isSafeInteger(value) || value <= 0) throw new TypeError(`${key} must be a positive integer`);
|
|
93
|
+
limits[key] = value;
|
|
94
|
+
}
|
|
95
|
+
if (limits.maxNodeBytes < MIN_NODE_BYTES) {
|
|
96
|
+
throw new TypeError(`maxNodeBytes must be at least ${MIN_NODE_BYTES}`);
|
|
97
|
+
}
|
|
98
|
+
if (limits.maxBytes < limits.maxNodeBytes) {
|
|
99
|
+
throw new TypeError('maxBytes must be at least maxNodeBytes');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
this.dir = dataDir;
|
|
103
|
+
this.path = join(dataDir, PLAN_FILE);
|
|
104
|
+
this.maxNodes = limits.maxNodes;
|
|
105
|
+
this.maxBytes = limits.maxBytes;
|
|
106
|
+
this.maxNodeBytes = limits.maxNodeBytes;
|
|
107
|
+
this.maxDepth = limits.maxDepth;
|
|
108
|
+
this.maxDocumentBytes = Math.max(limits.maxBytes * 2, MIN_DOCUMENT_BYTES);
|
|
109
|
+
this.fs = options.fs ?? nodeFs;
|
|
110
|
+
|
|
111
|
+
this.nodes = null;
|
|
112
|
+
this.lastSequence = 0;
|
|
113
|
+
this.loading = null;
|
|
114
|
+
this.refusal = null;
|
|
115
|
+
this.queue = Promise.resolve();
|
|
116
|
+
this.disposed = false;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async load(options = {}) {
|
|
120
|
+
const state = await this.ensureLoaded();
|
|
121
|
+
const filterConn = options?.connectionId;
|
|
122
|
+
const filtered = typeof filterConn === 'string' && filterConn !== ''
|
|
123
|
+
? state.nodes.filter((node) => node.connectionId === filterConn)
|
|
124
|
+
: state.nodes;
|
|
125
|
+
return filtered.map((node) => structuredClone(node));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Let cleanup validate relationships while queued plan mutations are held.
|
|
129
|
+
async withStableNodes(action) {
|
|
130
|
+
return await this.enqueue(async () => action(await this.load()));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Remove planning nodes in `seedNodeIds` (and their ancestors) that no longer have
|
|
135
|
+
* any remaining execution tasks or child plan nodes after a task cleanup.
|
|
136
|
+
*
|
|
137
|
+
* Never removes an unrelated empty planning node that was not in the cleaned tasks'
|
|
138
|
+
* own plan ancestry.
|
|
139
|
+
* @param {Iterable<string>} seedNodeIds - plan node ids referenced by the deleted tasks.
|
|
140
|
+
* @param {object[]} remainingTasks - all remaining live and archived task records.
|
|
141
|
+
* @returns {Promise<string[]>} removed plan node ids.
|
|
142
|
+
*/
|
|
143
|
+
async deleteEmptyAncestors(seedNodeIds, remainingTasks = []) {
|
|
144
|
+
if (this.disposed) throw this.disposedError();
|
|
145
|
+
this.throwIfRefused();
|
|
146
|
+
const seeds = new Set();
|
|
147
|
+
for (const id of Array.isArray(seedNodeIds) || seedNodeIds instanceof Set ? seedNodeIds : []) {
|
|
148
|
+
if (typeof id === 'string' && id.trim() !== '') seeds.add(id.trim());
|
|
149
|
+
}
|
|
150
|
+
if (seeds.size === 0) return [];
|
|
151
|
+
|
|
152
|
+
return await this.enqueue(async () => {
|
|
153
|
+
this.throwIfRefused();
|
|
154
|
+
const state = await this.ensureLoaded();
|
|
155
|
+
const byId = new Map(state.nodes.map((node) => [node.nodeId, node]));
|
|
156
|
+
|
|
157
|
+
// Collect the candidate scope: seed nodes and their plan ancestors.
|
|
158
|
+
const candidateScope = new Set();
|
|
159
|
+
for (const seedId of seeds) {
|
|
160
|
+
let cur = seedId;
|
|
161
|
+
const seen = new Set();
|
|
162
|
+
while (typeof cur === 'string' && cur !== '' && !seen.has(cur)) {
|
|
163
|
+
seen.add(cur);
|
|
164
|
+
const node = byId.get(cur);
|
|
165
|
+
if (!node) break;
|
|
166
|
+
candidateScope.add(node.nodeId);
|
|
167
|
+
cur = node.parentNodeId ?? null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (candidateScope.size === 0) return [];
|
|
171
|
+
|
|
172
|
+
const referencedByTasks = new Set();
|
|
173
|
+
for (const task of Array.isArray(remainingTasks) ? remainingTasks : []) {
|
|
174
|
+
if (typeof task?.planNodeId === 'string' && task.planNodeId.trim() !== '') {
|
|
175
|
+
referencedByTasks.add(task.planNodeId.trim());
|
|
176
|
+
}
|
|
177
|
+
if (typeof task?.parentNodeId === 'string' && task.parentNodeId.trim() !== '') {
|
|
178
|
+
referencedByTasks.add(task.parentNodeId.trim());
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let draft = state.nodes.map((node) => structuredClone(node));
|
|
183
|
+
const removedIds = [];
|
|
184
|
+
let changed = true;
|
|
185
|
+
while (changed) {
|
|
186
|
+
changed = false;
|
|
187
|
+
const parentIdsWithPlanChildren = new Set(
|
|
188
|
+
draft
|
|
189
|
+
.map((node) => node.parentNodeId)
|
|
190
|
+
.filter((id) => typeof id === 'string' && id !== ''),
|
|
191
|
+
);
|
|
192
|
+
const next = [];
|
|
193
|
+
for (const node of draft) {
|
|
194
|
+
if (
|
|
195
|
+
candidateScope.has(node.nodeId)
|
|
196
|
+
&& !referencedByTasks.has(node.nodeId)
|
|
197
|
+
&& !parentIdsWithPlanChildren.has(node.nodeId)
|
|
198
|
+
) {
|
|
199
|
+
removedIds.push(node.nodeId);
|
|
200
|
+
changed = true;
|
|
201
|
+
} else {
|
|
202
|
+
next.push(node);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
draft = next;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (removedIds.length === 0) return [];
|
|
209
|
+
const now = Date.now();
|
|
210
|
+
await this.writeDocument(draft, state.lastSequence, now);
|
|
211
|
+
this.nodes = draft;
|
|
212
|
+
return removedIds;
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async get(nodeId, connectionId) {
|
|
217
|
+
if (typeof nodeId !== 'string' || nodeId.trim() === '') return null;
|
|
218
|
+
if (typeof connectionId !== 'string' || connectionId.trim() === '') return null;
|
|
219
|
+
const state = await this.ensureLoaded();
|
|
220
|
+
const target = nodeId.trim();
|
|
221
|
+
const found = state.nodes.find((node) => node.nodeId === target) ?? null;
|
|
222
|
+
if (found === null || found.connectionId !== connectionId) return null;
|
|
223
|
+
return structuredClone(found);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async create(request, options = {}) {
|
|
227
|
+
if (this.disposed) throw this.disposedError();
|
|
228
|
+
this.throwIfRefused();
|
|
229
|
+
if (!isPlainObject(request)) {
|
|
230
|
+
throw new PlanError(PLAN_CODES.invalid, 'plan creation requires an object request', {});
|
|
231
|
+
}
|
|
232
|
+
const connectionId = requireConnectionId(request.connectionId);
|
|
233
|
+
const isBatch = request.nodes !== undefined;
|
|
234
|
+
const rawItems = isBatch ? validateBatchArray(request.nodes) : [singleItemFromRequest(request)];
|
|
235
|
+
const defaultWorkspacePath = request.workspacePath;
|
|
236
|
+
const defaultParentNodeId = request.parentNodeId;
|
|
237
|
+
|
|
238
|
+
if (defaultWorkspacePath !== undefined) validateWorkspaceInput(defaultWorkspacePath, 'workspacePath');
|
|
239
|
+
if (defaultParentNodeId !== undefined && defaultParentNodeId !== null) {
|
|
240
|
+
validateParentRefInput(defaultParentNodeId, 'parentNodeId');
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return await this.enqueue(async () => {
|
|
244
|
+
this.throwIfRefused();
|
|
245
|
+
const state = await this.ensureLoaded();
|
|
246
|
+
const draft = state.nodes.map((node) => structuredClone(node));
|
|
247
|
+
const existingById = new Map(draft.map((node) => [node.nodeId, node]));
|
|
248
|
+
|
|
249
|
+
// Validate batch keys and local parentKey graph before minting records.
|
|
250
|
+
const batchByKey = new Map();
|
|
251
|
+
for (let i = 0; i < rawItems.length; i += 1) {
|
|
252
|
+
const item = rawItems[i];
|
|
253
|
+
if (!isPlainObject(item)) {
|
|
254
|
+
throw new PlanError(PLAN_CODES.invalid, `nodes[${i}] must be an object`, { index: i });
|
|
255
|
+
}
|
|
256
|
+
if (item.key !== undefined) {
|
|
257
|
+
if (typeof item.key !== 'string' || item.key.trim() === '' || item.key.trim().length > MAX_KEY_CHARS) {
|
|
258
|
+
throw new PlanError(PLAN_CODES.invalid, `nodes[${i}].key must be a non-empty string of at most ${MAX_KEY_CHARS} characters`, {
|
|
259
|
+
index: i,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
const key = item.key.trim();
|
|
263
|
+
if (batchByKey.has(key)) {
|
|
264
|
+
throw new PlanError(PLAN_CODES.invalid, `duplicate batch node key ${JSON.stringify(key)}`, { key, index: i });
|
|
265
|
+
}
|
|
266
|
+
batchByKey.set(key, { index: i, item });
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Check local parentKey references & cycles across the batch.
|
|
271
|
+
const keyParent = new Map();
|
|
272
|
+
for (let i = 0; i < rawItems.length; i += 1) {
|
|
273
|
+
const item = rawItems[i];
|
|
274
|
+
const hasParentKey = item.parentKey !== undefined && item.parentKey !== null && item.parentKey !== '';
|
|
275
|
+
const hasParentId = item.parentNodeId !== undefined && item.parentNodeId !== null && item.parentNodeId !== '';
|
|
276
|
+
if (hasParentKey && hasParentId) {
|
|
277
|
+
throw new PlanError(PLAN_CODES.invalid, `nodes[${i}] cannot specify both parentKey and parentNodeId`, { index: i });
|
|
278
|
+
}
|
|
279
|
+
if (item.parentKey !== undefined && item.parentKey !== null) {
|
|
280
|
+
if (typeof item.parentKey !== 'string' || item.parentKey.trim() === '') {
|
|
281
|
+
throw new PlanError(PLAN_CODES.invalid, `nodes[${i}].parentKey must be a non-empty string`, { index: i });
|
|
282
|
+
}
|
|
283
|
+
const pk = item.parentKey.trim();
|
|
284
|
+
if (!batchByKey.has(pk)) {
|
|
285
|
+
throw new PlanError(PLAN_CODES.invalid, `nodes[${i}].parentKey references unknown batch key ${JSON.stringify(pk)}`, {
|
|
286
|
+
index: i,
|
|
287
|
+
parentKey: pk,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
if (typeof item.key === 'string' && item.key.trim() === pk) {
|
|
291
|
+
throw new PlanError(PLAN_CODES.cycleDetected, 'a plan node cannot be its own parent', { key: pk });
|
|
292
|
+
}
|
|
293
|
+
if (typeof item.key === 'string') keyParent.set(item.key.trim(), pk);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
for (const startKey of keyParent.keys()) {
|
|
297
|
+
const seen = new Set([startKey]);
|
|
298
|
+
let cur = keyParent.get(startKey) ?? null;
|
|
299
|
+
while (cur !== null) {
|
|
300
|
+
if (seen.has(cur)) {
|
|
301
|
+
throw new PlanError(PLAN_CODES.cycleDetected, 'batch plan nodes contain a parentKey cycle', { key: startKey });
|
|
302
|
+
}
|
|
303
|
+
seen.add(cur);
|
|
304
|
+
cur = keyParent.get(cur) ?? null;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Topological order so parents in the batch are resolved before their children.
|
|
309
|
+
const order = topologicalBatchOrder(rawItems, batchByKey);
|
|
310
|
+
const mintedByIndex = new Array(rawItems.length);
|
|
311
|
+
const mintedByKey = new Map();
|
|
312
|
+
let seq = state.lastSequence;
|
|
313
|
+
const now = Date.now();
|
|
314
|
+
|
|
315
|
+
for (const index of order) {
|
|
316
|
+
const item = rawItems[index];
|
|
317
|
+
const title = canonicalPlanTitle(item.title);
|
|
318
|
+
const goal = canonicalPlanDescription(item.goalDescription, TASK_DESCRIPTION_FIELDS.goal);
|
|
319
|
+
|
|
320
|
+
let resolvedParentId = null;
|
|
321
|
+
let parentSource = null;
|
|
322
|
+
let parentDepth = 0;
|
|
323
|
+
|
|
324
|
+
if (typeof item.parentKey === 'string' && item.parentKey.trim() !== '') {
|
|
325
|
+
const parentMinted = mintedByKey.get(item.parentKey.trim());
|
|
326
|
+
resolvedParentId = parentMinted.node.nodeId;
|
|
327
|
+
parentSource = parentMinted.rawSource;
|
|
328
|
+
parentDepth = parentMinted.depth;
|
|
329
|
+
} else {
|
|
330
|
+
const rawParentId = item.parentNodeId !== undefined ? item.parentNodeId : (isBatch ? defaultParentNodeId : request.parentNodeId);
|
|
331
|
+
if (rawParentId !== undefined && rawParentId !== null) {
|
|
332
|
+
const parentId = validateParentRefInput(rawParentId, 'parentNodeId');
|
|
333
|
+
const resolved = await this.resolveParentFacts(parentId, connectionId, existingById, options.resolveExternalParent);
|
|
334
|
+
resolvedParentId = parentId;
|
|
335
|
+
parentSource = resolved.source;
|
|
336
|
+
parentDepth = resolved.depth;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const depth = parentDepth + 1;
|
|
341
|
+
if (depth > this.maxDepth) {
|
|
342
|
+
throw new PlanError(PLAN_CODES.depthExceeded, `plan tree depth ${depth} exceeds the maximum of ${this.maxDepth}`, {
|
|
343
|
+
depth,
|
|
344
|
+
maxDepth: this.maxDepth,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const rawWs = item.workspacePath !== undefined ? item.workspacePath : defaultWorkspacePath;
|
|
349
|
+
const rawSource = resolveItemSource(rawWs, parentSource);
|
|
350
|
+
|
|
351
|
+
seq += 1;
|
|
352
|
+
const nodeId = `plan-${randomUUID()}`;
|
|
353
|
+
const node = projectPlanNode({
|
|
354
|
+
nodeId,
|
|
355
|
+
parentNodeId: resolvedParentId,
|
|
356
|
+
title,
|
|
357
|
+
goalDescription: goal,
|
|
358
|
+
sourcePath: rawSource.path === null ? null : redactText(rawSource.path),
|
|
359
|
+
sourcePathDigest: rawSource.digest,
|
|
360
|
+
connectionId,
|
|
361
|
+
sequence: seq,
|
|
362
|
+
createdAt: now,
|
|
363
|
+
updatedAt: now,
|
|
364
|
+
hostAcceptance: null,
|
|
365
|
+
reviewHistory: [],
|
|
366
|
+
}, this.maxNodeBytes);
|
|
367
|
+
|
|
368
|
+
mintedByIndex[index] = node;
|
|
369
|
+
if (typeof item.key === 'string' && item.key.trim() !== '') {
|
|
370
|
+
mintedByKey.set(item.key.trim(), { node, rawSource, depth });
|
|
371
|
+
}
|
|
372
|
+
draft.push(node);
|
|
373
|
+
existingById.set(nodeId, node);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
this.enforceDocumentLimits(draft, seq, now);
|
|
377
|
+
await this.writeDocument(draft, seq, now);
|
|
378
|
+
this.nodes = draft;
|
|
379
|
+
this.lastSequence = seq;
|
|
380
|
+
|
|
381
|
+
const created = mintedByIndex.map((node) => structuredClone(node));
|
|
382
|
+
return isBatch ? { nodes: created } : { node: created[0], nodes: created };
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
async update(request) {
|
|
387
|
+
if (this.disposed) throw this.disposedError();
|
|
388
|
+
this.throwIfRefused();
|
|
389
|
+
if (!isPlainObject(request)) {
|
|
390
|
+
throw new PlanError(PLAN_CODES.invalid, 'plan update requires an object request', {});
|
|
391
|
+
}
|
|
392
|
+
const connectionId = requireConnectionId(request.connectionId);
|
|
393
|
+
const nodeId = requireNodeId(request.nodeId);
|
|
394
|
+
if (request.parentNodeId !== undefined) {
|
|
395
|
+
throw new PlanError(PLAN_CODES.invalid, 'parentNodeId cannot be changed after a plan node is created', { nodeId });
|
|
396
|
+
}
|
|
397
|
+
const hasTitle = request.title !== undefined;
|
|
398
|
+
const hasGoal = request.goalDescription !== undefined;
|
|
399
|
+
const hasWs = request.workspacePath !== undefined;
|
|
400
|
+
if (!hasTitle && !hasGoal && !hasWs) {
|
|
401
|
+
throw new PlanError(PLAN_CODES.invalid, 'plan update requires at least one of title, goalDescription or workspacePath', {
|
|
402
|
+
nodeId,
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
const nextTitle = hasTitle ? canonicalPlanTitle(request.title) : undefined;
|
|
406
|
+
const nextGoal = hasGoal ? canonicalPlanDescription(request.goalDescription, TASK_DESCRIPTION_FIELDS.goal) : undefined;
|
|
407
|
+
if (hasGoal && nextGoal === null) {
|
|
408
|
+
throw new PlanError(PLAN_CODES.descriptionInvalid, 'goalDescription cannot be erased with an empty string', {
|
|
409
|
+
nodeId,
|
|
410
|
+
field: TASK_DESCRIPTION_FIELDS.goal,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
const wsFacts = hasWs ? validateWorkspaceInput(request.workspacePath, 'workspacePath') : null;
|
|
414
|
+
|
|
415
|
+
return await this.enqueue(async () => {
|
|
416
|
+
this.throwIfRefused();
|
|
417
|
+
const state = await this.ensureLoaded();
|
|
418
|
+
const draft = state.nodes.map((node) => structuredClone(node));
|
|
419
|
+
const index = draft.findIndex((node) => node.nodeId === nodeId);
|
|
420
|
+
if (index < 0 || draft[index].connectionId !== connectionId) {
|
|
421
|
+
throw absentNodeError(nodeId);
|
|
422
|
+
}
|
|
423
|
+
const current = draft[index];
|
|
424
|
+
if (current.hostAcceptance?.accepted === true) {
|
|
425
|
+
throw new PlanError(PLAN_CODES.reviewConflict, 'an accepted plan node is sealed and cannot be modified', {
|
|
426
|
+
nodeId,
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
let sourcePath = current.sourcePath;
|
|
431
|
+
let sourcePathDigest = current.sourcePathDigest;
|
|
432
|
+
if (wsFacts !== null) {
|
|
433
|
+
if (current.sourcePath !== null || current.sourcePathDigest !== null) {
|
|
434
|
+
if (!recordedDirectoryIs({ path: current.sourcePath, digest: current.sourcePathDigest }, wsFacts.path)) {
|
|
435
|
+
throw new PlanError(PLAN_CODES.sourceMismatch, 'a plan node cannot switch to a different source repository', {
|
|
436
|
+
nodeId,
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (current.parentNodeId !== null) {
|
|
441
|
+
const parent = draft.find((n) => n.nodeId === current.parentNodeId && n.connectionId === connectionId) ?? null;
|
|
442
|
+
if (parent !== null && (parent.sourcePath !== null || parent.sourcePathDigest !== null)) {
|
|
443
|
+
if (!recordedDirectoryIs({ path: parent.sourcePath, digest: parent.sourcePathDigest }, wsFacts.path)) {
|
|
444
|
+
throw new PlanError(PLAN_CODES.sourceMismatch, 'plan node workspacePath does not match its parent plan node', {
|
|
445
|
+
nodeId,
|
|
446
|
+
parentNodeId: current.parentNodeId,
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
for (const child of draft) {
|
|
452
|
+
if (child.connectionId === connectionId && child.parentNodeId === nodeId) {
|
|
453
|
+
if (child.sourcePath !== null || child.sourcePathDigest !== null) {
|
|
454
|
+
if (!recordedDirectoryIs({ path: child.sourcePath, digest: child.sourcePathDigest }, wsFacts.path)) {
|
|
455
|
+
throw new PlanError(PLAN_CODES.sourceMismatch, 'plan node workspacePath conflicts with an existing child node', {
|
|
456
|
+
nodeId,
|
|
457
|
+
childNodeId: child.nodeId,
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
sourcePath = redactText(wsFacts.path);
|
|
464
|
+
sourcePathDigest = wsFacts.digest;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const now = Date.now();
|
|
468
|
+
const updated = projectPlanNode({
|
|
469
|
+
...current,
|
|
470
|
+
title: nextTitle ?? current.title,
|
|
471
|
+
goalDescription: nextGoal !== undefined ? nextGoal : current.goalDescription,
|
|
472
|
+
sourcePath,
|
|
473
|
+
sourcePathDigest,
|
|
474
|
+
updatedAt: now,
|
|
475
|
+
}, this.maxNodeBytes);
|
|
476
|
+
|
|
477
|
+
draft[index] = updated;
|
|
478
|
+
this.enforceDocumentLimits(draft, state.lastSequence, now);
|
|
479
|
+
await this.writeDocument(draft, state.lastSequence, now);
|
|
480
|
+
this.nodes = draft;
|
|
481
|
+
return structuredClone(updated);
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
async review(request) {
|
|
486
|
+
if (this.disposed) throw this.disposedError();
|
|
487
|
+
this.throwIfRefused();
|
|
488
|
+
if (!isPlainObject(request)) {
|
|
489
|
+
throw new PlanError(PLAN_CODES.reviewInvalid, 'plan review requires an object request', {});
|
|
490
|
+
}
|
|
491
|
+
const connectionId = requireConnectionId(request.connectionId);
|
|
492
|
+
const nodeId = requireNodeId(request.nodeId);
|
|
493
|
+
const decision = request.decision;
|
|
494
|
+
if (decision !== 'accepted' && decision !== 'changes-requested') {
|
|
495
|
+
throw new PlanError(PLAN_CODES.reviewInvalid, 'decision must be "accepted" or "changes-requested"', { nodeId });
|
|
496
|
+
}
|
|
497
|
+
if (typeof request.note !== 'string') {
|
|
498
|
+
throw new PlanError(PLAN_CODES.reviewInvalid, 'a non-empty review note string is required', { nodeId });
|
|
499
|
+
}
|
|
500
|
+
const normalizedNote = normalizeReviewNote(request.note);
|
|
501
|
+
if (normalizedNote === '') {
|
|
502
|
+
throw new PlanError(PLAN_CODES.reviewInvalid, 'a non-empty review note is required', { nodeId });
|
|
503
|
+
}
|
|
504
|
+
if (reviewNoteLength(normalizedNote) > REVIEW_NOTE_MAX_CHARS) {
|
|
505
|
+
const preOverflow = reviewNoteOverflow(normalizedNote);
|
|
506
|
+
throw new PlanError(PLAN_CODES.reviewInvalid, preOverflow.message, {
|
|
507
|
+
nodeId,
|
|
508
|
+
limit: preOverflow.limit,
|
|
509
|
+
length: preOverflow.length,
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
const redactedNote = normalizeReviewNote(redactText(normalizedNote));
|
|
513
|
+
if (redactedNote === '') {
|
|
514
|
+
throw new PlanError(PLAN_CODES.reviewInvalid, 'the review note is empty after redaction', { nodeId });
|
|
515
|
+
}
|
|
516
|
+
if (reviewNoteLength(redactedNote) > REVIEW_NOTE_MAX_CHARS) {
|
|
517
|
+
const postOverflow = reviewNoteOverflow(redactedNote);
|
|
518
|
+
throw new PlanError(PLAN_CODES.reviewInvalid, postOverflow.message, {
|
|
519
|
+
nodeId,
|
|
520
|
+
limit: postOverflow.limit,
|
|
521
|
+
length: postOverflow.length,
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
const summary = canonicalPlanDescription(request.completionSummary, TASK_DESCRIPTION_FIELDS.summary, {
|
|
525
|
+
code: PLAN_CODES.reviewInvalid,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
return await this.enqueue(async () => {
|
|
529
|
+
this.throwIfRefused();
|
|
530
|
+
const state = await this.ensureLoaded();
|
|
531
|
+
const draft = state.nodes.map((node) => structuredClone(node));
|
|
532
|
+
const index = draft.findIndex((node) => node.nodeId === nodeId);
|
|
533
|
+
if (index < 0 || draft[index].connectionId !== connectionId) {
|
|
534
|
+
throw absentNodeError(nodeId);
|
|
535
|
+
}
|
|
536
|
+
const current = draft[index];
|
|
537
|
+
const existing = current.hostAcceptance;
|
|
538
|
+
if (existing !== null && existing !== undefined) {
|
|
539
|
+
const existingState = existing.state ?? existing.decision ?? null;
|
|
540
|
+
const existingSummary = existing.completionSummary ?? null;
|
|
541
|
+
if (existingState === decision && existing.note === redactedNote && existingSummary === summary) {
|
|
542
|
+
return { node: structuredClone(current), replayed: true };
|
|
543
|
+
}
|
|
544
|
+
if (existing.accepted === true) {
|
|
545
|
+
throw new PlanError(PLAN_CODES.reviewConflict, 'an accepted plan review is final and cannot be replaced', {
|
|
546
|
+
nodeId,
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const now = Date.now();
|
|
552
|
+
const nextHistory = Array.isArray(current.reviewHistory) ? [...current.reviewHistory] : [];
|
|
553
|
+
if (existing !== null && existing !== undefined) {
|
|
554
|
+
nextHistory.push(structuredClone(existing));
|
|
555
|
+
if (nextHistory.length > MAX_PLAN_REVIEW_HISTORY) {
|
|
556
|
+
nextHistory.splice(0, nextHistory.length - MAX_PLAN_REVIEW_HISTORY);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const nextAcceptance = {
|
|
561
|
+
accepted: decision === 'accepted',
|
|
562
|
+
state: decision,
|
|
563
|
+
decision,
|
|
564
|
+
acceptedAt: now,
|
|
565
|
+
by: 'commander',
|
|
566
|
+
note: redactedNote,
|
|
567
|
+
noteChars: reviewNoteLength(redactedNote),
|
|
568
|
+
...(summary === null ? {} : { completionSummary: summary }),
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
const updated = projectPlanNode({
|
|
572
|
+
...current,
|
|
573
|
+
updatedAt: now,
|
|
574
|
+
hostAcceptance: nextAcceptance,
|
|
575
|
+
reviewHistory: nextHistory,
|
|
576
|
+
}, this.maxNodeBytes);
|
|
577
|
+
|
|
578
|
+
draft[index] = updated;
|
|
579
|
+
this.enforceDocumentLimits(draft, state.lastSequence, now);
|
|
580
|
+
await this.writeDocument(draft, state.lastSequence, now);
|
|
581
|
+
this.nodes = draft;
|
|
582
|
+
return { node: structuredClone(updated), replayed: false };
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
async resolveParentFacts(parentId, connectionId, existingById, resolveExternalParent) {
|
|
587
|
+
const planParent = existingById.get(parentId) ?? null;
|
|
588
|
+
if (planParent !== null) {
|
|
589
|
+
if (planParent.connectionId !== connectionId) throw absentNodeError(parentId);
|
|
590
|
+
const depth = computeExistingDepth(parentId, existingById, connectionId);
|
|
591
|
+
return {
|
|
592
|
+
kind: 'plan',
|
|
593
|
+
depth,
|
|
594
|
+
source: {
|
|
595
|
+
path: planParent.sourcePath,
|
|
596
|
+
digest: planParent.sourcePathDigest,
|
|
597
|
+
},
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
if (typeof resolveExternalParent === 'function') {
|
|
601
|
+
const external = await resolveExternalParent(parentId, connectionId, existingById);
|
|
602
|
+
if (external !== null && external !== undefined && external.found === true) {
|
|
603
|
+
return {
|
|
604
|
+
kind: 'task',
|
|
605
|
+
depth: Number.isInteger(external.depth) && external.depth > 0 ? external.depth : 1,
|
|
606
|
+
source: {
|
|
607
|
+
path: external.sourcePath ?? null,
|
|
608
|
+
digest: external.sourcePathDigest ?? null,
|
|
609
|
+
},
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
throw absentNodeError(parentId);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
async flush() {
|
|
617
|
+
try {
|
|
618
|
+
await this.queue;
|
|
619
|
+
} catch {
|
|
620
|
+
// individual caller already observed its own rejection
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
async dispose() {
|
|
625
|
+
this.disposed = true;
|
|
626
|
+
await this.flush();
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
enqueue(fn) {
|
|
630
|
+
const next = this.queue.then(fn, fn);
|
|
631
|
+
this.queue = next.catch(() => {});
|
|
632
|
+
return next;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
async ensureLoaded() {
|
|
636
|
+
if (this.disposed) throw this.disposedError();
|
|
637
|
+
this.throwIfRefused();
|
|
638
|
+
if (this.nodes !== null) return { nodes: this.nodes, lastSequence: this.lastSequence };
|
|
639
|
+
if (this.loading === null) {
|
|
640
|
+
this.loading = this.readFromDisk().finally(() => {
|
|
641
|
+
this.loading = null;
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
return await this.loading;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
async readFromDisk() {
|
|
648
|
+
let stat;
|
|
649
|
+
try {
|
|
650
|
+
stat = await this.fs.stat(this.path);
|
|
651
|
+
} catch (error) {
|
|
652
|
+
if (error?.code === 'ENOENT') {
|
|
653
|
+
this.nodes = [];
|
|
654
|
+
this.lastSequence = 0;
|
|
655
|
+
return { nodes: this.nodes, lastSequence: 0 };
|
|
656
|
+
}
|
|
657
|
+
throw this.refuse(PLAN_CODES.unreadable, 'the plan document could not be stat-checked', {
|
|
658
|
+
cause: String(error?.code ?? 'STAT_FAILED'),
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
if (stat.size > this.maxDocumentBytes) {
|
|
662
|
+
throw this.refuse(PLAN_CODES.fileTooLarge, 'the plan document exceeds the maximum readable size', {
|
|
663
|
+
bytes: stat.size,
|
|
664
|
+
limit: this.maxDocumentBytes,
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
let text;
|
|
668
|
+
try {
|
|
669
|
+
text = await this.fs.readFile(this.path, 'utf8');
|
|
670
|
+
} catch (error) {
|
|
671
|
+
throw this.refuse(PLAN_CODES.unreadable, 'the plan document could not be read', {
|
|
672
|
+
cause: String(error?.code ?? 'READ_FAILED'),
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
let parsed;
|
|
676
|
+
try {
|
|
677
|
+
parsed = JSON.parse(text);
|
|
678
|
+
} catch {
|
|
679
|
+
throw this.refuse(PLAN_CODES.malformed, 'the plan document is not valid JSON', {});
|
|
680
|
+
}
|
|
681
|
+
if (!isPlainObject(parsed)) {
|
|
682
|
+
throw this.refuse(PLAN_CODES.malformed, 'the plan document root must be an object', {});
|
|
683
|
+
}
|
|
684
|
+
if (parsed.schemaVersion !== PLAN_SCHEMA_VERSION) {
|
|
685
|
+
throw this.refuse(PLAN_CODES.schemaUnsupported, `unsupported plan schemaVersion ${JSON.stringify(parsed.schemaVersion)}`, {
|
|
686
|
+
schemaVersion: typeof parsed.schemaVersion === 'number' ? parsed.schemaVersion : null,
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
if (!Array.isArray(parsed.nodes)) {
|
|
690
|
+
throw this.refuse(PLAN_CODES.malformed, 'the plan document must carry a nodes array', {});
|
|
691
|
+
}
|
|
692
|
+
if (parsed.nodes.length > this.maxNodes * 2) {
|
|
693
|
+
throw this.refuse(PLAN_CODES.malformed, 'the plan document contains too many nodes', {
|
|
694
|
+
count: parsed.nodes.length,
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
const seenIds = new Set();
|
|
699
|
+
const loaded = [];
|
|
700
|
+
let maxSeq = Number.isSafeInteger(parsed.lastSequence) && parsed.lastSequence >= 0 ? parsed.lastSequence : 0;
|
|
701
|
+
|
|
702
|
+
for (let i = 0; i < parsed.nodes.length; i += 1) {
|
|
703
|
+
const raw = parsed.nodes[i];
|
|
704
|
+
if (!isPlainObject(raw)) {
|
|
705
|
+
throw this.refuse(PLAN_CODES.malformed, `plan node at index ${i} is not an object`, { index: i });
|
|
706
|
+
}
|
|
707
|
+
const normalized = normalizeStoredPlanNode(raw, i, this.refuse.bind(this));
|
|
708
|
+
if (seenIds.has(normalized.nodeId)) {
|
|
709
|
+
throw this.refuse(PLAN_CODES.malformed, `duplicate plan nodeId ${normalized.nodeId}`, {
|
|
710
|
+
nodeId: normalized.nodeId,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
seenIds.add(normalized.nodeId);
|
|
714
|
+
if (normalized.sequence > maxSeq) maxSeq = normalized.sequence;
|
|
715
|
+
loaded.push(normalized);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
loaded.sort(comparePlanNodes);
|
|
719
|
+
this.nodes = loaded;
|
|
720
|
+
this.lastSequence = maxSeq;
|
|
721
|
+
return { nodes: this.nodes, lastSequence: this.lastSequence };
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
enforceDocumentLimits(draft, lastSequence, now) {
|
|
725
|
+
if (draft.length > this.maxNodes) {
|
|
726
|
+
throw new PlanError(PLAN_CODES.limitExceeded, `plan node count ${draft.length} exceeds limit ${this.maxNodes}`, {
|
|
727
|
+
nodes: draft.length,
|
|
728
|
+
limit: this.maxNodes,
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
const payload = serializeDocument(draft, lastSequence, now);
|
|
732
|
+
const bytes = Buffer.byteLength(payload, 'utf8');
|
|
733
|
+
if (bytes > this.maxBytes) {
|
|
734
|
+
throw new PlanError(PLAN_CODES.limitExceeded, `plan document size ${bytes} bytes exceeds limit ${this.maxBytes}`, {
|
|
735
|
+
bytes,
|
|
736
|
+
byteLimit: this.maxBytes,
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
async writeDocument(draft, lastSequence, now) {
|
|
742
|
+
const sorted = [...draft].sort(comparePlanNodes);
|
|
743
|
+
const payload = serializeDocument(sorted, lastSequence, now);
|
|
744
|
+
const temp = join(dirname(this.path), `.${PLAN_FILE}.${randomBytes(6).toString('hex')}.tmp`);
|
|
745
|
+
try {
|
|
746
|
+
await this.fs.mkdir(dirname(this.path), { recursive: true });
|
|
747
|
+
await this.fs.writeFile(temp, payload, 'utf8');
|
|
748
|
+
await renameWithRetry(this.fs, temp, this.path);
|
|
749
|
+
} catch (error) {
|
|
750
|
+
try {
|
|
751
|
+
await this.fs.rm(temp, { force: true });
|
|
752
|
+
} catch {
|
|
753
|
+
// best-effort temp cleanup
|
|
754
|
+
}
|
|
755
|
+
throw new PlanError(PLAN_CODES.writeFailed, 'the plan document could not be written', {
|
|
756
|
+
cause: String(error?.code ?? 'WRITE_FAILED'),
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
refuse(code, message, details) {
|
|
762
|
+
const refusal = this.refusal ?? { code, message, details };
|
|
763
|
+
this.refusal = refusal;
|
|
764
|
+
return new PlanError(refusal.code, refusal.message, { ...refusal.details });
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
throwIfRefused() {
|
|
768
|
+
if (this.refusal === null) return;
|
|
769
|
+
throw new PlanError(this.refusal.code, this.refusal.message, { ...this.refusal.details });
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
disposedError() {
|
|
773
|
+
return new PlanError(PLAN_CODES.disposed, 'the plan store has been disposed', {});
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function serializeDocument(nodes, lastSequence, updatedAt) {
|
|
778
|
+
return `${JSON.stringify({
|
|
779
|
+
schemaVersion: PLAN_SCHEMA_VERSION,
|
|
780
|
+
updatedAt,
|
|
781
|
+
lastSequence,
|
|
782
|
+
nodes,
|
|
783
|
+
}, null, 2)}\n`;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
function projectPlanNode(raw, maxNodeBytes) {
|
|
787
|
+
const entry = redactDeep({
|
|
788
|
+
nodeId: raw.nodeId,
|
|
789
|
+
parentNodeId: raw.parentNodeId ?? null,
|
|
790
|
+
title: raw.title,
|
|
791
|
+
...(raw.goalDescription === null || raw.goalDescription === undefined ? {} : { goalDescription: raw.goalDescription }),
|
|
792
|
+
sourcePath: raw.sourcePath ?? null,
|
|
793
|
+
sourcePathDigest: isPathDigest(raw.sourcePathDigest) ? raw.sourcePathDigest : null,
|
|
794
|
+
connectionId: raw.connectionId,
|
|
795
|
+
sequence: raw.sequence,
|
|
796
|
+
createdAt: raw.createdAt,
|
|
797
|
+
updatedAt: raw.updatedAt,
|
|
798
|
+
hostAcceptance: raw.hostAcceptance ?? null,
|
|
799
|
+
...(Array.isArray(raw.reviewHistory) && raw.reviewHistory.length > 0 ? { reviewHistory: raw.reviewHistory } : {}),
|
|
800
|
+
});
|
|
801
|
+
const bytes = Buffer.byteLength(JSON.stringify(entry), 'utf8');
|
|
802
|
+
if (bytes > maxNodeBytes) {
|
|
803
|
+
throw new PlanError(PLAN_CODES.limitExceeded, `plan node exceeds per-node limit of ${maxNodeBytes} bytes`, {
|
|
804
|
+
nodeId: entry.nodeId,
|
|
805
|
+
bytes,
|
|
806
|
+
limit: maxNodeBytes,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
return entry;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
function normalizeStoredPlanNode(raw, index, refuse) {
|
|
813
|
+
const nodeId = typeof raw.nodeId === 'string' ? raw.nodeId.trim() : '';
|
|
814
|
+
if (nodeId === '' || nodeId.length > MAX_ID_CHARS || !nodeId.startsWith('plan-')) {
|
|
815
|
+
throw refuse(PLAN_CODES.malformed, `plan node at index ${index} has an invalid nodeId`, { index });
|
|
816
|
+
}
|
|
817
|
+
const connectionId = typeof raw.connectionId === 'string' ? raw.connectionId.trim() : '';
|
|
818
|
+
if (connectionId === '' || connectionId.length > MAX_ID_CHARS) {
|
|
819
|
+
throw refuse(PLAN_CODES.malformed, `plan node ${nodeId} is missing a valid connectionId`, { nodeId });
|
|
820
|
+
}
|
|
821
|
+
const rawTitle = typeof raw.title === 'string' ? redactText(raw.title).trim() : '';
|
|
822
|
+
if (rawTitle === '') {
|
|
823
|
+
throw refuse(PLAN_CODES.malformed, `plan node ${nodeId} is missing a valid title`, { nodeId });
|
|
824
|
+
}
|
|
825
|
+
const title = truncateCodePoints(rawTitle, PLAN_TITLE_MAX_CHARS);
|
|
826
|
+
const parentNodeId = typeof raw.parentNodeId === 'string' && raw.parentNodeId.trim() !== ''
|
|
827
|
+
? raw.parentNodeId.trim().slice(0, MAX_ID_CHARS)
|
|
828
|
+
: null;
|
|
829
|
+
|
|
830
|
+
let goalDescription = null;
|
|
831
|
+
if (typeof raw.goalDescription === 'string' && raw.goalDescription.trim() !== '') {
|
|
832
|
+
goalDescription = truncateTaskDescription(redactText(raw.goalDescription).trim(), 0, TASK_DESCRIPTION_MAX_CHARS);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
const sourcePath = typeof raw.sourcePath === 'string' && raw.sourcePath.trim() !== ''
|
|
836
|
+
? redactText(raw.sourcePath.trim()).slice(0, MAX_PATH_CHARS)
|
|
837
|
+
: null;
|
|
838
|
+
const sourcePathDigest = isPathDigest(raw.sourcePathDigest) ? raw.sourcePathDigest : null;
|
|
839
|
+
|
|
840
|
+
const sequence = Number.isSafeInteger(raw.sequence) && raw.sequence >= 0 ? raw.sequence : index + 1;
|
|
841
|
+
const createdAt = safeTimestamp(raw.createdAt) ?? 0;
|
|
842
|
+
const updatedAt = safeTimestamp(raw.updatedAt) ?? createdAt;
|
|
843
|
+
const hostAcceptance = normalizeStoredAcceptance(raw.hostAcceptance);
|
|
844
|
+
const reviewHistory = Array.isArray(raw.reviewHistory)
|
|
845
|
+
? raw.reviewHistory
|
|
846
|
+
.map(normalizeStoredAcceptance)
|
|
847
|
+
.filter((item) => item !== null)
|
|
848
|
+
.slice(-MAX_PLAN_REVIEW_HISTORY)
|
|
849
|
+
: [];
|
|
850
|
+
|
|
851
|
+
return redactDeep({
|
|
852
|
+
nodeId,
|
|
853
|
+
parentNodeId,
|
|
854
|
+
title,
|
|
855
|
+
...(goalDescription === null ? {} : { goalDescription }),
|
|
856
|
+
sourcePath,
|
|
857
|
+
sourcePathDigest,
|
|
858
|
+
connectionId,
|
|
859
|
+
sequence,
|
|
860
|
+
createdAt,
|
|
861
|
+
updatedAt,
|
|
862
|
+
hostAcceptance,
|
|
863
|
+
...(reviewHistory.length > 0 ? { reviewHistory } : {}),
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
function normalizeStoredAcceptance(value) {
|
|
868
|
+
if (!isPlainObject(value)) return null;
|
|
869
|
+
const ver = value.state ?? value.decision;
|
|
870
|
+
if (ver !== 'accepted' && ver !== 'changes-requested') return null;
|
|
871
|
+
const note = typeof value.note === 'string' ? normalizeReviewNote(redactText(value.note)) : '';
|
|
872
|
+
if (note === '') return null;
|
|
873
|
+
const boundedNote = truncateCodePoints(note, REVIEW_NOTE_MAX_CHARS);
|
|
874
|
+
let summary = null;
|
|
875
|
+
if (typeof value.completionSummary === 'string' && value.completionSummary.trim() !== '') {
|
|
876
|
+
summary = truncateTaskDescription(redactText(value.completionSummary).trim(), 0, TASK_DESCRIPTION_MAX_CHARS);
|
|
877
|
+
}
|
|
878
|
+
return {
|
|
879
|
+
accepted: ver === 'accepted',
|
|
880
|
+
state: ver,
|
|
881
|
+
decision: ver,
|
|
882
|
+
acceptedAt: safeTimestamp(value.acceptedAt ?? value.decidedAt) ?? 0,
|
|
883
|
+
by: 'commander',
|
|
884
|
+
note: boundedNote,
|
|
885
|
+
noteChars: reviewNoteLength(boundedNote),
|
|
886
|
+
...(summary === null ? {} : { completionSummary: summary }),
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
function computeExistingDepth(startNodeId, existingById, connectionId) {
|
|
891
|
+
const visited = new Set();
|
|
892
|
+
let curId = startNodeId;
|
|
893
|
+
let depth = 0;
|
|
894
|
+
while (curId !== null) {
|
|
895
|
+
if (visited.has(curId)) {
|
|
896
|
+
throw new PlanError(PLAN_CODES.cycleDetected, 'existing plan ancestry contains a cycle', {
|
|
897
|
+
nodeId: startNodeId,
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
visited.add(curId);
|
|
901
|
+
const node = existingById.get(curId) ?? null;
|
|
902
|
+
if (node === null || node.connectionId !== connectionId) break;
|
|
903
|
+
depth += 1;
|
|
904
|
+
curId = node.parentNodeId ?? null;
|
|
905
|
+
}
|
|
906
|
+
return depth;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
function topologicalBatchOrder(items, batchByKey) {
|
|
910
|
+
const result = [];
|
|
911
|
+
const state = new Array(items.length).fill(0); // 0 = unvisited, 1 = visiting, 2 = done
|
|
912
|
+
const visit = (index) => {
|
|
913
|
+
if (state[index] === 2) return;
|
|
914
|
+
if (state[index] === 1) {
|
|
915
|
+
throw new PlanError(PLAN_CODES.cycleDetected, 'batch plan nodes contain a parentKey cycle', { index });
|
|
916
|
+
}
|
|
917
|
+
state[index] = 1;
|
|
918
|
+
const pk = items[index].parentKey;
|
|
919
|
+
if (typeof pk === 'string' && pk.trim() !== '') {
|
|
920
|
+
const parentEntry = batchByKey.get(pk.trim());
|
|
921
|
+
if (parentEntry !== undefined) visit(parentEntry.index);
|
|
922
|
+
}
|
|
923
|
+
state[index] = 2;
|
|
924
|
+
result.push(index);
|
|
925
|
+
};
|
|
926
|
+
for (let i = 0; i < items.length; i += 1) visit(i);
|
|
927
|
+
return result;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
function resolveItemSource(rawWorkspacePath, parentSource) {
|
|
931
|
+
if (rawWorkspacePath !== undefined && rawWorkspacePath !== null) {
|
|
932
|
+
const ws = validateWorkspaceInput(rawWorkspacePath, 'workspacePath');
|
|
933
|
+
if (parentSource !== null && (parentSource.path !== null || parentSource.digest !== null)) {
|
|
934
|
+
if (!recordedDirectoryIs(parentSource, ws.path)) {
|
|
935
|
+
throw new PlanError(
|
|
936
|
+
PLAN_CODES.sourceMismatch,
|
|
937
|
+
'child plan node workspacePath does not match its parent source repository',
|
|
938
|
+
{},
|
|
939
|
+
);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
return ws;
|
|
943
|
+
}
|
|
944
|
+
if (parentSource !== null && (parentSource.path !== null || parentSource.digest !== null)) {
|
|
945
|
+
const reading = readPathIdentity(parentSource.path, parentSource.digest);
|
|
946
|
+
if (reading.canonical !== null && reading.digest !== null && pathDigest(reading.canonical) !== reading.digest) {
|
|
947
|
+
throw new PlanError(PLAN_CODES.sourceMismatch, 'parent plan node has an inconsistent recorded source path', {});
|
|
948
|
+
}
|
|
949
|
+
if (reading.canonical === null && reading.digest === null) {
|
|
950
|
+
throw new PlanError(PLAN_CODES.sourceMismatch, 'parent plan node has an unusable masked source path', {});
|
|
951
|
+
}
|
|
952
|
+
return {
|
|
953
|
+
path: parentSource.path,
|
|
954
|
+
digest: reading.digest,
|
|
955
|
+
};
|
|
956
|
+
}
|
|
957
|
+
return { path: null, digest: null };
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
function validateWorkspaceInput(value, field) {
|
|
961
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
962
|
+
throw new PlanError(PLAN_CODES.invalid, `${field} must be a non-empty absolute path string`, { field });
|
|
963
|
+
}
|
|
964
|
+
const trimmed = value.trim();
|
|
965
|
+
if (trimmed.length > MAX_PATH_CHARS || !isAbsolute(trimmed) || recordedPathIsMasked(trimmed)) {
|
|
966
|
+
throw new PlanError(PLAN_CODES.invalid, `${field} must be an unmasked absolute path`, { field });
|
|
967
|
+
}
|
|
968
|
+
const digest = pathDigest(trimmed);
|
|
969
|
+
if (digest === null) {
|
|
970
|
+
throw new PlanError(PLAN_CODES.invalid, `${field} could not be resolved to a valid path identity`, { field });
|
|
971
|
+
}
|
|
972
|
+
return { path: trimmed, digest };
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
function validateParentRefInput(value, field) {
|
|
976
|
+
if (typeof value !== 'string') {
|
|
977
|
+
throw new PlanError(PLAN_CODES.invalid, `${field} must be a non-empty string`, { field });
|
|
978
|
+
}
|
|
979
|
+
const trimmed = value.trim();
|
|
980
|
+
if (trimmed === '' || trimmed.length > MAX_ID_CHARS) {
|
|
981
|
+
throw new PlanError(PLAN_CODES.invalid, `${field} must be a bounded non-empty string`, { field });
|
|
982
|
+
}
|
|
983
|
+
return trimmed;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
function validateBatchArray(nodes) {
|
|
987
|
+
if (!Array.isArray(nodes) || nodes.length === 0) {
|
|
988
|
+
throw new PlanError(PLAN_CODES.invalid, 'nodes must be a non-empty array', { field: 'nodes' });
|
|
989
|
+
}
|
|
990
|
+
if (nodes.length > MAX_PLAN_BATCH) {
|
|
991
|
+
throw new PlanError(PLAN_CODES.invalid, `nodes batch may contain at most ${MAX_PLAN_BATCH} entries`, {
|
|
992
|
+
field: 'nodes',
|
|
993
|
+
limit: MAX_PLAN_BATCH,
|
|
994
|
+
count: nodes.length,
|
|
995
|
+
});
|
|
996
|
+
}
|
|
997
|
+
return nodes;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
function singleItemFromRequest(request) {
|
|
1001
|
+
return {
|
|
1002
|
+
title: request.title,
|
|
1003
|
+
...(request.goalDescription === undefined ? {} : { goalDescription: request.goalDescription }),
|
|
1004
|
+
...(request.parentNodeId === undefined ? {} : { parentNodeId: request.parentNodeId }),
|
|
1005
|
+
...(request.workspacePath === undefined ? {} : { workspacePath: request.workspacePath }),
|
|
1006
|
+
};
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
function canonicalPlanTitle(value) {
|
|
1010
|
+
if (typeof value !== 'string') {
|
|
1011
|
+
throw new PlanError(PLAN_CODES.titleInvalid, 'plan title must be a non-empty string', {
|
|
1012
|
+
field: 'title',
|
|
1013
|
+
limit: PLAN_TITLE_MAX_CHARS,
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
const trimmed = value.trim();
|
|
1017
|
+
const points = Array.from(trimmed).length;
|
|
1018
|
+
if (points === 0 || points > PLAN_TITLE_MAX_CHARS) {
|
|
1019
|
+
throw new PlanError(
|
|
1020
|
+
PLAN_CODES.titleInvalid,
|
|
1021
|
+
`plan title must be 1..${PLAN_TITLE_MAX_CHARS} Unicode code points, saw ${points}`,
|
|
1022
|
+
{ field: 'title', limit: PLAN_TITLE_MAX_CHARS, length: points, unit: 'unicode-code-points' },
|
|
1023
|
+
);
|
|
1024
|
+
}
|
|
1025
|
+
const masked = redactText(trimmed).trim();
|
|
1026
|
+
const maskedPoints = Array.from(masked).length;
|
|
1027
|
+
if (maskedPoints === 0 || maskedPoints > PLAN_TITLE_MAX_CHARS) {
|
|
1028
|
+
throw new PlanError(
|
|
1029
|
+
PLAN_CODES.titleInvalid,
|
|
1030
|
+
`plan title after redaction must be 1..${PLAN_TITLE_MAX_CHARS} Unicode code points`,
|
|
1031
|
+
{ field: 'title', limit: PLAN_TITLE_MAX_CHARS, length: maskedPoints, unit: 'unicode-code-points' },
|
|
1032
|
+
);
|
|
1033
|
+
}
|
|
1034
|
+
return masked;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
function canonicalPlanDescription(value, field, options = {}) {
|
|
1038
|
+
const result = canonicalTaskDescription(value, field, redactText);
|
|
1039
|
+
if (result.ok === false) {
|
|
1040
|
+
throw new PlanError(options.code ?? PLAN_CODES.descriptionInvalid, result.refusal.message, {
|
|
1041
|
+
field: result.refusal.field,
|
|
1042
|
+
limit: result.refusal.limit,
|
|
1043
|
+
length: result.refusal.length,
|
|
1044
|
+
unit: result.refusal.unit,
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
return result.value;
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
function requireConnectionId(value) {
|
|
1051
|
+
if (typeof value !== 'string' || value.trim() === '' || value.trim().length > MAX_ID_CHARS) {
|
|
1052
|
+
throw new PlanError(PLAN_CODES.invalid, 'a verified connectionId is required', {});
|
|
1053
|
+
}
|
|
1054
|
+
return value.trim();
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
function requireNodeId(value) {
|
|
1058
|
+
if (typeof value !== 'string' || value.trim() === '' || value.trim().length > MAX_ID_CHARS) {
|
|
1059
|
+
throw new PlanError(PLAN_CODES.invalid, 'nodeId must be a non-empty string', { field: 'nodeId' });
|
|
1060
|
+
}
|
|
1061
|
+
return value.trim();
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
function absentNodeError(nodeId) {
|
|
1065
|
+
return new PlanError(PLAN_CODES.nodeNotFound, 'no such plan node for this connection', { nodeId });
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
function comparePlanNodes(a, b) {
|
|
1069
|
+
const seqA = a.sequence ?? 0;
|
|
1070
|
+
const seqB = b.sequence ?? 0;
|
|
1071
|
+
if (seqA !== seqB) return seqA - seqB;
|
|
1072
|
+
const left = a.createdAt ?? 0;
|
|
1073
|
+
const right = b.createdAt ?? 0;
|
|
1074
|
+
if (left !== right) return left - right;
|
|
1075
|
+
return a.nodeId < b.nodeId ? -1 : a.nodeId > b.nodeId ? 1 : 0;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
function safeTimestamp(value) {
|
|
1079
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return null;
|
|
1080
|
+
if (value < 0 || value > MAX_SAFE_TIMESTAMP) return null;
|
|
1081
|
+
return Math.trunc(value);
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
function isPlainObject(value) {
|
|
1085
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
1086
|
+
const proto = Object.getPrototypeOf(value);
|
|
1087
|
+
return proto === Object.prototype || proto === null;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
async function renameWithRetry(fs, temp, target) {
|
|
1091
|
+
let delay = 20;
|
|
1092
|
+
for (let attempt = 0; ; attempt += 1) {
|
|
1093
|
+
try {
|
|
1094
|
+
await fs.rename(temp, target);
|
|
1095
|
+
return;
|
|
1096
|
+
} catch (error) {
|
|
1097
|
+
const transient = process.platform === 'win32' && WINDOWS_TRANSIENT_RENAME.has(error?.code ?? '');
|
|
1098
|
+
if (!transient || attempt >= 8) throw error;
|
|
1099
|
+
}
|
|
1100
|
+
await new Promise((resolve) => { setTimeout(resolve, delay); });
|
|
1101
|
+
delay = Math.min(delay * 2, 200);
|
|
1102
|
+
}
|
|
1103
|
+
}
|