Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 118 additions & 104 deletions .opencode/plugins/sce-agent-trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment thread
davidabram marked this conversation as resolved.

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);
},
};
};
Comment thread
davidabram marked this conversation as resolved.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.formatOnSave": true,
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
}
}
4 changes: 2 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Comment thread
davidabram marked this conversation as resolved.
]
},
"formatter": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE diff_traces ADD COLUMN model_id TEXT;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE agent_traces ADD COLUMN agent_trace_id TEXT;
27 changes: 23 additions & 4 deletions cli/src/services/agent_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ pub struct Contributor {
/// Classification of this hunk's origin.
#[serde(rename = "type")]
pub kind: HunkContributor,
/// Model provenance for this contributor; omitted when unknown.
#[serde(skip_serializing_if = "Option::is_none")]
pub model_id: Option<String>,
}

/// A single line range in the new file.
Expand Down Expand Up @@ -322,12 +325,28 @@ fn build_trace_file(
.hunks
.iter()
.map(|post_commit_hunk| {
let contributor = match intersection_file {
Some(ifile) => classify_hunk(post_commit_hunk, &ifile.hunks),
None => HunkContributor::Unknown,
let (contributor_kind, contributor_model_id) = match intersection_file {
Some(ifile) => {
let contributor_kind = classify_hunk(post_commit_hunk, &ifile.hunks);
let matched_intersection_hunk = ifile
.hunks
.iter()
.find(|h| h.old_start == post_commit_hunk.old_start);
let contributor_model_id = match contributor_kind {
HunkContributor::Ai | HunkContributor::Mixed => {
matched_intersection_hunk.and_then(|hunk| hunk.model_id.clone())
}
HunkContributor::Unknown => None,
};
(contributor_kind, contributor_model_id)
}
None => (HunkContributor::Unknown, None),
};
Conversation {
contributor: Contributor { kind: contributor },
contributor: Contributor {
kind: contributor_kind,
model_id: contributor_model_id,
},
ranges: vec![line_range_from_hunk(post_commit_file, post_commit_hunk)],
}
})
Expand Down
Loading