refactor(cli): project observe-native-calls onto a session target and its contract document (#10700)

* refactor(cli): project observe-native-calls onto a session target and its contract document

* refactor(cli): project endpoint comparison and browser scenario documents with delivery options (#10711)

* refactor(cli): carry document command delivery as options and project endpoint and browser scenario commands

A JSON-document projection can now take the contract's detail and view
fields as --detail, --evidence_output and --view beside the document.
The document refuses those fields with the option to use, and the
delivery is admitted by the contract's own view schema before the
document is read.

compare-static-and-observed-endpoints, capture-browser-scenario and
validate-browser-scenario now register from their contracts; validation
projects the capture contract without detail.

* fix(cli): admit the endpoint comparison document before reading its Evidence bundle

The document command context carries the document's transport frame, so
a handler that reads an Evidence bundle first can refuse a malformed
document beforehand, as the hand-written command did.

* refactor(cli): pass only object documents to the document field refusal

* refactor(cli): project inspect-artifact and extract-artifact with open_binary target options (#10722)

* refactor(cli): project inspect-artifact and extract-artifact with open_binary target options

A declared session target can now take fields of the contract an MCP
client opens the target with as options. They are admitted by that
contract, with the target as its path, before the target is opened, and
the projection can narrow an enum to the values the command supports.

inspect-artifact and extract-artifact are derived from their contracts
with open_binary's format and ESP flash fields. MCP open_binary and the
CLI share one mapping from those fields to the target selection.
extract-artifact now runs under the command's cancellation signal.

* fix(cli): read open_binary from its session contract so startup stays lazy

* build(lint): refuse value imports of the complete tool catalog in CLI startup modules
This commit is contained in:
morluto authored and GitHub committed 2026-11-15 20:36:57 +08:00
1 parent e97b6c5282
commit 781f89e6f4
14 files changed
+1158 -584

No files matched your search

+58 -116
View File
@@ -1,131 +1,73 @@
import { z } from "incur";
import type { Logger } from "pino";
import type { Evidence } from "../domain/evidence.js";
import type { CliInstance } from "./types.js";
import { CLI_COMMANDS } from "../cliCommandNames.js";
import { parseCliJsonInput } from "../cliJsonInput.js";
import { logCliCommand } from "../cliLogging.js";
import { compareStaticObservedEndpointsFactsSchema } from "../contracts/javascript/applicationWorkflowInputContracts.js";
import { jsonInputArgument } from "./jsonInputArgument.js";
import { getEvidenceInputContract } from "../domain/evidenceInputContract.js";
import { endpointComparisonViewRequestSchema } from "../domain/analysisView/endpointComparisonView.js";
import { parseAnalysisInput } from "../application/AnalysisInput.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { APPLICATION_TOOL_CONTRACTS } from "../contracts/applicationToolContracts.js";
import { JAVASCRIPT_APPLICATION_WORKFLOW_PROVIDER } from "../domain/investigationProviders.js";
import { withCommandCancellation } from "./commandCancellation.js";
import { contractNamed } from "./contractCliDescription.js";
import { registerContractDocumentCommand } from "./contractCommand.js";
import {
evidenceBundleOption,
runEvidenceInputCommand,
} from "./evidenceInputCommand.js";
import {
analysisEvidenceDeliveryOptions,
parseCliAnalysisEvidenceDelivery,
publishCliAnalysisEvidence,
} from "./analysisEvidenceDelivery.js";
const operation = "compare_static_and_observed_endpoints";
const comparisonDocumentSchema = getEvidenceInputContract(
compareStaticObservedEndpointsFactsSchema,
).frameSchema;
const deliverySchema = z
.object({
detail: analysisEvidenceDeliveryOptions.detail,
view: endpointComparisonViewRequestSchema.optional(),
})
.superRefine((value, context) => {
if (value.view !== undefined && value.detail !== "summary")
context.addIssue({
code: "custom",
path: ["view"],
message:
"A selected page requires --detail summary and --evidence_output.",
});
});
const commandOptions = z.object({
evidenceBundle: evidenceBundleOption,
...analysisEvidenceDeliveryOptions,
view: z
.string()
.optional()
.describe(
"JSON endpoint-comparison page; requires --detail summary and --evidence_output",
),
});
const runComparison = async (
inputJson: string,
options: z.output<typeof commandOptions>,
signal: AbortSignal,
) => {
const selected = parseCliAnalysisEvidenceDelivery(
operation,
options,
deliverySchema,
);
if (!selected.ok) return projectAnalysisError(selected.error);
const delivery = {
providerId: JAVASCRIPT_APPLICATION_WORKFLOW_PROVIDER.id,
operation,
signal,
detail: selected.value.detail,
evidenceOutput: selected.value.evidenceOutput,
...(selected.value.view === undefined ? {} : { view: selected.value.view }),
};
const raw = await parseCliJsonInput(inputJson, operation, signal);
if (!raw.ok) return raw.error;
// The frame validates transport fields without cloning either Evidence before budget admission.
const input = parseAnalysisInput(
operation,
raw.value,
comparisonDocumentSchema,
signal,
);
if (!input.ok) return projectAnalysisError(input.error);
let sources: readonly Evidence[] = [];
const result = await runEvidenceInputCommand(
operation,
options.evidenceBundle,
signal,
async (lookup) => {
const { compareStaticAndObservedEndpointsEvidence } =
await import("../application/javascript/StaticObservedEndpointComparisonService.js");
const compared = await compareStaticAndObservedEndpointsEvidence(
{ ...input.value, detail: options.detail },
lookup,
{ signal },
);
if (!compared.ok) return compared;
sources = compared.value.sources;
return { ok: true, value: compared.value.evidence };
},
);
return publishCliAnalysisEvidence(result, {
...delivery,
sourceEvidence: sources,
});
};
import { publishCliAnalysisEvidence } from "./analysisEvidenceDelivery.js";
import { parseAnalysisInput } from "../application/AnalysisInput.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
/** Reuse standard CLI publication so summaries keep their complete source closure on disk. */
export const registerStaticObservedEndpointsCommand = (
cli: CliInstance,
logger: Logger,
): void => {
const name = CLI_COMMANDS.compareStaticAndObservedEndpoints;
cli.command(name, {
description:
"Compare static JavaScript endpoints and HAR Evidence JSON offline; --detail summary with --evidence_output saves full facts for selected inspect-analysis-view pages",
args: z.object({
inputJson: jsonInputArgument(
): void =>
registerContractDocumentCommand(
cli,
logger,
contractNamed(
APPLICATION_TOOL_CONTRACTS,
"compare_static_and_observed_endpoints",
),
{
command: CLI_COMMANDS.compareStaticAndObservedEndpoints,
description:
"Compare static JavaScript endpoints and HAR Evidence JSON offline; --detail summary with --evidence_output saves full facts for selected inspect-analysis-view pages",
document:
"Inline JSON or file containing application, capture and optional origin",
comparisonDocumentSchema,
),
}),
options: commandOptions,
run: ({ args, options }) =>
withCommandCancellation((signal) =>
logCliCommand(logger, name, () =>
runComparison(args.inputJson, options, signal),
),
),
});
};
evidenceDelivery: true,
viewDelivery: { invalidJson: "view must be valid JSON" },
cliOptions: { evidenceBundle: evidenceBundleOption },
},
async (document, { signal, operation, delivery, cliOptions, frame }) => {
if (delivery === undefined)
throw new Error(`${operation} declares evidenceDelivery`);
// Refuse a malformed document before reading the Evidence bundle.
const framed = parseAnalysisInput(operation, document, frame, signal);
if (!framed.ok) return projectAnalysisError(framed.error);
let sources: readonly Evidence[] = [];
const result = await runEvidenceInputCommand(
operation,
cliOptions.evidenceBundle,
signal,
async (lookup) => {
const { compareStaticAndObservedEndpointsEvidence } =
await import("../application/javascript/StaticObservedEndpointComparisonService.js");
const compared = await compareStaticAndObservedEndpointsEvidence(
document,
lookup,
{ signal },
);
if (!compared.ok) return compared;
sources = compared.value.sources;
return { ok: true, value: compared.value.evidence };
},
);
return publishCliAnalysisEvidence(result, {
providerId: JAVASCRIPT_APPLICATION_WORKFLOW_PROVIDER.id,
operation,
signal,
detail: delivery.detail,
evidenceOutput: delivery.evidenceOutput,
...(delivery.view === undefined ? {} : { view: delivery.view }),
sourceEvidence: sources,
});
},
);