Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion docs/agent-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ A list or document call that names no checked line, including a line recorded
after a write or with other query values, answers from the recording. Every
answer has the replay shape plus `state`: `{"source": "state", "view": …}`
naming the entity, collection or document path, or `{"source": "recording",
"reason": …}` with the view's reason, `no-view` or `unrecorded-request`. A
"reason": …}` with the view's reason, `no-view`, `unrecorded-request` or `page-exceeded` (a call naming a page of a whole list that the collection has outgrown). A
state answer carries the base record's status, content type and `replay`
provenance with `truncated: false`; one whose JSON would exceed 256 KiB is
refused as a `resource_constraint` tool error rather than cut. Query `POST`s
Expand Down
32 changes: 23 additions & 9 deletions docs/specs/agent-environments/state-writes/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ the exemplar.
one-element array, as item views locate it. When the response does not carry
it, the entity is observed in the first later read that shows it: an item
read of that entity, or for a create, the one entity a collection read newly
lists. With neither, the reason is `no-observation`.
lists. A create whose response names its id alone (a scalar, the identity
field, or one member) is observed only through a read of that id. With
neither, the reason is `no-observation`.

**Field rules for creates.** Each field of the observed entity gets the first
rule that explains its value. Rules that read the call come first, so an
Expand Down Expand Up @@ -174,18 +176,26 @@ members they sent.
**Reads.** A read is checked when the served environment would answer it
from the state:

- an item read of a state-backed item view;
- an item read of a state-backed item view, of the entity serving would name:
the read's collection and id as written, else a held collection whose path
matches once percent-decoded;
- a list or document read on one of its view's checked request lines,
chosen as replay chooses the line for the read's call: each path parameter
the read's value verbatim or once percent-decoded, preferring verbatim
spellings, then recorded order, and the query values decoded, in any order.
A read with no query on a whole list falls back, as serving does, on a
checked line that only pages it. A line whose query repeats a name is never
chosen, since no call's arguments can name it. A list is compared as served
from the collection of that line.
checked line that only pages it. A line whose query repeats a name with
different values is never chosen, since no call's arguments can name it; one
repeating a name with one value is, as a call naming it matches both. A list
is compared as served from the collection of that line. A read on a page
(`limit`, `size`, `per_page` and the like) of a whole list checks nothing
once the collection holds more entities than the page, which serving then
answers from its recording.
- A read made while a modelled write to its entity or collection was still
running, starting before that write ended, checks nothing: it may show the
state before or after the write.
- A read a write's effect was learned from checks nothing of that write, but
still contradicts the other writes it exercises.

**Comparison.** The read's answer is the full response serving would give from
the copy: an item read's entity rendered into its item view's base recording,
Expand All @@ -205,9 +215,13 @@ refuses to answer with.
An update's effect is the fields it sent or changed, derived ones included.
A later update overwrites the fields it sets (an increment adds to its field
instead), a recorded outcome every field, and a create or delete the whole
entity. A write with nothing left on an entity no longer counts there, nor in
a collection where no entity it changed keeps any of its effect. A pass confirms them and a mismatch
contradicts them.
entity. A document write's effect is the members it merged, or the whole
document when it replaces it. A write with nothing left on an entity no
longer counts there, nor in a collection where no entity it changed keeps any
of its effect. A mismatch contradicts them. A pass confirms those it shows
something of: a create's or delete's membership, or a changed field the
read's view shows and does not treat as volatile; a read that shows none of
an update's changes confirms nothing of it.
- A mismatch after an unmodelled write that followed those writes is checked
only on the members they sent: such a write may explain the rest.
- A mismatch on an entity or collection that no write changed contradicts,
Expand All @@ -221,7 +235,7 @@ refuses to answer with.
becomes the plan's rule, unless an earlier list check relied on the rule
(an item read's confirmation does not fix it).
- The read that supplied a create's or update's observation does not check
it.
it, but checks the other writes it exercises.

**Verdicts.** A plan is served when at least one check confirms it and none
contradicts it. Otherwise the reason, in this precedence, is:
Expand Down
23 changes: 23 additions & 0 deletions docs/specs/agent-environments/state-writes/verification.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,26 @@ against this branch's code:
Each new test fails on the previous head. The verdicts over the 50 captures
are unchanged plan by plan, the smoke applies all 31 served write tools, and
the gates passed again.

An eleventh round settled six more findings, five raised on this branch and one on the fidelity branch:

- a passing read confirms only the writes it shows something of;
- an item read names its entity as serving does, percent-decoded spellings included;
- a read a write's effect was learned from still contradicts the other writes;
- a line repeating a query name with one value stays checkable;
- document writes join the effect tracking, so a replacement overwrites an earlier merge;
- a call naming a page that the collection has outgrown answers from its recording (`page-exceeded`), and the gate does not check such reads.

Each new test fails on the previous head. The verdicts over the 50 captures are unchanged plan by plan, the smoke
applies all 31 served write tools, and the gates passed again.

A twelfth round settled four findings Greptile raised on the list-lines branch against this branch's code:

- a create that answers with its id alone is observed only through a read of that id;
- a later call that is the first to send the path's id in its body adds the identity member to the plan;
- the copy search walks a request body with an index, so a wide body no longer takes quadratic time (500,000 zeros: 63 s
before, 74 ms after);
- the sent values of fixed members a plan keeps are counted against the view's allowance.

Each new test fails on the previous head. The verdicts over the 50 captures are unchanged plan by plan, the smoke
applies all 31 served write tools, and the gates passed again.
15 changes: 14 additions & 1 deletion src/application/environment/environmentStateAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
allLines,
listItems,
listsAll,
pageSize,
pagesOnly,
missingRequiredFields,
renderStateEntity,
Expand Down Expand Up @@ -50,7 +51,11 @@ import { environmentToolArguments } from "./environmentTools.js";
export type StateProvenance =
| {
readonly source: "recording";
readonly reason: RecordedViewReason | "no-view" | "unrecorded-request";
readonly reason:
| RecordedViewReason
| "no-view"
| "unrecorded-request"
| "page-exceeded";
}
| {
readonly source: "state";
Expand Down Expand Up @@ -423,6 +428,14 @@ const answerList = (
const collection = collectionPathOf(base, false);
const type = store.type(view.type);
const all = listsAll(view, base.ordinal);
// A requested page the collection has outgrown is answered as recorded, never with more items than it asked for.
const size = exact ? pageSize(base.query ?? undefined) : undefined;
if (
all &&
size !== undefined &&
store.inCollection(view.type, collection).length > size
)
return fromRecording(source, tool, args, "page-exceeded");
const members: readonly StateEntity[] = all
? store.inCollection(view.type, collection)
: recorded.flatMap((item) => {
Expand Down
23 changes: 23 additions & 0 deletions src/application/environment/environmentStateWriteAnswer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,26 @@ describe("values the state started with", () => {
expect(create("acme1", "Epsilon")).toBe("/api/orgs/%61cme1/books");
});
});

describe("paged lines", () => {
it("answers a requested page the collection has outgrown from its recording, and a call with no query from the state", () => {
const books = servedFrom([
["GET", "/api/books?limit=2", [FIRST_BOOK, SECOND_BOOK]],
["GET", "/api/books/1", FIRST_BOOK],
["GET", "/api/books/2", SECOND_BOOK],
["POST", "/api/books", THIRD_BOOK, 201, { name: "Third Book" }],
// The application answers the same page with two books; serving would not answer it from the state.
["GET", "/api/books?limit=2", [FIRST_BOOK, SECOND_BOOK]],
["GET", "/api/books", [FIRST_BOOK, SECOND_BOOK, THIRD_BOOK]],
]);
const created = books.answer("post_books", CREATE_NEW_BOOK).body as Book;
expect(books.answer("get_books", { limit: "2" })).toMatchObject({
body: [FIRST_BOOK, SECOND_BOOK],
state: { source: "recording", reason: "page-exceeded" },
});
expect(books.answer("get_books")).toMatchObject({
body: [FIRST_BOOK, SECOND_BOOK, created],
state: { source: "state", membership: "all", listed: 3 },
});
});
});
199 changes: 199 additions & 0 deletions src/domain/analysisView/EnvironmentStateWriteSimulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
import type { JsonValue } from "../jsonValue.js";
import { ok } from "../result.js";
import { updatedFields } from "./environmentStateWriteApply.js";
import {
retainFixed,
type EffectContext,
} from "./environmentStateWriteEffects.js";

/** The plan the gate gives `tool` over a capture of `rows`. */
const gated = (rows: readonly TimedCaptureRow[], tool: string) => {
Expand Down Expand Up @@ -613,3 +617,198 @@ describe("reads as serving answers them", () => {
});
});
});

describe("what a read shows of a write", () => {
it("confirms no update by a read that shows none of the fields it changed", () => {
const rows: TimedCaptureRow[] = [
[
"GET",
"/api/books",
[
{ id: 1, name: "A" },
{ id: 2, name: "B" },
],
],
["GET", "/api/books/1", { id: 1, name: "A", description: "x" }],
["GET", "/api/books/2", { id: 2, name: "B", description: "y" }],
[
"PATCH",
"/api/books/1",
{ id: 1, name: "A", description: "new" },
200,
{ description: "new" },
],
// The list passes, but it shows no description.
[
"GET",
"/api/books",
[
{ id: 1, name: "A" },
{ id: 2, name: "B" },
],
],
];
expect(gated(rows, "patch_books_by_id")).toMatchObject({
served: false,
reason: "unconfirmed",
checked_record_ordinals: [],
});
});

it("lets a read a create was observed through still contradict another write", () => {
const rows: TimedCaptureRow[] = [
["GET", "/api/books", [FIRST_BOOK, SECOND_BOOK]],
["GET", "/api/books/1", FIRST_BOOK],
["GET", "/api/books/2", SECOND_BOOK],
["PATCH", "/api/books/1", RENAMED, 200, { name: "Renamed Book" }],
["GET", "/api/books/1", RENAMED],
// The create answers with its id alone, so the next list shows its entity; that list shows book 1 unrenamed.
["POST", "/api/books", { id: 3 }, 201, { name: "Third Book" }],
["GET", "/api/books", [FIRST_BOOK, SECOND_BOOK, THIRD_BOOK]],
];
expect(gated(rows, "patch_books_by_id")).toMatchObject({
served: false,
contradicted_by: 6,
});
});

it("confirms no document write a later replacement overwrote", () => {
const rows: TimedCaptureRow[] = [
["GET", "/api/settings", { theme: "light", lang: "en" }],
[
"PATCH",
"/api/settings",
{ theme: "dark", lang: "en" },
200,
{ theme: "dark" },
],
[
"PUT",
"/api/settings",
{ theme: "blue", lang: "fr" },
200,
{ theme: "blue", lang: "fr" },
],
["GET", "/api/settings", { theme: "blue", lang: "fr" }],
];
expect(gated(rows, "patch_settings")).toMatchObject({
served: false,
checked_record_ordinals: [],
});
expect(gated(rows, "put_settings")).toMatchObject({
served: true,
checked_record_ordinals: [3],
});
});
});

describe("reads named as serving names them", () => {
const orgBook = (id: number, org: string, name: string): Book => ({
id,
org,
name,
});
it("contradicts an update by a read spelled plainly of the encoded collection serving answers it from", () => {
const alpha = orgBook(1, "acme1", "Alpha");
const renamed = orgBook(1, "acme1", "Renamed");
const beta = orgBook(2, "beta2", "Beta");
const renamedBeta = orgBook(2, "beta2", "Renamed Beta");
const rows: TimedCaptureRow[] = [
["GET", "/api/orgs/%61cme1/books", [alpha]],
["GET", "/api/orgs/beta2/books", [beta]],
["GET", "/api/orgs/%61cme1/books/1", alpha],
["GET", "/api/orgs/beta2/books/2", beta],
["PATCH", "/api/orgs/%61cme1/books/1", renamed, 200, { name: "Renamed" }],
[
"PATCH",
"/api/orgs/beta2/books/2",
renamedBeta,
200,
{ name: "Renamed Beta" },
],
// Serving answers this plain spelling with the renamed book, which the recording shows absent.
["GET", "/api/orgs/acme1/books/1", null],
["GET", "/api/orgs/%61cme1/books/1", renamed],
["GET", "/api/orgs/beta2/books/2", renamedBeta],
];
const { writes } = environmentStateViewOf(timedCaptureEvidence(rows), [
"sibling-variance",
]);
expect(writes.find((plan) => plan.effect === "update")).toMatchObject({
served: false,
contradicted_by: 6,
});
});

it("checks a line whose query repeats a name with one value, which a call still names", () => {
const rows: TimedCaptureRow[] = [
["GET", "/api/books?tag=a&tag=a", [FIRST_BOOK]],
["GET", "/api/books/1", FIRST_BOOK],
["GET", "/api/books/2", SECOND_BOOK],
["PATCH", "/api/books/1", RENAMED, 200, { name: "Renamed Book" }],
// The list still shows the old name, though the item read after it shows the rename.
["GET", "/api/books?tag=a&tag=a", [FIRST_BOOK]],
["GET", "/api/books/1", RENAMED],
];
expect(gated(rows, "patch_books_by_id")).toMatchObject({
served: false,
contradicted_by: 4,
});
});
});

describe("what a write's own answer pins down", () => {
it("observes a create that answers with its id alone only through a read of that id", () => {
const fourth = book(4, "Fourth Book", "2026-10-01T11:00:00Z");
const rows: TimedCaptureRow[] = [
["GET", "/api/books", [FIRST_BOOK, SECOND_BOOK]],
["GET", "/api/books/1", FIRST_BOOK],
["GET", "/api/books/2", SECOND_BOOK],
["POST", "/api/books", { id: 3 }, 201, { name: "Third Book" }],
// A book the capture never listed is not the one the create made.
["GET", "/api/books/4", fourth],
["GET", "/api/books/4", fourth],
["GET", "/api/books/3", THIRD_BOOK],
["GET", "/api/books/3", THIRD_BOOK],
];
expect(gated(rows, "post_books")).toMatchObject({
observation: { ordinal: 6, pointer: "" },
checked_record_ordinals: [7],
});
});

it("keeps an update served when a later call is the first to send the path's id in its body", () => {
const renamedSecond = renamedBook(SECOND_BOOK, "Renamed Second", 5);
const rows: TimedCaptureRow[] = [
["GET", "/api/books", [FIRST_BOOK, SECOND_BOOK]],
["GET", "/api/books/1", FIRST_BOOK],
["GET", "/api/books/2", SECOND_BOOK],
["PATCH", "/api/books/1", RENAMED, 200, { name: "Renamed Book" }],
["GET", "/api/books/1", RENAMED],
[
"PATCH",
"/api/books/2",
renamedSecond,
200,
{ id: 2, name: "Renamed Second" },
],
["GET", "/api/books/2", renamedSecond],
];
expect(gated(rows, "patch_books_by_id")).toMatchObject({
served: true,
body: { identity: "id" },
});
});
});

it("counts the sent values a plan keeps for fixed members against the view's allowance", () => {
let retained = 0;
const context = {
io: { retain: (bytes: number) => (retained += bytes) },
} as unknown as EffectContext;
retainFixed(context, [{ sent: "x".repeat(10_000) }]);
expect(retained).toBeGreaterThan(20_000);
retained = 0;
retainFixed(context, []);
expect(retained).toBe(0);
});
Loading