Skip to content

Commit ceebc74

Browse files
tarasclaude
andcommitted
🚨 Refuse a retained root publication this version cannot read (#939)
Review of 3fc3f29 found that core consumed a retained publication without parsing it. `publish()` discarded what `retain()` handed back, and `retain()` returned the durable operation's stored value unread — so on a continuation a record this version cannot read was accepted as history: the live operand was bound over it, the suffix no record describes ran, and a completion was appended on top. The CLI projector parses the same bytes and refuses them, which is exactly why its parser cannot be the only enforcement: the trusted host reaches `executeInstalled()` directly. `publish()` now reads back what the journal holds and parses it before the caller binds and before any later work. Live, the record parsed is the one just written and the check is free; on a continuation it is the retained record, and an unreadable one raises `StaleInputError` — the same classification a stale loop terminal raises, so it escapes as the run's own durability failure rather than becoming a document error that a printing boundary could absorb. One record contract serves both readers. `parseRootBindingRecord()` holds the version, the binding name, its agreement with the effect name replayed under, and the closed outcome; the event reader applies it after its own event checks. Two readings of one persisted shape would be two contracts, and the day they disagreed is the day a history one of them refused continued through the other. A recognized retained outcome stays authoritative: parsing reads it and never refreshes it from today's operand, so an entry whose eligibility changed on reconstruction still replays the positions it recorded. Ordinary execution, legacy old-mode continuation and entry-local non-JSON behavior are unchanged. The review also named four frozen assertions the evidence had not supplied, and each is completed in the row that already owned it rather than in a new one: - P3 now writes an ordinary component capture inside the `<Each>` iteration, inside two spawned branches and inside an admitted generated fragment. Each runs — five retained symbol reads and the fragment's own admission prove it — and none publishes, reaches the root binding map or reaches the successor. - P9 gains the malformed-version representative on the core continuation path, a record whose binding disagrees with the effect name it replayed under, and the incompatible-retention comparison at core's own owned admission in both directions over a history that holds its terminal, each with a sound control. - P2 mutates a member one level down rather than a scalar, so the retained value's detachment is pinned at that depth in the entry's own binding, in the next entry and at the position that retained the first publication. - G2 opens the completed-prefix execution it then asserts is unchanged, so an append to the history being inspected would be caught rather than hidden behind an untouched copy. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 3fc3f29 commit ceebc74

3 files changed

Lines changed: 394 additions & 40 deletions

File tree

‎packages/cli/tests/repl-entries.test.ts‎

Lines changed: 102 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import { useReplRenderer } from "../src/repl/renderer.ts";
8888
import type { ReplRenderer } from "../src/repl/renderer.ts";
8989
import { committedContext } from "./fixtures/repl/presentation.ts";
9090
import { REFERENCE_DIRECTORY } from "./fixtures/repl/reference.ts";
91+
import { ordinaryGeneratedSelection } from "../src/evaluation-profile.ts";
9192
import {
9293
LEGACY_COMPLETED,
9394
LEGACY_UNFINISHED,
@@ -3997,6 +3998,10 @@ function* capturing(
39973998
source,
39983999
includes: [REFERENCE_DIRECTORY],
39994000
installations: [captureComponents(counted)],
4001+
// What a fragment in these entries may write. Stated because P3's named
4002+
// boundaries include a generated one; the entries that admit no fragment
4003+
// are unaffected by it.
4004+
generated: ordinaryGeneratedSelection(),
40004005
}),
40014006
);
40024007
return { execution, session };
@@ -4024,6 +4029,31 @@ function publications(events: readonly DurableEvent[]): RootBindingPublication[]
40244029
return held;
40254030
}
40264031

4032+
/** Every retained effect type this history holds, in append order. */
4033+
function recordedTypes(events: readonly DurableEvent[]): string[] {
4034+
return events.flatMap((event) => (event.type === "yield" ? [event.description.type] : []));
4035+
}
4036+
4037+
/**
4038+
* The marker of the first retained publication, as the first entry spells it.
4039+
*
4040+
* Derived through the same independent spelling the marker control uses rather
4041+
* than read off a checkpoint: an eval export and a publication both produce a
4042+
* binding checkpoint, so picking the first of those would stand at whichever
4043+
* came first. Valid for the first entry, whose markers the projection does not
4044+
* namespace.
4045+
*/
4046+
function firstPublicationMarker(events: readonly DurableEvent[]): string {
4047+
const at = events.findIndex(
4048+
(event) => event.type === "yield" && event.description.type === ROOT_BINDING_PUBLICATION,
4049+
);
4050+
const marker = at === -1 ? undefined : localMarkers(events)[at];
4051+
if (marker === undefined) {
4052+
throw new Error("this history retained no publication to stand at");
4053+
}
4054+
return marker;
4055+
}
4056+
40274057
/** The names one entry's projected scope holds, in the order they appeared. */
40284058
function names(bindings: readonly { name: string; value: Json }[]): string[] {
40294059
return bindings.map((binding) => binding.name);
@@ -4131,15 +4161,15 @@ const ORDERS_EVAL_FIRST = [
41314161
/** One entry that captures an object and then mutates the live binding. */
41324162
const MUTATES_AFTER_CAPTURE = [
41334163
"```js eval",
4134-
"const original = { keep: 1 };",
4164+
'const original = { outer: { keep: 1 }, label: "first" };',
41354165
"```",
41364166
"",
41374167
'<Let as="alias" value={original} />',
41384168
"",
41394169
"```js eval",
41404170
"const same = alias === original;",
4141-
"alias.keep = 2;",
4142-
"const after = alias.keep;",
4171+
"alias.outer.keep = 2;",
4172+
"const after = alias.outer.keep;",
41434173
"```",
41444174
"",
41454175
"Same: {same}, After: {after}",
@@ -4149,12 +4179,12 @@ const MUTATES_AFTER_CAPTURE = [
41494179
/** One entry that republishes the value it inherited, deliberately. */
41504180
const REPUBLISHES = [
41514181
"```js eval",
4152-
"alias.keep = 3;",
4182+
"alias.outer.keep = 3;",
41534183
"```",
41544184
"",
41554185
'<Let as="alias" value={alias} />',
41564186
"",
4157-
"Three: {alias.keep}",
4187+
"Three: {alias.outer.keep}",
41584188
"",
41594189
].join("\n");
41604190

@@ -4180,17 +4210,33 @@ describe("REPL entries: which publication wins, and which prefix keeps its own (
41804210
it("P2: a mutation republishes nothing, and a deliberate republication does", function* () {
41814211
const { execution, session } = yield* capturing(MUTATES_AFTER_CAPTURE);
41824212
expect((yield* session.join()).ok).toBe(true);
4183-
// The live binding is the operand, so the entry sees its own mutation.
4213+
// The live binding is the operand, so the entry sees its own mutation — and
4214+
// what it mutates is a member one level down, so what the assertions below
4215+
// pin is the depth the retained value is detached to rather than a scalar
4216+
// that any copy would separate.
41844217
expect(session.model.entries[0]?.terminal?.output).toContain("Same: true, After: 2");
4185-
// The retained snapshot was taken at the capture and is detached from it.
4186-
expect(value(session.model.entries[0]?.bindings ?? [], "alias")).toEqual({ keep: 1 });
4218+
// The retained snapshot was taken at the capture and is detached all the way
4219+
// down: the member the entry went on to change is still 1 here.
4220+
expect(value(session.model.entries[0]?.bindings ?? [], "alias")).toEqual({
4221+
outer: { keep: 1 },
4222+
label: "first",
4223+
});
41874224

4188-
expect((yield* following(session, "Two: {alias.keep}\n")).ok).toBe(true);
4225+
expect((yield* following(session, "Two: {alias.outer.keep}\n")).ok).toBe(true);
41894226
expect(session.model.entries[1]?.terminal?.output).toContain("Two: 1");
41904227

41914228
expect((yield* following(session, REPUBLISHES)).ok).toBe(true);
41924229
expect(session.model.entries[2]?.terminal?.output).toContain("Three: 3");
4193-
expect(inherited(yield* execution.stream.readAll())["alias"]).toEqual({ keep: 3 });
4230+
const events = yield* execution.stream.readAll();
4231+
expect(inherited(events)["alias"]).toEqual({ outer: { keep: 3 }, label: "first" });
4232+
4233+
// And the position that retained the first publication keeps its own deep
4234+
// value, after two later entries moved that member twice.
4235+
const first = firstPublicationMarker(events);
4236+
expect(value(projected(events, first).entries[0]?.bindings ?? [], "alias")).toEqual({
4237+
outer: { keep: 1 },
4238+
label: "first",
4239+
});
41944240
});
41954241
});
41964242

@@ -4202,7 +4248,17 @@ const SCOPES = [
42024248
"",
42034249
'<Shape title="Scoped" steps={1} as="shaped" />',
42044250
"",
4205-
'<Each in={[1, 2]} let="n" as="joined">{n}</Each>',
4251+
// An ordinary component capture inside each environment the accepted scope
4252+
// rule names, so what these rows measure is the publication helper running in
4253+
// that environment rather than only the construct's own structural capture.
4254+
'<Each in={[1, 2]} let="n" as="joined"><Syntax as="inEach" />{n}</Each>',
4255+
"",
4256+
"<All>",
4257+
'<Spawn><Syntax as="inSpawn" /></Spawn>',
4258+
'<Spawn><Syntax as="alsoSpawn" /></Spawn>',
4259+
"</All>",
4260+
"",
4261+
`<Evaluate text={${JSON.stringify('<Syntax as="inFragment" />\n')}} allow={["read"]} />`,
42064262
"",
42074263
"Scoped.",
42084264
"",
@@ -4216,6 +4272,10 @@ const READS_SCOPES = [
42164272
" typeof shapeLocal,",
42174273
" typeof n,",
42184274
" typeof joined,",
4275+
" typeof inEach,",
4276+
" typeof inSpawn,",
4277+
" typeof alsoSpawn,",
4278+
" typeof inFragment,",
42194279
" typeof props,",
42204280
'].join("/");',
42214281
"```",
@@ -4235,22 +4295,30 @@ describe("REPL entries: whose root a capture belongs to (#939)", () => {
42354295
// written at the root, so it publishes like any other root capture.
42364296
const first = session.model.entries[0];
42374297
expect(value(first?.bindings ?? [], "chosen")).toBe("under if");
4238-
// The component's own `<Let>`, the iteration's `let`, and the structural
4239-
// capture `<Each as>` makes are each somebody else's — and none of them
4240-
// becomes a root value because the file it was written in is the root's.
4241-
expect(names(first?.bindings ?? [])).not.toContain("shapeLocal");
4242-
expect(names(first?.bindings ?? [])).not.toContain("n");
4243-
expect(names(first?.bindings ?? [])).not.toContain("joined");
4244-
expect(publications(yield* execution.stream.readAll()).map((one) => one.binding)).toEqual([
4245-
"chosen",
4246-
"shaped",
4247-
]);
4298+
4299+
const events = yield* execution.stream.readAll();
4300+
// Each local capture really ran, which is what makes their absence below an
4301+
// absence of publication rather than an absence of work: one `<Syntax>` read
4302+
// per iteration, one per spawned branch, and one inside the fragment.
4303+
expect(recordedTypes(events).filter((type) => type === "syntax_symbols").length).toBe(5);
4304+
expect(recordedTypes(events)).toContain("generated_xmd");
4305+
4306+
// The component's own `<Let>`, the iteration's `let`, the structural capture
4307+
// `<Each as>` makes, and the ordinary captures written inside the iteration,
4308+
// the spawned branches and the admitted fragment are each somebody else's —
4309+
// and none of them becomes a root value because the file it was written in
4310+
// is the root's.
4311+
const locals = ["shapeLocal", "n", "joined", "inEach", "inSpawn", "alsoSpawn", "inFragment"];
4312+
for (const local of locals) {
4313+
expect(names(first?.bindings ?? [])).not.toContain(local);
4314+
}
4315+
expect(publications(events).map((one) => one.binding)).toEqual(["chosen", "shaped"]);
42484316

42494317
expect((yield* following(session, READS_SCOPES)).ok).toBe(true);
42504318
// `props` is the entry's own namespace, which every entry validates for
42514319
// itself: inherited or not, it is an object here because this root has one.
42524320
expect(session.model.entries[1]?.terminal?.output).toContain(
4253-
"Read: under if/undefined/undefined/undefined/object",
4321+
"Read: under if/undefined/undefined/undefined/undefined/undefined/undefined/undefined/object",
42544322
);
42554323
});
42564324
});
@@ -4672,6 +4740,10 @@ describe("REPL entries: a history written before captures were retained (#939)",
46724740
true,
46734741
);
46744742

4743+
// The completed entry alone, which is the history this row inspects.
4744+
const completed = events.slice(0, 5);
4745+
expect(completed.filter((event) => event.type === "close").length).toBe(1);
4746+
46754747
const cold = projected(events);
46764748
expect(cold.entries.map((entry) => entry.settled)).toEqual([true, false]);
46774749
// Only the eval export it really recorded. The four captures that entry
@@ -4682,22 +4754,23 @@ describe("REPL entries: a history written before captures were retained (#939)",
46824754
expect(LEGACY_COMPLETED).toContain('as="legacySummary"');
46834755
expect(publications(events)).toEqual([]);
46844756

4685-
// Reading it adds nothing: a settled entry is projected, never resumed.
4686-
const holder = replExecution(events);
4757+
// Reading it adds nothing: a settled entry is projected, never resumed. The
4758+
// execution inspected is the one the equality below reads, so an append to
4759+
// it would be caught rather than hidden behind an untouched copy.
4760+
const holder = replExecution(completed);
46874761
yield* scoped(function* () {
46884762
const performed = yield* countPerformed();
46894763
const reading = opened(
4690-
yield* openReplSession({
4691-
execution: replExecution(events.slice(0, 5)),
4692-
includes: [REFERENCE_DIRECTORY],
4693-
}),
4764+
yield* openReplSession({ execution: holder, includes: [REFERENCE_DIRECTORY] }),
46944765
);
46954766
expect(reading.live).toBe(false);
4767+
expect(names(reading.model.entries[0]?.bindings ?? [])).toEqual(["legacyEval"]);
46964768
yield* reading.join();
46974769
expect(performed.compiles).toBe(0);
46984770
expect(performed.reads).toEqual([]);
4771+
expect(yield* holder.stream.readAll()).toEqual(completed);
46994772
});
4700-
expect(yield* holder.stream.readAll()).toEqual(events);
4773+
expect(yield* holder.stream.readAll()).toEqual(completed);
47014774
});
47024775

47034776
it("G2: an unfinished legacy entry continues in its own mode through its close", function* () {

‎packages/core/src/root-binding-publications.ts‎

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838

3939
import { Err, Ok, useScope } from "effection";
4040
import type { Operation, Result } from "effection";
41-
import { createDurableOperation, DurableContext } from "@executablemd/durable-streams";
41+
import {
42+
createDurableOperation,
43+
DurableContext,
44+
StaleInputError,
45+
} from "@executablemd/durable-streams";
4246
import type {
4347
DurableEvent,
4448
EffectDescription,
@@ -128,7 +132,8 @@ export function createRootBindingPublications(root: EvalEnv): RootBindingPublica
128132
// retains nothing and binds exactly as it would have. There is no history
129133
// for a position in, so there is no sequence to keep either.
130134
const scope = yield* useScope();
131-
if (scope.get(DurableContext) === undefined) {
135+
const durable = scope.get(DurableContext);
136+
if (durable === undefined) {
132137
return;
133138
}
134139
const sequence = published;
@@ -137,10 +142,25 @@ export function createRootBindingPublications(root: EvalEnv): RootBindingPublica
137142
// snapshot is what the journal keeps, and reading it later would read
138143
// whatever the entry had done to the value in between.
139144
const outcome = snapshot(value);
140-
yield* retain(publicationName(sequence, binding), position, {
141-
binding,
142-
publication: outcome,
143-
});
145+
const name = publicationName(sequence, binding);
146+
const stored = yield* retain(name, position, { binding, publication: outcome });
147+
// Read back and parsed, which is the only way a replay can be told from a
148+
// live run here: `createDurableOperation` answers with the value the
149+
// journal already held, so this is where a retained publication this
150+
// version cannot read is discovered. Live it parses the record just
151+
// written, and the check costs nothing.
152+
//
153+
// Before the caller binds and before any later authored work: a history
154+
// whose publication cannot be read is not one this run may continue, so
155+
// it must not reach the binding, the suffix no record describes, or a
156+
// completion appended on top of it.
157+
const held = parseRootBindingRecord(stored, name);
158+
if (!held.ok) {
159+
throw new StaleInputError(UNREADABLE_PUBLICATION, {
160+
coroutineId: durable.coroutineId,
161+
description: { type: ROOT_BINDING_PUBLICATION, name },
162+
});
163+
}
144164
},
145165
};
146166
}
@@ -278,7 +298,7 @@ export const UNREADABLE_PUBLICATION =
278298
* write.
279299
*/
280300
export function parseRootBindingPublication(event: DurableEvent): Result<RootBindingPublication> {
281-
let record: Json;
301+
let carried: unknown;
282302
let name: unknown;
283303
try {
284304
if (event.type !== "yield" || event.description.type !== ROOT_BINDING_PUBLICATION) {
@@ -288,7 +308,38 @@ export function parseRootBindingPublication(event: DurableEvent): Result<RootBin
288308
return Err(new Error(UNREADABLE_PUBLICATION));
289309
}
290310
name = event.description.name;
291-
record = parseJson(event.result.value);
311+
carried = event.result.value;
312+
} catch {
313+
return Err(new Error(UNREADABLE_PUBLICATION));
314+
}
315+
if (typeof name !== "string") {
316+
return Err(new Error(UNREADABLE_PUBLICATION));
317+
}
318+
return parseRootBindingRecord(carried, name);
319+
}
320+
321+
/**
322+
* One retained publication record, parsed against the effect name it was
323+
* recorded under.
324+
*
325+
* The one contract both readers are held to: the projection reads a journal
326+
* somebody else wrote, and the publisher reads what its own durable operation
327+
* handed back — which on a continuation is the retained record rather than the
328+
* one just built. Two readings of this shape would be two contracts, and the
329+
* day they disagreed is the day a history one of them refused continued
330+
* through the other.
331+
*
332+
* The name is part of it. Replay matches a position by description, so a record
333+
* naming a different binding from the effect it came back for is a record this
334+
* version did not write there.
335+
*/
336+
export function parseRootBindingRecord(
337+
carried: unknown,
338+
name: string,
339+
): Result<RootBindingPublication> {
340+
let record: Json;
341+
try {
342+
record = parseJson(carried);
292343
} catch {
293344
return Err(new Error(UNREADABLE_PUBLICATION));
294345
}
@@ -306,7 +357,7 @@ export function parseRootBindingPublication(event: DurableEvent): Result<RootBin
306357
if (!validated.ok || validated.value !== binding) {
307358
return Err(new Error(UNREADABLE_PUBLICATION));
308359
}
309-
if (typeof name !== "string" || !namesBinding(name, binding)) {
360+
if (!namesBinding(name, binding)) {
310361
return Err(new Error(UNREADABLE_PUBLICATION));
311362
}
312363
const outcome = readOutcome(record["publication"]);

0 commit comments

Comments
 (0)