Read the script
#!/usr/bin/env -S bun
/**
* SessionStart / PostCompact hook: inject the current `Brain/active.md`
* digest as `additionalContext` so the agent sees the live set of
* confirmed and quarantined preferences without explicitly calling
* `brain_query` first.
*
* Three lanes share the surface, and the order between them is the point.
* `Brain/standing-rules.md` is operator-authored and goes FIRST, read
* outside the fail-open memory load so it survives a memory-layer
* failure and never enters the inject cache. The scoped operator rules
* for this session's project and this device
* (`Brain/standing-rules/{project,host}/<key>.md`) come next, also read
* outside the boundary and never cached - the cache key is vault-wide, so
* a cached block could replay one project's rules in another - but
* charged against the injection budget. Everything after them - runtime
* notices, `active.md`, `lessons.md` - is what the agent learned about
* itself, assembled inside the fail-open boundary and charged against
* what is left of the configured injection budget.
*
* One ceiling over all of it: the host takes at most 10,000 characters of
* context from one hook and replaces a longer value with a file path and
* a preview, so the whole payload of one run - every lane above, the
* notices, the framing and the separators - is sized to
* `HOST_CONTEXT_CEILING_CHARS` (`hooks/lib/host-context-ceiling.ts`).
* The memory bodies share what the lanes ahead of them leave; a payload
* the operator's caps still push past it is cut there with the usual
* truncation notice. Only a payload the re-delivery lane splits is sized
* per part instead.
*
* Contract (identical for Claude Code and Codex):
* stdin: hook payload JSON. The vault path is resolved from the
* persisted Open Second Brain config (env `VAULT_DIR` → config
* `vault:` field), not from the payload — both runtimes route the
* hook through the same `o2b-hook` PATH-shim, so this stays
* runtime-agnostic.
* stdout: JSON of the shape
* {
* "hookSpecificOutput": {
* "hookEventName": "SessionStart" | "PostCompact",
* "additionalContext": "<standing-rules block, the scoped rules
* block, then the budgeted memory context:
* runtime notices, the rendered
* Brain/active.md body, lessons>"
* }
* }
*
* Quiet on the failure modes that leave it with nothing to say (no config,
* no vault, malformed payload): the hook exits 0 with no output and the
* runtime proceeds as if it never ran. A SessionStart that silently fails
* is far less harmful than one that aborts the session with a stderr
* trace. The agent simply does not get the per-session preferences nudge —
* exactly the v0.9.0 behaviour.
*
* A missing `Brain/active.md` is NOT one of those modes any more, and
* neither is a memory layer that threw. The standing-rules lane is read
* outside the fail-open boundary, so a vault whose operator wrote rules
* still emits them with an empty memory context behind them; a rules file
* that exists and cannot be read emits the block that says so. The empty
* exit is reached only when there are NEITHER standing rules NOR memory,
* and `tests/hooks/active-inject.test.ts` asserts it.
*/
import { resolveAnswerQuoteCheckModes } from "../src/core/brain/quote-check-modes.ts";
import {
resolveQuoteCheckEnabled,
resolveRecallInjectEnabled,
resolveRegroundPartChars,
resolveRegroundPartsEnabled,
} from "../src/core/config.ts";
import { healCliSymlinks } from "../src/cli/install-cli.ts";
import { ensureVaultCurrent } from "../src/core/maintenance/ensure-current.ts";
import { hookAuditDir } from "../src/core/brain/paths.ts";
import { loadInjectContextFailOpen } from "../src/core/brain/inject-failopen.ts";
import { armProcessCeiling, decisionWaitMs, resolveHookCeilingMs } from "./lib/process-ceiling.ts";
import { appendAuditRecord } from "../src/core/reliability/audit.ts";
import { asHookPayload, readHookInput, type HookPayloadBase } from "./lib/stdin.ts";
import {
beginInjectionEpoch,
digestDeliveredText,
digestNotePaths,
isRealSessionId,
} from "./lib/injection-ledger.ts";
import {
LANE_BUDGETED,
LANE_UNBUDGETED,
renderStandingBlock,
type InjectionMeter,
} from "./lib/standing-block.ts";
import { buildAgentContextBlocks } from "./lib/agent-context-blocks.ts";
import {
assembleActiveContext,
BLOCK_SEPARATOR,
createInjectionMeter,
joinBlocks,
MEMORY_SOURCES,
renderScopedBlock,
resolveInjectionLimits,
scopedSectionCap,
SOURCE_ACTIVE_BODY,
SOURCE_DIGEST_CUT_NOTE,
SOURCE_LESSONS_BODY,
type InjectionLimits,
} from "./lib/active-context.ts";
import { pruneHookStateFiles } from "./lib/session-state.ts";
import { detectHookRuntime } from "./lib/detect.ts";
import { splitRegroundParts } from "../src/core/brain/reground-parts.ts";
import { isContextEventName } from "./lib/context-events.ts";
import { emitContextReceipt } from "../src/core/brain/context-receipts.ts";
import { estimateTokens } from "../src/core/brain/text/tokenizer.ts";
import type { InjectContextSource } from "../src/core/brain/inject-failopen.ts";
import { resolveHookTarget } from "./lib/hook-vault.ts";
import {
fitHostContext,
HOST_CONTEXT_CEILING_CHARS,
hostMeasureFor,
type HostMeasure,
} from "./lib/host-context-ceiling.ts";
import {
DIGEST_CUT_NOTE_RESERVE,
type DigestCut,
parseDigestCuts,
renderDigestCutNote,
} from "../src/core/agent-context/digest-cut-note.ts";
/**
* Best-effort hook audit line. Never throws: a failure to record must never
* disturb the fail-soft hook contract, and a hung filesystem is exactly when
* this runs, so it is wrapped defensively.
*/
function auditHook(vault: string | null, action: string, details: Record<string, unknown>): void {
if (vault === null) return;
try {
appendAuditRecord(hookAuditDir(vault), {
timestamp: new Dat