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,356 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
resolveBackendUrl,
|
|
5
|
+
daemonBaseUrl,
|
|
6
|
+
clearResolverCache,
|
|
7
|
+
invalidateBackend,
|
|
8
|
+
type DaemonService,
|
|
9
|
+
} from './backend-resolver.ts';
|
|
10
|
+
|
|
11
|
+
/** Build a daemon service row. */
|
|
12
|
+
function service(partial: Partial<DaemonService> & { port: number }): DaemonService {
|
|
13
|
+
return {
|
|
14
|
+
name: partial.name ?? `svc-${partial.port}`,
|
|
15
|
+
path: partial.path ?? '/tmp/does-not-match',
|
|
16
|
+
port: partial.port,
|
|
17
|
+
status: partial.status ?? 'running',
|
|
18
|
+
pid: partial.pid ?? null,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** JSON Response helper. */
|
|
23
|
+
const jsonResponse = (body: unknown, status = 200): Response =>
|
|
24
|
+
new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } });
|
|
25
|
+
|
|
26
|
+
/** Route table for the daemon + backend endpoints. */
|
|
27
|
+
function mockFetchRoutes(routes: Array<{ match: (url: string, method: string) => boolean; respond: () => Response | Promise<Response> }>): Mock {
|
|
28
|
+
const mock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
29
|
+
const url = String(input);
|
|
30
|
+
const method = init?.method ?? 'GET';
|
|
31
|
+
for (const route of routes) {
|
|
32
|
+
if (route.match(url, method)) return route.respond();
|
|
33
|
+
}
|
|
34
|
+
return jsonResponse({ error: 'not found' }, 404);
|
|
35
|
+
});
|
|
36
|
+
vi.stubGlobal('fetch', mock);
|
|
37
|
+
return mock;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('backend-resolver', () => {
|
|
41
|
+
const DAEMON = 'http://localhost:9998';
|
|
42
|
+
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
delete process.env.REPOBURG_BACKEND_URL;
|
|
45
|
+
delete process.env.REPOBURG_DAEMON_URL;
|
|
46
|
+
clearResolverCache();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(() => {
|
|
50
|
+
vi.unstubAllGlobals();
|
|
51
|
+
vi.useRealTimers();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('daemonBaseUrl defaults to 9998 and honors REPOBURG_DAEMON_URL', () => {
|
|
55
|
+
expect(daemonBaseUrl()).toBe('http://localhost:9998');
|
|
56
|
+
process.env.REPOBURG_DAEMON_URL = 'http://localhost:9999/';
|
|
57
|
+
expect(daemonBaseUrl()).toBe('http://localhost:9999');
|
|
58
|
+
delete process.env.REPOBURG_DAEMON_URL;
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('REPOBURG_BACKEND_URL bypasses the daemon entirely', async () => {
|
|
62
|
+
process.env.REPOBURG_BACKEND_URL = 'http://localhost:1234/';
|
|
63
|
+
const fetchMock = mockFetchRoutes([]);
|
|
64
|
+
const resolved = await resolveBackendUrl('/tmp/proj');
|
|
65
|
+
expect(resolved?.baseUrl).toBe('http://localhost:1234');
|
|
66
|
+
expect(fetchMock).not.toHaveBeenCalled();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('throws a clear error when the daemon is unreachable', async () => {
|
|
70
|
+
const fetchMock = vi.fn().mockRejectedValue(new Error('fetch failed'));
|
|
71
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
72
|
+
await expect(resolveBackendUrl('/tmp/proj')).rejects.toThrow(
|
|
73
|
+
/daemon not reachable at http:\/\/localhost:9998/,
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('returns a healthy running service matching the project path', async () => {
|
|
78
|
+
const abs = path.resolve('/tmp/proj');
|
|
79
|
+
const fetchMock = mockFetchRoutes([
|
|
80
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
81
|
+
{
|
|
82
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
83
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
84
|
+
},
|
|
85
|
+
{ match: (u) => u === 'http://localhost:3402/health', respond: () => jsonResponse({ status: 'ok' }) },
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
const resolved = await resolveBackendUrl('/tmp/proj');
|
|
89
|
+
expect(resolved).toEqual({ baseUrl: 'http://localhost:3402', port: 3402, name: 'proj' });
|
|
90
|
+
expect(fetchMock).toHaveBeenCalledTimes(3); // daemon health + list + backend health
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('reuses the cached resolution without re-listing daemon services', async () => {
|
|
94
|
+
const abs = path.resolve('/tmp/proj');
|
|
95
|
+
const fetchMock = mockFetchRoutes([
|
|
96
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
97
|
+
{
|
|
98
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
99
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
100
|
+
},
|
|
101
|
+
{ match: (u) => u === 'http://localhost:3402/health', respond: () => jsonResponse({ status: 'ok' }) },
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
await resolveBackendUrl('/tmp/proj');
|
|
105
|
+
await resolveBackendUrl('/tmp/proj');
|
|
106
|
+
// Second call: only the cached backend health probe — no daemon calls.
|
|
107
|
+
expect(fetchMock).toHaveBeenCalledTimes(4);
|
|
108
|
+
expect(fetchMock.mock.calls.every(([u]) => !String(u).includes('/services'))).toBe(false);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('restarts a running service whose backend died, then returns it', async () => {
|
|
112
|
+
const abs = path.resolve('/tmp/proj');
|
|
113
|
+
let backendDead = true;
|
|
114
|
+
const restartMock = vi.fn();
|
|
115
|
+
mockFetchRoutes([
|
|
116
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
117
|
+
{
|
|
118
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
119
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
match: (u) => u === 'http://localhost:3402/health',
|
|
123
|
+
respond: () => {
|
|
124
|
+
if (backendDead) return jsonResponse({ error: 'dead' }, 500);
|
|
125
|
+
return jsonResponse({ status: 'ok' });
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
match: (u, m) => u === `${DAEMON}/services/proj/restart` && m === 'POST',
|
|
130
|
+
respond: () => {
|
|
131
|
+
restartMock();
|
|
132
|
+
backendDead = false;
|
|
133
|
+
return jsonResponse(service({ path: abs, port: 3402, name: 'proj' }));
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
const resolved = await resolveBackendUrl('/tmp/proj');
|
|
139
|
+
expect(restartMock).toHaveBeenCalledTimes(1);
|
|
140
|
+
expect(resolved?.baseUrl).toBe('http://localhost:3402');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('restarts a stopped service for the path', async () => {
|
|
144
|
+
const abs = path.resolve('/tmp/proj');
|
|
145
|
+
const fetchMock = mockFetchRoutes([
|
|
146
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
147
|
+
{
|
|
148
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
149
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj', status: 'stopped' })]),
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
match: (u, m) => u === `${DAEMON}/services/proj/restart` && m === 'POST',
|
|
153
|
+
respond: () => jsonResponse(service({ path: abs, port: 3402, name: 'proj' })),
|
|
154
|
+
},
|
|
155
|
+
{ match: (u) => u === 'http://localhost:3402/health', respond: () => jsonResponse({ status: 'ok' }) },
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
const resolved = await resolveBackendUrl('/tmp/proj');
|
|
159
|
+
expect(resolved?.baseUrl).toBe('http://localhost:3402');
|
|
160
|
+
expect(fetchMock.mock.calls.some(([u, i]) => String(u).endsWith('/services/proj/restart') && (i as RequestInit)?.method === 'POST')).toBe(true);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('starts a new backend via the daemon when no service matches', async () => {
|
|
164
|
+
const abs = path.resolve('/tmp/proj');
|
|
165
|
+
const fetchMock = mockFetchRoutes([
|
|
166
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
167
|
+
{ match: (u) => u === `${DAEMON}/services`, respond: () => jsonResponse([]) },
|
|
168
|
+
{
|
|
169
|
+
match: (u, m) => u === `${DAEMON}/services/start` && m === 'POST',
|
|
170
|
+
respond: () => jsonResponse(service({ path: abs, port: 3410, name: 'proj' }), 201),
|
|
171
|
+
},
|
|
172
|
+
{ match: (u) => u === 'http://localhost:3410/health', respond: () => jsonResponse({ status: 'ok' }) },
|
|
173
|
+
]);
|
|
174
|
+
|
|
175
|
+
const resolved = await resolveBackendUrl('/tmp/proj');
|
|
176
|
+
expect(resolved).toEqual({ baseUrl: 'http://localhost:3410', port: 3410, name: 'proj' });
|
|
177
|
+
|
|
178
|
+
const startCall = fetchMock.mock.calls.find(([u]) => String(u).endsWith('/services/start'));
|
|
179
|
+
expect(JSON.parse((startCall![1] as RequestInit).body as string)).toEqual({ path: abs });
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('surfaces the daemon message when start fails (e.g. 401 unauthorized)', async () => {
|
|
183
|
+
mockFetchRoutes([
|
|
184
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
185
|
+
{ match: (u) => u === `${DAEMON}/services`, respond: () => jsonResponse([]) },
|
|
186
|
+
{
|
|
187
|
+
match: (u) => u === `${DAEMON}/services/start`,
|
|
188
|
+
respond: () => jsonResponse({ error: 'Unauthorized', message: 'Please run repoburg login' }, 401),
|
|
189
|
+
},
|
|
190
|
+
]);
|
|
191
|
+
|
|
192
|
+
await expect(resolveBackendUrl('/tmp/proj')).rejects.toThrow(/Please run repoburg login/);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('autoStart=false returns null for stopped or missing services, resolves running ones', async () => {
|
|
196
|
+
const abs = path.resolve('/tmp/proj');
|
|
197
|
+
mockFetchRoutes([
|
|
198
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
199
|
+
{
|
|
200
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
201
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj', status: 'stopped' })]),
|
|
202
|
+
},
|
|
203
|
+
]);
|
|
204
|
+
expect(await resolveBackendUrl('/tmp/proj', { autoStart: false })).toBeNull();
|
|
205
|
+
expect(await resolveBackendUrl('/tmp/other', { autoStart: false })).toBeNull();
|
|
206
|
+
|
|
207
|
+
clearResolverCache();
|
|
208
|
+
mockFetchRoutes([
|
|
209
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
210
|
+
{
|
|
211
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
212
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
213
|
+
},
|
|
214
|
+
{ match: (u) => u === 'http://localhost:3402/health', respond: () => jsonResponse({ status: 'ok' }) },
|
|
215
|
+
]);
|
|
216
|
+
const resolved = await resolveBackendUrl('/tmp/proj', { autoStart: false });
|
|
217
|
+
expect(resolved?.baseUrl).toBe('http://localhost:3402');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('autoStart=false returns null when the running backend is unhealthy (no restart)', async () => {
|
|
221
|
+
const abs = path.resolve('/tmp/proj');
|
|
222
|
+
const fetchMock = mockFetchRoutes([
|
|
223
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
224
|
+
{
|
|
225
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
226
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
227
|
+
},
|
|
228
|
+
{ match: (u) => u === 'http://localhost:3402/health', respond: () => jsonResponse({ error: 'dead' }, 500) },
|
|
229
|
+
]);
|
|
230
|
+
|
|
231
|
+
expect(await resolveBackendUrl('/tmp/proj', { autoStart: false })).toBeNull();
|
|
232
|
+
expect(fetchMock.mock.calls.some(([u]) => String(u).includes('restart'))).toBe(false);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('invalidateBackend drops the cache entry so the daemon is re-consulted', async () => {
|
|
236
|
+
const abs = path.resolve('/tmp/proj');
|
|
237
|
+
const fetchMock = mockFetchRoutes([
|
|
238
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
239
|
+
{
|
|
240
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
241
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
242
|
+
},
|
|
243
|
+
{ match: (u) => u === 'http://localhost:3402/health', respond: () => jsonResponse({ status: 'ok' }) },
|
|
244
|
+
]);
|
|
245
|
+
|
|
246
|
+
const first = await resolveBackendUrl('/tmp/proj');
|
|
247
|
+
invalidateBackend(first!.baseUrl);
|
|
248
|
+
await resolveBackendUrl('/tmp/proj');
|
|
249
|
+
// Re-consulted the daemon after invalidation: 3 (first flow) + 3 (second flow).
|
|
250
|
+
expect(fetchMock).toHaveBeenCalledTimes(6);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('throws when a freshly started backend never becomes healthy', async () => {
|
|
254
|
+
vi.useFakeTimers();
|
|
255
|
+
const abs = path.resolve('/tmp/proj');
|
|
256
|
+
mockFetchRoutes([
|
|
257
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
258
|
+
{ match: (u) => u === `${DAEMON}/services`, respond: () => jsonResponse([]) },
|
|
259
|
+
{
|
|
260
|
+
match: (u) => u === `${DAEMON}/services/start`,
|
|
261
|
+
respond: () => jsonResponse(service({ path: abs, port: 3410, name: 'proj' }), 201),
|
|
262
|
+
},
|
|
263
|
+
{ match: (u) => u === 'http://localhost:3410/health', respond: () => jsonResponse({ error: 'dead' }, 500) },
|
|
264
|
+
]);
|
|
265
|
+
|
|
266
|
+
const promise = resolveBackendUrl('/tmp/proj');
|
|
267
|
+
const assertion = expect(promise).rejects.toThrow(/did not become healthy within 30s/);
|
|
268
|
+
await vi.advanceTimersByTimeAsync(31_000);
|
|
269
|
+
await assertion;
|
|
270
|
+
});
|
|
271
|
+
it('accepts a healthy running backend whose health projectPath matches the workspace', async () => {
|
|
272
|
+
const abs = path.resolve('/tmp/proj');
|
|
273
|
+
mockFetchRoutes([
|
|
274
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
275
|
+
{
|
|
276
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
277
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
278
|
+
},
|
|
279
|
+
{ match: (u) => u === 'http://localhost:3402/health', respond: () => jsonResponse({ status: 'ok', projectPath: abs }) },
|
|
280
|
+
]);
|
|
281
|
+
|
|
282
|
+
const resolved = await resolveBackendUrl('/tmp/proj');
|
|
283
|
+
expect(resolved?.baseUrl).toBe('http://localhost:3402');
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('treats a projectPath mismatch as unhealthy (autoStart=false → null, no restart)', async () => {
|
|
287
|
+
const abs = path.resolve('/tmp/proj');
|
|
288
|
+
const fetchMock = mockFetchRoutes([
|
|
289
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
290
|
+
{
|
|
291
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
292
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
293
|
+
},
|
|
294
|
+
{ match: (u) => u === 'http://localhost:3402/health', respond: () => jsonResponse({ status: 'ok', projectPath: '/tmp/other-workspace' }) },
|
|
295
|
+
]);
|
|
296
|
+
|
|
297
|
+
expect(await resolveBackendUrl('/tmp/proj', { autoStart: false })).toBeNull();
|
|
298
|
+
expect(fetchMock.mock.calls.some(([u]) => String(u).includes('restart'))).toBe(false);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it('restarts a running backend that serves a different workspace, then resolves it', async () => {
|
|
302
|
+
const abs = path.resolve('/tmp/proj');
|
|
303
|
+
let servingWrongWorkspace = true;
|
|
304
|
+
const restartMock = vi.fn();
|
|
305
|
+
mockFetchRoutes([
|
|
306
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
307
|
+
{
|
|
308
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
309
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
310
|
+
},
|
|
311
|
+
{
|
|
312
|
+
match: (u) => u === 'http://localhost:3402/health',
|
|
313
|
+
respond: () =>
|
|
314
|
+
servingWrongWorkspace
|
|
315
|
+
? jsonResponse({ status: 'ok', projectPath: '/tmp/other-workspace' })
|
|
316
|
+
: jsonResponse({ status: 'ok', projectPath: abs }),
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
match: (u, m) => u === `${DAEMON}/services/proj/restart` && m === 'POST',
|
|
320
|
+
respond: () => {
|
|
321
|
+
restartMock();
|
|
322
|
+
servingWrongWorkspace = false;
|
|
323
|
+
return jsonResponse(service({ path: abs, port: 3402, name: 'proj' }));
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
]);
|
|
327
|
+
|
|
328
|
+
const resolved = await resolveBackendUrl('/tmp/proj');
|
|
329
|
+
expect(restartMock).toHaveBeenCalledTimes(1);
|
|
330
|
+
expect(resolved?.baseUrl).toBe('http://localhost:3402');
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it('re-consults the daemon when the cached backend starts serving a different workspace', async () => {
|
|
334
|
+
const abs = path.resolve('/tmp/proj');
|
|
335
|
+
let projectPath = abs;
|
|
336
|
+
const fetchMock = mockFetchRoutes([
|
|
337
|
+
{ match: (u) => u === `${DAEMON}/health`, respond: () => jsonResponse({ status: 'ok' }) },
|
|
338
|
+
{
|
|
339
|
+
match: (u) => u === `${DAEMON}/services`,
|
|
340
|
+
respond: () => jsonResponse([service({ path: abs, port: 3402, name: 'proj' })]),
|
|
341
|
+
},
|
|
342
|
+
{ match: (u) => u === 'http://localhost:3402/health', respond: () => jsonResponse({ status: 'ok', projectPath }) },
|
|
343
|
+
]);
|
|
344
|
+
|
|
345
|
+
// First resolve caches the backend (projectPath matches).
|
|
346
|
+
await resolveBackendUrl('/tmp/proj');
|
|
347
|
+
|
|
348
|
+
// The row got mis-mapped — the backend now serves another workspace.
|
|
349
|
+
projectPath = '/tmp/other-workspace';
|
|
350
|
+
fetchMock.mockClear();
|
|
351
|
+
await resolveBackendUrl('/tmp/proj', { autoStart: false });
|
|
352
|
+
// The cache was rejected and the daemon re-consulted (health probe rejected).
|
|
353
|
+
expect(fetchMock.mock.calls.some(([u]) => String(u) === 'http://localhost:3402/health')).toBe(true);
|
|
354
|
+
expect(fetchMock.mock.calls.some(([u]) => String(u) === `${DAEMON}/services`)).toBe(true);
|
|
355
|
+
});
|
|
356
|
+
});
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the repoburg backend instance for a project path via the
|
|
3
|
+
* platform daemon (default http://localhost:9998).
|
|
4
|
+
*
|
|
5
|
+
* The daemon is the platform's process manager (PM2): it spawns one
|
|
6
|
+
* repoburg-backend per workspace with PORT (3400-3999) and
|
|
7
|
+
* REPOBURG_PROJECT_PATH. AgentPane never spawns backends itself.
|
|
8
|
+
*
|
|
9
|
+
* Resolution order:
|
|
10
|
+
* 1. REPOBURG_BACKEND_URL env → direct URL (dev override, daemon skipped)
|
|
11
|
+
* 2. cached resolution re-verified healthy
|
|
12
|
+
* 3. daemon GET /services → healthy running service for the path
|
|
13
|
+
* 4. daemon restart → stopped/dead service for the path
|
|
14
|
+
* 5. daemon POST /services/start → no service yet
|
|
15
|
+
*
|
|
16
|
+
* License auth: the daemon's file-based fallback (~/.repoburg/token.jwt)
|
|
17
|
+
* authorizes same-machine callers without a token.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import path from 'node:path';
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Types
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
/** Daemon service row (GET /services). */
|
|
27
|
+
export interface DaemonService {
|
|
28
|
+
name: string;
|
|
29
|
+
path: string;
|
|
30
|
+
port: number;
|
|
31
|
+
status: 'running' | 'stopped' | 'errored';
|
|
32
|
+
pid: number | null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Root backend /health payload (projectPath is additive — absent on pre-v2 builds). */
|
|
36
|
+
interface HealthPayload {
|
|
37
|
+
status: string;
|
|
38
|
+
projectPath?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A resolved backend instance to connect to. */
|
|
42
|
+
export interface ResolvedBackend {
|
|
43
|
+
baseUrl: string;
|
|
44
|
+
port: number;
|
|
45
|
+
name: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ResolveOptions {
|
|
49
|
+
/** false = never start/restart; return null when no healthy instance runs. */
|
|
50
|
+
autoStart?: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
// Configuration
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
|
|
57
|
+
/** Health probe interval/timeout for freshly started backends (ms). */
|
|
58
|
+
const HEALTH_POLL_INTERVAL_MS = 1000;
|
|
59
|
+
const HEALTH_TIMEOUT_MS = 30_000;
|
|
60
|
+
|
|
61
|
+
/** Daemon base URL. */
|
|
62
|
+
export function daemonBaseUrl(): string {
|
|
63
|
+
return (process.env.REPOBURG_DAEMON_URL || 'http://localhost:9998').replace(/\/+$/, '');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// HTTP helpers
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
class HttpError extends Error {
|
|
71
|
+
constructor(
|
|
72
|
+
readonly status: number,
|
|
73
|
+
readonly body: string,
|
|
74
|
+
readonly url: string,
|
|
75
|
+
) {
|
|
76
|
+
super(`${url} failed: ${status} ${body}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** JSON GET/POST with a timeout. Throws HttpError on non-OK responses. */
|
|
81
|
+
async function fetchJson<T>(url: string, init?: RequestInit, timeoutMs = 5000): Promise<T> {
|
|
82
|
+
const controller = new AbortController();
|
|
83
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
84
|
+
let response: Response;
|
|
85
|
+
try {
|
|
86
|
+
response = await fetch(url, { ...init, signal: controller.signal });
|
|
87
|
+
} finally {
|
|
88
|
+
clearTimeout(timer);
|
|
89
|
+
}
|
|
90
|
+
const text = await response.text();
|
|
91
|
+
if (!response.ok) {
|
|
92
|
+
throw new HttpError(response.status, text, url);
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
return JSON.parse(text) as T;
|
|
96
|
+
} catch {
|
|
97
|
+
throw new Error(`Unexpected non-JSON response from ${url}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Extract a human message from a daemon HttpError body. */
|
|
102
|
+
function daemonErrorMessage(err: unknown): string {
|
|
103
|
+
if (err instanceof HttpError) {
|
|
104
|
+
try {
|
|
105
|
+
const parsed = JSON.parse(err.body) as { message?: string; error?: string };
|
|
106
|
+
return parsed.message || parsed.error || err.body;
|
|
107
|
+
} catch {
|
|
108
|
+
return err.body;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return (err as Error).message;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
// Resolution
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
/** Cached resolutions per resolved project path. */
|
|
119
|
+
const resolvedCache = new Map<string, ResolvedBackend>();
|
|
120
|
+
|
|
121
|
+
/** Test hook — clear the resolution cache. */
|
|
122
|
+
export function clearResolverCache(): void {
|
|
123
|
+
resolvedCache.clear();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Drop cached resolutions pointing at the given base URL (dead backend). */
|
|
127
|
+
export function invalidateBackend(baseUrl: string): void {
|
|
128
|
+
for (const [key, value] of resolvedCache) {
|
|
129
|
+
if (value.baseUrl === baseUrl) resolvedCache.delete(key);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Probe a backend's /health endpoint (short timeout). With expectedPath, a
|
|
135
|
+
* projectPath mismatch means unhealthy: the backend serves a different
|
|
136
|
+
* workspace than the daemon row claims (mis-mapped service).
|
|
137
|
+
*/
|
|
138
|
+
async function backendHealthy(baseUrl: string, expectedPath?: string): Promise<boolean> {
|
|
139
|
+
try {
|
|
140
|
+
const health = await fetchJson<HealthPayload>(`${baseUrl}/health`, undefined, 2000);
|
|
141
|
+
if (health.status !== 'ok') return false;
|
|
142
|
+
if (expectedPath && health.projectPath) {
|
|
143
|
+
return path.resolve(health.projectPath) === path.resolve(expectedPath);
|
|
144
|
+
}
|
|
145
|
+
return true;
|
|
146
|
+
} catch {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Wait until the backend on baseUrl reports healthy (for expectedPath). */
|
|
152
|
+
async function waitForHealthy(baseUrl: string, name: string, expectedPath?: string): Promise<void> {
|
|
153
|
+
const deadline = Date.now() + HEALTH_TIMEOUT_MS;
|
|
154
|
+
while (Date.now() < deadline) {
|
|
155
|
+
if (await backendHealthy(baseUrl, expectedPath)) return;
|
|
156
|
+
await new Promise((resolve) => setTimeout(resolve, HEALTH_POLL_INTERVAL_MS));
|
|
157
|
+
}
|
|
158
|
+
throw new Error(
|
|
159
|
+
`Backend '${name}' on ${baseUrl} did not become healthy within ${HEALTH_TIMEOUT_MS / 1000}s. Check daemon logs: repoburg logs ${name}`,
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function serviceBaseUrl(service: DaemonService): string {
|
|
164
|
+
return `http://localhost:${service.port}`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Ask the daemon to (re)start the named service. */
|
|
168
|
+
async function restartService(daemon: string, name: string): Promise<void> {
|
|
169
|
+
try {
|
|
170
|
+
await fetchJson<DaemonService>(`${daemon}/services/${name}/restart`, { method: 'POST' });
|
|
171
|
+
} catch (err) {
|
|
172
|
+
throw new Error(`Daemon failed to restart backend '${name}': ${daemonErrorMessage(err)}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Ask the daemon to start a backend for the project path. */
|
|
177
|
+
async function startService(daemon: string, projectPath: string): Promise<DaemonService> {
|
|
178
|
+
try {
|
|
179
|
+
return await fetchJson<DaemonService>(`${daemon}/services/start`, {
|
|
180
|
+
method: 'POST',
|
|
181
|
+
headers: { 'Content-Type': 'application/json' },
|
|
182
|
+
body: JSON.stringify({ path: projectPath }),
|
|
183
|
+
});
|
|
184
|
+
} catch (err) {
|
|
185
|
+
throw new Error(`Daemon failed to start backend for ${projectPath}: ${daemonErrorMessage(err)}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function cache(absPath: string, service: DaemonService): ResolvedBackend {
|
|
190
|
+
const resolved: ResolvedBackend = {
|
|
191
|
+
baseUrl: serviceBaseUrl(service),
|
|
192
|
+
port: service.port,
|
|
193
|
+
name: service.name,
|
|
194
|
+
};
|
|
195
|
+
resolvedCache.set(absPath, resolved);
|
|
196
|
+
return resolved;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Resolve the backend instance for a project path.
|
|
201
|
+
* Throws on unreachable daemon / start failure; returns null only when
|
|
202
|
+
* autoStart=false and no healthy instance is running.
|
|
203
|
+
*/
|
|
204
|
+
export async function resolveBackendUrl(
|
|
205
|
+
projectPath: string,
|
|
206
|
+
opts: ResolveOptions = {},
|
|
207
|
+
): Promise<ResolvedBackend | null> {
|
|
208
|
+
const autoStart = opts.autoStart !== false;
|
|
209
|
+
|
|
210
|
+
// 1. Explicit env override — direct connection, daemon skipped.
|
|
211
|
+
const envUrl = process.env.REPOBURG_BACKEND_URL;
|
|
212
|
+
if (envUrl) {
|
|
213
|
+
return { baseUrl: envUrl.replace(/\/+$/, ''), port: 0, name: 'REPOBURG_BACKEND_URL' };
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const absPath = path.resolve(projectPath);
|
|
217
|
+
const daemon = daemonBaseUrl();
|
|
218
|
+
|
|
219
|
+
// 2. Cached resolution, re-verified healthy (and serving this workspace).
|
|
220
|
+
const cached = resolvedCache.get(absPath);
|
|
221
|
+
if (cached && (await backendHealthy(cached.baseUrl, absPath))) {
|
|
222
|
+
return cached;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// 3. Daemon must be reachable.
|
|
226
|
+
try {
|
|
227
|
+
await fetchJson(`${daemon}/health`, undefined, 3000);
|
|
228
|
+
} catch {
|
|
229
|
+
throw new Error(
|
|
230
|
+
`Repoburg daemon not reachable at ${daemon}. Start it with 'repoburg-daemon', or set REPOBURG_BACKEND_URL to a running backend.`,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const services = await fetchJson<DaemonService[]>(`${daemon}/services`);
|
|
235
|
+
const match = services.find((s) => s.path === absPath);
|
|
236
|
+
|
|
237
|
+
if (match && match.status === 'running') {
|
|
238
|
+
if (await backendHealthy(serviceBaseUrl(match), absPath)) {
|
|
239
|
+
return cache(absPath, match);
|
|
240
|
+
}
|
|
241
|
+
if (!autoStart) return null;
|
|
242
|
+
// Unhealthy or serving a different workspace — the process is wrong
|
|
243
|
+
// for this path. Restart via the daemon.
|
|
244
|
+
await restartService(daemon, match.name);
|
|
245
|
+
await waitForHealthy(serviceBaseUrl(match), match.name, absPath);
|
|
246
|
+
return cache(absPath, match);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (!autoStart) return null; // stopped/errored/missing — left to the first turn.
|
|
250
|
+
|
|
251
|
+
if (match) {
|
|
252
|
+
await restartService(daemon, match.name);
|
|
253
|
+
await waitForHealthy(serviceBaseUrl(match), match.name, absPath);
|
|
254
|
+
return cache(absPath, match);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// No service for this path yet — start one.
|
|
258
|
+
const service = await startService(daemon, absPath);
|
|
259
|
+
await waitForHealthy(serviceBaseUrl(service), service.name, absPath);
|
|
260
|
+
return cache(absPath, service);
|
|
261
|
+
}
|