fix(snapshot): preserve complete investigation revision histories (#1354)

Select residual unknown histories as complete units and retain referenced registry mutation Evidence regardless of the active target at edit time. Prune incompatible dependency histories atomically instead of retaining stale dispositions.

Verify public MCP save/read/reopen for verified, withdrawn, and out-of-scope resolutions, and document target scope rules.
This commit is contained in:
You Wish
2026-10-10 06:39:29 +08:00
committed by GitHub
parent f504c2a351
commit 1c0cc8a675
4 changed files with 322 additions and 46 deletions
+130
View File
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import { createEvidence } from "./evidence.js";
import { ANALYSIS_SNAPSHOT_TARGET } from "./analysisSnapshot.fixture.js";
import {
createEvidenceBundle,
createImmutableEvidenceBundle,
@@ -10,6 +11,7 @@ import {
import {
createResidualUnknown,
recordUnknownInputSchema,
updateResidualUnknown,
} from "./residualUnknown.js";
const provider = { id: "fixture", name: "Fixture", version: "1" };
@@ -46,12 +48,140 @@ const makeUnknown = (
},
});
return {
input,
evidence: mutation,
unknown: createResidualUnknown(input, mutation.evidence_id, scopeDigest),
};
};
describe("evidenceBundleForTarget", () => {
it("retains mutation evidence from other targets for complete revision histories", () => {
const first = makeUnknown(
"Target question updated elsewhere",
targetDigest,
);
const update = createEvidence(
{ ...ANALYSIS_SNAPSHOT_TARGET, sha256: foreignDigest },
provider,
{
predicateType: "rea.residual-unknown-mutation",
operation: "update_unknown",
parameters: {
unknown_id: first.unknown.unknown_id,
expected_revision: 1,
},
result: { status: "resolved" },
},
);
const resolved = updateResidualUnknown(
first.unknown,
{
...first.input,
unknown_id: first.unknown.unknown_id,
expected_revision: 1,
status: "resolved",
resolution: {
disposition: "withdrawn",
rationale: "Withdrawn by the analyst",
evidence_ids: [],
},
},
update.evidence_id,
);
const unrelated = makeUnknown("Unrelated foreign question", foreignDigest);
const bundle = parseEvidenceBundle(
createEvidenceBundle(
[first.evidence, update, unrelated.evidence],
[first.unknown, resolved, unrelated.unknown],
),
);
const projected = evidenceBundleForTarget(bundle, targetDigest);
expect(parseEvidenceBundle(projected)).toEqual(
createEvidenceBundle([first.evidence, update], [first.unknown, resolved]),
);
expect(evidenceBundleForTarget(projected, targetDigest)).toEqual(projected);
});
it.each(["evidence", "relationship"])(
"removes whole histories and their dependents when a revision requires foreign %s",
(excluded) => {
const first = makeUnknown(
"Target history with an excluded revision",
targetDigest,
);
const foreign = makeUnknown("Excluded foreign context", foreignDigest);
const evidence = createEvidence(
{ ...ANALYSIS_SNAPSHOT_TARGET, sha256: foreignDigest },
provider,
{ operation: "foreign_observation", parameters: {}, result: true },
);
const updated = updateResidualUnknown(
first.unknown,
{
...first.input,
unknown_id: first.unknown.unknown_id,
expected_revision: 1,
status: "investigating",
supporting_evidence_ids:
excluded === "evidence" ? [evidence.evidence_id] : [],
relationships:
excluded === "relationship"
? [{ type: "related-to", unknown_id: foreign.unknown.unknown_id }]
: [],
resolution: null,
},
first.evidence.evidence_id,
);
const resolved = updateResidualUnknown(
updated,
{
...first.input,
unknown_id: first.unknown.unknown_id,
expected_revision: 2,
status: "resolved",
resolution: {
disposition: "withdrawn",
rationale: "Foreign context removed from the latest revision",
evidence_ids: [],
},
},
first.evidence.evidence_id,
);
const child = makeUnknown(
"Dependent target history",
targetDigest,
first.unknown.unknown_id,
);
const unrelated = makeUnknown("Independent target history", targetDigest);
const bundle = parseEvidenceBundle(
createEvidenceBundle(
[
first.evidence,
foreign.evidence,
evidence,
child.evidence,
unrelated.evidence,
],
[
first.unknown,
updated,
resolved,
foreign.unknown,
child.unknown,
unrelated.unknown,
],
),
);
expect(
parseEvidenceBundle(evidenceBundleForTarget(bundle, targetDigest)),
).toEqual(
createEvidenceBundle([unrelated.evidence], [unrelated.unknown]),
);
},
);
});
describe("evidenceBundleForTarget relationship pruning", () => {
it("removes dependent unknowns when their foreign-scope root is excluded", () => {
const foreignRoot = makeUnknown("Foreign root", foreignDigest);
const child = makeUnknown(
+44 -45
View File
@@ -126,7 +126,7 @@ export const createEvidenceBundle = (
};
};
/** Restrict a bundle to records and complete unknown histories for one artifact. */
/** Retain target records and eligible complete unknown histories with their mutation evidence. */
export const evidenceBundleForTarget = (
bundle: EvidenceBundle,
sha256: string,
@@ -134,57 +134,56 @@ export const evidenceBundleForTarget = (
const recordsById = new Map(
bundle.records.map((record) => [record.evidence_id, record]),
);
const candidates = bundle.unknowns.filter(
({ scope_digest: scopeDigest }) => scopeDigest === sha256,
);
const retained = new Set<ResidualUnknown>();
const revisionsById = new Map<string, number>();
const dependents = new Map<string, Set<ResidualUnknown>>();
for (const unknown of candidates) {
const mutationEvidenceIds = new Set(unknown.mutation_evidence_ids);
const evidenceAvailable = referencedEvidenceIds(unknown).every((id) => {
const record = recordsById.get(id);
return (
record?.subject?.digest.sha256 === sha256 ||
(mutationEvidenceIds.has(id) &&
record?.subject === null &&
record.predicate_type === "rea.residual-unknown-mutation")
);
const histories = new Map<string, ResidualUnknown[]>();
for (const unknown of bundle.unknowns) {
const history = histories.get(unknown.unknown_id) ?? [];
history.push(unknown);
histories.set(unknown.unknown_id, history);
}
const retained = new Set<string>();
const dependents = new Map<string, Set<string>>();
const relationships = new Map<string, Set<string>>();
for (const [id, history] of histories) {
const eligible = history.every((unknown) => {
if (unknown.scope_digest !== sha256) return false;
const mutationEvidenceIds = new Set(unknown.mutation_evidence_ids);
return referencedEvidenceIds(unknown).every((evidenceId) => {
const record = recordsById.get(evidenceId);
// Mutation subjects record the target active at the time of an edit,
// which need not be the question's immutable investigation scope.
return (
record?.subject?.digest.sha256 === sha256 ||
(mutationEvidenceIds.has(evidenceId) &&
record?.predicate_type === "rea.residual-unknown-mutation")
);
});
});
if (!evidenceAvailable) continue;
retained.add(unknown);
revisionsById.set(
unknown.unknown_id,
(revisionsById.get(unknown.unknown_id) ?? 0) + 1,
if (!eligible) continue;
retained.add(id);
const relatedIds = new Set(
history.flatMap((unknown) =>
unknown.relationships.map(({ unknown_id }) => unknown_id),
),
);
for (const relatedId of new Set(
unknown.relationships.map(({ unknown_id: id }) => id),
)) {
const references =
dependents.get(relatedId) ?? new Set<ResidualUnknown>();
references.add(unknown);
relationships.set(id, relatedIds);
for (const relatedId of relatedIds) {
const references = dependents.get(relatedId) ?? new Set<string>();
references.add(id);
dependents.set(relatedId, references);
}
}
const pending: ResidualUnknown[] = [];
for (const unknown of retained)
if (
unknown.relationships.some(({ unknown_id: id }) => !revisionsById.has(id))
)
pending.push(unknown);
const pending: string[] = [];
for (const [id, relatedIds] of relationships)
if ([...relatedIds].some((relatedId) => !retained.has(relatedId)))
pending.push(id);
for (let index = 0; index < pending.length; index += 1) {
const unknown = pending[index];
if (unknown === undefined || !retained.delete(unknown)) continue;
const remaining = (revisionsById.get(unknown.unknown_id) ?? 1) - 1;
if (remaining > 0) {
revisionsById.set(unknown.unknown_id, remaining);
continue;
}
revisionsById.delete(unknown.unknown_id);
for (const dependent of dependents.get(unknown.unknown_id) ?? [])
pending.push(dependent);
const id = pending[index];
if (id === undefined || !retained.delete(id)) continue;
for (const dependent of dependents.get(id) ?? []) pending.push(dependent);
}
const unknowns = candidates.filter((unknown) => retained.has(unknown));
const unknowns = bundle.unknowns.filter((unknown) =>
retained.has(unknown.unknown_id),
);
const mutationIds = new Set(
unknowns.flatMap(({ mutation_evidence_ids: ids }) => ids),
);