diff --git a/CHANGELOG.md b/CHANGELOG.md index c1373c2..d7bd8ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes per release tag. Most recent on top, ordered by tag creation date (which matches the git history). Tag names follow `vMAJOR.MINOR.PATCH-slug` — the slug describes what shipped, so the tag name alone is enough to recall the batch. +## v2.6.8-agent-attribution — 2026-05-31 + +v2.6 Phase 1-UX: agent attribution + switch affordances over the already-shipped `pending_changes.agent` column and `agent_sessions` table (read+display, no new backend capability). **Backend:** `pending_changes.agent` is now stamped at every queue site (native write tools → `'boocode'`, dispatched external agents → the task's agent, manual RightRail create → `NULL`) and flows through `listPending`; a new `GET /api/sessions/:id/agent-sessions` route returns `[{agent,status,has_session,last_active_at}]` per `(chat,agent)` for the session's chats; and the opencode warm-server backend consumes opencode's `session.next.step.ended` events, accumulating `input_tokens`/`output_tokens`/`cost` onto the `agent_sessions` row (new columns, idempotent). **Frontend:** the BooCoder DiffPanel renders a per-row agent badge (provider icon + label; `null` → "manual") with a "Changes from X, Y" note when a pending set spans multiple agents, and the AgentComposerBar shows a resumed / history / new-session chip beside the Provider picker — gated on an optional `sessionId` prop so BooChat is unaffected — driven by a new `useAgentSessions` hook that refetches on message-complete; `providerIcon` was extracted to a shared `components/coder/providerIcons.tsx`. Built by three parallel subagents over disjoint file sets; web + coder typecheck clean, 165 coder tests pass (9 new across `opencode-usage` and `agent-sessions.routes`). U.6's persisted token totals are conversation-cumulative and not yet surfaced in the UI (deferred). Implements the U.1–U.6 "remaining" plan from the v2.6 openspec reconciliation; Phase 2 (warm ACP goose/qwen) + Phase 3 (lifecycle hardening) remain. + ## v2.6.7-interrupt-guard — 2026-05-31 Fixes a post-interrupt correctness bug in the `v2.6.1-phase1-opencode` warm-server backend, made one-click reachable by `v2.6.5-panes-tabs-composer`'s Send→Stop composer. `opencode-server.ts` settled an in-flight turn on opencode's `session.idle`/`session.error` by calling `activeTurn.settle()` on whatever turn currently held the session slot — but opencode emits one trailing terminal event for a *cancelled* turn after `client.session.abort()`, and those events carry only a `sessionID` (no turn id). So after the user hit Stop and immediately sent another message, the aborted turn's orphan `session.idle` settled the *new* turn early as success (Paseo hit and fixed the same class in `1d38aac`). The fix adds a small pure guard (`turn-guard.ts`: `armAbortGuard`/`noteTurnActivity`/`consumeTerminal` over a per-session `swallowNextTerminal` flag): abort arms it, the next terminal is swallowed once, and a new turn's first delta self-heals the flag so a never-arriving orphan can't strand a real turn. Implemented test-first — three regression tests in `turn-guard.test.ts` (swallow-the-orphan, settle-when-no-abort, self-heal); full coder suite green (156 passed). This is the F.1 "fix-next" item from the v2.6 openspec reconciliation; Phase 1-UX / Phase 2 / Phase 3 remain. diff --git a/apps/coder/src/index.ts b/apps/coder/src/index.ts index bbd4dd7..a388f8b 100644 --- a/apps/coder/src/index.ts +++ b/apps/coder/src/index.ts @@ -25,6 +25,7 @@ import { setInferenceContext, clearInferenceContext } from './services/tools/inf import { registerMessageRoutes } from './routes/messages.js'; import { registerSkillRoutes } from './routes/skills.js'; import { registerPendingRoutes } from './routes/pending.js'; +import { registerAgentSessionRoutes } from './routes/agent-sessions.js'; import { registerTaskRoutes } from './routes/tasks.js'; import { registerInboxRoutes } from './routes/inbox.js'; import { registerStatsRoutes } from './routes/stats.js'; @@ -191,6 +192,7 @@ async function main() { registerMessageRoutes(app, sql, broker, inferenceApi); registerSkillRoutes(app, sql, broker, inferenceApi); registerPendingRoutes(app, sql); + registerAgentSessionRoutes(app, sql); registerTaskRoutes(app, sql, inferenceApi); registerInboxRoutes(app, sql); registerStatsRoutes(app, sql); diff --git a/apps/coder/src/routes/__tests__/agent-sessions.routes.test.ts b/apps/coder/src/routes/__tests__/agent-sessions.routes.test.ts new file mode 100644 index 0000000..518eb60 --- /dev/null +++ b/apps/coder/src/routes/__tests__/agent-sessions.routes.test.ts @@ -0,0 +1,75 @@ +import { describe, it, expect } from 'vitest'; +import Fastify, { type FastifyInstance } from 'fastify'; +import { registerAgentSessionRoutes } from '../agent-sessions.js'; +import type { Sql } from '../../db.js'; + +// Mock the porsager surface this route uses: a tagged-template `sql` dispatched by +// query substring. Two queries: the session-existence check and the agent_sessions +// JOIN. We return post-coercion shapes (booleans/strings) exactly as porsager would +// hand them to the route — `has_session` already a JS boolean, `last_active_at` a +// string|null — so the asserted JSON matches the API contract end-to-end. +interface MockState { + sessionExists: boolean; + rows: Array<{ agent: string; status: string; has_session: boolean; last_active_at: string | null }>; +} + +function mockSql(state: MockState): Sql { + return ((strings: TemplateStringsArray) => { + const q = strings.join(''); + if (q.includes('SELECT id FROM sessions')) { + return Promise.resolve(state.sessionExists ? [{ id: 'session-1' }] : []); + } + if (q.includes('FROM agent_sessions')) { + return Promise.resolve(state.rows); + } + return Promise.resolve([]); + }) as unknown as Sql; +} + +function buildApp(state: MockState): FastifyInstance { + const app = Fastify(); + registerAgentSessionRoutes(app, mockSql(state)); + return app; +} + +describe('GET /api/sessions/:id/agent-sessions', () => { + it('returns the per-(chat,agent) rows in the contracted shape', async () => { + const app = buildApp({ + sessionExists: true, + rows: [ + { agent: 'opencode', status: 'active', has_session: true, last_active_at: '2026-05-31T12:00:00.000Z' }, + { agent: 'goose', status: 'idle', has_session: false, last_active_at: null }, + ], + }); + const res = await app.inject({ method: 'GET', url: '/api/sessions/session-1/agent-sessions' }); + expect(res.statusCode).toBe(200); + const body = res.json(); + expect(Array.isArray(body)).toBe(true); + expect(body).toEqual([ + { agent: 'opencode', status: 'active', has_session: true, last_active_at: '2026-05-31T12:00:00.000Z' }, + { agent: 'goose', status: 'idle', has_session: false, last_active_at: null }, + ]); + // Contract field types. + expect(typeof body[0].agent).toBe('string'); + expect(typeof body[0].status).toBe('string'); + expect(typeof body[0].has_session).toBe('boolean'); + expect(body[1].last_active_at).toBeNull(); + await app.close(); + }); + + it('returns an empty array when the session has no agent_sessions rows', async () => { + const app = buildApp({ sessionExists: true, rows: [] }); + const res = await app.inject({ method: 'GET', url: '/api/sessions/session-1/agent-sessions' }); + expect(res.statusCode).toBe(200); + expect(res.json()).toEqual([]); + await app.close(); + }); + + it('404s when the session does not exist', async () => { + const app = buildApp({ sessionExists: false, rows: [] }); + const res = await app.inject({ method: 'GET', url: '/api/sessions/nope/agent-sessions' }); + expect(res.statusCode).toBe(404); + expect(res.json()).toEqual({ error: 'session not found' }); + await app.close(); + }); +}); diff --git a/apps/coder/src/routes/agent-sessions.ts b/apps/coder/src/routes/agent-sessions.ts new file mode 100644 index 0000000..7f1e019 --- /dev/null +++ b/apps/coder/src/routes/agent-sessions.ts @@ -0,0 +1,51 @@ +import type { FastifyInstance } from 'fastify'; +import type { Sql } from '../db.js'; + +// v2.6 Phase 1-UX (design §9b): chat-scoped "resumed vs new session" indicator. +// `agent_sessions` is keyed (chat_id, agent) — the tab/chat is the agent-context +// unit (P1.5-b). The route param is a SESSION id, so we resolve every chat in the +// session and return the union of their agent_sessions rows. A session with two +// opencode tabs yields two rows (one per chat); the frontend keys the chip per +// chat, but the wire shape is a flat per-(chat,agent) list. +// +// has_session = agent_session_id IS NOT NULL — i.e. a native backend session id +// (opencode/ACP) was created and stored, so switching back resumes rather than +// starts fresh. +export interface AgentSessionRow { + agent: string; + status: string; + has_session: boolean; + last_active_at: string | null; +} + +export function registerAgentSessionRoutes(app: FastifyInstance, sql: Sql): void { + // GET /api/sessions/:sessionId/agent-sessions — list the agent-session rows for + // every chat in the session (drives the AgentComposerBar resumed/new chip). + app.get<{ Params: { sessionId: string } }>( + '/api/sessions/:sessionId/agent-sessions', + async (req, reply) => { + const sessionId = req.params.sessionId; + + const session = await sql<{ id: string }[]>`SELECT id FROM sessions WHERE id = ${sessionId}`; + if (session.length === 0) { + reply.code(404); + return { error: 'session not found' }; + } + + // Join through chats so the session-scoped param resolves to its (chat,agent) + // rows. last_active_at first → the frontend reads the freshest activity. + const rows = await sql` + SELECT + a.agent AS agent, + a.status AS status, + (a.agent_session_id IS NOT NULL) AS has_session, + a.last_active_at AS last_active_at + FROM agent_sessions a + JOIN chats c ON c.id = a.chat_id + WHERE c.session_id = ${sessionId} + ORDER BY a.last_active_at DESC NULLS LAST, a.agent ASC + `; + return rows; + }, + ); +} diff --git a/apps/coder/src/routes/pending.ts b/apps/coder/src/routes/pending.ts index 9467126..6dc0a75 100644 --- a/apps/coder/src/routes/pending.ts +++ b/apps/coder/src/routes/pending.ts @@ -90,6 +90,8 @@ export function registerPendingRoutes(app: FastifyInstance, sql: Sql): void { parsed.data.file_path, parsed.data.content, projectRoot, + // Manual RightRail create — no agent staged it; renders as "manual". + null, ); return change; } catch (err) { diff --git a/apps/coder/src/schema.sql b/apps/coder/src/schema.sql index adf7c93..9513a03 100644 --- a/apps/coder/src/schema.sql +++ b/apps/coder/src/schema.sql @@ -131,6 +131,17 @@ END $$; -- v2.6: config fingerprint for stale-session detection (auto-recover on model change). ALTER TABLE agent_sessions ADD COLUMN IF NOT EXISTS config_hash TEXT; +-- v2.6 Phase 1-UX (U.6): opencode token/cost usage, ACCUMULATED per (chat_id, agent). +-- opencode's warm server emits `session.next.step.ended` once per LLM step (several +-- per multi-tool turn) carrying {tokens{input,output,reasoning,cache},cost}. We sum +-- each step's normalized {input,output,cost} onto the session row — running totals +-- for the whole conversation context, not last-step. Backend-only; no route/UI yet. +-- input_tokens folds in cache read+write; output_tokens folds in reasoning (see +-- backends/opencode-usage.ts). Defaults 0 so accumulation (col + delta) is well-defined. +ALTER TABLE agent_sessions ADD COLUMN IF NOT EXISTS input_tokens BIGINT NOT NULL DEFAULT 0; +ALTER TABLE agent_sessions ADD COLUMN IF NOT EXISTS output_tokens BIGINT NOT NULL DEFAULT 0; +ALTER TABLE agent_sessions ADD COLUMN IF NOT EXISTS cost DOUBLE PRECISION NOT NULL DEFAULT 0; + -- ─── P1.5-b (corrected): worktrees entity + re-key agent_sessions to (chat_id, agent) ─── -- The TAB (a chat) is the context unit: two opencode tabs in one session = two -- independent contexts sharing one worktree. So agent_sessions keys on diff --git a/apps/coder/src/services/backends/__tests__/opencode-usage.test.ts b/apps/coder/src/services/backends/__tests__/opencode-usage.test.ts new file mode 100644 index 0000000..005b18c --- /dev/null +++ b/apps/coder/src/services/backends/__tests__/opencode-usage.test.ts @@ -0,0 +1,51 @@ +import { describe, it, expect } from 'vitest'; +import { stepEndedToUsage } from '../opencode-usage.js'; + +describe('stepEndedToUsage (U.6)', () => { + it('folds cache read+write into input and reasoning into output', () => { + const u = stepEndedToUsage({ + cost: 0.0123, + tokens: { input: 100, output: 50, reasoning: 20, cache: { read: 10, write: 5 } }, + }); + expect(u).toEqual({ input: 115, output: 70, cost: 0.0123 }); + }); + + it('handles a step with no cache and no reasoning', () => { + const u = stepEndedToUsage({ + cost: 0, + tokens: { input: 8, output: 4, reasoning: 0, cache: { read: 0, write: 0 } }, + }); + expect(u).toEqual({ input: 8, output: 4, cost: 0 }); + }); + + it('is defensive against a missing tokens block', () => { + const u = stepEndedToUsage({ cost: 0.5 } as never); + expect(u).toEqual({ input: 0, output: 0, cost: 0.5 }); + }); + + it('is defensive against undefined props', () => { + expect(stepEndedToUsage(undefined)).toEqual({ input: 0, output: 0, cost: 0 }); + }); + + it('drops NaN / negative noise to zero rather than poisoning the accumulated total', () => { + const u = stepEndedToUsage({ + cost: Number.NaN, + tokens: { + input: -5, + output: Number.NaN, + reasoning: 3, + cache: { read: Number.POSITIVE_INFINITY, write: 2 }, + }, + }); + // input: (-5→0) + (Inf→0) + 2 = 2; output: (NaN→0) + 3 = 3; cost: NaN→0 + expect(u).toEqual({ input: 2, output: 3, cost: 0 }); + }); + + it('rounds fractional token counts', () => { + const u = stepEndedToUsage({ + cost: 1.5, + tokens: { input: 10.6, output: 4.4, reasoning: 0, cache: { read: 0, write: 0 } }, + }); + expect(u).toEqual({ input: 11, output: 4, cost: 1.5 }); + }); +}); diff --git a/apps/coder/src/services/backends/opencode-server.ts b/apps/coder/src/services/backends/opencode-server.ts index 4b62456..627b220 100644 --- a/apps/coder/src/services/backends/opencode-server.ts +++ b/apps/coder/src/services/backends/opencode-server.ts @@ -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 { + 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 { diff --git a/apps/coder/src/services/backends/opencode-usage.ts b/apps/coder/src/services/backends/opencode-usage.ts new file mode 100644 index 0000000..95fb793 --- /dev/null +++ b/apps/coder/src/services/backends/opencode-usage.ts @@ -0,0 +1,77 @@ +/** + * v2.6 Phase 1-UX (U.6) — pure mapper for opencode's per-step usage event. + * + * opencode's warm server emits `session.next.step.ended` once per completed LLM + * step (so a multi-tool turn fires it several times). Its `properties` carry the + * step's token + cost accounting: + * + * { + * timestamp: number; + * sessionID: string; + * finish: string; + * cost: number; // USD for this step + * tokens: { + * input: number; output: number; reasoning: number; + * cache: { read: number; write: number }; + * }; + * snapshot?: string; + * } + * + * (Verified against @opencode-ai/sdk@1.15.12 — `EventSessionNextStepEnded` in + * `dist/v2/gen/types.gen.d.ts`, a member of the `Event` union the SSE loop + * switches on.) + * + * We normalize to the review's target slice `{input, output, cost}` (the + * provider-agnostic `AgentUsage` shape lands later). cache read/write tokens are + * folded into `input` so the persisted input count reflects the real context the + * model billed for; reasoning tokens are folded into `output` since that's what + * the provider counts them as for generation. This keeps the persisted totals a + * faithful sum of what opencode reported, without inventing extra columns yet. + */ + +/** The `properties` shape of a `session.next.step.ended` event (subset we read). */ +export interface StepEndedProps { + cost: number; + tokens: { + input: number; + output: number; + reasoning: number; + cache: { read: number; write: number }; + }; +} + +/** Normalized per-step usage delta persisted onto the agent_sessions row. */ +export interface StepUsage { + input: number; + output: number; + cost: number; +} + +/** Coerce a possibly-missing/NaN number to a non-negative finite integer (tokens). */ +function n(v: unknown): number { + const x = typeof v === 'number' ? v : Number(v); + return Number.isFinite(x) && x > 0 ? Math.round(x) : 0; +} + +/** Coerce a possibly-missing/NaN number to a non-negative finite float (cost USD). */ +function f(v: unknown): number { + const x = typeof v === 'number' ? v : Number(v); + return Number.isFinite(x) && x > 0 ? x : 0; +} + +/** + * Map a `session.next.step.ended` payload → the normalized `{input, output, cost}` + * delta. Defensive against missing/partial token blocks (the wire is trusted but + * we never want a NaN to poison the accumulated DB total). `input` folds in cache + * read+write; `output` folds in reasoning. + */ +export function stepEndedToUsage(props: Partial | undefined): StepUsage { + const t = props?.tokens; + const cacheRead = n(t?.cache?.read); + const cacheWrite = n(t?.cache?.write); + return { + input: n(t?.input) + cacheRead + cacheWrite, + output: n(t?.output) + n(t?.reasoning), + cost: f(props?.cost), + }; +} diff --git a/apps/coder/src/services/dispatcher.ts b/apps/coder/src/services/dispatcher.ts index 94816e6..465969d 100644 --- a/apps/coder/src/services/dispatcher.ts +++ b/apps/coder/src/services/dispatcher.ts @@ -441,10 +441,11 @@ export function createDispatcher(deps: Deps): { start(): void; stop(): Promise { const resolved = resolveWritePath(projectRoot, filePath); const diff = JSON.stringify({ old: oldString, new: newString }); const [row] = await sql` - INSERT INTO pending_changes (session_id, task_id, file_path, operation, diff) - VALUES (${sessionId}, ${taskId}, ${resolved}, 'edit', ${diff}) + INSERT INTO pending_changes (session_id, task_id, file_path, operation, diff, agent) + VALUES (${sessionId}, ${taskId}, ${resolved}, 'edit', ${diff}, ${agent}) RETURNING * `; return row!; @@ -53,12 +61,15 @@ export async function queueCreate( filePath: string, content: string, projectRoot: string, + // See queueEdit: defaults to 'boocode' for the native write tools; the manual + // RightRail create route passes null. + agent: string | null = 'boocode', ): Promise { const resolved = resolveWritePath(projectRoot, filePath); const [row] = await sql` - INSERT INTO pending_changes (session_id, task_id, file_path, operation, diff) - VALUES (${sessionId}, ${taskId}, ${resolved}, 'create', ${content}) + INSERT INTO pending_changes (session_id, task_id, file_path, operation, diff, agent) + VALUES (${sessionId}, ${taskId}, ${resolved}, 'create', ${content}, ${agent}) RETURNING * `; return row!; @@ -70,12 +81,14 @@ export async function queueDelete( taskId: string | null, filePath: string, projectRoot: string, + // See queueEdit: defaults to 'boocode' for the native write tools. + agent: string | null = 'boocode', ): Promise { const resolved = resolveWritePath(projectRoot, filePath); const [row] = await sql` - INSERT INTO pending_changes (session_id, task_id, file_path, operation, diff) - VALUES (${sessionId}, ${taskId}, ${resolved}, 'delete', '') + INSERT INTO pending_changes (session_id, task_id, file_path, operation, diff, agent) + VALUES (${sessionId}, ${taskId}, ${resolved}, 'delete', '', ${agent}) RETURNING * `; return row!; diff --git a/apps/web/src/api/client.ts b/apps/web/src/api/client.ts index e17ca5f..da3dff8 100644 --- a/apps/web/src/api/client.ts +++ b/apps/web/src/api/client.ts @@ -25,6 +25,17 @@ import type { WorkspaceState, } from './types'; +// v2.6 Phase 1-UX §9b: chat-scoped agent-session rows. Returned by +// GET /api/coder/sessions/:id/agent-sessions; drives the AgentComposerBar +// resumed/new-session chip via useAgentSessions. `has_session` is true when a +// resumable backend session id exists for that agent in the chat. +export interface AgentSessionInfo { + agent: string; + status: string; + has_session: boolean; + last_active_at: string | null; +} + export class ApiError extends Error { constructor( public status: number, @@ -363,6 +374,11 @@ export const api = { request( `/api/coder/sessions/${sessionId}/messages${chatId ? `?chat_id=${encodeURIComponent(chatId)}` : ''}`, ), + // v2.6 Phase 1-UX §9b: per-(chat,agent) backend-session state for the + // resumed/new-session chip. Chat-scoped (NOT foldable into the project-level + // provider snapshot). Proxied to boocoder at /api/sessions/:id/agent-sessions. + agentSessions: (sessionId: string) => + request(`/api/coder/sessions/${sessionId}/agent-sessions`), skillInvoke: ( sessionId: string, paneId: string, diff --git a/apps/web/src/components/AgentComposerBar.tsx b/apps/web/src/components/AgentComposerBar.tsx index 2dab033..aa74d11 100644 --- a/apps/web/src/components/AgentComposerBar.tsx +++ b/apps/web/src/components/AgentComposerBar.tsx @@ -1,9 +1,10 @@ import { useEffect, useMemo, useRef, useState } from 'react'; -import { Check, ChevronDown, RefreshCw, Loader2, Shield, Brain, Bird, Bot, Dog, Terminal as TermIcon } from 'lucide-react'; -import { ClaudeIcon, OpenCodeIcon } from '@/components/icons/ProviderIcons'; +import { Check, ChevronDown, RefreshCw, Loader2, Shield, Brain, Bot } from 'lucide-react'; import { api } from '@/api/client'; import type { AgentSessionConfig, ProviderSnapshotEntry, AgentCommand } from '@/api/types'; import { useProviderSnapshot, refreshProviderSnapshot } from '@/hooks/useProviderSnapshot'; +import { providerIcon } from '@/components/coder/providerIcons'; +import { useAgentSessions } from '@/hooks/useAgentSessions'; import { DropdownMenu, DropdownMenuContent, @@ -172,9 +173,36 @@ interface Props { onChange: (next: AgentSessionConfig) => void; onProviderCommandsChange?: (commands: AgentCommand[]) => void; connected?: boolean; + // v2.6 Phase 1-UX §9b: chat id for the resumed/new-session chip. Optional so + // BooChat and any other AgentComposerBar caller renders no chip and is + // otherwise unaffected. When present + connected + the chat has ≥1 prior + // turn, a chip right of the Provider picker reports whether switching to the + // current provider resumes an agent session, replays history (boocode), or + // starts fresh. + sessionId?: string; + // True once the chat has at least one prior turn — gates the chip so it stays + // hidden on a brand-new chat. Defaults to false (no chip). + hasPriorTurn?: boolean; } -export function AgentComposerBar({ projectPath, value, onChange, onProviderCommandsChange, connected }: Props) { +// Relative-time formatter for the resumed-chip title (e.g. "3m ago"). +function relativeTime(iso: string | null): string { + if (!iso) return 'unknown'; + const then = new Date(iso).getTime(); + if (Number.isNaN(then)) return 'unknown'; + const diffMs = Date.now() - then; + if (diffMs < 0) return 'just now'; + const sec = Math.floor(diffMs / 1000); + if (sec < 60) return 'just now'; + const min = Math.floor(sec / 60); + if (min < 60) return `${min}m ago`; + const hr = Math.floor(min / 60); + if (hr < 24) return `${hr}h ago`; + const day = Math.floor(hr / 24); + return `${day}d ago`; +} + +export function AgentComposerBar({ projectPath, value, onChange, onProviderCommandsChange, connected, sessionId, hasPriorTurn }: Props) { const allEntries = useProviderSnapshot(projectPath); // 5.5 — the composer picker only offers ENABLED providers that are ready (or // still loading). Disabled (enabled:false) and unavailable/error providers are @@ -186,6 +214,13 @@ export function AgentComposerBar({ projectPath, value, onChange, onProviderComma ); const [refreshing, setRefreshing] = useState(false); + // v2.6 Phase 1-UX §9b: chat-scoped agent-session rows for the resumed/new + // chip. Hook is unconditional (hooks rule); it self-no-ops when sessionId is + // undefined or the chat has no prior turn, so BooChat callers cost nothing. + const { sessions: agentSessions } = useAgentSessions( + sessionId && hasPriorTurn ? sessionId : undefined, + ); + const hydratedRef = useRef(false); useEffect(() => { @@ -294,21 +329,30 @@ export function AgentComposerBar({ projectPath, value, onChange, onProviderComma ); } - const providerIcon = (name: string) => { - switch (name) { - case 'claude': return ; - case 'opencode': return ; - case 'goose': return ; - case 'qwen': return ; - default: return ; - } - }; - const providerOptions = entries.map((e) => ({ id: e.name, label: e.label })); const modeOptions = (currentEntry?.modes ?? []).map((m) => ({ id: m.id, label: m.label })); const modelOptions = (currentEntry?.models ?? []).map((m) => ({ id: m.id, label: m.label })); const thinkingOpts = thinkingOptions.map((t) => ({ id: t.id, label: t.label })); + // v2.6 Phase 1-UX §9b: resumed / history / new-session chip. Only meaningful + // when this is a real chat (sessionId), the WS is connected, and the chat has + // ≥1 prior turn — otherwise render nothing so fresh chats and non-coder + // callers stay clean. + const sessionRow = agentSessions.find((s) => s.agent === value.provider); + const sessionChip: { label: string; title: string } | null = + sessionId && hasPriorTurn && connected + ? value.provider === 'boocode' + ? // Native boocode never holds an agent_sessions row — it reconstructs + // the conversation from the chat transcript each turn. + { label: 'history', title: 'BooCode replays the chat transcript each turn' } + : sessionRow?.has_session + ? { + label: 'resumed', + title: `Resuming ${value.provider} · last active ${relativeTime(sessionRow.last_active_at)}`, + } + : { label: 'new session', title: `${value.provider} starts a fresh session this turn` } + : null; + return (
+ {sessionChip && ( + + {sessionChip.label} + + )} ; + case 'opencode': + return ; + case 'goose': + return ; + case 'qwen': + return ; + default: + return ; + } +} + +/** + * Human label for a provider/agent name. `null` → "manual" (a RightRail-staged + * change with no dispatching agent, per §9a). Unknown names pass through + * verbatim so a future provider still reads sensibly. + */ +export function providerLabel(name: string | null): string { + switch (name) { + case null: + return 'manual'; + case 'boocode': + return 'BooCode'; + case 'opencode': + return 'opencode'; + case 'claude': + return 'Claude'; + case 'goose': + return 'goose'; + case 'qwen': + return 'Qwen'; + default: + return name; + } +} diff --git a/apps/web/src/components/panes/CoderPane.tsx b/apps/web/src/components/panes/CoderPane.tsx index fd06cf3..266107c 100644 --- a/apps/web/src/components/panes/CoderPane.tsx +++ b/apps/web/src/components/panes/CoderPane.tsx @@ -16,6 +16,8 @@ import { toast } from 'sonner'; import { isSlashCommandToken, mergeCommandsByName, parseSlashInput, slashQuery } from '@/lib/slash-command'; import { mergeWireToolCall } from '@/lib/coder-tools'; import { CoderMessageList, type CoderTimelineWire } from '@/components/panes/CoderMessageList'; +import { providerIcon, providerLabel } from '@/components/coder/providerIcons'; +import { refreshAgentSessions } from '@/hooks/useAgentSessions'; import { cn } from '@/lib/utils'; // --------------------------------------------------------------------------- @@ -56,6 +58,10 @@ interface PendingChange { diff?: string; new_content?: string; status: 'pending' | 'approved' | 'rejected'; + // v2.6 Phase 1-UX §9a: which agent staged this change. 'boocode' for native + // write tools, the dispatched agent for worktree edits, null for a manual + // RightRail-staged create (renders as a neutral "manual" badge). + agent: string | null; } interface Props { @@ -394,6 +400,15 @@ function DiffPanel({ }) { const pending = changes.filter((c) => c.status === 'pending'); + // v2.6 Phase 1-UX §9a: when pending changes span >1 distinct agent, surface a + // one-line "Changes from , " note so mixed provenance is obvious. Null + // (manual) counts as its own bucket and renders as "manual". + const distinctAgents = Array.from(new Set(pending.map((c) => c.agent))); + const mixedNote = + distinctAgents.length > 1 + ? `Changes from ${distinctAgents.map((a) => providerLabel(a)).join(', ')}` + : null; + return (
@@ -410,6 +425,11 @@ function DiffPanel({
+ {mixedNote && ( +
+ {mixedNote} +
+ )}
{pending.length === 0 ? (
@@ -420,14 +440,25 @@ function DiffPanel({ {pending.map((change) => (
- + + + {providerIcon(change.agent, 11)} + {providerLabel(change.agent)} + - {change.file_path} + {change.file_path}