Skip to content

Commit c191fd0

Browse files
committed
🐛 Deliver a projection failure to its caller instead of the content scope
A throw-bound string projection failed inside the task it ran on the invocation's content scope, so the failure poisoned the scope and teardown re-reported it — an eval block that caught the DocumentationError could never recover. runProjection now resolves its outcome the way runInContentScope already does: the failure travels back to the caller, a caught error is explicit recovery, and the work the projected content started is torn down with its scope. Adds the two regressions from #233's contract: O32 (caught error recovers, nothing recorded, capture succeeds) and O33 (recovery leaks no projection-owned work past the invocation).
1 parent 6450699 commit c191fd0

3 files changed

Lines changed: 155 additions & 29 deletions

File tree

‎packages/core/src/expand.ts‎

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -311,40 +311,54 @@ function createProjectionHandle(state: ProjectionState): ProjectionHandle {
311311
const inner = request.kind === "markdown" ? handle : state.enclosing;
312312

313313
return yield* scoped(function* () {
314+
// The projection's failure travels back to the caller rather than into
315+
// the content scope (see runInContentScope): a throw-bound failure the
316+
// caller catches is explicit recovery, and the invocation must not
317+
// re-report it as a teardown error.
318+
const outcome = withResolvers<{ segments: Segment[] } | { failure: unknown }>();
314319
const task = contentScope.scope.run(function* () {
315-
yield* AmbientErrorPolicy.set(policy);
316-
// Slot errors are reported inside the policy-bound task, so an empty
317-
// selection cannot settle them under the invocation's baseline.
318-
const errors: Segment[] = [];
319-
if (!slotErrorsEmitted && slots.errors.length > 0) {
320-
slotErrorsEmitted = true;
321-
for (const slotError of slots.errors) {
322-
errors.push(yield* raise(slotError));
320+
try {
321+
yield* AmbientErrorPolicy.set(policy);
322+
// Slot errors are reported inside the policy-bound task, so an empty
323+
// selection cannot settle them under the invocation's baseline.
324+
const errors: Segment[] = [];
325+
if (!slotErrorsEmitted && slots.errors.length > 0) {
326+
slotErrorsEmitted = true;
327+
for (const slotError of slots.errors) {
328+
errors.push(yield* raise(slotError));
329+
}
323330
}
331+
if (segments.length === 0) {
332+
outcome.resolve({ segments: errors });
333+
return;
334+
}
335+
yield* provideEvalScope(contentScope);
336+
const projectionEnv = environmentFor(request);
337+
if (projectionEnv) {
338+
yield* provideEnv(projectionEnv);
339+
}
340+
yield* ActiveProjection.set(inner);
341+
// Dynamic markdown is the component's own text, so it is not written
342+
// where the caller's loop is and cannot break it.
343+
yield* ActiveLoop.set(request.kind === "markdown" ? undefined : state.callerLoop);
344+
const rendered = yield* expandSegments(
345+
project(segments),
346+
state.meta,
347+
state.props,
348+
state.hideSet,
349+
state.counter,
350+
);
351+
outcome.resolve({ segments: [...errors, ...rendered] });
352+
} catch (error) {
353+
outcome.resolve({ failure: error });
324354
}
325-
if (segments.length === 0) {
326-
return errors;
327-
}
328-
yield* provideEvalScope(contentScope);
329-
const projectionEnv = environmentFor(request);
330-
if (projectionEnv) {
331-
yield* provideEnv(projectionEnv);
332-
}
333-
yield* ActiveProjection.set(inner);
334-
// Dynamic markdown is the component's own text, so it is not written
335-
// where the caller's loop is and cannot break it.
336-
yield* ActiveLoop.set(request.kind === "markdown" ? undefined : state.callerLoop);
337-
const rendered = yield* expandSegments(
338-
project(segments),
339-
state.meta,
340-
state.props,
341-
state.hideSet,
342-
state.counter,
343-
);
344-
return [...errors, ...rendered];
345355
});
346356
yield* ensure(() => task.halt());
347-
return yield* task;
357+
const result = yield* outcome.operation;
358+
if ("failure" in result) {
359+
throw result.failure;
360+
}
361+
return result.segments;
348362
});
349363
}
350364

@@ -1687,6 +1701,8 @@ function* expandComponent(
16871701
// A capture never swallows an error. The body reported these where they
16881702
// were created (§6.9) — including the ones a string projection rendered
16891703
// away — so they are handed back as they are and the binding stays unset.
1704+
// Recorded errors precede expanded ones regardless of source position;
1705+
// the order across the two lists is deliberately unspecified.
16901706
const errors = [
16911707
...bodyContentErrors,
16921708
...expanded.filter((capturedSegment) => capturedSegment.type === "error"),

‎packages/core/tests/eval-policy.test.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,106 @@ describe("Tier O — Eval scope hierarchy", () => {
242242
// Reported once on the way out, not again when it crosses back.
243243
expect(String(output).split("Cannot resolve component: Missing").length - 1).toBe(1);
244244
});
245+
246+
// O32: a caught DocumentationError is explicit recovery. The throw-bound
247+
// projection fails before anything is recorded for the capture refusal, so
248+
// the recovered component completes and the capture succeeds.
249+
it("O32: a caught projection error recovers without a later capture refusal", function* () {
250+
const stream = new InMemoryStream();
251+
yield* useStubFs({
252+
"components/Wrap.md": [
253+
"```ts eval",
254+
"const projected = yield* call(function* () {",
255+
" try {",
256+
" return yield* renderChildren();",
257+
" } catch (error) {",
258+
' return "recovered";',
259+
" }",
260+
"});",
261+
"```",
262+
"",
263+
"<Output>",
264+
"```ts eval",
265+
"output(projected);",
266+
"```",
267+
"</Output>",
268+
].join("\n"),
269+
"doc.md": '<Wrap as="cap">\n<Missing />\n</Wrap>\n\nvalue:{cap}:end',
270+
});
271+
yield* useEchoExec();
272+
273+
const output = yield* collect(yield* execute({ path: "doc.md", stream }));
274+
275+
// The caught error is not reasserted: the binding holds the recovered
276+
// output and no error segment reaches the caller.
277+
expect(output).toContain("recovered");
278+
expect(output).not.toContain("{cap}");
279+
expect(output).not.toContain("ERROR");
280+
});
281+
282+
// O33: work the projected content started is torn down with its scope, not
283+
// leaked past the invocation because the projection's error was caught.
284+
it("O33: projection-owned work does not escape a caught projection error", function* () {
285+
const stream = new InMemoryStream();
286+
yield* useStubFs({
287+
"components/Wrap.md": [
288+
"```ts eval",
289+
"const projected = yield* call(function* () {",
290+
" try {",
291+
" return yield* renderChildren();",
292+
" } catch (error) {",
293+
' return "recovered";',
294+
" }",
295+
"});",
296+
"```",
297+
"",
298+
"<Output>",
299+
"```ts eval",
300+
"output(projected);",
301+
"```",
302+
"</Output>",
303+
].join("\n"),
304+
"doc.md": [
305+
"```js eval",
306+
"const probe = { started: false, halted: false };",
307+
"```",
308+
"",
309+
'<Wrap as="cap">',
310+
"```js persist eval",
311+
"yield* spawn(function*() {",
312+
" probe.started = true;",
313+
" try {",
314+
" yield* suspend();",
315+
" } finally {",
316+
" probe.halted = true;",
317+
" }",
318+
"});",
319+
"```",
320+
"",
321+
"```js eval",
322+
"yield* when(function*() {",
323+
' if (!probe.started) throw new Error("not yet");',
324+
"});",
325+
"```",
326+
"",
327+
"<Missing />",
328+
"</Wrap>",
329+
"",
330+
"value:{cap}:end",
331+
"",
332+
"```js eval",
333+
"output(`halted=${probe.halted}`);",
334+
"```",
335+
].join("\n"),
336+
});
337+
yield* useEchoExec();
338+
339+
const output = yield* collect(yield* execute({ path: "doc.md", stream }));
340+
341+
// The suspended task the children spawned is gone by the time the sibling
342+
// runs: recovery completed, and catching the error leaked nothing.
343+
expect(output).toContain("recovered");
344+
expect(output).toContain("halted=true");
345+
expect(output).not.toContain("ERROR");
346+
});
245347
});

‎specs/executable-mdx-spec.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,12 @@ documentation stops the body, and the same error inside an output region renders
25032503
as a comment. It is reported once, where it is created, and passes back to the
25042504
caller as an ordinary segment.
25052505

2506+
A throwing projection fails toward its caller, not into the content scope. An
2507+
eval block that catches the `DocumentationError` has recovered it explicitly:
2508+
nothing was recorded for a capture refusal, the invocation completes without
2509+
re-reporting the caught failure, and the work the projected content started is
2510+
torn down with its scope rather than escaping past the invocation.
2511+
25062512
`substituteContent` resolves the slots:
25072513

25082514
```typescript
@@ -5468,6 +5474,8 @@ visible warning blocks, collect into a separate error report).
54685474
| O23 | Persistent projection in documentation | A `persist eval` block's projection settles under the throwing policy of the block's own position, not the invocation's baseline |
54695475
| O24 | Persistent projection inside `<Output>` | The same block inside a region collects instead, and the projected error renders |
54705476
| O31 | Captured markdown projection | The same block under `as=` refuses the binding: the interpolation stays literal and the recorded error returns to the caller once |
5477+
| O32 | Caught projection error | A caught `DocumentationError` is explicit recovery: nothing is recorded, the component completes, and the capture succeeds |
5478+
| 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 |
54715479
| O22 | Durability composes | A component combining a durable effect with a directly acquired resource: across a partial replay the effect's executor runs once, output is identical, and the resource is re-established per execution |
54725480

54735481
### Tier CW — Contextual working directory

0 commit comments

Comments
 (0)