Skip to content

Commit 1043460

Browse files
committed
✅ Prove the halted handle refuses and a returned map is the reader's own (#939)
Completes the two selected observations the Planner asked for twice, in the core owned-handle controls. No production change: the resource and copy fixes at babfb6f already behave this way, and these rows are what makes that visible to a test rather than only to a trace. A new C5 row captures safe nested data, holds the run at an explicit barrier — a component that reports it was reached and then suspends, so the invocation is genuinely unfinished rather than waiting on a timer — reads the handle successfully there, then leaves the scope so the owner is halted and joined. The same retained handle then refuses with no map. The successful read before the halt is what makes that a refusal rather than an owner that never had anything. The C2 control now writes through the nested member of one returned settled answer and reads again. The accepted value is unchanged, the retained bytes are identical, and the separate cold execution over them still reads what the run decided. Both rows discriminate: reintroducing the settled-read alias fails C2, and a finalizer that stops clearing its live references fails C5.
1 parent babfb6f commit 1043460

1 file changed

Lines changed: 95 additions & 9 deletions

File tree

‎packages/core/tests/let.test.ts‎

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
import { beforeAll, describe, it } from "@executablemd/test-support/bdd";
1111
import { expect } from "@executablemd/test-support/expect";
12-
import { Err, scoped } from "effection";
12+
import { scoped, suspend, withResolvers } from "effection";
1313
import type { Operation, Result } from "effection";
1414
import { InMemoryStream } from "@executablemd/durable-streams";
1515
import type { DurableEvent, Json } from "@executablemd/durable-streams";
@@ -487,6 +487,41 @@ function readingComponent(counted: Produced): ExecutionInstallation {
487487
};
488488
}
489489

490+
/**
491+
* One host component that says it was reached and then never finishes.
492+
*
493+
* The barrier is the component itself rather than a delay: the run is held at
494+
* a real unfinished operation, and the row below reads the handle while it is
495+
* genuinely in that state instead of while a timer happens to be pending.
496+
*/
497+
function holdingComponent(reached: { open: () => void }): ExecutionInstallation {
498+
return {
499+
components: [
500+
{
501+
name: "Holding",
502+
origin: COMPUTED_ORIGIN,
503+
props: { type: "object", properties: {}, additionalProperties: false },
504+
factory: () =>
505+
function* Holding(): Operation<Json> {
506+
reached.open();
507+
yield* suspend();
508+
return null;
509+
},
510+
},
511+
],
512+
};
513+
}
514+
515+
/** One entry that captures at its root and is then held, unfinished. */
516+
const HOLDS_AFTER_CAPTURE = [
517+
'<Let as="held" value={{ answer: 42 }} />',
518+
"",
519+
"<Holding />",
520+
"",
521+
"Never reached.",
522+
"",
523+
].join("\n");
524+
490525
/** One entry that captures at its root through both sources. */
491526
const COMPUTES_ROOT = [
492527
'<Let as="direct" value={{ answer: 42 }} />',
@@ -580,10 +615,9 @@ describe("Tier LET — the computed root data an execution hands back", () => {
580615
it("C1: the owner reports every actual root capture, and output and value stay separate", function* () {
581616
const counted: Produced = { calls: 0 };
582617
const stream = new InMemoryStream();
583-
const execution = yield* executeInstalled(
584-
{ ...inlineSource(COMPUTES_ROOT), stream },
585-
[readingComponent(counted)],
586-
);
618+
const execution = yield* executeInstalled({ ...inlineSource(COMPUTES_ROOT), stream }, [
619+
readingComponent(counted),
620+
]);
587621
const rendered = String(yield* collect(execution));
588622
const settled = yield* execution;
589623
expect(settled.ok).toBe(true);
@@ -622,19 +656,68 @@ describe("Tier LET — the computed root data an execution hands back", () => {
622656
expect(held["verdict"]).toBe(undefined);
623657
});
624658

659+
it("C5: a halted execution's own handle refuses the root it was reading", function* () {
660+
const stream = new InMemoryStream();
661+
const arrived = withResolvers<void>();
662+
const reached = { open: () => arrived.resolve() };
663+
let retained: HostExecution | undefined;
664+
665+
yield* scoped(function* () {
666+
const execution = yield* executeInstalled({ ...inlineSource(HOLDS_AFTER_CAPTURE), stream }, [
667+
holdingComponent(reached),
668+
]);
669+
// Kept past this scope on purpose: the question is what the handle a host
670+
// is still holding answers once the run behind it is gone.
671+
retained = execution;
672+
yield* arrived.operation;
673+
674+
// While it is unfinished the handle reads, and reads the real thing —
675+
// which is what makes the refusal below a refusal rather than an owner
676+
// that never had anything.
677+
expect(yield* readBindings(execution)).toEqual({ held: { answer: 42 } });
678+
});
679+
680+
// Leaving that scope halts the execution and joins its teardown. The
681+
// document settled nothing, so there is no result to read and no live
682+
// environment left to read instead: the same handle refuses, with no map.
683+
if (retained === undefined) {
684+
throw new Error("this run never produced a handle to retain");
685+
}
686+
const after = yield* retained.bindings();
687+
expect(after.ok).toBe(false);
688+
expect(after.ok ? undefined : after.error).toBeInstanceOf(Error);
689+
expect(Object.hasOwn(after, "value")).toBe(false);
690+
// And halting it acknowledged nothing of its own.
691+
expect(stream.snapshot().some((event) => event.type === "close")).toBe(false);
692+
});
693+
625694
it("C2: an ordinary failure keeps its computed data, and a cold reader finds both", function* () {
626695
const counted: Produced = { calls: 0 };
627696
const stream = new InMemoryStream();
628-
const execution = yield* executeInstalled(
629-
{ ...inlineSource(COMPUTES_THEN_FAILS), stream },
630-
[readingComponent(counted)],
631-
);
697+
const execution = yield* executeInstalled({ ...inlineSource(COMPUTES_THEN_FAILS), stream }, [
698+
readingComponent(counted),
699+
]);
632700
const settled = yield* execution;
633701

634702
// The public outcome is the failure it was, and the live error survives.
635703
expect(settled.ok).toBe(false);
636704
expect(settled.ok ? "" : settled.error.message).toContain("stopped on purpose");
637705
// And the data computed before the failure is still the execution's own.
706+
const answered = yield* readBindings(execution);
707+
expect(answered).toEqual({
708+
direct: { answer: 42 },
709+
functionJson: { answer: 42 },
710+
});
711+
expect(counted.calls).toBe(1);
712+
713+
// C4: the answer is the reader's own graph. Writing through it is allowed
714+
// and reaches nothing: the accepted result is what the execution decided,
715+
// and a consumer editing its copy cannot edit that.
716+
const nested = answered["direct"];
717+
if (nested === null || typeof nested !== "object" || Array.isArray(nested)) {
718+
throw new Error("this control needs a nested object to write through");
719+
}
720+
nested["answer"] = 999;
638721
expect(yield* readBindings(execution)).toEqual({
639722
direct: { answer: 42 },
640723
functionJson: { answer: 42 },
@@ -667,5 +750,8 @@ describe("Tier LET — the computed root data an execution hands back", () => {
667750
});
668751
expect(cold.calls).toBe(0);
669752
expect(reread.snapshot().length).toBe(records.length);
753+
// Neither the bytes the mutation was made against nor a separate execution
754+
// over them moved: the same records, and the same accepted data.
755+
expect(stream.snapshot()).toEqual(records);
670756
});
671757
});

0 commit comments

Comments
 (0)