@devflow-tools/database 0.16.21 → 0.16.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/database.js CHANGED
@@ -11,6 +11,8 @@ const crypto_1 = require("crypto");
11
11
  const obligation_ledger_1 = require("./obligation-ledger");
12
12
  const host_actions_1 = require("./host-actions");
13
13
  const retrieval_sessions_1 = require("./retrieval-sessions");
14
+ const learning_candidates_1 = require("./learning-candidates");
15
+ const workflow_workers_1 = require("./workflow-workers");
14
16
  const CONTEXT_REQUIRED_SKILLS = new Set([
15
17
  'react', 'vue', 'nest', 'nextjs', 'graphql', 'typescript',
16
18
  ]);
@@ -477,6 +479,74 @@ class DevFlowDatabase {
477
479
  CREATE INDEX IF NOT EXISTS idx_session_closures_state
478
480
  ON devflow_session_closures(project_root, state, updated_at DESC);
479
481
 
482
+ CREATE TABLE IF NOT EXISTS devflow_learning_candidates (
483
+ id TEXT PRIMARY KEY,
484
+ project_root TEXT NOT NULL,
485
+ scope TEXT NOT NULL CHECK(scope IN ('project', 'global')),
486
+ state TEXT NOT NULL DEFAULT 'observed'
487
+ CHECK(state IN ('observed', 'candidate', 'shadow', 'evaluated', 'active', 'rejected', 'retired')),
488
+ kind TEXT NOT NULL CHECK(kind IN ('convention', 'workflow', 'tool_preference', 'correction')),
489
+ trigger_json TEXT NOT NULL,
490
+ instruction TEXT NOT NULL,
491
+ confidence REAL NOT NULL CHECK(confidence BETWEEN 0 AND 1),
492
+ overlay_version INTEGER NOT NULL DEFAULT 1 CHECK(overlay_version > 0),
493
+ supporting_sessions INTEGER NOT NULL DEFAULT 0 CHECK(supporting_sessions >= 0),
494
+ successful_outcomes INTEGER NOT NULL DEFAULT 0 CHECK(successful_outcomes >= 0),
495
+ contradictions INTEGER NOT NULL DEFAULT 0 CHECK(contradictions >= 0),
496
+ manual_only INTEGER NOT NULL DEFAULT 0 CHECK(manual_only IN (0, 1)),
497
+ risk TEXT NOT NULL DEFAULT 'low' CHECK(risk IN ('low', 'medium', 'high')),
498
+ grader_receipt TEXT,
499
+ expires_at INTEGER,
500
+ created_at INTEGER NOT NULL,
501
+ updated_at INTEGER NOT NULL
502
+ );
503
+ CREATE INDEX IF NOT EXISTS idx_learning_candidates_project
504
+ ON devflow_learning_candidates(project_root, state, kind, updated_at DESC);
505
+
506
+ CREATE TABLE IF NOT EXISTS devflow_learning_candidate_versions (
507
+ candidate_id TEXT NOT NULL,
508
+ version INTEGER NOT NULL CHECK(version > 0),
509
+ trigger_json TEXT NOT NULL,
510
+ instruction TEXT NOT NULL,
511
+ created_at INTEGER NOT NULL,
512
+ PRIMARY KEY(candidate_id, version)
513
+ );
514
+
515
+ CREATE TABLE IF NOT EXISTS devflow_learning_candidate_evidence (
516
+ id TEXT PRIMARY KEY,
517
+ candidate_id TEXT NOT NULL,
518
+ project_root TEXT NOT NULL,
519
+ session_id TEXT NOT NULL,
520
+ execution_id TEXT,
521
+ memory_observation_id TEXT,
522
+ outcome_id TEXT,
523
+ source_type TEXT NOT NULL
524
+ CHECK(source_type IN ('memory', 'correction', 'workflow_outcome', 'tool_preference', 'convention', 'grader', 'manual')),
525
+ polarity TEXT NOT NULL CHECK(polarity IN ('supporting', 'contradicting')),
526
+ outcome TEXT NOT NULL DEFAULT 'unknown' CHECK(outcome IN ('positive', 'negative', 'unknown')),
527
+ evidence_hash TEXT NOT NULL,
528
+ receipt TEXT,
529
+ payload_json TEXT NOT NULL DEFAULT '{}',
530
+ created_at INTEGER NOT NULL,
531
+ resolved_at INTEGER,
532
+ UNIQUE(candidate_id, evidence_hash)
533
+ );
534
+ CREATE INDEX IF NOT EXISTS idx_learning_evidence_candidate
535
+ ON devflow_learning_candidate_evidence(candidate_id, polarity, resolved_at, created_at);
536
+
537
+ CREATE TABLE IF NOT EXISTS devflow_learning_activation_history (
538
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
539
+ candidate_id TEXT NOT NULL,
540
+ from_state TEXT NOT NULL,
541
+ to_state TEXT NOT NULL,
542
+ reason TEXT NOT NULL,
543
+ grader_receipt TEXT,
544
+ manual_approval INTEGER NOT NULL DEFAULT 0 CHECK(manual_approval IN (0, 1)),
545
+ changed_at INTEGER NOT NULL
546
+ );
547
+ CREATE INDEX IF NOT EXISTS idx_learning_activation_candidate
548
+ ON devflow_learning_activation_history(candidate_id, changed_at DESC);
549
+
480
550
  CREATE TABLE IF NOT EXISTS devflow_host_actions (
481
551
  action_id TEXT PRIMARY KEY,
482
552
  run_id TEXT NOT NULL,
@@ -549,6 +619,85 @@ class DevFlowDatabase {
549
619
  BEGIN
550
620
  SELECT RAISE(ABORT, 'devflow_host_action_events is append-only');
551
621
  END;
622
+
623
+ CREATE TABLE IF NOT EXISTS devflow_workflow_workers (
624
+ worker_id TEXT PRIMARY KEY,
625
+ run_id TEXT NOT NULL,
626
+ step_id TEXT NOT NULL,
627
+ project_root TEXT NOT NULL,
628
+ branch TEXT NOT NULL,
629
+ worktree_path TEXT NOT NULL,
630
+ host TEXT NOT NULL,
631
+ state TEXT NOT NULL DEFAULT 'planned'
632
+ CHECK(state IN ('planned','leased','starting','running','reported','verifying','merge_ready','merged','hold','failed','cancelled')),
633
+ lease_owner TEXT,
634
+ lease_expires_at INTEGER,
635
+ heartbeat_at INTEGER,
636
+ context_receipt TEXT NOT NULL,
637
+ retrieval_session_id TEXT,
638
+ session_id TEXT,
639
+ execution_id TEXT,
640
+ base_revision TEXT NOT NULL,
641
+ final_revision TEXT,
642
+ allowed_files TEXT NOT NULL DEFAULT '[]',
643
+ handoff_json TEXT NOT NULL DEFAULT '{}',
644
+ hold_reason TEXT,
645
+ failure_reason TEXT,
646
+ created_at INTEGER NOT NULL,
647
+ updated_at INTEGER NOT NULL,
648
+ finished_at INTEGER,
649
+ UNIQUE(run_id, step_id),
650
+ UNIQUE(worktree_path),
651
+ CHECK(
652
+ (state IN ('merged','failed','cancelled') AND finished_at IS NOT NULL)
653
+ OR (state NOT IN ('merged','failed','cancelled') AND finished_at IS NULL)
654
+ )
655
+ );
656
+ CREATE INDEX IF NOT EXISTS idx_workflow_workers_project
657
+ ON devflow_workflow_workers(project_root, state, updated_at DESC);
658
+ CREATE INDEX IF NOT EXISTS idx_workflow_workers_lease
659
+ ON devflow_workflow_workers(project_root, state, lease_expires_at);
660
+
661
+ CREATE TABLE IF NOT EXISTS devflow_worker_events (
662
+ event_id TEXT PRIMARY KEY,
663
+ worker_id TEXT NOT NULL,
664
+ project_root TEXT NOT NULL,
665
+ from_state TEXT,
666
+ to_state TEXT NOT NULL,
667
+ reason TEXT,
668
+ payload_json TEXT NOT NULL DEFAULT '{}',
669
+ created_at INTEGER NOT NULL
670
+ );
671
+ CREATE INDEX IF NOT EXISTS idx_worker_events_worker
672
+ ON devflow_worker_events(worker_id, created_at, event_id);
673
+ CREATE TRIGGER IF NOT EXISTS prevent_worker_event_update
674
+ BEFORE UPDATE ON devflow_worker_events BEGIN
675
+ SELECT RAISE(ABORT, 'devflow_worker_events is append-only');
676
+ END;
677
+ CREATE TRIGGER IF NOT EXISTS prevent_worker_event_delete
678
+ BEFORE DELETE ON devflow_worker_events BEGIN
679
+ SELECT RAISE(ABORT, 'devflow_worker_events is append-only');
680
+ END;
681
+
682
+ CREATE TABLE IF NOT EXISTS devflow_merge_queue (
683
+ merge_id TEXT PRIMARY KEY,
684
+ worker_id TEXT NOT NULL UNIQUE,
685
+ project_root TEXT NOT NULL,
686
+ state TEXT NOT NULL DEFAULT 'waiting'
687
+ CHECK(state IN ('waiting','ready','conflict','merged','rejected')),
688
+ base_revision TEXT NOT NULL,
689
+ parent_revision TEXT NOT NULL,
690
+ candidate_revision TEXT NOT NULL,
691
+ grader_receipts TEXT NOT NULL DEFAULT '[]',
692
+ simulation_hash TEXT,
693
+ merge_commit TEXT,
694
+ reason TEXT,
695
+ created_at INTEGER NOT NULL,
696
+ updated_at INTEGER NOT NULL,
697
+ finished_at INTEGER
698
+ );
699
+ CREATE INDEX IF NOT EXISTS idx_merge_queue_project
700
+ ON devflow_merge_queue(project_root, state, created_at, merge_id);
552
701
  `);
553
702
  // Migration: add session_id to skill_executions (SQLite compat — ignore if exists)
554
703
  try {
@@ -1815,6 +1964,245 @@ class DevFlowDatabase {
1815
1964
  total: Number(count?.total ?? 0),
1816
1965
  };
1817
1966
  }
1967
+ // ---- Governed Learning ----
1968
+ upsertLearningCandidate(input) {
1969
+ if (!input.id.trim())
1970
+ throw new Error('Learning candidate requires an ID');
1971
+ if (!input.projectRoot.trim())
1972
+ throw new Error('Learning candidate requires a project root');
1973
+ if (!input.instruction.trim())
1974
+ throw new Error('Learning candidate requires an instruction');
1975
+ if (!Number.isFinite(input.confidence) || input.confidence < 0 || input.confidence > 1) {
1976
+ throw new Error('Learning candidate confidence must be between 0 and 1');
1977
+ }
1978
+ return this.db.transaction(() => {
1979
+ const now = Date.now();
1980
+ const existing = this.getLearningCandidate(input.id);
1981
+ const triggerJson = (0, learning_candidates_1.stableLearningJson)(input.trigger);
1982
+ const instruction = input.instruction.trim().slice(0, 2000);
1983
+ if (!existing) {
1984
+ this.db.prepare(`
1985
+ INSERT INTO devflow_learning_candidates (
1986
+ id, project_root, scope, state, kind, trigger_json, instruction,
1987
+ confidence, overlay_version, supporting_sessions, successful_outcomes,
1988
+ contradictions, manual_only, risk, expires_at, created_at, updated_at
1989
+ ) VALUES (?, ?, ?, 'observed', ?, ?, ?, ?, 1, 0, 0, 0, ?, ?, ?, ?, ?)
1990
+ `).run(input.id, input.projectRoot, input.scope, input.kind, triggerJson, instruction, input.confidence, input.manualOnly ? 1 : 0, input.risk ?? 'low', input.expiresAt ?? null, now, now);
1991
+ this.insertLearningCandidateVersion(input.id, 1, triggerJson, instruction, now);
1992
+ return this.getLearningCandidate(input.id);
1993
+ }
1994
+ if (existing.projectRoot !== input.projectRoot || existing.scope !== input.scope || existing.kind !== input.kind) {
1995
+ throw new Error(`Learning candidate identity mismatch for ${input.id}`);
1996
+ }
1997
+ const contentChanged = (0, learning_candidates_1.stableLearningJson)(existing.trigger) !== triggerJson
1998
+ || existing.instruction !== instruction;
1999
+ const nextVersion = contentChanged ? existing.overlayVersion + 1 : existing.overlayVersion;
2000
+ const nextState = contentChanged && (existing.state === 'active' || existing.state === 'evaluated')
2001
+ ? 'shadow'
2002
+ : existing.state;
2003
+ this.db.prepare(`
2004
+ UPDATE devflow_learning_candidates
2005
+ SET trigger_json = ?, instruction = ?, confidence = ?, overlay_version = ?,
2006
+ state = ?, manual_only = MAX(manual_only, ?),
2007
+ risk = CASE
2008
+ WHEN risk = 'high' OR ? = 'high' THEN 'high'
2009
+ WHEN risk = 'medium' OR ? = 'medium' THEN 'medium'
2010
+ ELSE 'low'
2011
+ END,
2012
+ expires_at = ?, updated_at = ?
2013
+ WHERE id = ?
2014
+ `).run(triggerJson, instruction, Math.max(existing.confidence, input.confidence), nextVersion, nextState, input.manualOnly ? 1 : 0, input.risk ?? 'low', input.risk ?? 'low', input.expiresAt ?? existing.expiresAt ?? null, now, input.id);
2015
+ if (contentChanged) {
2016
+ this.insertLearningCandidateVersion(input.id, nextVersion, triggerJson, instruction, now);
2017
+ if (nextState !== existing.state) {
2018
+ this.insertLearningActivation(input.id, existing.state, nextState, 'candidate_content_changed', undefined, false, now);
2019
+ }
2020
+ }
2021
+ return this.getLearningCandidate(input.id);
2022
+ });
2023
+ }
2024
+ getLearningCandidate(id) {
2025
+ const row = this.db.prepare('SELECT * FROM devflow_learning_candidates WHERE id = ?')
2026
+ .get(id);
2027
+ return row ? (0, learning_candidates_1.mapLearningCandidateRow)(row) : null;
2028
+ }
2029
+ listLearningCandidates(options = {}) {
2030
+ const predicates = [];
2031
+ const params = [];
2032
+ if (options.projectRoot) {
2033
+ predicates.push('(project_root = ? OR scope = \'global\')');
2034
+ params.push(options.projectRoot);
2035
+ }
2036
+ const states = [...new Set(options.states ?? [])];
2037
+ if (states.length > 0) {
2038
+ predicates.push(`state IN (${states.map(() => '?').join(',')})`);
2039
+ params.push(...states);
2040
+ }
2041
+ const rows = this.db.prepare(`
2042
+ SELECT * FROM devflow_learning_candidates
2043
+ ${predicates.length > 0 ? `WHERE ${predicates.join(' AND ')}` : ''}
2044
+ ORDER BY updated_at DESC, id ASC
2045
+ LIMIT ?
2046
+ `).all(...params, Math.max(1, Math.min(options.limit ?? 200, 2000)));
2047
+ return rows.map(learning_candidates_1.mapLearningCandidateRow);
2048
+ }
2049
+ addLearningCandidateEvidence(input) {
2050
+ if (!input.evidenceHash.trim())
2051
+ throw new Error('Learning evidence requires a hash');
2052
+ if (!input.sessionId.trim())
2053
+ throw new Error('Learning evidence requires a session ID');
2054
+ return this.db.transaction(() => {
2055
+ const candidate = this.getLearningCandidate(input.candidateId);
2056
+ if (!candidate)
2057
+ throw new Error(`Learning candidate ${input.candidateId} does not exist`);
2058
+ if (candidate.projectRoot !== input.projectRoot && candidate.scope !== 'global') {
2059
+ throw new Error(`Learning evidence project mismatch for ${input.candidateId}`);
2060
+ }
2061
+ const createdAt = input.createdAt ?? Date.now();
2062
+ const inserted = this.db.prepare(`
2063
+ INSERT OR IGNORE INTO devflow_learning_candidate_evidence (
2064
+ id, candidate_id, project_root, session_id, execution_id,
2065
+ memory_observation_id, outcome_id, source_type, polarity, outcome,
2066
+ evidence_hash, receipt, payload_json, created_at
2067
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2068
+ `).run(input.id, input.candidateId, input.projectRoot, input.sessionId, input.executionId ?? null, input.memoryObservationId ?? null, input.outcomeId ?? null, input.sourceType, input.polarity, input.outcome ?? 'unknown', input.evidenceHash, input.receipt ?? null, (0, learning_candidates_1.stableLearningJson)(input.payload ?? {}), createdAt).changes === 1;
2069
+ if (!inserted)
2070
+ return candidate;
2071
+ const previousState = candidate.state;
2072
+ const aggregates = this.getLearningEvidenceAggregates(input.candidateId);
2073
+ let state = previousState;
2074
+ if (state === 'observed' && aggregates.supportingSessions > 0)
2075
+ state = 'candidate';
2076
+ if (aggregates.contradictions > 0 && (state === 'active' || state === 'evaluated'))
2077
+ state = 'shadow';
2078
+ this.db.prepare(`
2079
+ UPDATE devflow_learning_candidates
2080
+ SET state = ?, supporting_sessions = ?, successful_outcomes = ?,
2081
+ contradictions = ?, updated_at = ?
2082
+ WHERE id = ?
2083
+ `).run(state, aggregates.supportingSessions, aggregates.successfulOutcomes, aggregates.contradictions, createdAt, input.candidateId);
2084
+ if (state !== previousState) {
2085
+ this.insertLearningActivation(input.candidateId, previousState, state, aggregates.contradictions > 0 ? 'unresolved_contradiction' : 'first_supporting_evidence', undefined, false, createdAt);
2086
+ }
2087
+ return this.getLearningCandidate(input.candidateId);
2088
+ });
2089
+ }
2090
+ listLearningCandidateEvidence(candidateId) {
2091
+ const rows = this.db.prepare(`
2092
+ SELECT * FROM devflow_learning_candidate_evidence
2093
+ WHERE candidate_id = ? ORDER BY created_at ASC, id ASC
2094
+ `).all(candidateId);
2095
+ return rows.map(learning_candidates_1.mapLearningEvidenceRow);
2096
+ }
2097
+ resolveLearningContradiction(candidateId, evidenceId, resolvedAt = Date.now()) {
2098
+ return this.db.transaction(() => {
2099
+ this.db.prepare(`
2100
+ UPDATE devflow_learning_candidate_evidence
2101
+ SET resolved_at = ?
2102
+ WHERE id = ? AND candidate_id = ? AND polarity = 'contradicting' AND resolved_at IS NULL
2103
+ `).run(resolvedAt, evidenceId, candidateId);
2104
+ const aggregates = this.getLearningEvidenceAggregates(candidateId);
2105
+ this.db.prepare(`
2106
+ UPDATE devflow_learning_candidates
2107
+ SET supporting_sessions = ?, successful_outcomes = ?, contradictions = ?, updated_at = ?
2108
+ WHERE id = ?
2109
+ `).run(aggregates.supportingSessions, aggregates.successfulOutcomes, aggregates.contradictions, resolvedAt, candidateId);
2110
+ const candidate = this.getLearningCandidate(candidateId);
2111
+ if (!candidate)
2112
+ throw new Error(`Learning candidate ${candidateId} does not exist`);
2113
+ return candidate;
2114
+ });
2115
+ }
2116
+ transitionLearningCandidate(candidateId, input) {
2117
+ return this.db.transaction(() => {
2118
+ const candidate = this.getLearningCandidate(candidateId);
2119
+ if (!candidate)
2120
+ throw new Error(`Learning candidate ${candidateId} does not exist`);
2121
+ if (candidate.state === input.target)
2122
+ return candidate;
2123
+ this.assertLearningTransition(candidate, input);
2124
+ const changedAt = input.changedAt ?? Date.now();
2125
+ const graderReceipt = input.graderReceipt ?? candidate.graderReceipt;
2126
+ this.db.prepare(`
2127
+ UPDATE devflow_learning_candidates
2128
+ SET state = ?, grader_receipt = COALESCE(?, grader_receipt), updated_at = ?
2129
+ WHERE id = ?
2130
+ `).run(input.target, graderReceipt ?? null, changedAt, candidateId);
2131
+ this.insertLearningActivation(candidateId, candidate.state, input.target, input.reason, graderReceipt, input.manualApproval === true, changedAt);
2132
+ return this.getLearningCandidate(candidateId);
2133
+ });
2134
+ }
2135
+ listLearningCandidateVersions(candidateId) {
2136
+ const rows = this.db.prepare(`
2137
+ SELECT * FROM devflow_learning_candidate_versions
2138
+ WHERE candidate_id = ? ORDER BY version ASC
2139
+ `).all(candidateId);
2140
+ return rows.map(learning_candidates_1.mapLearningVersionRow);
2141
+ }
2142
+ assertLearningTransition(candidate, input) {
2143
+ const terminal = input.target === 'rejected' || input.target === 'retired';
2144
+ const rollback = input.target === 'shadow'
2145
+ && (candidate.state === 'active' || candidate.state === 'evaluated');
2146
+ const legal = {
2147
+ observed: ['candidate', 'rejected', 'retired'],
2148
+ candidate: ['shadow', 'rejected', 'retired'],
2149
+ shadow: ['evaluated', 'rejected', 'retired'],
2150
+ evaluated: ['active', 'shadow', 'rejected', 'retired'],
2151
+ active: ['shadow', 'retired'],
2152
+ rejected: ['retired'],
2153
+ retired: [],
2154
+ };
2155
+ if (!terminal && !rollback && !legal[candidate.state].includes(input.target)) {
2156
+ throw new Error(`Illegal learning transition ${candidate.state} -> ${input.target}`);
2157
+ }
2158
+ if (input.target === 'shadow') {
2159
+ if (candidate.supportingSessions < 3 || candidate.successfulOutcomes < 2) {
2160
+ throw new Error('Learning candidate requires three sessions and two verified outcomes before shadow');
2161
+ }
2162
+ }
2163
+ if (input.target === 'evaluated' && candidate.contradictions > 0) {
2164
+ throw new Error('Learning candidate has unresolved contradictions');
2165
+ }
2166
+ if (input.target === 'active') {
2167
+ const receipt = input.graderReceipt ?? candidate.graderReceipt;
2168
+ if (!receipt)
2169
+ throw new Error('Learning candidate activation requires a grader receipt');
2170
+ if (candidate.contradictions > 0)
2171
+ throw new Error('Learning candidate has unresolved contradictions');
2172
+ const manualRequired = candidate.scope === 'global' || candidate.manualOnly || candidate.risk === 'high';
2173
+ if (manualRequired && input.manualApproval !== true) {
2174
+ throw new Error('Learning candidate requires manual approval');
2175
+ }
2176
+ }
2177
+ }
2178
+ getLearningEvidenceAggregates(candidateId) {
2179
+ const row = this.db.prepare(`
2180
+ SELECT
2181
+ COUNT(DISTINCT CASE WHEN polarity = 'supporting' THEN session_id END) AS supporting_sessions,
2182
+ COUNT(DISTINCT CASE WHEN polarity = 'supporting' AND outcome = 'positive' THEN session_id END) AS successful_outcomes,
2183
+ SUM(CASE WHEN polarity = 'contradicting' AND resolved_at IS NULL THEN 1 ELSE 0 END) AS contradictions
2184
+ FROM devflow_learning_candidate_evidence WHERE candidate_id = ?
2185
+ `).get(candidateId);
2186
+ return {
2187
+ supportingSessions: Number(row?.supporting_sessions ?? 0),
2188
+ successfulOutcomes: Number(row?.successful_outcomes ?? 0),
2189
+ contradictions: Number(row?.contradictions ?? 0),
2190
+ };
2191
+ }
2192
+ insertLearningCandidateVersion(candidateId, version, triggerJson, instruction, createdAt) {
2193
+ this.db.prepare(`
2194
+ INSERT OR IGNORE INTO devflow_learning_candidate_versions
2195
+ (candidate_id, version, trigger_json, instruction, created_at)
2196
+ VALUES (?, ?, ?, ?, ?)
2197
+ `).run(candidateId, version, triggerJson, instruction, createdAt);
2198
+ }
2199
+ insertLearningActivation(candidateId, fromState, toState, reason, graderReceipt, manualApproval, changedAt) {
2200
+ this.db.prepare(`
2201
+ INSERT INTO devflow_learning_activation_history
2202
+ (candidate_id, from_state, to_state, reason, grader_receipt, manual_approval, changed_at)
2203
+ VALUES (?, ?, ?, ?, ?, ?, ?)
2204
+ `).run(candidateId, fromState, toState, reason.trim().slice(0, 500), graderReceipt ?? null, manualApproval ? 1 : 0, changedAt);
2205
+ }
1818
2206
  // ---- Durable Work Queue ----
1819
2207
  enqueueWork(input) {
1820
2208
  if (!input.idempotencyKey.trim())
@@ -2032,6 +2420,15 @@ class DevFlowDatabase {
2032
2420
  }
2033
2421
  return completed;
2034
2422
  }
2423
+ heartbeatWork(id, owner, leaseMs, now = Date.now()) {
2424
+ if (!Number.isSafeInteger(leaseMs) || leaseMs <= 0)
2425
+ return false;
2426
+ return this.db.prepare(`
2427
+ UPDATE devflow_work_items
2428
+ SET lease_expires_at = ?, updated_at = ?
2429
+ WHERE id = ? AND state = 'leased' AND lease_owner = ?
2430
+ `).run(now + leaseMs, now, id, owner).changes === 1;
2431
+ }
2035
2432
  retryWork(id, owner, error, nextAttemptAt) {
2036
2433
  const updatedAt = Date.now();
2037
2434
  return this.db.prepare(`
@@ -2998,6 +3395,232 @@ class DevFlowDatabase {
2998
3395
  }
2999
3396
  return false;
3000
3397
  }
3398
+ // ---- Isolated Workflow Workers ----
3399
+ createWorkflowWorker(input) {
3400
+ for (const [name, value] of [
3401
+ ['run ID', input.runId],
3402
+ ['step ID', input.stepId],
3403
+ ['project root', input.projectRoot],
3404
+ ['branch', input.branch],
3405
+ ['worktree path', input.worktreePath],
3406
+ ['host', input.host],
3407
+ ['context receipt', input.contextReceipt],
3408
+ ['base revision', input.baseRevision],
3409
+ ]) {
3410
+ if (!value.trim())
3411
+ throw new Error(`Workflow worker requires a ${name}`);
3412
+ }
3413
+ const workerId = input.workerId ?? (0, crypto_1.randomUUID)();
3414
+ const now = Date.now();
3415
+ return this.withImmediateTransaction(() => {
3416
+ this.db.prepare(`
3417
+ INSERT INTO devflow_workflow_workers (
3418
+ worker_id, run_id, step_id, project_root, branch, worktree_path, host,
3419
+ state, context_receipt, retrieval_session_id, session_id, execution_id,
3420
+ base_revision, allowed_files, handoff_json, created_at, updated_at
3421
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, 'planned', ?, ?, ?, ?, ?, ?, ?, ?, ?)
3422
+ ON CONFLICT DO NOTHING
3423
+ `).run(workerId, input.runId, input.stepId, input.projectRoot, input.branch, input.worktreePath, input.host, input.contextReceipt, input.retrievalSessionId ?? null, input.sessionId ?? null, input.executionId ?? null, input.baseRevision, JSON.stringify([...new Set(input.allowedFiles ?? [])].sort()), JSON.stringify(input.handoff ?? {}), now, now);
3424
+ const rows = this.db.prepare(`
3425
+ SELECT * FROM devflow_workflow_workers
3426
+ WHERE worker_id = ? OR (run_id = ? AND step_id = ?)
3427
+ `).all(workerId, input.runId, input.stepId);
3428
+ if (rows.length !== 1)
3429
+ throw new Error(`WORKFLOW_WORKER_IDENTITY_CONFLICT:${input.runId}:${input.stepId}`);
3430
+ const record = (0, workflow_workers_1.mapWorkflowWorkerRow)(rows[0]);
3431
+ if (record.workerId !== workerId
3432
+ || record.projectRoot !== input.projectRoot
3433
+ || record.worktreePath !== input.worktreePath
3434
+ || record.contextReceipt !== input.contextReceipt) {
3435
+ throw new Error(`WORKFLOW_WORKER_IDENTITY_CONFLICT:${input.runId}:${input.stepId}`);
3436
+ }
3437
+ if (record.createdAt === now)
3438
+ this.appendWorkflowWorkerEvent(record, undefined, 'planned', undefined, {}, now);
3439
+ return record;
3440
+ });
3441
+ }
3442
+ getWorkflowWorker(projectRoot, workerId) {
3443
+ const row = this.db.prepare(`
3444
+ SELECT * FROM devflow_workflow_workers WHERE project_root = ? AND worker_id = ?
3445
+ `).get(projectRoot, workerId);
3446
+ return row ? (0, workflow_workers_1.mapWorkflowWorkerRow)(row) : null;
3447
+ }
3448
+ listWorkflowWorkers(projectRoot, options = {}) {
3449
+ const conditions = ['project_root = ?'];
3450
+ const values = [projectRoot];
3451
+ if (options.runId) {
3452
+ conditions.push('run_id = ?');
3453
+ values.push(options.runId);
3454
+ }
3455
+ if (options.states?.length) {
3456
+ conditions.push(`state IN (${options.states.map(() => '?').join(',')})`);
3457
+ values.push(...options.states);
3458
+ }
3459
+ values.push(Math.max(1, Math.min(options.limit ?? 200, 2000)));
3460
+ const rows = this.db.prepare(`
3461
+ SELECT * FROM devflow_workflow_workers
3462
+ WHERE ${conditions.join(' AND ')}
3463
+ ORDER BY updated_at DESC, worker_id ASC LIMIT ?
3464
+ `).all(...values);
3465
+ return rows.map(workflow_workers_1.mapWorkflowWorkerRow);
3466
+ }
3467
+ listWorkflowWorkerEvents(projectRoot, workerId) {
3468
+ return this.db.prepare(`
3469
+ SELECT * FROM devflow_worker_events
3470
+ WHERE project_root = ? AND worker_id = ?
3471
+ ORDER BY created_at ASC, event_id ASC
3472
+ `).all(projectRoot, workerId).map(workflow_workers_1.mapWorkflowWorkerEventRow);
3473
+ }
3474
+ leaseWorkflowWorker(projectRoot, workerId, owner, leaseMs, now = Date.now()) {
3475
+ if (!owner.trim())
3476
+ throw new Error('Workflow worker lease owner is required');
3477
+ if (!Number.isSafeInteger(leaseMs) || leaseMs <= 0)
3478
+ throw new Error('Workflow worker lease must be positive');
3479
+ return this.transitionWorkflowWorker({
3480
+ projectRoot,
3481
+ workerId,
3482
+ target: 'leased',
3483
+ leaseOwner: owner,
3484
+ leaseExpiresAt: now + leaseMs,
3485
+ heartbeatAt: now,
3486
+ now,
3487
+ });
3488
+ }
3489
+ heartbeatWorkflowWorker(projectRoot, workerId, owner, leaseMs, now = Date.now()) {
3490
+ const result = this.db.prepare(`
3491
+ UPDATE devflow_workflow_workers
3492
+ SET heartbeat_at = ?, lease_expires_at = ?, updated_at = ?
3493
+ WHERE project_root = ? AND worker_id = ? AND lease_owner = ?
3494
+ AND state IN ('leased','starting','running','reported','verifying','merge_ready')
3495
+ `).run(now, now + leaseMs, now, projectRoot, workerId, owner);
3496
+ if (result.changes !== 1)
3497
+ throw new Error(`WORKFLOW_WORKER_HEARTBEAT_REJECTED:${workerId}`);
3498
+ return this.getWorkflowWorker(projectRoot, workerId);
3499
+ }
3500
+ transitionWorkflowWorker(input) {
3501
+ return this.withImmediateTransaction(() => {
3502
+ const existing = this.getWorkflowWorker(input.projectRoot, input.workerId);
3503
+ if (!existing)
3504
+ throw new Error(`WORKFLOW_WORKER_NOT_FOUND:${input.workerId}`);
3505
+ if (workflow_workers_1.TERMINAL_WORKFLOW_WORKER_STATES.has(existing.state)) {
3506
+ if (existing.state === input.target)
3507
+ return existing;
3508
+ throw new Error(`WORKFLOW_WORKER_TERMINAL_CONFLICT:${input.workerId}`);
3509
+ }
3510
+ if (!workflow_workers_1.LEGAL_WORKFLOW_WORKER_TRANSITIONS[existing.state].includes(input.target)) {
3511
+ throw new Error(`WORKFLOW_WORKER_INVALID_TRANSITION:${existing.state}->${input.target}`);
3512
+ }
3513
+ if (existing.leaseOwner && input.leaseOwner && existing.leaseOwner !== input.leaseOwner) {
3514
+ throw new Error(`WORKFLOW_WORKER_LEASE_CONFLICT:${input.workerId}`);
3515
+ }
3516
+ const now = input.now ?? Date.now();
3517
+ const terminal = workflow_workers_1.TERMINAL_WORKFLOW_WORKER_STATES.has(input.target);
3518
+ const clearLease = terminal || input.target === 'hold';
3519
+ const reason = input.reason?.trim().slice(0, 1000);
3520
+ const result = this.db.prepare(`
3521
+ UPDATE devflow_workflow_workers SET
3522
+ state = ?,
3523
+ lease_owner = CASE WHEN ? THEN NULL ELSE COALESCE(?, lease_owner) END,
3524
+ lease_expires_at = CASE WHEN ? THEN NULL ELSE COALESCE(?, lease_expires_at) END,
3525
+ heartbeat_at = COALESCE(?, heartbeat_at),
3526
+ final_revision = COALESCE(?, final_revision),
3527
+ handoff_json = CASE WHEN ? IS NULL THEN handoff_json ELSE ? END,
3528
+ hold_reason = CASE WHEN ? = 'hold' THEN ? ELSE hold_reason END,
3529
+ failure_reason = CASE WHEN ? = 'failed' THEN ? ELSE failure_reason END,
3530
+ updated_at = ?, finished_at = ?
3531
+ WHERE project_root = ? AND worker_id = ? AND state = ?
3532
+ `).run(input.target, clearLease ? 1 : 0, input.leaseOwner ?? null, clearLease ? 1 : 0, input.leaseExpiresAt ?? null, input.heartbeatAt ?? null, input.finalRevision ?? null, input.handoff === undefined ? null : 1, JSON.stringify(input.handoff ?? {}), input.target, reason ?? null, input.target, reason ?? null, now, terminal ? now : null, input.projectRoot, input.workerId, existing.state);
3533
+ if (result.changes !== 1)
3534
+ throw new Error(`WORKFLOW_WORKER_UPDATE_CONFLICT:${input.workerId}`);
3535
+ const updated = this.getWorkflowWorker(input.projectRoot, input.workerId);
3536
+ this.appendWorkflowWorkerEvent(updated, existing.state, input.target, reason, input.handoff ?? {}, now);
3537
+ return updated;
3538
+ });
3539
+ }
3540
+ recoverExpiredWorkflowWorkers(projectRoot, now = Date.now()) {
3541
+ const expired = this.listWorkflowWorkers(projectRoot, {
3542
+ states: ['leased', 'starting', 'running', 'reported', 'verifying', 'merge_ready'],
3543
+ limit: 2000,
3544
+ }).filter(worker => worker.leaseExpiresAt !== undefined && worker.leaseExpiresAt <= now);
3545
+ return expired.map(worker => this.transitionWorkflowWorker({
3546
+ projectRoot,
3547
+ workerId: worker.workerId,
3548
+ target: 'hold',
3549
+ leaseOwner: worker.leaseOwner,
3550
+ reason: 'lease_expired_dirty_worktree_requires_inspection',
3551
+ now,
3552
+ }));
3553
+ }
3554
+ enqueueWorkflowMerge(input) {
3555
+ const now = Date.now();
3556
+ const mergeId = input.mergeId ?? (0, crypto_1.randomUUID)();
3557
+ this.db.prepare(`
3558
+ INSERT INTO devflow_merge_queue (
3559
+ merge_id, worker_id, project_root, state, base_revision, parent_revision,
3560
+ candidate_revision, grader_receipts, simulation_hash, created_at, updated_at
3561
+ ) VALUES (?, ?, ?, 'waiting', ?, ?, ?, ?, ?, ?, ?)
3562
+ ON CONFLICT(worker_id) DO NOTHING
3563
+ `).run(mergeId, input.workerId, input.projectRoot, input.baseRevision, input.parentRevision, input.candidateRevision, JSON.stringify([...new Set(input.graderReceipts ?? [])].sort()), input.simulationHash ?? null, now, now);
3564
+ const record = this.getWorkflowMergeByWorker(input.projectRoot, input.workerId);
3565
+ if (!record
3566
+ || record.baseRevision !== input.baseRevision
3567
+ || record.candidateRevision !== input.candidateRevision) {
3568
+ throw new Error(`WORKFLOW_MERGE_IDENTITY_CONFLICT:${input.workerId}`);
3569
+ }
3570
+ return record;
3571
+ }
3572
+ getWorkflowMerge(projectRoot, mergeId) {
3573
+ const row = this.db.prepare(`
3574
+ SELECT * FROM devflow_merge_queue WHERE project_root = ? AND merge_id = ?
3575
+ `).get(projectRoot, mergeId);
3576
+ return row ? (0, workflow_workers_1.mapWorkflowMergeRow)(row) : null;
3577
+ }
3578
+ getWorkflowMergeByWorker(projectRoot, workerId) {
3579
+ const row = this.db.prepare(`
3580
+ SELECT * FROM devflow_merge_queue WHERE project_root = ? AND worker_id = ?
3581
+ `).get(projectRoot, workerId);
3582
+ return row ? (0, workflow_workers_1.mapWorkflowMergeRow)(row) : null;
3583
+ }
3584
+ listWorkflowMerges(projectRoot, limit = 200) {
3585
+ return this.db.prepare(`
3586
+ SELECT * FROM devflow_merge_queue WHERE project_root = ?
3587
+ ORDER BY created_at ASC, merge_id ASC LIMIT ?
3588
+ `).all(projectRoot, Math.max(1, Math.min(limit, 2000))).map(workflow_workers_1.mapWorkflowMergeRow);
3589
+ }
3590
+ transitionWorkflowMerge(projectRoot, mergeId, target, update = {}, now = Date.now()) {
3591
+ const existing = this.getWorkflowMerge(projectRoot, mergeId);
3592
+ if (!existing)
3593
+ throw new Error(`WORKFLOW_MERGE_NOT_FOUND:${mergeId}`);
3594
+ const legal = {
3595
+ waiting: ['ready', 'conflict', 'rejected'],
3596
+ ready: ['conflict', 'merged', 'rejected'],
3597
+ conflict: ['waiting', 'rejected'],
3598
+ merged: [],
3599
+ rejected: [],
3600
+ };
3601
+ if (!legal[existing.state].includes(target)) {
3602
+ if (existing.state === target)
3603
+ return existing;
3604
+ throw new Error(`WORKFLOW_MERGE_INVALID_TRANSITION:${existing.state}->${target}`);
3605
+ }
3606
+ const terminal = target === 'merged' || target === 'rejected';
3607
+ const result = this.db.prepare(`
3608
+ UPDATE devflow_merge_queue SET state = ?, parent_revision = COALESCE(?, parent_revision),
3609
+ simulation_hash = COALESCE(?, simulation_hash), merge_commit = COALESCE(?, merge_commit),
3610
+ reason = COALESCE(?, reason), updated_at = ?, finished_at = ?
3611
+ WHERE project_root = ? AND merge_id = ? AND state = ?
3612
+ `).run(target, update.parentRevision ?? null, update.simulationHash ?? null, update.mergeCommit ?? null, update.reason?.trim().slice(0, 1000) ?? null, now, terminal ? now : null, projectRoot, mergeId, existing.state);
3613
+ if (result.changes !== 1)
3614
+ throw new Error(`WORKFLOW_MERGE_UPDATE_CONFLICT:${mergeId}`);
3615
+ return this.getWorkflowMerge(projectRoot, mergeId);
3616
+ }
3617
+ appendWorkflowWorkerEvent(record, fromState, toState, reason, payload, createdAt) {
3618
+ this.db.prepare(`
3619
+ INSERT INTO devflow_worker_events (
3620
+ event_id, worker_id, project_root, from_state, to_state, reason, payload_json, created_at
3621
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
3622
+ `).run((0, crypto_1.randomUUID)(), record.workerId, record.projectRoot, fromState ?? null, toState, reason ?? null, JSON.stringify(payload), createdAt);
3623
+ }
3001
3624
  // Convenience methods for raw SQL queries (used by AutoChecker)
3002
3625
  all(sql, ...params) {
3003
3626
  return this.db.prepare(sql).all(...params);
package/dist/index.d.ts CHANGED
@@ -6,4 +6,8 @@ export { RETRIEVAL_MAX_CYCLES, isRetrievalSessionState } from './retrieval-sessi
6
6
  export type { AppendRetrievalCycleInput, CreateRetrievalSessionInput, RetrievalCycleRecord, RetrievalGapKind, RetrievalGapRecord, RetrievalSessionRecord, RetrievalSessionState, } from './retrieval-sessions';
7
7
  export type { FailHostActionInput, HostActionRecord, HostActionState, ReportHostActionInput, RequestHostActionInput, StartHostActionInput, VerifyHostActionInput, } from './host-actions';
8
8
  export { mapSessionObligationRow, normalizeTurnId, } from './obligation-ledger';
9
+ export { LEGAL_WORKFLOW_WORKER_TRANSITIONS, TERMINAL_WORKFLOW_WORKER_STATES, mapWorkflowMergeRow, mapWorkflowWorkerEventRow, mapWorkflowWorkerRow, } from './workflow-workers';
10
+ export type { CreateWorkflowWorkerInput, EnqueueWorkflowMergeInput, TransitionWorkflowWorkerInput, WorkflowMergeRecord, WorkflowMergeState, WorkflowWorkerEventRecord, WorkflowWorkerRecord, WorkflowWorkerState, } from './workflow-workers';
11
+ export { mapLearningCandidateRow, mapLearningEvidenceRow, mapLearningVersionRow, stableLearningJson, } from './learning-candidates';
12
+ export type { AddLearningCandidateEvidenceInput, LearningCandidateEvidenceRecord, LearningCandidateKind, LearningCandidateRecord, LearningCandidateState, LearningCandidateVersionRecord, LearningEvidencePolarity, LearningOutcomeState, TransitionLearningCandidateInput, UpsertLearningCandidateInput, } from './learning-candidates';
9
13
  export type { SessionObligationKind, SessionObligationRecord, SessionObligationState, } from './obligation-ledger';