/** * @fileoverview Render-phase attribution capture. * * The capture mechanism for gen-maps. A {@link CaptureSink} is owned by * {@link import('@/context/RenderContext.ts').RenderContext} for the * duration of the single render pass and published to snippets through the * shared {@link CaptureChannel}: every `SnippetBase` instance self-wraps * its `toString` at construction (own-property wrapper), and the wrapper * observes into the channel's sink while the **capture interval** is open * — the span while `channel.sink` is set. Each `toString` invocation is * observed into an **occurrence tree** — one node per call, keyed by * invocation, not by instance. The wrapper is a pure pass-through: it * returns the subclass `toString`'s output verbatim and only *observes* * it. * * This replaces the old module-global constructor registry + * render-time prototype wrapping: discovery is per-instance (self-wrap at * birth — snippets constructed mid-render are captured too), the sink * travels by object reference through `this.context` (no duplicate-module * silent-failure hazard), and opening/closing the interval is a flag flip * (no install/restore pass). Outside the interval there is no state, so a * stray `toString()` during the generate phase captures nothing. * * Offset capture (this is the "store output, locate via `indexOf`" cut — * RESUME §3.3 option (a)): each node stores its rendered output; after a * file renders, {@link CaptureSink.spansForFile} locates each node's output * inside its parent's output (moving cursor) to recover byte ranges. A * later memory-efficient cut computes offsets inline as the stack unwinds * and frees the strings; the resolver interface ({offset,length,children}) * is unchanged by that switch. * * Offsets are UTF-16 code-unit indices (`indexOf` / `String.length`), * consistent with the rest of the attribution layer. */ import type { SnippetBase } from '../dsl/SnippetBase.js'; import type { Span } from './types.js'; /** * The shared slot through which the capture sink travels — created once * per run by `CoreContext`, handed to `GenerateContext` (snippets read it * via `this.context.captureSink`) and to `RenderContext` (which sets * `channel.sink` around the one capturing render and clears it in * `finally`). The capture interval is exactly the span while `sink` is * set. */ export type CaptureChannel = { sink: CaptureSink | undefined; }; /** * Transient capture state for one render pass. Owned by `RenderContext`. * * `active` gates {@link observe}: only while a file is rendering (inside * {@link captureFile}) does an observation record; otherwise `observe` is * an inert pass-through. This keeps a stray `toString()` between files * from polluting the tree even while the capture interval is open. */ export declare class CaptureSink { #private; /** True while a file is rendering — {@link observe} checks this. */ get active(): boolean; /** * Render one file with capture active and return its text plus the byte * spans for every contributing Definition / Snippet. The wrapper builds * the occurrence tree as `render()` runs; spans are resolved from that * tree against the returned file text. */ captureFile(render: () => string): { text: string; spans: Span[]; }; /** * Observe one `toString` invocation. The per-instance wrapper installed * by the `SnippetBase` constructor calls this whenever the capture * interval is open. Outside a file render (`active` false) it is a pure * pass-through; inside one it pushes an occurrence node, links it to its * parent (or the file roots), runs the real `toString`, records the * output, and returns it verbatim. * * @throws when `producer` is already live on the stack — a snippet that * composes itself, which would otherwise infinitely recurse. */ observe(producer: SnippetBase, impl: (this: SnippetBase) => string): string; }