Skip to content

Commit c05eb46

Browse files
fix(memory): preserve cancellation when optional lookup fails
Check interruption before recovering typed failures or defects, including mixed causes. Keep defect warnings bounded to project and run identifiers. Verify full prompt submission after optional failures and no submission after cancellation.
1 parent cd30fa7 commit c05eb46

2 files changed

Lines changed: 114 additions & 79 deletions

File tree

‎apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts‎

Lines changed: 104 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,82 +1451,115 @@ effectIt.effect("recovers an abandoned failed turn before continuing its native
14511451
}),
14521452
);
14531453

1454-
effectIt.effect("offers bounded fact references to unattended first turns only", () =>
1455-
Effect.gen(function* () {
1456-
const text = "Review SQLite migrations for project memory";
1457-
const projectId = ProjectId.make("project-native-account-command");
1458-
const suggest = vi.fn<ProjectKnowledge.ProjectKnowledge["Service"]["suggest"]>(() =>
1459-
Effect.succeed([
1460-
{
1461-
fact: {
1462-
id: ProjectFactId.make("fact-migrations"),
1463-
projectId,
1464-
status: "saved",
1465-
kind: "decision",
1466-
origin: { kind: "user" },
1467-
content: "Private assertion body",
1468-
entities: [],
1469-
tags: [],
1470-
sources: [{ kind: "user" }],
1471-
revision: 1,
1472-
trust: 0.8,
1473-
retrievalCount: 0,
1474-
createdAt: "2026-09-04T12:00:00.000Z",
1475-
updatedAt: "2026-09-04T12:00:00.000Z",
1476-
forgottenAt: null,
1477-
},
1478-
score: {
1479-
holographic: 1,
1480-
lexical: 1,
1481-
tokenOverlap: 1,
1482-
temporalDecay: 1,
1483-
reinforcement: 0,
1484-
trust: 0.8,
1485-
combined: 1,
1454+
effectIt.effect.each([
1455+
"success",
1456+
"error",
1457+
"defect",
1458+
"interrupted",
1459+
"interrupted-error",
1460+
"interrupted-defect",
1461+
] as const)(
1462+
"offers bounded fact references to unattended first turns with lookup outcome %s",
1463+
(outcome) =>
1464+
Effect.gen(function* () {
1465+
const text = "Review SQLite migrations for project memory. ".repeat(100);
1466+
const projectId = ProjectId.make("project-native-account-command");
1467+
const suggest = vi.fn<ProjectKnowledge.ProjectKnowledge["Service"]["suggest"]>(() => {
1468+
if (outcome === "error")
1469+
return Effect.fail(new ProjectKnowledgeError({ reason: "storage", projectId }));
1470+
if (outcome === "defect") return Effect.die(new Error("Memory search encoding failed"));
1471+
if (outcome === "interrupted") return Effect.interrupt;
1472+
if (outcome === "interrupted-error" || outcome === "interrupted-defect")
1473+
return Effect.failCause(
1474+
Cause.combine(
1475+
Cause.interrupt(),
1476+
outcome === "interrupted-error"
1477+
? Cause.fail(new ProjectKnowledgeError({ reason: "storage", projectId }))
1478+
: Cause.die(new Error("Memory cleanup failed")),
1479+
),
1480+
);
1481+
return Effect.succeed([
1482+
{
1483+
fact: {
1484+
id: ProjectFactId.make("fact-migrations"),
1485+
projectId,
1486+
status: "saved",
1487+
kind: "decision",
1488+
origin: { kind: "user" },
1489+
content: "Private assertion body",
1490+
entities: [],
1491+
tags: [],
1492+
sources: [{ kind: "user" }],
1493+
revision: 1,
1494+
trust: 0.8,
1495+
retrievalCount: 0,
1496+
createdAt: "2026-09-04T12:00:00.000Z",
1497+
updatedAt: "2026-09-04T12:00:00.000Z",
1498+
forgottenAt: null,
1499+
},
1500+
score: {
1501+
holographic: 1,
1502+
lexical: 1,
1503+
tokenOverlap: 1,
1504+
temporalDecay: 1,
1505+
reinforcement: 0,
1506+
trust: 0.8,
1507+
combined: 1,
1508+
},
1509+
reason: "Private match explanation",
14861510
},
1487-
reason: "Private match explanation",
1488-
},
1489-
]),
1490-
);
1491-
for (const [source, ordinal, defects] of [
1492-
["web", 1, false],
1493-
["mobile", 1, false],
1494-
["mcp", 2, false],
1495-
["mcp", 1, false],
1496-
["mcp", 1, true],
1497-
] as const) {
1498-
suggest.mockClear();
1499-
if (defects) suggest.mockImplementationOnce(() => Effect.die(new Error("memory defect")));
1500-
const harness = makeLocalCommandHarness({
1501-
text,
1502-
previousTurn: { runStatus: "completed", turnStatus: "completed" },
1503-
suggestKnowledge: suggest,
1504-
});
1505-
const projection = harness.projection();
1506-
harness.setProjection({
1507-
...projection,
1508-
runs: projection.runs.map((run) => (run.status === "starting" ? { ...run, ordinal } : run)),
1509-
messages: projection.messages.map((message) => ({ ...message, creationSource: source })),
1511+
]);
15101512
});
1511-
yield* harness.start;
1512-
const prompt = harness.startRootRun.mock.calls[0]?.[0].message.text;
1513-
if (source === "mcp" && ordinal === 1) {
1514-
expect(suggest).toHaveBeenCalledOnce();
1515-
expect(suggest.mock.calls[0]?.[0]).toMatchObject({ projectId, query: text });
1516-
if (defects) {
1517-
expect(harness.startRootRun).toHaveBeenCalledOnce();
1518-
expect(prompt).toBe(text);
1513+
for (const [source, ordinal] of [
1514+
["web", 1],
1515+
["mobile", 1],
1516+
["mcp", 2],
1517+
["mcp", 1],
1518+
["server", 1],
1519+
] as const) {
1520+
suggest.mockClear();
1521+
const harness = makeLocalCommandHarness({
1522+
text,
1523+
previousTurn: { runStatus: "completed", turnStatus: "completed" },
1524+
suggestKnowledge: suggest,
1525+
});
1526+
const projection = harness.projection();
1527+
harness.setProjection({
1528+
...projection,
1529+
runs: projection.runs.map((run) =>
1530+
run.status === "starting" ? { ...run, ordinal } : run,
1531+
),
1532+
messages: projection.messages.map((message) => ({ ...message, creationSource: source })),
1533+
});
1534+
const exit = yield* Effect.exit(harness.start);
1535+
const unattended = (source === "mcp" || source === "server") && ordinal === 1;
1536+
if (unattended && outcome.startsWith("interrupted")) {
1537+
expect(suggest).toHaveBeenCalledOnce();
1538+
expect(exit._tag).toBe("Failure");
1539+
if (exit._tag === "Failure") expect(Cause.hasInterruptsOnly(exit.cause)).toBe(true);
1540+
expect(harness.startRootRun).not.toHaveBeenCalled();
1541+
expect(harness.nativeStart).not.toHaveBeenCalled();
15191542
continue;
15201543
}
1521-
expect(prompt).toContain("fact-migrations");
1522-
expect(prompt).toContain(text);
1523-
expect(prompt).not.toContain("Private");
1524-
} else {
1525-
expect(suggest).not.toHaveBeenCalled();
1526-
expect(prompt).toBe(text);
1544+
expect(exit._tag).toBe("Success");
1545+
yield* dispatchKnowledge(harness.startRootRun.mock.calls[0]![0]);
1546+
const prompt = harness.nativeStart.mock.calls[0]?.[0].message.text;
1547+
if (unattended) {
1548+
expect(suggest).toHaveBeenCalledOnce();
1549+
expect(suggest.mock.calls[0]?.[0]).toMatchObject({ projectId, query: text });
1550+
if (outcome === "success") {
1551+
expect(prompt).toContain("fact-migrations");
1552+
expect(prompt).toContain(text);
1553+
expect(prompt).not.toContain("Private");
1554+
} else {
1555+
expect(prompt).toBe(text);
1556+
}
1557+
} else {
1558+
expect(suggest).not.toHaveBeenCalled();
1559+
expect(prompt).toBe(text);
1560+
}
15271561
}
1528-
}
1529-
}),
1562+
}),
15301563
);
15311564

15321565
effectIt.effect.each([

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
RunId,
2424
ThreadId,
2525
} from "@t3tools/contracts";
26+
import * as Cause from "effect/Cause";
2627
import * as Context from "effect/Context";
2728
import * as DateTime from "effect/DateTime";
2829
import * as Effect from "effect/Effect";
@@ -140,14 +141,15 @@ const unattendedMemoryHint = Effect.fn("ProviderTurnStartService.unattendedMemor
140141
})
141142
.pipe(
142143
Effect.timeoutOption("150 millis"),
143-
Effect.orElseSucceed(() => Option.none()),
144-
Effect.catchDefect((cause) =>
145-
Effect.logWarning("Optional project memory lookup failed", {
146-
projectId,
147-
runId,
148-
cause,
149-
}).pipe(Effect.as(Option.none())),
150-
),
144+
Effect.catchCause((cause) => {
145+
if (Cause.hasInterrupts(cause)) return Effect.interrupt;
146+
if (Cause.hasDies(cause))
147+
return Effect.logWarning("Optional project memory lookup failed", {
148+
projectId,
149+
runId,
150+
}).pipe(Effect.andThen(Effect.succeedNone));
151+
return Effect.succeedNone;
152+
}),
151153
);
152154
// IDs can also be supplied by clients. Only opaque identifiers belong in a provider hint.
153155
const opaqueId = /^[a-zA-Z0-9_:.-]{1,128}$/;

0 commit comments

Comments
 (0)