From 5db74e3fa7ce5128f7d5ebc1e22dfacbb2dd915a Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Fri, 30 Oct 2026 01:03:14 -0400 Subject: [PATCH] fix(goals): validate the goal RPC output before returning it --- backend/test/shared/opencode-goal.test.ts | 9 +++++++++ shared/src/opencode/goal.ts | 5 ++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/test/shared/opencode-goal.test.ts b/backend/test/shared/opencode-goal.test.ts index c20fc0cb..e7056a5a 100644 --- a/backend/test/shared/opencode-goal.test.ts +++ b/backend/test/shared/opencode-goal.test.ts @@ -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 }) diff --git a/shared/src/opencode/goal.ts b/shared/src/opencode/goal.ts index 41c9a953..2336de36 100644 --- a/shared/src/opencode/goal.ts +++ b/shared/src/opencode/goal.ts @@ -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