GeezerVison is showing github.com — fix(shared): keep pull request summary fields in compact tool output … · patroza/t3code@00fa6cd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/t3ToolSummary.test.ts +61Lines changed: 61 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { compactT3McpToolOutput } from "@t3tools/shared/toolOutput"; | 1 | 2 | import { describe, expect, it } from "vite-plus/test"; | 2 | 3 |
| 3 | 4 | import { summarizeT3ToolCalls, type T3ToolSummaryCall } from "./t3ToolSummary.ts"; | @@ -102,6 +103,66 @@ describe("summarizeT3ToolCalls", () => { | 102 | 103 | ); | 103 | 104 | }); | 104 | 105 |
| | 106 | + it("preserves pull request summary outcomes after MCP tool output compaction", () => { | | 107 | + const createExisting = completed( | | 108 | + { title: "t" }, | | 109 | + compactT3McpToolOutput("t3-code.pull_request_create", { | | 110 | + number: 1, | | 111 | + repository: "o/r", | | 112 | + url: "https://github.com/o/r/pull/1", | | 113 | + created: false, | | 114 | + }), | | 115 | + ); | | 116 | + expect(summarizeT3ToolCalls("create-pr", [createExisting]).label).toBe( | | 117 | + "Found 1 open pull request", | | 118 | + ); | | 119 | + | | 120 | + const mergeQueued = completed( | | 121 | + { auto: false }, | | 122 | + compactT3McpToolOutput("t3-code.pull_request_merge", { number: 1, state: "open" }), | | 123 | + ); | | 124 | + const mergeLanded = completed( | | 125 | + { auto: true }, | | 126 | + compactT3McpToolOutput("t3-code.pull_request_merge", { number: 2, state: "merged" }), | | 127 | + ); | | 128 | + expect(summarizeT3ToolCalls("merge-pr", [mergeQueued]).label).toBe( | | 129 | + "Queued 1 pull request to merge", | | 130 | + ); | | 131 | + expect(summarizeT3ToolCalls("merge-pr", [mergeLanded]).label).toBe("Merged 1 pull request"); | | 132 | + | | 133 | + const guidesMixed = completed( | | 134 | + {}, | | 135 | + compactT3McpToolOutput("t3-code.write_pull_request_guides", { | | 136 | + guides: [ | | 137 | + { number: 1, outcome: "started", detail: null }, | | 138 | + { number: 2, outcome: "failed", detail: "Guides disabled" }, | | 139 | + ], | | 140 | + }), | | 141 | + ); | | 142 | + expect(summarizeT3ToolCalls("write-pr-guides", [guidesMixed])).toEqual({ | | 143 | + label: "Started 1 review guide, 1 failed", | | 144 | + failedCount: 1, | | 145 | + }); | | 146 | + | | 147 | + const rerunMixed = completed( | | 148 | + {}, | | 149 | + compactT3McpToolOutput("t3-code.ci_rerun", { | | 150 | + checks: [ | | 151 | + { name: "test", rerun: true, detail: null }, | | 152 | + { name: "external", rerun: false, detail: "Cannot rerun" }, | | 153 | + ], | | 154 | + }), | | 155 | + ); | | 156 | + const rerunEmpty = completed({}, compactT3McpToolOutput("t3-code.ci_rerun", { checks: [] })); | | 157 | + expect(summarizeT3ToolCalls("ci-rerun", [rerunMixed])).toEqual({ | | 158 | + label: "Re-ran 1 check, 1 refused", | | 159 | + failedCount: 1, | | 160 | + }); | | 161 | + expect(summarizeT3ToolCalls("ci-rerun", [rerunEmpty]).label).toBe( | | 162 | + "Found no failed checks to re-run", | | 163 | + ); | | 164 | + }); | | 165 | + | 105 | 166 | it("excludes failed and unfinished sends even if the provider reports completed", () => { | 106 | 167 | const calls: T3ToolSummaryCall[] = [ | 107 | 168 | completed({ threadId: "success" }, { messageId: "ok", threadId: "success" }), | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/t3ToolSummary.ts +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -70,6 +70,37 @@ export function t3ToolResultIndicatesFailure(output: unknown): boolean { | 70 | 70 | return readResult(output).failed; | 71 | 71 | } | 72 | 72 |
| | 73 | +// A row's static past-tense verb ("Opened a pull request") is wrong when the call reports another outcome. | | 74 | +export function t3ToolOutcomeLabel( | | 75 | + action: T3McpToolSummaryAction, | | 76 | + call: T3ToolSummaryCall, | | 77 | + resolve: MFunctionType = mFallback, | | 78 | +): string | undefined { | | 79 | + if ( | | 80 | + action !== "create-pr" && | | 81 | + action !== "merge-pr" && | | 82 | + action !== "write-pr-guides" && | | 83 | + action !== "ci-rerun" | | 84 | + ) { | | 85 | + return undefined; | | 86 | + } | | 87 | + const output = readResult(call.output).data; | | 88 | + const contradicted = | | 89 | + action === "create-pr" | | 90 | + ? output?.created === false | | 91 | + : action === "merge-pr" | | 92 | + ? output?.state === undefined | | 93 | + ? readInput(call.input)?.auto === true | | 94 | + : output.state !== "merged" | | 95 | + : action === "write-pr-guides" | | 96 | + ? Array.isArray(output?.guides) && | | 97 | + output.guides.some((guide) => asRecord(guide)?.outcome === "failed") | | 98 | + : Array.isArray(output?.checks) && | | 99 | + (output.checks.length === 0 || | | 100 | + output.checks.some((check) => asRecord(check)?.rerun !== true)); | | 101 | + return contradicted ? summarizeT3ToolCalls(action, [call], resolve).label : undefined; | | 102 | +} | | 103 | + | 73 | 104 | function countEntities(ids: ReadonlyArray<string | undefined>): number { | 74 | 105 | return ( | 75 | 106 | new Set(ids.filter((value) => value !== undefined)).size + | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/work-log/presentation.test.ts +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test"; | 2 | 2 |
| 3 | 3 | import { ThreadId, TurnItemId, type OrchestrationV2TurnItem } from "@t3tools/contracts"; | 4 | 4 | import * as DateTime from "effect/DateTime"; | | 5 | +import { compactT3McpToolOutput } from "@t3tools/shared/toolOutput"; | 5 | 6 |
| 6 | 7 | import { | 7 | 8 | resolveViewedImageAsset, | @@ -160,6 +161,28 @@ describe("resolveWorkEntryToolPresentation", () => { | 160 | 161 | ).toEqual({ displayName: "Taking a snapshot of the preview page", icon: "browser" }); | 161 | 162 | }); | 162 | 163 |
| | 164 | + it("names the reported outcome when it contradicts the row's verb", () => { | | 165 | + const row = (tool: string, output: unknown) => | | 166 | + resolveWorkEntryToolPresentation({ | | 167 | + label: `T3-code.${tool}`, | | 168 | + toolLifecycleStatus: "completed", | | 169 | + toolData: { output: compactT3McpToolOutput(`t3-code.${tool}`, output) }, | | 170 | + })?.displayName; | | 171 | + expect(row("pull_request_create", { number: 470, created: false })).toBe( | | 172 | + "Found 1 open pull request", | | 173 | + ); | | 174 | + expect(row("pull_request_create", { number: 470, created: true })).toBe( | | 175 | + "Opened a pull request", | | 176 | + ); | | 177 | + expect(row("ci_rerun", { number: 883, checks: [] })).toBe("Found no failed checks to re-run"); | | 178 | + expect(row("ci_rerun", { number: 883, checks: [{ name: "ci", rerun: true }] })).toBe( | | 179 | + "Re-ran CI", | | 180 | + ); | | 181 | + expect(row("pull_request_merge", { number: 1, state: "open" })).toBe( | | 182 | + "Queued 1 pull request to merge", | | 183 | + ); | | 184 | + }); | | 185 | + | 163 | 186 | it("uses the summary's state only when the provider omitted a lifecycle status", () => { | 164 | 187 | const entry = { label: "T3-code.preview_click" }; | 165 | 188 | expect(resolveWorkEntryToolPresentation(entry, "inProgress")?.displayName).toBe( | |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/work-log/presentation.ts +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,6 +26,7 @@ import { toolOutputIndicatesFailure } from "@t3tools/shared/toolOutput"; | 26 | 26 |
| 27 | 27 | import { | 28 | 28 | summarizeT3ToolCalls, | | 29 | + t3ToolOutcomeLabel, | 29 | 30 | t3ToolResultIndicatesFailure, | 30 | 31 | type T3ToolSummaryCall, | 31 | 32 | } from "@t3tools/client-runtime/t3ToolSummary"; | @@ -304,13 +305,24 @@ export function resolveWorkEntryToolPresentation( | 304 | 305 | workEntryToolName(entry), | 305 | 306 | workEntryToolInput(entry), | 306 | 307 | ); | | 308 | + const output = workEntryToolOutput(entry); | | 309 | + const failed = definition !== null && t3ToolResultIndicatesFailure(output); | 307 | 310 | const status = entry.toolLifecycleStatus ?? fallbackStatus; | 308 | | - return resolveT3McpToolPresentation( | | 311 | + const presentation = resolveT3McpToolPresentation( | 309 | 312 | definition, | 310 | | - definition && t3ToolResultIndicatesFailure(workEntryToolOutput(entry)) ? "failed" : status, | | 313 | + failed ? "failed" : status, | 311 | 314 | entry.toolData, | 312 | 315 | resolve, | 313 | 316 | ); | | 317 | + if (definition === null || presentation === null || failed || status !== "completed") { | | 318 | + return presentation; | | 319 | + } | | 320 | + const outcomeLabel = t3ToolOutcomeLabel( | | 321 | + definition.summaryAction, | | 322 | + { input: workEntryToolInput(entry), output, outcome: "completed" }, | | 323 | + resolve, | | 324 | + ); | | 325 | + return outcomeLabel === undefined ? presentation : { ...presentation, displayName: outcomeLabel }; | 314 | 326 | } | 315 | 327 |
| 316 | 328 | function asRecord(value: unknown): Record<string, unknown> | null { | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/toolOutput.test.ts +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -165,6 +165,36 @@ describe("compactDynamicToolOutput", () => { | 165 | 165 | threadId: "known", | 166 | 166 | }); | 167 | 167 | }); | | 168 | + | | 169 | + it("keeps pull request summary outcomes without private details", () => { | | 170 | + expect( | | 171 | + compactDynamicToolOutput({ | | 172 | + number: 42, | | 173 | + url: "https://github.com/acme/app/pull/42", | | 174 | + created: false, | | 175 | + state: "merged", | | 176 | + }), | | 177 | + ).toEqual({ created: false, state: "merged" }); | | 178 | + expect( | | 179 | + compactDynamicToolOutput({ | | 180 | + guides: [ | | 181 | + { number: 1, outcome: "started", detail: null }, | | 182 | + { number: 2, outcome: "failed", detail: "Private error" }, | | 183 | + ], | | 184 | + checks: [{ name: "external", rerun: false, detail: "Private refusal" }], | | 185 | + }), | | 186 | + ).toEqual({ | | 187 | + guides: [{ outcome: "started" }, { outcome: "failed" }], | | 188 | + checks: [{ rerun: false }], | | 189 | + }); | | 190 | + expect( | | 191 | + compactDynamicToolOutput({ | | 192 | + state: "draft", | | 193 | + guides: [{ outcome: "started" }, { outcome: "unknown" }], | | 194 | + checks: [{ rerun: true }, { name: "missing-rerun" }], | | 195 | + }), | | 196 | + ).toBeUndefined(); | | 197 | + }); | 168 | 198 | }); | 169 | 199 |
| 170 | 200 | describe("toolOutputIndicatesFailure", () => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/toolOutput.ts +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,6 +25,7 @@ const MAX_PARSED_BYTES = 16_384; | 25 | 25 | const MAX_METADATA_BYTES = 8_192; | 26 | 26 | const MAX_ID_LENGTH = 256; | 27 | 27 | const MAX_THREADS = 100; | | 28 | +const MAX_PULL_REQUEST_ITEMS = 100; | 28 | 29 | const MAX_CONTENT_BLOCKS = 32; | 29 | 30 | const MAX_ENVELOPE_DEPTH = 4; | 30 | 31 | const MAX_ENVELOPE_NODES = 128; | @@ -56,6 +57,10 @@ interface CompactToolOutput { | 56 | 57 | threads?: Array<{ threadId?: string; status?: "rolled_back" }>; | 57 | 58 | linkedCount?: number; | 58 | 59 | unlinkedCount?: number; | | 60 | + created?: boolean; | | 61 | + state?: "open" | "closed" | "merged"; | | 62 | + guides?: Array<{ outcome: "started" | "running" | "failed" }>; | | 63 | + checks?: Array<{ rerun: boolean }>; | 59 | 64 | sentMessage?: string; | 60 | 65 | } | 61 | 66 |
| @@ -127,6 +132,21 @@ function boundedId(value: unknown): string | undefined { | 127 | 132 | return Array.from(value).join(""); | 128 | 133 | } | 129 | 134 |
| | 135 | +// All or nothing: a partial list would make the summary report wrong counts. | | 136 | +function readEntries<T>( | | 137 | + value: unknown, | | 138 | + read: (entry: { readonly [key: string]: unknown }) => T | undefined, | | 139 | +): T[] | undefined { | | 140 | + if (!Array.isArray(value) || value.length > MAX_PULL_REQUEST_ITEMS) return undefined; | | 141 | + const entries: T[] = []; | | 142 | + for (const entry of value) { | | 143 | + const compact = Predicate.isObject(entry) ? read(entry) : undefined; | | 144 | + if (compact === undefined) return undefined; | | 145 | + entries.push(compact); | | 146 | + } | | 147 | + return entries; | | 148 | +} | | 149 | + | 130 | 150 | export function compactDynamicToolOutput(value: unknown): CompactToolOutput | undefined { | 131 | 151 | const budget: ResultReadBudget = { | 132 | 152 | remainingBytes: MAX_PARSED_BYTES, | @@ -146,6 +166,20 @@ export function compactDynamicToolOutput(value: unknown): CompactToolOutput | un | 146 | 166 | const count = data[key]; | 147 | 167 | if (typeof count === "number" && Number.isSafeInteger(count)) output[key] = count; | 148 | 168 | } | | 169 | + if (typeof data.created === "boolean") output.created = data.created; | | 170 | + if (data.state === "open" || data.state === "closed" || data.state === "merged") { | | 171 | + output.state = data.state; | | 172 | + } | | 173 | + const guides = readEntries(data.guides, (entry) => | | 174 | + entry.outcome === "started" || entry.outcome === "running" || entry.outcome === "failed" | | 175 | + ? ({ outcome: entry.outcome } as const) | | 176 | + : undefined, | | 177 | + ); | | 178 | + if (guides !== undefined) output.guides = guides; | | 179 | + const checks = readEntries(data.checks, (entry) => | | 180 | + typeof entry.rerun === "boolean" ? { rerun: entry.rerun } : undefined, | | 181 | + ); | | 182 | + if (checks !== undefined) output.checks = checks; | 149 | 183 | if ( | 150 | 184 | typeof data.sentMessage === "string" && | 151 | 185 | data.sentMessage.trim() !== "" && | @@ -196,6 +230,8 @@ export function compactDynamicToolOutput(value: unknown): CompactToolOutput | un | 196 | 230 | delete output.threads; | 197 | 231 | delete output.threadId; | 198 | 232 | delete output.status; | | 233 | + delete output.guides; | | 234 | + delete output.checks; | 199 | 235 | } | 200 | 236 | const app = output[MCP_APP_OUTPUT_KEY]; | 201 | 237 | if (app?.csp !== undefined && oversized()) { | |
|