Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: validate LLM organization response shape and filter unknown IDs

Adam Eivyf61d2d91deb4
diff --git a/server/services/identity.js b/server/services/identity.js
index 41de2d8ce3..24921c9415 100644
--- a/server/services/identity.js
+++ b/server/services/identity.js
@@ -1225,6 +1225,18 @@ Respond with JSON only (no markdown fences). The response must be an object with
throw new ServerError(`AI returned invalid organization data: ${e.message}`, { status: 502, code: 'AI_PARSE_ERROR' });
}
+ // Validate required shape from LLM response
+ if (!parsed || typeof parsed !== 'object' || !Array.isArray(parsed.organization)) {
+ throw new ServerError('AI returned unexpected shape: missing organization array', { status: 502, code: 'AI_PARSE_ERROR' });
+ }
+ if (!parsed.apexGoal || typeof parsed.apexGoal !== 'object') {
+ throw new ServerError('AI returned unexpected shape: missing apexGoal', { status: 502, code: 'AI_PARSE_ERROR' });
+ }
+
+ // Filter organization to only known goal IDs
+ const goalIds = new Set(activeGoals.map(g => g.id));
+ parsed.organization = parsed.organization.filter(item => item.id && goalIds.has(item.id));
+
console.log(`🎯 Organized ${activeGoals.length} goals into hierarchy`);
return parsed;
}
diff --git a/server/services/identity.test.js b/server/services/identity.test.js
index d1a8b90361..4fd368d394 100644
--- a/server/services/identity.test.js
+++ b/server/services/identity.test.js
@@ -1802,18 +1802,9 @@ describe('Integration: organizeGoals', () => {
}))
}));
+ // callProviderAISimple mock set per-test to use actual goal IDs
vi.doMock('../lib/aiProvider.js', () => ({
- callProviderAISimple: vi.fn(async () => ({
- text: JSON.stringify({
- apexGoal: { existingId: null, suggestedTitle: 'Live fully', suggestedDescription: 'The ultimate purpose' },
- organization: [
- { id: 'g1', goalType: 'sub-apex', suggestedParentId: null, reasoning: 'Major life pillar' },
- { id: 'g2', goalType: 'standard', suggestedParentId: 'g1', reasoning: 'Supports pillar' }
- ],
- suggestedSubApex: [],
- analysis: 'Your goals center around personal growth.'
- })
- })),
+ callProviderAISimple: vi.fn(async () => ({ text: '{}' })),
parseLLMJSON: vi.fn((text) => JSON.parse(text))
}));
@@ -1823,8 +1814,23 @@ describe('Integration: organizeGoals', () => {
});
it('should return LLM organization suggestion', async () => {
- await createGoal({ title: 'Health & Fitness' });
- await createGoal({ title: 'Learn Spanish' });
+ const goal1 = await createGoal({ title: 'Health & Fitness' });
+ const goal2 = await createGoal({ title: 'Learn Spanish' });
+
+ // Re-mock with actual goal IDs
+ const { callProviderAISimple, parseLLMJSON } = await import('../lib/aiProvider.js');
+ callProviderAISimple.mockResolvedValue({
+ text: JSON.stringify({
+ apexGoal: { existingId: null, suggestedTitle: 'Live fully', suggestedDescription: 'The ultimate purpose' },
+ organization: [
+ { id: goal1.id, goalType: 'sub-apex', suggestedParentId: null, reasoning: 'Major life pillar' },
+ { id: goal2.id, goalType: 'standard', suggestedParentId: goal1.id, reasoning: 'Supports pillar' }
+ ],
+ suggestedSubApex: [],
+ analysis: 'Your goals center around personal growth.'
+ })
+ });
+ parseLLMJSON.mockImplementation((text) => JSON.parse(text));
const result = await organizeGoals();
@@ -1832,7 +1838,7 @@ describe('Integration: organizeGoals', () => {
expect(result.apexGoal.suggestedTitle).toBe('Live fully');
expect(result.organization).toHaveLength(2);
expect(result.organization[0].goalType).toBe('sub-apex');
- expect(result.organization[1].suggestedParentId).toBe('g1');
+ expect(result.organization[1].suggestedParentId).toBe(goal1.id);
expect(result.analysis).toContain('personal growth');
});