Agentic
⏱ 8 min read
📁 examples/20_durability.mjs
Lesson 35: A crash mid-run, resumed exactly once
session checkpoints, the at-most-once execution journal, the session fork
1. Why This Feature Exists
A release agent works one live deployment turn, then the process dies. The next process knows nothing except what is on disk under scratchpad/examples/20-durability/: the conversation checkpoint and the execution journal that records what already happened.
2. What the Example Demonstrates
A — the live turn
a real model calls
deploy_release, a tool whose execute() appends one line to a ledger file, inside a session bound to a LocalFileSessionStore and an ExecutionJournal.B — a tagged checkpoint
saveCheckpoint/restoreCheckpoint round-trip a payload under CHECKPOINT_KIND; asking for the wrong kind throws, so a resume cannot mistake another engine's record for its own.C — the crash
a NEW session on the same thread id restores the transcript with
loadCheckpoint(); the deploy is re-issued at its recorded boundary with the restored journal and REPLAYS — the ledger keeps exactly one line.D — interrupted work
a row left in
JOURNAL_STATUS.INTENT (the process died mid-call) fails closed for a non-idempotent tool; only a tool that declares idempotent: true is re-run.E — the fork
forkCheckpoint branches the thread; the branch takes its own turn and the source history does not move. What this shows: - at-most-once side effects across a process restart - withSessionStore + withExecutionJournal on one session - toolJournalKey as the identity a replay is matched on - forkCheckpoint as the branch point for a second attempt Run: node examples/20_durability.mjs Needs: PROVIDER + provider credentials in secret.env. @version 0.27.0imported from 'merci-sdk'
ConfigurationError, ExecutionJournal, JOURNAL_STATUS, createUserMessage, defaultDisplay, executeTools, initializeClient, loadCredentials, log, toolJournalKey
3. How the Core Call Works
The entry point, condensed. The full runnable file is linked below.
import { appendFile, mkdir, readFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import {
ConfigurationError, ExecutionJournal, JOURNAL_STATUS,
createUserMessage, defaultDisplay, initializeClient, loadCredentials, log, toolJournalKey,
} from 'merci-sdk';
import { CHECKPOINT_KIND, restoreCheckpoint, saveCheckpoint } from 'merci-sdk/planning';
import { LocalFileSessionStore } from 'merci-sdk/sessions';
import { executeTools } from 'merci-sdk/tools';
import { SCRATCH_DIR } from './_shared/real-tools.mjs';
import { reportUsage, resolveExample, runMain, trackUsage, warnEmptyAnswer, wireClient } from './_shared/example-utils.mjs';
const RUN_DIR = join(SCRATCH_DIR, '20-durability');
const STORE_DIR = join(RUN_DIR, 'sessions');
const LEDGER = join(RUN_DIR, 'deploy-ledger.jsonl');
const THREAD = 'release-42';
const NOTE_THREAD = 'release-42-note';
const FORK_THREAD = 'release-42-rollback';
const MARKER = 'RELEASE-42-READY';
const ROLLBACK_MARKER = 'ROLLBACK-PLANNED';
const DEPLOY_PROMPT = `Deploy this release to staging: call deploy_release exactly once with environment "staging". Then reply with exactly: ${MARKER}`;
const ROLLBACK_PROMPT = `A rollback is being considered for the same release. Reply with exactly: ${ROLLBACK_MARKER}`;
/**
* A deployment step whose execute() performs a REAL side effect: one appended
* line in the ledger file. Any re-run is therefore visible on disk.
* @param {{ledgerPath: string, counter: {n: number}, idempotent?: boolean}} arg0
*/
function deployTool({ ledgerPath, counter, idempotent = false }) {
return {
name: 'deploy_release',
description: 'Deploys the current release to one environment and appends the deployment to the ledger file. Performs a real side effect.',
parameters: {
type: 'object',
properties: { environment: { type: 'string', description: 'Target environment, e.g. staging.' } },
required: ['environment'],
},
idempotent,
/** @param {{args?: {environment?: string}}} arg0 */
execute: async ({ args = {} }) => {
const environment = String(args.environment ?? 'staging');
counter.n += 1;
const deployment = `dep-${counter.n}`;
await appendFile(ledgerPath, `${JSON.stringify({ deployment, environment, at: new Date().toISOString() })}\n`, 'utf8');4. Behavior the Code Fixes
session checkpoints, the at-most-once execution journal, the session fork
5. Run the Example
Execute the runnable example from the repository root:
node examples/20_durability.mjs
What it does: session checkpoints, the at-most-once execution journal, the session fork