mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix(ocm): abort the feature probe with the caller signal and keep fusion request ids across reopened multi-run commands
This commit is contained in:
1 parent
c41149dc39
commit
390f95710c
4 files changed
+42
-11
No files matched your search
@@ -151,21 +151,30 @@ export class ManagerApi {
|
||||
...requestInit,
|
||||
headers: { ...baseHeaders, ...(requestInit.headers as Record<string, string> | undefined) },
|
||||
})
|
||||
if (!res.ok) throw await this.handleErrorResponse(res, operation, feature === true)
|
||||
if (!res.ok) throw await this.handleErrorResponse(res, operation, feature === true, requestInit.signal ?? undefined)
|
||||
return parse(await res.json())
|
||||
}
|
||||
|
||||
private async handleErrorResponse(res: Response, operation: string, feature: boolean): Promise<ManagerApiError> {
|
||||
private async handleErrorResponse(
|
||||
res: Response,
|
||||
operation: string,
|
||||
feature: boolean,
|
||||
signal?: AbortSignal,
|
||||
): Promise<ManagerApiError> {
|
||||
const error = await formatErrorResponse(res, operation)
|
||||
if (feature && error.status === 401) {
|
||||
return this.probeFeatureSupport(operation, error)
|
||||
return this.probeFeatureSupport(operation, error, signal)
|
||||
}
|
||||
return error
|
||||
}
|
||||
|
||||
private async probeFeatureSupport(operation: string, fallback: ManagerApiError): Promise<ManagerApiError> {
|
||||
private async probeFeatureSupport(
|
||||
operation: string,
|
||||
fallback: ManagerApiError,
|
||||
signal?: AbortSignal,
|
||||
): Promise<ManagerApiError> {
|
||||
try {
|
||||
const res = await fetch(`${this.baseUrl}/api/internal/opencode-workspaces`, { headers: this.headers() })
|
||||
const res = await fetch(`${this.baseUrl}/api/internal/opencode-workspaces`, { headers: this.headers(), signal })
|
||||
if (res.ok) {
|
||||
return new ManagerApiError(
|
||||
`${operation} failed: this OpenCode Manager is too old for ocm goals and multi-runs; upgrade the Manager.`,
|
||||
|
||||
@@ -258,9 +258,9 @@ function fusionRequestKey(runId: number, form: FusionFormInput): string {
|
||||
return JSON.stringify([runId, [...form.entryIds].sort((a, b) => a - b), form.model, form.baseRef.trim(), form.instructions.trim()])
|
||||
}
|
||||
|
||||
function createMultiRunActions(context: Context, api: ManagerApi, repoId: number): MultiRunActions {
|
||||
const pendingFusions = new Map<string, string>()
|
||||
const pendingFusions = new Map<string, string>()
|
||||
|
||||
function createMultiRunActions(context: Context, api: ManagerApi, repoId: number): MultiRunActions {
|
||||
const attempt = async <Value>(operation: () => Promise<Value>): Promise<ActionResult<Value>> => {
|
||||
try {
|
||||
return { ok: true, value: await operation() }
|
||||
|
||||
@@ -261,6 +261,21 @@ describe('ManagerApi feature support', () => {
|
||||
expect((error as ManagerApiError).code).toBeNull()
|
||||
expect(isManagerRouteMissing(error)).toBe(false)
|
||||
})
|
||||
|
||||
it('forwards the caller abort signal to the token probe', async () => {
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce(errorResponse(401, JSON.stringify({ error: 'Unauthorized' })))
|
||||
.mockResolvedValueOnce(okResponse({ workspaces: [] }))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const controller = new AbortController()
|
||||
|
||||
await api.getLatestSessionGoal('ses_1', controller.signal).catch(() => undefined)
|
||||
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(2, `${BASE_URL}/api/internal/opencode-workspaces`, {
|
||||
headers: { Authorization: 'Bearer tok' },
|
||||
signal: controller.signal,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('isManagerRouteMissing', () => {
|
||||
|
||||
@@ -660,17 +660,24 @@ describe('multi-run dialog actions', () => {
|
||||
expect(fake.open).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('reuses the fusion request id while a network error leaves the outcome unknown', async () => {
|
||||
it('reuses the fusion request id across reopened commands while a network error leaves the outcome unknown', async () => {
|
||||
const fake = createFakeContext()
|
||||
const api = makeApi()
|
||||
api.fuseMultiRun.mockRejectedValue(new Error('offline'))
|
||||
api.fuseMultiRun.mockRejectedValueOnce(new Error('offline'))
|
||||
api.fuseMultiRun.mockRejectedValueOnce(new Error('offline'))
|
||||
api.fuseMultiRun.mockImplementationOnce(async (_runId: number, request: { requestId: string }) =>
|
||||
multiRun({ fusions: [fusion({ requestId: request.requestId, sessionId: 'ses_f' })] }),
|
||||
)
|
||||
|
||||
const actions = await launchActions(fake, api)
|
||||
|
||||
await expect(actions.fuse(multiRun(), fusionForm())).resolves.toBe('offline')
|
||||
await expect(actions.fuse(multiRun(), fusionForm())).resolves.toBe('offline')
|
||||
const reopened = await launchActions(createFakeContext(), api)
|
||||
await expect(reopened.fuse(multiRun(), fusionForm())).resolves.toBeNull()
|
||||
|
||||
const [first, second] = api.fuseMultiRun.mock.calls.map((call) => (call[1] as { requestId: string }).requestId)
|
||||
const [first, second, third] = api.fuseMultiRun.mock.calls.map((call) => (call[1] as { requestId: string }).requestId)
|
||||
expect(second).toBe(first)
|
||||
expect(third).toBe(first)
|
||||
})
|
||||
|
||||
it('mints a fresh fusion request id once the Manager has answered', async () => {
|
||||
|
||||
Reference in new issue
Block a user