Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
a1edeaf
feat(environment): explain values an application derives from other f…
earakely-scale Oct 24, 2026
8ce1127
docs(environment): record the derived-values verification
earakely-scale Oct 24, 2026
1ec053e
chore: merge main into derived values
earakely-scale Oct 24, 2026
a80a96f
fix(environment): keep clock spellings, linked ids and word boundarie…
earakely-scale Oct 24, 2026
1e6c02c
test(environment): keep a same-width fixed clock out of trimming
earakely-scale Oct 24, 2026
8e23f18
fix(environment): check composed parts and clock fits before they sha…
earakely-scale Oct 24, 2026
5a976d7
chore: merge main into derived values
earakely-scale Oct 24, 2026
09c48c5
test(environment): split the derived-values view tests by rule
earakely-scale Oct 24, 2026
2e5f56d
fix(environment): keep fixed-width, zone and generated-integer eviden…
earakely-scale Oct 24, 2026
96783c7
chore: merge main into derived values
earakely-scale Oct 24, 2026
fb0eece
Merge remote-tracking branch 'origin/main' into feat/environment-deri…
morluto Oct 24, 2026
21b60f4
test(setup): share registration prewarm host preparation
morluto Oct 24, 2026
5f8a954
fix(environment): retain write evidence and match trimmed response cl…
morluto Oct 24, 2026
94f1753
Merge remote-tracking branch 'origin/main' into feat/environment-deri…
morluto Oct 24, 2026
3705fae
fix(environment): withhold creates whose generated integer another st…
earakely-scale Oct 24, 2026
e54b75a
fix(environment): hold later creates and compare to the composed and …
earakely-scale Oct 24, 2026
aeae47f
chore: merge main into derived values
earakely-scale Oct 24, 2026
5fd87e9
fix(environment): preserve composed parts and unique generator families
morluto Oct 24, 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
fix(environment): preserve composed parts and unique generator families
  • Loading branch information
morluto committed Oct 24, 2026
commit 5fd87e9c639f6158ef322a02da4e6530dd8c8b6d
8 changes: 6 additions & 2 deletions docs/agent-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -1056,8 +1056,6 @@ two collections can reuse a live id, and an equal pair stays in that collection.
environment answers, a path parameter by the collection the live path names (a nested collection keeps its parent values, each decoded once, and a literal route segment stays), except a query value that pages a list, such as `limit`
or `page`. Each call is checked against the served tool's input schema first, as the MCP server checks it. After mapping, two clock values compare by format only (ISO-8601 strings, and epoch numbers under
`at`, `time`, `date`, `timestamp`, `created`, `updated` or `modified` field names, including snake-case and camel-case forms).
A `composed` field compares equal when each side's response spells it from its
own fields; those fields compare under their own rules.
Numeric arrays retain that containing field's clock context through nested
arrays; an object's own fields establish their own context. Numeric values at
the body root, in root arrays, or under measurement fields remain literal.
Expand All @@ -1078,6 +1076,12 @@ Admitted generated values must also preserve aliases and distinct identities
within that response. UUID identity uses the generator's case-insensitive
matching; token and sequence strings retain their literal identity. This
correspondence ends with the answer and is not inferred across calls.
A `composed` response field must spell the value its parts build from that
response's own fields. Its parts then compare by their rendered text or their
source fields' clock and ID rules. A coherent archive URL can therefore differ
only in normalized clock digits without reporting a value difference; an
incoherent URL still differs even when both sides repeat the same stale text.
Normalized compositions count under `generated`; source fields are counted once.
The report counts each kind of normalised
value; the generation rules remain model-derived facts, rather than new
observations of the application.
Expand Down
5 changes: 5 additions & 0 deletions docs/specs/agent-environments/write-derived-values/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ model could not explain, though the application derives it deterministically:

Plans that need none of this are unchanged.

Later creates must retain the exemplar's unique-value family (UUID, integer with
the same digit count and JSON kind, or opaque token). Their recorded composed
fields must equal the values the plan rebuilds; simulation cannot replace a
contradictory captured value and then count that replacement as confirmation.

## Acceptance

- The prompt-built Linkding, Homebox and Grafana captures serve the creates and renames they withheld, and write
Expand Down
151 changes: 151 additions & 0 deletions src/application/environment/environmentCompareDiff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { JsonValue } from "../../domain/jsonValue.js";
import type { FieldRule } from "../../domain/analysisView/environmentStateWriteRules.js";
import { compareAnswers } from "./environmentCompareDiff.js";
import { IdMap, learnIds } from "./environmentCompareIds.js";
import { bookmark } from "../../../tests/fixtures/environmentStateDerivedValues.js";

const compare = (
live: unknown,
Expand Down Expand Up @@ -34,6 +35,156 @@ describe("early-year clock formats", () => {
);
});

describe("composed response values", () => {
const rules: readonly FieldRule[] = [
{
field: "date_added",
rule: "clock",
format: { kind: "iso", separator: "T", fraction: 0, zone: "Z" },
},
{
field: "date_modified",
rule: "clock",
format: { kind: "iso", separator: "T", fraction: 0, zone: "Z" },
},
{
field: "web_archive_snapshot_url",
rule: "composed",
parts: [
{ text: "https://web.archive.org/web/" },
{ from: "date_added", as: "clock", pattern: "YYYYMMDDhhmmss" },
{ text: "/" },
{ from: "url", as: "value" },
],
},
];
const live = bookmark(
1,
"https://example.test/page",
"A bookmark",
"2026-10-20T09:00:00Z",
);
const env = bookmark(
1,
"https://example.test/page",
"A bookmark",
"2026-10-17T18:00:00Z",
);

it("compares coherent archive URLs through their clock parts without counting source fields twice", () => {
const result = compare(live, env, new IdMap(), [201, 201], rules);
expect(result.differences).toEqual([]);
expect(result.normalised).toMatchObject({ clocks: 2, generated: 1 });
});

it("compares a composed ID through the source field's mapping even when the address comes first", () => {
const map = new IdMap();
map.learn(101, 1);
const result = compare(
{ url: "/folders/101", id: 101 },
{ url: "/folders/1", id: 1 },
map,
[201, 201],
[
{
field: "url",
rule: "composed",
parts: [{ text: "/folders/" }, { from: "id", as: "value" }],
},
],
);
expect(result.differences).toEqual([]);
expect(result.normalised).toMatchObject({ ids: 1, generated: 1 });
});

it.each(["live", "env", "both"] as const)(
"reports an incoherent %s composition, including equal but stale URLs",
(side) => {
const invalid = "https://web.archive.org/web/stale";
const result = compare(
side === "env" ? live : { ...live, web_archive_snapshot_url: invalid },
side === "live" ? env : { ...env, web_archive_snapshot_url: invalid },
new IdMap(),
[201, 201],
rules,
);
expect(result.differences).toMatchObject([
{ category: "value", pointer: "/web_archive_snapshot_url" },
]);
expect(result.normalised.generated).toBe(0);
},
);

it("preserves actual input differences although each address correctly spells its own response", () => {
const different = bookmark(
1,
"https://example.test/other",
"A bookmark",
"2026-10-17T18:00:00Z",
);
const result = compare(live, different, new IdMap(), [201, 201], rules);
expect(result.differences.map(({ pointer }) => pointer).sort()).toEqual([
"/url",
"/web_archive_snapshot_url",
]);
expect(result.normalised.generated).toBe(0);
});

it("does not normalize a source's unproven generated-looking business value", () => {
const result = compare(
{ url: "/folders/token00001", token: "token00001" },
{ url: "/folders/token00002", token: "token00002" },
new IdMap(),
[201, 201],
[
{
field: "url",
rule: "composed",
parts: [{ text: "/folders/" }, { from: "token", as: "value" }],
},
],
);
expect(result.differences.map(({ pointer }) => pointer).sort()).toEqual([
"/token",
"/url",
]);
});

it("compares the projected slug while retaining a difference in its source title", () => {
const result = compare(
{
title: "Hello!",
date_added: "2026-10-20T09:00:00Z",
url: "/snapshot/20261020/hello",
},
{
title: "Hello",
date_added: "2026-10-17T18:00:00Z",
url: "/snapshot/20261017/hello",
},
new IdMap(),
[201, 201],
[
rules[0]!,
{
field: "url",
rule: "composed",
parts: [
{ text: "/snapshot/" },
{ from: "date_added", as: "clock", pattern: "YYYYMMDD" },
{ text: "/" },
{ from: "title", as: "slug" },
],
},
],
);
expect(result.differences).toMatchObject([
{ category: "value", pointer: "/title" },
]);
expect(result.normalised).toMatchObject({ clocks: 1, generated: 1 });
});
});

describe("trimmed response clocks", () => {
const rules: readonly FieldRule[] = [
{
Expand Down
85 changes: 48 additions & 37 deletions src/application/environment/environmentCompareDiff.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { JsonObject } from "../../domain/analysisView/environmentStateRules.js";
import type {
ComposedPart,
FieldRule,
} from "../../domain/analysisView/environmentStateWriteRules.js";
import type { FieldRule } from "../../domain/analysisView/environmentStateWriteRules.js";
import { matchesClockFormat } from "../../domain/analysisView/environmentStateWriteClockFormats.js";
import { composedValue } from "../../domain/analysisView/environmentStateWriteComposed.js";
import {
generatedIntegerDigits,
composedPartText,
composedValue,
} from "../../domain/analysisView/environmentStateWriteComposed.js";
import {
likeGeneratedInteger,
isUuid,
readClock,
type ClockFormat,
Expand Down Expand Up @@ -114,21 +114,6 @@ interface Generation {
readonly pointer: string;
}

/** Whether both values are generated integers of one JSON kind and length, as a `unique` integer field's are. */
const sameGeneratedInteger = (
live: string | number,
env: string | number,
): boolean => {
const liveDigits = generatedIntegerDigits(live);
const envDigits = generatedIntegerDigits(env);
return (
liveDigits !== undefined &&
envDigits !== undefined &&
typeof live === typeof env &&
liveDigits.length === envDigits.length
);
};

/** Whether a confirmed generation rule applies here and both values retain its generated format. */
const generatedPair = (
live: string | number,
Expand All @@ -140,7 +125,7 @@ const generatedPair = (
if (
rule.rule === "unique" &&
pointer === "" &&
sameGeneratedInteger(live, env)
likeGeneratedInteger(live, env)
)
return true;
if (typeof live !== "string" || typeof env !== "string") return false;
Expand All @@ -162,16 +147,9 @@ const generatedPair = (
);
};

/** Whether a composed field's value is the composition its own response's fields spell. */
const spellsOwn = (
parts: readonly ComposedPart[],
response: JsonObject,
value: JsonValue | undefined,
): boolean =>
typeof value === "string" && composedValue(parts, response) === value;

class Differ {
readonly #rules: ReadonlyMap<string, FieldRule>;
readonly #composedRules: readonly Extract<FieldRule, { rule: "composed" }>[];
readonly #generatedToEnv = new Map<string, string>();
readonly #generatedToLive = new Map<string, string>();
readonly differences: Difference[] = [];
Expand All @@ -195,6 +173,9 @@ class Differ {
},
) {
this.#rules = new Map(entity.rules.map((rule) => [rule.field, rule]));
this.#composedRules = entity.rules.filter(
(rule) => rule.rule === "composed",
);
}

add(difference: Difference): void {
Expand Down Expand Up @@ -441,6 +422,10 @@ class Differ {
: key;
};
const matched = new Set<string>();
const composed =
self && this.#composedRules.length > 0
? { agreed: new Set<string>(), fields: new Map<string, string>() }
: undefined;
for (const [key, value] of Object.entries(live)) {
const child = `${pointer}/${escapePointer(key)}`;
const other = envKey(key);
Expand All @@ -453,15 +438,11 @@ class Differ {
else {
matched.add(other);
const rule = self ? this.#rules.get(key) : undefined;
if (
rule?.rule === "composed" &&
value !== env[other] &&
spellsOwn(rule.parts, live, value) &&
spellsOwn(rule.parts, env, env[other])
) {
this.normalised.generated += 1;
if (rule?.rule === "composed" && composed !== undefined) {
composed.fields.set(key, other);
continue;
}
const before = this.differences.length;
this.value(
value,
env[other]!,
Expand All @@ -480,6 +461,36 @@ class Differ {
pointer: `${generation.pointer}/${escapePointer(key)}`,
},
);
if (this.differences.length === before) composed?.agreed.add(key);
}
}
// Compare source fields once, then reuse their clock/identity decisions.
// Validate each response's spelling before normalizing its composition.
if (composed !== undefined) {
for (const rule of this.#composedRules) {
const other = composed.fields.get(rule.field);
if (other === undefined) continue;
const coherent =
composedValue(rule.parts, live) === live[rule.field] &&
composedValue(rule.parts, env) === env[other];
if (
coherent &&
rule.parts.every(
(part) =>
!("from" in part) ||
composed.agreed.has(part.from) ||
composedPartText(part, live) === composedPartText(part, env),
)
) {
composed.agreed.add(rule.field);
if (live[rule.field] !== env[other]) this.normalised.generated += 1;
} else
this.add({
category: "value",
pointer: `${pointer}/${escapePointer(rule.field)}`,
live: shown(live[rule.field]),
env: shown(env[other]),
});
}
}
for (const [key, value] of Object.entries(env))
Expand Down
Loading
Loading