Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 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
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): measure data-plane replies before building them
83fcc73 bounded one /agentenv response to 64 MiB, but it still
serialized each reply whole before measuring it, so a batch of data/get
calls on a large state built every refused reply in full only to drop
it. Each reply is now measured in bounded JSON parts (the shared
jsonParts encoder), its parts kept only while it fits; a reply past the
budget is counted for its exact reply_bytes but never assembled. A test
checks that a refused read is never handed to JSON.stringify whole and
that its reply_bytes is exact.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
earakely-scale and claude committed Oct 20, 2026
commit 57b11681c2e3b1586e75da4476e08abb30cf7352
5 changes: 3 additions & 2 deletions docs/agent-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,9 @@ download.
One `/agentenv` response carries at most 64 MiB of replies, twice the 32 MiB
a loaded document may hold: room for a `data/get` of a loaded state, while a
batch of reads cannot multiply a large state into one body. Replies are
measured in order as they are produced; one that would take the response past
the budget is answered instead with `-32000` whose `error.data` gives
measured in order as they are produced, in bounded parts and before they are
assembled, so one that would take the response past the budget is never built
whole and is answered instead with `-32000` whose `error.data` gives
`max_response_bytes` and the `reply_bytes` it would have taken. That call still
ran, so repeat such a read in another request. A reply no larger than that
error, such as a write's `{}`, is always kept.
Expand Down
5 changes: 3 additions & 2 deletions docs/specs/agent-environments/state-serve/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ this increment: write tools are not served from the state.
`-32000` with reason `cancelled` without running, and a `data/add` whose
file read finished after the cancellation does not replace the state.
- Bound one `/agentenv` response to 64 MiB of replies (twice the intake
limit). Each reply is serialized and counted as it is produced, in message
order; one that would pass the budget is replaced by `-32000` with
limit). Each reply is measured in bounded JSON parts as it is produced, in
message order, and assembled only when it fits, so a reply past the budget is
counted but never serialized whole; it is replaced by `-32000` with
`data: {max_response_bytes, reply_bytes}`, unless that error is no smaller
than the reply, so the body is never built past the budget and small write
replies are kept.
Expand Down
25 changes: 20 additions & 5 deletions src/server/environment/serveEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ import {
type StateFetcher,
} from "./stateIntake.js";
import { TargetContentTransport } from "./targetContentTransport.js";
import {
bufferedJsonParts,
jsonParts,
} from "../../domain/jsonSerialization.js";

/** Where the environment card is served. */
export const ENVIRONMENT_CARD_PATH = "/.well-known/agent-env.json";
Expand Down Expand Up @@ -186,22 +190,33 @@ const overBudget = (replyBytes: number): RpcError => ({
data: { max_response_bytes: MAX_RPC_RESPONSE_BYTES, reply_bytes: replyBytes },
});

/** Serialize a response's replies in order within its budget; one that would pass it becomes an error stating it, unless that error is no smaller. */
/**
* Serialize a response's replies in order within its budget. Each reply is measured in bounded
* parts before it is assembled, and parts past the budget are counted but not kept, so a reply
* that would pass the budget is never built; it becomes an error stating it, unless that error
* is no smaller.
*/
const replyEncoder = (): ((reply: RpcReply) => string) => {
let used = 0;
return (reply) => {
const text = JSON.stringify(reply);
const bytes = Buffer.byteLength(text, "utf8");
const parts: string[] = [];
let bytes = 0;
for (const part of bufferedJsonParts(jsonParts(reply))) {
bytes += Buffer.byteLength(part, "utf8");
if (used + bytes <= MAX_RPC_RESPONSE_BYTES) parts.push(part);
}
if (used + bytes <= MAX_RPC_RESPONSE_BYTES) {
used += bytes;
return text;
return parts.join("");
}
const refused = JSON.stringify({
jsonrpc: "2.0",
id: reply.id,
error: overBudget(bytes),
});
return Buffer.byteLength(refused, "utf8") < bytes ? refused : text;
return Buffer.byteLength(refused, "utf8") < bytes
? refused
: JSON.stringify(reply);
};
};

Expand Down
59 changes: 58 additions & 1 deletion src/server/environment/serveEnvironmentState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";

import { timedCaptureEvidence } from "../../../tests/fixtures/analysisView.js";
import {
Expand Down Expand Up @@ -566,6 +566,63 @@ describe("state data plane response budget", () => {
);
expect(loginOf(plane.store)).toBe("probe");
});

it("measures each reply before building it, so one past the budget is never serialized whole", async () => {
const document = renamedDocument();
const login = "x".repeat(Math.ceil(MAX_RPC_RESPONSE_BYTES / 3));
const large = {
rea_state: {
...document.rea_state,
entities: document.rea_state.entities.map((entity) =>
entity.type === "/api/users" && entity.id === "2"
? { ...entity, fields: { ...entity.fields, login } }
: entity,
),
},
};
const root = await createTestTempDirectory(
"rea-environment-state-measure-",
);
const path = join(root, "large.json");
await writeFile(path, JSON.stringify(large));
const plane = dataPlane();
await plane.add({ kind: "file", file: { uri: pathToFileURL(path).href } });
const reply = (id: number) => ({
jsonrpc: "2.0",
id,
result: { parts: [{ kind: "data", data: plane.store.snapshot() }] },
});
const replyBytes = Buffer.byteLength(JSON.stringify(reply(3)));
const stringify = vi.spyOn(JSON, "stringify");
try {
const batch = await plane.post(
[1, 2, 3].map((id) => ({ jsonrpc: "2.0", id, method: "data/get" })),
);
// A refused read is measured exactly, from bounded parts.
expect(batch.json).toMatchObject([
{ id: 1 },
{ id: 2 },
{
id: 3,
error: {
data: { reply_bytes: replyBytes },
},
},
]);
// No whole reply was handed to JSON.stringify by the server.
expect(
stringify.mock.calls.some(
([value]) =>
typeof value === "object" &&
value !== null &&
"result" in value &&
(value as { id?: unknown }).id === 3,
),
).toBe(false);
} finally {
stringify.mockRestore();
}
});
});

const served: ServedEnvironment[] = [];
Expand Down
Loading