Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3
agent-trace: migrate diff capture to message.updated and add model attribution#42
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
72fe765
agent-trace-plugin: Switch event capture from session.diff to message…
ivke995 ab2fefd
agent-trace: Add model_id payload and diff trace migration
ivke995 05ecbaf
agent-trace-db: Add model_id diff trace insert support
ivke995 cd4ea7a
hooks: Add model_id validation to diff-trace intake
ivke995 9d244c4
plans: Complete T05 validation and finalize add-diff-traces-model-id …
ivke995 616b41e
fix: remove export function from opencode plugin ts script
ivke995 14518dc
agent-trace: Propagate hunk model_id provenance into post-commit cont…
stefanskoricdev 7db4809
agent-trace: Fix builder runtime documentation drift
ivke995 cddc89d
config-lib: Consolidate plugin support under shared package root
ivke995 3692c46
agent-trace-db: Add agent_trace_id column to agent_traces table
ivke995 51b4152
biome: add vscode settings.json with default formatter
ivke995 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3,134 +3,148 @@ import type { Hooks, Plugin } from "@opencode-ai/plugin"; | ||
| type OpenCodeEvent = Parameters<NonNullable<Hooks["event"]>>[0]["event"]; | ||
| const REQUIRED_EVENTS = new Set(["session.diff"]); | ||
| const REQUIRED_EVENTS = new Set(["message.updated"]); | ||
| const ALL_CAPTURED_EVENTS = REQUIRED_EVENTS; | ||
| type TraceInput = { | ||
| event?: OpenCodeEvent; | ||
| event?: OpenCodeEvent; | ||
| }; | ||
| type DiffTracePayload = { | ||
| sessionID: string; | ||
| diff: string; | ||
| time: number; | ||
| sessionID: string; | ||
| diff: string; | ||
| time: number; | ||
| model_id: string; | ||
| }; | ||
| function extractDiffTracePayload( | ||
| input: TraceInput, | ||
| input: TraceInput, | ||
| ): DiffTracePayload | undefined { | ||
| const event = input.event; | ||
| if (event === undefined || event.type !== "session.diff") { | ||
| return undefined; | ||
| } | ||
| const properties = event.properties; | ||
| if (typeof properties !== "object" || properties === null) { | ||
| return undefined; | ||
| } | ||
| const propertiesObj = properties as Record<string, unknown>; | ||
| const sessionID = | ||
| typeof propertiesObj.sessionID === "string" && | ||
| propertiesObj.sessionID.trim().length > 0 | ||
| ? propertiesObj.sessionID | ||
| : "unknown"; | ||
| const diffEntries = propertiesObj.diff; | ||
| if (!Array.isArray(diffEntries) || diffEntries.length === 0) { | ||
| return undefined; | ||
| } | ||
| const patches: string[] = []; | ||
| for (const entry of diffEntries) { | ||
| if (typeof entry !== "object" || entry === null) { | ||
| continue; | ||
| } | ||
| const entryObj = entry as Record<string, unknown>; | ||
| const patch = | ||
| typeof entryObj.patch === "string" | ||
| ? entryObj.patch | ||
| : typeof entryObj.diff === "string" | ||
| ? entryObj.diff | ||
| : undefined; | ||
| if (patch !== undefined && patch.trim().length > 0) { | ||
| patches.push(patch); | ||
| } | ||
| } | ||
| if (patches.length === 0) { | ||
| return undefined; | ||
| } | ||
| return { | ||
| sessionID, | ||
| diff: patches.join("\n"), | ||
| time: Date.now(), | ||
| }; | ||
| const event = input.event; | ||
| if (event === undefined || event.type !== "message.updated") { | ||
| return undefined; | ||
| } | ||
| const properties = event.properties; | ||
| if (typeof properties !== "object" || properties === null) { | ||
| return undefined; | ||
| } | ||
| const propertiesObj = properties; | ||
| // Access properties.info (the Message object) | ||
| const info = propertiesObj.info; | ||
| if (typeof info !== "object" || info === null) { | ||
| return undefined; | ||
| } | ||
| const infoObj = info; | ||
| // Only capture user messages (filter out assistant, system, etc.) | ||
| if (infoObj.role !== "user") { | ||
| return undefined; | ||
| } | ||
| const sessionID = | ||
| typeof infoObj.sessionID === "string" && infoObj.sessionID.trim().length > 0 | ||
| ? infoObj.sessionID | ||
| : "unknown"; | ||
| const model = infoObj.model; | ||
| // Access info.summary?.diffs via explicit checks | ||
| const summary = infoObj.summary; | ||
| const diffEntries = | ||
| typeof summary === "object" && summary !== null ? summary.diffs : undefined; | ||
| if (!Array.isArray(diffEntries) || diffEntries.length === 0) { | ||
| return undefined; | ||
| } | ||
| const patches: string[] = []; | ||
| for (const entry of diffEntries) { | ||
| if (typeof entry !== "object" || entry === null) { | ||
| continue; | ||
| } | ||
| const entryObj = entry as { patch?: string }; | ||
| const patch = entryObj.patch || ""; | ||
| patches.push(patch); | ||
| } | ||
| if (patches.length === 0) { | ||
| return undefined; | ||
| } | ||
| return { | ||
| sessionID, | ||
| diff: patches.join("\n"), | ||
| time: Date.now(), | ||
| model_id: `${model.providerID}/${model.modelID}`, | ||
| }; | ||
| } | ||
| function shouldCaptureEvent(eventType: string): boolean { | ||
| return ALL_CAPTURED_EVENTS.has(eventType); | ||
| return ALL_CAPTURED_EVENTS.has(eventType); | ||
| } | ||
| async function buildTrace(repoRoot: string, input: TraceInput): Promise<void> { | ||
| const diffTracePayload = extractDiffTracePayload(input); | ||
| const diffTracePayload = extractDiffTracePayload(input); | ||
| if (diffTracePayload === undefined) { | ||
| return; | ||
| } | ||
| if (diffTracePayload === undefined) { | ||
| return; | ||
| } | ||
| await runDiffTraceHook(repoRoot, diffTracePayload); | ||
| await runDiffTraceHook(repoRoot, diffTracePayload); | ||
| } | ||
| async function runDiffTraceHook( | ||
| repoRoot: string, | ||
| payload: DiffTracePayload, | ||
| repoRoot: string, | ||
| payload: DiffTracePayload, | ||
| ): Promise<void> { | ||
| await new Promise<void>((resolve, reject) => { | ||
| const child = spawn("sce", ["hooks", "diff-trace"], { | ||
| cwd: repoRoot, | ||
| stdio: ["pipe", "ignore", "inherit"], | ||
| }); | ||
| child.on("error", reject); | ||
| child.on("close", (code, signal) => { | ||
| if (code === 0) { | ||
| resolve(); | ||
| return; | ||
| } | ||
| const reason = | ||
| signal === null ? `exit code ${String(code)}` : `signal ${signal}`; | ||
| reject( | ||
| new Error(`Command 'sce hooks diff-trace' failed with ${reason}.`), | ||
| ); | ||
| }); | ||
| child.stdin.end(`${JSON.stringify(payload)}\n`); | ||
| }); | ||
| await new Promise<void>((resolve, reject) => { | ||
| const child = spawn("sce", ["hooks", "diff-trace"], { | ||
| cwd: repoRoot, | ||
| stdio: ["pipe", "ignore", "inherit"], | ||
| }); | ||
| child.on("error", reject); | ||
| child.on("close", (code, signal) => { | ||
| if (code === 0) { | ||
| resolve(); | ||
| return; | ||
| } | ||
| const reason = | ||
| signal === null ? `exit code ${String(code)}` : `signal ${signal}`; | ||
| reject( | ||
| new Error(`Command 'sce hooks diff-trace' failed with ${reason}.`), | ||
| ); | ||
| }); | ||
| child.stdin.end(`${JSON.stringify(payload)}\n`); | ||
| }); | ||
| } | ||
| export const SceAgentTracePlugin: Plugin = async ({ directory, worktree }) => { | ||
| const repoRoot = worktree ?? directory ?? process.cwd(); | ||
| return { | ||
| event: async (input) => { | ||
| const eventType = | ||
| typeof input.event === "object" && | ||
| input.event !== null && | ||
| typeof input.event.type === "string" | ||
| ? input.event.type | ||
| : undefined; | ||
| if (eventType === undefined || !shouldCaptureEvent(eventType)) { | ||
| return; | ||
| } | ||
| await buildTrace(repoRoot, input); | ||
| }, | ||
| }; | ||
| const repoRoot = worktree ?? directory ?? process.cwd(); | ||
| return { | ||
| event: async (input) => { | ||
| const eventType = | ||
| typeof input.event === "object" && | ||
| input.event !== null && | ||
| typeof input.event.type === "string" | ||
| ? input.event.type | ||
| : undefined; | ||
| if (eventType === undefined || !shouldCaptureEvent(eventType)) { | ||
| return; | ||
| } | ||
| await buildTrace(repoRoot, input); | ||
| }, | ||
| }; | ||
| }; | ||
davidabram marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
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,6 @@ | ||
| { | ||
| "editor.formatOnSave": true, | ||
| "[typescript]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -3,9 +3,9 @@ | ||
| "files": { | ||
| "includes": [ | ||
| "npm/**", | ||
| "config/lib/bash-policy-plugin/**", | ||
| "config/lib/**", | ||
| "!npm/node_modules", | ||
| "!config/lib/bash-policy-plugin/node_modules" | ||
| "!config/lib/node_modules" | ||
davidabram marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ] | ||
| }, | ||
| "formatter": { | ||
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 @@ | ||
| ALTER TABLE diff_traces ADD COLUMN model_id TEXT; |
1 change: 1 addition & 0 deletions
1 cli/migrations/agent-trace/006_add_agent_traces_agent_trace_id.sql
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 @@ | ||
| ALTER TABLE agent_traces ADD COLUMN agent_trace_id TEXT; |
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.