Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions docs/o08-durable-resume-decision-packet.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,30 @@ resume. Context-enabled recovery, live providers, production data, automatic
claim takeover, and O09 remain out of scope. O08 stays open pending remaining
criteria and independent review.

The slice is bounded to roughly one focused day: persist/link the stable retry
window, validate and classify read-only restart evidence, and add a finite
boundary/corruption matrix. Actual resume integration remains a separate slice
because it must reconstruct job and ledger state and compose current execution
authority, atomic one-shot claim, fresh stable-time checks, durable dispatch
marker, and uncertain-outcome quarantine at every asynchronous boundary. O09
remains a separate dependency gate.
The implementation and subprocess acceptance evidence above cover the bounded
no-context runtime path. Broader O08 criteria, different storage backends,
context-enabled recovery, production durability guarantees, and O09 remain
separate and unaccepted.

## Synthetic subprocess restart and crash acceptance — 2026-11-09

`resume-subprocess.test.js` seeds only the pinned Comic Manifest fixture in a
private temporary local store, then exercises the runner from fresh Node child
processes. It verifies: a new process completes attempt two with the original
attempt linkage; two concurrent processes contend while one is paused in
preflight and only one dispatches; process exit immediately after claim leaves
a held claim with no takeover; exit after the durable dispatch marker leaves no
mock effect and no replay; and exit after one mock effect plus a durable but
unlinked provider-outcome blob is quarantined without a second mock effect.
The test-only worker records every adapter `execute()` entry in a shared
temporary per-execution effect log, including normal restarts and both crash
points; all call counts are checked against the durable event history.

These are process-crash and local-filesystem integration tests around the
existing fsync-backed prototype store. They do not simulate power loss, prove
behavior on other filesystems, add a production backend, or provide a generic
exactly-once guarantee. O08 remains open pending its remaining criteria and
review; O09 remains gated separately.

## Finite synthetic restart and crash-point fixtures

Expand Down
105 changes: 105 additions & 0 deletions tests/offline-orchestration-prototype/resume-subprocess-worker.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { randomUUID } from 'node:crypto';
import { appendFileSync, existsSync, watch, writeFileSync } from 'node:fs';
import { createSyntheticExecutionAuthorityVerifier, SYNTHETIC_EXECUTION_PURPOSE } from '../../src/offline-orchestration-prototype/execution-authority.js';
import { createOfflineOrchestrationPrototypeForTest, LocalFilePrototypeStore } from '../../src/offline-orchestration-prototype/testing.js';
import { MockTextAdapter } from '../../src/prompt-sdk/index.js';

const root = process.env.DSS_O08_STORE_ROOT;
const jobId = process.env.DSS_O08_JOB_ID;
const mode = process.env.DSS_O08_WORKER_MODE;
const stableNow = Number(process.env.DSS_O08_STABLE_NOW);
const production = {
production_id: 'urn:uuid:10000000-0000-4000-8000-000000000001',
revision: 1,
identity_sha256: 'sha256:9250217ad23125b0356a9e7002f57a55dacf9d982f069caa403b89f8c6c234dc'
};
const record = (action, suffix) => ({
decision_id: `20000000-0000-4000-8000-${suffix.padStart(12, '0')}`,
decision: 'allow',
action,
purpose: SYNTHETIC_EXECUTION_PURPOSE,
production,
not_before: '2026-01-01T00:00:00Z',
review_after: '2027-01-01T00:00:00Z',
expires_at: '2027-02-01T00:00:00Z',
authority_reference: 'https://github.com/DefinitelySecureStudio/platform/issues/109'
});
const authority = createSyntheticExecutionAuthorityVerifier([record('execute', '1'), record('retry', '2')]);
const store = new LocalFilePrototypeStore(root);
const effectLog = process.env.DSS_O08_EFFECT_LOG;

class ObservedMock extends MockTextAdapter {
async execute(request, options) {
appendFileSync(effectLog, `${process.pid}\n`, { flag: 'a', mode: 0o600 });
return super.execute(request, options);
}
}

if (mode === 'crash-after-claim') {
const claimResume = store.claimResume.bind(store);
store.claimResume = value => {
const result = claimResume(value);
if (result.status === 'acquired') process.exit(73);
return result;
};
}

if (mode === 'crash-after-dispatch-marker') {
const append = store.append.bind(store);
store.append = event => {
const result = append(event);
if (event.type === 'dispatch-started') process.exit(74);
return result;
};
}

let mockAdapterFactory = configuration => new ObservedMock(configuration);
if (mode === 'hold-preflight') {
mockAdapterFactory = configuration => new class extends ObservedMock {
async describe() {
const ready = process.env.DSS_O08_READY_MARKER;
const release = process.env.DSS_O08_RELEASE_MARKER;
writeFileSync(ready, 'ready\n', { flag: 'wx', mode: 0o600 });
await new Promise(resolve => {
const watcher = watch(root, () => {
if (existsSync(release)) { watcher.close(); resolve(); }
});
if (existsSync(release)) { watcher.close(); resolve(); }
});
return super.describe();
}
}(configuration);
}
if (mode === 'crash-after-outcome-blob') {
const writeImmutable = store.writeImmutable.bind(store);
store.writeImmutable = (key, bytes) => {
const result = writeImmutable(key, bytes);
if (key.startsWith('provider-outcome:')) process.exit(75);
return result;
};
}

const monotonicEpoch = `subprocess-${process.pid}`;
const runner = createOfflineOrchestrationPrototypeForTest({
store,
runtime: {
now: () => Number(process.hrtime.bigint() / 1_000_000n),
epochId: monotonicEpoch,
wallNow: () => Date.parse('2026-11-08T02:40:00Z'),
setTimer: (callback, delay) => setTimeout(callback, delay),
clearTimer: timer => clearTimeout(timer)
},
idFactory: randomUUID,
executionAuthorityProvider: authority.verifier,
resumeTimeAuthority: {
id: 'studio.synthetic.resume-time-v1',
mode: 'synthetic',
domain_id: 'synthetic-resume-time-v1',
epoch_id: 'subprocess-fixture-epoch',
now: () => stableNow
},
mockAdapterFactory
});

const result = await runner.resume({ jobId, authorizeResume: true });
process.stdout.write(`${JSON.stringify({ pid: process.pid, result })}\n`);
224 changes: 224 additions & 0 deletions tests/offline-orchestration-prototype/resume-subprocess.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { existsSync, mkdtempSync, readFileSync, rmSync, watch, writeFileSync } from 'node:fs';
import { randomUUID } from 'node:crypto';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
import { createSyntheticExecutionAuthorityVerifier, SYNTHETIC_EXECUTION_PURPOSE } from '../../src/offline-orchestration-prototype/execution-authority.js';
import { LocalFilePrototypeStore, createOfflineOrchestrationPrototypeForTest } from '../../src/offline-orchestration-prototype/testing.js';

const fixture = JSON.parse(await readFile(new URL('../fixtures/comic-manifest-v1.json', import.meta.url), 'utf8'));
const productionSource = JSON.stringify(fixture.production);
const stableStart = Date.parse('2026-11-08T02:40:00Z');
const workerPath = fileURLToPath(new URL('./resume-subprocess-worker.mjs', import.meta.url));

function authority() {
const production = {
production_id: 'urn:uuid:10000000-0000-4000-8000-000000000001',
revision: 1,
identity_sha256: 'sha256:9250217ad23125b0356a9e7002f57a55dacf9d982f069caa403b89f8c6c234dc'
};
const record = (action, suffix) => ({
decision_id: `20000000-0000-4000-8000-${suffix.padStart(12, '0')}`,
decision: 'allow', action, purpose: SYNTHETIC_EXECUTION_PURPOSE, production,
not_before: '2026-01-01T00:00:00Z', review_after: '2027-01-01T00:00:00Z',
expires_at: '2027-02-01T00:00:00Z',
authority_reference: 'https://github.com/DefinitelySecureStudio/platform/issues/109'
});
return createSyntheticExecutionAuthorityVerifier([record('execute', '1'), record('retry', '2')]);
}

async function seedCandidate(t) {
const root = mkdtempSync(join(tmpdir(), 'dss-o08-subprocess-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
const store = new LocalFilePrototypeStore(root);
const runner = createOfflineOrchestrationPrototypeForTest({
store,
runtime: {
now: () => 10_000,
epochId: 'parent-seed-process',
wallNow: () => Date.parse('2026-11-08T02:40:00Z'),
setTimer: () => 1,
clearTimer: () => {}
},
idFactory: randomUUID,
executionAuthorityProvider: authority().verifier,
resumeTimeAuthority: {
id: 'studio.synthetic.resume-time-v1', mode: 'synthetic',
domain_id: 'synthetic-resume-time-v1', epoch_id: 'subprocess-fixture-epoch',
now: () => stableStart
}
});
const result = await runner.submit({
productionSource,
noOutputResultVersion: '1.1.0',
mockOutcome: { kind: 'rate-limit', retry_after_ms: 10 }
});
assert.equal(result.kind, 'terminal');
assert.equal(result.retry_eligible, true);
const events = store.readEvents(result.job_id);
const window = events.find(event => event.type === 'resume-window');
assert.equal(window.retry_not_before_at_ms, stableStart + 10);
return { root, jobId: result.job_id, store, window };
}

function startWorker(seed, mode = 'normal', extraEnv = {}) {
const effectLog = extraEnv.DSS_O08_EFFECT_LOG ?? join(seed.root, 'synthetic-effects.log');
const childEnvironment = {};
for (const key of ['PATH', 'TMPDIR', 'TEMP', 'SystemRoot', 'WINDIR', 'LANG', 'LC_ALL']) {
if (process.env[key] !== undefined) childEnvironment[key] = process.env[key];
}
const child = spawn(process.execPath, [workerPath], {
cwd: process.cwd(),
env: {
...childEnvironment,
DSS_O08_STORE_ROOT: seed.root,
DSS_O08_JOB_ID: seed.jobId,
DSS_O08_STABLE_NOW: String(seed.window.retry_not_before_at_ms),
DSS_O08_WORKER_MODE: mode,
DSS_O08_EFFECT_LOG: effectLog,
...extraEnv
},
stdio: ['pipe', 'pipe', 'pipe']
});
let allStdout = '';
let stderr = '';
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout.on('data', chunk => {
allStdout += chunk;
});
child.stderr.on('data', chunk => { stderr += chunk; });
const closed = new Promise((resolve, reject) => {
const timer = setTimeout(() => {
child.kill('SIGKILL');
reject(new Error(`Subprocess timed out. stderr=${stderr}`));
}, 15_000);
child.once('error', error => { clearTimeout(timer); reject(error); });
child.once('close', (code, signal) => {
clearTimeout(timer);
resolve({ code, signal, stdout: allStdout, stderr });
});
});
return {
child,
closed
};
}

function effectCount(path) {
if (!existsSync(path)) return 0;
const contents = readFileSync(path, 'utf8').trim();
return contents ? contents.split('\n').length : 0;
}

async function waitForFile(path, root, child) {
if (existsSync(path)) return;
await new Promise((resolve, reject) => {
let settled = false;
let timer;
let watcher;
const finish = error => {
if (settled) return;
settled = true;
watcher?.close();
clearTimeout(timer);
if (error) {
child.kill('SIGKILL');
reject(error);
} else resolve();
};
watcher = watch(root, () => { if (existsSync(path)) finish(); });
timer = setTimeout(() => finish(new Error(`Expected subprocess marker was not created: ${path}`)), 5_000);
if (existsSync(path)) finish();
});
}

async function runWorker(seed, mode = 'normal', extraEnv = {}) {
const worker = startWorker(seed, mode, extraEnv);
const exit = await worker.closed;
let message;
if (exit.code === 0 && exit.stdout.trim()) message = JSON.parse(exit.stdout.trim().split('\n').at(-1));
else if (exit.code === 0) throw new Error(`Resume worker exited without a result. stderr=${exit.stderr}`);
return { ...exit, pid: message?.pid, result: message?.result };
}

test('a separate Node process resumes the pinned synthetic candidate from the durable store', async t => {
const seeded = await seedCandidate(t);
const pidBefore = process.pid;
const child = await runWorker(seeded);
assert.equal(child.code, 0, child.stderr);
assert.notEqual(child.pid, pidBefore);
assert.equal(child.result.kind, 'terminal');
const events = seeded.store.readEvents(seeded.jobId);
const prepared = events.filter(event => event.type === 'prepared');
assert.equal(prepared.length, 2);
assert.equal(prepared[1].retry_of_attempt_id, prepared[0].attempt_id);
assert.equal(events.filter(event => event.type === 'dispatch-started').length, 2);
assert.equal(effectCount(join(seeded.root, 'synthetic-effects.log')), 1);
});

test('two Node processes contend for one durable claim and only the winner dispatches', async t => {
const seeded = await seedCandidate(t);
const ready = join(seeded.root, 'preflight.ready');
const release = join(seeded.root, 'preflight.release');
const winner = startWorker(seeded, 'hold-preflight', {
DSS_O08_READY_MARKER: ready,
DSS_O08_RELEASE_MARKER: release,
DSS_O08_EFFECT_LOG: join(seeded.root, 'synthetic-effects.log')
});
await waitForFile(ready, seeded.root, winner.child);
const contender = await runWorker(seeded);
assert.equal(contender.code, 0, contender.stderr);
assert.equal(contender.result.status, 'resume-claim-held');
assert.equal(effectCount(join(seeded.root, 'synthetic-effects.log')), 0);
writeFileSync(release, 'release\n', { flag: 'wx', mode: 0o600 });
const completed = await winner.closed;
assert.equal(completed.code, 0, completed.stderr);
const result = JSON.parse(completed.stdout.trim().split('\n').at(-1)).result;
assert.equal(result.kind, 'terminal');
assert.equal(effectCount(join(seeded.root, 'synthetic-effects.log')), 1);
assert.equal(seeded.store.readEvents(seeded.jobId).filter(event => event.type === 'dispatch-started').length, 2);
});

test('process death after claim leaves a permanent claim and no takeover', async t => {
const seeded = await seedCandidate(t);
const crashed = await runWorker(seeded, 'crash-after-claim');
assert.equal(crashed.code, 73);
const restarted = await runWorker(seeded);
assert.equal(restarted.code, 0, restarted.stderr);
assert.equal(restarted.result.status, 'resume-claim-held');
assert.equal(effectCount(join(seeded.root, 'synthetic-effects.log')), 0);
assert.equal(seeded.store.readEvents(seeded.jobId).filter(event => event.type === 'dispatch-started').length, 1);
});

test('process death after durable dispatch marker does not replay the mock call', async t => {
const seeded = await seedCandidate(t);
const effectLog = join(seeded.root, 'synthetic-effects.log');
const crashed = await runWorker(seeded, 'crash-after-dispatch-marker', { DSS_O08_EFFECT_LOG: effectLog });
assert.equal(crashed.code, 74);
assert.equal(effectCount(effectLog), 0);
const restarted = await runWorker(seeded);
assert.equal(restarted.code, 0, restarted.stderr);
assert.equal(restarted.result.status, 'resume-not-eligible');
assert.equal(effectCount(effectLog), 0);
assert.equal(seeded.store.readEvents(seeded.jobId).filter(event => event.type === 'dispatch-started').length, 2);
});

test('process death after one mock effect and an unlinked outcome blob quarantines without replay', async t => {
const seeded = await seedCandidate(t);
const effectLog = join(seeded.root, 'synthetic-effects.log');
const crashed = await runWorker(seeded, 'crash-after-outcome-blob', {
DSS_O08_EFFECT_LOG: effectLog
});
assert.equal(crashed.code, 75);
assert.equal(effectCount(effectLog), 1);
const restarted = await runWorker(seeded);
assert.equal(restarted.code, 0, restarted.stderr);
assert.equal(restarted.result.status, 'resume-not-eligible');
assert.equal(effectCount(effectLog), 1);
assert.equal(seeded.store.readEvents(seeded.jobId).filter(event => event.type === 'dispatch-started').length, 2);
});
Loading