synabun 2.2.1 → 2.2.2
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 +27 -0
- package/README.md +1 -1
- package/hooks/claude-code/pre-websearch.mjs +4 -1
- package/hooks/claude-code/prompt-submit.mjs +7 -0
- package/lib/release-scan.mjs +23 -10
- package/lib/virustotal.mjs +143 -39
- package/mcp-server/dist/http.d.ts +19 -0
- package/mcp-server/dist/http.d.ts.map +1 -1
- package/mcp-server/dist/http.js +55 -13
- package/mcp-server/dist/http.js.map +1 -1
- package/mcp-server/dist/index.d.ts.map +1 -1
- package/mcp-server/dist/index.js +14 -9
- package/mcp-server/dist/index.js.map +1 -1
- package/mcp-server/dist/services/browser-route-flow.d.ts +118 -0
- package/mcp-server/dist/services/browser-route-flow.d.ts.map +1 -0
- package/mcp-server/dist/services/browser-route-flow.js +339 -0
- package/mcp-server/dist/services/browser-route-flow.js.map +1 -0
- package/mcp-server/dist/services/desktop-client.js +2 -2
- package/mcp-server/dist/services/desktop-client.js.map +1 -1
- package/mcp-server/dist/services/identity.d.ts +66 -3
- package/mcp-server/dist/services/identity.d.ts.map +1 -1
- package/mcp-server/dist/services/identity.js +61 -4
- package/mcp-server/dist/services/identity.js.map +1 -1
- package/mcp-server/dist/services/neural-interface.d.ts +112 -1
- package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
- package/mcp-server/dist/services/neural-interface.js +555 -61
- package/mcp-server/dist/services/neural-interface.js.map +1 -1
- package/mcp-server/dist/tools/bluesky.d.ts +20 -7
- package/mcp-server/dist/tools/bluesky.d.ts.map +1 -1
- package/mcp-server/dist/tools/bluesky.js +147 -45
- package/mcp-server/dist/tools/bluesky.js.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.d.ts +11 -2
- package/mcp-server/dist/tools/browser-advanced.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-advanced.js +131 -18
- package/mcp-server/dist/tools/browser-advanced.js.map +1 -1
- package/mcp-server/dist/tools/browser-cheatsheet.d.ts +2 -0
- package/mcp-server/dist/tools/browser-cheatsheet.d.ts.map +1 -1
- package/mcp-server/dist/tools/browser-cheatsheet.js +3 -0
- package/mcp-server/dist/tools/browser-cheatsheet.js.map +1 -1
- package/mcp-server/dist/tools/morelogin.d.ts.map +1 -1
- package/mcp-server/dist/tools/morelogin.js +4 -2
- package/mcp-server/dist/tools/morelogin.js.map +1 -1
- package/mcp-server/dist/tools/skill-tools.d.ts +4 -4
- package/mcp-server/dist/tools/youtube/helpers.d.ts +1 -0
- package/mcp-server/dist/tools/youtube/helpers.d.ts.map +1 -1
- package/mcp-server/dist/tools/youtube/helpers.js +1 -1
- package/mcp-server/dist/tools/youtube/helpers.js.map +1 -1
- package/mcp-server/package-lock.json +2 -2
- package/mcp-server/package.json +1 -1
- package/mcp-server/src/http.ts +58 -12
- package/mcp-server/src/index.ts +15 -9
- package/mcp-server/src/services/browser-route-flow.ts +402 -0
- package/mcp-server/src/services/desktop-client.ts +2 -2
- package/mcp-server/src/services/identity.ts +80 -4
- package/mcp-server/src/services/neural-interface.ts +572 -70
- package/mcp-server/src/tools/bluesky.ts +164 -51
- package/mcp-server/src/tools/browser-advanced.ts +121 -17
- package/mcp-server/src/tools/browser-cheatsheet.ts +3 -0
- package/mcp-server/src/tools/morelogin.ts +4 -2
- package/mcp-server/src/tools/youtube/helpers.ts +2 -2
- package/neural-interface/i18n/en.json +24 -0
- package/neural-interface/i18n/pt-BR.json +24 -0
- package/neural-interface/lib/assistant-api.js +64 -23
- package/neural-interface/lib/assistant-catalog.js +20 -2
- package/neural-interface/lib/assistant-dispatch.js +27 -1
- package/neural-interface/lib/assistant-persona.js +1 -0
- package/neural-interface/lib/assistant-runtime.js +15 -1
- package/neural-interface/lib/assistant-tools.js +10 -1
- package/neural-interface/lib/browser-automation-routes.js +84 -0
- package/neural-interface/lib/browser-execution.js +16 -3
- package/neural-interface/lib/browser-owner.js +48 -0
- package/neural-interface/lib/browser-session-close.js +84 -0
- package/neural-interface/lib/browser-start-handoff.js +202 -0
- package/neural-interface/lib/browser-tool-policy.js +21 -4
- package/neural-interface/lib/browser-wedge-retire.js +64 -0
- package/neural-interface/lib/claude-agent-bridge.js +36 -1
- package/neural-interface/lib/claude-background-hold.js +239 -0
- package/neural-interface/lib/exec-loop-command.js +8 -0
- package/neural-interface/lib/http-guards.js +11 -6
- package/neural-interface/lib/loop-logger.js +6 -1
- package/neural-interface/lib/mcp-external-servers.js +37 -10
- package/neural-interface/lib/native-loop-providers.js +121 -10
- package/neural-interface/lib/native-loop-runtime.js +112 -11
- package/neural-interface/lib/owner-authority.js +113 -37
- package/neural-interface/lib/page-session.js +81 -23
- package/neural-interface/lib/peer-identity.js +263 -43
- package/neural-interface/lib/provider-panel-api-key-auth.js +7 -2
- package/neural-interface/lib/pty-host/core.js +36 -0
- package/neural-interface/lib/pty-host/protocol.js +2 -0
- package/neural-interface/lib/skills/api-v2.js +13 -2
- package/neural-interface/lib/skills/creation-api.js +3 -1
- package/neural-interface/lib/skills/evals-api.js +5 -1
- package/neural-interface/lib/social-write-pacing.js +151 -0
- package/neural-interface/lib/terminal-token-redaction.js +293 -0
- package/neural-interface/lib/terminal-tokens.js +359 -0
- package/neural-interface/lib/worker-mcp-channel.js +45 -10
- package/neural-interface/package-lock.json +2 -2
- package/neural-interface/package.json +1 -1
- package/neural-interface/public/shared/agy/agy-render.js +41 -1
- package/neural-interface/public/shared/agy/agy-styles.js +5 -0
- package/neural-interface/public/shared/api.js +1 -0
- package/neural-interface/public/shared/assistant/asst-computer.js +47 -2
- package/neural-interface/public/shared/assistant/asst-limit-wait.js +3 -1
- package/neural-interface/public/shared/assistant/asst-panel.js +7 -3
- package/neural-interface/public/shared/assistant/asst-render.js +10 -4
- package/neural-interface/public/shared/assistant/asst-styles.js +2 -1
- package/neural-interface/public/shared/cdx/cdx-render.js +73 -7
- package/neural-interface/public/shared/cdx/cdx-tabs.js +3 -3
- package/neural-interface/public/shared/cp/cp-agents.js +8 -0
- package/neural-interface/public/shared/cp/cp-permissions.js +6 -2
- package/neural-interface/public/shared/cp/cp-rehydrate.js +25 -1
- package/neural-interface/public/shared/cp/cp-tasks-model.js +33 -0
- package/neural-interface/public/shared/judgments-log-origins.js +29 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-automation-ownership.js +23 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-events.js +13 -0
- package/neural-interface/public/shared/ocp-v2/ocp-v2-icons.js +3 -1
- package/neural-interface/public/shared/ocp-v2/ocp-v2-panel.js +4 -2
- package/neural-interface/public/shared/ocp-v2/ocp-v2-render.js +6 -86
- package/neural-interface/public/shared/ocp-v2/ocp-v2-ws.js +5 -2
- package/neural-interface/public/shared/owner-access.js +134 -0
- package/neural-interface/public/shared/owner-authority-view.js +92 -0
- package/neural-interface/public/shared/page-session.js +143 -14
- package/neural-interface/public/shared/poll-backoff.js +59 -0
- package/neural-interface/public/shared/settings/settings-data.js +6 -1
- package/neural-interface/public/shared/settings/settings-kit.js +11 -3
- package/neural-interface/public/shared/settings/settings-shell.js +7 -0
- package/neural-interface/public/shared/shown-children.js +72 -0
- package/neural-interface/public/shared/static-bg-state.js +22 -0
- package/neural-interface/public/shared/styles.css +101 -7
- package/neural-interface/public/shared/synabun-tools.js +173 -0
- package/neural-interface/public/shared/tool-heartbeat.js +43 -0
- package/neural-interface/public/shared/tool-list-picker.js +3 -1
- package/neural-interface/public/shared/tool-list-view.js +23 -4
- package/neural-interface/public/shared/ui-automation-studio.js +8 -1
- package/neural-interface/public/shared/ui-claude-panel.js +159 -29
- package/neural-interface/public/shared/ui-judgments.js +37 -10
- package/neural-interface/public/shared/ui-navbar.js +6 -0
- package/neural-interface/public/shared/ui-owner-access.js +191 -0
- package/neural-interface/public/shared/ui-owner-authority.js +162 -0
- package/neural-interface/public/shared/ui-sessions.js +45 -16
- package/neural-interface/public/shared/ui-settings.js +86 -15
- package/neural-interface/public/shared/ui-terminal.js +15 -7
- package/neural-interface/public/shared/ui-tutorial-preview.js +4 -0
- package/neural-interface/public/shared/ui-update.js +3 -0
- package/neural-interface/public/shared/ui-whiteboard.js +4 -0
- package/neural-interface/public/shared/window-keys.js +38 -0
- package/neural-interface/server.js +1160 -205
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -44,7 +44,7 @@ function fail(res, err) {
|
|
|
44
44
|
* @param {object} o.evals lib/skills/evals.js
|
|
45
45
|
* @param {(req) => boolean} [o.isGuestRequest]
|
|
46
46
|
* @param {Function} [o.ownerGuard] lib/http-guards.js ownerRouteGuard(…) of this server
|
|
47
|
-
* @param {(req) => ({ kind: 'assistant', assistantSessionId } | { kind: 'worker' } | null)} [o.assistantCaller]
|
|
47
|
+
* @param {(req) => ({ kind: 'assistant', assistantSessionId } | { kind: 'worker' } | { kind: 'refused', code, error, status } | null)} [o.assistantCaller]
|
|
48
48
|
* @param {{ verify(req), require(req, res, next), attach(res) }} [o.pageSession] lib/page-session.js
|
|
49
49
|
*/
|
|
50
50
|
export function registerSkillEvalRoutes(app, { evals, isGuestRequest = null, ownerGuard = null, assistantCaller = null, pageSession = null } = {}) {
|
|
@@ -74,12 +74,16 @@ export function registerSkillEvalRoutes(app, { evals, isGuestRequest = null, own
|
|
|
74
74
|
// owner-authority lookup refuses, it is refused): what a local process that merely left the agent headers out lacks.
|
|
75
75
|
if (pageSession && !pageSession.verify(req)) {
|
|
76
76
|
if (write) return pageSession.require(req, res, next);
|
|
77
|
+
// (a caller that could not be identified on macOS: 503 PEER_UNAVAILABLE, as require() answers a write)
|
|
78
|
+
if (typeof pageSession.peerUnavailable === 'function' && pageSession.peerUnavailable(req)) return pageSession.sendUnavailable(res, req);
|
|
77
79
|
return sendGuardError(res, 403, 'PAGE_SECRET_REQUIRED', 'Evaluations are read from SynaBun\'s own page. Reload the page and try again.');
|
|
78
80
|
}
|
|
79
81
|
return ownerGuard ? ownerGuard(req, res, next) : next();
|
|
80
82
|
}
|
|
81
83
|
let who = null;
|
|
82
84
|
try { who = typeof assistantCaller === 'function' ? assistantCaller(req) : null; } catch { who = null; }
|
|
85
|
+
// A request that names a run or a brain it cannot prove it is (server.js browserCaller: no terminal token, another's).
|
|
86
|
+
if (who?.kind === 'refused') return sendGuardError(res, Number(who.status) || 403, typeof who.code === 'string' ? who.code : 'TERMINAL_TOKEN_INVALID', typeof who.error === 'string' ? who.error : 'This request names a run it cannot prove it is.');
|
|
83
87
|
if (who?.kind === 'worker') return sendGuardError(res, 403, 'WORKERS_NOT_PERMITTED', 'Dispatched workers cannot run skill evaluations: the Assistant or the owner does.');
|
|
84
88
|
if (who?.kind !== 'assistant' || typeof who.assistantSessionId !== 'string' || !who.assistantSessionId) {
|
|
85
89
|
return sendGuardError(res, 403, 'ASSISTANT_CREDENTIAL_REQUIRED', 'Skill evaluations are the owner\'s (Skills Studio) and the SynaBun Assistant\'s own brain\'s (its launch credential): a role header is not enough.');
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// ── Spacing the social writes a scheduled automation makes (DOM-free) ──
|
|
2
|
+
//
|
|
3
|
+
// Automations were told by their task to "space every write 60 to 180 seconds apart", and did it themselves:
|
|
4
|
+
// Claude Code refuses a foreground `sleep 90`, so the agent started a background sleep or a Monitor and ended its
|
|
5
|
+
// turn to wait for it, and the run stopped there (lib/claude-background-hold.js is the other half of that fix).
|
|
6
|
+
// The wait belongs to SynaBun: a loop's Bluesky write (post, reply, like, repost, follow and their undos, a DM)
|
|
7
|
+
// waits out the gap inside the tool call that makes it.
|
|
8
|
+
//
|
|
9
|
+
// One queue per platform and signed-in account (socialPaceAccountKey: the browser profile, never the session id, so
|
|
10
|
+
// a session that is closed and opened again keeps its gap): concurrent runs on the same account take turns, and
|
|
11
|
+
// the gap is measured from the last write that went out. A write whose tab is gone before it starts takes no slot. A tool call has a time limit (Codex and
|
|
12
|
+
// OpenCode stop an MCP call at about 60 s), so a call waits at most `maxWaitMs`: when more of the gap is left, it
|
|
13
|
+
// writes nothing and answers how much is left (`paced`), and the next call waits the rest. The agent never sleeps.
|
|
14
|
+
//
|
|
15
|
+
// Only loop-owned requests are paced (server.js isLoopPaced): an interactive session writes at once.
|
|
16
|
+
|
|
17
|
+
/** The gap between two writes of one account, by platform: randomized per write, so the rhythm is not robotic. */
|
|
18
|
+
export const SOCIAL_WRITE_GAPS = Object.freeze({
|
|
19
|
+
bluesky: Object.freeze({ minMs: 60_000, maxMs: 120_000 }),
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
/** What a call may wait at most, and what it keeps for the write itself after the wait. */
|
|
23
|
+
export const SOCIAL_PACE_LIMITS = Object.freeze({ maxWaitMs: 90_000, writeReserveMs: 15_000 });
|
|
24
|
+
|
|
25
|
+
/** The pace request of a browser call body ({ platform, maxWaitMs }), or null when it names nothing paced. */
|
|
26
|
+
export function socialPaceRequest(body) {
|
|
27
|
+
const pace = body?.pace;
|
|
28
|
+
if (!pace || typeof pace !== 'object') return null;
|
|
29
|
+
const platform = String(pace.platform || '');
|
|
30
|
+
if (!Object.prototype.hasOwnProperty.call(SOCIAL_WRITE_GAPS, platform)) return null;
|
|
31
|
+
const wait = Number(pace.maxWaitMs);
|
|
32
|
+
const maxWaitMs = Number.isFinite(wait) ? Math.max(0, Math.min(SOCIAL_PACE_LIMITS.maxWaitMs, Math.floor(wait))) : 0;
|
|
33
|
+
return { platform, maxWaitMs };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The queue a write waits in: one per platform and signed-in account, which is the browser profile the session runs
|
|
38
|
+
* (MoreLogin env, Chrome profile folder, or the saved storage of a clean browser), never the session id. A session
|
|
39
|
+
* that is closed and opened again (another automation's close, a retired wedged session, a run's recovery) is the
|
|
40
|
+
* same account, and its next write still waits out the gap of the last one. Only a session that names no profile
|
|
41
|
+
* at all falls back to its own id.
|
|
42
|
+
*/
|
|
43
|
+
export function socialPaceAccountKey(platform, profile = {}, sessionId = '') {
|
|
44
|
+
const p = profile || {};
|
|
45
|
+
if (p.profileMode === 'morelogin' && p.moreloginEnvId) return `${platform}:morelogin:${p.moreloginEnvId}`;
|
|
46
|
+
if (p.profileDir) return `${platform}:profile:${p.profileDir}`;
|
|
47
|
+
if (p.storageStatePath) return `${platform}:storage:${p.storageStatePath}`;
|
|
48
|
+
return `${platform}:session:${sessionId}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether an executed write script reached the account. A Bluesky script that stopped on an expired token before
|
|
53
|
+
* any write is the one case its tool retries at once (mcp-server/src/tools/bluesky.ts runPrepared): that attempt
|
|
54
|
+
* takes no slot. Anything else, failures included, counts: a write that may have landed must be spaced too.
|
|
55
|
+
*/
|
|
56
|
+
export function socialAttemptWrote(platform, result) {
|
|
57
|
+
if (platform === 'bluesky' && result && typeof result === 'object' && result.__bskyExpired === true && result.__bskyWrote !== true) return false;
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const defaultDelay = (ms) => new Promise((resolve) => { const timer = setTimeout(resolve, ms); timer.unref?.(); });
|
|
62
|
+
|
|
63
|
+
export function createSocialWritePacer({ now = Date.now, random = Math.random, delay = defaultDelay, gaps = SOCIAL_WRITE_GAPS } = {}) {
|
|
64
|
+
const states = new Map(); // key → { tail, nextAt, gapMs }
|
|
65
|
+
const stateOf = (key) => {
|
|
66
|
+
if (!states.has(key)) states.set(key, { tail: Promise.resolve(), nextAt: 0, gapMs: 0 });
|
|
67
|
+
return states.get(key);
|
|
68
|
+
};
|
|
69
|
+
const gapFor = (platform) => {
|
|
70
|
+
const gap = gaps[platform] || gaps.bluesky;
|
|
71
|
+
return gap.minMs + Math.floor(random() * (gap.maxMs - gap.minMs + 1));
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
/** How long the next write of `key` would wait now (ms). */
|
|
75
|
+
remaining(key) {
|
|
76
|
+
const state = states.get(key);
|
|
77
|
+
return state ? Math.max(0, state.nextAt - now()) : 0;
|
|
78
|
+
},
|
|
79
|
+
clear(key) { states.delete(key); },
|
|
80
|
+
/**
|
|
81
|
+
* Run one write of `key` when its gap allows it. `delay(ms, signal)` waits (the host's is cancellable: a call
|
|
82
|
+
* that is cut off never writes). Resolves { status: 'written', result, waitedMs } or, when more than
|
|
83
|
+
* `maxWaitMs` of the gap is left, { status: 'paced', retryAfterMs, waitedMs } without writing.
|
|
84
|
+
*/
|
|
85
|
+
async run(key, { platform = 'bluesky', maxWaitMs = 0, signal = null } = {}, execute, { wrote = (result) => socialAttemptWrote(platform, result) } = {}) {
|
|
86
|
+
const state = stateOf(key);
|
|
87
|
+
const predecessor = state.tail;
|
|
88
|
+
let release;
|
|
89
|
+
state.tail = new Promise((resolve) => { release = resolve; });
|
|
90
|
+
const startedAt = now();
|
|
91
|
+
try {
|
|
92
|
+
await predecessor;
|
|
93
|
+
const left = Math.max(0, state.nextAt - now());
|
|
94
|
+
const budget = Math.max(0, Number(maxWaitMs) || 0);
|
|
95
|
+
if (left > budget) {
|
|
96
|
+
if (budget > 0) await delay(budget, signal);
|
|
97
|
+
return { status: 'paced', retryAfterMs: Math.max(1, state.nextAt - now()), waitedMs: now() - startedAt, gapMs: state.gapMs };
|
|
98
|
+
}
|
|
99
|
+
if (left > 0) await delay(left, signal);
|
|
100
|
+
const waitedMs = now() - startedAt;
|
|
101
|
+
let counted = true;
|
|
102
|
+
try {
|
|
103
|
+
const result = await execute();
|
|
104
|
+
counted = wrote(result) !== false;
|
|
105
|
+
return { status: 'written', result, waitedMs, gapMs: state.gapMs };
|
|
106
|
+
} catch (error) {
|
|
107
|
+
// The write never started (its tab was closed while it waited for the gap): it takes no slot, and the
|
|
108
|
+
// retry on the run's new tab goes out as soon as the gap allows, not a whole gap later.
|
|
109
|
+
if (error && error.writeStarted === false) counted = false;
|
|
110
|
+
throw error;
|
|
111
|
+
} finally {
|
|
112
|
+
// A write that threw may still have landed: it is spaced like one that answered.
|
|
113
|
+
if (counted) {
|
|
114
|
+
state.gapMs = gapFor(platform);
|
|
115
|
+
state.nextAt = now() + state.gapMs;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
} finally {
|
|
119
|
+
release();
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// A task that writes to a social account (its tools, or what it says) gets the PACING block in its loop prompt.
|
|
126
|
+
const SOCIAL_TASK_RE = /\b(?:bluesky|bsky|tweet|twitter|x\.com|linkedin|facebook|instagram|threads|tiktok|reddit|mastodon)\b|\b(?:space|spacing|paced?|pacing)\b[^.\n]{0,80}\b(?:writes?|actions?|posts?|replies|likes?|follows?|seconds|minutes)\b|\b\d+\s*(?:to|-)\s*\d+\s*(?:s|sec|secs|seconds|min|minutes)\s+apart\b/i;
|
|
127
|
+
|
|
128
|
+
/** Whether a loop's task writes to a social account (and so may be told to pace itself). */
|
|
129
|
+
export function isSocialLoopTask(loopState) {
|
|
130
|
+
const tools = Array.isArray(loopState?.tools) ? loopState.tools.map(String) : [];
|
|
131
|
+
if (tools.includes('browser_bluesky')) return true;
|
|
132
|
+
return SOCIAL_TASK_RE.test(String(loopState?.task || ''));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The loop prompt's PACING block: the wait between writes is SynaBun's, so the agent never sleeps, starts a
|
|
137
|
+
* background wait or ends its turn to pace (that is what stalled the Bluesky schedules). '' for other tasks.
|
|
138
|
+
*/
|
|
139
|
+
export function socialPacingBlock(loopState) {
|
|
140
|
+
if (!isSocialLoopTask(loopState)) return '';
|
|
141
|
+
const gap = SOCIAL_WRITE_GAPS.bluesky;
|
|
142
|
+
return [
|
|
143
|
+
'',
|
|
144
|
+
'=== PACING (SynaBun spaces your writes) ===',
|
|
145
|
+
`- Any instruction in the task to space writes apart is enforced by SynaBun, not by you. bluesky_post, bluesky_action and bluesky_dm wait out the gap between this automation's Bluesky writes inside the call (${Math.round(gap.minMs / 1000)}-${Math.round(gap.maxMs / 1000)} s); the browser tools do the same for engagement clicks on X.`,
|
|
146
|
+
'- So call the next write directly. Never use sleep, a background shell, a Monitor or any timer to pace writes, and never end your turn to wait for a gap.',
|
|
147
|
+
'- When a write answers "Not written yet" because of the gap, call the same tool again at once with the same arguments: that call waits the rest and writes.',
|
|
148
|
+
'=== END PACING ===',
|
|
149
|
+
'',
|
|
150
|
+
].join('\n');
|
|
151
|
+
}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// ── Keeping SynaBun's own run secrets out of what a run says ──
|
|
2
|
+
//
|
|
3
|
+
// A run's terminal token (`sbt_…`, lib/terminal-tokens.js) and an Assistant brain's launch credential (`sbb_…`,
|
|
4
|
+
// assistant-runtime.js mintBrainCredential) live in the environment of the processes SynaBun starts for that run, a
|
|
5
|
+
// side panel or a brain: a CLI expands its MCP header from there, an OpenCode serve its `{env:…}` reference, a stdio
|
|
6
|
+
// MCP child reads it. The agent's own shell inherits that environment, so a worker that runs `env` (or `ps -E`) prints
|
|
7
|
+
// the value, and whatever SynaBun keeps of the run's output (its events and turn results in data/, a loop's log, a side
|
|
8
|
+
// panel's transcript the page stores, a reconnecting terminal's snapshot) would keep a live credential, which every
|
|
9
|
+
// backup then holds (review run c0eab425, finding M1).
|
|
10
|
+
//
|
|
11
|
+
// These scrubbers replace every value shaped like one of them with a placeholder, wherever it is: by shape, so they
|
|
12
|
+
// catch another run's token as well as the run's own. Where the exact value is known (the token registry registers
|
|
13
|
+
// every live token and credential here, registerKnownSecret; the terminal host passes the ones in a loop's
|
|
14
|
+
// environment), its usual encodings are replaced too (review 972afb74, finding 8): base64 and base64url of its body at
|
|
15
|
+
// each of the three byte alignments a longer encoded text can put it at (`env | base64`), hex in either case, and the
|
|
16
|
+
// base64 and hex of the 32 random bytes it encodes. URL encoding leaves base64url as it is, so the shape covers it.
|
|
17
|
+
// What no scrubber can cover is a run that wants its token out: one that reverses it, splits it with other text,
|
|
18
|
+
// wraps an encoding across lines, compresses or encrypts it. That is not a leak by accident, and the token is worth
|
|
19
|
+
// nothing once its run, panel process or brain ended.
|
|
20
|
+
//
|
|
21
|
+
// Nothing here imports anything: the terminal host (lib/pty-host/core.js, a process of its own) loads it too.
|
|
22
|
+
// Shapes: `sbt_` / `sbb_` + 43 base64url characters (32 random bytes). A string that merely looks like one is replaced
|
|
23
|
+
// too, which costs nothing.
|
|
24
|
+
|
|
25
|
+
/** What a token is replaced with. */
|
|
26
|
+
export const TOKEN_PLACEHOLDER = '[SynaBun token]';
|
|
27
|
+
|
|
28
|
+
const BODY_LENGTH = 43;
|
|
29
|
+
const TOKEN_LENGTH = 4 + BODY_LENGTH;
|
|
30
|
+
const SECRET_SHAPE_RE = /^sb[tb]_[A-Za-z0-9_-]{43}$/;
|
|
31
|
+
const MARKER_TEST_RE = /sb[tb]_/;
|
|
32
|
+
const BODY_AT_RE = /[A-Za-z0-9_-]{43}/y;
|
|
33
|
+
// 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
|
+
// marker and up to 42 of its characters.
|
|
35
|
+
const PARTIAL_TAIL_RE = /(?:s|sb|sb[tb]|sb[tb]_[A-Za-z0-9_-]{0,42})$/;
|
|
36
|
+
// A held beginning that starts with the marker is masked when the stream ends or waits; a lone `s`, `sb`, `sbt` (the
|
|
37
|
+
// end of a word) goes out as it is, and what continues it is masked when it arrives.
|
|
38
|
+
const MARKED_PARTIAL_RE = /^sb[tb]_/;
|
|
39
|
+
// A held beginning of a known encoded form this long or longer is masked when the stream waits; a shorter one goes
|
|
40
|
+
// out (at most this many characters of an encoding, a handful of bytes) and what continues it is masked.
|
|
41
|
+
const KNOWN_PARTIAL_MASK_MIN = 8;
|
|
42
|
+
/** How many known secrets the process-wide set keeps (the oldest goes first: a run that ended long ago). */
|
|
43
|
+
export const KNOWN_SECRETS_MAX = 512;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The encoded forms of `secret` (a token or a brain credential) to scrub besides the secret itself: substrings any
|
|
47
|
+
* base64 / base64url encoding of a text holding it contains whatever bytes come before or after, hex in both cases,
|
|
48
|
+
* the body in the standard base64 alphabet, and the base64 / hex of the 32 bytes the body encodes. [] for anything
|
|
49
|
+
* that is not shaped like one.
|
|
50
|
+
*/
|
|
51
|
+
export function encodedSecretForms(secret) {
|
|
52
|
+
if (typeof secret !== 'string' || !SECRET_SHAPE_RE.test(secret)) return [];
|
|
53
|
+
const bodyText = secret.slice(4);
|
|
54
|
+
const body = Buffer.from(bodyText, 'utf8');
|
|
55
|
+
const forms = new Set();
|
|
56
|
+
// Base64 of a longer text puts the body at offset 0, 1 or 2 of a 3-byte group: keep only the characters every
|
|
57
|
+
// such encoding shares (the groups made of the body's own bytes).
|
|
58
|
+
for (let k = 0; k < 3; k++) {
|
|
59
|
+
const encoded = Buffer.concat([Buffer.alloc(k), body]).toString('base64');
|
|
60
|
+
const stable = encoded.slice(k === 0 ? 0 : k + 1, 4 * Math.floor((k + body.length) / 3));
|
|
61
|
+
forms.add(stable);
|
|
62
|
+
forms.add(stable.replace(/\+/g, '-').replace(/\//g, '_'));
|
|
63
|
+
}
|
|
64
|
+
const hex = body.toString('hex');
|
|
65
|
+
forms.add(hex);
|
|
66
|
+
forms.add(hex.toUpperCase());
|
|
67
|
+
// The same 32 random bytes the body spells: the standard alphabet, and their hex.
|
|
68
|
+
forms.add(bodyText.replace(/-/g, '+').replace(/_/g, '/'));
|
|
69
|
+
const raw = Buffer.from(bodyText, 'base64url');
|
|
70
|
+
if (raw.length === 32) {
|
|
71
|
+
forms.add(raw.toString('hex'));
|
|
72
|
+
forms.add(raw.toString('hex').toUpperCase());
|
|
73
|
+
forms.add(raw.toString('base64').replace(/=+$/, ''));
|
|
74
|
+
}
|
|
75
|
+
// The body without its marker too (the shape needs the marker): `${TOKEN#sbt_}`, a decoder's output.
|
|
76
|
+
forms.add(bodyText);
|
|
77
|
+
return [...forms].filter((form) => form.length >= 40);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ── The process-wide set of known secrets (the token registry fills it) ──
|
|
81
|
+
const knownSecrets = new Map(); // secret → its encoded forms
|
|
82
|
+
let knownForms = [];
|
|
83
|
+
|
|
84
|
+
function rebuildKnown() {
|
|
85
|
+
const all = new Set();
|
|
86
|
+
for (const forms of knownSecrets.values()) for (const form of forms) all.add(form);
|
|
87
|
+
knownForms = [...all];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Scrub `secret`'s encoded forms too from now on (a live token or credential). Returns whether it was taken. */
|
|
91
|
+
export function registerKnownSecret(secret) {
|
|
92
|
+
const forms = encodedSecretForms(secret);
|
|
93
|
+
if (!forms.length) return false;
|
|
94
|
+
if (knownSecrets.has(secret)) return true;
|
|
95
|
+
knownSecrets.set(secret, forms);
|
|
96
|
+
while (knownSecrets.size > KNOWN_SECRETS_MAX) knownSecrets.delete(knownSecrets.keys().next().value);
|
|
97
|
+
rebuildKnown();
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Stop looking for `secret`'s encoded forms (its run ended: the value proves nothing any more). */
|
|
102
|
+
export function forgetKnownSecret(secret) {
|
|
103
|
+
if (!knownSecrets.delete(secret)) return false;
|
|
104
|
+
rebuildKnown();
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** For tests: how many secrets the process-wide set holds. */
|
|
109
|
+
export function knownSecretCount() {
|
|
110
|
+
return knownSecrets.size;
|
|
111
|
+
}
|
|
112
|
+
|
|
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
|
+
/** Sorted, overlapping or touching stretches joined. */
|
|
119
|
+
function mergeIntervals(found) {
|
|
120
|
+
if (found.length < 2) return found;
|
|
121
|
+
found.sort((a, b) => a[0] - b[0]);
|
|
122
|
+
const merged = [found[0].slice()];
|
|
123
|
+
for (const [start, end] of found.slice(1)) {
|
|
124
|
+
const last = merged[merged.length - 1];
|
|
125
|
+
if (start <= last[1]) last[1] = Math.max(last[1], end);
|
|
126
|
+
else merged.push([start, end]);
|
|
127
|
+
}
|
|
128
|
+
return merged;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 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.
|
|
134
|
+
*/
|
|
135
|
+
function secretIntervals(text, forms) {
|
|
136
|
+
const found = [];
|
|
137
|
+
if (MARKER_TEST_RE.test(text)) {
|
|
138
|
+
const marker = /sb[tb]_/g;
|
|
139
|
+
for (let m = marker.exec(text); m; m = marker.exec(text)) {
|
|
140
|
+
BODY_AT_RE.lastIndex = m.index + 4;
|
|
141
|
+
if (BODY_AT_RE.test(text)) found.push([m.index, m.index + TOKEN_LENGTH]);
|
|
142
|
+
marker.lastIndex = m.index + 1;
|
|
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]);
|
|
147
|
+
}
|
|
148
|
+
return mergeIntervals(found);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* `text[from, to)` with every masked stretch replaced by one placeholder. `state.lastMasked` says whether what was
|
|
153
|
+
* passed on just before `from` was masked: a stretch that continues it adds no second placeholder.
|
|
154
|
+
*/
|
|
155
|
+
function render(text, from, to, intervals, state = { lastMasked: false }) {
|
|
156
|
+
let out = '';
|
|
157
|
+
let at = from;
|
|
158
|
+
for (const [start, end] of intervals) {
|
|
159
|
+
if (end <= at || start >= to) continue;
|
|
160
|
+
if (start > at) { out += text.slice(at, start); state.lastMasked = false; }
|
|
161
|
+
const continues = start < from && at === from && state.lastMasked;
|
|
162
|
+
if (!continues) out += TOKEN_PLACEHOLDER;
|
|
163
|
+
state.lastMasked = true;
|
|
164
|
+
at = Math.min(end, to);
|
|
165
|
+
}
|
|
166
|
+
if (at < to) { out += text.slice(at, to); state.lastMasked = false; }
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** `text` with every token-shaped value and every known form replaced (the same string when there is none). */
|
|
171
|
+
export function scrubTokenText(text) {
|
|
172
|
+
if (typeof text !== 'string' || text.length < 40) return text;
|
|
173
|
+
const intervals = secretIntervals(text, knownForms);
|
|
174
|
+
return intervals.length ? render(text, 0, text.length, intervals) : text;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Whether a string holds a token-shaped value or a known form. */
|
|
178
|
+
export function containsTokenText(text) {
|
|
179
|
+
return typeof text === 'string' && text.length >= 40 && secretIntervals(text, knownForms).length > 0;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* `value` with every token-shaped string replaced, at any depth of plain objects and arrays (the events, results and
|
|
184
|
+
* messages SynaBun passes on). Copy on write: what holds no token is returned as it is (the same reference), and so is
|
|
185
|
+
* anything that is not a plain object or an array (a Buffer, a Date, a class instance). Cycles and depth are bounded.
|
|
186
|
+
*/
|
|
187
|
+
export function redactTerminalSecrets(value) {
|
|
188
|
+
return redactValue(value, new WeakSet(), 0);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function redactValue(value, seen, depth) {
|
|
192
|
+
if (typeof value === 'string') return scrubTokenText(value);
|
|
193
|
+
if (!value || typeof value !== 'object' || depth > 64 || seen.has(value)) return value;
|
|
194
|
+
if (Array.isArray(value)) {
|
|
195
|
+
seen.add(value);
|
|
196
|
+
let out = null;
|
|
197
|
+
for (let i = 0; i < value.length; i++) {
|
|
198
|
+
const next = redactValue(value[i], seen, depth + 1);
|
|
199
|
+
if (next !== value[i]) { if (!out) out = value.slice(); out[i] = next; }
|
|
200
|
+
}
|
|
201
|
+
return out || value;
|
|
202
|
+
}
|
|
203
|
+
const proto = Object.getPrototypeOf(value);
|
|
204
|
+
if (proto !== Object.prototype && proto !== null) return value;
|
|
205
|
+
seen.add(value);
|
|
206
|
+
let out = null;
|
|
207
|
+
for (const key of Object.keys(value)) {
|
|
208
|
+
const item = value[key];
|
|
209
|
+
const next = redactValue(item, seen, depth + 1);
|
|
210
|
+
if (next !== item) {
|
|
211
|
+
if (!out) out = { ...value };
|
|
212
|
+
out[key] = next;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return out || value;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The same for a stream (a terminal's output), where a token can arrive in pieces, at any time. Whatever the chunks and
|
|
220
|
+
* however long the waits between them, what it passes on never holds a whole token or a whole known form: push()
|
|
221
|
+
* returns what is safe to pass on and holds back an end that could still become one (a token's beginning, the
|
|
222
|
+
* beginning of a known form) until the next chunk tells; flush() gives back what is held (a held beginning that starts
|
|
223
|
+
* with the marker, or a held known form's beginning of 8 characters or more, masked); pending() says whether something
|
|
224
|
+
* is held, so the caller can release it after a short wait when no next chunk comes. What follows a beginning that went
|
|
225
|
+
* out is masked when it arrives (the redactor keeps the last characters it saw, not only what it holds): before
|
|
226
|
+
* 2026-11-05 a short beginning flushed after the wait was forgotten, and its rest passed on whole (review 972afb74,
|
|
227
|
+
* finding 6). `secrets`: the exact values this stream may carry (a loop's own token), whose encoded forms are masked
|
|
228
|
+
* too, besides the process-wide known ones.
|
|
229
|
+
*/
|
|
230
|
+
export function createTokenStreamRedactor({ secrets = [] } = {}) {
|
|
231
|
+
const extraForms = [];
|
|
232
|
+
for (const secret of secrets || []) for (const form of encodedSecretForms(secret)) extraForms.push(form);
|
|
233
|
+
let raw = ''; // the last characters seen (passed on and held), unchanged
|
|
234
|
+
let held = 0; // how many of raw's last characters are held back
|
|
235
|
+
const state = { lastMasked: false };
|
|
236
|
+
|
|
237
|
+
const windowFor = (forms) => Math.max(TOKEN_LENGTH, ...forms.map((f) => f.length));
|
|
238
|
+
/** Where the longest end of `text` that could still become a token or a known form starts, and whether to mask it. */
|
|
239
|
+
function partial(text, forms) {
|
|
240
|
+
let start = text.length;
|
|
241
|
+
let maskFrom = text.length;
|
|
242
|
+
const tail = PARTIAL_TAIL_RE.exec(text);
|
|
243
|
+
if (tail) {
|
|
244
|
+
start = Math.min(start, tail.index);
|
|
245
|
+
if (MARKED_PARTIAL_RE.test(tail[0])) maskFrom = Math.min(maskFrom, tail.index);
|
|
246
|
+
}
|
|
247
|
+
for (const form of forms) {
|
|
248
|
+
const from = Math.max(0, text.length - (form.length - 1));
|
|
249
|
+
for (let i = from; i < text.length; i++) {
|
|
250
|
+
if (text.charCodeAt(i) !== form.charCodeAt(0) || !form.startsWith(text.slice(i))) continue;
|
|
251
|
+
start = Math.min(start, i);
|
|
252
|
+
if (text.length - i >= KNOWN_PARTIAL_MASK_MIN) maskFrom = Math.min(maskFrom, i);
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return { start, maskFrom };
|
|
257
|
+
}
|
|
258
|
+
function trim(forms) {
|
|
259
|
+
const keep = windowFor(forms) + held;
|
|
260
|
+
if (raw.length > keep) raw = raw.slice(raw.length - keep);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
push(chunk) {
|
|
265
|
+
const text = String(chunk ?? '');
|
|
266
|
+
const forms = formsWith(extraForms);
|
|
267
|
+
const emittedEnd = raw.length - held;
|
|
268
|
+
raw += text;
|
|
269
|
+
const intervals = secretIntervals(raw, forms);
|
|
270
|
+
const { start } = partial(raw, forms);
|
|
271
|
+
const holdFrom = Math.max(emittedEnd, start);
|
|
272
|
+
const out = render(raw, emittedEnd, holdFrom, intervals, state);
|
|
273
|
+
held = raw.length - holdFrom;
|
|
274
|
+
trim(forms);
|
|
275
|
+
return out;
|
|
276
|
+
},
|
|
277
|
+
flush() {
|
|
278
|
+
if (!held) return '';
|
|
279
|
+
const forms = formsWith(extraForms);
|
|
280
|
+
const emittedEnd = raw.length - held;
|
|
281
|
+
const found = secretIntervals(raw, forms);
|
|
282
|
+
const { maskFrom } = partial(raw, forms);
|
|
283
|
+
// From where the held beginning starts, which may be before what went out: render() clips it and adds no
|
|
284
|
+
// second placeholder when the part that went out was masked already.
|
|
285
|
+
if (maskFrom < raw.length) found.push([maskFrom, raw.length]);
|
|
286
|
+
const out = render(raw, emittedEnd, raw.length, mergeIntervals(found), state);
|
|
287
|
+
held = 0;
|
|
288
|
+
trim(forms);
|
|
289
|
+
return out;
|
|
290
|
+
},
|
|
291
|
+
pending: () => held > 0,
|
|
292
|
+
};
|
|
293
|
+
}
|