feat(browser): reuse saved response values across scenario steps (#7267)

Save typed response fields and reuse generated IDs or authentication values across browser scenario requests, UI observations and navigation through the shared CLI/MCP workflow.

Preserve exact numbers, retained Evidence, explicit source/coverage limits and prospective page-value semantics. Bound response decoding, retained spellings and request serialization before allocation, and protect marked response values across source records, follow-up text and planned screenshots.

Integrates Edgar Arakelyan's #7162, including follow-ups through 9f5191ab0. Final local validation: 560 feature-branch tests and 561 current-main integration tests, static/build/docs checks and real PocketBase CLI/MCP privacy workflows. Whole-task comparison and verification limits are recorded in docs/verification/browser-saved-values-2026-10-26.md.

Co-authored-by: Edgar Arakelyan <edgar.arakelyan@scale.com>
This commit is contained in:
gensokyo-devandEdgar Arakelyan authored and GitHub committed 2026-10-27 08:15:52 +08:00
1 parent 60822e0702
commit 3fcbabe593
54 files changed
+7287 -370

No files matched your search

@@ -1,3 +1,4 @@
import { heldAmpersandSpellings } from "./environmentStateWriteCreateCalls.js";
import type { JsonValue } from "../jsonValue.js";
import { compareUnicodeCodePoints as compare } from "../unicodeCodePointOrder.js";
import {
@@ -26,11 +27,14 @@ import {
isUuid,
readClock,
sequenceNumber,
slugOf,
slugSpellings,
sortedStrings,
type ClockFormat,
} from "./environmentStateWriteValues.js";
import { createdComposer } from "./environmentStateWriteComposed.js";
import {
composedValue,
createdComposer,
} from "./environmentStateWriteComposed.js";
import { referenceRule } from "./environmentStateWriteReferences.js";
import {
fittedClockFormat,
@@ -60,6 +64,8 @@ import {
heldClock,
sameSecondClocks,
sentRule,
slugRule,
type SlugSource,
type UpdateEvidence,
} from "./environmentStateWriteChanges.js";
@@ -74,6 +80,8 @@ export interface CreateEvidence {
readonly type: readonly JsonObject[];
/** The entities of any type the state holds under an id. */
readonly named?: (id: string) => readonly StateEntity[];
/** Composed rules another call of the tool learned, which explain a field here when they spell its value. */
readonly hints?: readonly FieldRule[];
}
/** What explaining a create looks values up in, built once per request body. */
@@ -81,9 +89,24 @@ interface BodyIndex {
/** Each body string long enough to show a copy, at its shallowest pointer (breadth-first, members in order). */
readonly copies: ReadonlyMap<string, string>;
/** Each slug a top-level string member slugs to, other than its own value, from the first such member. */
readonly slugs: ReadonlyMap<string, string>;
readonly slugs: ReadonlyMap<string, SlugSource>;
}
/** Add a string's slugs to an index, keeping the first member that spells each; `own` keeps one equal to the string. */
const addSlugs = (
slugs: Map<string, SlugSource>,
member: string,
sent: string,
own = false,
): void => {
for (const { slug, ampersand } of slugSpellings(sent))
if ((own || slug !== sent) && !slugs.has(slug))
slugs.set(slug, {
from: member,
...(ampersand === undefined ? {} : { ampersand }),
});
};
/** Index a request body once, so explaining each field looks values up rather than walking the body again. */
const indexBody = (body: JsonObject): BodyIndex => {
const copies = new Map<string, string>();
@@ -109,12 +132,9 @@ const indexBody = (body: JsonObject): BodyIndex => {
pointer: `${next.pointer}${pointerTo(key)}`,
});
}
const slugs = new Map<string, string>();
const slugs = new Map<string, SlugSource>();
for (const [member, sent] of Object.entries(body))
if (typeof sent === "string") {
const slug = slugOf(sent);
if (slug !== sent && !slugs.has(slug)) slugs.set(slug, member);
}
if (typeof sent === "string") addSlugs(slugs, member, sent);
return { copies, slugs };
};
@@ -165,10 +185,8 @@ const requestRule = (
if (text !== undefined && text === pathValue)
return { field, rule: "path", from: name };
const slugged = typeof value === "string" && value.length >= 3;
const member = slugged ? index.slugs.get(value) : undefined;
return member === undefined
? undefined
: { field, rule: "slug", from: member };
const source = slugged ? index.slugs.get(value) : undefined;
return source === undefined ? undefined : slugRule(field, source);
};
/** A clock's format, trimmed when the type's values of the field show its fraction trimmed and none shows a fixed width. */
@@ -290,6 +308,36 @@ const bodyOf = (
});
/** Explain every field of a created entity: echoed, copied, path, slug, generated, template or constant (unlisted). */
/**
* A slug rule as the held entities spell `&`: as the word when the call slugged none and they spell it so, and no rule
* when they spell it both ways, or otherwise than the call did or in a way neither spelling gives. Other rules pass
* unchanged.
*/
const heldSlugRule = (
rule: FieldRule | undefined,
sent: JsonObject,
held: readonly JsonObject[],
): FieldRule | undefined => {
if (rule?.rule !== "slug") return rule;
const shown = heldAmpersandSpellings(rule, held);
const source = ownMember(sent, rule.from);
if (typeof source === "string" && source.includes("&"))
return shown.has("unknown") ||
shown.has(rule.ampersand === "and" ? "dropped" : "and")
? undefined
: rule;
if (shown.has("and") && shown.has("dropped")) return undefined;
return shown.has("and") && !shown.has("unknown")
? { ...rule, ampersand: "and" }
: rule;
};
/** Whether a composed string spells a field besides the one a template rebuilds it around, such as a title's slug beside a generated id. */
const spellsMore = (composed: FieldRule, template: FieldRule): boolean =>
composed.rule === "composed" &&
template.rule === "template" &&
composed.parts.some((part) => "from" in part && part.from !== template.from);
export const explainCreate = (
evidence: CreateEvidence,
): {
@@ -300,9 +348,15 @@ export const explainCreate = (
const ruled: FieldRule[] = [];
const unexplained: string[] = [];
const index = indexBody(evidence.call.body);
// The member each slug the held entities' `&` spellings refused was slugged from.
const refusedSlugs = new Map<string, string>();
for (const [field, value] of Object.entries(evidence.entity)) {
const requested = requestRule(evidence, index, field, value);
const held = heldSlugRule(requested, evidence.call.body, evidence.type);
if (requested?.rule === "slug" && held === undefined)
refusedSlugs.set(field, requested.from);
const rule =
requestRule(evidence, index, field, value) ??
held ??
generatedRule(evidence, field, value) ??
(evidence.named === undefined
? undefined
@@ -330,10 +384,26 @@ export const explainCreate = (
const constants: string[] = [];
const composed = createdComposer(evidence.entity, rules, evidence.type);
for (const field of unexplained) {
const template =
templateRule(evidence.entity, field, sources) ?? composed(field);
if (template === undefined) constants.push(field);
else rules.push(template);
const hinted = evidence.hints?.find(
(rule) =>
rule.field === field &&
rule.rule === "composed" &&
composedValue(rule.parts, evidence.entity) ===
ownMember(evidence.entity, field),
);
if (hinted !== undefined) {
rules.push(hinted);
continue;
}
const template = templateRule(evidence.entity, field, sources);
const spelled = composed(field);
const rule =
spelled !== undefined &&
(template === undefined || spellsMore(spelled, template))
? spelled
: template;
if (rule === undefined) constants.push(field);
else rules.push(rule);
}
const tokens = [...evidence.call.pathValues.values()].filter(
(value) => value.length > 0,
@@ -358,6 +428,11 @@ export const explainCreate = (
evidence.named ?? (() => []),
),
...instantMembers(evidence.call.body, evidence.entity, constants),
// A refused slug kept as recorded is right only for the name it was slugged from.
...constants.flatMap((field) => {
const from = refusedSlugs.get(field);
return from === undefined ? [] : [from];
}),
]);
const pinned = unreferenced(
rules,
@@ -392,12 +467,11 @@ export const explainCreate = (
const echoedSlugs = (
body: JsonObject,
echoed: ReadonlySet<string>,
): Map<string, string> => {
const slugs = new Map<string, string>();
): Map<string, SlugSource> => {
const slugs = new Map<string, SlugSource>();
for (const member of echoed) {
const sent = ownMember(body, member);
if (typeof sent === "string" && !slugs.has(slugOf(sent)))
slugs.set(slugOf(sent), member);
if (typeof sent === "string") addSlugs(slugs, member, sent, true);
}
return slugs;
};
@@ -431,6 +505,7 @@ export const explainUpdate = (
readonly unstamped: string[];
} => {
const { body } = evidence.call;
const knownEntities = [evidence.before, ...evidence.others];
const rules: FieldRule[] = [];
const echoed = new Set<string>();
const unexplained: string[] = [];
@@ -456,7 +531,15 @@ export const explainUpdate = (
continue;
}
if (!Object.hasOwn(evidence.after, member)) continue;
const rule = sentRule(evidence, member, held);
const rule = heldSlugRule(
sentRule(evidence, member, held),
body,
knownEntities,
);
if (rule === undefined) {
unexplained.push(member);
continue;
}
rules.push(rule);
if (rule.rule === "echo") echoed.add(member);
else if (rule.rule !== "constant") generated.add(member);
@@ -484,7 +567,11 @@ export const explainUpdate = (
if (kept !== undefined) rules.push(kept);
continue;
}
const rule = changeRule(evidence, field, slugs);
const rule = heldSlugRule(
changeRule(evidence, field, slugs),
body,
knownEntities,
);
if (rule === undefined) unexplained.push(field);
else rules.push(rule);
}