@deksden-com/dd-flow-cli 0.9.0-beta.1 → 0.9.0-beta.7

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.
@@ -4,12 +4,14 @@ import path from "node:path";
4
4
  import { AppError } from "../shared/errors.js";
5
5
  import { findRecentMatchingHookEvent, claimStageStartHookEvent, claimWorkStartHookEvent, hookSessionIdentity, workStartMatchKey } from "./hooks.js";
6
6
  import { validateSchema } from "./schema-validation.js";
7
- import { resolveProjectRoot, resolveRunReferences, writeJsonAtomic } from "../storage/paths.js";
7
+ import { resolveProjectRoot, resolveRunReferences } from "../storage/paths.js";
8
8
  import { refreshRunSessionProjection } from "./run-projection.js";
9
- import { readCodeCheckProfile, runCodeChecks } from "./code-checks.js";
9
+ import { readCodeCheckProfile, runCodeChecks, workspaceFingerprint } from "./code-checks.js";
10
10
  import { nextWorkId, nextWorkIds } from "./ids.js";
11
11
  import { appendFlowRunTimelineEvent } from "./runs.js";
12
12
  import { flowCommand } from "./stage-pause.js";
13
+ import { assertPortableArtifactRef } from "./portable-refs.js";
14
+ import { publicSessionIdentity } from "./session-identity.js";
13
15
  const workColumns = "work_id, project_id, run_id, parent_work_id, task, launch_policy, result_schema, payload_json, depends_on_json, status, result, created_at, started_at, updated_at, completed_at";
14
16
  export function ensureWorkRegistry(context) { context.db.exec("SELECT 1 FROM works LIMIT 1"); context.db.exec("SELECT 1 FROM work_sessions LIMIT 1"); }
15
17
  export function createChildWork(context, input) {
@@ -111,7 +113,7 @@ export function listWorks(context, input) {
111
113
  return { ...work, payload: parsePayload(work), payload_json: undefined, depends_on: parseDependencies(work), ready: isReadyNow, ...(isReadyNow ? { start_command: workStartCommand(context, work) } : {}), ...(input.includeResults ? {} : { result: undefined }) };
112
114
  }) };
113
115
  }
114
- export function showWork(context, id) { const work = requireWork(context, id); const sessions = context.db.all("SELECT id, work_id, session_id, hook_event_id, status, prompt_path, result_path, created_at, completed_at FROM work_sessions WHERE work_id = ? ORDER BY created_at", [work.work_id]); return { work: { ...work, short_id: shortWorkId(work.work_id), payload: parsePayload(work), payload_json: undefined, depends_on: parseDependencies(work), sessions } }; }
116
+ export function showWork(context, id) { const work = requireWork(context, id); const sessions = context.db.all("SELECT ws.id, ws.work_id, ws.session_id, ws.hook_event_id, ws.status, ws.prompt_path, ws.result_path, ws.created_at, ws.completed_at, s.harness AS harness_id, COALESCE(s.provider_session_id, s.session_id) AS native_session_id FROM work_sessions ws LEFT JOIN sessions s ON s.project_id = ? AND s.session_id = ws.session_id WHERE ws.work_id = ? ORDER BY ws.created_at", [work.project_id, work.work_id]); return { work: { ...work, short_id: shortWorkId(work.work_id), payload: parsePayload(work), payload_json: undefined, depends_on: parseDependencies(work), sessions: sessions.map(({ session_id, harness_id, native_session_id, ...session }) => ({ ...session, ...(harness_id && native_session_id ? { session: publicSessionIdentity({ harness: harness_id, provider_session_id: native_session_id, session_id }) } : {}) })) } }; }
115
117
  export function mutateWorkDeps(context, input) {
116
118
  const work = requireWork(context, input.workId);
117
119
  if (input.action === "list")
@@ -262,6 +264,17 @@ export function startBoundWork(context, work, run, identity, hookEventId) {
262
264
  fs.mkdirSync(directory, { recursive: true });
263
265
  const promptPath = path.join(directory, "prompt.md");
264
266
  const resultPath = path.join(directory, "result.json");
267
+ const contextPath = path.join(directory, "context.json");
268
+ const dependencyResults = parseDependencies(work).map((dependency) => context.db.get("SELECT work_id, result FROM works WHERE work_id = ?", [dependency])).filter(Boolean);
269
+ const prompt = renderWorkerPrompt(context, work, run, dependencyResults);
270
+ const payload = parsePayload(work);
271
+ const readOnly = payload?.read_only === true;
272
+ const workContext = { schema_id: "dd-flow/work-context@1", run_id: work.run_id, work_id: work.work_id, parent_work_id: work.parent_work_id, project_root: run.project_root, workspace_root: run.workspace_root, run_root: requireRunHome(run), depends_on: parseDependencies(work), launch_policy: work.launch_policy, result_schema: work.result_schema, read_only: readOnly, ...(readOnly ? { workspace_fingerprint: workspaceFingerprint(run.workspace_root) } : {}) };
273
+ const token = crypto.randomUUID();
274
+ const promptCandidate = `${promptPath}.${token}.tmp`;
275
+ const contextCandidate = `${contextPath}.${token}.tmp`;
276
+ fs.writeFileSync(promptCandidate, prompt);
277
+ fs.writeFileSync(contextCandidate, `${JSON.stringify(workContext, null, 2)}\n`);
265
278
  context.db.exec("BEGIN IMMEDIATE");
266
279
  try {
267
280
  bindSession(context, work, run, identity, now);
@@ -271,23 +284,23 @@ export function startBoundWork(context, work, run, identity, hookEventId) {
271
284
  if (claimed.changes !== 1)
272
285
  throw new AppError("conflict", "Work was claimed concurrently", 1, { work_id: work.work_id });
273
286
  context.db.run(`INSERT INTO work_sessions (id, work_id, session_id, hook_event_id, status, prompt_path, result_path, created_at, updated_at, completed_at) VALUES (?, ?, ?, ?, 'running', ?, ?, ?, ?, NULL)`, [linkId, work.work_id, identity.sessionId, hookEventId, promptPath, resultPath, now, now]);
287
+ fs.renameSync(promptCandidate, promptPath);
288
+ fs.renameSync(contextCandidate, contextPath);
274
289
  context.db.exec("COMMIT");
275
290
  }
276
291
  catch (error) {
277
292
  context.db.exec("ROLLBACK");
293
+ fs.rmSync(promptCandidate, { force: true });
294
+ fs.rmSync(contextCandidate, { force: true });
278
295
  throw error;
279
296
  }
280
- const dependencyResults = parseDependencies(work).map((dependency) => context.db.get("SELECT work_id, result FROM works WHERE work_id = ?", [dependency])).filter(Boolean);
281
- const prompt = renderWorkerPrompt(context, work, run, dependencyResults, resultPath);
282
- writeJsonAtomic(path.join(directory, "context.json"), { schema_id: "dd-flow/work-context@1", run_id: work.run_id, work_id: work.work_id, parent_work_id: work.parent_work_id, project_root: run.project_root, workspace_root: run.workspace_root, run_root: requireRunHome(run), depends_on: parseDependencies(work), launch_policy: work.launch_policy, result_schema: work.result_schema });
283
- fs.writeFileSync(promptPath, prompt);
284
297
  refreshRunWorkProjection(context, work.project_id, work.run_id);
285
- return { ok: true, work_id: work.work_id, work_session_id: linkId, worker_prompt_markdown: prompt, prompt_path: promptPath, session_binding: { source: "PreToolUse", session_id: identity.sessionId } };
298
+ return { ok: true, work_id: work.work_id, work_session_id: linkId, worker_prompt_markdown: prompt, prompt_path: promptPath, session_binding: { source: "PreToolUse", session: { harness_id: identity.harness, session_id: identity.nativeSessionId } } };
286
299
  }
287
300
  export function finishWork(context, id, result, progress) { return settle(context, id, "completed", result, progress); }
288
301
  /** Structured fan-in closes a parent after its Session was handed to a child. */
289
302
  export function finishFanInWork(context, id, result) { const work = requireWork(context, id); if (work.status !== "running")
290
- throw new AppError("invalid_work_state", "Fan-in Work is not running", 2, { work_id: work.work_id, status: work.status }); if (context.db.get("SELECT 1 FROM works WHERE parent_work_id = ? AND status IN ('created','running') LIMIT 1", [work.work_id]))
303
+ throw new AppError("invalid_work_state", "Fan-in Work is not running", 2, { work_id: work.work_id, status: work.status }); if (context.db.get("SELECT 1 FROM works WHERE parent_work_id = ? AND status IN ('created','running','paused') LIMIT 1", [work.work_id]))
291
304
  throw new AppError("active_child_work", "Fan-in Work still has active children", 2, { work_id: work.work_id }); const now = context.now(); context.db.run("UPDATE works SET status = 'completed', result = ?, completed_at = ?, updated_at = ? WHERE work_id = ?", [result, now, now, work.work_id]); context.db.run("UPDATE work_sessions SET status = 'completed', completed_at = COALESCE(completed_at, ?), updated_at = ? WHERE work_id = ? AND status = 'running'", [now, now, work.work_id]); refreshRunWorkProjection(context, work.project_id, work.run_id); appendFlowRunTimelineEvent(context, work.project_id, work.run_id, { type: "work_completed", work_id: work.work_id, fan_in: true }); return { ok: true, work_id: work.work_id, status: "completed", fan_in: true }; }
292
305
  export function failWork(context, id, reason) { return settle(context, id, "failed", reason); }
293
306
  export function cancelWork(context, id, reason) { return settle(context, id, "cancelled", reason); }
@@ -302,7 +315,7 @@ async function settle(context, id, status, result, progress) {
302
315
  id = work.work_id;
303
316
  if (work.status !== "running" && !(status === "cancelled" && work.status === "created"))
304
317
  throw new AppError("invalid_work_state", "Work is not running", 2, { status: work.status });
305
- if (status === "completed" && context.db.get("SELECT 1 FROM works WHERE parent_work_id = ? AND status IN ('created', 'running') LIMIT 1", [id]))
318
+ if (status === "completed" && context.db.get("SELECT 1 FROM works WHERE parent_work_id = ? AND status IN ('created', 'running', 'paused') LIMIT 1", [id]))
306
319
  throw new AppError("active_child_work", "Work cannot complete while a child Work is active", 2, { work_id: id });
307
320
  const run = requireRun(context, work.project_id, work.run_id);
308
321
  const link = context.db.get("SELECT id, work_id, session_id, hook_event_id, status, prompt_path, result_path, created_at, completed_at FROM work_sessions WHERE work_id = ? AND status = 'running' ORDER BY created_at DESC LIMIT 1", [id]);
@@ -311,7 +324,14 @@ async function settle(context, id, status, result, progress) {
311
324
  let receipts = [];
312
325
  let coordinationDrift = [];
313
326
  if (status === "completed") {
314
- validateWorkResult(work, result, run.project_root, run.workspace_root, run.id, link?.result_path ?? null);
327
+ if (parsePayload(work)?.read_only === true) {
328
+ const contextFile = link ? path.join(path.dirname(link.prompt_path), "context.json") : "";
329
+ const baseline = contextFile && fs.existsSync(contextFile) ? JSON.parse(fs.readFileSync(contextFile, "utf8")).workspace_fingerprint : null;
330
+ const current = workspaceFingerprint(run.workspace_root);
331
+ if (typeof baseline !== "string" || baseline !== current)
332
+ throw new AppError("read_only_work_mutated_workspace", "Read-only Work changed the accepted workspace", 2, { work_id: work.work_id, baseline, current });
333
+ }
334
+ validateWorkResult(work, result, run.project_root, run.workspace_root, requireRunHome(run), run.id, link?.result_path ?? null);
315
335
  const packet = codePacket(work);
316
336
  if (packet) {
317
337
  // CODE packets are projected from a PLAN that already validated this
@@ -362,21 +382,33 @@ function bindSession(context, work, run, identity, now) {
362
382
  const inferredParentSession = work.parent_work_id
363
383
  ? context.db.get("SELECT session_id FROM work_sessions WHERE work_id = ? ORDER BY created_at DESC LIMIT 1", [work.parent_work_id])?.session_id ?? null
364
384
  : priorWorkSession && priorWorkSession !== identity.sessionId ? priorWorkSession : null;
365
- const parentSession = identity.parentSessionId ?? inferredParentSession;
385
+ // Work ancestry and provider-tree containment are different relations. An
386
+ // isolated worker is logically a child Work but physically a provider root.
387
+ const parentSession = inferredParentSession ?? identity.parentSessionId;
388
+ const providerParentSession = identity.parentSessionId;
366
389
  if (work.parent_work_id && !parentSession)
367
390
  throw new AppError("parent_session_required", "Child Work requires a confirmed parent Work/Session link", 1, { work_id: work.work_id, parent_work_id: work.parent_work_id });
368
391
  if (work.launch_policy === "fresh_agent_required" && (identity.sessionId === parentSession || context.db.get("SELECT 1 FROM work_sessions ws JOIN works w ON w.work_id = ws.work_id WHERE w.project_id = ? AND w.run_id = ? AND ws.session_id = ? LIMIT 1", [work.project_id, work.run_id, identity.sessionId])))
369
392
  throw new AppError("fresh_session_required", "This Work requires a fresh Session in this RUN", 1, { work_id: work.work_id, session_id: identity.sessionId });
370
- const existing = context.db.get("SELECT session_id, parent_session_id FROM sessions WHERE project_id = ? AND session_id = ?", [work.project_id, identity.sessionId]);
393
+ const existing = context.db.get("SELECT session_id, parent_session_id, provider_parent_session_id FROM sessions WHERE project_id = ? AND session_id = ?", [work.project_id, identity.sessionId]);
371
394
  if (existing && existing.parent_session_id && parentSession && existing.parent_session_id !== parentSession)
372
395
  throw new AppError("session_parent_conflict", "Observed Session already has a different immutable parent", 1, { session_id: identity.sessionId });
373
- context.db.run(`INSERT INTO sessions (session_id, project_id, harness, provider_session_id, agent_id, parent_session_id, provider, model, reasoning, mode, agent_type, project_root, flow_kind, status, run_id, protocol_id, worker_id, workspace_path, continuation_policy, current_stage, next_action, last_action_hash, continuation_count, stop_reason, transcript_path, cwd, metadata_json, coverage_units_json, created_at, updated_at, stopped_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'vnext', 'active', ?, NULL, ?, ?, 'go_router', 'work', NULL, NULL, 0, NULL, ?, ?, '{}', '[]', ?, ?, NULL) ON CONFLICT(session_id, project_id) DO UPDATE SET harness = excluded.harness, provider_session_id = COALESCE(excluded.provider_session_id, provider_session_id), agent_id = COALESCE(excluded.agent_id, agent_id), parent_session_id = COALESCE(excluded.parent_session_id, parent_session_id), provider = COALESCE(excluded.provider, provider), model = COALESCE(excluded.model, model), reasoning = COALESCE(excluded.reasoning, reasoning), mode = COALESCE(excluded.mode, mode), agent_type = COALESCE(excluded.agent_type, agent_type), flow_kind = excluded.flow_kind, run_id = excluded.run_id, worker_id = excluded.worker_id, workspace_path = excluded.workspace_path, transcript_path = COALESCE(excluded.transcript_path, transcript_path), cwd = excluded.cwd, updated_at = excluded.updated_at`, [identity.sessionId, work.project_id, identity.harness, identity.providerSessionId, identity.agentId, existing?.parent_session_id ?? (parentSession === identity.sessionId ? null : parentSession), identity.provider, identity.model, identity.reasoning, identity.mode, identity.agentType, run.project_root, work.run_id, work.work_id, run.workspace_root, identity.transcriptPath, run.workspace_root, now, now]);
396
+ if (existing?.provider_parent_session_id && providerParentSession && existing.provider_parent_session_id !== providerParentSession)
397
+ throw new AppError("provider_session_parent_conflict", "Observed provider Session already has a different immutable provider parent", 1, { session_id: identity.sessionId });
398
+ context.db.run(`INSERT INTO sessions (session_id, project_id, harness, provider_session_id, provider_parent_session_id, agent_id, parent_session_id, provider, model, reasoning, mode, agent_type, project_root, flow_kind, status, run_id, protocol_id, worker_id, workspace_path, continuation_policy, current_stage, next_action, last_action_hash, continuation_count, stop_reason, transcript_path, cwd, metadata_json, coverage_units_json, created_at, updated_at, stopped_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'vnext', 'active', ?, NULL, ?, ?, 'go_router', 'work', NULL, NULL, 0, NULL, ?, ?, '{}', '[]', ?, ?, NULL) ON CONFLICT(session_id, project_id) DO UPDATE SET harness = excluded.harness, provider_session_id = COALESCE(excluded.provider_session_id, provider_session_id), provider_parent_session_id = COALESCE(excluded.provider_parent_session_id, provider_parent_session_id), agent_id = COALESCE(excluded.agent_id, agent_id), parent_session_id = COALESCE(excluded.parent_session_id, parent_session_id), provider = COALESCE(excluded.provider, provider), model = COALESCE(excluded.model, model), reasoning = COALESCE(excluded.reasoning, reasoning), mode = COALESCE(excluded.mode, mode), agent_type = COALESCE(excluded.agent_type, agent_type), flow_kind = excluded.flow_kind, run_id = excluded.run_id, worker_id = excluded.worker_id, workspace_path = excluded.workspace_path, transcript_path = COALESCE(excluded.transcript_path, transcript_path), cwd = excluded.cwd, updated_at = excluded.updated_at`, [identity.sessionId, work.project_id, identity.harness, identity.providerSessionId, providerParentSession, identity.agentId, existing?.parent_session_id ?? (parentSession === identity.sessionId ? null : parentSession), identity.provider, identity.model, identity.reasoning, identity.mode, identity.agentType, run.project_root, work.run_id, work.work_id, run.workspace_root, identity.transcriptPath, run.workspace_root, now, now]);
374
399
  reactivateBoundSession(context, work.project_id, identity.sessionId, now);
375
400
  }
401
+ /** Register the trusted physical Session before a paused Work moves to it. */
402
+ export function bindSessionForResume(context, workId, identity, now) {
403
+ const work = requireWork(context, workId);
404
+ if (work.status !== "paused")
405
+ throw new AppError("invalid_work_state", "Only a paused Work can bind a resume Session", 1, { work_id: work.work_id, status: work.status });
406
+ bindSession(context, work, requireRun(context, work.project_id, work.run_id), identity, now);
407
+ }
376
408
  function reactivateBoundSession(context, projectId, sessionId, now) {
377
409
  context.db.run("UPDATE sessions SET status = 'active', stop_reason = NULL, stopped_at = NULL, updated_at = ? WHERE project_id = ? AND session_id = ?", [now, projectId, sessionId]);
378
410
  }
379
- function validateWorkResult(work, result, projectRoot, workspaceRoot, runId, resultPath) {
411
+ function validateWorkResult(work, result, projectRoot, workspaceRoot, runHome, runId, resultPath) {
380
412
  if (!work.result_schema)
381
413
  return;
382
414
  if (!resultPath)
@@ -388,20 +420,35 @@ function validateWorkResult(work, result, projectRoot, workspaceRoot, runId, res
388
420
  catch {
389
421
  throw new AppError("validation", "Work result must be JSON for its declared result schema", 2, { work_id: work.work_id, result_schema: work.result_schema });
390
422
  }
391
- fs.writeFileSync(resultPath, result);
392
- validateSchema({ schemaName: work.result_schema.replace(/^dd-flow\//, "").replace(/@\d+$/, ""), file: resultPath, projectRoot, runId });
423
+ const candidate = `${resultPath}.candidate-${process.pid}`;
424
+ fs.writeFileSync(candidate, result);
425
+ try {
426
+ validateSchema({ schemaName: work.result_schema.replace(/^dd-flow\//, "").replace(/@\d+$/, ""), file: candidate, projectRoot, runId });
427
+ }
428
+ finally {
429
+ fs.rmSync(candidate, { force: true });
430
+ }
393
431
  if (work.result_schema === "dd-flow/code-work-result@2")
394
- validateCodeWorkResult(work, parsed, workspaceRoot);
432
+ validateCodeWorkResult(work, parsed, workspaceRoot, runHome, runId);
395
433
  if (work.result_schema === "dd-flow/code-review-result@1")
396
- validateCodeReviewResultIdentity(work, parsed);
434
+ validateCodeReviewResult(work, parsed, { workspaceRoot, runHome, runId });
435
+ if (work.result_schema === "dd-flow/plan-review-result@1")
436
+ validatePlanReviewResult(work, parsed, { workspaceRoot, runHome, runId });
397
437
  }
398
- function validateCodeWorkResult(work, value, projectRoot) {
438
+ function validateCodeWorkResult(work, value, projectRoot, runHome, runId) {
399
439
  const result = value;
400
440
  if ((result.deviations?.length ?? 0) > 0 || (result.blockers?.length ?? 0) > 0)
401
441
  throw new AppError("work_contract_incomplete", "CODE Work cannot complete with unresolved deviations or blockers; fail the Work and report the contract mismatch", 2, { work_id: work.work_id, deviations: result.deviations ?? [], blockers: result.blockers ?? [] });
402
442
  const packet = codePacket(work);
403
443
  if (!packet)
404
444
  return;
445
+ const acceptedCriteria = new Set(packet.acceptance.map((item) => item.criterion_id).filter((item) => typeof item === "string"));
446
+ for (const item of result.evidence ?? []) {
447
+ if (!item.criterion_id || !acceptedCriteria.has(item.criterion_id))
448
+ throw new AppError("evidence_criterion_unknown", "CODE Work evidence must reference an acceptance criterion assigned to this Work", 2, { work_id: work.work_id, criterion_id: item.criterion_id ?? null });
449
+ for (const ref of item.refs ?? [])
450
+ assertPortableArtifactRef(ref, { workspaceRoot: projectRoot, runHome, runId });
451
+ }
405
452
  const documentUpdates = (packet.document_updates ?? []);
406
453
  const changed = new Set(result.changed_paths ?? []);
407
454
  const missingDocuments = documentUpdates.map((entry) => entry.path).filter((entry) => !changed.has(entry));
@@ -413,6 +460,8 @@ function validateCodeWorkResult(work, value, projectRoot) {
413
460
  throw new AppError("document_update_not_materialized", "Assigned durable document updates must exist and differ from their PLAN baseline", 2, { work_id: work.work_id, unchanged_paths: unchangedDocuments });
414
461
  const assigned = packet.repair?.review_finding_ids ?? [];
415
462
  if (assigned.length) {
463
+ if ((result.changed_paths?.length ?? 0) === 0)
464
+ throw new AppError("review_repair_no_change", "Review repair must materialize a project change; a no-op cannot resolve a finding", 2, { work_id: work.work_id, findings: assigned });
416
465
  const resolved = new Set(result.resolved_finding_refs ?? []);
417
466
  const missing = assigned.filter((finding) => !resolved.has(finding));
418
467
  const unexpected = [...resolved].filter((finding) => !assigned.includes(finding));
@@ -420,24 +469,25 @@ function validateCodeWorkResult(work, value, projectRoot) {
420
469
  throw new AppError("review_repair_incomplete", "Review repair must explicitly resolve exactly its assigned finding references", 2, { work_id: work.work_id, missing, unexpected });
421
470
  }
422
471
  }
423
- function renderWorkerPrompt(context, work, run, dependencies, resultPath) {
472
+ function renderWorkerPrompt(context, work, run, dependencies) {
424
473
  const command = flowCommand(context);
425
474
  const packet = codePacket(work);
426
475
  const codeContext = packet ? ["<semantic_spine>", JSON.stringify(packet.semantic_spine, null, 2), "</semantic_spine>", "", ...(packet.repair ? ["<repair_context>", JSON.stringify(packet.repair, null, 2), "Read the failed receipt and its linked stdout/stderr before editing. Preserve the accepted origin context and fix only the evidenced failure.", "</repair_context>", ""] : []), "<accepted_requirements>", JSON.stringify(packet.requirements, null, 2), "</accepted_requirements>", "", "<acceptance_context>", "The criteria below are end-to-end context. Complete this Work's semantic contribution and declared checks; another ordered Work may own a different acceptance surface.", JSON.stringify(packet.acceptance, null, 2), "</acceptance_context>", "", "<required_read>", "These are mandatory starting sources, not a read allowlist. Read any additional project files needed to implement the Work correctly.", ...packet.required_read.map((item) => `- ${resolveRunReferences(item, work.run_id, requireRunHome(run))}`), "</required_read>", "", "<discovery_boundary>", "These are likely discovery areas, not a hard boundary. Expand project-local investigation when required and report material additions.", ...packet.discovery_boundary.map((item) => `- ${item}`), "</discovery_boundary>", "", "<planned_write_areas>", "SOFT COORDINATION HINT ONLY. These paths help the coordinator avoid concurrent collisions. They do not grant or deny write permission and do not limit the files needed for this Work. You may create or change any project file under workspace_root that is necessary and in semantic scope; report every actual changed path.", ...(packet.planned_write_areas.length ? packet.planned_write_areas.map((item) => `- ${item}`) : ["- none predicted; derive the necessary files from the task"]), "</planned_write_areas>", "", ...(packet.provides_checks.length ? ["<provided_checks>", ...packet.provides_checks.map((item) => `- ${item.id}: materialize ${item.command}${item.definition ? ` as ${item.definition}` : ""}; it is not usable until this Work finishes.`), "Update the declared project command or alias before Work finish. The CLI verifies the materialization and then executes the check.", "</provided_checks>", ""] : []), "<verification>", ...packet.checks.map((item) => `- ${item.id} at ${item.run_at}: ${item.command} — ${item.purpose}`), "The CLI executes work-scoped checks and retains their receipts. Report semantic evidence only; do not rerun declared checks manually.", "</verification>", "", "<stop_conditions>", ...packet.stop_conditions.map((item) => `- ${item}`), "</stop_conditions>", ""] : [];
427
476
  if (packet)
428
477
  codeContext.push("<document_updates>", JSON.stringify(packet.document_updates, null, 2), "Materialize every listed update. dd-flow verifies the resulting file against its PLAN-time baseline.", "</document_updates>", "", "<completion_contract>", "Successful completion requires empty deviations and blockers and every assigned document update in changed_paths. A necessary path outside planned_write_areas is normal coordination drift, not a blocker; include it in changed_paths and continue.", "</completion_contract>", "");
429
- return ["<work>", `- work_id: ${work.work_id}`, `- run_id: ${work.run_id}`, `- project_root: ${run.project_root}`, `- workspace_root: ${run.workspace_root}`, `- run_home: ${requireRunHome(run)}`, "</work>", "", "<hard_write_boundary>", `HARD RULE: all project reads and writes must remain under ${run.workspace_root}.`, "Do not write through project_root, outside workspace_root, into another RUN, or into Git/worktree control data. Do not create, switch, merge or delete branches/worktrees.", "Accepted requirements, non-goals and stop_conditions are semantic hard boundaries. planned_write_areas is not.", "</hard_write_boundary>", "", ...codeContext, "<dependency_results>", JSON.stringify(dependencies.filter(Boolean), null, 2), "</dependency_results>", "", "<task>", resolveRunReferences(work.task, work.run_id, requireRunHome(run)), "</task>", "", ...(work.result_schema ? ["<result_contract>", `Return JSON matching \`${work.result_schema}\`.`, ...resultSchemaGuidance(work), `Write it to ${resultPath}.`, "</result_contract>", ""] : []), "<completion>", "The CLI runs every declared required check before accepting this Work. A failed receipt means only that the check failed; it is not proof of an engine, harness, dependency, or environment blocker.", "Read the failed receipt and its stdout/stderr. Fix project-owned source, migration, test, formatting, or configuration errors in this same Work, then call Finish again. Do not invent a cause that does not appear in the retained output.", "Use Fail only for a concrete external blocker after deterministic bootstrap or a contradiction with an accepted requirement/non-goal. Never fail merely because a necessary project path was absent from planned_write_areas.", "Finish may run for several minutes. Preserve the shell tool's process/session handle and poll that same invocation until it exits; progress arrives as JSONL on stderr. Never reissue Finish merely because final stdout has not arrived.", `Finish as one standalone command: ${command} work finish ${work.work_id} --result-file ${JSON.stringify(resultPath)} --project-root ${JSON.stringify(run.project_root)} --json --progress-jsonl`, `Fail only for an evidenced external or semantic-contract blocker: ${command} work fail ${work.work_id} --reason "receipt path + exact external or semantic blocker" --project-root ${JSON.stringify(run.project_root)} --json`, "</completion>", ""].join("\n");
478
+ return ["<work>", `- work_id: ${work.work_id}`, `- run_id: ${work.run_id}`, `- project_root: ${run.project_root}`, `- workspace_root: ${run.workspace_root}`, `- run_home: ${requireRunHome(run)}`, "</work>", "", "<hard_write_boundary>", `HARD RULE: project source reads and writes must remain under ${run.workspace_root}.`, "Do not write through project_root, outside workspace_root, into another RUN, or into Git/worktree control data. Do not create, switch, merge or delete branches/worktrees.", "RUN artifacts are read-only evidence: refer to them with run:// URIs and let dd-flow persist your submitted result. Accepted requirements, non-goals and stop_conditions are semantic hard boundaries. planned_write_areas is not.", "</hard_write_boundary>", "", ...codeContext, "<dependency_results>", JSON.stringify(dependencies.filter(Boolean), null, 2), "</dependency_results>", "", "<task>", resolveRunReferences(work.task, work.run_id, requireRunHome(run)), "</task>", "", ...(work.result_schema ? ["<result_contract>", `Return JSON matching \`${work.result_schema}\`.`, ...resultSchemaGuidance(work, run.id), "Do not create result.json yourself. Send the JSON to dd-flow on stdin; it atomically validates and stores the canonical receipt.", "</result_contract>", ""] : []), "<completion>", "The CLI runs every declared required check before accepting this Work. A failed receipt means only that the check failed; it is not proof of an engine, harness, dependency, or environment blocker.", "Read the failed receipt and its stdout/stderr. Fix project-owned source, migration, test, formatting, or configuration errors in this same Work, then call Finish again. Do not invent a cause that does not appear in the retained output.", "Use Fail only for a concrete external blocker after deterministic bootstrap or a contradiction with an accepted requirement/non-goal. Never fail merely because a necessary project path was absent from planned_write_areas.", "Finish may run for several minutes. Preserve the shell tool's process/session handle and poll that same invocation until it exits; progress arrives as JSONL on stderr. Never reissue Finish merely because final stdout has not arrived.", `Finish as one standalone command, piping your JSON object to stdin: ${command} work finish ${work.work_id} --result-stdin --project-root ${JSON.stringify(run.project_root)} --json --progress-jsonl`, `Fail only for an evidenced external or semantic-contract blocker: ${command} work fail ${work.work_id} --reason "receipt path + exact external or semantic blocker" --project-root ${JSON.stringify(run.project_root)} --json`, "</completion>", ""].join("\n");
430
479
  }
431
- function resultSchemaGuidance(work) {
480
+ function resultSchemaGuidance(work, runId) {
432
481
  const schema = work.result_schema;
482
+ const refs = `Evidence refs for project source are relative to workspace_root; RUN evidence uses run://${runId}/path/to/artifact.`;
433
483
  if (schema === "dd-flow/code-work-result@2")
434
- return ["Use this complete minimal shape. For a review repair, also include resolved_finding_refs with exactly the finding references assigned in repair_context:", "```json", JSON.stringify({ schema_id: schema, summary: "What was implemented.", changed_paths: ["project-relative/path"], evidence: [{ criterion_id: "AC-001", refs: ["project-relative/evidence"] }], deviations: [], blockers: [], resolved_finding_refs: [] }, null, 2), "```"];
484
+ return [refs, "Use this complete minimal shape. For a review repair, also include resolved_finding_refs with exactly the finding references assigned in repair_context:", "```json", JSON.stringify({ schema_id: schema, summary: "What was implemented.", changed_paths: ["project-relative/path"], evidence: [{ criterion_id: "AC-001", refs: ["project-relative/evidence", `run://${runId}/05-code/checks/receipt.json`] }], deviations: [], blockers: [], resolved_finding_refs: [] }, null, 2), "```"];
435
485
  if (schema === "dd-flow/code-review-result@1") {
436
- return ["Assess every assigned aspect exactly once. Number findings locally inside this reviewer Work as FIND-001, FIND-002, ...; dd-flow adds the Work id to form the canonical reference.", "Use this complete minimal shape. Report only material, direct-evidence findings; taste and cosmetics are not findings:", "```json", JSON.stringify({ schema_id: schema, verdict: "pass | findings | blocked", summary: "Concise evidence-backed conclusion.", aspects: [{ aspect_id: "assigned_aspect_id", verdict: "pass | findings | blocked", summary: "Conclusion.", evidence_refs: ["path/to/file"] }], findings: [{ finding_id: "FIND-001", aspect_id: "assigned_aspect_id", priority: "p0 | p1 | p2 | p3", problem: "Violated obligation or rule.", impact: "Concrete risk or failure.", evidence_refs: ["path/to/file"], obligation_refs: ["R-001 | AC-001 | policy ref"] }] }, null, 2), "```"];
486
+ return [refs, "Assess every assigned aspect exactly once. Number findings locally inside this reviewer Work as FIND-001, FIND-002, ...; dd-flow adds the Work id to form the canonical reference.", "Use this complete minimal shape. Report only material, direct-evidence findings; taste and cosmetics are not findings:", "```json", JSON.stringify({ schema_id: schema, verdict: "pass | findings | blocked", summary: "Concise evidence-backed conclusion.", aspects: [{ aspect_id: "assigned_aspect_id", verdict: "pass | findings | blocked", summary: "Conclusion.", evidence_refs: ["path/to/file", `run://${runId}/05-code/checks/receipt.json`] }], findings: [{ finding_id: "FIND-001", aspect_id: "assigned_aspect_id", priority: "p0 | p1 | p2 | p3", problem: "Violated obligation or rule.", impact: "Concrete risk or failure.", evidence_refs: ["path/to/file"], obligation_refs: ["R-001 | AC-001 | policy ref"] }] }, null, 2), "```"];
437
487
  }
438
488
  if (schema !== "dd-flow/plan-review-result@1")
439
489
  return [];
440
- return ["Use this complete minimal shape; do not add fields. Number findings locally inside this reviewer Work as FIND-001, FIND-002, ...; dd-flow adds the Work id when the coordinator classifies them:", "```json", JSON.stringify({ schema_id: schema, plan_revision: 1, overall_verdict: "pass | watch | needs_changes | blocked", summary: "Concise evidence-backed conclusion.", aspects: [{ aspect_id: "assigned_aspect_id", verdict: "pass | watch | needs_changes | blocked", summary: "Conclusion for this aspect.", evidence_refs: ["path/to/file"], findings: [{ finding_id: "FIND-001", severity: "high | medium | low | info", summary: "Problem, if any.", evidence_refs: ["path/to/file"] }] }] }, null, 2), "```"];
490
+ return [refs, "Use this complete minimal shape; do not add fields. Number findings locally inside this reviewer Work as FIND-001, FIND-002, ...; dd-flow adds the Work id when the coordinator classifies them:", "```json", JSON.stringify({ schema_id: schema, plan_revision: 1, overall_verdict: "pass | watch | needs_changes | blocked", summary: "Concise evidence-backed conclusion.", aspects: [{ aspect_id: "assigned_aspect_id", verdict: "pass | watch | needs_changes | blocked", summary: "Conclusion for this aspect.", evidence_refs: ["path/to/file", `run://${runId}/03-plan/plan.json`], findings: [{ finding_id: "FIND-001", severity: "high | medium | low | info", summary: "Problem, if any.", evidence_refs: ["path/to/file"] }] }] }, null, 2), "```"];
441
491
  }
442
492
  export function validateCodeReviewResultIdentity(work, value) {
443
493
  const group = codeReviewGroup(work);
@@ -458,6 +508,57 @@ export function validateCodeReviewResultIdentity(work, value) {
458
508
  throw new AppError("review_evidence_invalid", "CODE reviewer finding ids must be local FIND-NNN ids and aspect refs must stay inside the assigned review group", 2, { work_id: work.work_id, group: group.key, required_format: "FIND-NNN", invalid, wrong_aspect: wrongAspect });
459
509
  }
460
510
  }
511
+ /**
512
+ * CODE review evidence is part of the Work contract, not a late stage-level
513
+ * concern. Reject it before `works.result` becomes authoritative so a later
514
+ * fan-in cannot discover a malformed accepted reviewer result.
515
+ */
516
+ function validateCodeReviewResult(work, value, input) {
517
+ validateCodeReviewResultIdentity(work, value);
518
+ const result = value;
519
+ const findings = result.findings ?? [];
520
+ // A reviewer is a child Work and cannot pause its coordinator-owned Stage.
521
+ // Its structured blocked result is evidence for the coordinator, which then
522
+ // either resolves the gap or pauses the Stage itself.
523
+ if ((result.verdict === "pass" && findings.length > 0) || (result.verdict === "findings" && findings.length === 0)) {
524
+ throw new AppError("review_evidence_invalid", "CODE reviewer verdict must agree with whether material findings are present", 2, { work_id: work.work_id, verdict: result.verdict, findings: findings.length });
525
+ }
526
+ const references = [
527
+ ...(result.aspects ?? []).flatMap((aspect) => Array.isArray(aspect.evidence_refs) ? aspect.evidence_refs : []),
528
+ ...(result.findings ?? []).flatMap((finding) => Array.isArray(finding.evidence_refs) ? finding.evidence_refs : [])
529
+ ];
530
+ for (const ref of references) {
531
+ if (typeof ref !== "string")
532
+ throw new AppError("review_evidence_invalid", "CODE reviewer evidence references must be strings", 2, { work_id: work.work_id, ref });
533
+ assertPortableArtifactRef(ref, input);
534
+ }
535
+ }
536
+ export function validatePlanReviewResult(work, value, input) {
537
+ const group = codeReviewGroup(work);
538
+ if (!group)
539
+ throw new AppError("review_evidence_invalid", "PLAN reviewer Work has no assigned review group", 2, { work_id: work.work_id });
540
+ const result = value;
541
+ const aspects = result.aspects ?? [];
542
+ const ids = aspects.map((item) => item.aspect_id ?? "");
543
+ const expected = new Set(group.aspect_ids);
544
+ const missing = group.aspect_ids.filter((id) => !ids.includes(id));
545
+ const unexpected = ids.filter((id) => !expected.has(id));
546
+ if (new Set(ids).size !== ids.length || missing.length || unexpected.length)
547
+ throw new AppError("review_evidence_invalid", "PLAN reviewer result must assess every assigned aspect exactly once", 2, { work_id: work.work_id, group: group.key, missing, unexpected });
548
+ const findingIds = aspects.flatMap((aspect) => aspect.findings ?? []).map((finding) => finding.finding_id ?? "");
549
+ const invalid = findingIds.filter((id) => !/^FIND-\d{3}$/.test(id));
550
+ if (new Set(findingIds).size !== findingIds.length || invalid.length)
551
+ throw new AppError("review_evidence_invalid", "PLAN reviewer finding ids must be unique local FIND-NNN ids", 2, { work_id: work.work_id, invalid });
552
+ const refs = aspects.flatMap((aspect) => [
553
+ ...(Array.isArray(aspect.evidence_refs) ? aspect.evidence_refs : []),
554
+ ...(aspect.findings ?? []).flatMap((finding) => Array.isArray(finding.evidence_refs) ? finding.evidence_refs : [])
555
+ ]);
556
+ for (const ref of refs) {
557
+ if (typeof ref !== "string")
558
+ throw new AppError("review_evidence_invalid", "PLAN reviewer evidence references must be strings", 2, { work_id: work.work_id, ref });
559
+ assertPortableArtifactRef(ref, input);
560
+ }
561
+ }
461
562
  function codeReviewGroup(work) {
462
563
  const payload = parsePayload(work);
463
564
  const group = payload?.group;
@@ -505,12 +606,14 @@ function validateItem(value, requireExecutionContext = false) { if (!value || ty
505
606
  throw new AppError("validation", "work item must be an object", 2); const item = value; if (typeof item.key !== "string" || !item.key || typeof item.task !== "string" || !item.task.trim())
506
607
  throw new AppError("validation", "work item requires key and task", 2); const code = item.schema_id === "dd-flow/code-work-packet@5"; if (requireExecutionContext && !code)
507
608
  throw new AppError("validation", "CODE batch requires code-work-packet@5 items", 2, { key: item.key }); for (const key of code ? ["required_read", "discovery_boundary", "planned_write_areas", "checks", "provides_checks", "stop_conditions"] : [])
508
- if (!Array.isArray(item[key]) || (!['provides_checks', 'planned_write_areas'].includes(key) && item[key].length === 0))
609
+ if (!Array.isArray(item[key]) || (!['provides_checks', 'planned_write_areas', 'checks'].includes(key) && item[key].length === 0) || (key === "checks" && item[key].length === 0 && !allowsEmptyRepairChecks(item)))
509
610
  throw new AppError("validation", `CODE work item requires ${['provides_checks', 'planned_write_areas'].includes(key) ? "an array" : `non-empty ${key}`}`, 2, { key: item.key }); if (item.depends_on !== undefined && (!Array.isArray(item.depends_on) || !item.depends_on.every((entry) => typeof entry === "string")))
510
611
  throw new AppError("validation", "depends_on must be a string array", 2); if (item.parent !== undefined && typeof item.parent !== "string")
511
612
  throw new AppError("validation", "parent must be a string", 2); if (item.launch_policy !== undefined && item.launch_policy !== "reuse_allowed" && item.launch_policy !== "fresh_agent_required")
512
613
  throw new AppError("validation", "launch_policy must be reuse_allowed or fresh_agent_required", 2); if (item.result_schema !== undefined && (typeof item.result_schema !== "string" || !item.result_schema))
513
614
  throw new AppError("validation", "result_schema must be a non-empty schema id", 2); const payload = code ? item : (item.payload && typeof item.payload === "object" && !Array.isArray(item.payload) ? item.payload : undefined); return { key: item.key, task: item.task, ...(Array.isArray(item.depends_on) ? { depends_on: item.depends_on } : {}), ...(typeof item.parent === "string" ? { parent: item.parent } : {}), ...(typeof item.launch_policy === "string" ? { launch_policy: item.launch_policy } : {}), ...(typeof item.result_schema === "string" ? { result_schema: item.result_schema } : {}), ...(payload ? { payload } : {}) }; }
615
+ function allowsEmptyRepairChecks(item) { const repair = item.repair; if (!repair || typeof repair !== "object" || Array.isArray(repair))
616
+ return false; const value = repair; return typeof value.check_receipt_id === "string" || (Array.isArray(value.review_check_refs) && value.review_check_refs.some((ref) => typeof ref === "string")) || (Array.isArray(value.semantic_unresolved) && value.semantic_unresolved.some((item) => typeof item === "string")); }
514
617
  function readJson(file) { try {
515
618
  return JSON.parse(fs.readFileSync(path.resolve(file), "utf8"));
516
619
  }
@@ -1,10 +1,12 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
+ import crypto from "node:crypto";
3
4
  import { createRequire } from "node:module";
4
5
  import { ensureDir } from "./paths.js";
5
6
  import { AppError } from "../shared/errors.js";
6
7
  const require = createRequire(import.meta.url);
7
8
  const { DatabaseSync } = require("node:sqlite");
9
+ const resourceDatabases = new Map();
8
10
  /** Creates the single Work/Session authority used by a fresh vNext beta runtime. */
9
11
  export function ensureVnextWorkStorage(db) {
10
12
  const legacy = db.get("SELECT name FROM sqlite_master WHERE type = 'table' AND name IN ('vnext_works', 'vnext_agent_turns', 'flow_agent_turns') LIMIT 1");
@@ -94,7 +96,7 @@ export function getDatabase(ddFlowHome, mode = "initialize") {
94
96
  // Internal additive schema changes must be available to every write command.
95
97
  // Higher-level Memory Bank migrations remain explicit in services/migrations.
96
98
  if (mode !== "read_existing")
97
- migrate(db);
99
+ migrate(db, dbPath);
98
100
  if (mode === "read_existing")
99
101
  db.exec("PRAGMA query_only = ON");
100
102
  db.exec("PRAGMA foreign_keys = ON");
@@ -108,6 +110,72 @@ export function getDatabase(ddFlowHome, mode = "initialize") {
108
110
  all: (sql, params = []) => db.prepare(sql).all(...params)
109
111
  };
110
112
  }
113
+ /**
114
+ * Opens the small host-wide resource registry. It is intentionally separate
115
+ * from a RUN database: a process or a port can outlive the CLI client that
116
+ * created it, while a RUN database belongs to one flow home.
117
+ */
118
+ export function getResourceDatabase(resourceHome) {
119
+ ensureDir(resourceHome);
120
+ const dbPath = path.join(resourceHome, "runtime.sqlite");
121
+ const cached = resourceDatabases.get(dbPath);
122
+ if (cached)
123
+ return cached;
124
+ const db = new DatabaseSync(dbPath);
125
+ configureDatabase(db, "initialize");
126
+ db.exec("PRAGMA foreign_keys = ON");
127
+ db.exec(`
128
+ CREATE TABLE IF NOT EXISTS managed_processes (
129
+ id TEXT PRIMARY KEY,
130
+ kind TEXT NOT NULL,
131
+ pid INTEGER,
132
+ pid_started_at TEXT,
133
+ owner_id TEXT NOT NULL,
134
+ lease_token TEXT NOT NULL,
135
+ lease_expires_at TEXT NOT NULL,
136
+ project_id TEXT,
137
+ run_id TEXT,
138
+ work_id TEXT,
139
+ check_id TEXT,
140
+ operation_id TEXT,
141
+ stdout_path TEXT,
142
+ stderr_path TEXT,
143
+ state TEXT NOT NULL,
144
+ started_at TEXT NOT NULL,
145
+ updated_at TEXT NOT NULL,
146
+ finished_at TEXT,
147
+ termination_reason TEXT,
148
+ metadata_json TEXT NOT NULL DEFAULT '{}'
149
+ );
150
+ CREATE INDEX IF NOT EXISTS idx_managed_processes_lease
151
+ ON managed_processes(state, lease_expires_at, updated_at);
152
+ CREATE TABLE IF NOT EXISTS managed_resources (
153
+ resource_kind TEXT NOT NULL,
154
+ resource_key TEXT NOT NULL,
155
+ owner_id TEXT NOT NULL,
156
+ lease_token TEXT NOT NULL,
157
+ lease_expires_at TEXT NOT NULL,
158
+ process_id TEXT,
159
+ metadata_json TEXT NOT NULL DEFAULT '{}',
160
+ created_at TEXT NOT NULL,
161
+ updated_at TEXT NOT NULL,
162
+ PRIMARY KEY(resource_kind, resource_key)
163
+ );
164
+ CREATE INDEX IF NOT EXISTS idx_managed_resources_lease
165
+ ON managed_resources(lease_expires_at, updated_at);
166
+ `);
167
+ const database = {
168
+ path: dbPath,
169
+ writable: true,
170
+ close: () => { resourceDatabases.delete(dbPath); db.close?.(); },
171
+ exec: (sql) => db.exec(sql),
172
+ run: (sql, params = []) => db.prepare(sql).run(...params),
173
+ get: (sql, params = []) => db.prepare(sql).get(...params),
174
+ all: (sql, params = []) => db.prepare(sql).all(...params)
175
+ };
176
+ resourceDatabases.set(dbPath, database);
177
+ return database;
178
+ }
111
179
  function emptyReadOnlyDatabase(dbPath) {
112
180
  return {
113
181
  path: dbPath,
@@ -125,7 +193,7 @@ function configureDatabase(db, mode) {
125
193
  db.exec("PRAGMA synchronous = NORMAL");
126
194
  }
127
195
  }
128
- function migrate(db) {
196
+ function migrate(db, dbPath) {
129
197
  db.exec(`
130
198
  CREATE TABLE IF NOT EXISTS projects (
131
199
  id TEXT PRIMARY KEY,
@@ -351,6 +419,7 @@ function migrate(db) {
351
419
  project_id TEXT NOT NULL,
352
420
  harness TEXT NOT NULL DEFAULT 'codex-desktop',
353
421
  provider_session_id TEXT,
422
+ provider_parent_session_id TEXT,
354
423
  agent_id TEXT,
355
424
  parent_id TEXT,
356
425
  provider TEXT,
@@ -713,6 +782,7 @@ function migrate(db) {
713
782
  ensureColumn(db, "sessions", "metadata_json", "ALTER TABLE sessions ADD COLUMN metadata_json TEXT NOT NULL DEFAULT '{}'");
714
783
  ensureColumn(db, "sessions", "run_id", "ALTER TABLE sessions ADD COLUMN run_id TEXT");
715
784
  ensureColumn(db, "sessions", "provider_session_id", "ALTER TABLE sessions ADD COLUMN provider_session_id TEXT");
785
+ ensureColumn(db, "sessions", "provider_parent_session_id", "ALTER TABLE sessions ADD COLUMN provider_parent_session_id TEXT");
716
786
  ensureColumn(db, "sessions", "harness", "ALTER TABLE sessions ADD COLUMN harness TEXT NOT NULL DEFAULT 'codex-desktop'");
717
787
  ensureColumn(db, "sessions", "agent_id", "ALTER TABLE sessions ADD COLUMN agent_id TEXT");
718
788
  ensureColumn(db, "sessions", "provider", "ALTER TABLE sessions ADD COLUMN provider TEXT");
@@ -727,6 +797,7 @@ function migrate(db) {
727
797
  ensureColumn(db, "sessions", "plan_item_id", "ALTER TABLE sessions ADD COLUMN plan_item_id TEXT");
728
798
  ensureColumn(db, "sessions", "session_kind", "ALTER TABLE sessions ADD COLUMN session_kind TEXT");
729
799
  ensureColumn(db, "sessions", "coverage_units_json", "ALTER TABLE sessions ADD COLUMN coverage_units_json TEXT NOT NULL DEFAULT '[]'");
800
+ migrateSessionStorageKeys(db, dbPath);
730
801
  ensureColumn(db, "usage", "cache_read_input_tokens", "ALTER TABLE usage ADD COLUMN cache_read_input_tokens INTEGER");
731
802
  ensureColumn(db, "usage", "cache_write_input_tokens", "ALTER TABLE usage ADD COLUMN cache_write_input_tokens INTEGER");
732
803
  ensureColumn(db, "usage", "uncached_input_tokens", "ALTER TABLE usage ADD COLUMN uncached_input_tokens INTEGER");
@@ -783,6 +854,58 @@ function migrate(db) {
783
854
  ON runs(short_id);
784
855
  `);
785
856
  }
857
+ /**
858
+ * Older builds used public or `harness:native` strings as foreign keys. Move
859
+ * those keys once to an opaque storage key while preserving the original
860
+ * native value in provider_session_id. We deliberately never split a native
861
+ * value on `:`: rows without a recorded provider ID remain untouched and are
862
+ * reported by normal preflight instead of guessed.
863
+ */
864
+ function migrateSessionStorageKeys(db, dbPath) {
865
+ const rows = db.prepare("SELECT project_id, session_id, harness, provider_session_id, parent_session_id FROM sessions WHERE provider_session_id IS NOT NULL").all();
866
+ const changed = rows.map((row) => ({ ...row, next: storageSessionKey(row.harness, row.provider_session_id) })).filter((row) => row.session_id !== row.next);
867
+ if (changed.length === 0)
868
+ return;
869
+ // This is the only non-additive storage migration. `VACUUM INTO` captures
870
+ // a consistent SQLite snapshot (including WAL state) before keys change.
871
+ if (dbPath && fs.existsSync(dbPath)) {
872
+ const backup = `${dbPath}.pre-session-key-v1-${Date.now()}.sqlite`;
873
+ db.exec(`VACUUM INTO '${backup.replace(/'/g, "''")}'`);
874
+ }
875
+ const duplicate = new Set();
876
+ for (const row of changed) {
877
+ const key = `${row.project_id}\0${row.next}`;
878
+ if (duplicate.has(key) || db.prepare("SELECT 1 FROM sessions WHERE project_id = ? AND session_id = ? AND session_id <> ?").get(row.project_id, row.next, row.session_id)) {
879
+ throw new AppError("session_identity_migration_conflict", "Cannot migrate ambiguous harness/native Session identity", 1, { project_id: row.project_id, harness_id: row.harness, session_id: row.provider_session_id });
880
+ }
881
+ duplicate.add(key);
882
+ }
883
+ db.exec("BEGIN IMMEDIATE");
884
+ try {
885
+ // First update references. Session rows themselves are updated afterwards,
886
+ // so every dependent row remains resolvable throughout the transaction.
887
+ for (const row of changed) {
888
+ db.prepare("UPDATE work_sessions SET session_id = ? WHERE session_id = ? AND work_id IN (SELECT work_id FROM works WHERE project_id = ?)").run(row.next, row.session_id, row.project_id);
889
+ db.prepare("UPDATE flow_session_segments SET session_id = ? WHERE project_id = ? AND session_id = ?").run(row.next, row.project_id, row.session_id);
890
+ db.prepare("UPDATE usage SET session_id = ? WHERE project_id = ? AND session_id = ?").run(row.next, row.project_id, row.session_id);
891
+ const parameters = [row.next, row.project_id, row.session_id];
892
+ db.prepare("UPDATE hook_events SET session_id = ? WHERE project_id = ? AND session_id = ?").run(...parameters);
893
+ db.prepare("UPDATE flow_jobs SET worker_session_id = ? WHERE project_id = ? AND worker_session_id = ?").run(...parameters);
894
+ db.prepare("UPDATE merge_queue SET claimed_by_session_id = ? WHERE project_id = ? AND claimed_by_session_id = ?").run(...parameters);
895
+ db.prepare("UPDATE sessions SET parent_session_id = ? WHERE project_id = ? AND parent_session_id = ?").run(...parameters);
896
+ db.prepare("UPDATE sessions SET session_id = ? WHERE project_id = ? AND session_id = ?").run(...parameters);
897
+ }
898
+ db.exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_sessions_harness_native ON sessions(project_id, harness, provider_session_id) WHERE provider_session_id IS NOT NULL");
899
+ db.exec("COMMIT");
900
+ }
901
+ catch (error) {
902
+ db.exec("ROLLBACK");
903
+ throw error;
904
+ }
905
+ }
906
+ function storageSessionKey(harness, native) {
907
+ return `SES-${crypto.createHash("sha256").update(harness).update("\0").update(native).digest("hex").slice(0, 24)}`;
908
+ }
786
909
  function migrateProjectScopedIdentity(db) {
787
910
  const protocolColumns = db.prepare("PRAGMA table_info(protocols)").all();
788
911
  const legacyGlobalPrimaryKey = protocolColumns.some((column) => column.name === "id" && column.pk === 1)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deksden-com/dd-flow-cli",
3
- "version": "0.9.0-beta.1",
3
+ "version": "0.9.0-beta.7",
4
4
  "description": "Mechanical runtime CLI for dd-flow workflows.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -16,6 +16,15 @@
16
16
  "node": ">=26.0.0",
17
17
  "pnpm": ">=10.0.0"
18
18
  },
19
+ "scripts": {
20
+ "build": "tsc -p tsconfig.build.json && node scripts/generate-build-info.mjs && node scripts/copy-assets.mjs",
21
+ "typecheck": "tsc --noEmit",
22
+ "lint": "eslint . --max-warnings=0",
23
+ "test": "vitest run",
24
+ "changeset": "changeset",
25
+ "version-packages": "changeset version",
26
+ "release": "pnpm run build && changeset publish"
27
+ },
19
28
  "devDependencies": {
20
29
  "@changesets/cli": "^2.31.0",
21
30
  "@eslint/js": "^9.39.1",
@@ -34,14 +43,5 @@
34
43
  "publishConfig": {
35
44
  "access": "public"
36
45
  },
37
- "license": "MIT",
38
- "scripts": {
39
- "build": "tsc -p tsconfig.build.json && node scripts/generate-build-info.mjs && node scripts/copy-assets.mjs",
40
- "typecheck": "tsc --noEmit",
41
- "lint": "eslint . --max-warnings=0",
42
- "test": "vitest run",
43
- "changeset": "changeset",
44
- "version-packages": "changeset version",
45
- "release": "pnpm run build && changeset publish"
46
- }
47
- }
46
+ "license": "MIT"
47
+ }