Read the script
/**
* Forge Intelligence — CRUD Operations
*
* Create/park/resume/complete/abandon forges.
* Record gates, spikes. Aggregate risk. Update co-failures.
*/
import { openForgeDb } from "./forge-schema.mjs";
// ── DB helper ──────────────────────────────────────────
/** Open db, run fn(db), close unconditionally. Returns fn's result. */
function withDb(cwd, fn) {
const db = openForgeDb(cwd);
try { return fn(db); } finally { db.close(); }
}
// ── Forge CRUD ──────────────────────────────────────────
export function createForge(cwd, { slug, systems = [], parentId = null, priority = "medium" }) {
const db = openForgeDb(cwd);
try {
const result = db.run(
`INSERT INTO forges (slug, parent_id, systems, priority, status, phase)
VALUES (?, ?, ?, ?, 'active', 'bf-precedent')`,
[slug, parentId, JSON.stringify(systems), priority]
);
saveCurrentState(db, { forgeId: result.lastInsertRowid, slug, phase: "bf-precedent", iteration: 1 });
return result.lastInsertRowid;
} finally { db.close(); }
}
export function parkForge(cwd, forgeId, reason = null) {
const db = openForgeDb(cwd);
try {
db.run(
`UPDATE forges SET status = 'parked', parked_at = datetime('now'),
updated_at = datetime('now'), blocking_reason = ?
WHERE id = ? AND status = 'active'`,
[reason, forgeId]
);
clearCurrentState(db);
} finally { db.close(); }
}
export function resumeForge(cwd, slugOrId) {
const db = openForgeDb(cwd);
try {
const forge = typeof slugOrId === "number"
? db.query("SELECT * FROM forges WHERE id = ?").get(slugOrId)
: db.query("SELECT * FROM forges WHERE slug = ?").get(slugOrId);
if (!forge) throw new Error(`Forge not found: ${slugOrId}`);
if (forge.status !== "parked") throw new Error(`Forge "${forge.slug}" is ${forge.status}, not parked`);
db.run(
`UPDATE forges SET status = 'active', parked_at = NULL, updated_at = datetime('now')
WHERE id = ?`, [forge.id]
);
saveCurrentState(db, {
forgeId: forge.id, slug: forge.slug, phase: forge.phase,
iteration: forge.iteration, context: forge.context
});
return forge;
} finally { db.close(); }
}
export function completeForge(cwd, forgeId, lesson = null) {
const db = openForgeDb(cwd);
try {
db.run(
`UPDATE forges SET status = 'completed', completed_at = datetime('now'),
updated_at = datetime('now'), context = json_set(COALESCE(context, '{}'), '$.lesson', ?)
WHERE id = ?`,
[lesson, forgeId]
);
updateCoFailures(db, forgeId);
clearCurrentState(db);
} finally { db.close(); }
}
export function abandonForge(cwd, forgeId, reason = null) {
const db = openForgeDb(cwd);
try {
db.run(
`UPDATE forges SET status = 'abandoned', completed_at = datetime('now'),
updated_at = datetime('now'), blocking_reason = ?
WHERE id = ?`,
[reason, forgeId]
);
clearCurrentState(db);
} finally { db.close(); }
}
export function updateForgePhase(cwd, forgeId, phase, iteration = null) {
const db = openForgeDb(cwd);
try {
if (iteration !== null) {
db.run(
`UPDATE forges SET phase = ?, iteration = ?, updated_at = datetime('now') WHERE id = ?`,
[phase, iteration, forgeId]
);
} else {
db.run(
`UPDATE forges SET phase = ?, updated_at = datetime('now') WHERE id = ?`,
[phase, forgeId]
);
}
const forge = db.query("SELECT slug, iteration FROM forges WHERE id = ?").get(forgeId);
saveCurrentState(db, { forgeId, slug: forge?.slug, phase, iteration: iteration ?? forge?.iteration });
} finally { db.close(); }
}
export function spawnForge(cwd, { slug, systems, parentId, blocksParent = false, priority = "medium" }) {
const db = openForgeDb(cwd);
try {
const result = db.run(
`INSERT INTO forges (slug, parent_id, systems, priority, status, phase)
VALUES (?, ?, ?, ?, 'active', 'bf-precedent')`,
[slug, parentId, JSON.stringify(systems), priority]
);
if (blocksParent) {
db.run(
`UPDATE forges SET status = 'blocked', blocked_by = ?, blocking_reason = 'waiting for: ' || ?
WHERE id = ?`,
[result.lastInsertRowid, slug, parentId]
);
}
return result.lastInsertRowid;
} finally { db.close(); }
}
// ── Gates ───────────────────────────────────────────────
export function recordGate(cwd, forgeId, iteration, gate, result, findings = [], { blind = 1, inputsSeen = [], metaFindings = [] } = {}) {
const db = openForgeDb(cwd);
try {
db.run(
`INSERT OR REPLACE INTO gates (forge_id, iteration, gate, result, findings, blind, inputs_seen, meta_findings)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
[forgeId, iteration, gate, result, JSON.stringify(findings), blind, JSON.stringify(inputsSeen), JSON.stringify(metaFindings)]
);
} finally { db.close(); }
}
// ── Spikes ──────────────────────────────────────────────
export function recordSpike(cwd, forgeId, assumption, result, actual = null) {
const db = openForgeDb(cwd);
try {
const permanent = result === "refuted" ? 1 : 0;
db.run(
`INSERT INTO spikes (forge_id, assumption, result, actual, permanent) VALUES (?, ?, ?, ?, ?)`,
[forgeId, assumption, result, actual, permanent]
);
} finally { db.close(); }
}
export function searchSpikes(cwd, query) {
const db = openForgeDb(cwd);
try {
return db.query(
`SELECT s.*, sf.rank FROM spikes_fts sf
JOIN spikes s ON s.id = sf.rowid
WHERE spikes_fts MATCH ?
AND (s.permanent = 1 OR s.tested_at > datetime('now', '-30 days'))
ORDER BY sf.rank LIMIT 10`
).all(query);
} finally { db.close(); }
}
// ── Visionary ──────────────────────────────────────────
export function recordVisionaryPass(cwd, forgeId, iteration, { passNumber, angle, agent, content }) {
const db = openForgeDb(cwd);
try {
db.run(
`INSERT OR IGNORE INTO visionary_passes (forge