Skip to content

Commit b85d96e

Browse files
committed
✅ Cover every lone teardown stage in O35
O35 now runs each stage — content, body, eval — as the only failure with a successful body, asserting the InvocationTeardownError shape, identity of the planted failure through causes and cause, and the full stage timeline. The eval-only case is the regression that guards the destructor ordering: against the pre-fix code it exposes the raw eval-stage failure the frame's re-halt substitutes for the wrapper. Also renames the suite to "Tier O — Invocation failure domains" to match the file and the §4.4 contract, and hoists the shared timeline expectation. The probe keeps its ensure()-registered cleanup: the local/no-yield-in-finally rule forbids suspending in a finally, so the Effection AGENTS.md finally-around-provide shape is not available here. Refs #234
1 parent 5aca16e commit b85d96e

2 files changed

Lines changed: 44 additions & 36 deletions

File tree

‎packages/core/tests/invocation-failures.test.ts‎

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ import { InvocationTeardownError, withInvocation } from "../src/invocation.ts";
1616
import { DocumentationError, fatalCause } from "../src/errors.ts";
1717
import type { Invocation } from "../src/invocation.ts";
1818

19+
type StageName = "content" | "body" | "eval";
20+
1921
/**
2022
* Records `start:<label>` on acquisition and `stop:<label>` on teardown, and
21-
* fails the teardown with the given error. The destructor genuinely yields
23+
* fails the teardown with the given error. The teardown genuinely yields
2224
* before it reports, so a stage has to await it rather than observe a
2325
* synchronous throw.
2426
*/
2527
function probe(timeline: string[], label: string, failure?: Error): Operation<void> {
2628
return resource(function* (provide) {
2729
timeline.push(`start:${label}`);
30+
// Cleanup registers through ensure() rather than a finally around
31+
// provide(): `local/no-yield-in-finally` forbids suspending in a finally.
2832
yield* ensure(function* () {
2933
yield* sleep(1);
3034
timeline.push(`stop:${label}`);
@@ -40,14 +44,23 @@ function probe(timeline: string[], label: string, failure?: Error): Operation<vo
4044
function* plantStages(
4145
invocation: Invocation,
4246
timeline: string[],
43-
failures: { content?: Error; body?: Error; eval?: Error },
47+
failures: Partial<Record<StageName, Error>>,
4448
): Operation<void> {
4549
yield* invocation.evalScope.eval(() => probe(timeline, "eval", failures.eval));
4650
const content = yield* invocation.useContentScope();
4751
yield* content.eval(() => probe(timeline, "content", failures.content));
4852
yield* probe(timeline, "body", failures.body);
4953
}
5054

55+
const FULL_TIMELINE = [
56+
"start:eval",
57+
"start:content",
58+
"start:body",
59+
"stop:content",
60+
"stop:body",
61+
"stop:eval",
62+
];
63+
5164
function* capture(op: () => Operation<unknown>): Operation<unknown> {
5265
try {
5366
yield* op();
@@ -57,7 +70,7 @@ function* capture(op: () => Operation<unknown>): Operation<unknown> {
5770
throw new Error("expected the invocation to fail");
5871
}
5972

60-
describe("Tier O — Eval scope hierarchy", () => {
73+
describe("Tier O — Invocation failure domains", () => {
6174
it("O34: a body failure is rethrown by identity when teardown succeeds", function* () {
6275
const timeline: string[] = [];
6376
const sentinel = new Error("body failure");
@@ -72,34 +85,36 @@ describe("Tier O — Eval scope hierarchy", () => {
7285

7386
expect(caught).toBe(sentinel);
7487
// Every stage still ran, in the boundary's order.
75-
expect(timeline).toEqual([
76-
"start:eval",
77-
"start:content",
78-
"start:body",
79-
"stop:content",
80-
"stop:body",
81-
"stop:eval",
82-
]);
88+
expect(timeline).toEqual(FULL_TIMELINE);
8389
});
8490

8591
it("O35: a lone teardown failure keeps the InvocationTeardownError shape", function* () {
86-
const timeline: string[] = [];
87-
const stage = new Error("stage failure");
88-
89-
const caught = yield* capture(() =>
90-
withInvocation(function* (invocation) {
91-
yield* plantStages(invocation, timeline, { body: stage });
92-
return "ok";
93-
}),
94-
);
95-
96-
expect(caught).toBeInstanceOf(InvocationTeardownError);
97-
if (!(caught instanceof InvocationTeardownError)) {
98-
throw new Error("unreachable");
92+
// Every stage gets its turn as the only failure; the eval case is the one
93+
// that guards the destructor ordering — the frame's re-halt of `evalHost`
94+
// would otherwise expose the raw stage failure instead of the wrapper.
95+
const stages: StageName[] = ["content", "body", "eval"];
96+
for (const stage of stages) {
97+
const timeline: string[] = [];
98+
const planted = new Error(`${stage} stage failure`);
99+
const failures: Partial<Record<StageName, Error>> = {};
100+
failures[stage] = planted;
101+
102+
const caught = yield* capture(() =>
103+
withInvocation(function* (invocation) {
104+
yield* plantStages(invocation, timeline, failures);
105+
return "ok";
106+
}),
107+
);
108+
109+
expect(caught).toBeInstanceOf(InvocationTeardownError);
110+
if (!(caught instanceof InvocationTeardownError)) {
111+
throw new Error("unreachable");
112+
}
113+
expect(caught.causes).toHaveLength(1);
114+
expect(caught.causes[0]).toBe(planted);
115+
expect(caught.cause).toBe(planted);
116+
expect(timeline).toEqual(FULL_TIMELINE);
99117
}
100-
expect(caught.causes).toHaveLength(1);
101-
expect(caught.causes[0]).toBe(stage);
102-
expect(caught.cause).toBe(stage);
103118
});
104119

105120
it("O36: a body failure and stage failures produce the two-domain aggregate", function* () {
@@ -199,13 +214,6 @@ describe("Tier O — Eval scope hierarchy", () => {
199214
}),
200215
);
201216

202-
expect(timeline).toEqual([
203-
"start:eval",
204-
"start:content",
205-
"start:body",
206-
"stop:content",
207-
"stop:body",
208-
"stop:eval",
209-
]);
217+
expect(timeline).toEqual(FULL_TIMELINE);
210218
});
211219
});

‎specs/executable-mdx-spec.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5485,7 +5485,7 @@ visible warning blocks, collect into a separate error report).
54855485
| O32 | Caught projection error | A caught `DocumentationError` is explicit recovery: nothing is recorded, the component completes, and the capture succeeds |
54865486
| O33 | Recovery leaks nothing | Work the projected content started is torn down with its scope; catching the error lets none of it escape the invocation |
54875487
| O34 | Body failure, clean teardown | The body's failure is rethrown by identity and every stage still runs |
5488-
| O35 | Lone teardown failure | The single teardown error keeps its shape and carries the stage failure by identity |
5488+
| O35 | Lone teardown failure | Each stage alone — content, body and eval — yields the single teardown error carrying the planted failure by identity, and every stage still runs |
54895489
| O36 | Both domains fail | One aggregate: the body failure first, then one teardown error with every stage failure in stage order, all by identity |
54905490
| O37 | Documentation failure survives teardown | A `DocumentationError` body failure stays discoverable through the aggregate |
54915491
| O38 | Durability failure survives teardown | A durability body failure plus a teardown failure is still fatal |

0 commit comments

Comments
 (0)