variant round: roll retired, chance is variant literature (strict parse, crypto pick on reception, picked-only rendering, /story variance metric); entity round: /story the whole deliverable, /table the play projection, the slidwin carry (smart contwin cutoff, whole exchanges, housekeeper reads session-minus-tail); dormant-session rework: universal session lifecycle (dormant->live->finished, sessions/<n> storage), move-at-stop, start lint + auto-send rule, rebaseable housekeeping, turn-0 -> carryover.jsonl, seed -> memory.json, draft-only save-package; format house rules enforced by the nine-rule check-repo (ascii bytes, no text-mutating css); smoke 105/105 + restart + reminder + browser pass green, irene hand-migrated to the new layout
2026-11-15 14:35:00 +00:00
|
|
|
// server/clerk.mjs -- the clerk boundary. Every state operation goes
|
2026-11-14 03:15:53 +00:00
|
|
|
// through the memory.mjs CLI; the server never touches the database
|
|
|
|
|
// directly. The clerk stays the one truth.
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
/** @typedef {import('./shapes.mjs').ClerkResult} ClerkResult */
|
|
|
|
|
/** @typedef {import('./shapes.mjs').User} User */
|
|
|
|
|
|
2026-11-14 03:15:53 +00:00
|
|
|
import { spawnSync } from 'node:child_process';
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
|
|
|
|
|
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
|
|
|
const CLERK = path.join(ROOT, 'tool', 'memory.mjs');
|
|
|
|
|
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
// every spawn strips the sqlite experimental warning from stderr; the
|
|
|
|
|
// rest is the clerk's own voice
|
|
|
|
|
/** @param {string} runId @param {...string} args @returns {ClerkResult} */
|
2026-11-14 03:15:53 +00:00
|
|
|
export function clerk(runId, ...args) {
|
|
|
|
|
const res = spawnSync(process.execPath, [CLERK, '--run', runId, '--quiet', ...args], { encoding: 'utf8' });
|
|
|
|
|
return {
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
code: res.status ?? 1, // null only on a signal kill; callers only ever test against 0
|
2026-11-14 03:15:53 +00:00
|
|
|
out: (res.stdout || '').trim(),
|
|
|
|
|
err: (res.stderr || '').split('\n')
|
|
|
|
|
.filter((l) => !l.includes('ExperimentalWarning') && !l.includes('trace-warnings'))
|
|
|
|
|
.join('\n').trim(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
/** Run a clerk verb and parse its --json stdout as T.
|
|
|
|
|
* @template T @param {string} runId @param {...string} args @returns {T} */
|
2026-11-14 03:15:53 +00:00
|
|
|
export function clerkJson(runId, ...args) {
|
|
|
|
|
const r = clerk(runId, ...args);
|
|
|
|
|
if (r.code !== 0) throw new Error(r.err || r.out || `clerk ${args[0]} failed`);
|
|
|
|
|
try {
|
|
|
|
|
return JSON.parse(r.out);
|
|
|
|
|
} catch {
|
|
|
|
|
throw new Error(`clerk ${args[0]}: unparseable output: ${r.out.slice(0, 200)}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// a clerk call that is allowed to fail (returns the result either way)
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
/** @param {string} runId @param {...string} args @returns {ClerkResult} */
|
2026-11-14 03:15:53 +00:00
|
|
|
export function clerkTry(runId, ...args) {
|
|
|
|
|
return clerk(runId, ...args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// commands that address no run (packages, runs, create)
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
/** @param {...string} args @returns {ClerkResult} */
|
2026-11-14 03:15:53 +00:00
|
|
|
export function clerkRaw(...args) {
|
|
|
|
|
const res = spawnSync(process.execPath, [CLERK, ...args], { encoding: 'utf8' });
|
|
|
|
|
return {
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
code: res.status ?? 1, // null only on a signal kill; callers only ever test against 0
|
2026-11-14 03:15:53 +00:00
|
|
|
out: (res.stdout || '').trim(),
|
|
|
|
|
err: (res.stderr || '').split('\n')
|
|
|
|
|
.filter((l) => !l.includes('ExperimentalWarning') && !l.includes('trace-warnings'))
|
|
|
|
|
.join('\n').trim(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// machine output for run-less commands (packages, runs)
|
jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases
2026-11-15 15:28:56 +00:00
|
|
|
/** @template T @param {...string} args @returns {T} */
|
2026-11-14 03:15:53 +00:00
|
|
|
export function clerkRawJson(...args) {
|
|
|
|
|
const r = clerkRaw('--json', ...args);
|
|
|
|
|
if (r.code !== 0) throw new Error(r.err || r.out || `clerk ${args[0]} failed`);
|
|
|
|
|
try {
|
|
|
|
|
return JSON.parse(r.out);
|
|
|
|
|
} catch {
|
|
|
|
|
throw new Error(`clerk ${args[0]}: unparseable output: ${r.out.slice(0, 200)}`);
|
|
|
|
|
}
|
|
|
|
|
}
|