diff --git a/src/application/XrefsAddressResolution.ts b/src/application/XrefsAddressResolution.ts index 9a4b72b01e..ac5e69d99c 100644 --- a/src/application/XrefsAddressResolution.ts +++ b/src/application/XrefsAddressResolution.ts @@ -76,7 +76,9 @@ export const resolveXrefsName = async ( } if (signal?.aborted) return err(cancelledBy(operation, signal)); const addresses = new Set(); - for (const [index, row] of inventory.value.fact.entries()) { + const rows = inventory.value.fact; + for (let index = 0; index < rows.length; index += 1) { + const row = rows[index]!; // An empty address cannot be passed back to xrefs. if (row.address.length === 0) return err( diff --git a/src/cli/cliResultOutput.ts b/src/cli/cliResultOutput.ts index c3031a965a..9f42730c8a 100644 --- a/src/cli/cliResultOutput.ts +++ b/src/cli/cliResultOutput.ts @@ -576,20 +576,13 @@ export const createCliResultOutput = ( // require a complete formatted string. const tokenValue = document; let characters = 0; - if ( - tokenValue !== undefined && - tokenValue !== null && - typeof tokenValue !== "function" && - typeof tokenValue !== "symbol" - ) { - if (format === "json") { - for (const part of jsonOutputParts( - tokenValue, - format, - isTerminalOutput(destination), - )) - characters += part.length; - } + if (format === "json") { + for (const part of jsonOutputParts( + tokenValue, + format, + isTerminalOutput(destination), + )) + characters += part.length; } if (characters <= constants.MAX_STRING_LENGTH) { let formatted: string; diff --git a/src/cli/commandRegistry.ts b/src/cli/commandRegistry.ts index 36e880a53b..68c316bfef 100644 --- a/src/cli/commandRegistry.ts +++ b/src/cli/commandRegistry.ts @@ -123,6 +123,7 @@ export const registerCliCommand = < source: registration.source ?? { kind: "host" }, }; registry.entries.push(entry); + operationCommandIndexes.delete(registry.entries); registry.byName.set(name, entry); }; @@ -211,6 +212,56 @@ export const commandOwnOperations = ( return operations; }; +interface OperationCommandIndex { + readonly canonical: ReadonlyMap; + readonly contractPrimary: ReadonlyMap; + readonly contractSecondary: ReadonlyMap; + readonly otherMatch: ReadonlyMap; +} + +const operationCommandIndexes = new WeakMap< + readonly CliCommandEntry[], + OperationCommandIndex +>(); + +const getOperationCommandIndex = ( + entries: readonly CliCommandEntry[], +): OperationCommandIndex => { + let cached = operationCommandIndexes.get(entries); + if (cached !== undefined) return cached; + const canonical = new Map(); + const contractPrimary = new Map(); + const contractSecondary = new Map(); + const otherMatch = new Map(); + for (const entry of entries) { + const own = commandOwnOperations(entry); + const primaryName = own[0]?.name; + for (let index = 0; index < own.length; index += 1) { + const matched = own[index]!; + const opName = matched.name; + const command = + matched.selector === undefined + ? entry.name + : `${entry.name} ${matched.selector}`; + if (entry.name === cliCommandFor(opName) && !canonical.has(opName)) + canonical.set(opName, command); + if (entry.source.kind === "contract") { + if (primaryName === opName) { + if (!contractPrimary.has(opName)) + contractPrimary.set(opName, command); + } else if (!contractSecondary.has(opName)) { + contractSecondary.set(opName, command); + } + } else if (!otherMatch.has(opName)) { + otherMatch.set(opName, command); + } + } + } + cached = { canonical, contractPrimary, contractSecondary, otherMatch }; + operationCommandIndexes.set(entries, cached); + return cached; +}; + /** * The CLI command spelling (`rea `) that runs a contract operation, * or undefined when no CLI command answers it. Prefers the canonical @@ -224,31 +275,18 @@ export const cliCommandForOperation = ( cliName: string, recordedCommands?: Readonly>, ): string | undefined => { + const index = getOperationCommandIndex(entries); + const direct = index.canonical.get(operation); + if (direct !== undefined) return `${cliName} ${direct}`; const canonical = cliCommandFor(operation); - let contractPrimary: string | undefined; - let contractSecondary: string | undefined; - let otherMatch: string | undefined; - for (const entry of entries) { - const own = commandOwnOperations(entry); - const matched = own.find(({ name }) => name === operation); - if (matched === undefined) continue; - const command = - matched.selector === undefined - ? entry.name - : `${entry.name} ${matched.selector}`; - if (entry.name === canonical) return `${cliName} ${command}`; - if (entry.source.kind === "contract") { - if (own[0]?.name === operation) contractPrimary ??= command; - else contractSecondary ??= command; - } else { - otherMatch ??= command; - } - } if ( recordedCommands !== undefined && Object.hasOwn(recordedCommands, canonical) ) return `${cliName} ${canonical}`; - const selected = contractPrimary ?? contractSecondary ?? otherMatch; + const selected = + index.contractPrimary.get(operation) ?? + index.contractSecondary.get(operation) ?? + index.otherMatch.get(operation); return selected === undefined ? undefined : `${cliName} ${selected}`; }; diff --git a/src/dotnet/ManagedMemberInspector.ts b/src/dotnet/ManagedMemberInspector.ts index abd3c02686..82b01d9100 100644 --- a/src/dotnet/ManagedMemberInspector.ts +++ b/src/dotnet/ManagedMemberInspector.ts @@ -326,6 +326,15 @@ export const inspectManagedMembersBytes = ( declarationTypeName, }); appendManagedParseIssues(issues, memberRefs.issues); + const memberRefKinds = new Map(); + for (let index = 0; index < memberRefs.refs.length; index += 1) { + const memberRef = memberRefs.refs[index]!; + if ( + memberRef.signature.kind === "field" || + memberRef.signature.kind === "method" + ) + memberRefKinds.set(memberRef.token, memberRef.signature.kind); + } const methods = parseMethods({ bytes, layout, @@ -334,14 +343,7 @@ export const inspectManagedMembersBytes = ( signatureTypeName, displayTypeName, declarationTypeName, - memberRefKinds: new Map( - memberRefs.refs.flatMap((memberRef) => - memberRef.signature.kind === "field" || - memberRef.signature.kind === "method" - ? [[memberRef.token, memberRef.signature.kind] as const] - : [], - ), - ), + memberRefKinds, memberLists, selected, methodOffset, diff --git a/src/server/sessionStatusTool.ts b/src/server/sessionStatusTool.ts index 1403688573..50e68958a7 100644 --- a/src/server/sessionStatusTool.ts +++ b/src/server/sessionStatusTool.ts @@ -130,7 +130,9 @@ export const readSessionStatus = async ( if (!(cause instanceof AnalysisError)) throw cause; return err(cause); } - const statusObject = ownedJsonObjectSchema.parse(status); + const statusObject = status as unknown as Readonly< + Record + >; // Availability is derived from the complete provider facts: an // advertised composed tool still depends on direct operations the // registration hides. Advertisement narrows only the public report. diff --git a/tests/boundary/cli/jsonOutput.test.ts b/tests/boundary/cli/jsonOutput.test.ts index 9bc1b988fa..0b29157a31 100644 --- a/tests/boundary/cli/jsonOutput.test.ts +++ b/tests/boundary/cli/jsonOutput.test.ts @@ -1,4 +1,8 @@ -import { registerCliCommand } from "../../../src/cli/commandRegistry.js"; +import { + cliCommandForOperation, + cliCommands, + registerCliCommand, +} from "../../../src/cli/commandRegistry.js"; import { createCommandCli } from "../../support/cli/commandCli.js"; import { constants } from "node:buffer"; import { createHash } from "node:crypto"; @@ -521,6 +525,7 @@ describe("full-output token counts", () => { [], ["--full-output"], ["--filter-output", "missing"], + ["--filter-output", "raw_result"], ["--format", "jsonl"], ]) { const argv = ["fixture", "--json", ...flags]; @@ -541,7 +546,10 @@ describe("full-output token counts", () => { destination, ); if (output === undefined) throw new Error("Missing output surface"); - await output.write({ rows: ["hello", "雪😀"] }, metadata); + await output.write( + { rows: ["hello", "雪😀"], raw_result: null }, + metadata, + ); return Buffer.concat(chunks).toString("utf8"); }; const document = await render(false); @@ -785,3 +793,21 @@ describe("streamed CLI filtered selections", () => { }, ); }); + +it("refreshes the operation lookup after registering another command", () => { + const cli = createCommandCli(); + registerCliCommand(cli, "existing", { run: () => undefined }); + const commands = cliCommands(cli); + const lookup = () => + cliCommandForOperation("fixture_late_operation", commands, "rea"); + expect(lookup()).toBeUndefined(); + registerCliCommand( + cli, + "fixture-late-operation", + { run: () => undefined }, + { + operations: [{ name: "fixture_late_operation", flags: {} }], + }, + ); + expect(lookup()).toBe("rea fixture-late-operation"); +});