Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
95c8c9d
feat(environment): render more read tools from the state and describe…
cursoragent Oct 21, 2026
ad8f674
feat(environment): apply evidence-gated write effects to the served s…
cursoragent Oct 21, 2026
733833e
feat(environment): model bodiless writes, omitted fields, projected r…
earakely-scale Oct 21, 2026
6f67a11
feat(environment): test and document wider write coverage
earakely-scale Oct 21, 2026
e44adb5
docs(environment): record the write-coverage checks on the current wr…
earakely-scale Oct 21, 2026
a4c4a23
docs(environment): record the re-recorded and end-to-end write-covera…
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
067f0fa
fix(environment): keep projected and partial reads out of the write g…
earakely-scale Oct 21, 2026
717ac2c
chore: merge main (8413ec08) into environment state writes
earakely-scale Oct 21, 2026
1074355
chore: merge environment state writes (717ac2c4) into environment wri…
earakely-scale Oct 22, 2026
0061234
chore: merge main (2451e187) into environment write coverage
earakely-scale Oct 22, 2026
70249b3
fix(environment): check the absence a projected read answers, and rea…
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
9517260
chore: merge environment state writes (2a125b71) into environment wri…
earakely-scale Oct 22, 2026
11e3f01
refactor(environment): keep the observing view helpers with the obser…
earakely-scale Oct 22, 2026
cfc43a7
chore: merge environment state writes (11e3f016) into environment wri…
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
54f7d85
fix(environment): note omitted fields only from reads the gate checks
earakely-scale Oct 22, 2026
3ac1136
chore: merge environment state writes (90d90227) into environment wri…
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
caf2acf
chore: merge environment state writes (3f1f825e) into environment wri…
earakely-scale Oct 22, 2026
613048a
chore: merge main (b8c1b453) into environment write coverage
earakely-scale Oct 22, 2026
e21f6c5
chore: merge main (538017d1, #5600 squashed) into environment write c…
earakely-scale Oct 22, 2026
06ec0d4
fix(merge): preserve write coverage with query-aware validation
morluto Oct 22, 2026
f5162bb
fix(environment): align write inference with resolved state facts
morluto Oct 22, 2026
17c5a44
chore: integrate write coverage with current main
gensokyo-dev Oct 22, 2026
8b6e35b
chore: sync write coverage with latest main
N0zoM1z0 Oct 22, 2026
6f726ef
fix(environment): preserve projection and omission evidence across wr…
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): bound additional collection history
  • Loading branch information
morluto committed Oct 21, 2026
commit f79f1e1833f604aef157dd920519b1577ed921c4
17 changes: 17 additions & 0 deletions docs/agent-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,23 @@ names its category in `error.data.reason`: `not-found`, `permission`,
downloads, and `cancelled` for a download the server's stop or its caller's
departure ended.

Served creates also bound additional collection history since the last reset
or `data/add` to 1 MiB of deterministic accounting: UTF-8 bytes for each new
collection spelling and its possible sequence keys, plus a 64-byte allowance
per collection and potential sequence-key record. The score bounds identifier
payload and a fixed record allowance; it is not a measurement or guarantee of
process memory. Collections
already present in the Evidence or loaded state are part of the state itself;
the store indexes their unique spellings so they remain known after deletion,
but they do not consume this additional-history budget. A create that would
exceed the budget is refused before the state changes. `data/reset` or `data/add`
clears generated history; if the same calls would hit the limit again, use
fewer distinct collection paths. The 1 MiB score ceiling is 1/32 of the
separate 32 MiB state-intake ceiling, reserving a small, explicit allowance
for identifiers retained beyond the loaded state. A loaded-state path that is
later deleted and then introduced again may consume the additional-history
budget when the store must retain it beyond the source entity.

Stopping the server cancels every pending call, and a caller that leaves before
its reply cancels its own: a download in progress ends, and calls not yet run
are answered `-32000` with `error.data.reason` `cancelled` without running. A
Expand Down
16 changes: 16 additions & 0 deletions docs/specs/agent-environments/state-writes/verification.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,19 @@ unmodelled write followed, checking only the others on their members. Both tests
verdicts over the 50 captures are unchanged plan by plan, the smoke applies all 31 served write tools, and the gates
passed again. A follow-up weighs interference only on targets where the tool's effect survives, so a call
whose effect a later write overwrote no longer excuses a later mismatch; its test fails on the previous head.

## Collection-history accounting check

On 2026-10-22, Node 24.18.0, I compared the source before the write-history
change (`cfb42ddde`) with the bounded-history implementation. Each operation
inserted an entity at a new collection path with a 128-character repeated
suffix and immediately deleted it. Across five fresh Vitest worker processes,
the median for 1,000 completed cycles was 14.57 ms wall / 23,867 us CPU before
and 9.98 ms / 16,914 us after. The process RSS samples were 381.1–387.9 MiB
before and 380.9–387.6 MiB after. This fixture benchmark measures one matched
completed trace, not a representative user task or process-family peak memory.
The values include Vitest and V8 and are noisy; they do not establish a memory
bound. The 1 MiB policy is a deterministic accounting allowance for
additional identifier history, set to 1/32 of the separate 32 MiB state-intake
ceiling; it is not derived from RSS samples and does not bound total store or
process memory.
110 changes: 73 additions & 37 deletions src/application/environment/environmentState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,49 +155,85 @@ describe("loadEnvironmentState", () => {
});

describe("served collection history", () => {
it("refuses a new collection before retained collection and sequence history exceeds its budget", () => {
const store = new EnvironmentStateStore(state);
it("bounds distinct paths added after reset while retaining baseline paths", () => {
const original = state.entities[0];
if (original === undefined) throw new Error("Expected a seeded entity");
const run = () => {
const store = new EnvironmentStateStore(state);
for (let index = 0; index < 2000; index += 1) {
const collection = `/api/history/${String(index)}-${"x".repeat(900)}`;
const id = `history-${String(index)}`;
const before = JSON.stringify(store.snapshot());
const result = store.insert({ ...original, collection, id }, 0);
if (!result.ok) {
expect(JSON.stringify(store.snapshot())).toBe(before);
expect(result.error).toMatchObject({
resource: "capacity",
retryable: false,
remediationAction: expect.stringContaining("fewer distinct"),
});
expect(result.error.reason).toContain("accounting budget");
return result.error.reportedLimits;
}
const inserted = store.entity(original.type, collection, id);
if (inserted === undefined) throw new Error("Expected inserted entity");
store.remove(inserted);
}
throw new Error("Expected a history budget refusal");
};
const refused = run();
if (refused === null)
throw new Error("Expected reported history budget details");
expect(refused).toEqual(run());
expect(refused).toMatchObject({
max_collection_history_bytes: ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES,
});
expect(refused.collection_history_bytes).toBeGreaterThan(
ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES,
);
});

it("does not charge a deleted loaded collection again at the history limit", () => {
const original = state.entities[0];
if (original === undefined) throw new Error("Expected a seeded entity");
let refused: ReturnType<typeof store.insert> | undefined;
const loadedState = {
...state,
entities: Array.from({ length: 1200 }, (_, index) => ({
...original,
collection: `/api/loaded/${String(index)}-${"y".repeat(80)}`,
id: `loaded-${String(index)}`,
})),
};
const store = new EnvironmentStateStore(loadedState);
for (const entity of store.ofType(original.type)) store.remove(entity);
expect(store.knownCollectionsOf(original.type)).toHaveLength(1200);

const baseline = loadedState.entities[0];
if (baseline === undefined) throw new Error("Expected a loaded entity");
let refused = false;
for (let index = 0; index < 2000; index += 1) {
const before = JSON.stringify(store.snapshot());
const result = store.insert(
{
...original,
collection: `/api/history/${String(index)}-${"x".repeat(900)}`,
id: `history-${String(index)}`,
},
0,
);
if (!result.ok) {
refused = result;
expect(JSON.stringify(store.snapshot())).toBe(before);
const collection = `/api/generated/${String(index)}-${"z".repeat(900)}`;
const entity = {
...original,
collection,
id: `generated-${String(index)}`,
};
const inserted = store.insert(entity, 0);
if (!inserted.ok) {
refused = true;
break;
}
const inserted = store.entity(
original.type,
`/api/history/${String(index)}-${"x".repeat(900)}`,
`history-${String(index)}`,
);
if (inserted === undefined) throw new Error("Expected inserted entity");
store.remove(inserted);
const held = store.entity(entity.type, collection, entity.id);
if (held === undefined) throw new Error("Expected generated entity");
store.remove(held);
}
expect(refused?.ok).toBe(false);
if (refused?.ok !== false)
throw new Error("Expected a history budget refusal");
expect(refused.error).toMatchObject({
resource: "capacity",
retryable: false,
reportedLimits: {
collection_history_bytes: expect.any(Number),
max_collection_history_bytes:
ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES,
},
remediationAction: expect.stringContaining("Reset the served state"),
});
expect(refused.error.reason).toContain("retained metadata budget");
expect(store.knownCollectionsOf(original.type).length).toBeLessThan(2000);
expect(refused).toBe(true);

const restored = store.insert(baseline, 0);
expect(restored.ok).toBe(true);
expect(
store.entity(baseline.type, baseline.collection, baseline.id),
).toEqual(baseline);
});
});

Expand Down
99 changes: 51 additions & 48 deletions src/application/environment/environmentStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,38 @@ import {
type StateContent,
} from "./environmentStateDocument.js";

/** Bound estimated collection and sequence-key metadata retained after served creates are deleted. */
/** Maximum logical budget for collection spellings added by served creates since reset/load. */
export const ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES = 1024 * 1024;

const collectionHistoryCost = (
state: EnvironmentState,
type: string,
collection: string,
): number => {
const sequenceKeys = new Set<string>();
// Account key bytes without building temporary key strings or a Set.
let sequenceBytes = 0;
for (const plan of state.writes.values()) {
if (!plan.served || plan.target !== type) continue;
for (const rule of plan.rules) {
if (rule.rule === "sequence" && rule.scope === "collection")
sequenceKeys.add(`${plan.tool} ${rule.field} ${collection}`);
sequenceBytes +=
64 +
Buffer.byteLength(plan.tool, "utf8") +
1 +
Buffer.byteLength(rule.field, "utf8") +
1 +
Buffer.byteLength(collection, "utf8");
else if (rule.rule === "identity" && rule.generator === "sequence")
sequenceKeys.add(`${type} ${rule.field} ${collection}`);
sequenceBytes +=
64 +
Buffer.byteLength(type, "utf8") +
1 +
Buffer.byteLength(rule.field, "utf8") +
1 +
Buffer.byteLength(collection, "utf8");
}
}
return (
64 +
Buffer.byteLength(collection, "utf8") +
[...sequenceKeys].reduce(
(bytes, key) => bytes + 64 + Buffer.byteLength(key, "utf8"),
0,
)
);
return 64 + Buffer.byteLength(collection, "utf8") + sequenceBytes;
};

/** What `data/get` reports: the current state as a `rea-state/v1` document. */
Expand Down Expand Up @@ -117,9 +123,9 @@ export class EnvironmentStateStore {
readonly #sequences = new Map<string, number>();
/** The entities the state held when it was last reset or loaded, whose values no sequence reuses either. */
#started: readonly StateEntity[] = [];
/** Every collection spelling a type's entities have been held under since the last reset or load, kept when a collection empties. */
readonly #knownCollections = new Map<string, string[]>();
/** Estimated retained collection and sequence-key metadata, reset with the serving history. */
/** Baseline and post-start spellings kept known after their entities are deleted. */
readonly #knownCollections = new Map<string, Set<string>>();
/** Logical UTF-8 key bytes plus a 64-byte allowance per collection and potential sequence-key record. */
#collectionHistoryBytes = 0;
/** The collections of the Evidence's own state, which stay known after any load. */
#evidenceCollections: ReadonlyMap<string, readonly string[]> = new Map();
Expand Down Expand Up @@ -204,27 +210,20 @@ export class EnvironmentStateStore {
this.#writes = 0;
this.#sequences.clear();
this.#started = this.#content.entities;
// A reset or load knows the Evidence's spellings and its own, never those of a state it replaced.
this.#knownCollections.clear();
this.#collectionHistoryBytes = 0;
this.#remember(this.#evidenceCollections);
this.#remember(this.#typeCollections);
for (const [type, collections] of this.#typeCollections)
for (const collection of collections)
this.#rememberCollection(type, collection);
for (const [type, collections] of this.#evidenceCollections)
for (const collection of collections)
this.#rememberCollection(type, collection);
}

#remember(named: ReadonlyMap<string, readonly string[]>): void {
for (const [type, collections] of named) {
const known = this.#knownCollections.get(type) ?? [];
for (const collection of collections)
if (!known.includes(collection)) {
known.push(collection);
this.#collectionHistoryBytes += collectionHistoryCost(
this.state,
type,
collection,
);
}
this.#knownCollections.set(type, known);
}
#rememberCollection(type: string, collection: string): void {
const known = this.#knownCollections.get(type) ?? new Set<string>();
known.add(collection);
this.#knownCollections.set(type, known);
}

/** The entities of a type the state held when it was last reset or loaded. */
Expand All @@ -247,17 +246,19 @@ export class EnvironmentStateStore {
jsonBytes(entity) + (entities.length > 0 ? 1 : 0),
);
if (!bytes.ok) return bytes;
const known = this.#knownCollections.get(entity.type) ?? [];
if (!known.includes(entity.collection)) {
const historyBytes =
this.#collectionHistoryBytes +
collectionHistoryCost(this.state, entity.type, entity.collection);
const known = this.#knownCollections.get(entity.type);
const newCollectionBytes =
known?.has(entity.collection) === true
? 0
: collectionHistoryCost(this.state, entity.type, entity.collection);
if (newCollectionBytes > 0) {
const historyBytes = this.#collectionHistoryBytes + newCollectionBytes;
if (historyBytes > ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES)
return err(
new AnalysisResourceConstraintError(
ENVIRONMENT_SERVE_OPERATION,
"capacity",
"Served collection and sequence history would exceed its retained metadata budget",
"Served collection history would exceed its deterministic accounting budget",
{
max_collection_history_bytes:
ENVIRONMENT_COLLECTION_HISTORY_LIMIT_BYTES,
Expand All @@ -266,7 +267,7 @@ export class EnvironmentStateStore {
{
retryable: false,
remediationAction:
"Reset the served state to clear collection history, then use fewer distinct collection paths.",
"Reset the served state or load a smaller state to clear generated collection history, then use fewer distinct collection paths.",
},
),
);
Expand All @@ -289,6 +290,10 @@ export class EnvironmentStateStore {
},
bytes.value,
);
if (newCollectionBytes > 0) {
this.#rememberCollection(entity.type, entity.collection);
this.#collectionHistoryBytes += newCollectionBytes;
}
return ok(undefined);
}

Expand Down Expand Up @@ -388,14 +393,13 @@ export class EnvironmentStateStore {
return this.#typeCollections.get(type) ?? [];
}

/** Every collection spelling a type's entities have been held under since the last reset or load, or the Evidence's state holds: those holding entities first. */
/** Every collection spelling in the active, baseline, Evidence, or generated history, with active entities first. */
knownCollectionsOf(type: string): readonly string[] {
return [
...new Set([
...this.collectionsOf(type),
...(this.#knownCollections.get(type) ?? []),
]),
];
const known = new Set([
...this.collectionsOf(type),
...(this.#knownCollections.get(type) ?? []),
]);
return [...known];
}

document(tool: string): HeldDocument | undefined {
Expand Down Expand Up @@ -424,7 +428,7 @@ export class EnvironmentStateStore {
);
}

/** Build every index before assigning any, so a reader never sees half a state. */
/** Build current-state indexes without copying baseline collections into generated history. */
#install(content: StateContent, bytes: number): void {
const entities = new Map<string, StateEntity>();
const collections = new Map<string, StateEntity[]>();
Expand All @@ -449,7 +453,6 @@ export class EnvironmentStateStore {
this.#entities = entities;
this.#collections = collections;
this.#typeCollections = typeCollections;
this.#remember(typeCollections);
this.#documents = new Map(
content.documents.map((document) => [document.tool, document]),
);
Expand Down