diff --git a/docs/guides/android/android-analysis.md b/docs/guides/android/android-analysis.md index 80bc93408aa..38105b4f575 100644 --- a/docs/guides/android/android-analysis.md +++ b/docs/guides/android/android-analysis.md @@ -258,6 +258,16 @@ a paged slice reports `coverage: "partial"`. Class/method operations use `class_ } ``` +For class searches, `offset` is the zero-based index among matches, and +`limit` is the maximum number of matches to return. Pass `--offset` and +`--limit` on the CLI or `offset` and `limit` in an MCP request. Omitting +both returns every match. Omitting `limit` with an offset returns the matching +suffix. Keep the same APK and query when advancing the offset by each page's +length. A page reports `partial` when it omits matching rows, including earlier +pages. A short or empty page ends forward paging. If the last page was full, +request the next offset to check for more. `duplicate_names` counts occurrences +within the returned page. + Use `inspect_android_class` to choose an overload. REA rejects an ambiguous method request rather than silently selecting the first match. Method reference queries accept the same `overload_index`; the bridge resolves the selected diff --git a/scripts/verify/advertised-schemas/tools/search_android_classes.json b/scripts/verify/advertised-schemas/tools/search_android_classes.json index 608865355d3..5112cab6ebc 100644 --- a/scripts/verify/advertised-schemas/tools/search_android_classes.json +++ b/scripts/verify/advertised-schemas/tools/search_android_classes.json @@ -1,6 +1,6 @@ { "schemas": { - "input compact": "db67ac2566308239", + "input compact": "59b120ee57b1b0a6", "output compact": "4bda7c3651bfc579" }, "patterns": { diff --git a/src/android/JadxAnalysis.ts b/src/android/JadxAnalysis.ts index d9f17ce8219..624bcc8c682 100644 --- a/src/android/JadxAnalysis.ts +++ b/src/android/JadxAnalysis.ts @@ -297,10 +297,18 @@ const searchClasses = async ( loaded: z.infer, engine: Engine, ): Promise => { + const matchOffset = request.input.offset ?? 0; + const matchLimit = request.input.limit; + const stopAfter = + matchLimit === undefined ? undefined : matchOffset + matchLimit; const matches: string[] = []; - const pageSize = 4096; + const maximumPageSize = 4096; let offset = 0; do { + const pageSize = + stopAfter === undefined + ? maximumPageSize + : Math.min(maximumPageSize, stopAfter - matches.length + 1); const page = await tools.listClasses({ query: request.input.query, offset, @@ -333,9 +341,18 @@ const searchClasses = async ( ); matches.push(...page.items); offset = page.next_offset; + if (stopAfter !== undefined && matches.length > stopAfter) break; } while (offset < loaded.class_count); + const pagedMatches = + matchLimit === undefined + ? matchOffset === 0 + ? matches + : matches.slice(matchOffset) + : matches.slice(matchOffset, matchOffset + matchLimit); + const truncated = + pagedMatches.length < matches.length || offset < loaded.class_count; const occurrences = new Map(); - for (const name of matches) + for (const name of pagedMatches) occurrences.set(name, (occurrences.get(name) ?? 0) + 1); const duplicateNames = [...occurrences] .filter(([, count]) => count > 1) @@ -344,9 +361,9 @@ const searchClasses = async ( engine, query: request.input.query, total_classes: loaded.class_count, - matches, + matches: pagedMatches, duplicate_names: duplicateNames, - coverage: "complete", + coverage: truncated ? "partial" : "complete", }); }; diff --git a/src/contracts/android/androidToolContracts.ts b/src/contracts/android/androidToolContracts.ts index 0c7918a3948..24fc212b272 100644 --- a/src/contracts/android/androidToolContracts.ts +++ b/src/contracts/android/androidToolContracts.ts @@ -123,7 +123,7 @@ export const ANDROID_TOOL_CONTRACTS = [ }), ), description: - "Search complete top-level decompiler class-name rows in an explicit local APK by case-sensitive substring. Empty query inventories all rows. Repeated names remain repeated and appear in duplicate_names; the provider accepts only class names for lookup, so these rows do not identify which same-name class node a later lookup selects. Retains artifact and engine identity; no APK execution.", + "Search top-level decompiler class-name rows in an explicit local APK by case-sensitive substring. Empty query matches every row. Use limit and offset to page through matches. Duplicate counts describe the returned rows; the provider accepts only class names for lookup, so these rows do not identify which same-name class node a later lookup selects. Retains artifact and engine identity; no APK execution.", kind: "android-provider", inputSchema: androidInputSchemas.search_android_classes, outputSchema: evidenceResultOf(androidResultSchemas.search_android_classes), diff --git a/src/domain/android/androidAnalysis.ts b/src/domain/android/androidAnalysis.ts index 75deee8c46f..8e493502863 100644 --- a/src/domain/android/androidAnalysis.ts +++ b/src/domain/android/androidAnalysis.ts @@ -43,6 +43,18 @@ export const androidInputSchemas = { query: z .string() .describe("Case-sensitive class-name substring; empty lists all classes"), + limit: z + .int() + .positive() + .optional() + .describe( + "Maximum number of matching class names to return; omit to return all matches", + ), + offset: z + .int() + .nonnegative() + .optional() + .describe("Zero-based starting match index; defaults to 0"), }), inspect_android_class: z.strictObject({ ...classTarget, ...mappingPath }), inspect_android_method: z.strictObject({ @@ -258,7 +270,7 @@ export const androidResultSchemas = { occurrences: z.int().min(2), }), ), - coverage: z.literal("complete"), + coverage: knownCoverageStatusSchema, }), inspect_android_class: z.strictObject({ ...common, diff --git a/tests/boundary/process/androidSearchEvidenceBudget.test.ts b/tests/boundary/process/androidSearchEvidenceBudget.test.ts index 4b572f7ed5a..fb6483dc898 100644 --- a/tests/boundary/process/androidSearchEvidenceBudget.test.ts +++ b/tests/boundary/process/androidSearchEvidenceBudget.test.ts @@ -60,7 +60,7 @@ const rawInventoryNames = (raw: unknown): number => return sum + items.length; }, 0); -test("delivers bounded search_android_classes Evidence while retaining the complete inventory", async () => { +const connectJadxSearchFixture = async () => { const { provider, apk, launches } = await createJadxProtocolFixture( "search-audit-metadata", undefined, @@ -84,22 +84,31 @@ test("delivers bounded search_android_classes Evidence while retaining the compl await client.close(); await server.close(); await session.close(); + await verifyJadxFixtureCleanup(launches); }); const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); await server.connect(serverTransport); await client.connect(clientTransport); - const search = async (query: string) => + const search = async ( + query: string, + paging: { readonly limit?: number; readonly offset?: number } = {}, + ) => parseEvidence( ( await client.callTool({ name: "search_android_classes", - arguments: { path: apk, query }, + arguments: { path: apk, query, ...paging }, }) ).structuredContent, ); + return { apk, client, search, session }; +}; + +test("delivers bounded search_android_classes Evidence while retaining the complete inventory", async () => { + const { search, session } = await connectJadxSearchFixture(); const single = await search("Target"); expect( androidResultSchemas.search_android_classes.parse(single.normalized_result), @@ -211,10 +220,56 @@ test("delivers bounded search_android_classes Evidence while retaining the compl }, }, ]); +}); - await client.close(); - await server.close(); - await verifyJadxFixtureCleanup(launches); +test("pages filtered class matches with bounded provider lookahead", async () => { + const { apk, client, search } = await connectJadxSearchFixture(); + + const page = await search("Class", { offset: 1, limit: 2 }); + expect(page.normalized_result).toMatchObject({ + matches: ["fixture.Class1", "fixture.Class2"], + coverage: "partial", + }); + expect(listClassCalls(page.raw_result).map(responsePage)).toMatchObject([ + { limit: 4, next_offset: 5, items_omitted: 4 }, + ]); + + const boundaryPage = await search("", { limit: 4096 }); + expect(boundaryPage.normalized_result).toMatchObject({ coverage: "partial" }); + expect( + listClassCalls(boundaryPage.raw_result).map(responsePage), + ).toMatchObject([ + { limit: 4096, next_offset: 4096 }, + { limit: 1, next_offset: 4097 }, + ]); + + const largePage = await search("Class7094", { limit: 20_000 }); + expect(largePage.normalized_result).toMatchObject({ + matches: ["fixture.Class7094"], + coverage: "complete", + }); + + const suffix = await search("Class709", { offset: 4 }); + expect(suffix.normalized_result).toMatchObject({ + matches: ["fixture.Class7093", "fixture.Class7094"], + coverage: "partial", + }); + const exhausted = await search("Target", { offset: 1, limit: 1 }); + expect(exhausted.normalized_result).toMatchObject({ + matches: [], + coverage: "partial", + }); + const rejected = await client.callTool({ + name: "search_android_classes", + arguments: { path: apk, query: "", limit: 0, offset: -1 }, + }); + expect(rejected.isError).toBe(true); + + const emptyPage = await search("fixture.Missing", { offset: 1 }); + expect(emptyPage.normalized_result).toMatchObject({ + matches: [], + coverage: "complete", + }); }); test("keeps the complete paged inventory inline without a retaining session", async () => {