feat(cli): add unknown command registry support
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
import {
|
||||
createResidualUnknown,
|
||||
updateResidualUnknown,
|
||||
residualUnknownSchema,
|
||||
type RecordUnknownInput,
|
||||
type ResidualUnknown,
|
||||
type UnknownStatus,
|
||||
type UpdateUnknownInput,
|
||||
} from "../domain/residualUnknown.js";
|
||||
import { UnknownRegistryError } from "../domain/errors.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
export interface UnknownRegistryLimits {
|
||||
readonly maxRecords: number;
|
||||
readonly maxRelationships: number;
|
||||
}
|
||||
|
||||
type UnknownRegistryMutation = Result<ResidualUnknown, UnknownRegistryError>;
|
||||
|
||||
type UnknownRegistryQuery = Result<
|
||||
readonly ResidualUnknown[],
|
||||
UnknownRegistryError
|
||||
>;
|
||||
|
||||
type UnknownRegistryVerify = Result<
|
||||
{
|
||||
readonly valid: boolean;
|
||||
readonly truthVerified: boolean;
|
||||
readonly unknown: ResidualUnknown;
|
||||
},
|
||||
UnknownRegistryError
|
||||
>;
|
||||
|
||||
/**
|
||||
* Persistent, bounded registry of residual unknowns linked to evidence.
|
||||
*
|
||||
* Each unknown carries a stable ID derived from its question and scope,
|
||||
* an immutable revision graph, and evidence-qualified resolution state.
|
||||
*/
|
||||
export class UnknownRegistry {
|
||||
readonly #revisions = new Map<string, ResidualUnknown>();
|
||||
readonly #heads = new Map<string, ResidualUnknown>();
|
||||
|
||||
constructor(private readonly limits: UnknownRegistryLimits) {
|
||||
if (!Number.isSafeInteger(limits.maxRecords) || limits.maxRecords < 1)
|
||||
throw new RangeError("maxRecords must be a positive safe integer");
|
||||
if (
|
||||
!Number.isSafeInteger(limits.maxRelationships) ||
|
||||
limits.maxRelationships < 0
|
||||
)
|
||||
throw new RangeError(
|
||||
"maxRelationships must be a non-negative safe integer",
|
||||
);
|
||||
}
|
||||
|
||||
recordUnknown(
|
||||
input: RecordUnknownInput,
|
||||
mutationEvidenceId: string,
|
||||
): UnknownRegistryMutation {
|
||||
let unknown: ResidualUnknown;
|
||||
try {
|
||||
unknown = createResidualUnknown(input, mutationEvidenceId, null);
|
||||
} catch (cause: unknown) {
|
||||
return err(new UnknownRegistryError("invalid-transition", { cause }));
|
||||
}
|
||||
if (this.#heads.has(unknown.unknown_id))
|
||||
return err(new UnknownRegistryError("already-exists"));
|
||||
return this.#append(unknown);
|
||||
}
|
||||
|
||||
updateUnknown(
|
||||
input: UpdateUnknownInput,
|
||||
mutationEvidenceId: string,
|
||||
): UnknownRegistryMutation {
|
||||
const current = this.#heads.get(input.unknown_id);
|
||||
if (current === undefined)
|
||||
return err(new UnknownRegistryError("not-found"));
|
||||
if (current.revision !== input.expected_revision)
|
||||
return err(new UnknownRegistryError("revision-conflict"));
|
||||
let updated: ResidualUnknown;
|
||||
try {
|
||||
updated = updateResidualUnknown(current, input, mutationEvidenceId);
|
||||
} catch (cause: unknown) {
|
||||
return err(new UnknownRegistryError("invalid-transition", { cause }));
|
||||
}
|
||||
return this.#append(updated);
|
||||
}
|
||||
|
||||
listUnknowns(
|
||||
filters: {
|
||||
readonly status?: UnknownStatus;
|
||||
readonly severity?: ResidualUnknown["severity"];
|
||||
readonly domain?: string;
|
||||
} = {},
|
||||
): UnknownRegistryQuery {
|
||||
return ok(
|
||||
[...this.#heads.values()]
|
||||
.filter(
|
||||
(unknown) =>
|
||||
(filters.status === undefined ||
|
||||
unknown.status === filters.status) &&
|
||||
(filters.severity === undefined ||
|
||||
unknown.severity === filters.severity) &&
|
||||
(filters.domain === undefined || unknown.domain === filters.domain),
|
||||
)
|
||||
.sort((left, right) => left.unknown_id.localeCompare(right.unknown_id)),
|
||||
);
|
||||
}
|
||||
|
||||
verifyUnknownResolution(unknownId: string): UnknownRegistryVerify {
|
||||
const unknown = this.#heads.get(unknownId);
|
||||
if (unknown === undefined)
|
||||
return err(new UnknownRegistryError("not-found"));
|
||||
return ok({
|
||||
valid: unknown.status === "resolved",
|
||||
truthVerified: unknown.resolution?.disposition === "verified",
|
||||
unknown,
|
||||
});
|
||||
}
|
||||
|
||||
get(unknownId: string): ResidualUnknown | undefined {
|
||||
const unknown = this.#heads.get(unknownId);
|
||||
return unknown === undefined ? undefined : structuredClone(unknown);
|
||||
}
|
||||
|
||||
has(unknownId: string): boolean {
|
||||
return this.#heads.has(unknownId);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.#revisions.clear();
|
||||
this.#heads.clear();
|
||||
}
|
||||
|
||||
#append(unknown: ResidualUnknown): UnknownRegistryMutation {
|
||||
const pending = this.#revisions.size + 1;
|
||||
if (pending > this.limits.maxRecords)
|
||||
return err(new UnknownRegistryError("limit"));
|
||||
if (unknown.relationships.length > this.limits.maxRelationships)
|
||||
return err(new UnknownRegistryError("limit"));
|
||||
try {
|
||||
residualUnknownSchema.parse(unknown);
|
||||
} catch (cause: unknown) {
|
||||
return err(new UnknownRegistryError("integrity", { cause }));
|
||||
}
|
||||
this.#revisions.set(
|
||||
`${unknown.unknown_id}:${String(unknown.revision)}`,
|
||||
unknown,
|
||||
);
|
||||
this.#heads.set(unknown.unknown_id, unknown);
|
||||
return ok(unknown);
|
||||
}
|
||||
}
|
||||
+9
-1
@@ -19,6 +19,8 @@ import { importReferenceSource } from "./application/ReferenceSourceImport.js";
|
||||
import { registerEvidenceCommands } from "./cliEvidenceCommands.js";
|
||||
import { registerProcessCommands } from "./cliProcessCommands.js";
|
||||
import { registerInvestigationCommands } from "./cliInvestigationCommands.js";
|
||||
import { registerUnknownCommands } from "./cliUnknownCommands.js";
|
||||
import { registerBrowserCommands } from "./cliBrowserCommands.js";
|
||||
import {
|
||||
AnalysisProtocolError,
|
||||
PermissionRequiredError,
|
||||
@@ -27,7 +29,8 @@ import {
|
||||
import { projectReferenceSourceImportError } from "./application/ReferenceSourceImportTypes.js";
|
||||
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js";
|
||||
import { registerPolicyCommands } from "./cliPolicyCommands.js";
|
||||
import { registerBrowserCommands } from "./cliBrowserCommands.js";
|
||||
import { UnknownRegistry } from "./application/UnknownRegistry.js";
|
||||
import { UnknownRegistry } from "./application/UnknownRegistry.js";
|
||||
|
||||
/**
|
||||
* Build the one-shot Incur CLI without starting Hopper at import time.
|
||||
@@ -71,6 +74,11 @@ export const createCli = (): ReturnType<typeof Cli.create> => {
|
||||
registerArtifactCommands(cli, logger);
|
||||
registerInvestigationCommands(cli, logger);
|
||||
registerEvidenceCommands(cli, logger);
|
||||
registerUnknownCommands(
|
||||
cli,
|
||||
logger,
|
||||
new UnknownRegistry({ maxRecords: 1_000, maxRelationships: 100 }),
|
||||
);
|
||||
registerReferenceSourceCommand(cli, logger);
|
||||
registerProcessCommands(cli, logger);
|
||||
registerPolicyCommands(cli, logger);
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import { Cli, z } from "incur";
|
||||
|
||||
import type { UnknownRegistry } from "./application/UnknownRegistry.js";
|
||||
import type { Logger } from "./logger.js";
|
||||
import type {
|
||||
RecordUnknownInput,
|
||||
UpdateUnknownInput,
|
||||
} from "./domain/residualUnknown.js";
|
||||
|
||||
/** Register residual-unknown registry CLI commands. */
|
||||
export const registerUnknownCommands = (
|
||||
cli: ReturnType<typeof Cli.create>,
|
||||
_logger: Logger,
|
||||
registry: UnknownRegistry,
|
||||
): void => {
|
||||
cli.command("unknowns", {
|
||||
description: "List recorded residual unknowns with optional filters",
|
||||
options: z.object({
|
||||
status: z
|
||||
.enum(["open", "investigating", "blocked", "contradicted", "resolved"])
|
||||
.optional()
|
||||
.describe("Filter by status"),
|
||||
severity: z
|
||||
.enum(["low", "medium", "high", "critical"])
|
||||
.optional()
|
||||
.describe("Filter by severity"),
|
||||
domain: z.string().optional().describe("Filter by domain"),
|
||||
}),
|
||||
run: ({ options }): ReturnType<typeof registry.listUnknowns> => {
|
||||
const filters: {
|
||||
status?:
|
||||
| "open"
|
||||
| "investigating"
|
||||
| "blocked"
|
||||
| "contradicted"
|
||||
| "resolved";
|
||||
severity?: "low" | "medium" | "high" | "critical";
|
||||
domain?: string;
|
||||
} = {};
|
||||
if (options.status !== undefined) filters.status = options.status;
|
||||
if (options.severity !== undefined) filters.severity = options.severity;
|
||||
if (options.domain !== undefined) filters.domain = options.domain;
|
||||
return registry.listUnknowns(filters);
|
||||
},
|
||||
});
|
||||
|
||||
cli.command("unknown-record", {
|
||||
description: "Record one approved residual unknown question",
|
||||
options: z.object({
|
||||
question: z.string().min(1).max(1_000).describe("The bounded question"),
|
||||
severity: z
|
||||
.enum(["low", "medium", "high", "critical"])
|
||||
.describe("Severity of the unknown"),
|
||||
domain: z.string().min(1).max(100).describe("Domain of the unknown"),
|
||||
authority: z
|
||||
.enum([
|
||||
"shipped-artifact",
|
||||
"controlled-replay",
|
||||
"historical-reference",
|
||||
"external-service",
|
||||
"analyst-inference",
|
||||
])
|
||||
.describe("Required authority to resolve"),
|
||||
confidence: z
|
||||
.enum(["observed", "derived", "inferred"])
|
||||
.describe("Required confidence level"),
|
||||
evidence: z
|
||||
.string()
|
||||
.min(1)
|
||||
.describe("Mutation evidence ID for this record operation"),
|
||||
}),
|
||||
run: ({ options }) =>
|
||||
registry.recordUnknown(
|
||||
{
|
||||
question: options.question,
|
||||
severity: options.severity,
|
||||
domain: options.domain,
|
||||
required_authority: options.authority,
|
||||
required_confidence: options.confidence,
|
||||
required_environment: null,
|
||||
supporting_evidence_ids: [],
|
||||
contradicting_evidence_ids: [],
|
||||
recommended_probes: [],
|
||||
relationships: [],
|
||||
approved: true,
|
||||
} as RecordUnknownInput,
|
||||
options.evidence,
|
||||
),
|
||||
});
|
||||
|
||||
cli.command("unknown-update", {
|
||||
description: "Update one residual unknown with new evidence or status",
|
||||
options: z.object({
|
||||
id: z.string().describe("Unknown ID (unk_…)"),
|
||||
revision: z.number().int().min(1).describe("Expected current revision"),
|
||||
status: z
|
||||
.enum(["open", "investigating", "blocked", "contradicted", "resolved"])
|
||||
.describe("New status"),
|
||||
severity: z
|
||||
.enum(["low", "medium", "high", "critical"])
|
||||
.describe("New severity"),
|
||||
evidence: z
|
||||
.string()
|
||||
.min(1)
|
||||
.describe("Mutation evidence ID for this update operation"),
|
||||
}),
|
||||
run: ({ options }) =>
|
||||
registry.updateUnknown(
|
||||
{
|
||||
unknown_id: options.id,
|
||||
expected_revision: options.revision,
|
||||
approved: true,
|
||||
status: options.status,
|
||||
severity: options.severity,
|
||||
required_authority: null,
|
||||
required_confidence: "observed",
|
||||
required_environment: null,
|
||||
supporting_evidence_ids: [],
|
||||
contradicting_evidence_ids: [],
|
||||
recommended_probes: [],
|
||||
relationships: [],
|
||||
resolution: null,
|
||||
} as unknown as UpdateUnknownInput,
|
||||
options.evidence,
|
||||
),
|
||||
});
|
||||
|
||||
cli.command("unknown-verify", {
|
||||
description: "Verify the resolution state of one residual unknown",
|
||||
options: z.object({
|
||||
id: z.string().describe("Unknown ID (unk_…)"),
|
||||
}),
|
||||
run: ({ options }) => registry.verifyUnknownResolution(options.id),
|
||||
});
|
||||
};
|
||||
+1
-1
@@ -12,7 +12,7 @@ const logLevelSchema = z
|
||||
export const parseLogLevel = (value: unknown): LogLevel =>
|
||||
logLevelSchema.parse(value);
|
||||
|
||||
/** Create a JSON logger on stderr so protocol and CLI result stdout stay valid. */
|
||||
/** Create a JSON logger whose stderr destination cannot corrupt structured output. */
|
||||
export const createLogger = (mode: "mcp" | "cli", level: LogLevel): Logger =>
|
||||
pino(
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user