Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
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
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
11e3f01
refactor(environment): keep the observing view helpers with the obser…
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
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
4bc6002
fix(environment): start the served clock from the records whose times…
earakely-scale 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): credit sent fields, negative sequences, and encoded…
… targets

A later overwrite copied surviving derived fields into the sent-member
claim, so a members-only check rejected an update whose sent field still
matched. Serving floored sequences at zero and stored missing history as
zero, so a collection sequence learned from negative ranks skipped the
next value. An absence after a delete forgot the resolved collection, and
an encoded read could not observe an update spelled with another
collection encoding.

Regression tests cover each case.

Co-authored-by: morluto <morluto@users.noreply.github.com>
  • Loading branch information
cursoragent and morluto committed Oct 21, 2026
commit 7de20bc9eaf08eca6540d6c2c929266e0ad84948
17 changes: 12 additions & 5 deletions src/application/environment/environmentStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
StateEntity,
StateType,
} from "../../domain/analysisView/environmentStateRules.js";
import { nextSequence } from "../../domain/analysisView/environmentStateWriteValues.js";
import {
AnalysisInputError,
AnalysisResourceConstraintError,
Expand Down Expand Up @@ -191,13 +192,19 @@ export class EnvironmentStateStore {
/** Count an applied write and keep the sequence values it took, so a refused call takes none. */
commitWrite(sequences: ReadonlyMap<string, number>): void {
this.#writes += 1;
for (const [key, value] of sequences)
this.#sequences.set(key, Math.max(value, this.#sequences.get(key) ?? 0));
for (const [key, value] of sequences) {
const taken = this.#sequences.get(key);
// No earlier value leaves this one in place, including when it is negative.
this.#sequences.set(
key,
taken === undefined ? value : Math.max(taken, value),
);
}
}

/** The next value of a sequence: one past both the largest held value and the largest an applied write took. */
sequenceAfter(key: string, held: number): number {
return Math.max(held, (this.#sequences.get(key) ?? 0) + 1);
/** The next value of a sequence: the next held integer, else one past the later of that and the last value taken. */
sequenceAfter(key: string, held: number | undefined): number {
return nextSequence(held, this.#sequences.get(key));
}

#restart(): void {
Expand Down
36 changes: 36 additions & 0 deletions src/application/environment/environmentStateWriteAnswer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,24 @@ describe("refusals", () => {
});
});

/** Items whose `rank` is a collection sequence, with a recorded create at `recorded`. */
const rankedItems = (initial: readonly [number, number], recorded: number) => {
const item = (id: number, rank: number) => ({
id,
name: `n${String(id)}`,
rank,
});
const [first, second] = initial;
return servedFrom([
["GET", "/api/items", [item(1, first), item(2, second)]],
["GET", "/api/items/1", item(1, first)],
["GET", "/api/items/2", item(2, second)],
["POST", "/api/items", item(3, recorded), 201, { name: "n3" }],
["GET", "/api/items/3", item(3, recorded)],
["GET", "/api/items", [item(1, first), item(2, second), item(3, recorded)]],
]);
};

describe("generated values", () => {
it("answers the same calls identically after a reset, never reusing a deleted id", () => {
const books = servedFrom();
Expand All @@ -362,6 +380,24 @@ describe("generated values", () => {
});
});

it("serves the next rank when a collection sequence runs through negative values", () => {
const acrossZero = rankedItems([-2, -1], 0);
expect(
acrossZero.answer("post_items", { body: { name: "n4" } }).body,
).toMatchObject({ id: 3, rank: 0 });
expect(
acrossZero.answer("post_items", { body: { name: "n5" } }).body,
).toMatchObject({ id: 4, rank: 1 });
// The value after a stored negative rank stays on that sequence.
const negative = rankedItems([-4, -3], -2);
expect(
negative.answer("post_items", { body: { name: "n4" } }).body,
).toMatchObject({ id: 3, rank: -2 });
expect(
negative.answer("post_items", { body: { name: "n5" } }).body,
).toMatchObject({ id: 4, rank: -1 });
});

it("restarts the clock and the sequences when data/add loads a state", () => {
const books = servedFrom();
const initial: unknown = JSON.parse(books.snapshot());
Expand Down
25 changes: 13 additions & 12 deletions src/application/environment/environmentStateWriteAnswer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
writtenResponse,
type GenerateValue,
} from "../../domain/analysisView/environmentStateWriteApply.js";
import { insertIndex } from "../../domain/analysisView/environmentStateWriteExplain.js";
import {
insertIndex,
nextInteger,
} from "../../domain/analysisView/environmentStateWriteExplain.js";
import {
pathValuesOf,
type GeneratedRule,
Expand All @@ -17,7 +20,6 @@ import {
} from "../../domain/analysisView/environmentStateWriteRules.js";
import {
formatClock,
integerOf,
isUuid,
seededToken,
seededUuid,
Expand Down Expand Up @@ -126,13 +128,12 @@ const observedEntity = (
return isObject(object) ? object : undefined;
};

/** The largest integer the entities hold in a field, plus one. */
const nextInteger = (entities: readonly StateEntity[], field: string): number =>
entities.reduce(
(largest, entity) =>
Math.max(largest, integerOf(entity.fields[field]) ?? 0),
0,
) + 1;
/** One past the largest integer these entities hold in the field, or undefined when none holds one. */
const nextHeld = (entities: readonly StateEntity[], field: string) =>
nextInteger(
entities.map((entity) => entity.fields),
field,
);

/**
* Values one served write generates: its clock instant, sequences one past the largest held
Expand All @@ -154,7 +155,7 @@ const generator = (
const { store } = serving;
const write = store.pendingWrite();
const taken = new Map<string, number>();
const sequence = (key: string, held: number): number => {
const sequence = (key: string, held: number | undefined): number => {
const value = store.sequenceAfter(key, held);
taken.set(key, value);
return value;
Expand Down Expand Up @@ -191,7 +192,7 @@ const generator = (
exemplar,
sequence(
`${request.tool.name} ${rule.field} ${rule.scope === "collection" ? target.name : ""}`,
nextInteger(
nextHeld(
rule.scope === "collection"
? [...target.collection, ...startedIn]
: [...ofType, ...started],
Expand All @@ -211,7 +212,7 @@ const generator = (
exemplar,
sequence(
`${request.plan.target ?? ""} ${rule.field} ${target.name}`,
nextInteger([...target.collection, ...startedIn], rule.field),
nextHeld([...target.collection, ...startedIn], rule.field),
),
),
);
Expand Down
85 changes: 85 additions & 0 deletions src/domain/analysisView/EnvironmentStateWriteSimulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,60 @@ describe("reads named as serving names them", () => {
});
});

it("confirms a delete by a plain absence of an entity held under the encoded collection", () => {
const alpha = orgBook(1, "acme1", "Alpha");
const beta = orgBook(2, "beta2", "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],
["DELETE", "/api/orgs/acme1/books/1", "", 204],
["GET", "/api/orgs/acme1/books/1", BOOK_NOT_FOUND, 404],
["DELETE", "/api/orgs/beta2/books/2", "", 204],
["GET", "/api/orgs/beta2/books/2", BOOK_NOT_FOUND, 404],
];
const { writes } = environmentStateViewOf(timedCaptureEvidence(rows), [
"sibling-variance",
]);
expect(writes.find((plan) => plan.effect === "delete")).toMatchObject({
served: true,
checked_record_ordinals: expect.arrayContaining([5]),
});
});

it("learns a plain-path update from a later read under the encoded collection", () => {
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/acme1/books/1", "", 204, { name: "Renamed" }],
["GET", "/api/orgs/%61cme1/books/1", renamed],
["GET", "/api/orgs/%61cme1/books/1", renamed],
[
"PATCH",
"/api/orgs/beta2/books/2",
renamedBeta,
200,
{ name: "Renamed Beta" },
],
["GET", "/api/orgs/beta2/books/2", renamedBeta],
];
const { writes } = environmentStateViewOf(timedCaptureEvidence(rows), [
"sibling-variance",
]);
expect(writes.find((plan) => plan.effect === "update")).toMatchObject({
served: true,
observation: { ordinal: 5 },
checked_record_ordinals: expect.arrayContaining([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]],
Expand Down Expand Up @@ -870,6 +924,37 @@ describe("what a check leaves pending", () => {
});
});

it("checks a surviving sent member after a derived field was bumped by an unmodelled write", () => {
const entry = (
name: string,
description: string,
version: number,
): Book => ({ id: 1, name, description, version });
const other = { id: 2, name: "B", description: "y", version: 1 };
const rows: TimedCaptureRow[] = [
["GET", "/api/books", [entry("A", "x", 1), other]],
["GET", "/api/books/1", entry("A", "x", 1)],
["GET", "/api/books/2", other],
[
"PATCH",
"/api/books/1",
entry("A2", "new", 2),
200,
{ name: "A2", description: "new" },
],
["PUT", "/api/books/1", entry("A3", "new", 2), 200, { name: "A3" }],
["POST", "/api/books/1/archive", { archived: true }, 204, {}],
// The read still shows the description the patch sent. The version bump is not that patch.
["GET", "/api/books/1", entry("A3", "new", 9)],
];
expect(gated(rows, "patch_books_by_id")).toMatchObject({
served: true,
rules: expect.arrayContaining([{ field: "version", rule: "increment" }]),
checked_record_ordinals: [6],
contradicted_by: null,
});
});

it("neither resynchronises nor checks an entity by a read begun while a write to it still ran", () => {
const rows: TimedCaptureRow[] = [
["GET", "/api/books", [FIRST_BOOK, SECOND_BOOK]],
Expand Down
13 changes: 12 additions & 1 deletion src/domain/analysisView/environmentStateWriteAttribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,18 @@ export class WriteEffects {
const left = tool === at.writer ? claim : remaining(claim, write);
if (left !== undefined) {
own!.set(tool, left);
if (tool !== at.writer) touched.get(at.entity)?.set(tool, left);
if (tool !== at.writer) {
const tools = touched.get(at.entity);
const sent = tools?.get(tool);
// touched keeps sent members. A members check compares those; a surviving derived field stays on the effect.
if (tools !== undefined && sent !== undefined)
tools.set(
tool,
sent !== null && left !== null
? sent.filter((field) => left.includes(field))
: left,
);
}
} else {
own!.delete(tool);
touched.get(at.entity)?.delete(tool);
Comment thread
morluto marked this conversation as resolved.
Resolved
Expand Down
95 changes: 76 additions & 19 deletions src/domain/analysisView/environmentStateWriteChecks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,19 @@ export const collectionTarget = (type: string, collection: string) =>
`c${compositeKey([type, collection])}`;
export const documentTarget = (tool: string) => `d${compositeKey([tool])}`;

/** A target `remove` dropped, so a later absence can still name the collection and id serving resolved. */
interface RemovedTarget {
readonly type: string;
readonly collection: string;
readonly id: string;
}

/** The gate's copy of the state: entities in collection order and documents, changed by modelled writes. */
export class SimulatedState {
readonly #entities = new Map<string, StateEntity>();
readonly #collections = new Map<string, string[]>();
readonly #documents = new Map<string, JsonValue>();
readonly #removed: RemovedTarget[] = [];

constructor(
entities: readonly StateEntity[],
Expand Down Expand Up @@ -203,6 +211,27 @@ export class SimulatedState {
collection,
order.filter((member) => member !== key),
);
const index = this.#removed.findIndex(
(target) =>
target.type === entity.type &&
target.collection === entity.collection &&
target.id === entity.id,
);
if (index >= 0) this.#removed.splice(index, 1);
this.#removed.push({
type: entity.type,
collection: entity.collection,
id: entity.id,
});
}

/** Removed targets of this type the state does not hold again. */
removedTargets(type: string): readonly RemovedTarget[] {
return this.#removed.filter(
(target) =>
target.type === type &&
!this.#entities.has(entityTarget(type, target.collection, target.id)),
);
}

document(tool: string): JsonValue | undefined {
Expand Down Expand Up @@ -264,9 +293,34 @@ const segmentMatch = (
return verbatim;
};

/** A held target an item path names, preferring the path's own collection, then a verbatim spelling. */
const matchingTarget = (
path: {
readonly read: readonly string[];
readonly ids: readonly string[];
readonly written: string;
},
collections: readonly string[],
held: (collection: string, id: string) => boolean,
): { readonly collection: string; readonly id: string } | undefined => {
for (const id of path.ids)
if (held(path.written, id)) return { collection: path.written, id };
const ranked = [...new Set(collections)]
.flatMap((collection) => {
const verbatim = segmentMatch(segmentsOf(collection), path.read);
return verbatim === undefined ? [] : [{ collection, verbatim }];
})
.sort((left, right) => right.verbatim - left.verbatim);
for (const { collection } of ranked)
for (const id of path.ids)
if (held(collection, id)) return { collection, id };
return undefined;
};

/**
* The entity an item read or write names, as serving names it: in a held collection its path matches, verbatim
* spellings first, the id percent-decoded once, then as written; else the collection and decoded id as written.
* spellings first, the id percent-decoded once, then as written; else a removed target still named that way, so
* an absence keeps the collection and id a delete resolved; else the collection and decoded id as written.
*/
export const itemTarget = (
state: SimulatedState,
Expand All @@ -275,24 +329,27 @@ export const itemTarget = (
): { readonly collection: string; readonly id: string } => {
const read = segmentsOf(record.path);
const raw = read.pop() ?? "";
const ids = [...new Set([decodedSegment(raw), raw])];
const written = `/${read.join("/")}`;
for (const id of ids)
if (state.entity(type, written, id) !== undefined)
return { collection: written, id };
const collections = [
...new Set(state.ofType(type).map((entity) => entity.collection)),
]
.flatMap((collection) => {
const verbatim = segmentMatch(segmentsOf(collection), read);
return verbatim === undefined ? [] : [{ collection, verbatim }];
})
.sort((left, right) => right.verbatim - left.verbatim);
for (const { collection } of collections)
for (const id of ids)
if (state.entity(type, collection, id) !== undefined)
return { collection, id };
return { collection: written, id: ids[0]! };
const path = {
read,
ids: [...new Set([decodedSegment(raw), raw])],
written: `/${read.join("/")}`,
};
const removed = state.removedTargets(type);
return (
matchingTarget(
path,
state.ofType(type).map((entity) => entity.collection),
(collection, id) => state.entity(type, collection, id) !== undefined,
) ??
matchingTarget(
path,
removed.map((target) => target.collection),
(collection, id) =>
removed.some(
(target) => target.collection === collection && target.id === id,
),
) ?? { collection: path.written, id: path.ids[0]! }
);
};

export const checkItemRead = (
Expand Down
Loading
Loading