fix(environment): preserve document write replay match provenance (#13604)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 20:54:28 +00:00
1 parent 65943f934b
commit 5deee1acf1
2 files changed
+57 -50

No files matched your search

@@ -396,6 +396,7 @@ export const writeAnswer = (
readonly id: string;
readonly sent?: readonly string[];
}
| { readonly body: JsonValue }
| ((
recorded: JsonValue | undefined,
) => Result<JsonValue | undefined, string>),
@@ -417,6 +418,8 @@ export const writeAnswer = (
)?.exact === true;
if (plan.response.shape === "recorded")
return ok({ ...replayAnswer(serving.source, exchange, exact), state });
if (typeof rendered !== "function" && "body" in rendered)
return stateAnswer(serving, { exchange, exact }, rendered.body, state);
const recorded = recordedJson(exchange);
const body =
typeof rendered === "function"
@@ -840,20 +843,11 @@ const answerDocument = (
view: "document-write",
path: held.path,
} as const;
const answer =
const rendered =
plan.response?.shape === "entity"
? (() => {
const exchange = serving.source.exchanges.get(plan.response.ordinal);
return exchange === undefined
? err(refused(tool, `Tool ${tool.name} has no recorded response.`))
: stateAnswer(
serving,
{ exchange, exact: true },
written.value,
state,
);
})()
: writeAnswer(serving, request, { fields: undefined, id: "" }, state);
? { body: written.value }
: { fields: undefined, id: "" };
const answer = writeAnswer(serving, request, rendered, state);
if (!answer.ok) return answer;
const set = serving.store.setDocument(held.tool, written.value);
if (!set.ok) return set;
@@ -22,12 +22,16 @@ import { STATE_INTAKE_LIMIT_BYTES } from "./environmentStateDocument.js";
const DARK = { theme: "dark", meta: { n: 1 } };
const LIGHT = { theme: "light", meta: { n: 2 } };
/** One organisation's settings document, read, patched with both members and read again. */
const SETTINGS_ROWS: readonly TimedCaptureRow[] = [
["GET", "/api/orgs/1/settings", DARK],
["PATCH", "/api/orgs/1/settings", LIGHT, 200, LIGHT],
["GET", "/api/orgs/1/settings", LIGHT],
const SETTINGS_PATH = "/api/orgs/1/settings";
const settingsRows = (
patchPath = SETTINGS_PATH,
updated: JsonValue = LIGHT,
): readonly TimedCaptureRow[] => [
["GET", SETTINGS_PATH, DARK],
["PATCH", patchPath, updated, 200, updated],
["GET", SETTINGS_PATH, updated],
];
const SETTINGS_ROWS = settingsRows();
const READ_SETTINGS = "get_orgs_settings_by_id";
const PATCH_SETTINGS = "patch_orgs_settings_by_id";
@@ -59,7 +63,8 @@ const nested = (depth: number): JsonValue =>
describe("served document writes", () => {
it("changes only the recorded document path with the learned member names and types", () => {
const settings = servedFrom(SETTINGS_ROWS);
const query = "scope=panel&view=full";
const settings = servedFrom(settingsRows(`${SETTINGS_PATH}?${query}`));
expect(settings.state.writes.get(PATCH_SETTINGS)).toMatchObject({
served: true,
effect: "document",
@@ -82,39 +87,47 @@ describe("served document writes", () => {
],
]);
expect(settings.answer(READ_SETTINGS, { id: "1" }).body).toEqual(DARK);
const blue = { theme: "blue", meta: DARK.meta };
expect(
settings.answer(PATCH_SETTINGS, { id: "1", body: { theme: "blue" } }),
).toMatchObject({
body: blue,
state: { view: "document-write", path: "/api/orgs/1/settings" },
});
expect(settings.answer(READ_SETTINGS, { id: "1" }).body).toEqual(blue);
const patch = settings.source.manifest.tools.find(
({ name }) => name === PATCH_SETTINGS,
)!;
const respond = environmentToolAnswer(settings.source, settings.store);
for (const [rawQuery, theme, exact] of [
[query, "blue", true],
["view=full&scope=panel", "green", false],
] as const) {
const body = { theme, meta: DARK.meta };
expect(
respond(patch, { id: "1", body: { theme } }, rawQuery),
).toMatchObject({
ok: true,
value: {
body,
replay: { exact_match: exact, record_ordinal: 1 },
state: { view: "document-write", path: "/api/orgs/1/settings" },
},
});
expect(settings.answer(READ_SETTINGS, { id: "1" }).body).toEqual(body);
}
});
it("leaves a document write unserved when its recording changed a member's JSON type, which serving would refuse", () => {
const retyped = { theme: 5, meta: LIGHT.meta };
const settings = servedFrom([
["GET", "/api/orgs/1/settings", DARK],
["PATCH", "/api/orgs/1/settings", retyped, 200, retyped],
["GET", "/api/orgs/1/settings", retyped],
]);
expect(settings.state.writes.get(PATCH_SETTINGS)).toMatchObject({
served: false,
reason: "unexplained-change",
});
});
it("does not confirm a document write at another path, which serving refuses", () => {
const settings = servedFrom([
["GET", "/api/orgs/1/settings", DARK],
["PATCH", "/api/orgs/2/settings", LIGHT, 200, LIGHT],
["GET", "/api/orgs/1/settings", LIGHT],
]);
expect(settings.state.writes.get(PATCH_SETTINGS)).toMatchObject({
served: false,
});
});
it.each([
[
"changes a member's type",
SETTINGS_PATH,
{ theme: 5, meta: LIGHT.meta },
{ reason: "unexplained-change" },
],
["targets another path", "/api/orgs/2/settings", LIGHT, {}],
] as const)(
"withholds a recorded document write that %s",
(_, path, updated, reason) => {
const settings = servedFrom(settingsRows(path, updated));
expect(settings.state.writes.get(PATCH_SETTINGS)).toMatchObject({
served: false,
...reason,
});
},
);
});
describe("values the state can load back", () => {