repoburg 1.3.199 → 1.3.202
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agent-pane/backend/data.db +0 -0
- package/agent-pane/backend/data.db-shm +0 -0
- package/agent-pane/backend/data.db-wal +0 -0
- package/agent-pane/backend/dist/index.js +1658 -277
- package/agent-pane/backend/src/claude/claude-provider.test.ts +393 -3
- package/agent-pane/backend/src/claude/claude-provider.ts +53 -50
- package/agent-pane/backend/src/db/index.ts +12 -0
- package/agent-pane/backend/src/index.ts +5 -2
- package/agent-pane/backend/src/integration/threads-claude-fork.test.ts +241 -0
- package/agent-pane/backend/src/providers/index.ts +3 -1
- package/agent-pane/backend/src/providers/runtime-provider.ts +6 -0
- package/agent-pane/backend/src/repoburg/action-mapping.test.ts +122 -0
- package/agent-pane/backend/src/repoburg/action-mapping.ts +294 -0
- package/agent-pane/backend/src/repoburg/agent-runtime-client.test.ts +271 -0
- package/agent-pane/backend/src/repoburg/agent-runtime-client.ts +285 -0
- package/agent-pane/backend/src/repoburg/backend-resolver.test.ts +356 -0
- package/agent-pane/backend/src/repoburg/backend-resolver.ts +261 -0
- package/agent-pane/backend/src/repoburg/repoburg-client.test.ts +180 -0
- package/agent-pane/backend/src/repoburg/repoburg-client.ts +194 -0
- package/agent-pane/backend/src/repoburg/repoburg-provider.test.ts +780 -0
- package/agent-pane/backend/src/repoburg/repoburg-provider.ts +471 -0
- package/agent-pane/backend/src/repoburg/turn-projector.test.ts +621 -0
- package/agent-pane/backend/src/repoburg/turn-projector.ts +637 -0
- package/agent-pane/backend/src/routes/providers.test.ts +18 -1
- package/agent-pane/backend/src/routes/providers.ts +19 -1
- package/agent-pane/backend/src/routes/sessions.test.ts +215 -1
- package/agent-pane/backend/src/routes/sessions.ts +82 -9
- package/agent-pane/backend/src/routes/threads.ts +21 -6
- package/agent-pane/backend/src/routes/turns.test.ts +45 -1
- package/agent-pane/frontend/dist/assets/index-CJxD4YmJ.js +370 -0
- package/agent-pane/frontend/dist/assets/{index-Dwwhx7YN.css → index-r-xwfOKQ.css} +1 -1
- package/agent-pane/frontend/dist/index.html +2 -2
- package/agent-pane/frontend/src/api/sessions.test.ts +1 -0
- package/agent-pane/frontend/src/api/websocket.test.ts +2 -2
- package/agent-pane/frontend/src/api/websocket.ts +3 -1
- package/agent-pane/frontend/src/components/AskUserItem.test.tsx +143 -0
- package/agent-pane/frontend/src/components/AskUserItem.tsx +207 -0
- package/agent-pane/frontend/src/components/CommentableText.test.tsx +196 -0
- package/agent-pane/frontend/src/components/CommentableText.tsx +109 -21
- package/agent-pane/frontend/src/components/IdeWorkspace.test.tsx +39 -0
- package/agent-pane/frontend/src/components/IdeWorkspace.tsx +28 -2
- package/agent-pane/frontend/src/components/ResizeHandle.tsx +29 -0
- package/agent-pane/frontend/src/components/TurnContent.test.tsx +79 -0
- package/agent-pane/frontend/src/components/TurnContent.tsx +49 -3
- package/agent-pane/frontend/src/components/panels/ThreadsPanel.tsx +35 -3
- package/agent-pane/frontend/src/components/panels/TurnsPanel.test.tsx +48 -0
- package/agent-pane/frontend/src/components/panels/TurnsPanel.tsx +7 -2
- package/agent-pane/frontend/src/constants/models.ts +37 -0
- package/agent-pane/frontend/src/hooks/useResizable.test.ts +97 -0
- package/agent-pane/frontend/src/hooks/useResizable.ts +75 -0
- package/agent-pane/frontend/src/store/store.test.ts +54 -0
- package/agent-pane/frontend/src/store/uiSlice.ts +50 -0
- package/agent-pane/frontend/src/store/wsSlice.ts +10 -3
- package/agent-pane/frontend/src/styles/app.css +26 -1
- package/agent-pane/frontend/src/test/component-helpers.ts +2 -0
- package/agent-pane/frontend/src/test/fixtures.ts +1 -0
- package/agent-pane/frontend/src/types/index.ts +2 -0
- package/agent-pane/frontend/src/utils/duration.test.ts +1 -0
- package/agent-pane/shared/types.ts +17 -0
- package/agent-pane-bug-repro-no-agent-reply.png +0 -0
- package/askuser-after-step5-postgres-selected.png +0 -0
- package/askuser-after-step7-answer-sent.png +0 -0
- package/askuser-card-visible.png +0 -0
- package/backend/dist/src/agent-runtime/agent-runtime-event-emitter.d.ts +36 -0
- package/backend/dist/src/agent-runtime/agent-runtime-event-emitter.js +199 -0
- package/backend/dist/src/agent-runtime/agent-runtime-event-emitter.js.map +1 -0
- package/backend/dist/src/agent-runtime/agent-runtime.controller.d.ts +11 -0
- package/backend/dist/src/agent-runtime/agent-runtime.controller.js +91 -0
- package/backend/dist/src/agent-runtime/agent-runtime.controller.js.map +1 -0
- package/backend/dist/src/agent-runtime/agent-runtime.module.d.ts +2 -0
- package/backend/dist/src/agent-runtime/agent-runtime.module.js +26 -0
- package/backend/dist/src/agent-runtime/agent-runtime.module.js.map +1 -0
- package/backend/dist/src/agent-runtime/agent-runtime.types.d.ts +92 -0
- package/backend/dist/src/agent-runtime/agent-runtime.types.js +3 -0
- package/backend/dist/src/agent-runtime/agent-runtime.types.js.map +1 -0
- package/backend/dist/src/app.controller.d.ts +1 -0
- package/backend/dist/src/app.module.js +4 -0
- package/backend/dist/src/app.module.js.map +1 -1
- package/backend/dist/src/app.service.d.ts +1 -0
- package/backend/dist/src/app.service.js +4 -1
- package/backend/dist/src/app.service.js.map +1 -1
- package/backend/dist/src/interactive-chat/chat.module.js +4 -0
- package/backend/dist/src/interactive-chat/chat.module.js.map +1 -1
- package/backend/dist/src/interactive-chat/chat.service.d.ts +7 -1
- package/backend/dist/src/interactive-chat/chat.service.js +54 -7
- package/backend/dist/src/interactive-chat/chat.service.js.map +1 -1
- package/backend/dist/src/llm-orchestration/action-handlers/invoke-subagent.handler.js +12 -1
- package/backend/dist/src/llm-orchestration/action-handlers/invoke-subagent.handler.js.map +1 -1
- package/backend/dist/src/llm-orchestration/hooks/frontend-notification.hook.d.ts +3 -1
- package/backend/dist/src/llm-orchestration/hooks/frontend-notification.hook.js +20 -2
- package/backend/dist/src/llm-orchestration/hooks/frontend-notification.hook.js.map +1 -1
- package/backend/dist/src/llm-orchestration/llm-orchestration.interfaces.d.ts +1 -0
- package/backend/dist/src/llm-orchestration/llm-orchestration.interfaces.js +1 -0
- package/backend/dist/src/llm-orchestration/llm-orchestration.interfaces.js.map +1 -1
- package/backend/dist/src/llm-orchestration/llm-orchestration.module.js +2 -0
- package/backend/dist/src/llm-orchestration/llm-orchestration.module.js.map +1 -1
- package/backend/dist/src/llm-orchestration/llm-turn-processor.service.d.ts +4 -1
- package/backend/dist/src/llm-orchestration/llm-turn-processor.service.js +37 -2
- package/backend/dist/src/llm-orchestration/llm-turn-processor.service.js.map +1 -1
- package/backend/dist/src/llm-provider/ollama-llm.provider.d.ts +1 -0
- package/backend/dist/src/llm-provider/ollama-llm.provider.js +19 -1
- package/backend/dist/src/llm-provider/ollama-llm.provider.js.map +1 -1
- package/backend/dist/src/llm-responses/llm-responses.module.js +4 -0
- package/backend/dist/src/llm-responses/llm-responses.module.js.map +1 -1
- package/backend/dist/src/llm-responses/llm-responses.service.d.ts +4 -1
- package/backend/dist/src/llm-responses/llm-responses.service.js +52 -2
- package/backend/dist/src/llm-responses/llm-responses.service.js.map +1 -1
- package/backend/dist/tsconfig.build.tsbuildinfo +1 -1
- package/daemon/dist/api/services.js +4 -0
- package/daemon/dist/index.js +0 -0
- package/daemon/dist/serviceManager.js +30 -11
- package/daemon/dist/serviceManager.spec.js +26 -0
- package/daemon/dist/workspace-validation.js +63 -0
- package/daemon/dist/workspace-validation.spec.js +63 -0
- package/package.json +1 -1
- package/r2-step4-apple-banana.png +0 -0
- package/r2-step5-after-rewind-apple-last.png +0 -0
- package/r2-step7-apple-cherry.png +0 -0
- package/r2-step8-cherry-menu-no-restore.png +0 -0
- package/r3-step3-live-task-card-running.png +0 -0
- package/r3-step4-after-completion.png +0 -0
- package/r3-step5-persistence-after-reload.png +0 -0
- package/r3-step6-tasks-tab-card.png +0 -0
- package/reverify-answered-state-answer-sent.png +0 -0
- package/reverify-card-visible-after-completion.png +0 -0
- package/rewind-step3-apple-banana-turns.png +0 -0
- package/rewind-step5-ui-unchanged-after-confirmed-rewind.png +0 -0
- package/rewind-step6-apple-menu-no-restore.png +0 -0
- package/rewind-step7-final-turnlist-unchanged.png +0 -0
- package/rewind-step7-negative-error-toast.png +0 -0
- package/rewind-step7-negative-error-toast2.png +0 -0
- package/subagent-r2-step5-turn-after-reload-no-card.png +0 -0
- package/subagent-r2-step6-tasks-panel-card.png +0 -0
- package/subagent-step3-tasks-tab-done-card.png +0 -0
- package/subagent-step4-turn-stuck-no-cards.png +0 -0
- package/agent-pane/frontend/dist/assets/index-BYo-lnxm.js +0 -362
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Repoburg provider — drives the repoburg backend's full agent engine
|
|
3
|
+
* through TWO channels:
|
|
4
|
+
*
|
|
5
|
+
* - CONTROL (HTTP): session create/patch, input, cancel, models — via
|
|
6
|
+
* RepoburgHttpClient (repoburg-client.ts).
|
|
7
|
+
* - MESSAGES (SSE): the agent-runtime stream (agent-runtime-client.ts) —
|
|
8
|
+
* Claude-Code-style ordered messages projected onto agent-pane items by
|
|
9
|
+
* TurnProjector (turn-projector.ts). The terminal `result` message ends
|
|
10
|
+
* the turn; ordering is guaranteed by the backend's emission points.
|
|
11
|
+
*
|
|
12
|
+
* The repoburg session UUID is persisted in `sessions.external_thread_id`.
|
|
13
|
+
* Backend lifecycle: the backend instance per workspace is resolved via the
|
|
14
|
+
* platform daemon (backend-resolver.ts) — started/restarted on demand; the
|
|
15
|
+
* user never starts a repoburg backend manually.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type Database from 'better-sqlite3';
|
|
19
|
+
import type { ModelInfo } from '@agentpane/shared';
|
|
20
|
+
import { getDb } from '../db/index.ts';
|
|
21
|
+
import { broadcast } from '../ws/index.ts';
|
|
22
|
+
import { RepoburgHttpClient } from './repoburg-client.ts';
|
|
23
|
+
import type { RepoburgRuntimeClient } from './repoburg-client.ts';
|
|
24
|
+
import { resolveBackendUrl, invalidateBackend } from './backend-resolver.ts';
|
|
25
|
+
import { setSessionStatus } from './action-mapping.ts';
|
|
26
|
+
import { TurnProjector } from './turn-projector.ts';
|
|
27
|
+
import { streamAgentRuntimeMessages } from './agent-runtime-client.ts';
|
|
28
|
+
import type { Session, Turn } from '../db/schema.ts';
|
|
29
|
+
import type { RuntimeProvider } from '../providers/runtime-provider.ts';
|
|
30
|
+
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
// Reasoning-effort mapping (root enum ↔ Ollama think levels)
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
/** Reasoning levels offered for non-Ollama runtime models (full root enum). */
|
|
36
|
+
const REASONING_LEVELS: { effort: string; label: string }[] = [
|
|
37
|
+
{ effort: 'low', label: 'Low' },
|
|
38
|
+
{ effort: 'medium', label: 'Medium' },
|
|
39
|
+
{ effort: 'high', label: 'High' },
|
|
40
|
+
{ effort: 'xhigh', label: 'Extra High' },
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
/** Ollama `think` accepts only low|medium|high|max|bool — xhigh/minimal/none
|
|
44
|
+
* are rejected with 400. Offer only the expressible levels. */
|
|
45
|
+
const OLLAMA_REASONING_LEVELS = REASONING_LEVELS.filter((l) => l.effort !== 'xhigh');
|
|
46
|
+
|
|
47
|
+
/** Map an agent-pane reasoning level onto a root-backend ReasoningEffort the
|
|
48
|
+
* runtime's LLM accepts. The root backend forwards reasoning_effort verbatim
|
|
49
|
+
* into the provider call (Ollama `think`), so unsafe levels are clamped or
|
|
50
|
+
* omitted: ollama xhigh→high, minimal/none→omit; other providers verbatim. */
|
|
51
|
+
export function mapReasoningEffort(
|
|
52
|
+
modelId: string | null | undefined,
|
|
53
|
+
effort: string | null | undefined,
|
|
54
|
+
): string | undefined {
|
|
55
|
+
if (!effort) return undefined;
|
|
56
|
+
if (!['xhigh', 'high', 'medium', 'low', 'minimal', 'none'].includes(effort)) return undefined;
|
|
57
|
+
if (modelId?.startsWith('ollama/')) {
|
|
58
|
+
if (effort === 'xhigh') return 'high';
|
|
59
|
+
if (effort === 'low' || effort === 'medium' || effort === 'high') return effort;
|
|
60
|
+
return undefined; // minimal/none are not expressible via Ollama `think`
|
|
61
|
+
}
|
|
62
|
+
return effort;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ---------------------------------------------------------------------------
|
|
66
|
+
// State
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
|
|
69
|
+
/** One running agent-pane turn on a repoburg session. */
|
|
70
|
+
interface ActiveTurn {
|
|
71
|
+
turnId: string;
|
|
72
|
+
projector: TurnProjector;
|
|
73
|
+
/** Aborts the current SSE connection (stall watchdog / interrupt). */
|
|
74
|
+
sseAbort: AbortController;
|
|
75
|
+
/** true once the user interrupted — stream loop stops reconnecting. */
|
|
76
|
+
cancelled: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Per-agent-pane-session runtime state. */
|
|
80
|
+
interface SessionEntry {
|
|
81
|
+
agentPaneSessionId: string;
|
|
82
|
+
repoburgSessionId: string;
|
|
83
|
+
client: RepoburgRuntimeClient;
|
|
84
|
+
baseUrl: string;
|
|
85
|
+
/** Last model id pushed to the runtime session (mid-session change detection). */
|
|
86
|
+
modelId: string | null;
|
|
87
|
+
/** Last mapped reasoning effort pushed to the runtime session. */
|
|
88
|
+
reasoningEffort: string | undefined;
|
|
89
|
+
activeTurn: ActiveTurn | null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const sessionMap = new Map<string, SessionEntry>();
|
|
93
|
+
|
|
94
|
+
/** Bytes-level stream watchdog: no bytes (message OR heartbeat) within this
|
|
95
|
+
* window → abort the SSE connection and reconnect. */
|
|
96
|
+
const STALLED_STREAM_MS = 60_000;
|
|
97
|
+
|
|
98
|
+
/** Connection-level failures that mean the backend URL must be re-resolved. */
|
|
99
|
+
function isConnectionError(err: unknown): boolean {
|
|
100
|
+
const message = String((err as Error)?.message ?? '');
|
|
101
|
+
return /fetch failed|ECONNREFUSED|ENOTFOUND|ECONNRESET|ETIMEDOUT|EHOSTUNREACH|socket hang up/i.test(
|
|
102
|
+
message,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
// Session management
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
|
|
110
|
+
/** Create or reuse the runtime session for an agent-pane session. */
|
|
111
|
+
async function ensureSession(
|
|
112
|
+
session: Session,
|
|
113
|
+
client: RepoburgRuntimeClient,
|
|
114
|
+
baseUrl: string,
|
|
115
|
+
): Promise<SessionEntry> {
|
|
116
|
+
const existing = sessionMap.get(session.id);
|
|
117
|
+
if (existing) return existing;
|
|
118
|
+
|
|
119
|
+
if (session.external_thread_id) {
|
|
120
|
+
const entry: SessionEntry = {
|
|
121
|
+
agentPaneSessionId: session.id,
|
|
122
|
+
repoburgSessionId: session.external_thread_id,
|
|
123
|
+
client,
|
|
124
|
+
baseUrl,
|
|
125
|
+
modelId: session.model ?? null,
|
|
126
|
+
// undefined forces a re-push on the next turn — heals sessions created
|
|
127
|
+
// before unsafe effort values (e.g. ollama xhigh) were clamped.
|
|
128
|
+
reasoningEffort: undefined,
|
|
129
|
+
activeTurn: null,
|
|
130
|
+
};
|
|
131
|
+
sessionMap.set(session.id, entry);
|
|
132
|
+
return entry;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// First turn — create a runtime session carrying model + reasoning config.
|
|
136
|
+
const mappedEffort = mapReasoningEffort(session.model, session.model_reasoning_effort);
|
|
137
|
+
const created = await client.createSession({
|
|
138
|
+
session_title: session.title ?? undefined,
|
|
139
|
+
model_id: session.model ?? undefined,
|
|
140
|
+
reasoning_effort: mappedEffort,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const db = getDb();
|
|
144
|
+
const now = new Date().toISOString();
|
|
145
|
+
db.prepare('UPDATE sessions SET external_thread_id = ?, updated_at = ? WHERE id = ?').run(
|
|
146
|
+
created.id,
|
|
147
|
+
now,
|
|
148
|
+
session.id,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const entry: SessionEntry = {
|
|
152
|
+
agentPaneSessionId: session.id,
|
|
153
|
+
repoburgSessionId: created.id,
|
|
154
|
+
client,
|
|
155
|
+
baseUrl,
|
|
156
|
+
modelId: session.model ?? null,
|
|
157
|
+
reasoningEffort: mappedEffort,
|
|
158
|
+
activeTurn: null,
|
|
159
|
+
};
|
|
160
|
+
sessionMap.set(session.id, entry);
|
|
161
|
+
return entry;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ---------------------------------------------------------------------------
|
|
165
|
+
// Turn lifecycle
|
|
166
|
+
// ---------------------------------------------------------------------------
|
|
167
|
+
|
|
168
|
+
/** Mark a turn failed and reset the session — used before a projector exists. */
|
|
169
|
+
function failTurnBeforeStart(db: Database.Database, sessionId: string, turnId: string, error: string): void {
|
|
170
|
+
const now = new Date().toISOString();
|
|
171
|
+
db.prepare('UPDATE turns SET status = ?, error = ?, updated_at = ? WHERE id = ?').run(
|
|
172
|
+
'failed',
|
|
173
|
+
error,
|
|
174
|
+
now,
|
|
175
|
+
turnId,
|
|
176
|
+
);
|
|
177
|
+
setSessionStatus(db, sessionId, 0, 'failed', now);
|
|
178
|
+
broadcast('turn:failed', { sessionId, turnId, error });
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Fail the active turn — projector closed, stream stopped, state dropped. */
|
|
182
|
+
function failTurn(entry: SessionEntry, error: string): void {
|
|
183
|
+
const active = entry.activeTurn;
|
|
184
|
+
if (!active) return;
|
|
185
|
+
entry.activeTurn = null;
|
|
186
|
+
active.projector.close();
|
|
187
|
+
active.sseAbort.abort();
|
|
188
|
+
|
|
189
|
+
const db = getDb();
|
|
190
|
+
const now = new Date().toISOString();
|
|
191
|
+
db.prepare('UPDATE turns SET status = ?, error = ?, updated_at = ? WHERE id = ?').run(
|
|
192
|
+
'failed',
|
|
193
|
+
error,
|
|
194
|
+
now,
|
|
195
|
+
active.turnId,
|
|
196
|
+
);
|
|
197
|
+
db.prepare('UPDATE items SET status = ?, updated_at = ? WHERE turn_id = ? AND status = ?').run(
|
|
198
|
+
'failed',
|
|
199
|
+
now,
|
|
200
|
+
active.turnId,
|
|
201
|
+
'in_progress',
|
|
202
|
+
);
|
|
203
|
+
setSessionStatus(db, entry.agentPaneSessionId, 0, 'failed', now);
|
|
204
|
+
broadcast('turn:failed', { sessionId: entry.agentPaneSessionId, turnId: active.turnId, error });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* SSE loop — consume the agent-runtime message stream until the projector
|
|
209
|
+
* finalizes the turn. Handles reconnects (connection drops) and the
|
|
210
|
+
* bytes-level stall watchdog (silent connection → abort + reconnect).
|
|
211
|
+
*/
|
|
212
|
+
async function runStreamLoop(entry: SessionEntry, projector: TurnProjector): Promise<void> {
|
|
213
|
+
while (entry.activeTurn?.projector === projector && !projector.isDone) {
|
|
214
|
+
const active = entry.activeTurn;
|
|
215
|
+
const controller = new AbortController();
|
|
216
|
+
active.sseAbort = controller;
|
|
217
|
+
|
|
218
|
+
// Bytes watchdog — heartbeats count as activity (server alive).
|
|
219
|
+
let stallTimer: ReturnType<typeof setTimeout> | null = null;
|
|
220
|
+
const resetStall = (): void => {
|
|
221
|
+
if (stallTimer) clearTimeout(stallTimer);
|
|
222
|
+
stallTimer = setTimeout(() => controller.abort(), STALLED_STREAM_MS);
|
|
223
|
+
};
|
|
224
|
+
resetStall();
|
|
225
|
+
|
|
226
|
+
const reason = await streamAgentRuntimeMessages(
|
|
227
|
+
entry.baseUrl,
|
|
228
|
+
entry.repoburgSessionId,
|
|
229
|
+
(message) => projector.handle(message),
|
|
230
|
+
{ signal: controller.signal, onActivity: resetStall },
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
if (stallTimer) clearTimeout(stallTimer);
|
|
234
|
+
|
|
235
|
+
if (projector.isDone || entry.activeTurn?.projector !== projector) return;
|
|
236
|
+
if (reason === 'session-not-found') {
|
|
237
|
+
failTurn(entry, 'Repoburg session no longer exists on the backend.');
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (reason === 'aborted') {
|
|
241
|
+
if (active.cancelled) return; // user interrupt — already finalized.
|
|
242
|
+
continue; // stall-forced abort → reconnect with a fresh signal.
|
|
243
|
+
}
|
|
244
|
+
// reason === 'result' implies projector.isDone (handled above).
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Start a repoburg turn. Fire-and-forget — messages stream via SSE. */
|
|
249
|
+
export async function startTurn(session: Session, turn: Turn): Promise<void> {
|
|
250
|
+
const db = getDb();
|
|
251
|
+
|
|
252
|
+
// Phase 0: resolve the workspace's backend instance (daemon-managed).
|
|
253
|
+
let client: RepoburgRuntimeClient;
|
|
254
|
+
let baseUrl: string;
|
|
255
|
+
try {
|
|
256
|
+
const resolved = await resolveBackendUrl(session.cwd ?? process.cwd());
|
|
257
|
+
if (!resolved) throw new Error('No repoburg backend resolved.');
|
|
258
|
+
baseUrl = resolved.baseUrl;
|
|
259
|
+
client = new RepoburgHttpClient(baseUrl);
|
|
260
|
+
} catch (err) {
|
|
261
|
+
failTurnBeforeStart(db, session.id, turn.id, `Failed to resolve repoburg backend: ${(err as Error).message}`);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Pinned-session cross-check (belt and braces): a daemon reshuffle may
|
|
266
|
+
// resolve a different URL than the one pinned in the session entry. The
|
|
267
|
+
// pinned entry wins until the session ends (deliberate) — log the mismatch.
|
|
268
|
+
const pinned = sessionMap.get(session.id);
|
|
269
|
+
if (pinned && pinned.baseUrl !== baseUrl) {
|
|
270
|
+
console.warn(
|
|
271
|
+
`[repoburg] resolved backend ${baseUrl} differs from pinned ${pinned.baseUrl} for session ${session.id} — keeping the pinned entry.`,
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Phase 1: ensure the runtime session exists.
|
|
276
|
+
let entry: SessionEntry;
|
|
277
|
+
try {
|
|
278
|
+
entry = await ensureSession(session, client, baseUrl);
|
|
279
|
+
} catch (err) {
|
|
280
|
+
if (isConnectionError(err)) invalidateBackend(baseUrl);
|
|
281
|
+
failTurnBeforeStart(db, session.id, turn.id, `Failed to create repoburg session: ${(err as Error).message}`);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Phase 2: push model/reasoning changes (mid-session switches in agent-pane).
|
|
286
|
+
const wantedModel = session.model ?? null;
|
|
287
|
+
const wantedEffort = mapReasoningEffort(session.model, session.model_reasoning_effort);
|
|
288
|
+
if (wantedModel !== entry.modelId || wantedEffort !== entry.reasoningEffort) {
|
|
289
|
+
try {
|
|
290
|
+
await entry.client.patchSession(entry.repoburgSessionId, {
|
|
291
|
+
model_id: wantedModel ?? undefined,
|
|
292
|
+
reasoning_effort: wantedEffort,
|
|
293
|
+
});
|
|
294
|
+
entry.modelId = wantedModel;
|
|
295
|
+
entry.reasoningEffort = wantedEffort;
|
|
296
|
+
} catch (err) {
|
|
297
|
+
console.error(`[repoburg] patchSession failed for ${entry.repoburgSessionId}:`, err);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Phase 3: mark running + open the message stream.
|
|
302
|
+
const now = new Date().toISOString();
|
|
303
|
+
db.prepare('UPDATE turns SET status = ?, updated_at = ? WHERE id = ?').run('running', now, turn.id);
|
|
304
|
+
setSessionStatus(db, session.id, 1, 'running', now);
|
|
305
|
+
broadcast('turn:started', { sessionId: session.id, turnId: turn.id });
|
|
306
|
+
|
|
307
|
+
const projector = new TurnProjector({
|
|
308
|
+
agentPaneSessionId: session.id,
|
|
309
|
+
turnId: turn.id,
|
|
310
|
+
repoburgSessionId: entry.repoburgSessionId,
|
|
311
|
+
client: entry.client,
|
|
312
|
+
});
|
|
313
|
+
entry.activeTurn = { turnId: turn.id, projector, sseAbort: new AbortController(), cancelled: false };
|
|
314
|
+
|
|
315
|
+
// Phase 4: consume the SSE message stream (reconnects internally).
|
|
316
|
+
runStreamLoop(entry, projector).catch((err) => {
|
|
317
|
+
console.error('[repoburg] stream loop crashed:', err);
|
|
318
|
+
failTurn(entry, `Stream loop failed: ${(err as Error).message}`);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// Phase 5: start the agent turn on the backend.
|
|
322
|
+
try {
|
|
323
|
+
// `auto_apply` keeps actions autonomous (agent-like). It is per-input, so
|
|
324
|
+
// the backend's global execution-strategy setting stays untouched.
|
|
325
|
+
await entry.client.createInput(entry.repoburgSessionId, {
|
|
326
|
+
user_prompt: turn.user_prompt,
|
|
327
|
+
execution_strategy: 'auto_apply',
|
|
328
|
+
});
|
|
329
|
+
} catch (err) {
|
|
330
|
+
if (isConnectionError(err)) invalidateBackend(entry.baseUrl);
|
|
331
|
+
failTurn(entry, (err as Error).message);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// ---------------------------------------------------------------------------
|
|
336
|
+
// Provider interface
|
|
337
|
+
// ---------------------------------------------------------------------------
|
|
338
|
+
|
|
339
|
+
/** Cancel a running turn by agent-pane session id. Returns true if a turn was cancelled. */
|
|
340
|
+
export async function interruptTurn(sessionId: string): Promise<boolean> {
|
|
341
|
+
const entry = sessionMap.get(sessionId);
|
|
342
|
+
const active = entry?.activeTurn;
|
|
343
|
+
if (!entry || !active) return false;
|
|
344
|
+
|
|
345
|
+
// Finalize locally at once — the backend's result{aborted} may follow on a
|
|
346
|
+
// stream we no longer consume.
|
|
347
|
+
active.cancelled = true;
|
|
348
|
+
entry.activeTurn = null;
|
|
349
|
+
active.projector.close();
|
|
350
|
+
active.sseAbort.abort();
|
|
351
|
+
|
|
352
|
+
// Best-effort abort of the in-flight LLM round. Note: this cannot stop
|
|
353
|
+
// mid-action execution on the runtime side.
|
|
354
|
+
entry.client
|
|
355
|
+
.cancel(entry.repoburgSessionId)
|
|
356
|
+
.catch((err) => console.error('[repoburg] cancel failed:', err));
|
|
357
|
+
|
|
358
|
+
const db = getDb();
|
|
359
|
+
const now = new Date().toISOString();
|
|
360
|
+
db.prepare('UPDATE turns SET status = ?, updated_at = ? WHERE id = ?').run('aborted', now, active.turnId);
|
|
361
|
+
db.prepare('UPDATE items SET status = ?, updated_at = ? WHERE turn_id = ? AND status = ?').run(
|
|
362
|
+
'completed',
|
|
363
|
+
now,
|
|
364
|
+
active.turnId,
|
|
365
|
+
'in_progress',
|
|
366
|
+
);
|
|
367
|
+
setSessionStatus(db, sessionId, 0, 'active', now);
|
|
368
|
+
broadcast('turn:cancelled', { sessionId, turnId: active.turnId });
|
|
369
|
+
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Drop a session's active stream (rewind/provider switch) and in-memory state. */
|
|
374
|
+
export function clearThread(sessionId: string): void {
|
|
375
|
+
const entry = sessionMap.get(sessionId);
|
|
376
|
+
if (entry?.activeTurn) {
|
|
377
|
+
const active = entry.activeTurn;
|
|
378
|
+
entry.activeTurn = null;
|
|
379
|
+
active.projector.close();
|
|
380
|
+
active.sseAbort.abort();
|
|
381
|
+
}
|
|
382
|
+
sessionMap.delete(sessionId);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** Parse a turn row's runtime_input_ids (JSON string in DB, array in typed Turn). */
|
|
386
|
+
function parseRuntimeInputIds(turn: Turn): string[] {
|
|
387
|
+
const raw = (turn as Turn & { runtime_input_ids?: string | string[] | null }).runtime_input_ids;
|
|
388
|
+
if (Array.isArray(raw)) return raw;
|
|
389
|
+
if (typeof raw === 'string' && raw) {
|
|
390
|
+
try {
|
|
391
|
+
const parsed = JSON.parse(raw);
|
|
392
|
+
if (Array.isArray(parsed)) return parsed as string[];
|
|
393
|
+
} catch {
|
|
394
|
+
// Corrupt JSON — no anchor.
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return [];
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Truncate the runtime session IN PLACE (platform-parity rewind): hard-delete
|
|
401
|
+
* every runtime input after the target turn's LAST runtime input via the
|
|
402
|
+
* until-here endpoint. Throws on failure — the caller skips the local rewind.
|
|
403
|
+
* forkSession is intentionally NOT implemented for repoburg (a hard-deleted
|
|
404
|
+
* session cannot be forked; truncation is the rewind primitive). */
|
|
405
|
+
export async function rewindRuntime(session: Session, targetTurn: Turn): Promise<void> {
|
|
406
|
+
if (!session.external_thread_id) return; // no runtime session yet — nothing to truncate
|
|
407
|
+
const anchor = parseRuntimeInputIds(targetTurn).pop();
|
|
408
|
+
if (!anchor) {
|
|
409
|
+
throw new Error(
|
|
410
|
+
'Cannot truncate runtime history — the target turn has no runtime input ids (turn predates rewind support or never ran on repoburg).',
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
const entry = sessionMap.get(session.id);
|
|
414
|
+
let client = entry?.client ?? null;
|
|
415
|
+
if (!client) {
|
|
416
|
+
const resolved = await resolveBackendUrl(session.cwd ?? process.cwd());
|
|
417
|
+
if (!resolved) throw new Error('No repoburg backend resolved.');
|
|
418
|
+
client = new RepoburgHttpClient(resolved.baseUrl);
|
|
419
|
+
}
|
|
420
|
+
await client.deleteInputsUntilHere(session.external_thread_id, anchor);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** Clear all session state. Used in test cleanup. */
|
|
424
|
+
export function clearAllThreads(): void {
|
|
425
|
+
for (const entry of sessionMap.values()) {
|
|
426
|
+
if (entry.activeTurn) {
|
|
427
|
+
const active = entry.activeTurn;
|
|
428
|
+
entry.activeTurn = null;
|
|
429
|
+
active.projector.close();
|
|
430
|
+
active.sseAbort.abort();
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
sessionMap.clear();
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** Fetch available models from the runtime and map to ModelInfo[].
|
|
437
|
+
* Never starts a backend (autoStart=false) — empty list until the first turn
|
|
438
|
+
* brings a workspace backend up. */
|
|
439
|
+
async function getModels(): Promise<ModelInfo[]> {
|
|
440
|
+
try {
|
|
441
|
+
const resolved = await resolveBackendUrl(process.cwd(), { autoStart: false });
|
|
442
|
+
if (!resolved) return [];
|
|
443
|
+
const models = await new RepoburgHttpClient(resolved.baseUrl).getModels();
|
|
444
|
+
return models.map((m) => ({
|
|
445
|
+
slug: m.id,
|
|
446
|
+
displayName: m.name,
|
|
447
|
+
description: 'Repoburg backend',
|
|
448
|
+
defaultReasoning: 'medium',
|
|
449
|
+
// Ollama rejects xhigh/minimal/none as `think` — offer only valid levels.
|
|
450
|
+
reasoningLevels: m.id.startsWith('ollama/') ? OLLAMA_REASONING_LEVELS : REASONING_LEVELS,
|
|
451
|
+
}));
|
|
452
|
+
} catch {
|
|
453
|
+
return [];
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** Graceful shutdown — abort active streams and clear state. */
|
|
458
|
+
async function shutdown(): Promise<void> {
|
|
459
|
+
clearAllThreads();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** Repoburg provider — implements RuntimeProvider. */
|
|
463
|
+
export const repoburgProvider: RuntimeProvider = {
|
|
464
|
+
startTurn,
|
|
465
|
+
interruptTurn,
|
|
466
|
+
clearThread,
|
|
467
|
+
clearAllThreads,
|
|
468
|
+
getModels,
|
|
469
|
+
rewindRuntime,
|
|
470
|
+
shutdown,
|
|
471
|
+
};
|