feat(coder): Phase 1-UX backend — agent attribution + agent-sessions route + opencode usage
pending_changes.agent stamped at every queue site (native -> 'boocode', dispatched external -> task.agent, manual RightRail -> NULL) + flows through listPending. New GET /api/sessions/:id/agent-sessions -> [{agent,status,has_session,last_active_at}] per (chat,agent). opencode warm server consumes session.next.step.ended, accumulating input_tokens/output_tokens/cost onto agent_sessions (new idempotent columns) via a pure opencode-usage.ts mapper. Tests: agent-sessions.routes (3) + opencode-usage (6); tsc clean.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -38,6 +38,7 @@ import type { ToolCallStatus } from '@agentclientprotocol/sdk';
|
||||
import type { Sql } from '../../db.js';
|
||||
import type { AcpToolSnapshot } from '../acp-tool-snapshot.js';
|
||||
import { armAbortGuard, noteTurnActivity, consumeTerminal } from './turn-guard.js';
|
||||
import { stepEndedToUsage, type StepUsage } from './opencode-usage.js';
|
||||
import type {
|
||||
AgentBackend,
|
||||
AgentEvent,
|
||||
@@ -282,6 +283,19 @@ export class OpenCodeServerBackend implements AgentBackend {
|
||||
st.activeTurn.onEvent({ type: 'tool_update', toolCall: snap });
|
||||
return;
|
||||
}
|
||||
// ─── per-step usage (U.6) — token/cost accounting for opencode sessions ──
|
||||
case 'session.next.step.ended': {
|
||||
const p = ev.properties;
|
||||
const st = this.byOpencodeId.get(p.sessionID);
|
||||
if (!st?.activeTurn) return;
|
||||
this.bumpActivity(st);
|
||||
// Accumulate this step's normalized usage onto the (chat_id, agent) row.
|
||||
// Fire-and-forget: a DB hiccup must not stall the turn. opencode emits this
|
||||
// once per LLM step, so a multi-tool turn sums several deltas.
|
||||
const usage = stepEndedToUsage(p);
|
||||
void this.accumulateUsage(st, usage);
|
||||
return;
|
||||
}
|
||||
// ─── message.part.* — terminal/post-hoc events (dedup gate) ────────────
|
||||
case 'message.part.delta': {
|
||||
const p = ev.properties;
|
||||
@@ -428,6 +442,33 @@ export class OpenCodeServerBackend implements AgentBackend {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── per-step usage persistence (U.6) ────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Accumulate one `session.next.step.ended`'s normalized usage onto the session's
|
||||
* agent_sessions row, keyed by the resumed `agent_session_id` (unique per active
|
||||
* row — the dispatcher's `(chat_id, agent)` lookup wrote it). Running totals for
|
||||
* the whole conversation context (not last-step). Zero-delta steps are skipped to
|
||||
* avoid a no-op write. Errors are swallowed: usage telemetry must never fail a turn.
|
||||
*/
|
||||
private async accumulateUsage(st: SessionState, u: StepUsage): Promise<void> {
|
||||
if (u.input === 0 && u.output === 0 && u.cost === 0) return;
|
||||
try {
|
||||
await this.sql`
|
||||
UPDATE agent_sessions SET
|
||||
input_tokens = input_tokens + ${u.input},
|
||||
output_tokens = output_tokens + ${u.output},
|
||||
cost = cost + ${u.cost}
|
||||
WHERE agent_session_id = ${st.agentSessionId}
|
||||
`;
|
||||
} catch (err) {
|
||||
this.log.warn(
|
||||
{ err: errMsg(err), agentSessionId: st.agentSessionId },
|
||||
'opencode-server: failed to persist step usage (non-fatal)',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ─── ensureSession: create-or-resume against agent_sessions (1.5) ────────────
|
||||
|
||||
async ensureSession(sessionId: string, opts: EnsureSessionOpts): Promise<AgentSessionHandle> {
|
||||
|
||||
Reference in New Issue
Block a user