fix(environment): keep post-write observations out of initial state

This commit is contained in:
gensokyo-dev committed 2026-10-20 02:53:20 +00:00
1 parent 32fbaa5de7
commit 04e2c568be
9 files changed
+175 -14

No files matched your search

@@ -35,7 +35,7 @@ later increments.
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, including between two tools of the type, leaves every tool of the type
recorded.
recorded. Compare list membership, including empty lists, and field presence before selecting each request line's latest recording; a change across a write also leaves the type recorded. Different query strings remain distinct.
- 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.
+1 -1
View File
@@ -492,7 +492,7 @@ 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`; so is a value one tool
read before a write and another tool read after it. Entities are ordered
read before a write and another tool read after it. Repeated reads of one full request line also keep the type recorded when list membership changes across a write, including transitions to or from an empty list, or when entity fields are added or removed. Different query strings remain distinct request lines. Entities are ordered
by type, collection and first sighting; that order is a list's state order.
Every entity cites its `record_ordinals`.
+1 -1
View File
@@ -17,7 +17,7 @@ export const ANALYSIS_VIEW_TOOL_CONTRACTS = [
...toolContractMetadata("inspect_analysis_view"),
kind: "application",
description:
'Inspect selected views of completed binary-layout, JavaScript application, native analyze_function, inspect_artifact, inventory_artifact, inspect_wasm_artifact, inspect_macho, decode_interface_builder, inspect_evm_interface, inspect_managed_artifact, inspect_managed_members, inspect_android_package, inspect_dart_aot, inspect_il2cpp_metadata or inspect_web_network_capture Evidence without repeating analysis. Historical capture summaries return identity and counts without bodies or archive inventories. Capture views page retained records: view {kind: "capture", detail: "preview" | "complete", offset: 0, limit: 20}; preview selects recorded headline fields and binary descriptors, complete returns exact records with all sidecars. Offsets follow the selected parent record order, preserving original producer ordinals. Paging never reopens the source or reruns the decoder. For inspect_pcap_capture parents, kind pcap pages endpoints, dns-queries, tls-sni, protocols, http-uris or unattributed-addresses. Select an exact query for dns-addresses, or a tshark/capinfos stream for UTF-16 diagnostics text. Page totals count retained facts; source coverage remains explicit. Qlog summaries keep profile/scope and counts; kind qlog pages traces, streams, loss-declarations, incomplete-records, contributions, ranges or final-sizes. Except traces, select trace_index; stream-specific facets also require exact stream_id and direction. Facts retain all-recorded-events-before-selection scope, and interval overlap or endpoint loss declarations do not prove retransmission or network loss. The api-surface view groups retained HAR requests by method, origin and format-rule path template; templated segments are inferences. It reads no headers or bodies and reports no query values. The environment-manifest view projects the same HAR requests on the primary origin (most JSON-like records) into a replayable tool surface: view {kind: "environment-manifest", template_rules: ["after-uid-literal", "opaque-token", "sibling-variance"], include_query_parameter_names: true}. Each tool has a deterministic name, a path template and parameters from the four format rules plus the selected opt-in rules, observed query parameter names, a value-free body shape for POST/PUT/PATCH, status and content-type sets and record ordinals; records are pointers into the parent, excluded groups carry a reason and three fixed unknowns are always stated. Templates, parameters and names are inferences; the view copies no header, cookie or body values and refuses parents inspected with sensitive values. A capture_browser_scenario parent that selected response bodies or header values projects the same way, one record per network transaction in request order; it relies on capture-time redaction of declared secrets instead of that refusal, and a transaction whose URL was redacted is left unrepresented. The environment-state view extracts, from the same parents, the initial state the recorded reads observed: view {kind: "environment-state", template_rules: [...]} with the rules of the manifest it accompanies. It reads response bodies recorded before the first related write and reports entity types with an identity field (proved by a path parameter or inferred from a field name), entities in collection order with per-view values where views differ, documents for written non-entity paths, and a render plan per read tool: item and list views reproduce their latest recordings from the state, and every other read tool is a recorded view with a reason. Types and identity are inferences; values are recorded data, so it refuses parents inspected with sensitive values, and a scenario parent must have selected response bodies; its values keep the capture redaction markers, and a write whose URL it redacted ends the state of every type. For a retained record use source: {kind: "retained-evidence", evidence_id: "<parent_evidence_id>"}. Use normalized_result.parent_evidence_id from a summary receipt. Source kind is required. Portable source uses {kind: "inline", evidence: <complete Evidence>}. A completed Evidence JSON file on this host uses {kind: "evidence-file", path: "<absolute path>"}: REA reads and authenticates it, then projects it like retained Evidence, so a large capture is bounded by the file reader rather than by inline-input accounting. For layout, JavaScript, inventory, WASM, Mach-O, Interface Builder, EVM, managed artifact, Android package and Dart AOT sources, request a summary with view: {kind: "summary"}; summary is a view kind and has no facet. The WASM summary gives validation, counts, WAT digest/length, tool profile and execution/network states without reading WAT text. Select a layout mitigations/linkage facet, one section/symbol/module, or a stable page. Module pages include assets, bundled and source modules; use node_id for ambiguous or unavailable paths. Typed module references use view {kind: "javascript", collection: "modules", projection: "references", Line truncated
'Inspect selected views of completed binary-layout, JavaScript application, native analyze_function, inspect_artifact, inventory_artifact, inspect_wasm_artifact, inspect_macho, decode_interface_builder, inspect_evm_interface, inspect_managed_artifact, inspect_managed_members, inspect_android_package, inspect_dart_aot, inspect_il2cpp_metadata or inspect_web_network_capture Evidence without repeating analysis. Historical capture summaries return identity and counts without bodies or archive inventories. Capture views page retained records: view {kind: "capture", detail: "preview" | "complete", offset: 0, limit: 20}; preview selects recorded headline fields and binary descriptors, complete returns exact records with all sidecars. Offsets follow the selected parent record order, preserving original producer ordinals. Paging never reopens the source or reruns the decoder. For inspect_pcap_capture parents, kind pcap pages endpoints, dns-queries, tls-sni, protocols, http-uris or unattributed-addresses. Select an exact query for dns-addresses, or a tshark/capinfos stream for UTF-16 diagnostics text. Page totals count retained facts; source coverage remains explicit. Qlog summaries keep profile/scope and counts; kind qlog pages traces, streams, loss-declarations, incomplete-records, contributions, ranges or final-sizes. Except traces, select trace_index; stream-specific facets also require exact stream_id and direction. Facts retain all-recorded-events-before-selection scope, and interval overlap or endpoint loss declarations do not prove retransmission or network loss. The api-surface view groups retained HAR requests by method, origin and format-rule path template; templated segments are inferences. It reads no headers or bodies and reports no query values. The environment-manifest view projects the same HAR requests on the primary origin (most JSON-like records) into a replayable tool surface: view {kind: "environment-manifest", template_rules: ["after-uid-literal", "opaque-token", "sibling-variance"], include_query_parameter_names: true}. Each tool has a deterministic name, a path template and parameters from the four format rules plus the selected opt-in rules, observed query parameter names, a value-free body shape for POST/PUT/PATCH, status and content-type sets and record ordinals; records are pointers into the parent, excluded groups carry a reason and three fixed unknowns are always stated. Templates, parameters and names are inferences; the view copies no header, cookie or body values and refuses parents inspected with sensitive values. A capture_browser_scenario parent that selected response bodies or header values projects the same way, one record per network transaction in request order; it relies on capture-time redaction of declared secrets instead of that refusal, and a transaction whose URL was redacted is left unrepresented. The environment-state view extracts, from the same parents, the initial state the recorded reads observed: view {kind: "environment-state", template_rules: [...]} with the rules of the manifest it accompanies. It reads response bodies recorded before the first related write and reports entity types with an identity field (proved by a path parameter or inferred from a field name), entities in collection order with per-view values where views differ, documents for non-parameterized non-entity paths written by POST/PUT/PATCH (item-shaped and delete-only paths stay recorded), and a render plan per read tool: item and list views reproduce their latest recordings from the state, and every other read tool is a recorded view with a reason. Types and identity are inferences; values are recorded data, so it refuses parents inspected with sensitive values, and a scenario parent must have selected response bodies; its values keep the capture redaction markers, and a write whose URL it redacted ends the state of every type. For a retained record use source: {kind: "retained-evidence", evidence_id: "<parent_evidence_id>"}. Use normalized_result.parent_evidence_id from a summary receipt. Source kind is required. Portable source uses {kind: "inline", evidence: <complete Evidence>}. A completed Evidence JSON file on this host uses {kind: "evidence-file", path: "<absolute path>"}: REA reads and authenticates it, then projects it like retained Evidence, so a large capture is bounded by the file reader rather than by inline-input accounting. For layout, JavaScript, inventory, WASM, Mach-O, Interface Builder, EVM, managed artifact, Android package and Dart AOT sources, request a summary with view: {kind: "summary"}; summary is a view kind and has no facet. The WASM summary gives validation, counts, WAT digest/length, tool profile and execution/network states without reading WAT text. Select a layout mitigations/linkage facet, one section/symbol/module, or a stable page. Module pages include assets, bundled and source modules; use node_id for ambiguous or unavailable paths. Typed module refeLine truncated
inputSchema: inspectAnalysisViewInputSchema,
outputSchema: evidenceResultOf(analysisViewResultSchema),
examples: [
@@ -56,6 +56,12 @@ class StateBudgetExhausted extends Error {}
type Candidate = Pick<StatePlan, "tool" | "item" | "type">;
interface ObservedMembership {
record: StateRecordFacts;
readonly ids: Set<string>;
capacity: number;
}
const entityKey = (type: string, collection: string, id: string): string =>
compositeKey([type, collection, id]);
@@ -332,6 +338,12 @@ export class EnvironmentStateExtraction {
}
#observePlan(plan: StatePlan): void {
// Only lists whose observations span a write need membership snapshots, including empty lists.
const memberships =
!plan.item &&
this.#writeWithin(plan.recordings[0]!, plan.recordings.at(-1)!)
? new Map<string, ObservedMembership>()
: undefined;
for (const record of plan.recordings)
this.#withBody(record.ordinal, (body) => {
const located = body === undefined ? undefined : at(body, plan.pointer);
@@ -339,15 +351,49 @@ export class EnvironmentStateExtraction {
? isObject(located)
? [located]
: []
: Array.isArray(located)
? located.filter(isObject)
: [];
: body === undefined
? []
: (listItems(body, plan.pointer) ?? []);
if (memberships !== undefined)
this.#observeMembership(plan, record, objects, memberships);
objects.forEach((object, index) =>
this.#observe(plan, record, object, index),
);
});
}
/** Compare one request line's membership before discarding older recordings; reuse its bounded set. */
#observeMembership(
plan: StatePlan,
record: StateRecordFacts,
objects: readonly JsonObject[],
memberships: Map<string, ObservedMembership>,
): void {
const line = requestLine(record);
let previous = memberships.get(line);
const earlier = previous?.record;
if (previous === undefined) {
this.#retain(ENTRY_BYTES * 2 + textBytes(line.length));
previous = { record, ids: new Set(), capacity: 0 };
memberships.set(line, previous);
}
// Reserve the peak membership representation before growing the set, including numeric ID text.
if (objects.length > previous.capacity) {
this.#retain((objects.length - previous.capacity) * ENTRY_BYTES * 4);
previous.capacity = objects.length;
}
const field = this.#identity.get(plan.type)!.field;
let changed = previous.ids.size !== objects.length;
for (const object of objects)
if (!previous.ids.delete(scalarText(object[field])!)) changed = true;
if (previous.ids.size > 0) changed = true;
if (earlier !== undefined && changed && this.#writeWithin(earlier, record))
this.#rewritten.add(plan.type);
previous.ids.clear();
for (const object of objects) previous.ids.add(scalarText(object[field])!);
previous.record = record;
}
/** Record one observed entity object; a value that changes between one tool's own observations is volatile. */
#observe(
plan: StatePlan,
@@ -100,11 +100,12 @@ export const classifyEntityChanges = (
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)
if (repeat !== undefined) {
if (writeBetween(repeat, after)) {
// Key additions/removals are changes too, even when every shared field is unchanged.
if (!same(repeat.object, after.object)) rewritten.add(entity.type);
} else
for (const name of changed(repeat, after))
volatile.set(
entity.type,
(volatile.get(entity.type) ?? new Set()).add(name),
@@ -234,8 +234,10 @@ export const apiBaseLengths = (
const groups = new Map<string, string[][]>();
for (const template of templates) {
const segments = segmentsOf(template);
const group = groups.get(segments[0] ?? "") ?? [];
groups.set(segments[0] ?? "", [...group, segments]);
const first = segments[0] ?? "";
const group = groups.get(first);
if (group === undefined) groups.set(first, [segments]);
else group.push(segments);
}
return new Map(
[...groups.entries()].map(([first, members]) => {
@@ -375,6 +375,23 @@ it("holds a written path's non-entity read as a document", () => {
});
});
it.each([
["/api/files/123", "PUT", "get_files_by_id"],
["/api/settings", "DELETE", "get_settings"],
] as const)(
"leaves a deferred document path recorded (%s, %s)",
(path, method, tool) => {
const state = stateOf(
captureOf([
["GET", path, { text: "initial document" }],
[method, path, null],
]),
);
expect(state.documents).toEqual([]);
expect(viewOf(state, tool)).toMatchObject({ shape: "recorded" });
},
);
it("orders a scenario capture by request order and needs its response bodies", () => {
const state = stateOf(parentOf(scenarioCaptureEvidence()));
expect(state.timeline).toBe("record-order");
@@ -531,6 +548,93 @@ it("leaves a type recorded when two of its tools read different values across a
]);
});
it.each([
[[], [{ id: 1 }]],
[[{ id: 1 }], []],
[[{ id: 1 }], [{ id: 2 }]],
] as const)(
"keeps changed list membership across another root's write recorded (%j -> %j)",
(before, after) => {
const state = stateOf(
captureOf([
["GET", "/api/search", [...before]],
["POST", "/api/items", { id: 2 }],
["GET", "/api/search", [...after]],
]),
);
expect(state.types).toEqual([]);
expect(state.entities).toEqual([]);
expect(viewOf(state, "get_search")).toEqual({
tool: "get_search",
shape: "recorded",
reason: "fields-differ",
});
},
);
it("accepts stable membership across another root's write and changes confined before it", () => {
const state = stateOf(
captureOf([
["GET", "/api/search", []],
["GET", "/api/search", [{ id: 1 }]],
["POST", "/api/items", { id: 2 }],
["GET", "/api/search", [{ id: 1 }]],
]),
);
expect(viewOf(state, "get_search")).toMatchObject({ shape: "list" });
expect(state.entities.map((entity) => entity.id)).toEqual(["1"]);
});
it("keeps membership checks specific to the full request line", () => {
const state = stateOf(
captureOf([
["GET", "/api/search?page=1", [{ id: 1 }]],
["POST", "/api/items", { id: 2 }],
["GET", "/api/search?page=2", [{ id: 2 }]],
]),
);
expect(viewOf(state, "get_search")).toMatchObject({
shape: "list",
membership: "recorded",
});
});
it("does not treat a first observation as a membership change", () => {
const parent = captureOf([
["GET", "/api/search", [{ id: 1 }]],
["POST", "/api/items", { id: 2 }],
["GET", "/api/search", [{ id: 1 }]],
]);
const data = structuredClone(parent.normalizedResult) as {
records: { reported: { time: number } }[];
};
data.records[0]!.reported.time = 1500;
const state = stateOf({ ...parent, normalizedResult: data });
expect(viewOf(state, "get_search")).toMatchObject({ shape: "list" });
});
it.each([
[{ id: 1 }, { id: 1, title: "Created" }],
[{ id: 1, title: "Original" }, { id: 1 }],
])(
"keeps fields added or removed across another root's write recorded (%j -> %j)",
(before, after) => {
const state = stateOf(
captureOf([
["GET", "/api/search/1", before],
["POST", "/api/items", { id: 2 }],
["GET", "/api/search/1", after],
]),
);
expect(state.types).toEqual([]);
expect(state.entities).toEqual([]);
expect(viewOf(state, "get_search_by_id")).toMatchObject({
shape: "recorded",
reason: "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) =>
@@ -206,6 +206,7 @@ it("reserves request text even when another event's body was not captured", () =
normalizedResult: { ...capture, events: { ...capture.events, items } },
limitations: [],
},
"environment-manifest",
new EnvironmentManifestAccumulator(8 * 1024 * 1024),
);
expect(projected).toMatchObject({
+8 -1
View File
@@ -1168,7 +1168,7 @@ it.each(PUBLISHED_EXAMPLES)(
},
);
it("extracts an environment state identically from retained and portable MCP Evidence", async () => {
it("extracts an environment state identically from retained, portable and file MCP Evidence", async () => {
const { client, session } = await connect();
const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas(
client,
@@ -1177,6 +1177,11 @@ it("extracts an environment state identically from retained and portable MCP Evi
const capture = environmentStateCaptureFixture();
const parent = analysisViewCaptureEvidence(capture);
expect(session.recordEvidence(parent).ok).toBe(true);
const evidencePath = join(
await createTestTempDirectory("rea-state-file-mcp-"),
"capture.json",
);
await writeFile(evidencePath, JSON.stringify(parent));
const view = {
kind: "environment-state",
template_rules: ["after-uid-literal", "opaque-token", "sibling-variance"],
@@ -1185,6 +1190,7 @@ it("extracts an environment state identically from retained and portable MCP Evi
for (const source of [
{ kind: "inline", evidence: parent },
{ kind: "retained-evidence", evidence_id: parent.evidence_id },
{ kind: "evidence-file", path: evidencePath },
]) {
expect(ajv.validate(inputSchema, { source, view })).toBe(true);
const response = await client.callTool({
@@ -1211,6 +1217,7 @@ it("extracts an environment state identically from retained and portable MCP Evi
outputs.push(evidence.normalized_result);
}
expect(outputs[0]).toEqual(outputs[1]);
expect(outputs[2]).toEqual(outputs[0]);
const hidden = createEvidence(
{
path: capture.artifact.path,