Files
reverse-engineering-anything/src/domain/analysisView/environmentStateVariants.ts
T
977c9727e6 feat(environment): give more write tools a state target (#6385)
* fix(environment): keep read-only POSTs out of the write list

A POST that repeats the latest read of its path, tunnels only read
operations, names a lookup in its path's last meaningful word, or renews
a session no longer counts as a state write: it is replayed, does not cut
initial windows, and does not end observation searches. A tool stays a
write when a related read line changes across one of its calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): take an entity an unplanned write made when a read first shows it

A successful item read of an entity the gate's copy never held, after a
write no built plan models, puts the entity into the copy instead of
failing the read, so later writes to it are checked as usual. An entity a
modelled write touched is never taken back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): plan a create into the type its returned id is read under

A POST whose path names no state type becomes a create into the type
whose item read first names an id its response returned, when the id is
new to the state; serving creates in that read's collection. A successful
POST that returned an id a type's reads only show after it also ends that
type's initial window, so entities a capture created stay out of the
initial state.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): end a type's initial reads at the write its reads disagree across

When a type's reads disagree across a write under another root, the
type's initial window now ends at the earliest write within that change
and the state is extracted again, instead of leaving every view of the
type replayed. The cut composes with the creation cut; the earlier wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): replay request lines that show entities in another form

A read line that shows the same entities in another form, by an extra
query parameter that adds or drops members, or conflicts with a base
line on a shared entity, is a variant: it leaves the view's checks and
the entity observations, the view names it with the query keys that pick
it out, and a call matching those keys replays its recording. A filter
role whose name matches no field needs two agreeing values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): specify and record the state-target changes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): keep a read-only POST a write when its evidence is incomplete

Body-level container operations are skipped like query ones, a lookup is named only
by the path's last literal segment and never alongside a write word, a tool with a
redirected or unanswered call is not reclassified, and the mutation veto is resolved
over all candidates at once so another read-only call cannot explain a read change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): answer a field selection only when the state shows it exactly

Serving tests a field selection before any variant key: a projection value a replayed
read gave, or one no checked line gave other than `*`, replays. A list sets aside a
line asking for some fields only before the identity proof, and a dropped variant's
membership change no longer rewrites the type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): hold adopted entities and linked creates to what the recording shows

An adopted entity is charged against the view's budget like a created one. A linked
create holds only for its exemplar's path arguments: serving refuses others and the
gate counts such a call as unmodelled. A response's identity-named id links at any
length, but a short one only to a type read before the call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the review fixes' measurements

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): release a discarded planning pass before planning again

A rewrite cut clears the first pass, so the budget rewinds to a checkpoint taken after
the write cutoffs and created ids before the state is extracted again. Rewrite spans are
charged and appended without spreading them into an argument list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): link no create whose id an earlier read showed, and read only whole batches

A linked create is rejected when an earlier list of its type or read of its collection
showed the returned id. Body ids are walked without spreading children into an argument
list. An RPC call reads only when every operations entry names an operation of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): let the gate skip exactly the reads serving replays

Serving's replay decision moves to the domain as replayReason, and the write gate uses it:
a read serving replays can neither approve nor veto a write, and a read of every field
beside a replayed selection is checked. A selection read missing a required field is
still skipped, since part of an entity can neither confirm nor check it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the second review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): read writes, links and creation cuts only from evidence that rules out other causes

- A read-only POST repeats only a read that finished before it began; the veto compares
  the latest read finished before a call with the first begun after it, and counts a
  recorded absence on one side as a change. `modify` and `replace` are write words.
- A redirected or unanswered write lets a later item read adopt the entity it may have made.
- A creation cut counts a read still running when the POST began as after it, and a list
  read shows its listed entities' own ids, not those they nest.
- A linked create's item read must come before any other tool's write.
- Line shapes are charged to the budget as they are built.
- A `fields=*` list line is whole, not a field selection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the third review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): tie linked creates to their returned ids and order state by the lines left

- A linked create is observed only in a read of an id its response returned,
  envelopes included, and serving takes its recorded path values in either spelling.
- A list read shows a suffix-named identity (`projectId`) distinct across its entities.
- A read-only POST compares the read that finished last before it began.
- An entity's first sighting comes from the lines left after variant lines are dropped.
- The pairwise variant pass stops at a work limit, leaving every line checked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the fourth review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): keep foreign keys, stale reads and double decoding out of write evidence

- A list read counts a suffix-named field as its entities' ids only when no
  id-named field identifies them, so an `ownerId` beside `id` cuts no window.
- A read-only POST is compared with the read that finished last among those no
  other write follows, so a consuming call stays a write.
- A linked create's source path values compare as spelled on both sides, so no
  spelling is decoded twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the fifth review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): compare linked sources by position, decode path fields once and bound read and variant scans

- A linked create's source values compare segment by segment, so a template
  naming two parameters alike cannot accept a changed first one.
- Serving fills a linked create's path fields with its arguments decoded once,
  as the gate learned them.
- The read-only check reads each related line's records once per call, between
  the nearest other writes.
- Member signatures are JSON arrays, so a name holding the separator still
  conflicts; the variant work limit counts line pairs as well as entities.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the sixth review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): replay late and variant-only items, fill linked path fields from the exemplar and charge membership spans

- An item line first recorded after the initial window that names a new shaping
  parameter and shows other members is a replayed variant, like an initial one.
- An item only a replayed line showed, which the state never started with,
  answers with its own recording; one the state held and lost stays missing.
- A linked create fills its path fields from its exemplar's path decoded once,
  whichever accepted spelling the call sends.
- Each membership change span is charged against the budget before it is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the seventh review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): decide late item lines against the lines left, charge replay keys and bisect rewrite cuts

- Item lines first recorded after the initial reads are decided once variants
  are dropped, against the lines left: a new shaping parameter showing other
  members, or a selection of some fields leaving members out, replays.
- Each replay key is charged against the budget before it is kept.
- A rewrite span's earliest write is found by bisection over the writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the eighth review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): count only reads finished before a linked create began as showing its id held

A list still running when the call began may show what the call made, as the
creation cut already decides, so it no longer refuses the link.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): keep served creates' items missing, check same-type earlier reads, and bound late lines and read-only stretches

- An item only a replayed line showed replays only while no served create has
  added to its type, so a created and deleted item stays missing.
- A linked create's earlier item-read veto checks only reads of the candidate's
  type or collection that show the item, so another type's id does not hide it.
- An item read answered with JSON null shows no id to a creation cut.
- Late lines' member names are charged before they are kept.
- The read-only check precomputes nearest writes and passes once over each
  write-free stretch per path, skipping lines whose reads all agree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the ninth review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(environment): link creates past empty item reads, sweep repeated reads once and keep only returned ids for creation cuts

- A linked create passes over a later item read naming its id whose body shows
  no item, such as JSON null, and links the next one.
- Read-only repeated reads are found in one sweep: each path's reads wait by
  end time until a call's start passes them, and a write clears them.
- A creation cut keeps, and charges, only the shown ids some creation returned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the tenth review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(environment): compare variant lines through shared entities and parse each compared body once

The variant pass indexes a tool's lines by the entities they show and compares only lines that share one, charging
the index to the budget while it is held; the pairwise work limit still decides when a tool keeps every line checked.
Read-only decisions keep each response body's canonical digest, taken once while its parse is charged, across calls
and veto passes. The creation cut no longer parses reads at or after the write that ends the initial reads, and reads
the ids a body shows inside its lease. Adoption looks up a read's target only after an unplanned write before it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the eleventh review round's checks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(environment): list a type's collections only when the path's own holds no id, and parse variant base lines once

The write gate's item target lists the type's held collections, and the removed targets', only when the path's own
collection holds none of its ids, so a served item view's reads no longer walk every entity of the type. The variant
pass parses the base lines' queries once for all the variants they key, and not at all when it found none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(environment): record the twelfth review round's checks and the sweep's cost

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
2026-10-24 15:58:32 +08:00

536 lines
19 KiB
TypeScript

import { compareUnicodeCodePoints as compare } from "../unicodeCodePointOrder.js";
import { ENTRY_BYTES, textBytes } from "./webNetworkRouteTemplates.js";
import type { LateLine, StatePlan } from "./environmentStateGate.js";
import type { ObservedEntity } from "./environmentStateMerge.js";
import {
isProjectionParameter,
projectsFields,
type ReplayKey,
type StateItemView,
type StateListView,
type StateRecordFacts,
} from "./environmentStateRules.js";
/** Query names that page a list rather than choose how it shows its entities. */
const PAGING = new Set([
"limit",
"offset",
"page",
"per_page",
"perpage",
"page_size",
"pagesize",
"size",
"start",
"_start",
"_end",
"cursor",
"pagenumber",
"pageindex",
"skip",
"take",
"top",
"$top",
"$skip",
]);
/** What one recorded line's objects showed: each entity's member names, and the members any and every object carried. */
export interface LineShape {
readonly entities: ReadonlyMap<string, string>;
readonly carried: ReadonlySet<string>;
readonly common: ReadonlySet<string> | undefined;
}
/** Each name a query names, with every value it took. */
export const queryValues = (
query: string | null | undefined,
): Map<string, string[]> => {
const values = new Map<string, string[]>();
for (const [name, value] of new URLSearchParams(query ?? ""))
values.set(name, [...(values.get(name) ?? []), value]);
return values;
};
/** Whether a query names a parameter that may change how a line shows its entities, paging and field selections aside. */
export const shapesOtherwise = (query: string | null | undefined): boolean =>
shapingNames(query).some((name) => !isProjectionParameter(name));
/** The query names that may change how a line shows its entities: all but paging. */
const shapingNames = (query: string | null | undefined): string[] =>
[...queryValues(query).keys()].filter(
(name) => !PAGING.has(name.toLowerCase()),
);
/** The shape each observed recording showed of its entities, by ordinal, each part counted against the budget before it is kept. */
export const lineShapes = (
entities: Iterable<ObservedEntity>,
retain: (bytes: number) => void,
): Map<number, LineShape> => {
const shapes = new Map<
number,
{
entities: Map<string, string>;
carried: Set<string>;
common: Set<string> | undefined;
}
>();
retain(ENTRY_BYTES);
for (const entity of entities)
for (const observation of entity.observations) {
const members = Object.keys(observation.object);
let shape = shapes.get(observation.ordinal);
if (shape === undefined) {
// The shape's own entry and containers, with room for the members every object carries.
retain(ENTRY_BYTES * (4 + members.length));
shape = { entities: new Map(), carried: new Set(), common: undefined };
shapes.set(observation.ordinal, shape);
}
const key = JSON.stringify([entity.type, entity.collection, entity.id]);
// A JSON array, since a member name may hold any separator.
const signature = JSON.stringify([...members].sort(compare));
if (!shape.entities.has(key))
retain(ENTRY_BYTES + textBytes(key.length + signature.length));
shape.entities.set(key, signature);
for (const member of members)
if (!shape.carried.has(member)) {
retain(ENTRY_BYTES);
shape.carried.add(member);
}
shape.common =
shape.common === undefined
? new Set(members)
: new Set(members.filter((member) => shape!.common!.has(member)));
}
return shapes;
};
const EMPTY: LineShape = {
entities: new Map(),
carried: new Set(),
common: undefined,
};
/** Line pairs and entity lookups the variant pass may make; a plan whose lines need more keeps every line checked. */
const VARIANT_WORK_LIMIT = 1 << 24;
/** For each entity some lines show, those lines by the members they show it with. */
type LinesByEntity = ReadonlyMap<
string,
ReadonlyMap<string, readonly StateRecordFacts[]>
>;
/** The lines by the entities they show, an entry for each entity each line shows charged before it is kept. */
const linesByEntity = (
lines: readonly StateRecordFacts[],
shape: (record: StateRecordFacts) => LineShape,
retain: (bytes: number) => void,
): LinesByEntity => {
const shown = new Map<string, Map<string, StateRecordFacts[]>>();
for (const line of lines)
for (const [key, members] of shape(line).entities) {
retain(ENTRY_BYTES);
const showing = shown.get(key) ?? new Map<string, StateRecordFacts[]>();
shown.set(key, showing);
const group = showing.get(members);
if (group === undefined) showing.set(members, [line]);
else group.push(line);
}
return shown;
};
/** The lines that show one of a line's entities with other members. */
const conflictingLines = (
mine: LineShape,
shown: LinesByEntity,
): Set<StateRecordFacts> => {
const conflicting = new Set<StateRecordFacts>();
for (const [key, members] of mine.entities)
for (const [others, lines] of shown.get(key)!)
if (others !== members) for (const line of lines) conflicting.add(line);
return conflicting;
};
/** What lines compared later are compared with: the shaping parameter names they named, the members their objects carried, and those every one carried. */
interface LineTotals {
readonly names: Set<string>;
readonly carried: Set<string>;
common: Set<string> | undefined;
}
/** Totals over no lines yet. */
const noLines = (): LineTotals => ({
names: new Set(),
carried: new Set(),
common: undefined,
});
/** Count one more line into the totals. */
const addLine = (
totals: LineTotals,
query: string | null | undefined,
shape: Pick<LineShape, "carried" | "common">,
): void => {
for (const name of shapingNames(query)) totals.names.add(name);
for (const member of shape.carried) totals.carried.add(member);
const { common } = shape;
if (common !== undefined)
totals.common =
totals.common === undefined
? new Set(common)
: new Set([...totals.common].filter((member) => common.has(member)));
};
/** Whether a query names a shaping parameter, paging aside, that none of the counted lines named. */
const namesNewParameter = (
query: string | null | undefined,
totals: LineTotals,
): boolean => shapingNames(query).some((name) => !totals.names.has(name));
/** Whether a line's objects carry a member the counted lines' never did, or lack one theirs always carried. */
const showsOtherMembers = (
shape: Pick<LineShape, "carried" | "common">,
totals: LineTotals,
): boolean => {
const { common } = shape;
return (
[...shape.carried].some((member) => !totals.carried.has(member)) ||
(common !== undefined &&
totals.common !== undefined &&
[...totals.common].some((member) => !common.has(member)))
);
};
/** The base lines so far, with their totals. */
interface BaseLines extends LineTotals {
readonly lines: Set<StateRecordFacts>;
}
/**
* The latest lines that show the plan's entities in another representation than its base lines, as
* an expanding or reducing query does; none when comparing every pair of lines would exceed the work limit.
*/
export const variantLines = (
latest: readonly StateRecordFacts[],
shapeOf: (record: StateRecordFacts) => LineShape | undefined,
retain: (bytes: number) => void = () => undefined,
): StateRecordFacts[] => {
if (latest.length < 2) return [];
const shapes = new Map(
latest.map((record) => [record, shapeOf(record) ?? EMPTY]),
);
const entities = [...shapes.values()].reduce(
(sum, line) => sum + line.entities.size,
0,
);
// Comparing every pair of lines entity by entity bounds comparing only the lines that share an entity.
if (latest.length * (latest.length + entities) > VARIANT_WORK_LIMIT)
return [];
const shape = (record: StateRecordFacts): LineShape => shapes.get(record)!;
const shown = linesByEntity(latest, shape, retain);
const ranks = new Map(
latest.map((record) => {
const names = shapingNames(record.query).length;
const agreeing =
latest.length - 1 - conflictingLines(shape(record), shown).size;
return [
record,
[
names === 0 ? 0 : 1,
projectsFields(record.query ?? undefined) ? 1 : 0,
-agreeing,
names,
shape(record).carried.size,
],
];
}),
);
// Base lines first: no shaping query, then no projection, then the line most others agree with.
const ordered = [...latest].sort((left, right) => {
const [a, b] = [ranks.get(left)!, ranks.get(right)!];
const differs = a.findIndex((value, index) => value !== b[index]);
return differs === -1 ? 0 : a[differs]! - b[differs]!;
});
const base: BaseLines = { ...noLines(), lines: new Set() };
const variants: StateRecordFacts[] = [];
for (const record of ordered) {
if (base.lines.size > 0 && isVariant(record, base, shape(record), shown))
variants.push(record);
else {
base.lines.add(record);
addLine(base, record.query, shape(record));
}
}
return variants;
};
/** Whether a line shows a base line's entity with other members, or names a new shaping parameter while its objects carry or lack members the base lines do not. */
const isVariant = (
record: StateRecordFacts,
base: BaseLines,
mine: LineShape,
shown: LinesByEntity,
): boolean =>
[...conflictingLines(mine, shown)].some((line) => base.lines.has(line)) ||
(namesNewParameter(record.query, base) && showsOtherMembers(mine, base));
/** What replay keys are found against: every value each base line gave a parameter, and the parameters every base line names. */
interface KeyBase {
readonly values: ReadonlyMap<string, ReadonlySet<string>>;
readonly everywhere: readonly string[];
}
/** Parse the base lines' queries once, for any number of replayed lines. */
const keyBase = (base: readonly (string | null | undefined)[]): KeyBase => {
const baseValues = base.map(queryValues);
const values = new Map<string, Set<string>>();
for (const line of baseValues)
for (const [name, given] of line) {
const seen = values.get(name) ?? new Set<string>();
values.set(name, seen);
for (const value of given) seen.add(value);
}
const everywhere = [...values.keys()].filter(
(name) =>
!PAGING.has(name.toLowerCase()) &&
baseValues.every((line) => line.has(name)),
);
return { values, everywhere };
};
/** The keys that tell replayed lines from base lines: a parameter or value only replayed lines give, or a parameter every base line names that one leaves out. */
export const replayKeys = (
base: readonly (string | null | undefined)[],
replayed: readonly (string | null | undefined)[],
retain: (bytes: number) => void = () => undefined,
): ReplayKey[] => keysAgainst(keyBase(base), replayed, retain);
/** The replay keys of replayed lines against base lines parsed once. */
const keysAgainst = (
{ values: baseValues, everywhere }: KeyBase,
replayed: readonly (string | null | undefined)[],
retain: (bytes: number) => void,
): ReplayKey[] => {
const keys = new Map<string, ReplayKey>();
const add = (key: ReplayKey): void => {
const id = JSON.stringify([
key.name,
key.value ?? null,
key.absent ?? false,
]);
if (keys.has(id)) return;
// Each key is kept with its text, so it is charged before it is.
retain(ENTRY_BYTES * 2 + textBytes(id.length));
keys.set(id, key);
};
for (const query of replayed) {
const values = queryValues(query);
for (const [name, given] of values) {
if (PAGING.has(name.toLowerCase())) continue;
const seen = baseValues.get(name);
const unseen = given.filter((value) => seen?.has(value) !== true);
if (seen === undefined) add({ name });
else for (const value of unseen) add({ name, value });
}
for (const name of everywhere)
if (!values.has(name)) add({ name, absent: true });
}
return [...keys.values()].sort(
(left, right) =>
compare(left.name, right.name) ||
compare(left.value ?? "", right.value ?? "") ||
Number(left.absent ?? false) - Number(right.absent ?? false),
);
};
/** Whether a call's query names a replay key: the parameter, the parameter with that value, or leaves out an absent key's parameter. */
const matchesReplayKey = (
keys: readonly ReplayKey[],
call: ReadonlyMap<string, readonly string[]>,
): boolean =>
keys.some((key) =>
key.absent === true
? !call.has(key.name)
: key.value === undefined
? call.has(key.name)
: call.get(key.name)?.includes(key.value) === true,
);
/** Whether a call asks for a selection of fields the checked lines do not show exactly: a projection value a replayed line gave, or one no checked line gave other than `*`. */
const selectsOtherFields = (
call: ReadonlyMap<string, readonly string[]>,
checked: readonly (string | null | undefined)[],
replayed: readonly (string | null | undefined)[],
): boolean => {
const [base, other] = [checked.map(queryValues), replayed.map(queryValues)];
const gave = (lines: Map<string, string[]>[], name: string, value: string) =>
lines.some((values) => values.get(name)?.includes(value) === true);
return [...call].some(
([name, values]) =>
isProjectionParameter(name) &&
values.some(
(value) =>
gave(other, name, value) ||
(value !== "*" && !gave(base, name, value)),
),
);
};
/** Why serving replays a read call rather than answering it from the state (`projection` for some fields only, `variant` for another representation); the gate skips the same reads. */
export const replayReason = (
view: Pick<
StateItemView | StateListView,
| "checked_record_ordinals"
| "projected_record_ordinals"
| "projected_query_keys"
>,
call: ReadonlyMap<string, readonly string[]>,
queryOf: (ordinal: number) => string | null | undefined,
): "projection" | "variant" | undefined => {
if (view.projected_record_ordinals === undefined) return undefined;
// A field selection is tested on its own: a variant key cannot name every value the checked lines do not show.
if (
[...call.keys()].some(isProjectionParameter) &&
selectsOtherFields(
call,
view.checked_record_ordinals.map(queryOf),
view.projected_record_ordinals.map(queryOf),
)
)
return "projection";
const matched = (view.projected_query_keys ?? []).filter(
(key) =>
(key.absent === true || !isProjectionParameter(key.name)) &&
matchesReplayKey([key], call),
);
if (matched.length === 0) return undefined;
return matched.some((key) => isProjectionParameter(key.name))
? "projection"
: "variant";
};
const requestLine = (record: StateRecordFacts): string =>
record.path + (record.query ?? "");
/** The found lines a replay key tells from the other latest lines, which are parsed once for all of them. */
const keyedVariants = (
latest: readonly StateRecordFacts[],
found: readonly StateRecordFacts[],
): StateRecordFacts[] => {
if (found.length === 0) return [];
const variant = new Set(found);
const base = keyBase(
latest.filter((record) => !variant.has(record)).map((line) => line.query),
);
return found.filter(
(record) => keysAgainst(base, [record.query], () => undefined).length > 0,
);
};
/** The plan with the variants a key picks out moved to its replayed reads and their recordings dropped, or undefined when it replays nothing. */
export const withoutVariants = (
plan: StatePlan,
facts: {
readonly shapeOf: (record: StateRecordFacts) => LineShape | undefined;
readonly queryOf: (ordinal: number) => string | null | undefined;
readonly position: (ordinal: number) => number;
readonly retain: (bytes: number) => void;
readonly checkpoint: () => () => void;
},
):
| { readonly plan: StatePlan; readonly dropped: ReadonlySet<number> }
| undefined => {
// The lines indexed by entity are dropped once compared, so what they were charged is released.
const release = facts.checkpoint();
const found = variantLines(plan.latest, facts.shapeOf, facts.retain);
release();
const variants = keyedVariants(plan.latest, found);
const lines = new Set(variants.map(requestLine));
const latest = plan.latest.filter(
(record) => !lines.has(requestLine(record)),
);
const projected = [
...(plan.projected ?? []),
...lateReplays(plan.late ?? [], latest, facts.shapeOf),
];
if (variants.length === 0 && projected.length === 0) return undefined;
const replayed = [
...projected,
...variants.map((record) => record.ordinal),
].sort((left, right) => facts.position(left) - facts.position(right));
const dropped = new Set(
plan.recordings
.filter((record) => lines.has(requestLine(record)))
.map((record) => record.ordinal),
);
return {
plan: {
...plan,
recordings: plan.recordings.filter(
(record) => !dropped.has(record.ordinal),
),
latest,
projected: replayed,
projectedKeys: replayKeys(
latest.map((record) => record.query),
replayed.map(facts.queryOf),
facts.retain,
),
},
dropped,
};
};
/**
* The later item lines serving replays, decided against the lines left: a selection of some fields
* that leaves out members they always show, or a line naming a shaping parameter none of them names
* while its object carries or lacks members theirs do not.
*/
const lateReplays = (
late: readonly LateLine[],
base: readonly StateRecordFacts[],
shapeOf: (record: StateRecordFacts) => LineShape | undefined,
): number[] => {
if (late.length === 0) return [];
const totals = noLines();
for (const record of base)
addLine(totals, record.query, shapeOf(record) ?? EMPTY);
return late.flatMap(({ record, members, projecting }) =>
(projecting ||
shapingNames(record.query).some(
(name) => !isProjectionParameter(name) && !totals.names.has(name),
)) &&
showsOtherMembers({ carried: members, common: members }, totals)
? [record.ordinal]
: [],
);
};
/** Forget the observations of dropped recordings, and entities only they showed; an entity is first sighted where its kept observations show it first. */
export const dropObservations = (
entities: Map<string, ObservedEntity>,
dropped: ReadonlySet<number>,
): void => {
for (const [key, entity] of entities) {
const kept = entity.observations.filter(
(observation) => !dropped.has(observation.ordinal),
);
if (kept.length === entity.observations.length) continue;
if (kept.length === 0) {
entities.delete(key);
continue;
}
const first = kept.reduce((earliest, observation) =>
observation.position < earliest.position ||
(observation.position === earliest.position &&
observation.index < earliest.index)
? observation
: earliest,
);
entities.set(key, {
...entity,
order: [first.position, first.index],
observations: kept,
});
}
};