Skip to content

Commit c26fd43

Browse files
committed
Persist agent output to database and replay on reconnection
1 parent 1a73d83 commit c26fd43

5 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎src/backend/db/database.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,28 @@ export const agentStmts = {
219219
},
220220
};
221221

222+
export const agentOutputStmts = {
223+
append: {
224+
run: (args: { $agentId: string; $data: string; $createdAt: number }): void => {
225+
db.query(
226+
`INSERT INTO agent_output (agent_id, data, created_at)
227+
VALUES ($agentId, $data, $createdAt)`,
228+
).run(args);
229+
},
230+
},
231+
list: {
232+
all: (agentId: string): string[] =>
233+
db
234+
.query<{ data: string }, [string]>(
235+
`SELECT data FROM agent_output
236+
WHERE agent_id = ?
237+
ORDER BY id`,
238+
)
239+
.all(agentId)
240+
.map((row) => row.data),
241+
},
242+
};
243+
222244
export const remoteStmts = {
223245
get: {
224246
get: (): RemoteConfig | null =>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Migration } from "../migrator.ts";
2+
3+
export default {
4+
name: "005_add_agent_output",
5+
up(db) {
6+
db.run(`
7+
CREATE TABLE IF NOT EXISTS agent_output (
8+
id INTEGER PRIMARY KEY AUTOINCREMENT,
9+
agent_id TEXT NOT NULL,
10+
data TEXT NOT NULL,
11+
created_at INTEGER NOT NULL,
12+
FOREIGN KEY (agent_id) REFERENCES agents(id) ON DELETE CASCADE
13+
)
14+
`);
15+
16+
db.run("CREATE INDEX IF NOT EXISTS idx_agent_output_agent_id_id ON agent_output(agent_id, id)");
17+
},
18+
} satisfies Migration;

‎src/backend/db/migrations/registry.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import m001 from "./001_baseline.ts";
33
import m002 from "./002_add_agent_title.ts";
44
import m003 from "./003_add_session_id.ts";
55
import m004 from "./004_add_agent_base_branch.ts";
6+
import m005 from "./005_add_agent_output.ts";
67

7-
export const migrations: Migration[] = [m001, m002, m003, m004];
8+
export const migrations: Migration[] = [m001, m002, m003, m004, m005];

‎src/backend/services/OrchestratorService.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function writeHookSettings(worktreePath: string, agentId: string): void {
6161

6262
const settings = {
6363
hooks: {
64+
SessionStart: [hookEntry("SessionStart")],
6465
Stop: [hookEntry("Stop")],
6566
Notification: [hookEntry("Notification")],
6667
TaskCreated: [hookEntry("TaskCreated")],

‎src/backend/ws/hub.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ServerWebSocket } from "bun";
2-
import { agentStmts } from "../db/index.ts";
2+
import { agentOutputStmts, agentStmts } from "../db/index.ts";
33
import { agentProcessManager } from "../services/AgentProcessManager.ts";
44
import { shellSessionManager } from "../services/ShellSessionManager.ts";
55
import { errorMeta, logger } from "../lib/logger.ts";
@@ -26,6 +26,12 @@ export function appendScrollback(agentId: string, data: string): void {
2626
const buf = agentScrollback.get(agentId)!;
2727
buf.push(data);
2828
if (buf.length > SCROLLBACK_LIMIT) buf.splice(0, buf.length - SCROLLBACK_LIMIT);
29+
30+
try {
31+
agentOutputStmts.append.run({ $agentId: agentId, $data: data, $createdAt: Date.now() });
32+
} catch (err) {
33+
log.error("failed to persist agent output", { agentId, ...errorMeta(err) });
34+
}
2935
}
3036

3137
export function appendShellScrollback(sessionId: string, data: string): void {
@@ -86,7 +92,7 @@ export const wsHandlers = {
8692
agentClients.get(agentId)!.add(ws);
8793

8894
// Replay buffered output — covers both "still running" and "already exited" cases
89-
const scrollback = agentScrollback.get(agentId) ?? [];
95+
const scrollback = agentScrollback.get(agentId) ?? agentOutputStmts.list.all(agentId);
9096
for (const chunk of scrollback) {
9197
if (ws.readyState === WebSocket.OPEN) ws.send(chunk);
9298
}

0 commit comments

Comments
 (0)