fix(goals): validate the goal RPC output before returning it

This commit is contained in:
Chris Scott committed 2026-10-30 01:03:14 -04:00
1 parent cbe92c92ad
commit 5db74e3fa7
2 files changed
+13 -1

No files matched your search

@@ -91,6 +91,15 @@ describe('readSessionGoal', () => {
})
})
it.each([
['an unknown status', goalWith({ status: 'bogus' as GoalState['status'] })],
['a missing objective', goalWithout('objective')],
['a non-object output', 'not a goal'],
])('reports a goal with %s as unsupported', async (_label, output) => {
const call = vi.fn().mockResolvedValue({ output })
await expect(readSessionGoal(createApi(call), { sessionID: SESSION_ID })).resolves.toEqual({ supported: false })
})
it('reports an unavailable rpc as unsupported', async () => {
const call = vi.fn().mockRejectedValue(rpcError('rpc.unavailable', 'RPC is unavailable: opencode.goal'))
await expect(readSessionGoal(createApi(call), { sessionID: SESSION_ID })).resolves.toEqual({ supported: false })
+4 -1
View File
@@ -50,7 +50,10 @@ export async function readSessionGoal(
input: { sessionID: input.sessionID },
...(input.directory ? { location: { directory: input.directory } } : {}),
})
return { supported: true, goal: (result.output ?? null) as GoalState | null }
const output: unknown = result.output ?? null
if (output === null) return { supported: true, goal: null }
if (!isGoalState(output)) return { supported: false }
return { supported: true, goal: output }
} catch (error) {
if (isRpcError(error) && error.type === 'rpc.unavailable') return { supported: false }
throw error