diff --git a/bridge/memory.js b/bridge/memory.js index 3ee4b18..567923a 100644 --- a/bridge/memory.js +++ b/bridge/memory.js @@ -83,6 +83,8 @@ function rank(a, b) { } function matchFilters(e, f) { + if (f.state && f.state !== "all") { if ((e.state || null) !== f.state) return false; } + else if (!f.state) { if (e.state === "archive") return false; } if (f.track && e.track !== f.track) return false; if (f.scope && e.scope !== f.scope) return false; if (f.importance && e.importance !== f.importance) return false; @@ -98,7 +100,7 @@ function opList(state, args) { let totalBytes = 0; for (const e of items) totalBytes += e.bytes || 0; return { - items: items.map(function (e) { return { id: e.id, track: e.track, scope: e.scope, key: e.key, importance: e.importance, tags: e.tags || [], bytes: e.bytes, updatedAt: e.updatedAt, recallCount: e.recallCount, preview: preview(e.text, pv) }; }), + items: items.map(function (e) { return { id: e.id, track: e.track, scope: e.scope, key: e.key, importance: e.importance, state: e.state || null, tags: e.tags || [], bytes: e.bytes, updatedAt: e.updatedAt, recallCount: e.recallCount, preview: preview(e.text, pv) }; }), totalBytes: totalBytes, matched: items.length, previewChars: pv }; } @@ -139,7 +141,7 @@ function opSearch(state, args) { }).sort(rank).slice(0, limit); let totalBytes = 0; for (const e of hits) totalBytes += e.bytes || 0; - return { items: hits.map(function (e) { return { id: e.id, key: e.key, track: e.track, importance: e.importance, tags: e.tags || [], bytes: e.bytes, preview: preview(e.text, pv) }; }), totalBytes: totalBytes, matched: hits.length }; + return { items: hits.map(function (e) { return { id: e.id, key: e.key, track: e.track, importance: e.importance, state: e.state || null, tags: e.tags || [], bytes: e.bytes, preview: preview(e.text, pv) }; }), totalBytes: totalBytes, matched: hits.length }; } function opBudgets(state) { @@ -205,7 +207,14 @@ function opWrite(state, args) { } e.importance = args.importance; } - if (args.state === 'active' || args.state === 'done' || args.state === 'archive') e.state = args.state; + if (args.state === 'active' || args.state === 'done' || args.state === 'archive') { + // replace checks directive limit + if (args.state === 'active' && e.state !== 'active' && String(e.key).indexOf('directive_') === 0) { + const dirCount = state.entries.filter(function (x) { return String(x.key).indexOf('directive_') === 0 && x.state === 'active' && (x.chatId || '') === (e.chatId || '') && x.id !== e.id; }).length; + if (dirCount >= DIRECTIVES_PER_CHAT) throw new Error('E_TOO_MANY_DIRECTIVES: limit ' + DIRECTIVES_PER_CHAT + ' per chat'); + } + e.state = args.state; + } if (typeof args.chatId === 'string') e.chatId = args.chatId; e.version = (e.version || 1) + 1; e.updatedAt = nowIso(); diff --git a/content/content.js b/content/content.js index ebe7287..5a3b74d 100644 --- a/content/content.js +++ b/content/content.js @@ -66,7 +66,18 @@ var parsed = extractCalls(d.text, 10); try { var __callSum = []; if (parsed ? (parsed.calls ? true : false) : false) { for (var __ci = 0; __ci < parsed.calls.length; __ci++) { var __c = parsed.calls[__ci]; __callSum.push({ i: __ci, tool: (__c ? String(__c.tool) : 'null'), hasArgs: (__c ? (__c.args ? true : false) : false), argsPreview: (__c ? String(JSON.stringify(__c.args || {})).slice(0, 400) : '') }) } } __debugReport({ ev: 'sse-parse', ts: Date.now(), pid: pid, execSource: execMode, consumed: (parsed ? parsed.consumed : -1), textLen: d.text.length, calls: __callSum, parsedCount: (parsed ? parsed.calls.length : 0), text: String(d.text).slice(0, 2000) }) } catch (e7) { } if (parsed ? (parsed.calls ? (parsed.calls.length > 0) : false) : false) { - for (var k = 0; k < parsed.calls.length; k++) queue.push(parsed.calls[k]); + var validCount = 0; + for (var k = 0; k < parsed.calls.length; k++) { + var c = parsed.calls[k]; + if (c && c.tool === 'invalid') { + try { __debugReport({ ev: 'sse-exec-result', ts: Date.now(), pid: pid, execSource: execMode, tool: 'invalid', ok: false, errorCode: 'E_PARSE_INVALID', errorMsg: String((c.args && c.args._reason) || '') }) } catch (e) { } + __uiLog('sse invalid call: ' + String((c.args && c.args._reason) || '')); + continue; + } + queue.push(c); + validCount++; + } + if (validCount === 0) { if (UI) UI.setPhase('idle'); return; } if (UI) { if (UI.setDot) UI.setDot('ok-busy'); UI.setPhase('executing'); } __uiLog('sse exec ' + parsed.calls.length + ' pid ' + pid); pump(); @@ -238,7 +249,7 @@ if (parsed.calls.length === 0) { if (hasMarker) { try { var __dh = hashText(text rememberCallsHash(ch); processed.add(m); rememberHash(h); - for (k = 0; k < parsed.calls.length; k++) queue.push(parsed.calls[k]); if (UI && UI.setDot) UI.setDot('ok-busy'); + for (k = 0; k < parsed.calls.length; k++) { var __dc = parsed.calls[k]; if (__dc && __dc.tool === 'invalid') { try { __debugReport({ ev: 'sse-exec-result', ts: Date.now(), pid: lastSsePid, execSource: execMode, tool: 'invalid', ok: false, errorCode: 'E_PARSE_INVALID', errorMsg: String((__dc.args && __dc.args._reason) || '') }) } catch (e) { } continue; } queue.push(__dc); } if (UI && UI.setDot) UI.setDot('ok-busy'); if (UI) UI.setPhase('executing'); } pump(); diff --git a/content/parser.js b/content/parser.js index d279597..52a8de9 100644 --- a/content/parser.js +++ b/content/parser.js @@ -42,6 +42,10 @@ function parseFileHeader(header) { return header.slice(q1 + 1, q2); } +function makeInvalid(reason, rawSnippet, closeIdx, closeLen) { + return { call: { tool: 'invalid', args: { _reason: reason, _raw: String(rawSnippet || '').slice(0, 200) } }, nextIdx: (typeof closeIdx === 'number' && closeIdx >= 0) ? closeIdx + closeLen : -1 }; +} + function parseOne(text, startIdx) { if (text.startsWith(OPEN_FILE, startIdx)) { const openEnd = text.indexOf(R, startIdx + OPEN_FILE.length); @@ -59,9 +63,10 @@ function parseOne(text, startIdx) { const closeIdx = text.indexOf(CLOSE_B64, startIdx + OPEN_B64.length); if (closeIdx < 0) return null; const raw = text.slice(startIdx + OPEN_B64.length, closeIdx); - let json = null; try { json = b64DecodeUtf8(raw); } catch (e) { return null; } + let json = null; try { json = b64DecodeUtf8(raw); } catch (e) { return makeInvalid('b64_decode_failed', raw, closeIdx, CLOSE_B64.length); } const obj = tryParseJson(json); - if (!obj || typeof obj !== 'object' || !obj.tool) return null; + if (!obj || typeof obj !== 'object') return makeInvalid('b64_json_invalid', json, closeIdx, CLOSE_B64.length); + if (!obj.tool) return makeInvalid('b64_no_tool_field', json, closeIdx, CLOSE_B64.length); return { call: obj, nextIdx: closeIdx + CLOSE_B64.length }; } if (text.startsWith(OPEN_DSH, startIdx)) { @@ -69,7 +74,8 @@ function parseOne(text, startIdx) { if (closeIdx < 0) return null; const raw = text.slice(startIdx + OPEN_DSH.length, closeIdx); const obj = tryParseJson(raw); - if (!obj || typeof obj !== 'object' || !obj.tool) return null; + if (!obj || typeof obj !== 'object') return makeInvalid('json_parse_failed', raw, closeIdx, CLOSE_DSH.length); + if (!obj.tool) return makeInvalid('no_tool_field', raw, closeIdx, CLOSE_DSH.length); return { call: obj, nextIdx: closeIdx + CLOSE_DSH.length }; } return null;