import type { SubjectFields } from './hash.js'; import type { ActionProjection, TwinActionPrecondition } from './actions.js'; export type TwinCredentialShape = { /** header name (lowercased) or query-param name the credential arrived under. */ name: string; /** where it sat on the request. */ in: 'header' | 'query'; /** the auth SCHEME when the value carried one (`Bearer`, `Basic`, `AWS4-HMAC-SHA256`). A scheme * is not a secret, and it is what distinguishes "a bearer token arrived" from "a named vendor * header arrived" — the exact confusion this journal was added to end. */ scheme?: string; /** `sha256:<16 hex>` of the value (of the credential part when a scheme was present). Stable * across processes and runs, so equal fingerprints mean equal values; non-reversible. */ fp?: string; /** the name arrived with an EMPTY value — the "the SDK sent the header but no key" case. */ empty?: true; }; /** The header a World puts on a request it forwards to a twin: who made it (`key:`, `person:`, `token`, * `token:read`). The World replaces any a caller sent; the serve seam records it in the twin's request journal and * removes it before the twin's handler. A twin reached on its own port, not through a World, records what it was sent. */ export declare const CALLER_HEADER = "x-volter-caller"; /** The caller as it travels in CALLER_HEADER: percent-encoded, since a key's name is a person's words (`preview pr-8 · * org token ci`) and a header carries bytes only. `callerFromHeader` reads it back; a value it cannot decode stands. */ export declare const callerForHeader: (caller: string) => string; export declare const callerFromHeader: (value: string) => string; export type TwinRequestJournalEntry = { at?: string; method: string; path: string; status: number; /** the serve's wall time in milliseconds (layer 8: a world's response-time report) */ ms?: number; /** credential-looking headers/query params that arrived, named + fingerprinted, never quoted. */ credentials?: TwinCredentialShape[]; /** who made it, as the World named its caller (CALLER_HEADER: `key:`, `person:`, `token`); absent for a * request that did not come through a World */ caller?: string; /** the request's trace-id and parent-id (its `traceparent`). The World mints the parent-id of every request it * forwards, and the request's log entries carry it, so an entry is joined to its caller by `span`, which no caller * chooses (a trace-id is the caller's and spans requests) */ trace?: string; span?: string; /** `request`: written BEFORE the request is served (an audit device's request entry, so a request whose * audit cannot be written is refused); its outcome is the entry without a phase written after. */ phase?: 'request'; /** what the World's scenario did to a request that wrote nothing (a fault that landed on it, a lag that withheld * subjects from it), which the twin's log never holds (scenario-consume.ts noteFault) */ scenario?: Array>; }; /** The journal read back: every entry kept for a service, oldest first (an empty list when the journal is switched off or empty). * With `since` (epoch ms), segments that ended before it are not read. */ export declare function readTwinRequestJournal(service: string, root?: string, options?: { since?: number; }): TwinRequestJournalEntry[]; /** Journal lines this process failed to keep, since it started, for one service's journal under `root` * (a World's request report sums its own twins and links). */ export declare function twinRequestJournalFailures(service: string, root?: string): number; /** Where a reader of a journal stopped: the newest closed segment it had seen and the bytes of the live file it read. */ export type TwinRequestJournalCursor = { segment: number; offset: number; }; /** The journal's lines after `cursor` (all of them without one), and the cursor to read on from. A rotation closes * the live file into the next segment as it stood, so a reader that stopped in the live file goes on in that * segment at its offset, then any later segments, and the next read takes the live file from its start; a live file shorter than where * the reader stopped (read between a rotation's two writes) is read again from its start, its lines repeated rather * than lost. Only whole lines are read (a line being appended is read next time). Without a cursor, the newest * `segments` closed segments are read (all of them when omitted), then the live file. */ export declare function readTwinRequestJournalFrom(service: string, root: string | undefined, cursor: TwinRequestJournalCursor | null, options?: { segments?: number; }): { entries: TwinRequestJournalEntry[]; cursor: TwinRequestJournalCursor; }; /** Non-reversible, stable fingerprint. 64 bits of sha256 — enough that two distinct credentials * never collide in a journal, far too little to walk back to a real key. */ export declare function credentialFingerprint(value: string): string; /** * The credential SHAPE of one request: every credential-looking header and query param, named and * fingerprinted, sorted for stable diffing. Values never leave this function. */ export declare function twinRequestCredentials(headers: Headers | Record, url?: string | URL): TwinCredentialShape[]; /** The audit is kept unless it is switched off (VOLTER_TWIN_REQUEST_JOURNAL=0): no audit is the * setting someone chooses, never the default. */ export declare function twinRequestJournalEnabled(env?: Record): boolean; /** Where a service's request journal lives: beside its `actions.jsonl`. */ export declare function twinRequestJournalPath(service: string, root?: string): string; /** * Append one request to the journal — a no-op only when VOLTER_TWIN_REQUEST_JOURNAL=0. Never throws; * answers whether the line was kept (true when the journal is switched off: no audit was chosen), so a * caller that must not act unaudited (a link) can refuse. */ export declare function journalTwinRequest(service: string, entry: TwinRequestJournalEntry, root?: string): boolean; export declare const TWIN_JOURNAL_IDENTITY: unique symbol; export type TwinJournalIdentity = { service: string; root?: string; }; /** Note on the journal line of the request being served what the World's scenario did to it; a request served outside * the journal (switched off, or in-process) keeps no note. */ export declare function noteTwinRequest(note: Record): void; export declare function vendorFromStack(stack: string | undefined): string | undefined; /** * Wrap `Bun.serve` so EVERY twin's HTTP surface journals, with no per-pack line. Idempotent * (a `Symbol.for` marker survives a second copy of this module) and a strict pass-through when * VOLTER_TWIN_REQUEST_JOURNAL=0. Called once at module load, below; exported so a test can assert it. * * A pack that knows its own identity may declare it by putting `{service, root}` on the serve * options under `TWIN_JOURNAL_IDENTITY` (a symbol key Bun's option reader ignores). Nothing in * the estate needs to: `createTwinServer` is the only caller, because it is the one server whose * service is an argument rather than a fact about the pack. */ export declare function installTwinRequestJournal(): boolean; export type TwinResource = { id: string; type: string; updatedAt: string; } & Record; export declare function twinResources(service: string, root?: string): TwinResource[]; export type TwinWriteResult = { status: 'performed' | 'replayed'; actionId: string; /** the id the vendor minted when the head performed the write */ externalId?: string; /** what the vendor answered when the head performed the write, as the adapter returned it */ vendorData?: unknown; }; export type TwinWriteInput = { operation: string; provider?: string; subjectType: string; subjectId: string; fields: SubjectFields; input?: Record; projection?: ActionProjection; occurredAt?: string; actor?: { kind: 'agent' | 'human' | 'bot' | 'system'; id?: string; }; preconditions?: TwinActionPrecondition[]; correlationId?: string; uniqueness?: string; /** AT-MOST-ONCE, opt in. A local vendor write is an OCCURRENCE by default: two calls are two * actions, even byte-identical in the same instant. Pass a caller-supplied key when a re-issue * of the SAME request must collapse onto the first. Without a key the kernel cannot tell a retry * from a repeat, and guessing from content is what silently dropped writes. * * SCOPE: the key dedupes * on (service, operation, subjectId, key). It therefore helps only when the CALLER supplies the * subject id. A create whose id the twin mints gets a fresh id per call and is NOT deduped by * this — which is the commonest shape a vendor's own idempotency header covers — so a pack * modelling such a header for creates keeps doing it itself, by request signature and stored * response (the payments pack in this catalog does). Reuse of a key with a DIFFERENT payload * raises a conflict, which the generic twin server answers as a 400. */ idempotencyKey?: string; /** The EXACT identity of an action being re-materialized, when the caller already has one — * replay reproducing a source changeset's actions into a target world. Distinct from * `idempotencyKey`, which is a REQUEST key the kernel derives an id from: this IS the id, so a * replayed action keeps the identity it had in the source world. Implies at-most-once, because * identity equality is what at-most-once means. Not for packs. */ actionId?: string; }; export type AtomicTwinWriteDecision = { kind: 'skip'; value: T; } | { kind: 'write'; value: T; write: TwinWriteInput; }; /** A recorded request as it was sent: each string the log keeps as a blob, read back. */ export declare function recordedInput(input: unknown): Promise; /** * Decide a state-dependent local write and append it under the same cross-process action lock. * Use this when acceptance or the new fields depend on current projected state; a caller-side * read followed by `applyTwinWrite` is not atomic across processes. */ export declare function applyTwinWriteAtomic(service: string, prepare: (resources: readonly TwinResource[]) => AtomicTwinWriteDecision, root?: string, /** A type index when the decision and its preconditions need only that type, read inside the state lock. */ read?: () => TwinResource[]): Promise<{ value: T; result?: TwinWriteResult; }>; export declare function resolveTwinRead(service: string, pathname: string, opts?: { root?: string; }): { status: number; body: unknown; }; export declare function applyTwinWrite(service: string, write: TwinWriteInput, root?: string): Promise<{ result: TwinWriteResult; resource: TwinResource; }>; /** * Several local writes of one service, landed as `applyTwinWrite` lands each called in turn — the same actions, ids and * order — under one lock and one append (actions.ts `appendActionOccurrences`). Writes that need the one-at-a-time * path take it: a caller's identity (`idempotencyKey`/`actionId`), preconditions, or a head that performs each entry as * it lands. An input is offloaded once per distinct value, as each write's own offload would name it. */ export declare function applyTwinWrites(service: string, writes: readonly TwinWriteInput[], root?: string): Promise>; export declare function createTwinServer(options: { service: string; root?: string; port?: number; readOnly?: boolean; }): Promise<{ port: number; stop: () => void; }>;