Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
155 commits
Select commit Hold shift + click to select a range
ad439bd
feat(environment): extract the initial state a capture's reads observed
earakely-scale Oct 20, 2026
7a668b7
Merge main into feat/environment-state-view
earakely-scale Oct 20, 2026
f2c9a2e
fix(environment): bound state at redacted writes; infer identity only…
earakely-scale Oct 20, 2026
2999128
fix(environment): keep first sightings, one list pointer and linear f…
earakely-scale Oct 20, 2026
a5c7b92
fix(environment): keep post-write values and partial lists out of the…
kaoru0822-kitauji Oct 20, 2026
4e93547
docs(environment): scope item-shaped and deleted written paths out of…
kaoru0822-kitauji Oct 20, 2026
aba9cb1
fix(environment): count writes in flight when a value changes between…
kaoru0822-kitauji Oct 20, 2026
856c8c0
Merge main into feat/environment-state-view
kaoru0822-kitauji Oct 20, 2026
55480bd
chore: merge main (5fa4defc) into environment state view
earakely-scale Oct 20, 2026
9fee36d
fix(environment): catch cross-tool changes across writes; read only i…
earakely-scale Oct 20, 2026
3c64cbe
chore: merge the branch's own main merge (856c8c0b), identical to 554…
earakely-scale Oct 20, 2026
47f8a27
chore: merge main (f93a0335, rich captures) into environment state view
earakely-scale Oct 20, 2026
32fbaa5
chore: merge main into environment state view
gensokyo-dev Oct 20, 2026
04e2c56
fix(environment): keep post-write observations out of initial state
gensokyo-dev Oct 20, 2026
3daeebb
chore: retain author main integration in environment state view
gensokyo-dev Oct 20, 2026
25923c3
chore: sync environment state view with latest main
gensokyo-dev Oct 20, 2026
6ad63bc
fix(environment): retain field provenance and bound state inference work
gensokyo-dev Oct 20, 2026
2bd0b3c
feat(environment): serve tool calls from the environment state
earakely-scale Oct 20, 2026
35aee7c
feat(environment): render more read tools from the state and describe…
earakely-scale Oct 20, 2026
9f60e54
Merge the environment state view's review fixes into feat/environment…
earakely-scale Oct 20, 2026
62708a8
Merge the environment state view's review fixes into feat/environment…
earakely-scale Oct 20, 2026
cb04dcd
docs(environment): record verification after merging the state view's…
earakely-scale Oct 20, 2026
9dfc05f
docs(environment): record verification after merging the state view's…
earakely-scale Oct 20, 2026
aa48c74
Merge main (#5319 squashed, #5349) into feat/environment-state-coverage
earakely-scale Oct 20, 2026
e96484b
Merge main (#5319 squashed, #5349) into feat/environment-state-serve
earakely-scale Oct 20, 2026
5bb7ca4
fix(environment): stop identity checks at the first missing or repeat…
earakely-scale Oct 20, 2026
6676582
feat(environment)!: serve the environment state by default
earakely-scale Oct 20, 2026
83fcc73
fix(environment): bound data-plane replies, keep batch order, cancel …
earakely-scale Oct 20, 2026
d9c0357
fix(environment): keep bodies too deep for the state's nesting out of…
earakely-scale Oct 20, 2026
d9a717e
Merge main (#5350 write cutoffs, #5031 protobuf bodies) into feat/env…
earakely-scale Oct 20, 2026
57b1168
fix(environment): measure data-plane replies before building them
earakely-scale Oct 20, 2026
5956e00
chore: merge main (d2a3269d) into environment state serve
earakely-scale Oct 20, 2026
bf0dd92
chore: merge environment state coverage into the state writes branch
earakely-scale Oct 20, 2026
a98df6c
chore: merge main (d2a3269d) into environment state coverage
earakely-scale Oct 20, 2026
ef49dca
fix(environment): keep changed documents out of the state; mark OPTIO…
earakely-scale Oct 20, 2026
306c3da
docs(specs): plan evidence-gated write effects on the served state
earakely-scale Oct 20, 2026
5a61853
fix(environment): release batch replies as they are encoded; judge ex…
earakely-scale Oct 20, 2026
b4b7ebc
perf(environment): intersect list identity candidates through a set
earakely-scale Oct 20, 2026
b9fbcd7
feat(environment): apply the write effects a capture confirms to the …
earakely-scale Oct 20, 2026
84291a0
fix(environment): count only applied writes and serve writes the held…
earakely-scale Oct 20, 2026
cbb7b61
test(environment): cover write plans, the write gate and served writes
earakely-scale Oct 20, 2026
e7bd24b
docs(specs): record the write effects verification
earakely-scale Oct 20, 2026
a0b3c55
Merge remote-tracking branch 'fork/feat/environment-state-serve' into…
earakely-scale Oct 20, 2026
0864bac
Merge remote-tracking branch 'fork/feat/environment-state-coverage' i…
earakely-scale Oct 20, 2026
2e89a39
Merge remote-tracking branch 'origin/main' into feat/environment-stat…
earakely-scale Oct 20, 2026
82dfd94
docs(specs): record the write effects checks after merging the base b…
earakely-scale Oct 20, 2026
ce12615
Merge remote-tracking branch 'origin/main' into feat/environment-stat…
earakely-scale Oct 20, 2026
52b6b78
docs(specs): record the checks after merging main again
earakely-scale Oct 20, 2026
dbd0dce
chore: merge main (1629266e) into environment state coverage
earakely-scale Oct 20, 2026
f6c756a
fix(environment): quote captured names in response summaries
earakely-scale Oct 20, 2026
370b9be
docs(specs): correct the showcase capture count to 50
earakely-scale Oct 20, 2026
c00fde4
fix(environment): keep room for refusals and framing in the reply budget
earakely-scale Oct 20, 2026
61c8e3e
chore: merge environment state serve (c00fde41) into environment stat…
earakely-scale Oct 20, 2026
862c88c
chore: merge environment state coverage (f6c756aa) into environment s…
earakely-scale Oct 20, 2026
b068b09
chore: merge main (1629266e) into environment state writes
earakely-scale Oct 20, 2026
82556cd
fix(environment): answer unfiltered lists from their own parent; desc…
earakely-scale Oct 20, 2026
138ea4b
fix(environment): judge writes by the full served response; model onl…
earakely-scale Oct 20, 2026
6d21b62
fix(environment): refuse writes the state could not hold or reload
earakely-scale Oct 20, 2026
f1b00f5
docs(environment): specify the write refusals, 2xx-only modelling and…
earakely-scale Oct 20, 2026
df2bded
test(environment): pin the replay card with quoted summary names; rec…
earakely-scale Oct 20, 2026
2086da6
fix(environment): make the write gate and serving agree on lines, rul…
earakely-scale Oct 20, 2026
7056f5f
docs(environment): record the second review round's verification
earakely-scale Oct 20, 2026
db8df45
fix(environment): keep checked lines' path spellings; check later slu…
earakely-scale Oct 20, 2026
5327e59
fix(environment): choose a read's checked line as replay chooses the …
earakely-scale Oct 20, 2026
b6c3b16
fix(environment): match document write paths, verbatim create collect…
earakely-scale Oct 20, 2026
f2b9535
fix(environment): answer a missing entity with a 2xx null only for reads
earakely-scale Oct 20, 2026
25b6622
chore: merge main (335c042d) into environment state serve
earakely-scale Oct 21, 2026
54d3d5e
feat(environment): decide a list's whole-collection membership per line
earakely-scale Oct 21, 2026
19ef539
chore: merge main (335c042d) into environment state coverage
earakely-scale Oct 21, 2026
87e94d1
test(environment): cover per-line list membership; document it
earakely-scale Oct 21, 2026
665112e
fix(environment): quote the prefix of a member name too long to show …
earakely-scale Oct 21, 2026
4aeefd9
chore: merge environment state serve (25b6622d) into environment stat…
earakely-scale Oct 21, 2026
9edbabe
chore: merge environment state coverage (665112e9) into environment s…
earakely-scale Oct 21, 2026
111556a
docs(specs): record per-line list membership measured on six environm…
earakely-scale Oct 21, 2026
6b8db19
fix(environment): refuse a loaded state that data/get would write pas…
earakely-scale Oct 21, 2026
fc403d9
chore: merge environment state serve (6b8db198) into environment stat…
earakely-scale Oct 21, 2026
1d1c478
fix(environment): confirm a write only by reads that still see it, an…
earakely-scale Oct 21, 2026
dc5e2aa
fix(environment): weigh an update's derived changes when a later writ…
earakely-scale Oct 21, 2026
8a374b6
Merge remote-tracking branch 'fork/feat/environment-state-writes' int…
earakely-scale Oct 21, 2026
4080063
fix(environment): count no write for a collection once a read shows i…
earakely-scale Oct 21, 2026
847b093
docs(specs): record per-line list membership over fifty captures and …
earakely-scale Oct 21, 2026
fe8ab58
fix(environment): erase overwritten fields from earlier write effects…
earakely-scale Oct 21, 2026
edc09db
Merge remote-tracking branch 'fork/feat/environment-state-writes' int…
earakely-scale Oct 21, 2026
73c0e9b
fix(environment): create in a held collection when an Evidence spelli…
earakely-scale Oct 21, 2026
b224462
chore: merge main into environment state writes
morluto Oct 21, 2026
19118fe
chore: merge environment state writes (73c0e9b5) into environment lis…
earakely-scale Oct 21, 2026
0a46f89
chore: merge main (4dfa1c67) into environment list lines
earakely-scale Oct 21, 2026
9f513d0
fix(environment): check reads on the lines serving answers, and refus…
earakely-scale Oct 21, 2026
6d28aa7
chore: merge the remote environment state writes (b2244622) into the …
earakely-scale Oct 21, 2026
2eb7be0
chore: merge environment state writes (6d28aa78) into environment lis…
earakely-scale Oct 21, 2026
7ffd077
fix(environment): take list counters from whole lines, per line, and …
earakely-scale Oct 21, 2026
bec65b3
chore: merge main (d8a9673f) into environment state writes
earakely-scale Oct 21, 2026
3580d85
fix(environment): confirm only the writes a read shows, and name and …
earakely-scale Oct 21, 2026
43d98ea
fix(environment): pin a create's observation to the id it answered, a…
earakely-scale Oct 21, 2026
3e18322
chore: merge environment state writes (43d98ea9) into environment lis…
earakely-scale Oct 21, 2026
de13b0c
docs(specs): record the list-lines re-run after the write-plan merge
earakely-scale Oct 21, 2026
264ce41
fix(environment): keep unseen writes pending, and hold later calls to…
earakely-scale Oct 21, 2026
2807278
chore: merge environment state writes (264ce410) into environment lis…
earakely-scale Oct 21, 2026
7d2587c
docs(specs): record the list-lines re-run after the third write-plan …
earakely-scale Oct 21, 2026
0ae6440
fix(environment): never rule an entity's identity, and read a zero pa…
earakely-scale Oct 21, 2026
34cbdd6
chore: merge environment state writes (0ae64400) into environment lis…
earakely-scale Oct 21, 2026
6257de9
refactor(environment): move the paging rules to environmentStatePagin…
earakely-scale Oct 21, 2026
4f40bff
chore: merge environment state writes (6257de93) into environment lis…
earakely-scale Oct 21, 2026
c68c63b
docs(specs): record the list-lines re-run after the later write-plan …
earakely-scale Oct 21, 2026
466ed8b
fix(environment): name entities and weigh interference per write as s…
earakely-scale Oct 21, 2026
f239fa3
chore: merge environment state writes (466ed8b4) into environment lis…
earakely-scale Oct 21, 2026
ed4b6db
docs(specs): record the list-lines re-run after the fourth write-plan…
earakely-scale Oct 21, 2026
7cef2fb
fix(environment): bound pages per size, refuse only read-observed rem…
earakely-scale Oct 21, 2026
8eea9f6
chore: merge environment state writes (7cef2fb2) into environment lis…
earakely-scale Oct 21, 2026
734786e
docs(specs): record the list-lines re-run after the 7cef2fb2 merge
earakely-scale Oct 21, 2026
45a5c8b
fix(environment): count interference on any target a read covers, and…
earakely-scale Oct 21, 2026
27dd606
chore: merge environment state writes (45a5c8b9) into environment lis…
earakely-scale Oct 21, 2026
325c128
fix(environment): weigh interference only where a tool's effect survives
earakely-scale Oct 21, 2026
9b868c0
chore: merge environment state writes (325c128f) into environment lis…
earakely-scale Oct 21, 2026
6c8f4f7
feat(environment): render more read tools from the state and describe…
cursoragent Oct 21, 2026
378a109
fix(environment): keep rendered documents and quoted samples inside t…
cursoragent Oct 21, 2026
cfb42dd
feat(environment): apply evidence-gated write effects to the served s…
cursoragent Oct 21, 2026
5d0e096
fix(environment): bound and validate write state history
morluto Oct 21, 2026
5b42469
feat(environment): render more read tools from the state and describe…
cursoragent Oct 21, 2026
54c8934
fix(environment): keep rendered documents and quoted samples inside t…
cursoragent Oct 21, 2026
19b50ea
feat(environment): apply evidence-gated write effects to the served s…
cursoragent Oct 21, 2026
a057ea9
fix(environment): bound and validate write state history
morluto Oct 21, 2026
f79f1e1
fix(environment): bound additional collection history
morluto Oct 21, 2026
4a678f1
Merge remote-tracking branch 'live-5600/feat/environment-state-writes…
morluto Oct 21, 2026
7de20bc
fix(environment): credit sent fields, negative sequences, and encoded…
cursoragent Oct 21, 2026
a1076ca
Merge live environment write verification fixes
morluto Oct 21, 2026
d3015d2
test(environment): cover resolved encoded update identities
morluto Oct 21, 2026
cddd3e6
fix: validate timed update observations
morluto Oct 21, 2026
fb6785d
fix(environment): share write overlap and check clocks on earlier calls
cursoragent Oct 21, 2026
d7c5c62
merge: restack environment state writes onto main after #5352
cursoragent Oct 21, 2026
3b34bbd
fix: preserve partial update evidence
morluto Oct 21, 2026
5d00ff3
Merge remote-tracking branch 'live-5600/feat/environment-state-writes…
morluto Oct 21, 2026
717ac2c
chore: merge main (8413ec08) into environment state writes
earakely-scale Oct 21, 2026
891f35e
chore: merge environment state writes (717ac2c4) into environment lis…
earakely-scale Oct 22, 2026
2013511
chore: merge main (2451e187) into environment list lines
earakely-scale Oct 22, 2026
e923fc7
fix(environment): let an unmodelled write explain only the list membe…
earakely-scale Oct 21, 2026
75d7bb0
fix(environment): explain added fields, tie creates to serving's coll…
earakely-scale Oct 21, 2026
cd26a76
docs(environment): state how list reads weigh unmodelled writes, adde…
earakely-scale Oct 22, 2026
2a125b7
chore: merge main (4a4bbab3) into environment state writes
earakely-scale Oct 22, 2026
ddab0a9
chore: merge environment state writes (2a125b71) into environment lis…
earakely-scale Oct 22, 2026
11e3f01
refactor(environment): keep the observing view helpers with the obser…
earakely-scale Oct 22, 2026
6685b37
chore: merge environment state writes (11e3f016) into environment lis…
earakely-scale Oct 22, 2026
53a7866
fix(environment): resolve unmodelled writes onto the held collection
cursoragent Oct 22, 2026
329aeed
fix(environment): confirm no write through volatile fields or overwri…
earakely-scale Oct 22, 2026
ccdf091
fix(environment): leave unknown entities unchecked by failed reads, v…
earakely-scale Oct 22, 2026
1c97933
fix(environment): fill a create's path fields from the collection ser…
earakely-scale Oct 22, 2026
90d9022
fix(environment): name unmodelled writes as modelled ones, count fiel…
earakely-scale Oct 22, 2026
ca88b5e
chore: merge environment state writes (90d90227) into environment lis…
earakely-scale Oct 22, 2026
0fe1738
refactor(environment): keep the written-field checks with the attribu…
earakely-scale Oct 22, 2026
756c631
fix(environment): let a document replacement overwrite everything und…
earakely-scale Oct 22, 2026
3f1f825
chore: merge the published environment state writes (756c6312)
earakely-scale Oct 22, 2026
79f0560
chore: merge environment state writes (3f1f825e) into environment lis…
earakely-scale Oct 22, 2026
1802e41
chore: merge main (b8c1b453) into environment list lines
earakely-scale Oct 22, 2026
0499ed1
chore: merge main (538017d1, #5600 squashed) into environment list lines
earakely-scale Oct 22, 2026
36571c4
fix(environment): preserve whole-list semantics when integrating late…
gensokyo-dev Oct 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(environment): match document write paths, verbatim create collect…
…ions and absences

Four places where the gate and serving still disagreed:
- the gate applied a document write to the held document whatever path
  it wrote, while serving refuses any other path; a write at another
  path is now unmodelled;
- a create took the first held collection its parent value matched,
  so with acme1 and %61cme1 both held a create for acme1 could land in
  the encoded one; the verbatim spelling now wins, as for item calls;
- the gate accepts a 2xx JSON null as a recorded absence, but serving
  answered a missing entity only with a recorded 400, 404 or 410; the
  null answer is now served too;
- data/add measured only the document it was given, so one that omits
  types and the state Evidence id could load under 32 MiB and grow past
  it as data/get writes it back; such a document is now refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
earakely-scale and claude committed Oct 20, 2026
commit b6c3b168f63d32c56a95e1c00511d0c7f90bc426
18 changes: 13 additions & 5 deletions docs/specs/agent-environments/state-writes/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ response, with the entity re-rendered when the response carries it.
**Documents.** A document write merges the body's members (replaces them, for
`PUT`) into the document object that holds most of them: the root or one
member. Document reads answer only on their recorded line, so a write is
served only at the path the state holds the document at. A recorded write
whose body serving would refuse (a member of another JSON type than the
document holds) is `unexplained-change`.
served only at the path the state holds the document at, and the gate treats a
recorded write at another path as an unmodelled write. A recorded write whose
body serving would refuse (a member of another JSON type than the document
holds) is `unexplained-change`.

**Responses.** A write answers with the status and content type of one
recorded response:
Expand Down Expand Up @@ -257,6 +258,12 @@ When the tool, or else an item view of the type, recorded a 400, 404 or 410
response, an update or delete of a missing entity answers with the latest such
recording instead.

A create in a collection held under more than one spelling (`acme1` and
`%61cme1`) goes to the one the call spells verbatim, as item calls choose their
entity. `data/add` refuses a document that `data/get` would write back past the
32 MiB intake limit once it adds the members a document may omit, such as
`types` and the state Evidence id.

**Clock and generators.** The store's clock starts at `write_clock` and
advances one second before each served write; clock fields take its value in
their recorded format.
Expand All @@ -271,8 +278,9 @@ their recorded format.
sequences.

**Missing entities.** An item read of an entity the state does not hold
answers with the tool's latest recorded 400, 404 or 410 response when it has
one, labelled `state.view: "missing"`. Otherwise it stays a tool error.
answers with the tool's latest recorded 400, 404 or 410 response, or 2xx JSON
`null` response, the absences the gate accepts, when it has one, labelled
`state.view: "missing"`. Otherwise it stays a tool error.

**Lists of a whole collection.** Written entities matter only if lists show
them, so the read gate's `membership: "all"` also covers a line that only pages
Expand Down
10 changes: 10 additions & 0 deletions docs/specs/agent-environments/state-writes/verification.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,13 @@ names; both new tests fail on the previous head. A fourth round chose a read's
line as replay does, so a read spelled plainly is checked against the encoded
line that answers it, and its list against that line's collection; its test
fails on the previous head.

A review of the fidelity branch built on this one found four more places: a
document write at another path is now unmodelled in the gate, a create picks
the collection the call spells verbatim, a missing entity is answered with a
recorded 2xx `null` the gate accepted as an absence, and `data/add` refuses a
document whose `data/get` form would exceed the intake limit. Each new test
fails on the previous head. The gates passed again (domain and services 7475
with 5 pre-existing skips, boundaries 75, conformance, `jscpd` 3.46%), the
verdicts over the 50 captures were unchanged plan by plan, and the smoke
applied all 33 served write tools.
8 changes: 7 additions & 1 deletion src/application/environment/environmentStateAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,18 @@ export const missingAnswer = (
readonly id: string;
},
): Result<StateAnswer, AnalysisError> => {
// The write gate takes a 2xx JSON null as an absence too, so a delete it confirmed that way can be answered.
const latestAbsence = (ordinals: readonly number[]) =>
ordinals
.map((ordinal) => serving.source.exchanges.get(ordinal))
.filter(
(exchange): exchange is RecordedExchange =>
exchange !== undefined && isAbsence(exchange.status),
exchange !== undefined &&
(isAbsence(exchange.status) ||
(exchange.status !== null &&
exchange.status >= 200 &&
exchange.status < 300 &&
recordedJson(exchange) === null)),
)
.at(-1);
// The tool's own recorded absence first, else that of an item view of the type.
Expand Down
23 changes: 21 additions & 2 deletions src/application/environment/environmentStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type {
StateType,
} from "../../domain/analysisView/environmentStateRules.js";
import {
AnalysisInputError,
AnalysisResourceConstraintError,
type AnalysisInputError,
} from "../../domain/analysisErrorCore.js";
import type { JsonValue } from "../../domain/jsonValue.js";
import { err, ok, type Result } from "../../domain/result.js";
Expand Down Expand Up @@ -83,7 +83,26 @@ export class EnvironmentStateStore {
replace(document: unknown): Result<void, AnalysisInputError> {
const content = parseStateDocument(this.state, document);
if (!content.ok) return content;
this.#install(content.value, this.#contentBytes(content.value));
// data/get writes members a document may omit, so the state it reports must still be one data/add loads.
const bytes = this.#contentBytes(content.value);
if (bytes > STATE_INTAKE_LIMIT_BYTES)
return err(
new AnalysisInputError(
ENVIRONMENT_SERVE_OPERATION,
{
userMessage: `The state would exceed ${String(STATE_INTAKE_LIMIT_BYTES)} bytes as data/get writes it.`,
},
[
{
path: [],
reason: "out_of_range",
message: `would exceed ${String(STATE_INTAKE_LIMIT_BYTES)} bytes as data/get writes it`,
maximum: STATE_INTAKE_LIMIT_BYTES,
},
],
),
);
this.#install(content.value, bytes);
this.#restart();
return ok(undefined);
}
Expand Down
65 changes: 65 additions & 0 deletions src/application/environment/environmentStateWriteAnswer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,68 @@ describe("path values", () => {
}
});
});

describe("collections a create chooses", () => {
const orgBook = (id: number, name: string): Book => ({ id, name });
/** One organisation listed under two spellings and another organisation, each with a create. */
const ROWS: readonly TimedCaptureRow[] = [
["GET", "/api/orgs/%61cme1/books", [orgBook(1, "Alpha")]],
["GET", "/api/orgs/acme1/books", [orgBook(2, "Beta")]],
["GET", "/api/orgs/beta2/books", [orgBook(3, "Gamma")]],
[
"POST",
"/api/orgs/acme1/books",
orgBook(4, "Delta"),
201,
{ name: "Delta" },
],
["GET", "/api/orgs/acme1/books", [orgBook(2, "Beta"), orgBook(4, "Delta")]],
[
"POST",
"/api/orgs/beta2/books",
orgBook(5, "Epsilon"),
201,
{ name: "Epsilon" },
],
[
"GET",
"/api/orgs/beta2/books",
[orgBook(3, "Gamma"), orgBook(5, "Epsilon")],
],
];

it("creates in the collection the call spells verbatim, as item calls choose their entity", () => {
const books = servedFrom(ROWS);
const tool = books.source.manifest.tools.find(
(candidate) => candidate.method === "POST",
)!;
expect(books.state.writes.get(tool.name)).toMatchObject({ served: true });
const [org] = environmentToolArguments(tool).path.values();
books.answer(tool.name, { [org!]: "acme1", body: { name: "Zeta" } });
expect(
books.store
.snapshot()
.rea_state.entities.find((entity) => entity.fields["name"] === "Zeta")
?.collection,
).toBe("/api/orgs/acme1/books");
});
});

describe("missing entities after a delete", () => {
it("answers with a recorded 2xx null, which the gate also took as an absence", () => {
const books = servedFrom([
...BOOK_INIT_ROWS,
["DELETE", "/api/books/2", "", 204],
["GET", "/api/books/2", null],
]);
expect(books.state.writes.get("delete_books_by_id")).toMatchObject({
served: true,
});
books.answer("delete_books_by_id", { id: "1" });
expect(books.answer("get_books_by_id", { id: "1" })).toMatchObject({
status: 200,
body: null,
state: { view: "missing", id: "1" },
});
});
});
15 changes: 12 additions & 3 deletions src/application/environment/environmentStateWriteAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,18 @@ const createdCollection = (
): string | undefined => {
const segments = callSegments(request.tool, request.args);
if (segments === undefined) return undefined;
const held = serving.store
.collectionsOf(request.plan.target ?? "")
.find((collection) => collectionMatch(collection, segments) !== undefined);
// The spelling the call supplies verbatim wins, as item calls choose their entity.
let held: string | undefined;
let verbatim = -1;
for (const collection of serving.store.collectionsOf(
request.plan.target ?? "",
)) {
const matched = collectionMatch(collection, segments);
if (matched !== undefined && matched > verbatim) {
held = collection;
verbatim = matched;
}
}
return (
held ??
`/${segments.map(({ text, supplied }) => (supplied ? encodeURIComponent(text) : text)).join("/")}`
Expand Down
47 changes: 47 additions & 0 deletions src/application/environment/environmentStateWriteRefusals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ describe("served document writes", () => {
});
});

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, [
Expand Down Expand Up @@ -219,4 +230,40 @@ describe("the state size limit", () => {
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);
});
});
2 changes: 1 addition & 1 deletion src/domain/analysisView/EnvironmentStateWriteSimulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class EnvironmentStateWriteSimulation {
unknown: (type, collection, id) =>
this.#taint.unknown(type, collection, id),
initialDocument: (tool) =>
this.#input.documents.find((document) => document.tool === tool)?.value,
this.#input.documents.find((document) => document.tool === tool),
};
}

Expand Down
27 changes: 24 additions & 3 deletions src/domain/analysisView/environmentStateWriteEffects.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { JsonValue } from "../jsonValue.js";
import {
collectionPathOf,
decodedSegment,
isObject,
isStateWrite,
lastSegmentValue,
locateItem,
at,
same,
segmentsOf,
type JsonObject,
type StateDocument,
type StateRecordFacts,
type StateToolFacts,
type StateView,
Expand Down Expand Up @@ -85,8 +88,8 @@ export interface EffectContext {
readonly observed: (ordinal: number) => void;
/** Whether an unmodelled write may have changed the entity since the state last saw it. */
readonly unknown: (type: string, collection: string, id: string) => boolean;
/** A document's value in the initial state, which serving also reads member types from. */
readonly initialDocument: (tool: string) => JsonValue | undefined;
/** A document as the initial state holds it: its path, and the value serving also reads member types from. */
readonly initialDocument: (tool: string) => StateDocument | undefined;
}

/** One recorded write: the record, its timeline index, its tool, and whether its plan only takes the recorded outcome. */
Expand Down Expand Up @@ -443,13 +446,31 @@ export const applyDelete = (
};
};

/** Whether a write's path names the held document's, a segment verbatim or once percent-decoded, as serving matches a call. */
const namesDocument = (path: string, held: string): boolean => {
const written = segmentsOf(path);
const recorded = segmentsOf(held);
return (
written.length === recorded.length &&
recorded.every(
(segment, index) =>
segment === written[index] ||
decodedSegment(segment) === written[index],
)
);
};

/** Apply a recorded document write: the body merged into, or replacing, the object holding most of its members. */
export const applyDocument = (
context: EffectContext,
draft: WriteDraft,
step: WriteStep,
): EffectResult => {
const tool = draft.plan.target!;
const initial = context.initialDocument(tool);
// Serving refuses a write at another path, so one here changes no document the state holds.
if (initial !== undefined && !namesDocument(step.record.path, initial.path))
return UNKNOWN;
const value = context.state.document(tool);
const body = requestObject(context, step.record);
const pointer =
Expand Down Expand Up @@ -482,7 +503,7 @@ export const applyDocument = (
// Serving refuses a body its check refuses, so such a call cannot confirm the plan.
if (
documentBodyProblem(draft.plan, call, {
initial: context.initialDocument(tool),
initial: initial?.value,
current: value,
}) !== undefined
) {
Expand Down
Loading