(text)
| 81 | // bridge call the Brain emits is always the first occurrence in stdout; taking |
| 82 | // the last would match an example inside the task and mis-spawn. |
| 83 | function extractFirstSpawnPayload(text) { |
| 84 | const s = String(text || ''); |
| 85 | const idx = s.indexOf(SPAWN_MARKER); |
| 86 | if (idx === -1) return null; |
| 87 | |
| 88 | // Locate the opening brace after the marker (allow only whitespace between). |
| 89 | let braceStart = -1; |
| 90 | for (let i = idx + SPAWN_MARKER.length; i < s.length; i++) { |
| 91 | if (s[i] === '{') { braceStart = i; break; } |
| 92 | if (!/\s/.test(s[i])) break; // anything other than whitespace before '{' = malformed |
| 93 | } |
| 94 | if (braceStart === -1) return null; |
| 95 | |
| 96 | let depth = 0; |
| 97 | let inString = false; |
| 98 | let escape = false; |
| 99 | for (let i = braceStart; i < s.length; i++) { |
| 100 | const ch = s[i]; |
| 101 | if (inString) { |
| 102 | if (escape) escape = false; |
| 103 | else if (ch === '\\') escape = true; |
| 104 | else if (ch === '"') inString = false; |
| 105 | } else if (ch === '"') { |
| 106 | inString = true; |
| 107 | } else if (ch === '{') { |
| 108 | depth++; |
| 109 | } else if (ch === '}') { |
| 110 | depth--; |
| 111 | if (depth === 0) return s.slice(braceStart, i + 1); |
| 112 | } |
| 113 | } |
| 114 | return null; // unbalanced braces |
| 115 | } |
| 116 | |
| 117 | // Parse the FIRST sessions_spawn(...) call into its object, or null if none / |
| 118 | // invalid. renderSessionsSpawnCall emits strict JSON.stringify output, so a |
no outgoing calls
no test coverage detected