feat(mcp): add opt-in paging for retained unknown heads

Keep complete default delivery and exact filters. Select stable ID pages at the ledger owner before cloning, return filtered totals and continuation offsets, and preserve detached records through the shared session port.

Validate public MCP paging, safe-integer edges, snapshot replay and converted JSON Schemas. Document the bounded Flameox workflow comparison and its token/CPU/latency measurement limits; no real model performance claim.
This commit is contained in:
morluto
2026-10-13 08:08:24 +08:00
parent 34a4bb6508
commit 9a18251426
13 changed files with 311 additions and 18 deletions
@@ -19,6 +19,10 @@ import type {
UnknownStatus,
UpdateUnknownInput,
} from "../../domain/residualUnknown.js";
import type {
UnknownPage,
UnknownQuery,
} from "../investigation/InvestigationRecordPort.js";
import { err, ok, type Result } from "../../domain/result.js";
import type {
AnalysisExecution,
@@ -350,6 +354,10 @@ export abstract class BinarySessionRecords {
return this.#records.listUnknowns(filters);
}
listUnknownsPage(query: UnknownQuery = {}): UnknownPage {
return this.#records.listUnknownsPage(query);
}
verifyUnknownResolution(unknownId: string): Result<
{
readonly valid: boolean;
@@ -26,6 +26,7 @@ import {
} from "../../domain/residualUnknown.js";
import { err, ok, type Result } from "../../domain/result.js";
import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js";
import type { UnknownPage, UnknownQuery } from "./InvestigationRecordPort.js";
type EvidenceLedgerFailure = EvidenceIntegrityError;
type RecordResult = Result<"added" | "duplicate", EvidenceLedgerFailure>;
@@ -326,18 +327,39 @@ export class EvidenceLedger {
readonly domain?: string;
} = {},
): ResidualUnknown[] {
return [...this.#unknownHeads.values()]
return this.listUnknownsPage(filters).items;
}
/** Select a stable page of current heads before detaching record payloads. */
listUnknownsPage(query: UnknownQuery = {}): UnknownPage {
const offset = query.offset ?? 0;
const matching = [...this.#unknownHeads.values()]
.filter(
(unknown) =>
(filters.status === undefined || unknown.status === filters.status) &&
(filters.severity === undefined ||
unknown.severity === filters.severity) &&
(filters.domain === undefined || unknown.domain === filters.domain),
(query.status === undefined || unknown.status === query.status) &&
(query.severity === undefined ||
unknown.severity === query.severity) &&
(query.domain === undefined || unknown.domain === query.domain),
)
.sort((left, right) =>
compareUnicodeCodePoints(left.unknown_id, right.unknown_id),
)
.map((unknown) => structuredClone(unknown));
);
const start = Math.min(offset, matching.length);
const end =
query.limit === undefined
? undefined
: start + Math.min(query.limit, matching.length - start);
const page = matching.slice(start, end);
const nextOffset =
offset <= matching.length && start + page.length < matching.length
? start + page.length
: null;
return {
items: page.map((unknown) => structuredClone(unknown)),
total: matching.length,
offset,
next_offset: nextOffset,
};
}
/** Return current resolution validity; imported invalid states are rejected. */
@@ -38,6 +38,21 @@ export interface EvidenceUnknownWriter {
): Result<ResidualUnknown | null, AnalysisError>;
}
export type UnknownPage = {
readonly items: ResidualUnknown[];
readonly total: number;
readonly offset: number;
readonly next_offset: number | null;
};
export interface UnknownQuery {
readonly status?: UnknownStatus;
readonly severity?: ResidualUnknown["severity"];
readonly domain?: string;
readonly offset?: number;
readonly limit?: number;
}
/** Optimistic Unknown revisions and consistency verification. */
export interface UnknownRegistryPort {
recordUnknown(
@@ -51,6 +66,7 @@ export interface UnknownRegistryPort {
readonly severity?: ResidualUnknown["severity"];
readonly domain?: string;
}): ResidualUnknown[];
listUnknownsPage(query?: UnknownQuery): UnknownPage;
verifyUnknownResolution(unknownId: string): Result<
{
readonly valid: boolean;
@@ -19,6 +19,8 @@ import type {
EvidenceWriter,
EvidenceUnknownWriter,
UnknownRegistryPort,
UnknownPage,
UnknownQuery,
} from "./InvestigationRecordPort.js";
/** One investigation's validated record owner, independent of binary execution. */
@@ -124,6 +126,10 @@ export class InvestigationRecords
return this.#evidence.listUnknowns(filters);
}
listUnknownsPage(query: UnknownQuery = {}): UnknownPage {
return this.#evidence.listUnknownsPage(query);
}
/** Verify registry consistency without asserting the answer is true. */
verifyUnknownResolution(unknownId: string): Result<
{
+1 -1
View File
@@ -139,7 +139,7 @@ export const SESSION_TOOL_CONTRACTS = [
),
session(
"list_unknowns",
"List every current residual-unknown head in deterministic ID order, with optional exact status, severity, and domain filters. Results are complete and inline. This is read-only; unresolved, contradicted, and non-truth dispositions remain distinct.",
"List current residual-unknown heads in deterministic ID order, with optional exact status, severity, and domain filters. Results are complete inline by default; supply offset and limit to page the filtered order. total is the matching-head count and next_offset is null when the page exhausts the result. This is read-only; unresolved, contradicted, and non-truth dispositions remain distinct.",
listUnknownsInputSchema,
),
session(
+16
View File
@@ -52,6 +52,22 @@ export const listUnknownsInputSchema = z.strictObject({
.optional(),
severity: z.enum(["low", "medium", "high", "critical"]).optional(),
domain: z.string().trim().min(1).optional(),
offset: z
.number()
.int()
.min(0)
.max(Number.MAX_SAFE_INTEGER)
.default(0)
.describe("Zero-based offset in the stable filtered unknown ID order."),
limit: z
.number()
.int()
.positive()
.max(Number.MAX_SAFE_INTEGER)
.optional()
.describe(
"Optional maximum number of heads to return. Omit to return every matching head from offset.",
),
});
/** Exact residual-unknown identity to revalidate. */
+2
View File
@@ -555,6 +555,8 @@ export const sessionOutputSchemas = {
z.object({
items: z.array(residualUnknownSchema),
total: z.number().int().min(0),
offset: z.number().int().min(0),
next_offset: z.number().int().min(0).nullable(),
}),
),
record_unknown: lifecycleResultOf(residualUnknownSchema),
+1 -1
View File
@@ -2,7 +2,7 @@ import type { JsonValue } from "../domain/jsonValue.js";
/** Exact example inputs for residual-unknown lifecycle contracts. */
export const UNKNOWN_CONTRACT_EXAMPLES = {
list_unknowns: {},
list_unknowns: { offset: 0, limit: 50 },
record_unknown: {
question: "Does this branch require an unavailable external service?",
severity: "medium",
+4 -8
View File
@@ -151,18 +151,14 @@ const registerListUnknownsTool = ({
listContract.name,
toolRegistrationOptions(listContract),
(input) => {
const all = session.listUnknowns({
const page = session.listUnknownsPage({
...(input.status === undefined ? {} : { status: input.status }),
...(input.severity === undefined ? {} : { severity: input.severity }),
...(input.domain === undefined ? {} : { domain: input.domain }),
offset: input.offset,
...(input.limit === undefined ? {} : { limit: input.limit }),
});
return server.delivery.toCallToolResult(
ok({
items: all,
total: all.length,
}),
listContract,
);
return server.delivery.toCallToolResult(ok(page), listContract);
},
);
};