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

fix([med-ai-dispatch-server-services-aidetect-js-242]): address codex review — gate parseAiResponse with a detection-shape predicate

[._.]/ Adam Eivyc824c1a55b11
diff --git a/server/services/aiDetect.js b/server/services/aiDetect.js
index ef01b22b2e..20e684fa2d 100644
--- a/server/services/aiDetect.js
+++ b/server/services/aiDetect.js
@@ -116,13 +116,18 @@ Rules:
- If the app has both frontend and backend, suggest separate PM2 processes`;
}
+// Match the detection-response JSON, not the package.json the prompt echoes
+// back. Prompt-echoing CLI/TUI providers (Codex, Claude Code) replay the input
+// before the real answer; without a shape gate, extractJson would lock onto
+// the echoed `{ "name": "...", "scripts": {...} }` block and return defaults.
+const isDetectionShape = (v) => (
+ v && typeof v === 'object' && !Array.isArray(v)
+ && ('hasFrontend' in v || 'hasBackend' in v || 'uiPort' in v || 'apiPort' in v || 'pm2ProcessNames' in v || 'startCommands' in v)
+);
+
function parseAiResponse(response) {
- // Route through the shared extractor so banner-stripping, trailing-comma
- // repair, and the `[...]` placeholder elision the rest of PortOS's LLM
- // callers benefit from also apply here — TUI providers in particular emit
- // banner text around the JSON payload that the legacy regex would miss.
- const { value } = extractJson(response);
- if (!value || typeof value !== 'object') throw new Error('Failed to parse AI detection response');
+ const { value } = extractJson(response, { shapePredicate: isDetectionShape });
+ if (!isDetectionShape(value)) throw new Error('Failed to parse AI detection response');
return value;
}
diff --git a/server/services/aiDetect.test.js b/server/services/aiDetect.test.js
index d09da9ed25..b5dd3c43f8 100644
--- a/server/services/aiDetect.test.js
+++ b/server/services/aiDetect.test.js
@@ -133,4 +133,24 @@ describe('detectAppWithAi', () => {
expect(result.detected.name).toBe('Test App');
});
});
+
+ it('skips an echoed package.json block when picking the detection JSON (TUI prompt-echo case)', async () => {
+ const provider = { id: 'p', name: 'P', type: 'tui', enabled: true };
+ getActiveProvider.mockResolvedValue(provider);
+ // Prompt-echoing TUI providers replay the package.json from the prompt
+ // before the real answer — the first parseable block must be skipped.
+ const echoedPackageJson = JSON.stringify({ name: 'echoed-from-prompt', scripts: { dev: 'vite' } });
+ runPromptThroughProvider.mockResolvedValue({
+ text: 'package.json:\n' + echoedPackageJson + '\n\nMy answer:\n' + VALID_DETECTION_JSON,
+ runId: 'r1',
+ model: 'm1'
+ });
+
+ await withProjectDir({ 'package.json': echoedPackageJson }, async (dir) => {
+ const result = await detectAppWithAi(dir);
+ expect(result.success).toBe(true);
+ expect(result.detected.name).toBe('Test App');
+ expect(result.detected.uiPort).toBe(3000);
+ });
+ });
});