perf(mcp)!: compact repeated session discovery facts (#1988)
Group repeated session tool availability and provider capability facts, preserve complete named contracts and ordered limitations, and remove duplicate catalog descriptors. Update consumers and migration guidance. Validated by all 31 CI jobs, focused source checks, and actual Claude Code/Linux Hopper/native Windows Ghidra small-target workflows on the current integration tree. Refs #723. BREAKING CHANGE: binary_session no longer includes server_identity.catalog.tools; tool_availability and provider capabilities use grouped facts documented in docs/mcp-contracts.md. Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,15 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { buildCapabilityInventory } from "./CapabilityInventory.js";
|
||||
import {
|
||||
buildCapabilityInventory,
|
||||
compactToolAvailability,
|
||||
} from "./CapabilityInventory.js";
|
||||
import { TOOL_CONTRACTS } from "../contracts/toolContracts.js";
|
||||
import {
|
||||
expandToolAvailability,
|
||||
toolAvailabilityInventory,
|
||||
type ToolAvailability,
|
||||
} from "../contracts/toolOutputSchemaPrimitives.js";
|
||||
|
||||
it("reports module syntax and native resolution availability separately", () => {
|
||||
const input = { open: false, capabilities: [] };
|
||||
@@ -55,22 +64,25 @@ const status = (
|
||||
open: options.open ?? false,
|
||||
...(options.kind === undefined ? {} : { kind: options.kind }),
|
||||
...(options.format === undefined ? {} : { format: options.format }),
|
||||
capabilities: (options.capabilities ?? []).map((capability) => ({
|
||||
...capability,
|
||||
availability_code: capability.available
|
||||
? null
|
||||
: (capability.availability_code ?? null),
|
||||
effects: {
|
||||
mutates_artifact: false,
|
||||
launches_process: false,
|
||||
may_show_ui: false,
|
||||
may_access_network: false,
|
||||
may_write_filesystem: false,
|
||||
changes_permissions: false,
|
||||
requires_root: false,
|
||||
},
|
||||
limitations: [],
|
||||
})),
|
||||
capabilities:
|
||||
options.capabilities === undefined || options.capabilities.length === 0
|
||||
? []
|
||||
: [
|
||||
{
|
||||
provider: { id: "fixture", name: "Fixture", version: "1" },
|
||||
shared_limitations: [],
|
||||
groups: options.capabilities.map((capability) => ({
|
||||
operations: [capability.operation],
|
||||
effects: [],
|
||||
limitations: [],
|
||||
available: capability.available,
|
||||
reason: capability.reason,
|
||||
availability_code: capability.available
|
||||
? null
|
||||
: (capability.availability_code ?? null),
|
||||
})),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const entry = (
|
||||
@@ -171,6 +183,152 @@ describe("capability failure reasons", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("compact tool availability", () => {
|
||||
it("round-trips complete facts, omitted modes, and per-group client requirements", () => {
|
||||
const full = buildCapabilityInventory(status(), enabledPolicy);
|
||||
const alternateRequirements = {
|
||||
required: ["roots"],
|
||||
optional: [],
|
||||
missing_required: ["roots"],
|
||||
missing_optional: [],
|
||||
} satisfies ToolAvailability["client_requirements"];
|
||||
const withOverride = full.map((entry, index) =>
|
||||
index === 1
|
||||
? {
|
||||
...entry,
|
||||
available: false as const,
|
||||
reason: "client_capability_missing" as const,
|
||||
remediation: "Use an MCP client with roots support.",
|
||||
client_requirements: alternateRequirements,
|
||||
}
|
||||
: entry,
|
||||
);
|
||||
const compact = compactToolAvailability(withOverride);
|
||||
const expanded = expandToolAvailability(compact).toSorted((left, right) =>
|
||||
left.name.localeCompare(right.name),
|
||||
);
|
||||
|
||||
expect(expanded).toEqual(
|
||||
withOverride.toSorted((left, right) =>
|
||||
left.name.localeCompare(right.name),
|
||||
),
|
||||
);
|
||||
expect(compact.client_requirements).toEqual(
|
||||
withOverride[0]?.client_requirements,
|
||||
);
|
||||
expect(
|
||||
compact.groups.find((group) => group.client_requirements !== undefined)
|
||||
?.client_requirements,
|
||||
).toEqual(alternateRequirements);
|
||||
expect(compact.groups.every((group) => group.names.length > 0)).toBe(true);
|
||||
const names = compact.groups.flatMap((group) => group.names).sort();
|
||||
expect(names).toEqual(TOOL_CONTRACTS.map(({ name }) => name).sort());
|
||||
expect(new Set(names).size).toBe(names.length);
|
||||
expect(expanded.some((entry) => !Object.hasOwn(entry, "modes"))).toBe(true);
|
||||
});
|
||||
|
||||
it("preserves an empty inventory with an explicit empty client baseline", () => {
|
||||
const compact = compactToolAvailability([]);
|
||||
expect(compact).toEqual({
|
||||
client_requirements: {
|
||||
required: [],
|
||||
optional: [],
|
||||
missing_required: [],
|
||||
missing_optional: [],
|
||||
},
|
||||
groups: [],
|
||||
});
|
||||
expect(expandToolAvailability(compact)).toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects malformed compact facts and accepts an equivalent explicit baseline", () => {
|
||||
const compact = compactToolAvailability(
|
||||
buildCapabilityInventory(status(), enabledPolicy),
|
||||
);
|
||||
const firstGroup = compact.groups[0];
|
||||
expect(firstGroup).toBeDefined();
|
||||
if (firstGroup === undefined) throw new Error("Expected tool groups");
|
||||
const availableIndex = compact.groups.findIndex((group) => group.available);
|
||||
if (availableIndex < 0) throw new Error("Expected an available tool group");
|
||||
|
||||
const malformedInventories = [
|
||||
{
|
||||
...compact,
|
||||
groups: compact.groups.map((group, index) =>
|
||||
index === 0
|
||||
? { ...group, effects: [...group.effects, "unknown_effect"] }
|
||||
: group,
|
||||
),
|
||||
},
|
||||
{
|
||||
...compact,
|
||||
groups: compact.groups.map((group, index) =>
|
||||
index === 0
|
||||
? {
|
||||
...group,
|
||||
annotations: [...group.annotations, "unknown_hint"],
|
||||
}
|
||||
: group,
|
||||
),
|
||||
},
|
||||
{
|
||||
...compact,
|
||||
groups: compact.groups.map((group, index) =>
|
||||
index === 0 ? { ...group, names: [] } : group,
|
||||
),
|
||||
},
|
||||
{
|
||||
...compact,
|
||||
groups: compact.groups.map((group, index) =>
|
||||
index === 0 ? { ...group, names: [""] } : group,
|
||||
),
|
||||
},
|
||||
{
|
||||
...compact,
|
||||
groups: compact.groups.map((group, index) =>
|
||||
index === availableIndex
|
||||
? { ...group, reason: "provider_missing" }
|
||||
: group,
|
||||
),
|
||||
},
|
||||
{
|
||||
...compact,
|
||||
groups: compact.groups.map((group, index) =>
|
||||
index === availableIndex
|
||||
? { ...group, remediation: "unexpected remediation" }
|
||||
: group,
|
||||
),
|
||||
},
|
||||
];
|
||||
for (const malformed of malformedInventories)
|
||||
expect(toolAvailabilityInventory.safeParse(malformed).success).toBe(
|
||||
false,
|
||||
);
|
||||
|
||||
const inheritedGroup = compact.groups.find(
|
||||
(group) => group.client_requirements === undefined,
|
||||
);
|
||||
expect(inheritedGroup).toBeDefined();
|
||||
if (inheritedGroup === undefined)
|
||||
throw new Error("Expected an inherited client-requirements group");
|
||||
const explicitBaseline = {
|
||||
...compact,
|
||||
groups: compact.groups.map((group) =>
|
||||
group === inheritedGroup
|
||||
? { ...group, client_requirements: compact.client_requirements }
|
||||
: group,
|
||||
),
|
||||
};
|
||||
const parsedOverride =
|
||||
toolAvailabilityInventory.safeParse(explicitBaseline);
|
||||
expect(parsedOverride.success).toBe(true);
|
||||
if (parsedOverride.success)
|
||||
expect(expandToolAvailability(parsedOverride.data)).toEqual(
|
||||
expandToolAvailability(compact),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("partial navigation availability", () => {
|
||||
it("allows explicit documents when the provider has no current-document getter", () => {
|
||||
const capabilities = [
|
||||
@@ -258,6 +416,36 @@ describe("partial navigation availability", () => {
|
||||
remediation: "Navigation requires macOS.",
|
||||
});
|
||||
});
|
||||
|
||||
it("lists shared missing operations once in combined remediation", () => {
|
||||
const availability = entry(
|
||||
"get_navigation_context",
|
||||
status({ open: true, kind: "executable" }),
|
||||
);
|
||||
|
||||
expect(availability).toMatchObject({
|
||||
reason: "provider_missing",
|
||||
remediation:
|
||||
"Configure a provider for one of: current_document, current_address, resolve_containing_procedure.",
|
||||
modes: [
|
||||
{
|
||||
name: "current_selection",
|
||||
missing_operations: [
|
||||
"current_document",
|
||||
"current_address",
|
||||
"resolve_containing_procedure",
|
||||
],
|
||||
},
|
||||
{
|
||||
name: "explicit_document",
|
||||
missing_operations: [
|
||||
"current_address",
|
||||
"resolve_containing_procedure",
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("reports host denial before target mismatch but keeps composed workflow prerequisites", () => {
|
||||
|
||||
@@ -5,9 +5,14 @@ import type {
|
||||
} from "./OptionalObservationProviders.js";
|
||||
|
||||
import {
|
||||
providerCapability,
|
||||
expandProviderCapabilities,
|
||||
providerCapabilitySet,
|
||||
toolAvailabilityInventory,
|
||||
TOOL_ANNOTATION_NAMES,
|
||||
TOOL_EFFECT_NAMES,
|
||||
type ClientFeatureAvailability,
|
||||
type ProviderCapability,
|
||||
type ToolAvailabilityInventory,
|
||||
type ToolAvailability,
|
||||
type ToolAvailabilityMode,
|
||||
type ToolUnavailabilityReason,
|
||||
@@ -26,7 +31,9 @@ const statusSchema = z.object({
|
||||
open: z.boolean(),
|
||||
kind: z.enum(["executable", "database", "archive", "artifact"]).optional(),
|
||||
format: z.string().optional(),
|
||||
capabilities: z.array(providerCapability),
|
||||
capabilities: z
|
||||
.array(providerCapabilitySet)
|
||||
.transform(expandProviderCapabilities),
|
||||
});
|
||||
|
||||
type ToolAvailabilityReason = "available" | ToolUnavailabilityReason;
|
||||
@@ -214,6 +221,60 @@ export const buildCapabilityInventory = (
|
||||
}).sort((left, right) => compareUnicodeCodePoints(left.name, right.name));
|
||||
};
|
||||
|
||||
type ClientRequirements = ToolAvailability["client_requirements"];
|
||||
type MutableAvailabilityGroup = Omit<
|
||||
ToolAvailabilityInventory["groups"][number],
|
||||
"names"
|
||||
> & { names: string[] };
|
||||
|
||||
const EMPTY_CLIENT_REQUIREMENTS: ClientRequirements = {
|
||||
required: [],
|
||||
optional: [],
|
||||
missing_required: [],
|
||||
missing_optional: [],
|
||||
};
|
||||
|
||||
/** Compact repeated metadata while retaining every operation's current availability inline. */
|
||||
export const compactToolAvailability = (
|
||||
availability: readonly ToolAvailability[],
|
||||
): ToolAvailabilityInventory => {
|
||||
const clientRequirements =
|
||||
availability[0]?.client_requirements ?? EMPTY_CLIENT_REQUIREMENTS;
|
||||
const groups = new Map<string, MutableAvailabilityGroup>();
|
||||
|
||||
for (const entry of availability) {
|
||||
const effects = TOOL_EFFECT_NAMES.filter((name) => entry.effects[name]);
|
||||
const annotations = TOOL_ANNOTATION_NAMES.filter(
|
||||
(name) => entry.annotations[name],
|
||||
);
|
||||
const groupFacts = {
|
||||
surface: entry.surface,
|
||||
effects,
|
||||
annotations,
|
||||
available: entry.available,
|
||||
reason: entry.reason,
|
||||
remediation: entry.remediation,
|
||||
...(entry.default_mode_available === undefined
|
||||
? {}
|
||||
: { default_mode_available: entry.default_mode_available }),
|
||||
...(entry.modes === undefined ? {} : { modes: [...entry.modes] }),
|
||||
...(JSON.stringify(entry.client_requirements) ===
|
||||
JSON.stringify(clientRequirements)
|
||||
? {}
|
||||
: { client_requirements: entry.client_requirements }),
|
||||
};
|
||||
const key = JSON.stringify(groupFacts);
|
||||
const existing = groups.get(key);
|
||||
if (existing) existing.names.push(entry.name);
|
||||
else groups.set(key, { ...groupFacts, names: [entry.name] });
|
||||
}
|
||||
|
||||
return toolAvailabilityInventory.parse({
|
||||
client_requirements: clientRequirements,
|
||||
groups: [...groups.values()],
|
||||
});
|
||||
};
|
||||
|
||||
const availabilityFor = (context: AvailabilityContext): Availability => {
|
||||
// These built-in workflows accept a caller-selected endpoint; they do not load the optional passive provider.
|
||||
if (
|
||||
@@ -626,14 +687,15 @@ const navigationContextAvailability = (
|
||||
};
|
||||
if (availableMode !== undefined)
|
||||
return { ...facts, reason: "available", remediation: null };
|
||||
const missingOperations = [
|
||||
...new Set(modes.flatMap((mode) => mode.missing_operations)),
|
||||
];
|
||||
return {
|
||||
...facts,
|
||||
reason: failure?.reason ?? "provider_missing",
|
||||
remediation:
|
||||
specificFailureRemediation ??
|
||||
`Configure a provider for one of: ${modes
|
||||
.flatMap((mode) => mode.missing_operations)
|
||||
.join(", ")}.`,
|
||||
`Configure a provider for one of: ${missingOperations.join(", ")}.`,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,391 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type {
|
||||
CapabilityDescriptor,
|
||||
ProviderIdentity,
|
||||
} from "../AnalysisProvider.js";
|
||||
import {
|
||||
expandProviderCapabilities,
|
||||
type ProviderCapabilitySet,
|
||||
} from "../../contracts/toolOutputSchemaPrimitives.js";
|
||||
import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js";
|
||||
import type { AnalysisProviderCandidateStatus } from "./AnalysisProviderRegistry.js";
|
||||
import type {
|
||||
SessionProviderRoute,
|
||||
SessionProviderRouter,
|
||||
} from "./SessionProviderRouter.js";
|
||||
import { binarySessionStatus } from "./BinarySessionStatus.js";
|
||||
|
||||
type ProjectedStatus = {
|
||||
capabilities: ProviderCapabilitySet[];
|
||||
analysis_provider_candidates: {
|
||||
provider: ProviderIdentity;
|
||||
availability: unknown;
|
||||
target_support: unknown;
|
||||
selected: boolean;
|
||||
capabilities: ProviderCapabilitySet[];
|
||||
}[];
|
||||
};
|
||||
|
||||
const providerA: ProviderIdentity = {
|
||||
id: "provider-a",
|
||||
name: "Provider A",
|
||||
version: "1.0",
|
||||
};
|
||||
const providerB: ProviderIdentity = {
|
||||
id: "provider-b",
|
||||
name: "Provider B",
|
||||
version: "2.0",
|
||||
};
|
||||
|
||||
const MUTATING_EFFECTS = {
|
||||
mutates_artifact: true,
|
||||
launches_process: false,
|
||||
may_show_ui: false,
|
||||
may_access_network: false,
|
||||
may_write_filesystem: false,
|
||||
changes_permissions: false,
|
||||
requires_root: false,
|
||||
};
|
||||
|
||||
type DescriptorOptions = {
|
||||
readonly launchesProcess?: boolean;
|
||||
} & (
|
||||
| {
|
||||
readonly available?: true;
|
||||
readonly reason?: never;
|
||||
readonly availabilityCode?: never;
|
||||
}
|
||||
| {
|
||||
readonly available: false;
|
||||
readonly reason?: string;
|
||||
readonly availabilityCode?: "architecture_unsupported";
|
||||
}
|
||||
);
|
||||
|
||||
const descriptor = (
|
||||
provider: ProviderIdentity,
|
||||
operation: CapabilityDescriptor["operation"],
|
||||
limitations: readonly string[],
|
||||
options: DescriptorOptions = {},
|
||||
): CapabilityDescriptor => {
|
||||
const availability =
|
||||
options.available === false
|
||||
? {
|
||||
available: false as const,
|
||||
reason: options.reason ?? "Unavailable",
|
||||
...(options.availabilityCode === undefined
|
||||
? {}
|
||||
: { availabilityCode: options.availabilityCode }),
|
||||
}
|
||||
: { available: true as const, reason: null };
|
||||
return {
|
||||
provider,
|
||||
operation,
|
||||
...availability,
|
||||
effects: {
|
||||
mutatesArtifact: true,
|
||||
launchesProcess: options.launchesProcess ?? false,
|
||||
mayShowUi: false,
|
||||
mayAccessNetwork: false,
|
||||
mayWriteFilesystem: false,
|
||||
changesPermissions: false,
|
||||
requiresRoot: false,
|
||||
},
|
||||
limitations,
|
||||
};
|
||||
};
|
||||
|
||||
const candidate = (
|
||||
provider: ProviderIdentity,
|
||||
capabilities: readonly CapabilityDescriptor[],
|
||||
): AnalysisProviderCandidateStatus => ({
|
||||
provider,
|
||||
availability: {
|
||||
status: "available",
|
||||
code: null,
|
||||
reason: null,
|
||||
diagnostics: {},
|
||||
},
|
||||
targetSupport: {
|
||||
status: "unknown",
|
||||
code: null,
|
||||
reason: "No target is open.",
|
||||
diagnostics: {},
|
||||
},
|
||||
selected: true,
|
||||
capabilities,
|
||||
});
|
||||
|
||||
const project = (
|
||||
routeCapabilities: readonly CapabilityDescriptor[],
|
||||
candidates: readonly AnalysisProviderCandidateStatus[],
|
||||
runtimeUnavailability: ReadonlyMap<
|
||||
string,
|
||||
{ readonly reason: string }
|
||||
> = new Map(),
|
||||
): ProjectedStatus => {
|
||||
const route = {
|
||||
capabilities: new Map(
|
||||
routeCapabilities.map((capability) => [capability.operation, capability]),
|
||||
),
|
||||
binding: null,
|
||||
} as unknown as SessionProviderRoute;
|
||||
const router = {
|
||||
candidateStatuses: () => candidates,
|
||||
} as unknown as SessionProviderRouter;
|
||||
return binarySessionStatus({
|
||||
target: undefined,
|
||||
route,
|
||||
router,
|
||||
runtimeUnavailability,
|
||||
runId: undefined,
|
||||
runtimeLineageSnapshots: [],
|
||||
requestActivitySnapshots: [],
|
||||
}) as unknown as ProjectedStatus;
|
||||
};
|
||||
|
||||
const expandCanonicalCapabilities = (sets: readonly ProviderCapabilitySet[]) =>
|
||||
[...expandProviderCapabilities(sets)].sort((left, right) =>
|
||||
compareUnicodeCodePoints(left.operation, right.operation),
|
||||
);
|
||||
|
||||
const selectedCapabilities = [
|
||||
descriptor(providerA, "xrefs", ["Shared limitation"]),
|
||||
descriptor(providerA, "procedure_info", ["Shared limitation"]),
|
||||
descriptor(providerA, "read_bytes", ["Shared limitation", "beta detail"]),
|
||||
];
|
||||
const otherCapabilities = [
|
||||
descriptor(providerB, "read_bytes", ["Different limitation"], {
|
||||
launchesProcess: true,
|
||||
available: false,
|
||||
reason: "Architecture unsupported",
|
||||
availabilityCode: "architecture_unsupported",
|
||||
}),
|
||||
];
|
||||
|
||||
const expectedRouteCapabilities: ProviderCapabilitySet[] = [
|
||||
{
|
||||
provider: providerA,
|
||||
shared_limitations: ["Shared limitation"],
|
||||
groups: [
|
||||
{
|
||||
operations: ["procedure_info"],
|
||||
effects: ["mutates_artifact"],
|
||||
limitations: [],
|
||||
available: false,
|
||||
reason: "Runtime operation failure",
|
||||
availability_code: null,
|
||||
},
|
||||
{
|
||||
operations: ["read_bytes"],
|
||||
effects: ["mutates_artifact"],
|
||||
limitations: ["beta detail"],
|
||||
available: true,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
},
|
||||
{
|
||||
operations: ["xrefs"],
|
||||
effects: ["mutates_artifact"],
|
||||
limitations: [],
|
||||
available: true,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
const expectedExpandedRouteCapabilities = [
|
||||
{
|
||||
provider: providerA,
|
||||
operation: "procedure_info",
|
||||
available: false,
|
||||
reason: "Runtime operation failure",
|
||||
availability_code: null,
|
||||
effects: MUTATING_EFFECTS,
|
||||
limitations: ["Shared limitation"],
|
||||
},
|
||||
{
|
||||
provider: providerA,
|
||||
operation: "read_bytes",
|
||||
available: true,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
effects: MUTATING_EFFECTS,
|
||||
limitations: ["Shared limitation", "beta detail"],
|
||||
},
|
||||
{
|
||||
provider: providerA,
|
||||
operation: "xrefs",
|
||||
available: true,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
effects: MUTATING_EFFECTS,
|
||||
limitations: ["Shared limitation"],
|
||||
},
|
||||
];
|
||||
const expectedCandidateCapabilities: ProviderCapabilitySet[] = [
|
||||
{
|
||||
provider: providerA,
|
||||
shared_limitations: ["Shared limitation"],
|
||||
groups: [
|
||||
{
|
||||
operations: ["procedure_info", "xrefs"],
|
||||
effects: ["mutates_artifact"],
|
||||
limitations: [],
|
||||
available: true,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
},
|
||||
{
|
||||
operations: ["read_bytes"],
|
||||
effects: ["mutates_artifact"],
|
||||
limitations: ["beta detail"],
|
||||
available: true,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
provider: providerB,
|
||||
shared_limitations: ["Different limitation"],
|
||||
groups: [
|
||||
{
|
||||
operations: ["read_bytes"],
|
||||
effects: ["mutates_artifact", "launches_process"],
|
||||
limitations: [],
|
||||
available: false,
|
||||
reason: "Architecture unsupported",
|
||||
availability_code: "architecture_unsupported",
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
const expectedCandidateStatusFacts = [
|
||||
expect.objectContaining({
|
||||
provider: providerA,
|
||||
target_support: {
|
||||
status: "unknown",
|
||||
code: null,
|
||||
reason: "No target is open.",
|
||||
diagnostics: {},
|
||||
},
|
||||
capabilities: [expectedCandidateCapabilities[0]],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: providerB,
|
||||
capabilities: [expectedCandidateCapabilities[1]],
|
||||
}),
|
||||
];
|
||||
const expectedExpandedCandidateCapabilities = [
|
||||
[
|
||||
{
|
||||
provider: providerA,
|
||||
operation: "procedure_info",
|
||||
available: true,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
effects: MUTATING_EFFECTS,
|
||||
limitations: ["Shared limitation"],
|
||||
},
|
||||
{
|
||||
provider: providerA,
|
||||
operation: "read_bytes",
|
||||
available: true,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
effects: MUTATING_EFFECTS,
|
||||
limitations: ["Shared limitation", "beta detail"],
|
||||
},
|
||||
{
|
||||
provider: providerA,
|
||||
operation: "xrefs",
|
||||
available: true,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
effects: MUTATING_EFFECTS,
|
||||
limitations: ["Shared limitation"],
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
provider: providerB,
|
||||
operation: "read_bytes",
|
||||
available: false,
|
||||
reason: "Architecture unsupported",
|
||||
availability_code: "architecture_unsupported",
|
||||
effects: { ...MUTATING_EFFECTS, launches_process: true },
|
||||
limitations: ["Different limitation"],
|
||||
},
|
||||
],
|
||||
];
|
||||
|
||||
describe("binary session provider capability projection", () => {
|
||||
it("preserves provider scope and route overlays while factoring exact prefixes", () => {
|
||||
const selectedBefore = structuredClone(selectedCapabilities);
|
||||
const otherBefore = structuredClone(otherCapabilities);
|
||||
const status = project(
|
||||
selectedCapabilities,
|
||||
[
|
||||
candidate(providerA, selectedCapabilities),
|
||||
candidate(providerB, otherCapabilities),
|
||||
],
|
||||
new Map([["procedure_info", { reason: "Runtime operation failure" }]]),
|
||||
);
|
||||
expect(selectedCapabilities).toEqual(selectedBefore);
|
||||
expect(otherCapabilities).toEqual(otherBefore);
|
||||
expect(status.capabilities).toEqual(expectedRouteCapabilities);
|
||||
expect(expandCanonicalCapabilities(status.capabilities)).toEqual(
|
||||
expectedExpandedRouteCapabilities,
|
||||
);
|
||||
expect(status.analysis_provider_candidates).toEqual(
|
||||
expectedCandidateStatusFacts,
|
||||
);
|
||||
const selectedCandidate = status.analysis_provider_candidates[0];
|
||||
const otherCandidate = status.analysis_provider_candidates[1];
|
||||
expect(selectedCandidate).toBeDefined();
|
||||
expect(otherCandidate).toBeDefined();
|
||||
expect(
|
||||
expandCanonicalCapabilities(selectedCandidate?.capabilities ?? []),
|
||||
).toEqual(expectedExpandedCandidateCapabilities[0]);
|
||||
expect(
|
||||
expandCanonicalCapabilities(otherCandidate?.capabilities ?? []),
|
||||
).toEqual(expectedExpandedCandidateCapabilities[1]);
|
||||
});
|
||||
|
||||
it("keeps empty route and candidate capability collections empty", () => {
|
||||
const status = project([], [candidate(providerA, [])]);
|
||||
expect(status.capabilities).toEqual([]);
|
||||
expect(status.analysis_provider_candidates[0]?.capabilities).toEqual([]);
|
||||
});
|
||||
|
||||
it("preserves repeated limitations in their original order", () => {
|
||||
const capabilities = [
|
||||
descriptor(providerA, "procedure_info", [
|
||||
"Repeated",
|
||||
"Repeated",
|
||||
"left detail",
|
||||
]),
|
||||
descriptor(providerA, "xrefs", ["Repeated", "Repeated", "right detail"]),
|
||||
];
|
||||
const status = project(capabilities, []);
|
||||
|
||||
expect(status.capabilities[0]).toMatchObject({
|
||||
shared_limitations: ["Repeated", "Repeated"],
|
||||
groups: [
|
||||
{ operations: ["procedure_info"], limitations: ["left detail"] },
|
||||
{ operations: ["xrefs"], limitations: ["right detail"] },
|
||||
],
|
||||
});
|
||||
expect(
|
||||
expandCanonicalCapabilities(status.capabilities).map(
|
||||
({ limitations }) => limitations,
|
||||
),
|
||||
).toEqual([
|
||||
["Repeated", "Repeated", "left detail"],
|
||||
["Repeated", "Repeated", "right detail"],
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -4,9 +4,14 @@ import type { JsonValue } from "../../domain/jsonValue.js";
|
||||
import type { ProviderOperationHealth } from "../../domain/providerOperationHealth.js";
|
||||
import type {
|
||||
CapabilityDescriptor,
|
||||
ProviderIdentity,
|
||||
ProviderRequestActivitySnapshot,
|
||||
ProviderRuntimeLineageSnapshot,
|
||||
} from "../AnalysisProvider.js";
|
||||
import {
|
||||
PROVIDER_EFFECT_NAMES,
|
||||
type ProviderCapabilitySet,
|
||||
} from "../../contracts/toolOutputSchemaPrimitives.js";
|
||||
import type {
|
||||
SessionProviderRoute,
|
||||
SessionProviderRouter,
|
||||
@@ -38,7 +43,7 @@ export const binarySessionStatus = ({
|
||||
requestActivitySnapshots,
|
||||
providerOperationHealth,
|
||||
}: BinarySessionStatusInput): JsonValue => {
|
||||
const capabilities = [...route.capabilities.values()]
|
||||
const routeCapabilities = [...route.capabilities.values()]
|
||||
.filter(({ operation }) => operation !== "inventory_artifact")
|
||||
.sort((left, right) =>
|
||||
compareUnicodeCodePoints(left.operation, right.operation),
|
||||
@@ -52,8 +57,8 @@ export const binarySessionStatus = ({
|
||||
available: false as const,
|
||||
reason: unavailable.reason,
|
||||
};
|
||||
})
|
||||
.map(capabilityStatus);
|
||||
});
|
||||
const capabilities = providerCapabilitySets(routeCapabilities);
|
||||
const analysisProviderBinding =
|
||||
route.binding === null
|
||||
? null
|
||||
@@ -69,13 +74,14 @@ export const binarySessionStatus = ({
|
||||
availability: structuredClone(candidate.availability),
|
||||
target_support: structuredClone(candidate.targetSupport),
|
||||
selected: candidate.selected,
|
||||
capabilities: candidate.capabilities
|
||||
.slice()
|
||||
.filter(({ operation }) => operation !== "inventory_artifact")
|
||||
.sort((left, right) =>
|
||||
compareUnicodeCodePoints(left.operation, right.operation),
|
||||
)
|
||||
.map(capabilityStatus),
|
||||
capabilities: providerCapabilitySets(
|
||||
candidate.capabilities
|
||||
.slice()
|
||||
.filter(({ operation }) => operation !== "inventory_artifact")
|
||||
.sort((left, right) =>
|
||||
compareUnicodeCodePoints(left.operation, right.operation),
|
||||
),
|
||||
),
|
||||
}));
|
||||
const common = {
|
||||
capabilities,
|
||||
@@ -187,17 +193,109 @@ const processLineageStatus = (
|
||||
})),
|
||||
};
|
||||
|
||||
const capabilityStatus = (
|
||||
descriptor: Omit<CapabilityDescriptor, "available" | "reason"> & {
|
||||
readonly available: boolean;
|
||||
readonly reason: string | null;
|
||||
},
|
||||
) => ({
|
||||
operation: descriptor.operation,
|
||||
available: descriptor.available,
|
||||
reason: descriptor.reason,
|
||||
availability_code: descriptor.availabilityCode ?? null,
|
||||
effects: {
|
||||
type CapabilityEffectName = (typeof PROVIDER_EFFECT_NAMES)[number];
|
||||
type CapabilityAvailabilityCode = Exclude<
|
||||
CapabilityDescriptor["availabilityCode"],
|
||||
undefined
|
||||
>;
|
||||
type CapabilityProjection = Omit<CapabilityDescriptor, "available" | "reason"> &
|
||||
(
|
||||
| { readonly available: true; readonly reason: null }
|
||||
| { readonly available: false; readonly reason: string }
|
||||
);
|
||||
type ProviderCapabilityGroup = ProviderCapabilitySet["groups"][number];
|
||||
|
||||
/** Group inline capability facts without merging distinct providers or states. */
|
||||
const providerCapabilitySets = (
|
||||
descriptors: readonly CapabilityProjection[],
|
||||
): ProviderCapabilitySet[] => {
|
||||
const providers = new Map<
|
||||
string,
|
||||
{
|
||||
readonly identity: ProviderIdentity;
|
||||
readonly descriptors: CapabilityProjection[];
|
||||
}
|
||||
>();
|
||||
for (const descriptor of descriptors) {
|
||||
const key = JSON.stringify([
|
||||
descriptor.provider.id,
|
||||
descriptor.provider.name,
|
||||
descriptor.provider.version,
|
||||
]);
|
||||
const provider = providers.get(key);
|
||||
if (provider === undefined)
|
||||
providers.set(key, {
|
||||
identity: descriptor.provider,
|
||||
descriptors: [descriptor],
|
||||
});
|
||||
else provider.descriptors.push(descriptor);
|
||||
}
|
||||
return [...providers.values()].map(({ identity, descriptors: owned }) => {
|
||||
const sharedLimitations = commonLimitationsPrefix(owned);
|
||||
const groups = new Map<string, ProviderCapabilityGroup>();
|
||||
for (const descriptor of owned) {
|
||||
const effects = enabledCapabilityEffects(descriptor);
|
||||
const limitations = descriptor.limitations.slice(
|
||||
sharedLimitations.length,
|
||||
);
|
||||
const availabilityCode: CapabilityAvailabilityCode | null =
|
||||
descriptor.availabilityCode ?? null;
|
||||
const status = descriptor.available
|
||||
? {
|
||||
available: true as const,
|
||||
reason: null,
|
||||
availability_code: null,
|
||||
}
|
||||
: {
|
||||
available: false as const,
|
||||
reason: descriptor.reason,
|
||||
availability_code: availabilityCode,
|
||||
};
|
||||
const key = JSON.stringify([
|
||||
effects,
|
||||
limitations,
|
||||
status.available,
|
||||
status.reason,
|
||||
status.availability_code,
|
||||
]);
|
||||
const group = groups.get(key);
|
||||
if (group === undefined)
|
||||
groups.set(key, {
|
||||
operations: [descriptor.operation],
|
||||
effects,
|
||||
limitations,
|
||||
...status,
|
||||
});
|
||||
else group.operations.push(descriptor.operation);
|
||||
}
|
||||
return {
|
||||
provider: providerSummary(identity),
|
||||
shared_limitations: sharedLimitations,
|
||||
groups: [...groups.values()],
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const commonLimitationsPrefix = (
|
||||
descriptors: readonly CapabilityProjection[],
|
||||
): string[] => {
|
||||
const first = descriptors[0]?.limitations;
|
||||
if (first === undefined) return [];
|
||||
let length = first.length;
|
||||
for (const descriptor of descriptors.slice(1)) {
|
||||
length = Math.min(length, descriptor.limitations.length);
|
||||
let index = 0;
|
||||
while (index < length && first[index] === descriptor.limitations[index])
|
||||
index++;
|
||||
length = index;
|
||||
}
|
||||
return first.slice(0, length);
|
||||
};
|
||||
|
||||
const enabledCapabilityEffects = (
|
||||
descriptor: CapabilityProjection,
|
||||
): CapabilityEffectName[] => {
|
||||
const effectValues = {
|
||||
mutates_artifact: descriptor.effects.mutatesArtifact,
|
||||
launches_process: descriptor.effects.launchesProcess,
|
||||
may_show_ui: descriptor.effects.mayShowUi,
|
||||
@@ -205,9 +303,9 @@ const capabilityStatus = (
|
||||
may_write_filesystem: descriptor.effects.mayWriteFilesystem,
|
||||
changes_permissions: descriptor.effects.changesPermissions,
|
||||
requires_root: descriptor.effects.requiresRoot,
|
||||
},
|
||||
limitations: [...descriptor.limitations],
|
||||
});
|
||||
} satisfies Record<CapabilityEffectName, boolean>;
|
||||
return PROVIDER_EFFECT_NAMES.filter((name) => effectValues[name]);
|
||||
};
|
||||
|
||||
const providerOperationHealthStatus = (
|
||||
health: ProviderOperationHealth | null | undefined,
|
||||
|
||||
Reference in New Issue
Block a user