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/fork.mjs -- fork: a run copied whole, the owner replaced.
|
2026-11-14 20:01:15 +00:00
|
|
|
//
|
|
|
|
|
// Runs are entirely independent: no lineage is recorded, and the only
|
|
|
|
|
// mark a fork bears is `forked: unixtime` in run.json (its `created`
|
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
|
|
|
// carries over unchanged -- the story's start date is a fact of the
|
2026-11-14 20:01:15 +00:00
|
|
|
// contents). No inconsistent state on disk at any point: the copy
|
|
|
|
|
// lands staged under a dot-name and is swapped in by a same-volume
|
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
|
|
|
// rename, which is atomic -- no reader ever sees a half-run. The
|
2026-11-14 20:01:15 +00:00
|
|
|
// caller holds the source run's activity lock, so the copy is never
|
2026-11-14 23:48:27 +00:00
|
|
|
// taken mid-turn; a copied open session reopens honestly in the fork
|
2026-11-14 20:01:15 +00:00
|
|
|
// (the log is the truth, same as boot-rebuild).
|
|
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
import { randomBytes } from 'node:crypto';
|
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
|
import { requireRun } from './bundle.mjs';
|
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
|
|
|
import { liveSession, loadSession } from './session.mjs';
|
2026-11-14 20:01:15 +00:00
|
|
|
|
|
|
|
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
|
|
|
const RUNS_DIR = path.join(ROOT, 'runs');
|
|
|
|
|
|
|
|
|
|
export function forkRun(ctx, srcId, user) {
|
|
|
|
|
requireRun(srcId);
|
|
|
|
|
let id;
|
|
|
|
|
for (;;) {
|
|
|
|
|
id = randomBytes(4).toString('hex'); // anonymous, same alphabet as create
|
|
|
|
|
if (!fs.existsSync(path.join(RUNS_DIR, id)) && !fs.existsSync(path.join(RUNS_DIR, `.staging-${id}`))) break;
|
|
|
|
|
}
|
|
|
|
|
const staging = path.join(RUNS_DIR, `.staging-${id}`);
|
|
|
|
|
fs.cpSync(path.join(RUNS_DIR, srcId), staging, { recursive: true }); // sync: atomic wrt the event loop
|
|
|
|
|
const metaPath = path.join(staging, 'run.json');
|
|
|
|
|
const meta = JSON.parse(fs.readFileSync(metaPath, 'utf8'));
|
|
|
|
|
meta.owner = user.id;
|
|
|
|
|
meta.forked = Math.floor(Date.now() / 1000); // unixtime, UTC by definition
|
|
|
|
|
fs.writeFileSync(metaPath, JSON.stringify(meta, null, 2) + '\n');
|
|
|
|
|
fs.renameSync(staging, path.join(RUNS_DIR, id)); // same-volume rename: the atomic swap-in
|
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
|
|
|
// a live session in the source is a live session in the fork -- the
|
|
|
|
|
// log is the truth, same as boot-rebuild; a dormant run forks
|
|
|
|
|
// dormant (the review window copies honestly, unhousekept guard
|
|
|
|
|
// included)
|
|
|
|
|
const live = liveSession(id);
|
|
|
|
|
if (live != null) ctx.sessions.set(id, loadSession(id, live));
|
2026-11-14 20:01:15 +00:00
|
|
|
return { run: id };
|
|
|
|
|
}
|