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: fix stripCodeFences whitespace + soundness fixes in two tests

[._.]/ Adam Eivy07943538ddda
diff --git a/server/lib/aiProvider.js b/server/lib/aiProvider.js
index 6f5883c9fa..e9f7c7f57c 100644
--- a/server/lib/aiProvider.js
+++ b/server/lib/aiProvider.js
@@ -210,9 +210,13 @@ export async function callProviderAISimple(provider, model, prompt, options = {}
/**
* Strip markdown code fences from LLM output before JSON.parse.
+ *
+ * Trims surrounding whitespace BEFORE the fence regex so common LLM shapes
+ * with trailing newlines/spaces around the closing fence (e.g. "```json\n{}\n```\n")
+ * still get the closing ``` stripped — the regex anchors on end-of-string.
*/
export function stripCodeFences(raw) {
- return raw.replace(/^```(?:json)?\n?/, '').replace(/\n?```$/, '').trim();
+ return raw.trim().replace(/^```(?:json)?\n?/, '').replace(/\n?```$/, '').trim();
}
/**
diff --git a/server/lib/aiProvider.test.js b/server/lib/aiProvider.test.js
index 09a0f4bf16..22b93a3296 100644
--- a/server/lib/aiProvider.test.js
+++ b/server/lib/aiProvider.test.js
@@ -30,6 +30,15 @@ describe('aiProvider pure helpers', () => {
const raw = '{"src":"```foo```"}';
expect(stripCodeFences(raw)).toBe('{"src":"```foo```"}');
});
+
+ it('strips fences with surrounding whitespace (real LLM output shape)', () => {
+ // LLMs commonly emit a trailing newline after the closing fence — the
+ // strip helper must tolerate it so the closing ``` still goes away.
+ expect(stripCodeFences('```json\n{"a":1}\n```\n')).toBe('{"a":1}');
+ expect(stripCodeFences('```json\n{"a":1}\n``` ')).toBe('{"a":1}');
+ expect(stripCodeFences(' ```json\n{"a":1}\n``` ')).toBe('{"a":1}');
+ expect(stripCodeFences('\n\n```\n{"a":1}\n```\n\n')).toBe('{"a":1}');
+ });
});
describe('parseLLMJSON', () => {
diff --git a/server/lib/pipelineIssueOrder.test.js b/server/lib/pipelineIssueOrder.test.js
index 72253de098..bf2b0d6abc 100644
--- a/server/lib/pipelineIssueOrder.test.js
+++ b/server/lib/pipelineIssueOrder.test.js
@@ -1,8 +1,14 @@
-import { describe, it, expect } from 'vitest';
+import { describe, it, expect, beforeEach } from 'vitest';
import { applyVolumeOrderedNumbers, UNSCOPED_ANCHOR } from './pipelineIssueOrder.js';
+// Deterministic counter for auto-generated ids — keeps test failures readable
+// (no randomized ids in error messages) and avoids any future test
+// accidentally depending on Math.random ordering.
+let _issueIdCounter = 0;
+beforeEach(() => { _issueIdCounter = 0; });
+
const mkIssue = (over = {}) => ({
- id: over.id || `i${Math.random().toString(36).slice(2, 7)}`,
+ id: over.id || `i${++_issueIdCounter}`,
seriesId: 's1',
seasonId: over.seasonId ?? null,
arcPosition: over.arcPosition ?? 1,
diff --git a/server/lib/providerModels.test.js b/server/lib/providerModels.test.js
index a09b9a0fa9..54be9ff6d9 100644
--- a/server/lib/providerModels.test.js
+++ b/server/lib/providerModels.test.js
@@ -134,20 +134,33 @@ describe('providerModels', () => {
});
});
- it('hasModelFlag and extractBakedModel stay consistent on common shapes', () => {
+ it('extractBakedModel returning a value implies hasModelFlag is true', () => {
+ // The sound direction: if extractBakedModel finds a real value, the args
+ // definitely contain a usable model flag. The reverse direction does NOT
+ // hold for adversarial argv shapes — extractBakedModel returns early on
+ // the first --model/-m it sees and may give up (returning null) on a
+ // valueless first flag even when a later --model has a real value.
const shapes = [
['--model', 'gpt-5'],
['-m', 'gpt-5'],
['--model=gpt-5'],
['-m=gpt-5'],
['--model'],
- ['--model=']
+ ['--model='],
+ // Adversarial: first flag has no value, second one does. Documents
+ // current early-exit behavior — extractBakedModel returns null on the
+ // first '--model' (because next is '--other'), so hasModelFlag may
+ // disagree with it. We only assert the sound direction.
+ ['--model', '--other', '--model', 'gpt-5'],
+ // Mixed argv with other tool flags before the model pin.
+ ['--temperature', '0.7', '--model', 'gpt-5']
];
for (const args of shapes) {
const has = hasModelFlag(args);
const baked = extractBakedModel(args);
- // If hasModelFlag says yes, extractBakedModel must produce a non-null value.
- if (has) expect(baked, `args=${JSON.stringify(args)}`).not.toBeNull();
+ if (baked !== null) {
+ expect(has, `args=${JSON.stringify(args)}`).toBe(true);
+ }
}
});
});