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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: recover missing LLM JSON quotes
[._.]/ Adam Eivy01d24f17bd54
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 672387c64f..1d6153dbfa 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -56,7 +56,7 @@## Fixed-- **Complete LLM answers no longer fail on recoverable JSON serialization mistakes.** A long character-foundation repair could begin as normal JSON, then emit its complete remaining objects as escaped `\\n` / `\\"` text and leave quotation marks inside prose unescaped. The shared parser now decodes an escaped tail only from the JSON parser's exact failure position, escapes only quotes that cannot legally close their string, and accepts repairs only when the entire reconstructed response parses—preserving expensive completed work without making partial or arbitrary text look valid.+- **Complete LLM answers no longer fail on recoverable JSON serialization mistakes.** Long foundation calls could begin as normal JSON, then emit the complete remaining objects as escaped `\\n` / `\\"` text, leave quotation marks inside prose unescaped, or omit a prose field's closing quote immediately before its structural `}` line. The shared parser now decodes an escaped tail only from the JSON parser's exact failure position, repairs quotes only at structurally unambiguous boundaries, and accepts repairs only when the entire reconstructed response parses—preserving expensive completed work without making partial or arbitrary text look valid.- **The foundation judge no longer mistakes visual prompt families for binding story canon.** Universe Builder categories are additive image-prompt design systems, but the foundation judge rendered their labels beside the narrative bible and named canon, then penalized duplicate-looking creatures, vehicles, and artifacts even though its world-repair editor can only revise narrative world rules. That produced a structurally unwinnable loop: the critic demanded canon-tier metadata or inventory pruning from a repair path that could not—and must not—delete the user's visual assets. Foundation scoring now reads the universe logline, premise, style, influences, and named canon only; visual prompt families remain intact and available for rendering, but edits to them no longer invalidate or distort a narrative verdict.diff --git a/server/lib/jsonExtract.js b/server/lib/jsonExtract.jsindex 96b1faed18..12f82add1f 100644--- a/server/lib/jsonExtract.js+++ b/server/lib/jsonExtract.js@@ -221,6 +221,54 @@ function escapeControlCharsInStrings(input) {return out;}+/**+ * Close a string whose final quote was omitted immediately before a structural+ * `}` / `]` line. A raw line break is illegal inside JSON strings; when its next+ * non-horizontal-whitespace character closes the containing object or array,+ * the only structurally coherent interpretation is that the prose value ended+ * before the line break. Other raw newlines remain untouched for the existing+ * control-character escape repair.+ */+function closeStringBeforeStructuralLine(input) {+ if (typeof input !== 'string' || !input) return input;+ let out = '';+ let inString = false;+ let escaped = false;+ for (let i = 0; i < input.length; i += 1) {+ const ch = input[i];+ if (!inString) {+ out += ch;+ if (ch === '"') inString = true;+ continue;+ }+ if (escaped) {+ out += ch;+ escaped = false;+ continue;+ }+ if (ch === '\\') {+ out += ch;+ escaped = true;+ continue;+ }+ if (ch === '"') {+ out += ch;+ inString = false;+ continue;+ }+ if (ch === '\n' || ch === '\r') {+ let next = i + 1;+ while (next < input.length && (input[next] === ' ' || input[next] === '\t')) next += 1;+ if (input[next] === '}' || input[next] === ']') {+ out += '"';+ inString = false;+ }+ }+ out += ch;+ }+ return out;+}+/*** Escape a quote that appears inside a JSON string without its required* backslash. A quote can close a JSON string only when the next non-whitespace@@ -282,6 +330,7 @@ function escapeBareQuotesInStrings(input) {* result when the reconstructed whole document parses.* - Unescaped quotation marks inside prose string values — escape only a* quote that cannot legally close the string because prose follows it.+ * - A missing closing quote immediately before a structural `}` / `]` line.* - Raw control chars (literal newlines/tabs) inside a string value —* escaped to their `\n`/`\t`/`\uXXXX` forms (see* escapeControlCharsInStrings). Common when a model writes a long@@ -315,8 +364,14 @@ export function tryParseWithRepair(jsonText) {if (!tailResult.error) return tailResult;}- const escapedQuotes = escapeBareQuotesInStrings(unescapedTail);- if (escapedQuotes !== unescapedTail) {+ const closedStrings = closeStringBeforeStructuralLine(unescapedTail);+ if (closedStrings !== unescapedTail) {+ const closedResult = safeParse(closedStrings);+ if (!closedResult.error) return closedResult;+ }++ const escapedQuotes = escapeBareQuotesInStrings(closedStrings);+ if (escapedQuotes !== closedStrings) {const quotesResult = safeParse(escapedQuotes);if (!quotesResult.error) return quotesResult;}diff --git a/server/lib/jsonExtract.test.js b/server/lib/jsonExtract.test.jsindex fabf6a2d4b..4ec2bea313 100644--- a/server/lib/jsonExtract.test.js+++ b/server/lib/jsonExtract.test.js@@ -112,6 +112,25 @@ describe('jsonExtract.tryParseWithRepair', () => {});});+ it('closes a prose string whose final quote is missing before its structural close line', () => {+ const bad = '{\n "dimensions": {\n "world": {\n "score": 8,\n "fix": "Make each culture arise from its own history.\n },\n "craft": { "score": 7 }\n }\n}';+ expect(tryParseWithRepair(bad)).toEqual({+ value: {+ dimensions: {+ world: { score: 8, fix: 'Make each culture arise from its own history.' },+ craft: { score: 7 },+ },+ },+ });+ });++ it('preserves an ordinary raw multiline string for the control-character repair', () => {+ const bad = '{"body":"line one\nline two","ok":true}';+ expect(tryParseWithRepair(bad)).toEqual({+ value: { body: 'line one\nline two', ok: true },+ });+ });+it('combines serialized-tail and bare-quote repairs in one completed response', () => {const bad = '{"items":[{"voice":"plain"}\\n, {\\n \\"voice\\": \\"Calls it \\"witness\\" before answering.\\"\\n}\\n]}';expect(tryParseWithRepair(bad)).toEqual({