Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 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
9f60e54
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
e96484b
Merge main (#5319 squashed, #5349) into feat/environment-state-serve
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
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
5a61853
fix(environment): release batch replies as they are encoded; judge ex…
earakely-scale Oct 20, 2026
c00fde4
fix(environment): keep room for refusals and framing in the reply budget
earakely-scale Oct 20, 2026
25b6622
chore: merge main (335c042d) into environment state serve
earakely-scale Oct 21, 2026
6b8db19
fix(environment): refuse a loaded state that data/get would write pas…
earakely-scale Oct 21, 2026
31a618e
chore: merge main (d9767eb4) into environment state serve
cursoragent Oct 21, 2026
96ce6c6
fix(environment): keep merged export checks on the shared test graph
cursoragent Oct 21, 2026
bfe9d4b
chore: merge main into environment state serve
morluto Oct 21, 2026
0da8e23
fix(environment): keep a state file whose close failed owned until it…
earakely-scale Oct 21, 2026
98a7511
fix(environment): attempt all state cleanup on close
morluto Oct 21, 2026
d681bfd
fix(environment): bound wide state JSON traversal
morluto Oct 21, 2026
e4a0fc6
chore: merge current main into state server fixes
morluto Oct 21, 2026
467828e
fix(environment): scope cached state reply sizes to requests
morluto Oct 21, 2026
05deae3
fix(domain): preserve JSON depth diagnostic guidance
morluto Oct 21, 2026
7c3d0e9
chore: merge current main into state server fixes
morluto Oct 21, 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): catch cross-tool changes across writes; read only i…
…tem totals

A value that changed across a write was caught only between repeats of
one tool's request line, so a list read before a write and an item read
after it put the post-write value into the initial state. Changes are
now classified per entity in timeline order after observation: across
any recorded write, a change between repeats of a line or between two
lines of the type rewrites the type (every tool recorded as
fields-differ); with no write between, a repeat's change is volatile.
The classification lives in environmentStateMerge.ts.

Any numeric member whose name contained "total" or "count" counted as
an item total, so total_pages, account_id or page_count made complete
lists partial. Item totals are now members whose name tokens include
total or count and none of page, pages or per; a string of digits counts
as a number; and a set has_more, next or next-page token member also
marks the list partial.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
earakely-scale and claude committed Oct 20, 2026
commit 9fee36d3155145fa8e568b34713e0e68c7675216
8 changes: 5 additions & 3 deletions docs/specs/agent-environments/state-view/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ later increments.
latest values win over list views', differing views keep their own values,
and a value that changed between repeats of one request line with no recorded
write is volatile and keeps the latest; a change across a write under another
root leaves every tool of the type recorded.
- A list has `all` membership only when no `total` or `count` envelope member
differs from the number of entities it lists.
root, including between two tools of the type, leaves every tool of the type
recorded.
- A list has `all` membership only when no item-total envelope member (`total`,
`count` and their compounds, not page counts) differs from the number of
entities it lists and no `has_more`, `next` or next-page token is set.
- Keep an item or list plan only when it reproduces each request line's latest
initial recording from the state, comparing JSON values without regard to
member order; otherwise record the tool with a reason. Parse bodies only when
Expand Down
15 changes: 8 additions & 7 deletions docs/web-network-captures.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,18 +477,19 @@ whose latest values differ keeps them in `view_fields`, and a field whose value
changed between one tool's observations with no recorded write between is named
in the type's `volatile_fields`, keeping the latest value. A value that changed
across a write under another root may be that write's effect, so every tool of
its type is a recorded view with reason `fields-differ`. Entities are ordered
its type is a recorded view with reason `fields-differ`; so is a value one tool
read before a write and another tool read after it. Entities are ordered
by type, collection and first sighting; that order is a list's state order.
Every entity cites its `record_ordinals`.

`views` lists one plan per read tool in tool order:

| Shape | Plan |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `item` | The entity object at `pointer` (the root or one member), the ordered `fields` the view shows and the `required_fields` every recording had, and `envelope_ordinal`: the recording whose response, with the entity replaced, every item response shares |
| `list` | The array at `pointer`, its `fields`, `membership` (`all`: every entity of the collection in state order, when each recording listed exactly that, had no query and no `total` or `count` member differing from the number listed; `recorded`: the entities each recording listed) and `counters` that equal the number listed |
| `document` | The path's latest initial value, held in `documents` |
| `recorded` | A `reason` the tool replays its recordings instead |
| Shape | Plan |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `item` | The entity object at `pointer` (the root or one member), the ordered `fields` the view shows and the `required_fields` every recording had, and `envelope_ordinal`: the recording whose response, with the entity replaced, every item response shares |
| `list` | The array at `pointer`, its `fields`, `membership` (`all`: every entity of the collection in state order, when each recording listed exactly that, had no query, no item-total member (`total`, `count`, `totalCount`, `item_count`, but not `total_pages` or `per_page`; a number or a string of digits) differing from the number listed, and no `has_more`, `next` or next-page token member set; `recorded`: the entities each recording listed) and `counters` that equal the number listed |
| `document` | The path's latest initial value, held in `documents` |
| `recorded` | A `reason` the tool replays its recordings instead |

An item or list plan is kept only when it reproduces the latest initial
recording of each request line from the state, comparing JSON values without
Expand Down
41 changes: 17 additions & 24 deletions src/domain/analysisView/EnvironmentStateExtraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
locateList,
rootOf,
rootsRelated,
same,
scalarText,
segmentsOf,
apiBaseLengths,
Expand All @@ -40,9 +39,11 @@ import {
} from "./environmentStateRules.js";
import { ENTRY_BYTES, textBytes } from "./webNetworkRouteTemplates.js";
import {
classifyEntityChanges,
compareObservedEntities,
mergeObservedEntity,
type ObservedEntity,
type StateObservation,
} from "./environmentStateMerge.js";
import {
checkItemPlan,
Expand Down Expand Up @@ -114,6 +115,7 @@ export class EnvironmentStateExtraction {
for (const candidate of candidates.filter((entry) => !entry.item))
this.#planList(candidate);
for (const plan of this.#plans) this.#observePlan(plan);
this.#classifyChanges();
this.#merge();
const gate = this.#gateContext();
for (const plan of this.#plans)
Expand Down Expand Up @@ -376,29 +378,6 @@ export class EnvironmentStateExtraction {
if (position < first || (position === first && index < at))
entity.order = [position, index];
}
// Only a repeated request line shows a value changing; other lines may differ by query.
const line = requestLine(record);
const earlier = entity.observations.findLast(
(observation) =>
observation.tool === plan.tool.name &&
requestLine(this.#byOrdinal.get(observation.ordinal)!) === line,
);
const changed = Object.keys(object).filter(
(name) =>
earlier !== undefined &&
Object.hasOwn(earlier.object, name) &&
!same(earlier.object[name], object[name]),
);
if (earlier !== undefined && changed.length > 0) {
// A change across any recorded write, even under another root, is not evidence of volatility.
if (this.#writeWithin(this.#byOrdinal.get(earlier.ordinal)!, record))
this.#rewritten.add(plan.type);
else
this.#volatile.set(
plan.type,
new Set([...(this.#volatile.get(plan.type) ?? []), ...changed]),
);
}
this.#retain(ENTRY_BYTES + jsonBytes(object));
entity.observations.push({
position,
Expand All @@ -409,6 +388,20 @@ export class EnvironmentStateExtraction {
});
}

/** A change across any recorded write rewrites the type; one between repeats of a line with no write is volatile. */
#classifyChanges(): void {
const recordOf = (observation: StateObservation): StateRecordFacts =>
this.#byOrdinal.get(observation.ordinal)!;
const changes = classifyEntityChanges(
this.#entities.values(),
(observation) => [observation.tool, requestLine(recordOf(observation))],
(earlier, later) => this.#writeWithin(recordOf(earlier), recordOf(later)),
);
for (const type of changes.rewritten) this.#rewritten.add(type);
for (const [type, names] of changes.volatile)
this.#volatile.set(type, names);
}

#merge(): void {
for (const entity of [...this.#entities.values()].sort(
compareObservedEntities,
Expand Down
52 changes: 48 additions & 4 deletions src/domain/analysisView/environmentStateGate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,61 @@ const listFacts = (
} else all = false;
const parent = plan.pointer === "" || !isObject(body) ? {} : body;
const counted = Object.keys(parent).filter(
(name) =>
/total|count/iu.test(name) && typeof parent[name] === "number",
(name) => isItemTotal(name) && countOf(parent[name]) !== undefined,
);
// A count that differs from the number listed shows the list is not the whole collection.
if (counted.some((name) => parent[name] !== items.length)) all = false;
// A total that differs from the number listed, or a next page, shows the list is partial.
if (
counted.some((name) => countOf(parent[name]) !== items.length) ||
Object.keys(parent).some((name) => continues(name, parent[name]))
)
all = false;
const names = counted.filter((name) => parent[name] === items.length);
counters = (counters ?? names).filter((name) => names.includes(name));
});
return { all, counters: (counters ?? []).sort(compare) };
};

const nameTokens = (name: string): string[] =>
name
.split(/[_\-\s]|(?<=[a-z0-9])(?=[A-Z])/u)
.map((token) => token.toLowerCase());

/** `total`, `count`, `totalCount` or `item_count`, but not `total_pages`, `per_page` or `account_id`. */
const isItemTotal = (name: string): boolean => {
const tokens = nameTokens(name);
return (
tokens.some((token) => token === "total" || token === "count") &&
!tokens.some(
(token) => token === "page" || token === "pages" || token === "per",
)
);
};

/** A count recorded as a number or as a string of digits. */
const countOf = (value: JsonValue | undefined): number | undefined =>
typeof value === "number"
? value
: typeof value === "string" && /^\d+$/u.test(value)
? Number(value)
: undefined;

const CONTINUATION_NAMES = new Set([
"hasmore",
"next",
"nextcursor",
"nextpage",
"nextpagetoken",
"nexttoken",
]);

/** A top-level `has_more`, `next` or next-page token member that says more items follow. */
const continues = (name: string, value: JsonValue | undefined): boolean =>
CONTINUATION_NAMES.has(nameTokens(name).join("")) &&
value !== undefined &&
value !== null &&
value !== false &&
value !== "";

/** Whether one latest list recording renders from its own envelope and the listed entities' state. */
const listReproduces = (
context: StateGateContext,
Expand Down
54 changes: 54 additions & 0 deletions src/domain/analysisView/environmentStateMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,57 @@ export const mergeObservedEntity = (entity: ObservedEntity): StateEntity => {
],
};
};

/**
* How observed values changed, per entity in timeline order. A shared field that differs from the
* previous repeat of the same request line is volatile, unless a recorded write lies between them;
* a field that differs from the previous observation of another line across a write rewrites the
* type. Either across a write means the reads do not agree on one initial state.
*/
export const classifyEntityChanges = (
entities: Iterable<ObservedEntity>,
lineOf: (observation: StateObservation) => readonly string[],
writeBetween: (earlier: StateObservation, later: StateObservation) => boolean,
): {
readonly rewritten: ReadonlySet<string>;
readonly volatile: ReadonlyMap<string, Set<string>>;
} => {
const rewritten = new Set<string>();
const volatile = new Map<string, Set<string>>();
const changed = (before: StateObservation, after: StateObservation) =>
Object.keys(after.object).filter(
(name) =>
Object.hasOwn(before.object, name) &&
!same(before.object[name], after.object[name]),
);
for (const entity of entities) {
const timeline = [...entity.observations].sort(
(left, right) => left.position - right.position,
);
const lastOfLine = new Map<string, StateObservation>();
timeline.forEach((after, index) => {
const line = JSON.stringify(lineOf(after));
const repeat = lastOfLine.get(line);
const differs = repeat === undefined ? [] : changed(repeat, after);
if (repeat !== undefined && differs.length > 0) {
if (writeBetween(repeat, after)) rewritten.add(entity.type);
else
for (const name of differs)
volatile.set(
entity.type,
(volatile.get(entity.type) ?? new Set()).add(name),
);
}
const previous = timeline[index - 1];
if (
previous !== undefined &&
JSON.stringify(lineOf(previous)) !== line &&
changed(previous, after).length > 0 &&
writeBetween(previous, after)
)
rewritten.add(entity.type);
lastOfLine.set(line, after);
});
}
return { rewritten, volatile };
};
43 changes: 43 additions & 0 deletions src/domain/analysisView/environmentStateView.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,46 @@ it("does not take a later recording that is itself an entity as the list", () =>
);
expect(state.entities).toEqual([]);
});

it("leaves a type recorded when two of its tools read different values across a write", () => {
const state = stateOf(
captureOf([
["GET", "/api/search", [{ id: 1, title: "Granite Ledger" }]],
["POST", "/api/dashboards/db", { id: 9 }],
["GET", "/api/search/1", { id: 1, title: "Saltmarsh Depot" }],
]),
);
expect(state.types).toEqual([]);
expect(state.entities).toEqual([]);
expect(
state.views.map((view) =>
view.shape === "recorded" ? [view.tool, view.reason] : view.tool,
),
).toEqual([
["get_search", "fields-differ"],
["get_search_by_id", "fields-differ"],
]);
});

it("reads only item totals and next-page markers as signs of a partial list", () => {
const membership = (body: JsonValue) =>
stateOf(captureOf([["GET", "/api/items", body]])).views.map((view) =>
view.shape === "list" ? [view.membership, view.counters] : view.shape,
);
// Page and id numbers are not item totals, so a complete page stays whole.
expect(
membership({
page: 1,
total: 2,
total_pages: 1,
account_id: 77,
items: [{ id: 1 }, { id: 2 }],
}),
).toEqual([["all", ["total"]]]);
expect(membership({ has_more: true, items: [{ id: 1 }, { id: 2 }] })).toEqual(
[["recorded", []]],
);
expect(membership({ total: "50", items: [{ id: 1 }] })).toEqual([
["recorded", []],
]);
});