Skip to content

Commit 34e8c48

Browse files
committed
🐛 Give the exact-source record to the run that owns it (#722)
The record of which segments an expansion produced as source was a module-scoped `WeakSet`, which `local/no-module-scoped-registry` refuses: one table shared by every execution the process performs, surviving the run that filled it and reachable from no caller's scope. CI caught it where a local lint run did not, because the failure was one error among a thousand warnings. It is now created inside the execution that owns it and handed down through context, beside the segment-cause table it sits next to — reclaimed with the run, answering nothing during the next. The security property is unchanged and is what it always was: membership is keyed by the identity of the segment objects canonical expansion marked, so nothing a document, a package or an import can supply is able to join it. The context is exported for the two core modules that produce and consume the mark and reaches neither public package entrypoint. The emission paths read that record once, where reading is an ordinary operation rather than a durable one. Claude-Session: https://claude.ai/code/session_01DNPFLq52HVSu3NH6WczYLf
1 parent e474dff commit 34e8c48

4 files changed

Lines changed: 86 additions & 32 deletions

File tree

‎packages/core/src/execute.ts‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ import {
9696
import { Component, importComponent, raise } from "./component-api.ts";
9797
import { sourceDescription } from "./source-position.ts";
9898
import { renderSegment } from "./render.ts";
99-
import { isExactSource } from "./output/exact-source.ts";
99+
import { ExactSource, useExactSource } from "./output/exact-source.ts";
100+
import type { ExactSource as ExactSourceRecord } from "./output/exact-source.ts";
100101
import { DocumentOutput } from "./api.ts";
101102
import {
102103
composeBoundExecChain,
@@ -1761,10 +1762,11 @@ function* runValueRoot(
17611762
authority,
17621763
ownBody,
17631764
);
1765+
const exactRecord = yield* ExactSource.get();
17641766
for (const resolved of expanded) {
17651767
const text = renderSegment(resolved);
17661768
if (text) {
1767-
yield* ephemeral(DocumentOutput.operations.output(text, exactly(resolved)));
1769+
yield* ephemeral(DocumentOutput.operations.output(text, exactly(exactRecord, resolved)));
17681770
chunks.push(text);
17691771
}
17701772
}
@@ -1792,8 +1794,8 @@ function* runValueRoot(
17921794
* the run says so (#441).
17931795
*/
17941796
/** Whether one expanded segment carries exact bytes rather than prose. */
1795-
function exactly(segment: Segment): boolean {
1796-
return isExactSource(segment);
1797+
function exactly(exact: ExactSourceRecord | undefined, segment: Segment): boolean {
1798+
return exact !== undefined && exact.has(segment);
17971799
}
17981800

17991801
/**
@@ -1811,14 +1813,17 @@ interface Emission {
18111813
readonly exact: boolean;
18121814
}
18131815

1814-
function emissions(segments: readonly Segment[]): Emission[] {
1816+
function emissions(
1817+
record: ExactSourceRecord | undefined,
1818+
segments: readonly Segment[],
1819+
): Emission[] {
18151820
const runs: Emission[] = [];
18161821
for (const segment of segments) {
18171822
const text = renderSegment(segment);
18181823
if (!text) {
18191824
continue;
18201825
}
1821-
const exact = exactly(segment);
1826+
const exact = exactly(record, segment);
18221827
const last = runs[runs.length - 1];
18231828
if (last !== undefined && last.exact === exact) {
18241829
runs[runs.length - 1] = { text: last.text + text, exact };
@@ -1870,6 +1875,11 @@ function* documentWorkflow(
18701875
// per-segment expansion calls (see spec §6.1).
18711876
const counter = createBlockCounter();
18721877

1878+
// Which segments this run produced as source, read once: the emission paths
1879+
// below include the durable workflow's own, where an ordinary context read is
1880+
// not what `yield*` means.
1881+
const exactRecord = yield* ephemeral(ExactSource.get());
1882+
18731883
// What the document rendered before it stopped, held outside the expansion
18741884
// scope so a failure still leaves it here (§6.9 Partial output). The buffered
18751885
// root fills `selected`; every streaming root has already emitted `streamed`.
@@ -1955,7 +1965,7 @@ function* documentWorkflow(
19551965
undefined,
19561966
);
19571967
// An empty buffered root emits no output event.
1958-
const runs = emissions(selected);
1968+
const runs = emissions(exactRecord, selected);
19591969
for (const run of runs) {
19601970
yield* ephemeral(DocumentOutput.operations.output(run.text, run.exact));
19611971
}
@@ -1993,7 +2003,10 @@ function* documentWorkflow(
19932003
// (non-durable) — output emission is a derived side effect,
19942004
// not journaled.
19952005
yield* ephemeral(
1996-
DocumentOutput.operations.output(text, resolved !== undefined && exactly(resolved)),
2006+
DocumentOutput.operations.output(
2007+
text,
2008+
resolved !== undefined && exactly(exactRecord, resolved),
2009+
),
19972010
);
19982011
streamed.push(text);
19992012
}
@@ -2018,7 +2031,7 @@ function* documentWorkflow(
20182031
// chunks is who the preservation is for, and the completion path only emits
20192032
// for a run that streamed nothing at all — which stops being true as soon
20202033
// as an earlier segment went out.
2021-
const runs = emissions(produced.slice(emittedThrough));
2034+
const runs = emissions(exactRecord, produced.slice(emittedThrough));
20222035
const tail = runs.map((run) => run.text).join("");
20232036
for (const run of runs) {
20242037
yield* ephemeral(DocumentOutput.operations.output(run.text, run.exact));
@@ -2175,6 +2188,9 @@ function* executeDocument(
21752188
// failure from. All created here and reclaimed with this task, so nothing
21762189
// a run decided outlives it.
21772190
yield* useSegmentCauses();
2191+
// Which segments this run produced as source. Scope-owned like the rest:
2192+
// reclaimed with the run, and answering nothing during the next.
2193+
yield* useExactSource();
21782194
yield* usePropsCompiler();
21792195
yield* useParseCompiler();
21802196
const liveFailure: LiveFailureSlot = {};

‎packages/core/src/expand.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2760,7 +2760,7 @@ function* expandComponent(
27602760
// after it produced them — is what carries the fact to the emission loop,
27612761
// which is outside every scope the invocation owned.
27622762
if (authorizedCanonically && authority?.declared?.declaresExact(name) === true) {
2763-
markExactSource(bodyOwner === undefined ? expanded : bodyOwner.slice(renderedFrom));
2763+
yield* markExactSource(bodyOwner === undefined ? expanded : bodyOwner.slice(renderedFrom));
27642764
}
27652765

27662766
if (asBinding) {

‎packages/core/src/output/exact-source.ts‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,63 @@
1010
* `Component.importComponent` middleware publish unpresented bytes by writing a
1111
* field.
1212
*
13-
* So the record lives here, keyed by the identity of the segment objects the
14-
* engine marked, and it is written only where canonical expansion has already
15-
* decided the provenance. It is deliberately not a field on `Segment`: a field
16-
* can be supplied, and this cannot — a segment scanned from a document, parsed
17-
* from a journal, or built by a handler is simply not in this set.
13+
* So the record is kept beside the segments rather than on them, keyed by the
14+
* identity of the objects canonical expansion marked, and written only where
15+
* that expansion has already decided the provenance. A segment scanned from a
16+
* document, parsed from a journal, or built by a handler is simply not in it —
17+
* where a field could be supplied, membership cannot be.
18+
*
19+
* It belongs to the run that made it. The table is created inside the execution
20+
* that owns it and handed down through context, so what one document decided is
21+
* reclaimed with it and answers nothing during the next.
1822
*/
1923

24+
import { createContext } from "effection";
25+
import type { Context, Operation } from "effection";
26+
2027
import type { Segment } from "../types.ts";
2128

29+
/** The segments one execution produced from exact-source components. */
30+
export type ExactSource = WeakSet<Segment>;
31+
2232
/**
23-
* The segments this engine produced from exact-source components.
33+
* Where this execution keeps that record.
2434
*
25-
* Weak, so a segment is remembered exactly as long as something still holds it,
26-
* and identity-keyed, so two segments with identical content are two different
27-
* answers. Module-level because the producer and the emission loop are the same
28-
* loaded copy of core; nothing crosses a copy boundary, and nothing a document
29-
* or a package can reach ever writes to it.
35+
* Module-private: it is exported for the two core modules that produce and
36+
* consume the mark, and reaches neither public package entrypoint, so nothing a
37+
* document or a package can load is able to read or replace it.
3038
*/
31-
const EXACT = new WeakSet<Segment>();
39+
export const ExactSource: Context<ExactSource | undefined> = createContext<ExactSource | undefined>(
40+
"xmd.exact-source",
41+
undefined,
42+
);
43+
44+
/** Install this execution's own record, and hand it back. */
45+
export function* useExactSource(): Operation<ExactSource> {
46+
const exact: ExactSource = new WeakSet();
47+
yield* ExactSource.set(exact);
48+
return exact;
49+
}
3250

33-
/** Record that canonical expansion produced these segments as source. */
34-
export function markExactSource(segments: readonly Segment[]): void {
51+
/**
52+
* Record that canonical expansion produced these segments as source.
53+
*
54+
* With no run installed there is nothing to record into, and nothing is
55+
* published as source — which is the safe answer rather than a lost one.
56+
*/
57+
export function* markExactSource(segments: readonly Segment[]): Operation<void> {
58+
const exact = yield* ExactSource.get();
59+
if (exact === undefined) {
60+
return;
61+
}
3562
for (const segment of segments) {
3663
if (segment.type === "text") {
37-
EXACT.add(segment);
64+
exact.add(segment);
3865
}
3966
}
4067
}
4168

4269
/** Whether this exact segment object is one canonical expansion marked. */
43-
export function isExactSource(segment: Segment): boolean {
44-
return EXACT.has(segment);
70+
export function isExactSource(exact: ExactSource | undefined, segment: Segment): boolean {
71+
return exact !== undefined && exact.has(segment);
4572
}

‎packages/core/tests/declared-markdown-component.test.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import { retainedSource } from "../src/root-source.ts";
5555
import { DocumentOutput } from "../src/api.ts";
5656
import { useNormalizedOutput } from "../src/output/normalize.ts";
5757
import { useTerminalOutput } from "../src/output/terminal.ts";
58-
import { isExactSource } from "../src/output/exact-source.ts";
58+
import { isExactSource, useExactSource } from "../src/output/exact-source.ts";
5959
import type { ComponentInvocation } from "../src/invocation-identity.ts";
6060
import type { ImportedDefinition } from "../src/components/import-authority.ts";
6161
import type { PropsSchema, Segment } from "../src/types.ts";
@@ -1475,10 +1475,21 @@ describe("Tier DM — exact source is a provenance, not a field", () => {
14751475
// carrying any field at all — including the one an earlier design used —
14761476
// answers false. This is the assertion that discriminates: a marker that
14771477
// consulted a field would pass it back.
1478-
expect(
1479-
isExactSource({ type: "text", content: PRESENTABLE, exact: true } as unknown as Segment),
1480-
).toBe(false);
1481-
expect(isExactSource({ type: "text", content: PRESENTABLE } as unknown as Segment)).toBe(false);
1478+
yield* scoped(function* () {
1479+
// The record belongs to the scope that made it, so the questions are
1480+
// asked inside that scope rather than by carrying it out.
1481+
const record = yield* useExactSource();
1482+
expect(
1483+
isExactSource(record, {
1484+
type: "text",
1485+
content: PRESENTABLE,
1486+
exact: true,
1487+
} as unknown as Segment),
1488+
).toBe(false);
1489+
expect(
1490+
isExactSource(record, { type: "text", content: PRESENTABLE } as unknown as Segment),
1491+
).toBe(false);
1492+
});
14821493

14831494
// Then the end-to-end shape, which records a second fact worth keeping:
14841495
// expansion rebuilds text segments, so a field a definition wrote onto its

0 commit comments

Comments
 (0)