fix(mcp): retain complete application results across response limits
Retain complete authenticated application Evidence when the repeated MCP representation exceeds the default receive frame or Node string limit. Return a typed transport constraint, keep useful followup queries available, and stream full canonical bundles to atomic destinations. Verify complete Notion and Obsidian workflows on Linux and native Windows, full CLI/Evidence parity, and real large Chrome screenshot recovery. Preserve complete success contracts and caller-selected evidence paths. Refs #1065, #1053.
This commit is contained in:
@@ -116,6 +116,36 @@ describe("evidence ledger recording", () => {
|
||||
ledger.clear();
|
||||
expect(ledger.export().records).toEqual([]);
|
||||
});
|
||||
|
||||
it("protects borrowed snapshots while preserving detached mutable public reads", () => {
|
||||
const evidence = createEvidence(TARGET, PROVIDER, {
|
||||
operation: "health",
|
||||
parameters: {},
|
||||
result: { nested: { value: "original" } },
|
||||
});
|
||||
const ledger = new EvidenceLedger();
|
||||
expect(ledger.record(evidence).ok).toBe(true);
|
||||
const borrowed = ledger.forAnalysis(evidence.evidence_id);
|
||||
if (borrowed === undefined) throw new Error("Missing recorded Evidence");
|
||||
expect(Object.isFrozen(borrowed)).toBe(true);
|
||||
expect(Object.isFrozen(borrowed.normalized_result)).toBe(true);
|
||||
expect(Reflect.set(borrowed, "operation", "forged")).toBe(false);
|
||||
expect(ledger.forAnalysis(evidence.evidence_id)).toBe(borrowed);
|
||||
expect(ledger.record(borrowed)).toEqual({ ok: true, value: "duplicate" });
|
||||
const detached = ledger.get(evidence.evidence_id);
|
||||
if (detached === undefined) throw new Error("Missing detached Evidence");
|
||||
expect(Reflect.set(detached, "operation", "forged")).toBe(true);
|
||||
expect(ledger.record(detached).ok).toBe(false);
|
||||
expect(ledger.forAnalysis(evidence.evidence_id)).toBe(borrowed);
|
||||
const snapshot = ledger.forSerialization();
|
||||
expect(snapshot).toEqual(ledger.export());
|
||||
expect(snapshot.records[0]).toBe(borrowed);
|
||||
expect(Object.isFrozen(snapshot.records)).toBe(true);
|
||||
ledger.clear();
|
||||
expect(ledger.forAnalysis(evidence.evidence_id)).toBeUndefined();
|
||||
expect(ledger.forSerialization().records).toEqual([]);
|
||||
expect(snapshot.records).toEqual([evidence]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("evidence ledger metadata imports", () => {
|
||||
|
||||
@@ -2,6 +2,7 @@ import canonicalize from "canonicalize";
|
||||
|
||||
import {
|
||||
createEvidenceBundle,
|
||||
createImmutableEvidenceBundle,
|
||||
describeEvidenceBundleFailure,
|
||||
parseEvidenceBundle,
|
||||
validateResidualUnknownAddition,
|
||||
@@ -9,7 +10,11 @@ import {
|
||||
} from "../../domain/evidenceBundle.js";
|
||||
import { EvidenceIntegrityError } from "../../domain/evidenceErrors.js";
|
||||
import { UnknownRegistryError } from "../../domain/unknownRegistryError.js";
|
||||
import { parseEvidence, type Evidence } from "../../domain/evidence.js";
|
||||
import {
|
||||
immutableEvidence,
|
||||
parseEvidence,
|
||||
type Evidence,
|
||||
} from "../../domain/evidence.js";
|
||||
import {
|
||||
createResidualUnknown,
|
||||
updateResidualUnknown,
|
||||
@@ -42,7 +47,7 @@ export class EvidenceLedger {
|
||||
record(input: Evidence): RecordResult {
|
||||
let evidence: Evidence;
|
||||
try {
|
||||
evidence = parseEvidence(input);
|
||||
evidence = immutableEvidence(input);
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new EvidenceIntegrityError("Evidence record validation failed", {
|
||||
@@ -52,6 +57,10 @@ export class EvidenceLedger {
|
||||
}
|
||||
const existing = this.#records.get(evidence.evidence_id);
|
||||
if (existing !== undefined) {
|
||||
// A borrowed record is already authenticated and deeply immutable. Its
|
||||
// identity cannot change between reads, so comparing document-sized
|
||||
// canonical strings again would add no integrity check.
|
||||
if (existing === evidence) return ok("duplicate");
|
||||
return recordsAgree(existing, evidence)
|
||||
? ok("duplicate")
|
||||
: err(new EvidenceIntegrityError("Conflicting evidence record"));
|
||||
@@ -97,8 +106,9 @@ export class EvidenceLedger {
|
||||
additions.set(evidence.evidence_id, evidence);
|
||||
}
|
||||
for (const [id, evidence] of replacements)
|
||||
this.#records.set(id, evidence);
|
||||
for (const [id, evidence] of additions) this.#records.set(id, evidence);
|
||||
this.#records.set(id, immutableEvidence(evidence));
|
||||
for (const [id, evidence] of additions)
|
||||
this.#records.set(id, immutableEvidence(evidence));
|
||||
return ok({
|
||||
recordsAdded: additions.size,
|
||||
unknownsAdded: 0,
|
||||
@@ -165,6 +175,14 @@ export class EvidenceLedger {
|
||||
);
|
||||
}
|
||||
|
||||
/** Borrow a sealed canonical bundle for serialization without copying its records. */
|
||||
forSerialization(): EvidenceBundle {
|
||||
return createImmutableEvidenceBundle(
|
||||
[...this.#records.values()],
|
||||
[...this.#unknownRevisions.values()],
|
||||
);
|
||||
}
|
||||
|
||||
/** Check whether one immutable Evidence ID is present in this ledger. */
|
||||
has(evidenceId: string): boolean {
|
||||
return this.#records.has(evidenceId);
|
||||
@@ -176,6 +194,11 @@ export class EvidenceLedger {
|
||||
return evidence === undefined ? undefined : structuredClone(evidence);
|
||||
}
|
||||
|
||||
/** Borrow authenticated immutable Evidence for read-only analysis without a full clone. */
|
||||
forAnalysis(evidenceId: string): Evidence | undefined {
|
||||
return this.#records.get(evidenceId);
|
||||
}
|
||||
|
||||
/** Create one approved unknown and atomically append its mutation evidence. */
|
||||
recordUnknown(
|
||||
input: RecordUnknownInput,
|
||||
@@ -380,7 +403,8 @@ export class EvidenceLedger {
|
||||
} catch (cause: unknown) {
|
||||
return err(new UnknownRegistryError("integrity", { cause }));
|
||||
}
|
||||
for (const [id, evidence] of additions) this.#records.set(id, evidence);
|
||||
for (const [id, evidence] of additions)
|
||||
this.#records.set(id, immutableEvidence(evidence));
|
||||
if (unknown !== undefined && unknownKey !== undefined) {
|
||||
this.#unknownRevisions.set(unknownKey, unknown);
|
||||
this.#unknownHeads.set(unknown.unknown_id, unknown);
|
||||
@@ -407,7 +431,8 @@ export class EvidenceLedger {
|
||||
unknowns: ReadonlyMap<string, ResidualUnknown>,
|
||||
): void {
|
||||
this.#records.clear();
|
||||
for (const [id, evidence] of records) this.#records.set(id, evidence);
|
||||
for (const [id, evidence] of records)
|
||||
this.#records.set(id, immutableEvidence(evidence));
|
||||
this.#unknownRevisions.clear();
|
||||
this.#unknownHeads.clear();
|
||||
for (const [key, unknown] of unknowns) {
|
||||
|
||||
@@ -15,6 +15,10 @@ import type { Result } from "../../domain/result.js";
|
||||
export interface EvidenceReader {
|
||||
hasEvidence(evidenceId: string): boolean;
|
||||
evidenceById(evidenceId: string): Evidence | undefined;
|
||||
/** Optional immutable analysis read; older adapters can keep detached reads. */
|
||||
evidenceForAnalysis?(evidenceId: string): Evidence | undefined;
|
||||
/** Optional immutable bundle snapshot for complete serialization without graph copies. */
|
||||
evidenceBundleForSerialization?(): EvidenceBundle;
|
||||
exportEvidenceBundle(): EvidenceBundle;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,11 +51,21 @@ export class InvestigationRecords
|
||||
return this.#evidence.get(evidenceId);
|
||||
}
|
||||
|
||||
/** Borrow an immutable authenticated record for a read-only application workflow. */
|
||||
evidenceForAnalysis(evidenceId: string): Evidence | undefined {
|
||||
return this.#evidence.forAnalysis(evidenceId);
|
||||
}
|
||||
|
||||
/** Export a detached deterministic bundle of Evidence and Unknown revisions. */
|
||||
exportEvidenceBundle(): EvidenceBundle {
|
||||
return this.#evidence.export();
|
||||
}
|
||||
|
||||
/** Borrow a sealed bundle snapshot for complete JSON serialization. */
|
||||
evidenceBundleForSerialization(): EvidenceBundle {
|
||||
return this.#evidence.forSerialization();
|
||||
}
|
||||
|
||||
/** Atomically merge a bundle and report changes for the owner's post-commit observers. */
|
||||
mergeEvidenceBundle(
|
||||
bundle: unknown,
|
||||
|
||||
Reference in New Issue
Block a user