diff --git a/backend/src/services/opencode-restart-coordinator.test.ts b/backend/src/services/opencode-restart-coordinator.test.ts index 22348343..27a2b6a6 100644 --- a/backend/src/services/opencode-restart-coordinator.test.ts +++ b/backend/src/services/opencode-restart-coordinator.test.ts @@ -119,8 +119,8 @@ describe('OpenCodeRestartCoordinator', () => { 'forward:/api/session/s1/interrupt', 'forward:/api/session/s2/interrupt', 'restart', - 'forward:/api/session/s1/prompt', - 'forward:/api/session/s2/prompt', + 'forward:/session/s1/message', + 'forward:/session/s2/message', ]) // Aborts called first for both sessions @@ -139,20 +139,22 @@ describe('OpenCodeRestartCoordinator', () => { expect(forward).toHaveBeenCalledTimes(4) expect(forward).toHaveBeenNthCalledWith(3, { method: 'POST', - path: '/api/session/s1/prompt', + path: '/session/s1/message', + directory: '/a', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ prompt: { text: 'continue' } }), + body: JSON.stringify({ parts: [{ type: 'text', text: 'continue' }] }), }) expect(forward).toHaveBeenNthCalledWith(4, { method: 'POST', - path: '/api/session/s2/prompt', + path: '/session/s2/message', + directory: '/b', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ prompt: { text: 'continue' } }), + body: JSON.stringify({ parts: [{ type: 'text', text: 'continue' }] }), }) // Verify resume body deserializes correctly const resumeBody = JSON.parse(forward.mock.calls[2]![0].body as string) - expect(resumeBody).toEqual({ prompt: { text: 'continue' } }) + expect(resumeBody).toEqual({ parts: [{ type: 'text', text: 'continue' }] }) expect(result).toEqual({ healthy: true, resumedSessionIDs: ['s1', 's2'] }) }) @@ -176,11 +178,11 @@ describe('OpenCodeRestartCoordinator', () => { }) expect(restart).toHaveBeenCalledOnce() - // No prompt calls - const promptCalls = forward.mock.calls.filter( - (call: unknown[]) => (call[0] as { path: string }).path.includes('/prompt'), + // No resume calls + const resumeCalls = forward.mock.calls.filter( + (call: unknown[]) => (call[0] as { path: string }).path.includes('/message'), ) - expect(promptCalls).toHaveLength(0) + expect(resumeCalls).toHaveLength(0) expect(result).toEqual({ healthy: false, resumedSessionIDs: [] }) }) @@ -222,9 +224,10 @@ describe('OpenCodeRestartCoordinator', () => { }) expect(forward).toHaveBeenNthCalledWith(2, { method: 'POST', - path: '/api/session/manual1/prompt', + path: '/session/manual1/message', + directory: '/a', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ prompt: { text: 'continue' } }), + body: JSON.stringify({ parts: [{ type: 'text', text: 'continue' }] }), }) expect(result).toEqual({ healthy: true, resumedSessionIDs: ['manual1'] }) diff --git a/backend/src/services/opencode-restart-coordinator.ts b/backend/src/services/opencode-restart-coordinator.ts index 0750935a..93908658 100644 --- a/backend/src/services/opencode-restart-coordinator.ts +++ b/backend/src/services/opencode-restart-coordinator.ts @@ -60,9 +60,10 @@ export class OpenCodeRestartCoordinator { try { const response = await this.client.forward({ method: 'POST', - path: `/api/session/${s.sessionID}/prompt`, + path: `/session/${s.sessionID}/message`, + directory: s.directory, headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ prompt: { text: 'continue' } }), + body: JSON.stringify({ parts: [{ type: 'text', text: 'continue' }] }), }) return response.ok ? s.sessionID : null } catch (error) { diff --git a/backend/src/services/schedules.ts b/backend/src/services/schedules.ts index 2491f1cc..21fbdf5c 100644 --- a/backend/src/services/schedules.ts +++ b/backend/src/services/schedules.ts @@ -670,7 +670,7 @@ export class ScheduleService { } if (run.sessionId) { - const messages = await this.listSessionMessages(run.sessionId) + const messages = await this.listSessionMessages(run.sessionId, run.worktreePath ?? repo.fullPath) const assistantState = getAssistantMessageState(messages) const terminal = isRunTerminal(assistantState) @@ -744,21 +744,34 @@ export class ScheduleService { const repo = this.assertRepo(input.repoId) try { - const promptResponse = await this.openCodeClient.forward({ - method: 'POST', - path: `/api/session/${input.sessionId}/prompt`, - body: JSON.stringify({ - prompt: { text: await buildPromptWithSkills(input.job.prompt, input.job.skillMetadata, input.directory, this.openCodeClient) }, - }), - headers: { 'Content-Type': 'application/json' }, - }) + const promptText = await buildPromptWithSkills(input.job.prompt, input.job.skillMetadata, input.directory, this.openCodeClient) - if (!promptResponse.ok) { - const errorText = await promptResponse.text() - throw new ScheduleServiceError(errorText || 'Failed to run scheduled prompt', 502) - } + // POST /session/{id}/message (the SDK's session.prompt) drives the agent turn and + // resolves when it completes. Fire it without awaiting completion so monitorRunCompletion + // can poll for the result, answer pending permissions, and enforce the stall timeout. + // A failed submit is captured and surfaced to the monitor loop so the run fails fast. + let submitErrorText: string | null = null + const submitPromise = (async () => { + try { + const promptResponse = await this.openCodeClient.forward({ + method: 'POST', + path: `/session/${input.sessionId}/message`, + directory: input.directory, + body: JSON.stringify({ + parts: [{ type: 'text', text: promptText }], + }), + headers: { 'Content-Type': 'application/json' }, + }) + if (!promptResponse.ok) { + const errorText = await promptResponse.text() + submitErrorText = errorText || 'Failed to run scheduled prompt' + } + } catch (error) { + submitErrorText = getErrorMessage(error) + } + })() - await this.respondToPendingRequests(input.sessionId, input.ruleset) + await this.respondToPendingRequests(input.sessionId, input.ruleset, input.directory) await this.monitorRunCompletion({ sessionMonitor: input.sessionMonitor, @@ -770,7 +783,9 @@ export class ScheduleService { triggerSource: input.triggerSource, ruleset: input.ruleset, directory: input.directory, + getSubmitError: () => submitErrorText, }) + await submitPromise return } catch (error) { const finishedAt = Date.now() @@ -819,10 +834,11 @@ export class ScheduleService { triggerSource: ScheduleRunTriggerSource ruleset: SchedulePermissionRuleset directory: string + getSubmitError?: () => string | null }): Promise { try { const repo = this.assertRepo(input.repoId) - const currentMessages = await this.listSessionMessages(input.sessionId) + const currentMessages = await this.listSessionMessages(input.sessionId, input.directory) const currentAssistantState = getAssistantMessageState(currentMessages) const terminal = isRunTerminal(currentAssistantState) if (terminal.terminal) { @@ -841,7 +857,7 @@ export class ScheduleService { return } - const response = await this.waitForAssistantMessage(input.job, input.sessionId, input.sessionMonitor, input.ruleset) + const response = await this.waitForAssistantMessage(input.job, input.sessionId, input.directory, input.sessionMonitor, input.ruleset, input.getSubmitError) const currentRun = getScheduleRunById(this.db, input.repoId, input.job.id, input.runId) if (!currentRun || currentRun.status !== 'running') { return @@ -926,11 +942,12 @@ export class ScheduleService { } } - private async respondToPendingRequests(sessionId: string, ruleset: SchedulePermissionRuleset): Promise { + private async respondToPendingRequests(sessionId: string, ruleset: SchedulePermissionRuleset, directory: string): Promise { try { const permResponse = await this.openCodeClient.forward({ method: 'GET', path: `/api/session/${sessionId}/permission`, + directory, }) if (permResponse.ok) { @@ -944,6 +961,7 @@ export class ScheduleService { const replyResponse = await this.openCodeClient.forward({ method: 'POST', path: `/api/session/${sessionId}/permission/${req.id}/reply`, + directory, body: JSON.stringify(replyBody), headers: { 'Content-Type': 'application/json' }, }) @@ -965,6 +983,7 @@ export class ScheduleService { const questionResponse = await this.openCodeClient.forward({ method: 'GET', path: `/api/session/${sessionId}/question`, + directory, }) if (questionResponse.ok) { @@ -974,6 +993,7 @@ export class ScheduleService { const rejectResponse = await this.openCodeClient.forward({ method: 'POST', path: `/api/session/${sessionId}/question/${q.id}/reject`, + directory, }) if (!rejectResponse.ok && rejectResponse.status !== 404) { logger.warn(`Unexpected status ${rejectResponse.status} rejecting question ${q.id} in session ${sessionId}`) @@ -1003,10 +1023,10 @@ export class ScheduleService { return } - const messages = await this.listSessionMessages(run.sessionId) + const messages = await this.listSessionMessages(run.sessionId, runDirectory) const assistantState = getAssistantMessageState(messages) - const activeSessions = await this.getActiveSessions() + const activeSessions = await this.getActiveSessions(runDirectory) const sessionActive = run.sessionId ? run.sessionId in activeSessions : false const terminal = isRunTerminal(assistantState) @@ -1118,14 +1138,22 @@ export class ScheduleService { private async waitForAssistantMessage( job: ScheduleJob, sessionId: string, + directory: string, sessionMonitor: SessionMonitor, ruleset: SchedulePermissionRuleset, + getSubmitError?: () => string | null, ): Promise<{ responseText: string | null; errorText: string | null }> { let timeoutStartedAt: number | null = null while (true) { - await this.respondToPendingRequests(sessionId, ruleset) - const messages = await this.listSessionMessages(sessionId) + await this.respondToPendingRequests(sessionId, ruleset, directory) + + const submitErrorText = getSubmitError?.() + if (submitErrorText) { + return { responseText: null, errorText: submitErrorText } + } + + const messages = await this.listSessionMessages(sessionId, directory) const assistantState = getAssistantMessageState(messages) if (assistantState?.errorText) { @@ -1143,7 +1171,7 @@ export class ScheduleService { } } - const activeSessions = await this.getActiveSessions() + const activeSessions = await this.getActiveSessions(directory) const sessionActive = sessionId in activeSessions if (assistantState?.completed) { @@ -1172,40 +1200,45 @@ export class ScheduleService { } } - private async listSessionMessages(sessionId: string): Promise { - const allMessages: SessionMessage[] = [] - let cursor: string | null = null - let pages = 0 - const MAX_PAGES = 10 + private async listSessionMessages(sessionId: string, directory: string): Promise { + // Uses the SDK's session.messages endpoint (GET /session/{id}/message), which returns + // the directory-scoped message list as a plain array of { info, parts }. The /api/session + // variant does not resolve worktree-scoped sessions reliably, so a directory is required. + const messagesResponse = await this.openCodeClient.forward({ + method: 'GET', + path: `/session/${sessionId}/message`, + directory, + }) - do { - const query = cursor - ? `/api/session/${sessionId}/message?limit=200&cursor=${encodeURIComponent(cursor)}` - : `/api/session/${sessionId}/message?limit=200` + if (!messagesResponse.ok) { + const errorText = await messagesResponse.text() + throw new ScheduleServiceError(errorText || 'Failed to fetch session messages', 502) + } - const messagesResponse = await this.openCodeClient.forward({ - method: 'GET', - path: query, - }) - - if (!messagesResponse.ok) { - const errorText = await messagesResponse.text() - throw new ScheduleServiceError(errorText || 'Failed to fetch session messages', 502) + const body = await messagesResponse.json() as Array<{ + info?: { + id?: string + role?: string + time?: { created?: number; completed?: number } + error?: { name?: string; data?: { message?: string } } } + parts?: SessionMessageContent[] + }> - const body = await messagesResponse.json() as { data: SessionMessage[]; cursor?: { next?: string } } - allMessages.push(...body.data) - cursor = body.cursor?.next ?? null - pages++ - } while (cursor && pages < MAX_PAGES) - - return allMessages + return body.map((message) => ({ + id: message.info?.id ?? '', + type: message.info?.role ?? '', + content: message.parts, + error: message.info?.error, + time: message.info?.time, + })) } - private async getActiveSessions(): Promise> { + private async getActiveSessions(directory: string): Promise> { const response = await this.openCodeClient.forward({ method: 'GET', path: '/api/session/active', + directory, }) if (!response.ok) { diff --git a/backend/test/services/schedules.permission.test.ts b/backend/test/services/schedules.permission.test.ts index 784b6426..b6012b28 100644 --- a/backend/test/services/schedules.permission.test.ts +++ b/backend/test/services/schedules.permission.test.ts @@ -129,7 +129,15 @@ function v2Messages(messages: Array<{ finish?: string error?: { name?: string; data?: { message?: string } } }>): Response { - return jsonResponse({ data: messages.map(m => ({ ...m, id: m.id ?? 'msg-1' })), cursor: {} }) + return jsonResponse(messages.map(m => ({ + info: { + id: m.id ?? 'msg-1', + role: m.type, + time: m.time, + error: m.error, + }, + parts: m.content, + }))) } function createOpenCodeClientStub(): OpenCodeClient { @@ -232,7 +240,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === '/api/session' && method === 'POST') { return jsonResponse({ data: { id: 'ses-perm-auto' } }) } - if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') { + if (path === `/session/ses-perm-auto/message` && method === 'POST') { return promptReceipt() } if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') { @@ -241,7 +249,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === `/api/session/ses-perm-auto/question` && method === 'GET') { return jsonResponse({ data: [] }) } - if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') { + if (path.startsWith('/session/ses-perm-auto/message') && method === 'GET') { return v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' }, ]) @@ -294,7 +302,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === '/api/session' && method === 'POST') { return jsonResponse({ data: { id: 'ses-perm-auto' } }) } - if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') { + if (path === `/session/ses-perm-auto/message` && method === 'POST') { return promptReceipt() } if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') { @@ -303,7 +311,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === `/api/session/ses-perm-auto/question` && method === 'GET') { return jsonResponse({ data: [] }) } - if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') { + if (path.startsWith('/session/ses-perm-auto/message') && method === 'GET') { return v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' }, ]) @@ -346,7 +354,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === '/api/session' && method === 'POST') { return jsonResponse({ data: { id: 'ses-perm-auto' } }) } - if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') { + if (path === `/session/ses-perm-auto/message` && method === 'POST') { return promptReceipt() } if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') { @@ -355,7 +363,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === `/api/session/ses-perm-auto/question` && method === 'GET') { return jsonResponse({ data: [] }) } - if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') { + if (path.startsWith('/session/ses-perm-auto/message') && method === 'GET') { return v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' }, ]) @@ -398,7 +406,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === '/api/session' && method === 'POST') { return jsonResponse({ data: { id: 'ses-perm-auto' } }) } - if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') { + if (path === `/session/ses-perm-auto/message` && method === 'POST') { return promptReceipt() } if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') { @@ -407,7 +415,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === `/api/session/ses-perm-auto/question` && method === 'GET') { return jsonResponse({ data: [] }) } - if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') { + if (path.startsWith('/session/ses-perm-auto/message') && method === 'GET') { return v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' }, ]) @@ -450,7 +458,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === '/api/session' && method === 'POST') { return jsonResponse({ data: { id: 'ses-perm-auto' } }) } - if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') { + if (path === `/session/ses-perm-auto/message` && method === 'POST') { return promptReceipt() } if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') { @@ -459,7 +467,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === `/api/session/ses-perm-auto/question` && method === 'GET') { return jsonResponse({ data: [{ id: 'q-1' }] }) } - if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') { + if (path.startsWith('/session/ses-perm-auto/message') && method === 'GET') { return v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' }, ]) @@ -501,7 +509,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === '/api/session' && method === 'POST') { return jsonResponse({ data: { id: 'ses-perm-auto' } }) } - if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') { + if (path === `/session/ses-perm-auto/message` && method === 'POST') { return promptReceipt() } if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') { @@ -510,7 +518,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === `/api/session/ses-perm-auto/question` && method === 'GET') { return textResponse('Internal Server Error', 500) } - if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') { + if (path.startsWith('/session/ses-perm-auto/message') && method === 'GET') { return v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' }, ]) @@ -554,7 +562,7 @@ describe('ScheduleService permission auto-responder', () => { if (path === '/api/session' && method === 'POST') { return jsonResponse({ data: { id: 'ses-perm-auto' } }) } - if (path === `/api/session/ses-perm-auto/prompt` && method === 'POST') { + if (path === `/session/ses-perm-auto/message` && method === 'POST') { return promptReceipt() } if (path === `/api/session/ses-perm-auto/permission` && method === 'GET') { @@ -570,7 +578,7 @@ describe('ScheduleService permission auto-responder', () => { replyCount++ return jsonResponse({}) } - if (path.startsWith('/api/session/ses-perm-auto/message') && method === 'GET') { + if (path.startsWith('/session/ses-perm-auto/message') && method === 'GET') { return v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: 1000, completed: 2000 }, finish: 'stop' }, ]) diff --git a/backend/test/services/schedules.test.ts b/backend/test/services/schedules.test.ts index 797fa5fa..63d79855 100644 --- a/backend/test/services/schedules.test.ts +++ b/backend/test/services/schedules.test.ts @@ -136,7 +136,15 @@ function v2Messages(messages: Array<{ finish?: string error?: { name?: string; data?: { message?: string } } }>): Response { - return jsonResponse({ data: messages.map(m => ({ ...m, id: m.id ?? 'msg-1' })), cursor: {} }) + return jsonResponse(messages.map(m => ({ + info: { + id: m.id ?? 'msg-1', + role: m.type, + time: m.time, + error: m.error, + }, + parts: m.content, + }))) } function createOpenCodeClientStub(): OpenCodeClient { @@ -240,11 +248,11 @@ describe('ScheduleService', () => { return Promise.resolve(jsonResponse({ data: { id: 'ses-run-1' } })) } - if (path === `/api/session/ses-run-1/prompt` && method === 'POST') { + if (path === `/session/ses-run-1/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-run-1/message') && method === 'GET') { + if (path.startsWith('/session/ses-run-1/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Stale status.' }], time: { created: 1, completed: 2 }, finish: 'stop' }, { type: 'assistant', content: [{ type: 'text', text: 'System health is stable.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, @@ -302,10 +310,10 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-think' } })) } - if (path === `/api/session/ses-think/prompt` && method === 'POST') { + if (path === `/session/ses-think/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-think/message') && method === 'GET') { + if (path.startsWith('/session/ses-think/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', @@ -354,10 +362,10 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return jsonResponse({ data: { id: 'ses-content-type' } }) } - if (path === `/api/session/ses-content-type/prompt` && method === 'POST') { + if (path === `/session/ses-content-type/message` && method === 'POST') { return promptReceipt() } - if (path.startsWith('/api/session/ses-content-type/message') && method === 'GET') { + if (path.startsWith('/session/ses-content-type/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -384,7 +392,7 @@ describe('ScheduleService', () => { expect(mocks.forward).toHaveBeenCalledWith( expect.objectContaining({ method: 'POST', - path: `/api/session/ses-content-type/prompt`, + path: `/session/ses-content-type/message`, headers: expect.objectContaining({ 'Content-Type': 'application/json' }), }), ) @@ -407,11 +415,11 @@ describe('ScheduleService', () => { return Promise.resolve(jsonResponse({ data: { id: 'ses-patch-fail' } })) } - if (path === `/api/session/ses-patch-fail/prompt` && method === 'POST') { + if (path === `/session/ses-patch-fail/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-patch-fail/message') && method === 'GET') { + if (path.startsWith('/session/ses-patch-fail/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Completed despite title issue.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -462,11 +470,11 @@ describe('ScheduleService', () => { return Promise.resolve(jsonResponse({ data: { id: 'ses-run-2' } })) } - if (path === `/api/session/ses-run-2/prompt` && method === 'POST') { + if (path === `/session/ses-run-2/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-run-2/message') && method === 'GET') { + if (path.startsWith('/session/ses-run-2/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Immediate status summary.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -557,10 +565,14 @@ describe('ScheduleService', () => { return Promise.resolve(jsonResponse({ data: { id: 'ses-run-6' } })) } - if (path === `/api/session/ses-run-6/prompt` && method === 'POST') { + if (path === `/session/ses-run-6/message` && method === 'POST') { return Promise.resolve(textResponse('Provider unavailable', 500)) } + if (path.startsWith('/session/ses-run-6/message') && method === 'GET') { + return Promise.resolve(v2Messages([])) + } + if (path.match(/^\/session\/[\w-]+$/) && method === 'PATCH') { return Promise.resolve(jsonResponse({})) } @@ -605,10 +617,10 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-err-v2' } })) } - if (path === `/api/session/ses-err-v2/prompt` && method === 'POST') { + if (path === `/session/ses-err-v2/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-err-v2/message') && method === 'GET') { + if (path.startsWith('/session/ses-err-v2/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Partial output' }], error: { name: 'provider_error', data: { message: 'Model crashed' } } }, ])) @@ -663,10 +675,10 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-active-long' } })) } - if (path === `/api/session/ses-active-long/prompt` && method === 'POST') { + if (path === `/session/ses-active-long/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-active-long/message') && method === 'GET') { + if (path.startsWith('/session/ses-active-long/message') && method === 'GET') { activePolls++ if (activePolls <= 155) { return Promise.resolve(v2Messages([])) @@ -722,10 +734,10 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-completed-still-active' } })) } - if (path === `/api/session/ses-completed-still-active/prompt` && method === 'POST') { + if (path === `/session/ses-completed-still-active/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-completed-still-active/message') && method === 'GET') { + if (path.startsWith('/session/ses-completed-still-active/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Completed while still active.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -778,10 +790,10 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-timeout' } })) } - if (path === `/api/session/ses-timeout/prompt` && method === 'POST') { + if (path === `/session/ses-timeout/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-timeout/message') && method === 'GET') { + if (path.startsWith('/session/ses-timeout/message') && method === 'GET') { return Promise.resolve(v2Messages([])) } if (path === '/api/session/active' && method === 'GET') { @@ -831,7 +843,7 @@ describe('ScheduleService', () => { mocks.getScheduleRunById.mockReturnValue(runningRun) mocks.updateScheduleRun.mockReturnValue(cancelledRun) routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-run-3/message') && method === 'GET') { + if (path.startsWith('/session/ses-run-3/message') && method === 'GET') { return Promise.resolve(v2Messages([])) } @@ -912,7 +924,7 @@ describe('ScheduleService', () => { mocks.getScheduleRunById.mockReturnValue(runningRun) routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-run-7/message') && method === 'GET') { + if (path.startsWith('/session/ses-run-7/message') && method === 'GET') { return Promise.resolve(v2Messages([])) } @@ -944,7 +956,7 @@ describe('ScheduleService', () => { mocks.listRunningScheduleRuns.mockReturnValue([orphanedRun]) routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-run-4/message') && method === 'GET') { + if (path.startsWith('/session/ses-run-4/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Partial summary' }] }, ])) @@ -1008,7 +1020,7 @@ describe('ScheduleService', () => { mocks.listRunningScheduleRuns.mockReturnValue([completedRun]) routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-run-8/message') && method === 'GET') { + if (path.startsWith('/session/ses-run-8/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Recovered summary' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1048,7 +1060,7 @@ describe('ScheduleService', () => { mocks.listRunningScheduleRuns.mockReturnValue([resumedRun]) mocks.getScheduleRunById.mockReturnValue(resumedRun) routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-run-9/message') && method === 'GET') { + if (path.startsWith('/session/ses-run-9/message') && method === 'GET') { messageRequests += 1 if (messageRequests === 1) { @@ -1211,7 +1223,7 @@ describe('ScheduleService', () => { mocks.getScheduleRunById.mockReturnValueOnce(runningRun).mockReturnValueOnce(runningRun).mockReturnValueOnce(completedRun) routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-run-5/message') && method === 'GET') { + if (path.startsWith('/session/ses-run-5/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Completed summary' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1264,11 +1276,11 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-skills-1' } })) } - if (path === `/api/session/ses-skills-1/prompt` && method === 'POST') { + if (path === `/session/ses-skills-1/message` && method === 'POST') { capturedPromptBody = body return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-skills-1/message') && method === 'GET') { + if (path.startsWith('/session/ses-skills-1/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1286,10 +1298,10 @@ describe('ScheduleService', () => { }) const parsed = JSON.parse(capturedPromptBody!) - expect(parsed.prompt.text).toContain('') - expect(parsed.prompt.text).toContain('') - expect(parsed.prompt.text).toContain('Release instructions here') - expect(parsed.prompt.text).toContain('Review instructions here') + expect(parsed.parts[0].text).toContain('') + expect(parsed.parts[0].text).toContain('') + expect(parsed.parts[0].text).toContain('Release instructions here') + expect(parsed.parts[0].text).toContain('Review instructions here') }) it('appends skill notes when provided', async () => { @@ -1319,11 +1331,11 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-skills-2' } })) } - if (path === `/api/session/ses-skills-2/prompt` && method === 'POST') { + if (path === `/session/ses-skills-2/message` && method === 'POST') { capturedPromptBody = body return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-skills-2/message') && method === 'GET') { + if (path.startsWith('/session/ses-skills-2/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1344,9 +1356,9 @@ describe('ScheduleService', () => { }) const parsed = JSON.parse(capturedPromptBody!) - expect(parsed.prompt.text).toContain('') - expect(parsed.prompt.text).toContain('Release instructions here') - expect(parsed.prompt.text).toContain('\nSkill notes: Focus on changelog') + expect(parsed.parts[0].text).toContain('') + expect(parsed.parts[0].text).toContain('Release instructions here') + expect(parsed.parts[0].text).toContain('\nSkill notes: Focus on changelog') }) it('does not modify the prompt when skillSlugs is empty', async () => { @@ -1371,11 +1383,11 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-skills-3' } })) } - if (path === `/api/session/ses-skills-3/prompt` && method === 'POST') { + if (path === `/session/ses-skills-3/message` && method === 'POST') { capturedPromptBody = body return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-skills-3/message') && method === 'GET') { + if (path.startsWith('/session/ses-skills-3/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1396,7 +1408,7 @@ describe('ScheduleService', () => { }) const parsed = JSON.parse(capturedPromptBody!) - expect(parsed.prompt.text).toBe(job.prompt) + expect(parsed.parts[0].text).toBe(job.prompt) }) it('falls back to name-only injection when skill endpoint fails', async () => { @@ -1424,11 +1436,11 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-skills-4' } })) } - if (path === `/api/session/ses-skills-4/prompt` && method === 'POST') { + if (path === `/session/ses-skills-4/message` && method === 'POST') { capturedPromptBody = body return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-skills-4/message') && method === 'GET') { + if (path.startsWith('/session/ses-skills-4/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1449,7 +1461,7 @@ describe('ScheduleService', () => { }) const parsed = JSON.parse(capturedPromptBody!) - expect(parsed.prompt.text).toContain('For this task, use the following skills: git-release') + expect(parsed.parts[0].text).toContain('For this task, use the following skills: git-release') }) it('falls back gracefully when a skill slug is not found in the list', async () => { @@ -1477,11 +1489,11 @@ describe('ScheduleService', () => { if (path === '/api/session' && method === 'POST') { return Promise.resolve(jsonResponse({ data: { id: 'ses-skills-5' } })) } - if (path === `/api/session/ses-skills-5/prompt` && method === 'POST') { + if (path === `/session/ses-skills-5/message` && method === 'POST') { capturedPromptBody = body return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-skills-5/message') && method === 'GET') { + if (path.startsWith('/session/ses-skills-5/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Done.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1502,7 +1514,7 @@ describe('ScheduleService', () => { }) const parsed = JSON.parse(capturedPromptBody!) - expect(parsed.prompt.text).toContain('For this task, use the following skills: unknown-skill') + expect(parsed.parts[0].text).toContain('For this task, use the following skills: unknown-skill') }) }) }) @@ -1552,10 +1564,10 @@ describe('ScheduleService worktree isolation', () => { expect(parsed.location.directory).toBe(worktreePath) return Promise.resolve(jsonResponse({ data: { id: 'ses-wt-1' } })) } - if (path === `/api/session/ses-wt-1/prompt` && method === 'POST') { + if (path === `/session/ses-wt-1/message` && method === 'POST') { return Promise.resolve(textResponse('')) } - if (path.startsWith('/api/session/ses-wt-1/message') && method === 'GET') { + if (path.startsWith('/session/ses-wt-1/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Worktree run done.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1599,10 +1611,10 @@ describe('ScheduleService worktree isolation', () => { if (path.match(/^\/session\/[\w-]+$/) && method === 'PATCH') { return Promise.resolve(jsonResponse({})) } - if (path === `/api/session/ses-wt-1/prompt` && method === 'POST') { + if (path === `/session/ses-wt-1/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-wt-1/message') && method === 'GET') { + if (path.startsWith('/session/ses-wt-1/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Worktree run done.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1646,10 +1658,10 @@ describe('ScheduleService worktree isolation', () => { capturedDirectory = parsed.location.directory return Promise.resolve(jsonResponse({ data: { id: 'ses-inline-1' } })) } - if (path === `/api/session/ses-inline-1/prompt` && method === 'POST') { + if (path === `/session/ses-inline-1/message` && method === 'POST') { return Promise.resolve(promptReceipt()) } - if (path.startsWith('/api/session/ses-inline-1/message') && method === 'GET') { + if (path.startsWith('/session/ses-inline-1/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Inline run done.' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1695,7 +1707,7 @@ describe('ScheduleService worktree isolation', () => { setupWorktreeFinalize('ghi789') routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-cancel-wt/message') && method === 'GET') { + if (path.startsWith('/session/ses-cancel-wt/message') && method === 'GET') { return Promise.resolve(v2Messages([])) } if (path === `/api/session/ses-cancel-wt/interrupt` && method === 'POST') { @@ -1735,7 +1747,7 @@ describe('ScheduleService worktree isolation', () => { // Session exists but has no completed message — triggers finalizeRecoveredRun routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-recover-wt/message') && method === 'GET') { + if (path.startsWith('/session/ses-recover-wt/message') && method === 'GET') { return Promise.resolve(v2Messages([])) } if (path === '/api/session/active' && method === 'GET') { @@ -1774,7 +1786,7 @@ describe('ScheduleService worktree isolation', () => { activeTeardowns.add('42:7:5') routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-race-double/message') && method === 'GET') { + if (path.startsWith('/session/ses-race-double/message') && method === 'GET') { return Promise.resolve(v2Messages([ { type: 'assistant', content: [{ type: 'text', text: 'Already done' }], time: { created: Math.floor(Date.now() / 1000), completed: Math.floor(Date.now() / 1000) }, finish: 'stop' }, ])) @@ -1818,7 +1830,7 @@ describe('ScheduleService worktree isolation', () => { mocks.getScheduleRunById.mockReturnValue(runningRun) routeForward(({ path, method }) => { - if (path.startsWith('/api/session/ses-guard-cycle/message') && method === 'GET') { + if (path.startsWith('/session/ses-guard-cycle/message') && method === 'GET') { return Promise.resolve(v2Messages([])) } if (path === `/api/session/ses-guard-cycle/interrupt` && method === 'POST') {