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
address review (agy): reset string tracking at line breaks, guard the primitive parse, and keep the rescue non-fatal
[._.]/ Adam Eivy5d7aa40124da
diff --git a/server/lib/agentSentinel.js b/server/lib/agentSentinel.jsindex 640662984e..f5c682e6ae 100644--- a/server/lib/agentSentinel.js+++ b/server/lib/agentSentinel.js@@ -135,7 +135,10 @@ export async function extractSentinelPayloadFromTranscript(transcript, isPayloadfor (let i = blocks.length - 1; i >= 0; i -= 1) {const parsed = safeJSONParse(blocks[i], null, { allowArray: false });- if (!parsed) continue;+ // `typeof` guard, not just truthiness: the `in` check below throws on a+ // primitive, and a best-effort salvage must never be the thing that breaks+ // finalize.+ if (!parsed || typeof parsed !== 'object') continue;// Two shapes reach a transcript: the documented `{ summary, payload }`// envelope the agent should have written to the sentinel, and the BARE// payload object — what a model answering in the terminal actually prints.diff --git a/server/lib/agentSentinel.test.js b/server/lib/agentSentinel.test.jsindex a8fcff028e..96dfa0bab1 100644--- a/server/lib/agentSentinel.test.js+++ b/server/lib/agentSentinel.test.js@@ -161,6 +161,11 @@ describe('extractSentinelPayloadFromTranscript', () => {.toEqual({ summary: '', payload: null });});+ it('recovers the answer even when an earlier line left an unmatched quote', async () => {+ const { payload } = await extractSentinelPayloadFromTranscript(`\u2502 renaming "foo\n${ANSWER}`, isReasonerPayload);+ expect(payload).not.toBeNull();+ });+it('yields no payload for JSON that is not the hook deliverable', async () => {expect((await extractSentinelPayloadFromTranscript('{"tokens":1200}', isReasonerPayload)).payload).toBeNull();});diff --git a/server/lib/jsonExtract.js b/server/lib/jsonExtract.jsindex fa06f340c6..ae550dffb5 100644--- a/server/lib/jsonExtract.js+++ b/server/lib/jsonExtract.js@@ -91,7 +91,8 @@ export function findBalancedBlocks(s, { startChar = '{', endChar = '}' } = {}) {* object of the last-printed block before any of its children.** Still one linear pass (an unmatched `}` with an empty stack is dropped), so- * scanning a large tail stays cheap.+ * scanning a large tail stays cheap. String tracking resets at every line break+ * — see the note in the loop for why a transcript needs that.** @param {string} s — input text* @param {object} [options]@@ -107,6 +108,12 @@ export function findAllBalancedBlocks(s, { startChar = '{', endChar = '}' } = {}let escaped = false;for (let i = 0; i < s.length; i += 1) {const ch = s[i];+ // A line break ends string tracking. JSON forbids a raw newline inside a+ // string literal, so nothing valid is lost — but a transcript is full of+ // stray quotes (a truncated redraw, an echoed `echo "…`, a log message),+ // and without this ONE of them would leave the walker "inside a string"+ // for the rest of the scan, hiding every brace in the JSON that follows.+ if (ch === '\n' || ch === '\r') { inString = false; escaped = false; continue; }if (inString) {if (escaped) escaped = false;else if (ch === '\\') escaped = true;diff --git a/server/lib/jsonExtract.test.js b/server/lib/jsonExtract.test.jsindex c34f142aa2..6e04084c3f 100644--- a/server/lib/jsonExtract.test.js+++ b/server/lib/jsonExtract.test.js@@ -273,6 +273,13 @@ describe('findAllBalancedBlocks', () => {expect(findAllBalancedBlocks('{"a":"{ not a brace \\" }"}')).toEqual(['{"a":"{ not a brace \\" }"}']);});+ it('does not let a stray quote on one line swallow the JSON on the next', () => {+ // A truncated PTY redraw leaves an unmatched `"`; without the line-break+ // reset the walker would treat the rest of the transcript as string content.+ const transcript = '\u2502 renaming "foo to bar\n{"a":1}\n';+ expect(findAllBalancedBlocks(transcript)).toEqual(['{"a":1}']);+ });+it('returns [] for non-string or empty input', () => {expect(findAllBalancedBlocks(null)).toEqual([]);expect(findAllBalancedBlocks('')).toEqual([]);diff --git a/server/services/agentFinalization.js b/server/services/agentFinalization.jsindex 6b2e0885dc..fb561b5e84 100644--- a/server/services/agentFinalization.js+++ b/server/services/agentFinalization.js@@ -747,14 +747,24 @@ const TRANSCRIPT_RESCUE_TAIL_BYTES = 256 * 1024;*/async function rescueTranscriptPayload({ agentId, taskType }) {if (!agentId || !isProgrammaticIoTaskType(taskType)) return null;- const isPayload = await getTaskOutputPayloadPredicate(taskType);- if (!isPayload) return null;- const { PATHS, readFileTail } = await import('../lib/fileUtils.js');- const transcript = await readFileTail(join(PATHS.cosAgents, agentId, 'raw.txt'), TRANSCRIPT_RESCUE_TAIL_BYTES);- if (!transcript) return null;- const { extractSentinelPayloadFromTranscript } = await import('../lib/agentSentinel.js');- const { payload } = await extractSentinelPayloadFromTranscript(transcript, isPayload);- return payload ?? null;+ // Sanctioned try/catch (see CLAUDE.md — this runs outside the request+ // lifecycle): a best-effort salvage must never be able to make the outcome+ // WORSE than not attempting it. A hook whose own shape predicate throws would+ // otherwise abort the dispatch and skip the hook entirely, turning "we+ // couldn't recover the payload" into "the hook never ran".+ try {+ const isPayload = await getTaskOutputPayloadPredicate(taskType);+ if (!isPayload) return null;+ const { PATHS, readFileTail } = await import('../lib/fileUtils.js');+ const transcript = await readFileTail(join(PATHS.cosAgents, agentId, 'raw.txt'), TRANSCRIPT_RESCUE_TAIL_BYTES);+ if (!transcript) return null;+ const { extractSentinelPayloadFromTranscript } = await import('../lib/agentSentinel.js');+ const { payload } = await extractSentinelPayloadFromTranscript(transcript, isPayload);+ return payload ?? null;+ } catch (err) {+ emitLog('warn', `⚠️ Transcript payload rescue failed for ${agentId} (${taskType}): ${err.message}`, { agentId });+ return null;+ }}/**