-
Notifications
You must be signed in to change notification settings - Fork 0
feat(inspect): add --filter flag to narrow timeline by method #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { mkdtempSync, writeFileSync, rmSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { inspectTranscript } from "../src/inspect.js"; | ||
| import type { Frame } from "../src/types.js"; | ||
|
|
||
| function frameLine(f: Frame): string { | ||
| return JSON.stringify(f); | ||
| } | ||
|
|
||
| describe("inspectTranscript --filter", () => { | ||
| let dir: string; | ||
|
|
||
| beforeEach(() => { | ||
| dir = mkdtempSync(join(tmpdir(), "mcprec-filter-")); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| function write(name: string, frames: Frame[]): string { | ||
| const file = join(dir, name); | ||
| writeFileSync(file, frames.map(frameLine).join("\n") + "\n", "utf8"); | ||
| return file; | ||
| } | ||
|
|
||
| const mixedFrames: Frame[] = [ | ||
| { t: 0.0, dir: "→", msg: { jsonrpc: "2.0", id: 1, method: "tools/list" } }, | ||
| { t: 0.1, dir: "←", msg: { jsonrpc: "2.0", id: 1, result: { tools: [] } } }, | ||
| { t: 0.2, dir: "→", msg: { jsonrpc: "2.0", id: 2, method: "tools/call", params: { name: "search_issues", arguments: { q: "is:open" } } } }, | ||
| { t: 0.9, dir: "←", msg: { jsonrpc: "2.0", id: 2, result: { content: [] } } }, | ||
| { t: 1.0, dir: "→", msg: { jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "get_file", arguments: { path: "README.md" } } } }, | ||
| { t: 1.2, dir: "←", msg: { jsonrpc: "2.0", id: 3, result: { content: "..." } } }, | ||
| { t: 1.3, dir: "→", msg: { jsonrpc: "2.0", id: 4, method: "ping" } }, | ||
| { t: 1.4, dir: "←", msg: { jsonrpc: "2.0", id: 4, result: {} } }, | ||
| ]; | ||
|
|
||
| it("shows only matching request frames and their responses when filter is set", async () => { | ||
| const file = write("mixed.jsonl", mixedFrames); | ||
| const out = await inspectTranscript(file, { filter: "tools/list" }); | ||
|
|
||
| // Should contain tools/list. | ||
| expect(out).toContain("tools/list"); | ||
| // Should not show tools/call or ping frames in the timeline. | ||
| const frameLines = out.split("\n").filter((l) => /^\s*\d+\.\d{3}s/.test(l)); | ||
| expect(frameLines.every((l) => !l.includes("tools/call") && !l.includes("ping"))).toBe(true); | ||
| // Should have exactly 2 frame lines: the request and its response. | ||
| expect(frameLines).toHaveLength(2); | ||
| }); | ||
|
|
||
| it("matches tools/call frames by tool name substring", async () => { | ||
| const file = write("mixed2.jsonl", mixedFrames); | ||
| const out = await inspectTranscript(file, { filter: "tools/call[search_issues]" }); | ||
|
|
||
| const frameLines = out.split("\n").filter((l) => /^\s*\d+\.\d{3}s/.test(l)); | ||
| // 1 request + 1 response. | ||
| expect(frameLines).toHaveLength(2); | ||
| expect(out).toContain("tools/call"); | ||
| // The get_file call should not appear in the timeline frame lines. | ||
| expect(frameLines.every((l) => !l.includes("get_file"))).toBe(true); | ||
| }); | ||
|
|
||
| it("matches all tools/call frames when filtering by the base method name", async () => { | ||
| const file = write("mixed3.jsonl", mixedFrames); | ||
| const out = await inspectTranscript(file, { filter: "tools/call" }); | ||
|
|
||
| const frameLines = out.split("\n").filter((l) => /^\s*\d+\.\d{3}s/.test(l)); | ||
| // 2 calls, each with a response: 4 frame lines. | ||
| expect(frameLines).toHaveLength(4); | ||
| // ping and tools/list should not appear in timeline frame lines. | ||
| expect(frameLines.every((l) => !l.includes("ping") && !l.includes("tools/list"))).toBe(true); | ||
| }); | ||
|
|
||
| it("shows no frame lines when filter matches nothing", async () => { | ||
| const file = write("nope.jsonl", mixedFrames); | ||
| const out = await inspectTranscript(file, { filter: "nonexistent_method" }); | ||
|
|
||
| const frameLines = out.split("\n").filter((l) => /^\s*\d+\.\d{3}s/.test(l)); | ||
| expect(frameLines).toHaveLength(0); | ||
| // Summary footer still reflects the full transcript. | ||
| expect(out).toContain("8 frames"); | ||
| }); | ||
|
|
||
| it("still shows full transcript stats in the footer when filter is active", async () => { | ||
| const file = write("stats.jsonl", mixedFrames); | ||
| const out = await inspectTranscript(file, { filter: "ping" }); | ||
|
|
||
| // All 8 frames and 4 pairs reported in the footer. | ||
| expect(out).toContain("8 frames"); | ||
| expect(out).toContain("4 request/response pairs"); | ||
| // Full method list still present. | ||
| expect(out).toContain("tools/list: 1"); | ||
| expect(out).toContain("ping: 1"); | ||
| }); | ||
|
|
||
| it("includes a filter note in the footer when filter is active", async () => { | ||
| const file = write("note.jsonl", mixedFrames); | ||
| const out = await inspectTranscript(file, { filter: "ping" }); | ||
|
|
||
| expect(out).toContain("filter: ping"); | ||
| }); | ||
|
|
||
| it("shows all frames when no filter is supplied (backward-compatible)", async () => { | ||
| const file = write("all.jsonl", mixedFrames); | ||
| const out = await inspectTranscript(file); | ||
|
|
||
| const frameLines = out.split("\n").filter((l) => /^\s*\d+\.\d{3}s/.test(l)); | ||
| expect(frameLines).toHaveLength(8); | ||
| // No filter note in the footer. | ||
| expect(out).not.toContain("filter:"); | ||
| }); | ||
|
|
||
| it("shows all frames when filter is an empty string", async () => { | ||
| const file = write("empty-filter.jsonl", mixedFrames); | ||
| const out = await inspectTranscript(file, { filter: "" }); | ||
|
|
||
| // An empty string is falsy: the filter is treated as absent. | ||
| const frameLines = out.split("\n").filter((l) => /^\s*\d+\.\d{3}s/.test(l)); | ||
| expect(frameLines).toHaveLength(8); | ||
| expect(out).not.toContain("filter:"); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a transcript reuses a JSON-RPC ID—such as multiple HTTP clients each starting with
id: 1, or a later request reusing an ID—this predicate includes every frame carrying that ID. Filtering for the later method therefore also shows responses belonging to earlier nonmatching requests (and can include reverse-direction requests with the same ID), so the filtered timeline is incorrect. Associate each matched request with its actual subsequent response using direction and position rather than collecting IDs globally.Useful? React with 👍 / 👎.