synabun 2.4.5 → 2.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +1 -1
- package/docs/whatsapp.md +17 -1
- package/hooks/claude-code/session-start.mjs +21 -18
- package/lib/claude-global-config.js +214 -0
- package/lib/client-config-repair.js +17 -3
- package/mcp-server/dist/index.d.ts.map +1 -1
- package/mcp-server/dist/index.js +6 -1
- package/mcp-server/dist/index.js.map +1 -1
- package/mcp-server/dist/services/tool-catalog.js +1 -1
- package/mcp-server/dist/services/tool-catalog.js.map +1 -1
- package/mcp-server/dist/tools/agents.d.ts +2 -0
- package/mcp-server/dist/tools/agents.d.ts.map +1 -1
- package/mcp-server/dist/tools/agents.js +11 -5
- package/mcp-server/dist/tools/agents.js.map +1 -1
- package/mcp-server/dist/tools/skill-tools.d.ts +24 -5
- package/mcp-server/dist/tools/skill-tools.d.ts.map +1 -1
- package/mcp-server/dist/tools/skill-tools.js +88 -8
- package/mcp-server/dist/tools/skill-tools.js.map +1 -1
- package/mcp-server/dist/tools/tool-groups.d.ts.map +1 -1
- package/mcp-server/dist/tools/tool-groups.js +19 -4
- package/mcp-server/dist/tools/tool-groups.js.map +1 -1
- package/mcp-server/package-lock.json +2 -2
- package/mcp-server/package.json +1 -1
- package/mcp-server/src/index.ts +6 -1
- package/mcp-server/src/services/tool-catalog.ts +1 -1
- package/mcp-server/src/tools/agents.ts +11 -5
- package/mcp-server/src/tools/skill-tools.ts +82 -9
- package/mcp-server/src/tools/tool-groups.ts +19 -4
- package/neural-interface/i18n/en.json +135 -10
- package/neural-interface/i18n/pt-BR.json +135 -10
- package/neural-interface/lib/antigravity/panel-bridge.js +7 -3
- package/neural-interface/lib/assistant-dispatch.js +15 -1
- package/neural-interface/lib/assistant-runtime.js +79 -8
- package/neural-interface/lib/assistant-user-context.js +7 -3
- package/neural-interface/lib/claude-agent-bridge.js +13 -2
- package/neural-interface/lib/codex-thread-history.js +144 -7
- package/neural-interface/lib/codex-worker-app-server.js +84 -30
- package/neural-interface/lib/db.js +26 -0
- package/neural-interface/lib/greeting/sources.js +7 -3
- package/neural-interface/lib/jsonl-lines.js +10 -11
- package/neural-interface/lib/line-buffer.js +88 -0
- package/neural-interface/lib/loop-stall.js +15 -1
- package/neural-interface/lib/memory-list-read.js +105 -0
- package/neural-interface/lib/memory-timers.js +4 -1
- package/neural-interface/lib/native-loop-runtime.js +7 -3
- package/neural-interface/lib/provider-panel-api-key-auth.js +33 -6
- package/neural-interface/lib/skills/api-v2.js +18 -3
- package/neural-interface/lib/skills/bundle.js +2 -2
- package/neural-interface/lib/skills/deploy.js +169 -34
- package/neural-interface/lib/skills/digest.js +57 -3
- package/neural-interface/lib/skills/health.js +55 -1
- package/neural-interface/lib/skills/known-bridge-digests.json +12 -0
- package/neural-interface/lib/skills/runtime-checks.js +7 -4
- package/neural-interface/lib/skills/scan-shell.js +56 -7
- package/neural-interface/lib/skills/scan.js +13 -12
- package/neural-interface/lib/skills/service.js +7 -2
- package/neural-interface/lib/skills/telemetry.js +2 -2
- package/neural-interface/lib/teams/README.md +12 -5
- package/neural-interface/lib/teams/api.js +15 -4
- package/neural-interface/lib/teams/index.js +2 -0
- package/neural-interface/lib/teams/landing.js +195 -75
- package/neural-interface/lib/teams/scheduler.js +31 -32
- package/neural-interface/lib/terminal-token-redaction.js +228 -58
- package/neural-interface/lib/terminal-tokens.js +18 -0
- package/neural-interface/lib/whatsapp/brain.js +21 -0
- package/neural-interface/lib/whatsapp/bridge.js +40 -4
- package/neural-interface/package-lock.json +2 -2
- package/neural-interface/package.json +1 -1
- package/neural-interface/public/index.html +117 -37
- package/neural-interface/public/shared/agents/studio/ax-api.js +11 -0
- package/neural-interface/public/shared/agents/studio/ax-create-view.js +1 -1
- package/neural-interface/public/shared/agents/studio/ax-icons.js +1 -0
- package/neural-interface/public/shared/agents/studio/ax-library-view.js +3 -2
- package/neural-interface/public/shared/agents/studio/ax-main.js +127 -41
- package/neural-interface/public/shared/agents/studio/ax-review-view.js +3 -2
- package/neural-interface/public/shared/agents/studio/ax-teams-view.js +137 -39
- package/neural-interface/public/shared/agents/studio/ax-teams.js +219 -4
- package/neural-interface/public/shared/agents/studio/ax-ui.js +31 -0
- package/neural-interface/public/shared/agents/studio/ax-views.js +67 -69
- package/neural-interface/public/shared/agents/studio/ax-work-answer.js +86 -0
- package/neural-interface/public/shared/agents/studio/ax-work-view.js +305 -0
- package/neural-interface/public/shared/agents/studio/ax-work.css +74 -0
- package/neural-interface/public/shared/agents/studio/ax-work.js +300 -0
- package/neural-interface/public/shared/cdx/cdx-deferred.js +90 -0
- package/neural-interface/public/shared/cdx/cdx-render.js +18 -7
- package/neural-interface/public/shared/cdx/cdx-tabs.js +2 -1
- package/neural-interface/public/shared/loading-diagnostic-entry.js +16 -1
- package/neural-interface/public/shared/skills/studio/sx-operations-view.js +5 -0
- package/neural-interface/public/shared/skills/studio/sx-plan.js +42 -3
- package/neural-interface/public/shared/skills/studio/sx-publish-view.js +20 -4
- package/neural-interface/server.js +316 -252
- package/npm-shrinkwrap.json +2 -2
- package/package.json +2 -1
- package/skills/skill-forge/SKILL.md +7 -3
|
@@ -28,7 +28,6 @@ export const TOKEN_PLACEHOLDER = '[SynaBun token]';
|
|
|
28
28
|
const BODY_LENGTH = 43;
|
|
29
29
|
const TOKEN_LENGTH = 4 + BODY_LENGTH;
|
|
30
30
|
const SECRET_SHAPE_RE = /^sb[tb]_[A-Za-z0-9_-]{43}$/;
|
|
31
|
-
const MARKER_TEST_RE = /sb[tb]_/;
|
|
32
31
|
const BODY_AT_RE = /[A-Za-z0-9_-]{43}/y;
|
|
33
32
|
// The end of a text that could be the beginning of a token cut in two by a stream: `s`, `sb`, `sbt` / `sbb`, or the
|
|
34
33
|
// marker and up to 42 of its characters.
|
|
@@ -77,14 +76,151 @@ export function encodedSecretForms(secret) {
|
|
|
77
76
|
return [...forms].filter((form) => form.length >= 40);
|
|
78
77
|
}
|
|
79
78
|
|
|
79
|
+
// ── Finding the known forms in a text, whatever their number ──
|
|
80
|
+
//
|
|
81
|
+
// Before 2026-11-29 every scrub ran `text.indexOf(form)` for every known form: about 13 forms a secret, up to
|
|
82
|
+
// KNOWN_SECRETS_MAX secrets, on every string of 40 characters or more. Opening a Codex thread whose reply held 171 MB
|
|
83
|
+
// of inline images cost about 172 ms per known secret, on the server's own thread: 71 s with some 410 live tokens.
|
|
84
|
+
//
|
|
85
|
+
// A matcher finds every occurrence of every form in a time that depends on the text, not on how many forms there are.
|
|
86
|
+
// Every form is at least `minLength` characters, so any occurrence holds, whole, the GRAM characters that start at
|
|
87
|
+
// some multiple of `stride` (= minLength - GRAM + 1): the text is read at those positions only (about a tenth of its
|
|
88
|
+
// characters), each sample is looked up in a bit set of all the forms' GRAM-character pieces, and a hit is checked
|
|
89
|
+
// against the forms that hold that piece at that offset (`startsWith`, the only thing that decides). An occurrence
|
|
90
|
+
// long enough to hold two samples is found twice; the stretches are merged afterwards, as they always were.
|
|
91
|
+
const GRAM = 4;
|
|
92
|
+
/** Whether a character can be part of a form (the base64, base64url and hex alphabets). */
|
|
93
|
+
const FORM_CHAR = new Uint8Array(128);
|
|
94
|
+
for (const c of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/-_') FORM_CHAR[c.charCodeAt(0)] = 1;
|
|
95
|
+
const pow2AtLeast = (n) => { let p = 1; while (p < n) p *= 2; return p; };
|
|
96
|
+
// A sample's key is its four characters' low bytes; these two spread it over the bit set and the table.
|
|
97
|
+
const MIX_A = 0x9E3779B1;
|
|
98
|
+
const MIX_B = 0x85EBCA6B;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A matcher over `formList` (what encodedSecretForms makes: strings of GRAM characters or more in the base64,
|
|
102
|
+
* base64url and hex alphabets; partialStart relies on that alphabet), or null for none.
|
|
103
|
+
* scan(text, found) pushes [start, end] for every occurrence of every form (some of them more than once);
|
|
104
|
+
* partialStart(text) where the longest end of `text` that is the beginning of a form, but not a whole one, starts
|
|
105
|
+
* (`text.length` when no end is one).
|
|
106
|
+
*/
|
|
107
|
+
function createFormMatcher(formList) {
|
|
108
|
+
const forms = [...new Set(formList)].filter((form) => typeof form === 'string' && form.length >= GRAM).sort();
|
|
109
|
+
if (!forms.length) return null;
|
|
110
|
+
let minLength = Infinity;
|
|
111
|
+
let maxLength = 0;
|
|
112
|
+
let grams = 0;
|
|
113
|
+
for (const form of forms) {
|
|
114
|
+
if (form.length < minLength) minLength = form.length;
|
|
115
|
+
if (form.length > maxLength) maxLength = form.length;
|
|
116
|
+
grams += form.length - GRAM + 1;
|
|
117
|
+
}
|
|
118
|
+
const stride = minLength - GRAM + 1;
|
|
119
|
+
// The bit set: two bits a piece in 64 bits a piece (a sample of a text that holds no form passes both about once
|
|
120
|
+
// in a thousand), 2 MB at most.
|
|
121
|
+
const bitCount = Math.min(1 << 24, Math.max(1 << 10, pow2AtLeast(grams * 64)));
|
|
122
|
+
const bitShift = 32 - Math.log2(bitCount);
|
|
123
|
+
const bits = new Uint8Array(bitCount >>> 3);
|
|
124
|
+
// The pieces themselves: an open-addressing table of (key, form, offset in the form), half empty at least.
|
|
125
|
+
const slots = Math.max(16, pow2AtLeast(grams * 2));
|
|
126
|
+
const slotShift = 32 - Math.log2(slots);
|
|
127
|
+
const slotMask = slots - 1;
|
|
128
|
+
const slotForm = new Int32Array(slots).fill(-1);
|
|
129
|
+
const slotKey = new Int32Array(slots);
|
|
130
|
+
const slotOffset = new Int32Array(slots);
|
|
131
|
+
const firstChar = new Uint8Array(128);
|
|
132
|
+
for (let f = 0; f < forms.length; f++) {
|
|
133
|
+
const form = forms[f];
|
|
134
|
+
const first = form.charCodeAt(0);
|
|
135
|
+
if (first < 128) firstChar[first] = 1;
|
|
136
|
+
for (let offset = 0; offset + GRAM <= form.length; offset++) {
|
|
137
|
+
const key = (form.charCodeAt(offset) & 255) | ((form.charCodeAt(offset + 1) & 255) << 8)
|
|
138
|
+
| ((form.charCodeAt(offset + 2) & 255) << 16) | ((form.charCodeAt(offset + 3) & 255) << 24);
|
|
139
|
+
const bitA = Math.imul(key, MIX_A) >>> bitShift;
|
|
140
|
+
bits[bitA >>> 3] |= 1 << (bitA & 7);
|
|
141
|
+
const bitB = Math.imul(key, MIX_B) >>> bitShift;
|
|
142
|
+
bits[bitB >>> 3] |= 1 << (bitB & 7);
|
|
143
|
+
let slot = Math.imul(key, MIX_B) >>> slotShift;
|
|
144
|
+
while (slotForm[slot] !== -1) slot = (slot + 1) & slotMask;
|
|
145
|
+
slotForm[slot] = f;
|
|
146
|
+
slotKey[slot] = key;
|
|
147
|
+
slotOffset[slot] = offset;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function scan(text, found) {
|
|
152
|
+
const length = text.length;
|
|
153
|
+
if (length < minLength) return;
|
|
154
|
+
const last = length - GRAM;
|
|
155
|
+
for (let p = 0; p <= last; p += stride) {
|
|
156
|
+
const key = (text.charCodeAt(p) & 255) | ((text.charCodeAt(p + 1) & 255) << 8)
|
|
157
|
+
| ((text.charCodeAt(p + 2) & 255) << 16) | ((text.charCodeAt(p + 3) & 255) << 24);
|
|
158
|
+
const bitA = Math.imul(key, MIX_A) >>> bitShift;
|
|
159
|
+
if ((bits[bitA >>> 3] & (1 << (bitA & 7))) === 0) continue;
|
|
160
|
+
const mixed = Math.imul(key, MIX_B);
|
|
161
|
+
const bitB = mixed >>> bitShift;
|
|
162
|
+
if ((bits[bitB >>> 3] & (1 << (bitB & 7))) === 0) continue;
|
|
163
|
+
for (let slot = mixed >>> slotShift; slotForm[slot] !== -1; slot = (slot + 1) & slotMask) {
|
|
164
|
+
if (slotKey[slot] !== key) continue;
|
|
165
|
+
const start = p - slotOffset[slot];
|
|
166
|
+
if (start < 0) continue;
|
|
167
|
+
const form = forms[slotForm[slot]];
|
|
168
|
+
if (text.startsWith(form, start)) found.push([start, start + form.length]);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Whether `piece` is the beginning of a form that is longer than it. The forms are sorted: a binary search. */
|
|
174
|
+
function beginsForm(piece) {
|
|
175
|
+
let lo = 0;
|
|
176
|
+
let hi = forms.length;
|
|
177
|
+
while (lo < hi) {
|
|
178
|
+
const mid = (lo + hi) >>> 1;
|
|
179
|
+
if (forms[mid] < piece) lo = mid + 1; else hi = mid;
|
|
180
|
+
}
|
|
181
|
+
// Everything that starts with `piece` follows it directly; `piece` itself may be a form (then the next one tells).
|
|
182
|
+
if (lo < forms.length && forms[lo] === piece) lo++;
|
|
183
|
+
return lo < forms.length && forms[lo].startsWith(piece);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function partialStart(text) {
|
|
187
|
+
const length = text.length;
|
|
188
|
+
const from = Math.max(0, length - (maxLength - 1));
|
|
189
|
+
// Only an end made of the forms' own characters can begin one.
|
|
190
|
+
let run = length;
|
|
191
|
+
while (run > from) {
|
|
192
|
+
const code = text.charCodeAt(run - 1);
|
|
193
|
+
if (code >= 128 || FORM_CHAR[code] === 0) break;
|
|
194
|
+
run--;
|
|
195
|
+
}
|
|
196
|
+
for (let i = run; i < length; i++) {
|
|
197
|
+
if (firstChar[text.charCodeAt(i)] === 1 && beginsForm(text.slice(i))) return i;
|
|
198
|
+
}
|
|
199
|
+
return length;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return { scan, partialStart, maxLength };
|
|
203
|
+
}
|
|
204
|
+
|
|
80
205
|
// ── The process-wide set of known secrets (the token registry fills it) ──
|
|
81
206
|
const knownSecrets = new Map(); // secret → its encoded forms
|
|
82
|
-
let
|
|
207
|
+
let knownMatcher = null; // the matcher over every known secret's forms (null: there is none)
|
|
208
|
+
let knownMatcherStale = false;
|
|
83
209
|
|
|
210
|
+
/** The set changed: the matcher is made again at the next scrub (registering is rare, scrubbing is not). */
|
|
84
211
|
function rebuildKnown() {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
212
|
+
knownMatcherStale = true;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The matcher over the process-wide known forms, or null when there is none. */
|
|
216
|
+
function knownFormMatcher() {
|
|
217
|
+
if (knownMatcherStale) {
|
|
218
|
+
const all = [];
|
|
219
|
+
for (const forms of knownSecrets.values()) for (const form of forms) all.push(form);
|
|
220
|
+
knownMatcher = createFormMatcher(all);
|
|
221
|
+
knownMatcherStale = false;
|
|
222
|
+
}
|
|
223
|
+
return knownMatcher;
|
|
88
224
|
}
|
|
89
225
|
|
|
90
226
|
/** Scrub `secret`'s encoded forms too from now on (a live token or credential). Returns whether it was taken. */
|
|
@@ -105,16 +241,11 @@ export function forgetKnownSecret(secret) {
|
|
|
105
241
|
return true;
|
|
106
242
|
}
|
|
107
243
|
|
|
108
|
-
/**
|
|
244
|
+
/** How many secrets the process-wide set holds (tests, and the server's diagnostics: a number, never a value). */
|
|
109
245
|
export function knownSecretCount() {
|
|
110
246
|
return knownSecrets.size;
|
|
111
247
|
}
|
|
112
248
|
|
|
113
|
-
/** The forms of `extra` secrets plus the process-wide ones. */
|
|
114
|
-
function formsWith(extraForms) {
|
|
115
|
-
return extraForms.length ? [...new Set([...knownForms, ...extraForms])] : knownForms;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
249
|
/** Sorted, overlapping or touching stretches joined. */
|
|
119
250
|
function mergeIntervals(found) {
|
|
120
251
|
if (found.length < 2) return found;
|
|
@@ -130,21 +261,22 @@ function mergeIntervals(found) {
|
|
|
130
261
|
|
|
131
262
|
/**
|
|
132
263
|
* The stretches of `text` to mask, merged and in order: every token-shaped value (overlapping ones too: a token whose
|
|
133
|
-
* body holds another marker is masked through the end of both) and every occurrence of a known form
|
|
264
|
+
* body holds another marker is masked through the end of both) and every occurrence of a known form (`matcher`, and
|
|
265
|
+
* `extra` for the forms one stream adds; either may be null).
|
|
134
266
|
*/
|
|
135
|
-
function secretIntervals(text,
|
|
267
|
+
function secretIntervals(text, matcher, extra = null) {
|
|
136
268
|
const found = [];
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
for (const form of forms) {
|
|
146
|
-
for (let at = text.indexOf(form); at !== -1; at = text.indexOf(form, at + 1)) found.push([at, at + form.length]);
|
|
269
|
+
// Every `sbt_` / `sbb_` followed by a body, overlapping ones included. Found from the marker's last character: a
|
|
270
|
+
// long base64 text (an inline image) holds no `_` at all, and the search for one character is the fastest there is
|
|
271
|
+
// (a tenth of the time the expression /sb[tb]_/ took on such a text).
|
|
272
|
+
for (let at = text.indexOf('_', 3); at !== -1; at = text.indexOf('_', at + 1)) {
|
|
273
|
+
const kind = text.charCodeAt(at - 1);
|
|
274
|
+
if ((kind !== 116 && kind !== 98) || text.charCodeAt(at - 2) !== 98 || text.charCodeAt(at - 3) !== 115) continue;
|
|
275
|
+
BODY_AT_RE.lastIndex = at + 1;
|
|
276
|
+
if (BODY_AT_RE.test(text)) found.push([at - 3, at - 3 + TOKEN_LENGTH]);
|
|
147
277
|
}
|
|
278
|
+
if (matcher) matcher.scan(text, found);
|
|
279
|
+
if (extra) extra.scan(text, found);
|
|
148
280
|
return mergeIntervals(found);
|
|
149
281
|
}
|
|
150
282
|
|
|
@@ -170,13 +302,13 @@ function render(text, from, to, intervals, state = { lastMasked: false }) {
|
|
|
170
302
|
/** `text` with every token-shaped value and every known form replaced (the same string when there is none). */
|
|
171
303
|
export function scrubTokenText(text) {
|
|
172
304
|
if (typeof text !== 'string' || text.length < 40) return text;
|
|
173
|
-
const intervals = secretIntervals(text,
|
|
305
|
+
const intervals = secretIntervals(text, knownFormMatcher());
|
|
174
306
|
return intervals.length ? render(text, 0, text.length, intervals) : text;
|
|
175
307
|
}
|
|
176
308
|
|
|
177
309
|
/** Whether a string holds a token-shaped value or a known form. */
|
|
178
310
|
export function containsTokenText(text) {
|
|
179
|
-
return typeof text === 'string' && text.length >= 40 && secretIntervals(text,
|
|
311
|
+
return typeof text === 'string' && text.length >= 40 && secretIntervals(text, knownFormMatcher()).length > 0;
|
|
180
312
|
}
|
|
181
313
|
|
|
182
314
|
/**
|
|
@@ -190,14 +322,8 @@ export function partialSecretTail(text) {
|
|
|
190
322
|
let start = text.length;
|
|
191
323
|
const tail = PARTIAL_TAIL_RE.exec(text);
|
|
192
324
|
if (tail) start = Math.min(start, tail.index);
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
for (let i = from; i < start; i++) {
|
|
196
|
-
if (text.charCodeAt(i) !== form.charCodeAt(0) || !form.startsWith(text.slice(i))) continue;
|
|
197
|
-
start = i;
|
|
198
|
-
break;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
325
|
+
const matcher = knownFormMatcher();
|
|
326
|
+
if (matcher) start = Math.min(start, matcher.partialStart(text));
|
|
201
327
|
return text.length - start;
|
|
202
328
|
}
|
|
203
329
|
|
|
@@ -214,7 +340,7 @@ export function scrubPageText(text, { before = '', final = false } = {}) {
|
|
|
214
340
|
if (typeof text !== 'string' || !text) return '';
|
|
215
341
|
const context = typeof before === 'string' ? before.slice(-PAGE_CONTEXT_CHARS) : '';
|
|
216
342
|
const whole = context + text;
|
|
217
|
-
const found = secretIntervals(whole,
|
|
343
|
+
const found = secretIntervals(whole, knownFormMatcher());
|
|
218
344
|
if (final) {
|
|
219
345
|
const tail = PARTIAL_TAIL_RE.exec(whole);
|
|
220
346
|
if (tail && MARKED_PARTIAL_RE.test(tail[0])) found.push([tail.index, whole.length]);
|
|
@@ -243,7 +369,7 @@ export function scrubOutputWindow(text, { before = '', after = '', final = false
|
|
|
243
369
|
const start = context.length;
|
|
244
370
|
let end = start + page.length;
|
|
245
371
|
if (!page) return { text: '', consumed: 0, held: false };
|
|
246
|
-
const found = secretIntervals(whole,
|
|
372
|
+
const found = secretIntervals(whole, knownFormMatcher());
|
|
247
373
|
let held = false;
|
|
248
374
|
if (final) {
|
|
249
375
|
const tail = PARTIAL_TAIL_RE.exec(whole);
|
|
@@ -271,16 +397,56 @@ export function redactTerminalSecrets(value) {
|
|
|
271
397
|
return redactValue(value, new WeakSet(), 0);
|
|
272
398
|
}
|
|
273
399
|
|
|
400
|
+
/** A string this long is remembered, with the object or array that holds it, once it was scrubbed. */
|
|
401
|
+
export const BIG_STRING_CHARS = 64 * 1024;
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* What one kind of scrub already did to the long strings of the values it returned, so the next pass over the same
|
|
405
|
+
* value does not read megabytes again. A message is scrubbed where it is parsed and once more where it is sent (a
|
|
406
|
+
* Codex app-server's line, then the packet for the page): with an inline image in it, that second pass cost as much
|
|
407
|
+
* as the first. The memory is the holder (an object or an array) and the key the string is under: `seen(holder,
|
|
408
|
+
* key, text)` is true only while that very holder still holds that very text there, so a holder somebody changed
|
|
409
|
+
* afterwards is read again. Entries go with their holders (a WeakMap).
|
|
410
|
+
*/
|
|
411
|
+
export function createScrubMemo() {
|
|
412
|
+
const scrubbed = new WeakMap(); // holder → Map(key → the scrubbed text it holds there)
|
|
413
|
+
return {
|
|
414
|
+
seen(holder, key, text) {
|
|
415
|
+
const known = scrubbed.get(holder);
|
|
416
|
+
return known !== undefined && known.get(key) === text;
|
|
417
|
+
},
|
|
418
|
+
note(holder, key, text) {
|
|
419
|
+
let known = scrubbed.get(holder);
|
|
420
|
+
if (!known) scrubbed.set(holder, known = new Map());
|
|
421
|
+
known.set(key, text);
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const terminalScrubMemo = createScrubMemo();
|
|
427
|
+
|
|
428
|
+
/** `holder[key]` (a string) scrubbed; a long one only when this holder was not seen holding it scrubbed already. */
|
|
429
|
+
function scrubHeld(holder, key, text, long) {
|
|
430
|
+
if (text.length < BIG_STRING_CHARS) return scrubTokenText(text);
|
|
431
|
+
if (terminalScrubMemo.seen(holder, key, text)) return text;
|
|
432
|
+
const next = scrubTokenText(text);
|
|
433
|
+
long.push(key, next);
|
|
434
|
+
return next;
|
|
435
|
+
}
|
|
436
|
+
|
|
274
437
|
function redactValue(value, seen, depth) {
|
|
275
438
|
if (typeof value === 'string') return scrubTokenText(value);
|
|
276
439
|
if (!value || typeof value !== 'object' || depth > 64 || seen.has(value)) return value;
|
|
440
|
+
const long = []; // key, scrubbed text, … of the long strings read in this pass
|
|
277
441
|
if (Array.isArray(value)) {
|
|
278
442
|
seen.add(value);
|
|
279
443
|
let out = null;
|
|
280
444
|
for (let i = 0; i < value.length; i++) {
|
|
281
|
-
const
|
|
282
|
-
|
|
445
|
+
const item = value[i];
|
|
446
|
+
const next = typeof item === 'string' ? scrubHeld(value, i, item, long) : redactValue(item, seen, depth + 1);
|
|
447
|
+
if (next !== item) { if (!out) out = value.slice(); out[i] = next; }
|
|
283
448
|
}
|
|
449
|
+
for (let i = 0; i < long.length; i += 2) terminalScrubMemo.note(out || value, long[i], long[i + 1]);
|
|
284
450
|
return out || value;
|
|
285
451
|
}
|
|
286
452
|
const proto = Object.getPrototypeOf(value);
|
|
@@ -289,12 +455,13 @@ function redactValue(value, seen, depth) {
|
|
|
289
455
|
let out = null;
|
|
290
456
|
for (const key of Object.keys(value)) {
|
|
291
457
|
const item = value[key];
|
|
292
|
-
const next = redactValue(item, seen, depth + 1);
|
|
458
|
+
const next = typeof item === 'string' ? scrubHeld(value, key, item, long) : redactValue(item, seen, depth + 1);
|
|
293
459
|
if (next !== item) {
|
|
294
460
|
if (!out) out = { ...value };
|
|
295
461
|
out[key] = next;
|
|
296
462
|
}
|
|
297
463
|
}
|
|
464
|
+
for (let i = 0; i < long.length; i += 2) terminalScrubMemo.note(out || value, long[i], long[i + 1]);
|
|
298
465
|
return out || value;
|
|
299
466
|
}
|
|
300
467
|
|
|
@@ -313,13 +480,16 @@ function redactValue(value, seen, depth) {
|
|
|
313
480
|
export function createTokenStreamRedactor({ secrets = [] } = {}) {
|
|
314
481
|
const extraForms = [];
|
|
315
482
|
for (const secret of secrets || []) for (const form of encodedSecretForms(secret)) extraForms.push(form);
|
|
483
|
+
// This stream's own forms get a matcher of their own, made once; the process-wide one is asked at every push.
|
|
484
|
+
const extra = createFormMatcher(extraForms);
|
|
316
485
|
let raw = ''; // the last characters seen (passed on and held), unchanged
|
|
317
486
|
let held = 0; // how many of raw's last characters are held back
|
|
318
487
|
const state = { lastMasked: false };
|
|
319
488
|
|
|
320
|
-
|
|
489
|
+
/** As long as the longest thing a stretch can be: a token, or the longest form of either set. */
|
|
490
|
+
const windowFor = (known) => Math.max(TOKEN_LENGTH, known ? known.maxLength : 0, extra ? extra.maxLength : 0);
|
|
321
491
|
/** Where the longest end of `text` that could still become a token or a known form starts, and whether to mask it. */
|
|
322
|
-
function partial(text,
|
|
492
|
+
function partial(text, known) {
|
|
323
493
|
let start = text.length;
|
|
324
494
|
let maskFrom = text.length;
|
|
325
495
|
const tail = PARTIAL_TAIL_RE.exec(text);
|
|
@@ -327,48 +497,48 @@ export function createTokenStreamRedactor({ secrets = [] } = {}) {
|
|
|
327
497
|
start = Math.min(start, tail.index);
|
|
328
498
|
if (MARKED_PARTIAL_RE.test(tail[0])) maskFrom = Math.min(maskFrom, tail.index);
|
|
329
499
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
500
|
+
// The longest end that begins a form of either set: held, and masked at a flush when it is long enough (a
|
|
501
|
+
// shorter end that begins another form is inside it, so the longest one decides both).
|
|
502
|
+
let begins = text.length;
|
|
503
|
+
if (known) begins = Math.min(begins, known.partialStart(text));
|
|
504
|
+
if (extra) begins = Math.min(begins, extra.partialStart(text));
|
|
505
|
+
if (begins < text.length) {
|
|
506
|
+
start = Math.min(start, begins);
|
|
507
|
+
if (text.length - begins >= KNOWN_PARTIAL_MASK_MIN) maskFrom = Math.min(maskFrom, begins);
|
|
338
508
|
}
|
|
339
509
|
return { start, maskFrom };
|
|
340
510
|
}
|
|
341
|
-
function trim(
|
|
342
|
-
const keep = windowFor(
|
|
511
|
+
function trim(known) {
|
|
512
|
+
const keep = windowFor(known) + held;
|
|
343
513
|
if (raw.length > keep) raw = raw.slice(raw.length - keep);
|
|
344
514
|
}
|
|
345
515
|
|
|
346
516
|
return {
|
|
347
517
|
push(chunk) {
|
|
348
518
|
const text = String(chunk ?? '');
|
|
349
|
-
const
|
|
519
|
+
const known = knownFormMatcher();
|
|
350
520
|
const emittedEnd = raw.length - held;
|
|
351
521
|
raw += text;
|
|
352
|
-
const intervals = secretIntervals(raw,
|
|
353
|
-
const { start } = partial(raw,
|
|
522
|
+
const intervals = secretIntervals(raw, known, extra);
|
|
523
|
+
const { start } = partial(raw, known);
|
|
354
524
|
const holdFrom = Math.max(emittedEnd, start);
|
|
355
525
|
const out = render(raw, emittedEnd, holdFrom, intervals, state);
|
|
356
526
|
held = raw.length - holdFrom;
|
|
357
|
-
trim(
|
|
527
|
+
trim(known);
|
|
358
528
|
return out;
|
|
359
529
|
},
|
|
360
530
|
flush() {
|
|
361
531
|
if (!held) return '';
|
|
362
|
-
const
|
|
532
|
+
const known = knownFormMatcher();
|
|
363
533
|
const emittedEnd = raw.length - held;
|
|
364
|
-
const found = secretIntervals(raw,
|
|
365
|
-
const { maskFrom } = partial(raw,
|
|
534
|
+
const found = secretIntervals(raw, known, extra);
|
|
535
|
+
const { maskFrom } = partial(raw, known);
|
|
366
536
|
// From where the held beginning starts, which may be before what went out: render() clips it and adds no
|
|
367
537
|
// second placeholder when the part that went out was masked already.
|
|
368
538
|
if (maskFrom < raw.length) found.push([maskFrom, raw.length]);
|
|
369
539
|
const out = render(raw, emittedEnd, raw.length, mergeIntervals(found), state);
|
|
370
540
|
held = 0;
|
|
371
|
-
trim(
|
|
541
|
+
trim(known);
|
|
372
542
|
return out;
|
|
373
543
|
},
|
|
374
544
|
pending: () => held > 0,
|
|
@@ -430,6 +430,24 @@ export function createTerminalTokenRegistry({
|
|
|
430
430
|
},
|
|
431
431
|
/** For tests and diagnostics: how many runs hold a token, how many ended ones are kept. */
|
|
432
432
|
stats: () => ({ live: live.size, revoked: revoked.size }),
|
|
433
|
+
/**
|
|
434
|
+
* For diagnostics: what the live proofs are, by kind and by the family of their id (a Codex panel's app-server, a
|
|
435
|
+
* Claude panel tab, an automation-window agent, an MCP session, anything else: a run, a loop, a brain's session).
|
|
436
|
+
* Numbers only: never an id or a value. Each live proof is one secret the scrubbers look for.
|
|
437
|
+
*/
|
|
438
|
+
breakdown: () => {
|
|
439
|
+
const kinds = {};
|
|
440
|
+
const families = {};
|
|
441
|
+
for (const [id, held] of live) {
|
|
442
|
+
kinds[held.kind] = (kinds[held.kind] || 0) + 1;
|
|
443
|
+
const family = held.kind === 'mcp-session' ? 'mcp-session'
|
|
444
|
+
: id.startsWith('codex-sp-') ? 'codex-panel'
|
|
445
|
+
: id.startsWith('sidepanel-') ? 'claude-panel'
|
|
446
|
+
: id.startsWith('agent:') ? 'agent' : 'run';
|
|
447
|
+
families[family] = (families[family] || 0) + 1;
|
|
448
|
+
}
|
|
449
|
+
return { kinds, families };
|
|
450
|
+
},
|
|
433
451
|
/** For tests and diagnostics: how many closed MCP sessions are kept refused (their own list, never the runs'). */
|
|
434
452
|
sessionStats: () => ({ revokedSessions: revokedSessions.size }),
|
|
435
453
|
};
|
|
@@ -77,6 +77,9 @@ export function checkBrainChoice(choice, catalog) {
|
|
|
77
77
|
return { ok: true, brain: { provider: clean.provider, model: hit.row.id, effort } };
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
// Providers whose accounts SynaBun keeps (Settings → Accounts).
|
|
81
|
+
const ACCOUNT_PROVIDERS = new Set(['claude-code', 'codex']);
|
|
82
|
+
|
|
80
83
|
/**
|
|
81
84
|
* The brain a WhatsApp conversation runs on now.
|
|
82
85
|
* → { source: 'assistant' | 'choice', brain, fallback }
|
|
@@ -84,6 +87,11 @@ export function checkBrainChoice(choice, catalog) {
|
|
|
84
87
|
* fallback: { reason: 'disabled' | 'unknown', provider, model } when a stored choice
|
|
85
88
|
* could not be used. Only a definite answer falls back: a catalog that is not
|
|
86
89
|
* built yet, or a provider whose list is unavailable, keeps the choice.
|
|
90
|
+
*
|
|
91
|
+
* The account: the panel's (its pinned account, or its Auto) on the panel's provider; a model of
|
|
92
|
+
* another provider names none (its Default). Either way the runtime keeps a WhatsApp conversation
|
|
93
|
+
* on an account that can run (assistant-runtime.js phoneAccountMoves: Default and an exhausted or
|
|
94
|
+
* signed-out account follow usage; an API key and a named subscription with usage left stay).
|
|
87
95
|
*/
|
|
88
96
|
export function resolveBrainChoice(choice, { catalog = null, panelBrain = null } = {}) {
|
|
89
97
|
const base = isPlainObject(panelBrain) ? { ...panelBrain } : {};
|
|
@@ -102,6 +110,19 @@ export function resolveBrainChoice(choice, { catalog = null, panelBrain = null }
|
|
|
102
110
|
return { source: 'choice', brain: { ...kept, provider: clean.provider, model: clean.model, effort: clean.effort || null }, fallback: null };
|
|
103
111
|
}
|
|
104
112
|
|
|
113
|
+
/**
|
|
114
|
+
* The account fields a session switching to `target`'s provider is given (runtime.updateSession):
|
|
115
|
+
* Auto as `accountId: 'auto'` (the runtime resolves it then), the panel's pinned account as is, or
|
|
116
|
+
* nothing (the provider's Default). Only for a provider switch: another model of the same provider
|
|
117
|
+
* keeps the account, and with it the provider's conversation.
|
|
118
|
+
*/
|
|
119
|
+
export function brainAccountPatch(target) {
|
|
120
|
+
const brain = target?.brain || {};
|
|
121
|
+
if (!ACCOUNT_PROVIDERS.has(brain.provider)) return {};
|
|
122
|
+
if (brain.accountId === 'auto' || brain.accountPolicy === 'auto') return { accountId: 'auto' };
|
|
123
|
+
return brain.accountId ? { accountId: String(brain.accountId) } : {};
|
|
124
|
+
}
|
|
125
|
+
|
|
105
126
|
/** What identifies the configured choice: a session switches brain only when this changes. */
|
|
106
127
|
export function brainSignature(target) {
|
|
107
128
|
if (!target || target.source !== 'choice') return 'assistant';
|
|
@@ -60,7 +60,7 @@ import { assistantEventKey } from '../../public/shared/assistant/asst-state.js';
|
|
|
60
60
|
import { abandonedPrograms } from '../../public/shared/shell-stop-view.js';
|
|
61
61
|
import { stripFollowups } from '../../public/shared/assistant/asst-followups.js';
|
|
62
62
|
import { brainCapsLevel, defaultRemotePolicyRegistry, effectiveLevel, remoteBrainNotice } from '../remote-policy.js';
|
|
63
|
-
import { brainSignature, resolveBrainChoice } from './brain.js';
|
|
63
|
+
import { brainAccountPatch, brainSignature, resolveBrainChoice } from './brain.js';
|
|
64
64
|
import { answerCard, cardFromPacket, failClosedResponse, isBareAnswer, randomTag, renderCard } from './cards.js';
|
|
65
65
|
import { HELP_TEXT, SETTINGS_REFUSAL, parseCommand, statusText } from './commands.js';
|
|
66
66
|
|
|
@@ -448,6 +448,15 @@ export function createWhatsAppBridge({
|
|
|
448
448
|
function presence(value) { try { const r = transport.presence?.(value); if (r?.catch) r.catch(() => {}); } catch {} }
|
|
449
449
|
|
|
450
450
|
// ── sessions ───────────────────────────────────────────────────────────────
|
|
451
|
+
/**
|
|
452
|
+
* The panel's Auto that cannot pick now (no signed-in subscription, or every one limited) as a
|
|
453
|
+
* conversation starts or switches provider: the switch still happens, on the provider's Default
|
|
454
|
+
* account, and the runtime decides at the turn (assistant-runtime.js phoneAccountMoves): a key
|
|
455
|
+
* runs, a limited subscription is refused with the soonest reset, never sent the turn.
|
|
456
|
+
*/
|
|
457
|
+
function autoAccountFallsBack(asked, error) {
|
|
458
|
+
return asked?.accountId === 'auto' && ['ACCOUNT_NO_SUBSCRIPTION', 'ACCOUNTS_ALL_LIMITED'].includes(error?.code);
|
|
459
|
+
}
|
|
451
460
|
function defaultBrain() {
|
|
452
461
|
let brain = null;
|
|
453
462
|
try { brain = typeof getDefaultBrain === 'function' ? getDefaultBrain() : null; } catch { brain = null; }
|
|
@@ -483,7 +492,15 @@ export function createWhatsAppBridge({
|
|
|
483
492
|
if (differs && rt?.updateSession) {
|
|
484
493
|
try {
|
|
485
494
|
// "off" is the explicit reset to the model's default effort (a missing effort would keep the old one).
|
|
486
|
-
|
|
495
|
+
const patch = { provider: want.provider, ...(want.model ? { model: want.model } : {}), effort: effortOf(want) || 'off' };
|
|
496
|
+
// Another provider: its account too (brain.js brainAccountPatch), never the old provider's id.
|
|
497
|
+
const account = want.provider !== have.provider ? brainAccountPatch(target) : {};
|
|
498
|
+
try { await rt.updateSession(sessionId, { brain: { ...patch, ...account } }); }
|
|
499
|
+
catch (error) {
|
|
500
|
+
if (!autoAccountFallsBack(account, error)) throw error;
|
|
501
|
+
await rt.updateSession(sessionId, { brain: patch });
|
|
502
|
+
log('whatsapp:bridge', `session ${sessionId}: Auto could not pick a ${want.provider} account (${error.code}); switched on its Default account`);
|
|
503
|
+
}
|
|
487
504
|
log('whatsapp:bridge', `session ${sessionId}: brain set to ${want.provider}${want.model ? `/${want.model}` : ''} (${target.source === 'choice' ? 'the WhatsApp setting' : 'same as the Assistant'})`);
|
|
488
505
|
} catch (error) {
|
|
489
506
|
// Not applied (the model was disabled meanwhile, the runtime refused): the conversation stays on its brain; tried again at the next message.
|
|
@@ -508,10 +525,20 @@ export function createWhatsAppBridge({
|
|
|
508
525
|
const rt = runtime();
|
|
509
526
|
if (!rt?.createSession) throw Object.assign(new Error('The Assistant is not available right now.'), { code: 'RUNTIME_UNAVAILABLE' });
|
|
510
527
|
const target = brainTarget();
|
|
511
|
-
const
|
|
512
|
-
{ brain
|
|
528
|
+
const start = (brain) => rt.createSession(
|
|
529
|
+
{ brain, label: `WhatsApp · ${monthDay(now())}`, fallback: true, channel: 'whatsapp' },
|
|
513
530
|
{ remote: remoteFromConfig() },
|
|
514
531
|
);
|
|
532
|
+
let session;
|
|
533
|
+
try { session = await start(target.brain); }
|
|
534
|
+
catch (error) {
|
|
535
|
+
if (!autoAccountFallsBack(target.brain, error)) throw error;
|
|
536
|
+
const brain = { ...target.brain };
|
|
537
|
+
delete brain.accountId;
|
|
538
|
+
delete brain.accountPolicy;
|
|
539
|
+
session = await start(brain);
|
|
540
|
+
log('whatsapp:bridge', `Auto could not pick a ${brain.provider} account (${error.code}); started on its Default account`);
|
|
541
|
+
}
|
|
515
542
|
state.sessionId = session.id;
|
|
516
543
|
state.brainApplied = { sessionId: session.id, signature: brainSignature(target) };
|
|
517
544
|
state.policySeen.delete(session.id);
|
|
@@ -1498,9 +1525,18 @@ export function createWhatsAppBridge({
|
|
|
1498
1525
|
persistBridgeState();
|
|
1499
1526
|
react(job.sourceIds, 'failed');
|
|
1500
1527
|
if (code === 'SESSION_BUDGET_EXCEEDED' || code === 'BRAIN_SHARE_EXHAUSTED') { await say(budgetText(code, sessionId, result.message), { maxChunks: 1 }); return; }
|
|
1528
|
+
if (code === 'ACCOUNTS_ALL_LIMITED') { await say(limitedText(sessionId, result.resetsAt), { maxChunks: 1 }); return; }
|
|
1501
1529
|
if (code === 'EMPTY_PROMPT') return;
|
|
1502
1530
|
await say(`Could not start that: ${String(result.message || code).slice(0, 400)}`, { maxChunks: 1 });
|
|
1503
1531
|
}
|
|
1532
|
+
/** Every signed-in subscription of the brain's provider is limited: nothing was sent, and when the first is back (local time). */
|
|
1533
|
+
function limitedText(sessionId, resetsAt) {
|
|
1534
|
+
const name = sessionView(sessionId)?.brain?.provider === 'codex' ? 'Codex' : 'Claude';
|
|
1535
|
+
const at = Date.parse(resetsAt || '');
|
|
1536
|
+
const day = (ms) => new Date(ms).toDateString();
|
|
1537
|
+
const when = Number.isFinite(at) ? `The first one is back at ${hhmm(at)}${day(at) === day(now()) ? '' : ` on ${monthDay(at)}`}.` : 'When they reset is not known yet.';
|
|
1538
|
+
return `Not sent: every signed-in ${name} subscription has reached its usage limit. ${when} Send it again then, or choose another model for WhatsApp in SynaBun.`;
|
|
1539
|
+
}
|
|
1504
1540
|
/** Spend against the budget, and the two ways on: /new or a higher budget. Never a silent new session. */
|
|
1505
1541
|
function budgetText(code, sessionId, fallback) {
|
|
1506
1542
|
const view = sessionView(sessionId);
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "neural-memory-interface",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.6",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "neural-memory-interface",
|
|
9
|
-
"version": "2.4.
|
|
9
|
+
"version": "2.4.6",
|
|
10
10
|
"hasInstallScript": true,
|
|
11
11
|
"license": "Apache-2.0",
|
|
12
12
|
"dependencies": {
|