* feat(environment): render more read tools from the state and describe responses
Keep main's environment serving and fold in coverage for non-entity reads,
response summaries, and MCP tool annotations.
Squashed rebase of #5352 onto main.
* fix(environment): keep rendered documents and quoted samples inside their budgets
A recorded body of exponential numbers can render past 256 KiB and turn a
working read into a state size error, so that document stays a recorded replay.
Description samples are measured after JSON quoting, and the replay-fallback
catalog check uses a tool count the longer response descriptions still separate.
Co-authored-by: morluto <morluto@users.noreply.github.com>
* feat(environment): apply evidence-gated write effects to the served state
Serve write tools whose later reads confirm the effect, and keep main's
state-file ownership and part-wise reply measurement.
Squashed rebase of #5600 onto the rebased #5352 tip.
* fix(environment): bound and validate write state history
* feat(environment): render more read tools from the state and describe responses
Keep main's environment serving and fold in coverage for non-entity reads,
response summaries, and MCP tool annotations.
Squashed rebase of #5352 onto main.
* fix(environment): keep rendered documents and quoted samples inside their budgets
A recorded body of exponential numbers can render past 256 KiB and turn a
working read into a state size error, so that document stays a recorded replay.
Description samples are measured after JSON quoting, and the replay-fallback
catalog check uses a tool count the longer response descriptions still separate.
Co-authored-by: morluto <morluto@users.noreply.github.com>
* feat(environment): apply evidence-gated write effects to the served state
Serve write tools whose later reads confirm the effect, and keep main's
state-file ownership and part-wise reply measurement.
Squashed rebase of #5600 onto the rebased #5352 tip.
* fix(environment): bound and validate write state history
* fix(environment): bound additional collection history
* fix(environment): credit sent fields, negative sequences, and encoded targets
A later overwrite copied surviving derived fields into the sent-member
claim, so a members-only check rejected an update whose sent field still
matched. Serving floored sequences at zero and stored missing history as
zero, so a collection sequence learned from negative ranks skipped the
next value. An absence after a delete forgot the resolved collection, and
an encoded read could not observe an update spelled with another
collection encoding.
Regression tests cover each case.
Co-authored-by: morluto <morluto@users.noreply.github.com>
* test(environment): cover resolved encoded update identities
* fix: validate timed update observations
* fix(environment): share write overlap and check clocks on earlier calls
Observation search skips a read begun before the write ended. That
comparison now uses the same overlap as the check path.
A clock learned on a later call was not checked against earlier calls
that kept the field. Serving would stamp those calls too, so the plan
stays unserved. A clock learned earlier is still refused when a later
call does not advance it.
Regression tests cover an overlapping empty update and a published_at
clock that one recorded call leaves unchanged.
Co-authored-by: morluto <morluto@users.noreply.github.com>
* fix: preserve partial update evidence
* fix(environment): let an unmodelled write explain only the list members that differ
A failed whole-list read now reports which members alone account for the
difference. An unmodelled write excuses the read's writes only when it came
after each such member's last check and pending writes, so an older change
elsewhere no longer hides a newer write the list shows undone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 7b6aab9d7191e600051693f2c192afcfc743d083)
* fix(environment): explain added fields, tie creates to serving's collection, and bound write descriptions
An update explains a field the entity lacked before when the observing
view had shown the entity, so a server-filled field is not dropped. A
create is tied to the held collection serving picks for its path. Write
descriptions and body refusals quote member names and sample them within
a byte bound. The request body is indexed once per explanation, so copied
and slugged fields no longer rescan it.
Replays 07aca54e onto the restacked branch. Its rule that a read begun
before a write ended teaches nothing is already here (fb6785dc), so only
its test is kept.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* docs(environment): state how list reads weigh unmodelled writes, added update fields and quoted members
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(environment): keep the observing view helpers with the observations
shownBefore and the item-or-list fields check move to the observe module,
keeping the write effects within the line limit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(environment): resolve unmodelled writes onto the held collection
An unmodelled path kept the request's collection spelling. An archive under
the plain spelling of a collection held encoded marked only that plain
collection unknown, so a later update was blamed for the archive's fields
and dropped. Name that entity the way a modelled write does.
Co-authored-by: morluto <morluto@users.noreply.github.com>
* fix(environment): confirm no write through volatile fields or overwritten replacements, and bound the served clock
A members-only check no longer compares volatile fields, which render
from the read itself. A document replacement credits the members it
wrote or removed at its object, so a later merge that overwrites them
takes its confirmation away. A recorded time past the range of a date
gives no served clock instead of throwing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(environment): leave unknown entities unchecked by failed reads, volatile fields and unchecked calls
A failed item read that is no absence leaves an unknown entity unknown, so
a later usable read still checks its writes. A read showing an unknown
entity again checks no write on a volatile field. An update of an unknown
entity takes its recorded outcome and adds nothing to a built plan. A later
update that does not answer with its entity is observed only by a read
whose view shows every learned clock; without one it is unmodelled.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(environment): fill a create's path fields from the collection serving selects
The gate built a later create's path fields from the recorded request path,
while serving fills them from the held collection it selects for that path,
so a read showing the other spelling's parent could confirm a create serving
cannot reproduce. Rules still learn from the recorded path.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(environment): name unmodelled writes as modelled ones, count fields updates write, and keep later members' types
An unmodelled write below an item path names its entity through itemTarget,
and one at or above a collection resolves the held collection, as modelled
writes do, so an encoded spelling no longer hides the entity it changed.
An update's answer counts the fields its rules write, other than increments,
as available, so a field only the update writes no longer makes it
incomplete-entities. A member a later call first stores keeps that call's
JSON type in the body's kinds, which the body check falls back to.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(environment): let a document replacement overwrite everything under its object
A replacement claims only the members it wrote or removed, but it now
overwrites every earlier effect under its object, so a merge that wrote
the object it replaces loses its confirmation.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(environment): start the served clock from the records whose times a date can hold
A record timed past the range of a date no longer leaves the served clock
without a start, which the store read as zero and stamped writes in 1970;
the clock starts after the latest record a date can hold.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
270 lines
9.5 KiB
TypeScript
270 lines
9.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import type { TimedCaptureRow } from "../../../tests/fixtures/analysisView.js";
|
|
import {
|
|
servedFrom,
|
|
type Served,
|
|
} from "../../../tests/fixtures/environmentStateServing.js";
|
|
import {
|
|
FIRST_BOOK,
|
|
SECOND_BOOK,
|
|
renamedBook,
|
|
type Book,
|
|
} from "../../../tests/fixtures/environmentStateWrites.js";
|
|
import { MAX_JSON_DEPTH, type JsonValue } from "../../domain/jsonValue.js";
|
|
import type { ReplayArguments } from "./environmentReplay.js";
|
|
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 READ_SETTINGS = "get_orgs_settings_by_id";
|
|
const PATCH_SETTINGS = "patch_orgs_settings_by_id";
|
|
|
|
/** Every refused call is a tool error naming its problem, and leaves the state as it was. */
|
|
const expectRefusedUnchanged = (
|
|
served: Served,
|
|
refused: readonly (readonly [string, ReplayArguments, string])[],
|
|
): void => {
|
|
const before = served.snapshot();
|
|
for (const [name, args, message] of refused) {
|
|
expect(served.refusal(name, args)).toMatchObject({
|
|
code: "invalid_request",
|
|
message: expect.stringContaining(message),
|
|
});
|
|
expect(served.snapshot()).toBe(before);
|
|
}
|
|
};
|
|
|
|
/** What data/get reports loads back through data/add byte for byte. */
|
|
const expectReloadsUnchanged = (served: Served): void => {
|
|
const before = served.snapshot();
|
|
expect(served.store.replace(JSON.parse(before)).ok).toBe(true);
|
|
expect(served.snapshot()).toBe(before);
|
|
};
|
|
|
|
const nested = (depth: number): JsonValue =>
|
|
Array.from({ length: depth }).reduce<JsonValue>((inner) => [inner], 1);
|
|
|
|
describe("served document writes", () => {
|
|
it("accepts only the members the capture sent, as the JSON types the document holds", () => {
|
|
const settings = servedFrom(SETTINGS_ROWS);
|
|
expect(settings.state.writes.get(PATCH_SETTINGS)).toMatchObject({
|
|
served: true,
|
|
effect: "document",
|
|
});
|
|
expectRefusedUnchanged(settings, [
|
|
[
|
|
PATCH_SETTINGS,
|
|
{ id: "1", body: { theme: 5, extra: true } },
|
|
"Member theme must be a JSON string",
|
|
],
|
|
[
|
|
PATCH_SETTINGS,
|
|
{ id: "1", body: { extra: true } },
|
|
"Member extra is not one the capture showed stored",
|
|
],
|
|
]);
|
|
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);
|
|
});
|
|
|
|
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("changes the document only at the path the state holds it", () => {
|
|
const settings = servedFrom(SETTINGS_ROWS);
|
|
expectRefusedUnchanged(settings, [
|
|
[
|
|
PATCH_SETTINGS,
|
|
{ id: "2", body: { theme: "blue" } },
|
|
"holds this document only at /api/orgs/1/settings",
|
|
],
|
|
]);
|
|
expect(settings.answer(READ_SETTINGS, { id: "1" }).body).toEqual(DARK);
|
|
});
|
|
});
|
|
|
|
describe("values the state can load back", () => {
|
|
it("refuses a body value data/add would refuse, whatever member carries it", () => {
|
|
const settings = servedFrom(SETTINGS_ROWS);
|
|
const unexact =
|
|
"Member meta would hold a value that is not exactly representable JSON";
|
|
expectRefusedUnchanged(settings, [
|
|
[PATCH_SETTINGS, { id: "1", body: { meta: { n: 1e20 } } }, unexact],
|
|
[
|
|
PATCH_SETTINGS,
|
|
{
|
|
id: "1",
|
|
body: JSON.parse('{"meta":{"__proto__":{"n":1}}}') as JsonValue,
|
|
},
|
|
unexact,
|
|
],
|
|
[
|
|
PATCH_SETTINGS,
|
|
{ id: "1", body: { meta: { n: nested(MAX_JSON_DEPTH) } } },
|
|
unexact,
|
|
],
|
|
]);
|
|
settings.answer(PATCH_SETTINGS, { id: "1", body: { meta: { n: 3 } } });
|
|
expectReloadsUnchanged(settings);
|
|
});
|
|
|
|
it("refuses an increment past the exact integers, so data/get still loads back", () => {
|
|
const versioned = (entry: Book, version: number): Book => ({
|
|
...entry,
|
|
version,
|
|
});
|
|
const last = Number.MAX_SAFE_INTEGER;
|
|
const edition = versioned(renamedBook(SECOND_BOOK, "Second Edition", 3), 2);
|
|
const books = servedFrom([
|
|
[
|
|
"GET",
|
|
"/api/books",
|
|
[versioned(FIRST_BOOK, last - 1), versioned(SECOND_BOOK, 1)],
|
|
],
|
|
["GET", "/api/books/1", versioned(FIRST_BOOK, last - 1)],
|
|
["GET", "/api/books/2", versioned(SECOND_BOOK, 1)],
|
|
["PATCH", "/api/books/2", edition, 200, { name: "Second Edition" }],
|
|
["GET", "/api/books/2", edition],
|
|
]);
|
|
const rename = (name: string) => ({ id: "1", body: { name } });
|
|
expect(
|
|
books.answer("patch_books_by_id", rename("Renamed")).body,
|
|
).toMatchObject({ version: last });
|
|
expectRefusedUnchanged(books, [
|
|
[
|
|
"patch_books_by_id",
|
|
rename("Renamed Again"),
|
|
"Field version would hold a value that is not exactly representable JSON",
|
|
],
|
|
]);
|
|
expectReloadsUnchanged(books);
|
|
});
|
|
});
|
|
|
|
describe("the state size limit", () => {
|
|
it("keeps the size data/get writes as each write changes the state", () => {
|
|
const books = servedFrom();
|
|
const settings = servedFrom(SETTINGS_ROWS);
|
|
const tracked = (served: Served) =>
|
|
expect(served.store.byteLength()).toBe(
|
|
Buffer.byteLength(served.snapshot(), "utf8"),
|
|
);
|
|
tracked(books);
|
|
books.answer("post_books", { body: { name: "Ünïcode Bøok" } });
|
|
tracked(books);
|
|
books.answer("patch_books_by_id", { id: "3", body: { name: "Short" } });
|
|
tracked(books);
|
|
books.answer("delete_books_by_id", { id: "1" });
|
|
books.answer("delete_books_by_id", { id: "2" });
|
|
books.answer("delete_books_by_id", { id: "3" });
|
|
tracked(books);
|
|
books.answer("post_books", { body: { name: "Only Book" } });
|
|
tracked(books);
|
|
settings.answer(PATCH_SETTINGS, { id: "1", body: { theme: "dark blue" } });
|
|
tracked(settings);
|
|
expect(settings.store.replace(JSON.parse(settings.snapshot())).ok).toBe(
|
|
true,
|
|
);
|
|
tracked(settings);
|
|
});
|
|
|
|
it("refuses a create that would take data/get past what data/add loads", () => {
|
|
const books = servedFrom();
|
|
// Each answer renders the book, so each stays under the reply body limit.
|
|
const create = { body: { name: "a".repeat(100_000) } };
|
|
let refused: ReturnType<Served["call"]> | undefined;
|
|
for (let made = 0; made < 200 && refused === undefined; made += 1) {
|
|
const result = books.call("post_books", create);
|
|
if (!result.ok) refused = result;
|
|
}
|
|
const before = books.snapshot();
|
|
expect(Buffer.byteLength(before, "utf8")).toBeLessThanOrEqual(
|
|
STATE_INTAKE_LIMIT_BYTES,
|
|
);
|
|
expect(refused?.ok === false && refused.error).toMatchObject({
|
|
resource: "capacity",
|
|
reportedLimits: { max_state_bytes: STATE_INTAKE_LIMIT_BYTES },
|
|
});
|
|
expect(books.refusal("post_books", create)).toMatchObject({
|
|
code: "resource_constraint",
|
|
});
|
|
expect(books.snapshot()).toBe(before);
|
|
expectReloadsUnchanged(books);
|
|
// A smaller state has room again.
|
|
books.answer("delete_books_by_id", { id: "3" });
|
|
expect(books.answer("post_books", create).status).toBe(201);
|
|
});
|
|
|
|
it("refuses a document that data/get would write past what data/add loads", () => {
|
|
const books = servedFrom();
|
|
const { rea_state: state } = books.store.snapshot();
|
|
// Without the members data/get adds back, the document fits only until they return.
|
|
const document = {
|
|
rea_state: {
|
|
version: state.version,
|
|
entities: state.entities.map(({ type, collection, id, fields }) => ({
|
|
type,
|
|
collection,
|
|
id,
|
|
fields: { ...fields },
|
|
})),
|
|
documents: state.documents,
|
|
},
|
|
};
|
|
const first = document.rea_state.entities[0]!.fields;
|
|
first["name"] = `${String(first["name"])}${"a".repeat(
|
|
STATE_INTAKE_LIMIT_BYTES -
|
|
Buffer.byteLength(JSON.stringify(document), "utf8"),
|
|
)}`;
|
|
expect(Buffer.byteLength(JSON.stringify(document), "utf8")).toBe(
|
|
STATE_INTAKE_LIMIT_BYTES,
|
|
);
|
|
const before = books.snapshot();
|
|
const replaced = books.store.replace(document);
|
|
expect(replaced.ok).toBe(false);
|
|
expect(!replaced.ok && replaced.error.issues).toEqual([
|
|
expect.objectContaining({
|
|
reason: "out_of_range",
|
|
maximum: STATE_INTAKE_LIMIT_BYTES,
|
|
}),
|
|
]);
|
|
expect(books.snapshot()).toBe(before);
|
|
});
|
|
});
|