synabun 2.4.4 → 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 +42 -0
- package/README.md +1 -1
- package/docs/browser-extension-bridge.md +5 -4
- package/docs/whatsapp.md +19 -3
- 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/neural-interface.d.ts +1 -0
- package/mcp-server/dist/services/neural-interface.d.ts.map +1 -1
- package/mcp-server/dist/services/neural-interface.js +1 -0
- package/mcp-server/dist/services/neural-interface.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 +18 -3
- package/mcp-server/dist/tools/agents.d.ts.map +1 -1
- package/mcp-server/dist/tools/agents.js +85 -16
- 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/neural-interface.ts +2 -1
- package/mcp-server/src/services/tool-catalog.ts +1 -1
- package/mcp-server/src/tools/agents.ts +78 -16
- 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 +484 -19
- package/neural-interface/i18n/pt-BR.json +484 -19
- package/neural-interface/lib/agents-studio-api.js +7 -2
- package/neural-interface/lib/antigravity/home.js +43 -8
- package/neural-interface/lib/antigravity/panel-bridge.js +7 -3
- package/neural-interface/lib/antigravity/tool-call.js +14 -0
- package/neural-interface/lib/assistant-brains/antigravity.js +4 -2
- package/neural-interface/lib/assistant-dispatch.js +15 -1
- package/neural-interface/lib/assistant-persona.js +8 -3
- package/neural-interface/lib/assistant-route-gate.js +11 -2
- package/neural-interface/lib/assistant-runtime.js +95 -13
- package/neural-interface/lib/assistant-spilled-output.js +53 -0
- package/neural-interface/lib/assistant-user-context.js +7 -3
- package/neural-interface/lib/browser-default.js +22 -2
- package/neural-interface/lib/browser-extension-bridge.js +50 -3
- package/neural-interface/lib/browser-extension-pin.json +7 -7
- package/neural-interface/lib/browser-extension-protocol.js +30 -1
- package/neural-interface/lib/browser-extension-setup.js +204 -36
- 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/remote-policy.js +24 -7
- 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 +34 -6
- package/neural-interface/lib/teams/api.js +94 -10
- package/neural-interface/lib/teams/index.js +4 -0
- package/neural-interface/lib/teams/landing.js +195 -75
- package/neural-interface/lib/teams/recovery.js +68 -0
- package/neural-interface/lib/teams/scheduler.js +380 -61
- package/neural-interface/lib/teams/worktrees.js +123 -30
- 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/extension-setup.html +6 -2
- package/neural-interface/public/index.html +117 -37
- package/neural-interface/public/shared/agents/studio/ax-api.js +27 -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 +436 -33
- package/neural-interface/public/shared/agents/studio/ax-teams.js +357 -10
- 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/browser-extension-updates.js +3 -1
- 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/extension-setup/es-page.js +23 -4
- package/neural-interface/public/shared/extension-setup/es-view.js +51 -11
- package/neural-interface/public/shared/extension-update-view.js +1 -1
- package/neural-interface/public/shared/loading-diagnostic-entry.js +16 -1
- package/neural-interface/public/shared/settings/settings-ai-browser.js +23 -3
- package/neural-interface/public/shared/settings/settings-browser-extension.js +575 -197
- package/neural-interface/public/shared/settings/settings-extension-diagrams.js +116 -0
- package/neural-interface/public/shared/settings/settings-extension-guide.js +222 -0
- package/neural-interface/public/shared/settings/settings-ia.js +2 -2
- package/neural-interface/public/shared/settings/settings-shell.js +5 -0
- 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/public/shared/styles.css +196 -13
- package/neural-interface/public/shared/ui-settings.js +48 -11
- package/neural-interface/server.js +336 -253
- package/npm-shrinkwrap.json +2 -2
- package/package.json +2 -1
- package/skills/skill-forge/SKILL.md +7 -3
|
@@ -548,16 +548,111 @@ export function createTeamWorktrees({ state, git, locks, setup: defaultSetup = n
|
|
|
548
548
|
});
|
|
549
549
|
}
|
|
550
550
|
|
|
551
|
+
/**
|
|
552
|
+
* The checks of a guarded removal, read only: every worktree is a linked worktree of its own on its branch, clean
|
|
553
|
+
* (nothing uncommitted, untracked files included), and every branch is merged into `target` (mergedInto), with
|
|
554
|
+
* `git branch -d`'s own criterion met for the ancestors it deletes. The same checks answer "could this go now"
|
|
555
|
+
* for a list (`onTarget`) and run again, under the repository's lock, right before anything is removed.
|
|
556
|
+
* → { items: [{ sliceId, path, branch, head, via }], checks: [{ name: 'clean'|'merged', ok, detail, sliceId, … }] }
|
|
557
|
+
*/
|
|
558
|
+
async function guardedChecks(record, target) {
|
|
559
|
+
const teamId = record.teamId;
|
|
560
|
+
const checks = [];
|
|
561
|
+
const items = piecesOf(record.plan).map(sliceId => ({ sliceId, path: worktreePath(record.projectPath, teamId, sliceId), branch: sliceId === INTEGRATION_SLICE_ID ? integrationBranchName(teamId) : branchName(teamId, sliceId) }));
|
|
562
|
+
const registered = await listWorktrees(git, record.projectPath);
|
|
563
|
+
for (const item of items) {
|
|
564
|
+
let linked = false;
|
|
565
|
+
try { linked = lstatSync(join(item.path, '.git')).isFile() && !lstatSync(item.path).isSymbolicLink(); } catch {}
|
|
566
|
+
const valid = linked && await branchAt(git, item.path) === item.branch && registered.some(w => realOf(w.path) === realOf(item.path) && w.branch === item.branch && !w.locked) && registered.every(w => w.branch !== item.branch || realOf(w.path) === realOf(item.path));
|
|
567
|
+
checks.push({ name: 'clean', ok: !!valid, detail: `${item.path}: ${valid ? 'linked worktree verified' : 'not an unlocked linked worktree on its expected branch'}`, sliceId: item.sliceId, path: item.path, kind: 'worktree' });
|
|
568
|
+
if (valid) {
|
|
569
|
+
const changes = await changesOf(git, item.path);
|
|
570
|
+
checks.push({ name: 'clean', ok: changes !== null && changes.length === 0, detail: `${item.path}: ${changes === null ? 'status unavailable' : changes.map(c => c.path).join(', ') || 'clean'}`, sliceId: item.sliceId, path: item.path, kind: 'changes', files: (changes || []).map(c => c.path).slice(0, 50) });
|
|
571
|
+
}
|
|
572
|
+
// Merged: an ancestor of the target, or every commit beyond the base patch-equivalent to one in it
|
|
573
|
+
// (mergedInto). The head it checked is the one the deletion compares against.
|
|
574
|
+
const merged = await mergedInto(git, record.projectPath, item.branch, target, record.baseCommit);
|
|
575
|
+
item.head = merged.head;
|
|
576
|
+
item.via = merged.via;
|
|
577
|
+
checks.push({ name: 'merged', ok: merged.ok, detail: merged.detail, branch: item.branch, head: merged.head, via: merged.via, commits: merged.commits, sliceId: item.sliceId });
|
|
578
|
+
}
|
|
579
|
+
// branch -d checks the configured upstream, or HEAD when none exists.
|
|
580
|
+
// Verify that criterion too for the ancestors it deletes, so a different target checkout cannot cause
|
|
581
|
+
// partial removal. A patch-equivalent branch is deleted by compare-and-swap, which asks neither.
|
|
582
|
+
for (const item of items.filter((each) => each.via === 'ancestor')) {
|
|
583
|
+
const upstream = line(await git(['rev-parse', '--abbrev-ref', `${item.branch}@{upstream}`], { cwd: record.projectPath })) || 'HEAD';
|
|
584
|
+
const safe = (await git(['merge-base', '--is-ancestor', item.head, upstream], { cwd: record.projectPath })).ok;
|
|
585
|
+
checks.push({ name: 'merged', ok: safe, detail: `${item.branch}: safe branch -d against ${upstream}`, branch: item.branch, sliceId: item.sliceId });
|
|
586
|
+
}
|
|
587
|
+
return { items, checks };
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Is everything a `ready` team joined on its base branch already: the commit SynaBun recorded as the integration
|
|
592
|
+
* head, and the integration branch as it is now (when somebody moved it)? Asked of git, never of the records.
|
|
593
|
+
* → { ok, detail, target, targetHead, head, live }
|
|
594
|
+
*/
|
|
595
|
+
async function onTargetCheck(record) {
|
|
596
|
+
const cwd = record.projectPath;
|
|
597
|
+
const target = record.baseBranch;
|
|
598
|
+
const branch = integrationBranchName(record.teamId);
|
|
599
|
+
const no = (detail, extra = {}) => ({ ok: false, detail, target: target || null, ...extra });
|
|
600
|
+
if (!target) return no('The project was on no branch when the team was created, so there is no base branch to compare with.');
|
|
601
|
+
const targetHead = await commitOf(git, cwd, `refs/heads/${target}`);
|
|
602
|
+
if (!targetHead) return no(`The base branch ${target} is not there.`);
|
|
603
|
+
const head = record.integration?.head || null;
|
|
604
|
+
const live = await commitOf(git, cwd, `refs/heads/${branch}`);
|
|
605
|
+
if (!head) return no('No integration head is recorded for this team.', { targetHead });
|
|
606
|
+
if (!live) return no(`${branch} is not there.`, { targetHead, head });
|
|
607
|
+
const on = async (commit) => commit === targetHead || (await git(['merge-base', '--is-ancestor', commit, targetHead], { cwd })).ok;
|
|
608
|
+
if (!(await on(head))) return no(`${target} does not have the team's integration head ${head.slice(0, 12)}: it has not landed.`, { targetHead, head, live });
|
|
609
|
+
if (live !== head && !(await on(live))) return no(`${branch} moved to ${live.slice(0, 12)}, which ${target} does not have.`, { targetHead, head, live });
|
|
610
|
+
return { ok: true, detail: `${target} has ${branch} (${head.slice(0, 12)}).`, target, targetHead, head, live };
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* A `ready` code team whose joined work is on its base branch already (merged outside `land`, its worktrees and
|
|
615
|
+
* branches still there): where, and whether its worktrees and branches could go now with nothing lost (`clean`:
|
|
616
|
+
* every guarded check passes). Read only and without a lock: a list asks it. Null for any other team, and when git
|
|
617
|
+
* does not show the work on the base branch. Never removes anything: `remove(teamId, { guarded: true,
|
|
618
|
+
* mergedOutside: true })` asks it all again, under the lock, before it does.
|
|
619
|
+
*
|
|
620
|
+
* @returns {Promise<null | { branch: string, head: string, targetHead: string, live: string, moved: boolean,
|
|
621
|
+
* clean: boolean, dirty: { sliceId: string, path: string, files: string[] }[],
|
|
622
|
+
* unmerged: { sliceId: string, branch: string, commits: string[] }[], other: string[] }>}
|
|
623
|
+
*/
|
|
624
|
+
async function onTarget(teamId) {
|
|
625
|
+
const record = await get(teamId);
|
|
626
|
+
if (!record || record.plan.readOnly || record.removed || record.state !== 'ready' || !isDirectory(record.projectPath)) return null;
|
|
627
|
+
const on = await onTargetCheck(record);
|
|
628
|
+
if (!on.ok) return null;
|
|
629
|
+
const { checks } = await guardedChecks(record, on.target);
|
|
630
|
+
const failed = checks.filter((check) => !check.ok);
|
|
631
|
+
// Uncommitted files git listed; commits the base branch lacks; anything else (a status git could not read, a
|
|
632
|
+
// worktree that is not where SynaBun left it, a branch that is gone) as git's own words.
|
|
633
|
+
const isDirty = (check) => check.kind === 'changes' && Array.isArray(check.files) && check.files.length > 0;
|
|
634
|
+
const isUnmerged = (check) => check.name === 'merged' && Array.isArray(check.commits) && check.commits.length > 0;
|
|
635
|
+
const dirty = failed.filter(isDirty).map((check) => ({ sliceId: check.sliceId, path: check.path, files: check.files }));
|
|
636
|
+
const unmerged = failed.filter(isUnmerged).map((check) => ({ sliceId: check.sliceId, branch: check.branch, commits: check.commits.slice(0, 20) }));
|
|
637
|
+
const other = failed.filter((check) => !isDirty(check) && !isUnmerged(check)).map((check) => check.detail).slice(0, 10);
|
|
638
|
+
return { branch: on.target, head: on.head, targetHead: on.targetHead, live: on.live, moved: on.live !== on.head, clean: failed.length === 0, dirty, unmerged, other };
|
|
639
|
+
}
|
|
640
|
+
|
|
551
641
|
/**
|
|
552
642
|
* Remove a team's worktrees and branches. Only after a landing, or with `discard: true`. A worktree with
|
|
553
643
|
* uncommitted changes and a branch whose commits are nowhere else are kept and reported, unless `force: true`.
|
|
644
|
+
* `guarded: true` removes all or nothing, after every check passed (guardedChecks); with `mergedOutside: true` it
|
|
645
|
+
* takes a `ready` team whose work is on its base branch already, records that landing (`landed.external`) and
|
|
646
|
+
* removes its worktrees and branches the same way.
|
|
554
647
|
*
|
|
555
648
|
* @returns {Promise<{ ok: true, complete: boolean, removed: object[], kept: object[] } | { ok: false, code: string }>}
|
|
556
649
|
*/
|
|
557
|
-
async function remove(teamId, { discard = false, force = false, guarded = false, checkOnly = false } = {}) {
|
|
650
|
+
async function remove(teamId, { discard = false, force = false, guarded = false, checkOnly = false, mergedOutside = false } = {}) {
|
|
558
651
|
const record = await get(teamId);
|
|
559
652
|
if (!record) return refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is recorded.`);
|
|
560
653
|
if (guarded && record.plan.readOnly) return refuse('TEAM_NOT_LANDED', 'Read-only teams cannot land.', { checks: [{ name: 'landed', ok: false, detail: 'Read-only team' }] });
|
|
654
|
+
if (mergedOutside && !guarded) return refuse('INVALID_OPTIONS', 'mergedOutside is a guarded removal.');
|
|
655
|
+
if (mergedOutside && (record.state !== 'ready' || record.removed)) return refuse('TEAM_NOT_READY', `Team "${teamId}" is ${record.removed ? 'removed' : record.state}: only a ready team whose work is on its base branch already is cleaned up this way.`, { state: record.state, checks: [{ name: 'on-target', ok: false, detail: `Team state: ${record.state}` }] });
|
|
561
656
|
if (record.plan.readOnly) {
|
|
562
657
|
const removed = [], kept = [];
|
|
563
658
|
for (const slice of record.plan.slices) {
|
|
@@ -570,42 +665,40 @@ export function createTeamWorktrees({ state, git, locks, setup: defaultSetup = n
|
|
|
570
665
|
}
|
|
571
666
|
if (record.state === 'integrating' || record.state === 'landing' || record.state === 'creating') return refuse('TEAM_NEEDS_RECOVERY', `Team "${teamId}" is recorded as ${record.state}: run recover() before removing it.`, { state: record.state });
|
|
572
667
|
const landed = record.state === 'merged';
|
|
573
|
-
if (!landed && !discard && !record.plan.readOnly && !checkOnly) return refuse('TEAM_NOT_LANDED', `Team "${teamId}" has not landed: its result is on ${integrationBranchName(teamId)}. Land it first, or pass discard: true to give its work up.`, { state: record.state });
|
|
668
|
+
if (!landed && !discard && !record.plan.readOnly && !checkOnly && !mergedOutside) return refuse('TEAM_NOT_LANDED', `Team "${teamId}" has not landed: its result is on ${integrationBranchName(teamId)}. Land it first, or pass discard: true to give its work up.`, { state: record.state });
|
|
574
669
|
if (!isDirectory(record.projectPath)) return refuse('PROJECT_NOT_FOUND', `${record.projectPath} is not a folder.`);
|
|
575
670
|
|
|
576
671
|
return locks.run(repoKey(record.projectPath), async () => {
|
|
577
672
|
if (guarded) {
|
|
673
|
+
// Merged outside `land`: what the integration branch has must be on the base branch already (asked again
|
|
674
|
+
// here, under the lock), and the base branch is the target every other check measures against.
|
|
675
|
+
// The record as it is now, under the lock: what is checked and what is recorded are the same team.
|
|
676
|
+
const subject = mergedOutside ? await get(teamId) : record;
|
|
677
|
+
if (mergedOutside && (subject?.state !== 'ready' || subject.removed)) return refuse('TEAM_NOT_READY', `Team "${teamId}" is ${subject?.removed ? 'removed' : subject?.state || 'gone'} now: nothing was removed.`, { state: subject?.state || null, checks: [{ name: 'on-target', ok: false, detail: `Team state: ${subject?.state || 'gone'}` }] });
|
|
678
|
+
const target = mergedOutside ? subject.baseBranch : record.landed?.targetBranch;
|
|
578
679
|
const checks = [];
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
let linked = false;
|
|
584
|
-
try { linked = lstatSync(join(item.path, '.git')).isFile() && !lstatSync(item.path).isSymbolicLink(); } catch {}
|
|
585
|
-
const valid = linked && await branchAt(git, item.path) === item.branch && registered.some(w => realOf(w.path) === realOf(item.path) && w.branch === item.branch && !w.locked) && registered.every(w => w.branch !== item.branch || realOf(w.path) === realOf(item.path));
|
|
586
|
-
checks.push({ name: 'clean', ok: !!valid, detail: `${item.path}: ${valid ? 'linked worktree verified' : 'not an unlocked linked worktree on its expected branch'}` });
|
|
587
|
-
if (valid) {
|
|
588
|
-
const changes = await changesOf(git, item.path);
|
|
589
|
-
checks.push({ name: 'clean', ok: changes !== null && changes.length === 0, detail: `${item.path}: ${changes === null ? 'status unavailable' : changes.map(c => c.path).join(', ') || 'clean'}` });
|
|
590
|
-
}
|
|
591
|
-
// Merged: an ancestor of the target, or every commit beyond the base patch-equivalent to one in it
|
|
592
|
-
// (mergedInto). The head it checked is the one the deletion compares against.
|
|
593
|
-
const merged = await mergedInto(git, record.projectPath, item.branch, target, record.baseCommit);
|
|
594
|
-
item.head = merged.head;
|
|
595
|
-
item.via = merged.via;
|
|
596
|
-
checks.push({ name: 'merged', ok: merged.ok, detail: merged.detail, branch: item.branch, head: merged.head, via: merged.via, commits: merged.commits });
|
|
597
|
-
}
|
|
598
|
-
// branch -d checks the configured upstream, or HEAD when none exists.
|
|
599
|
-
// Verify that criterion too for the ancestors it deletes, so a different target checkout cannot cause
|
|
600
|
-
// partial removal. A patch-equivalent branch is deleted by compare-and-swap, which asks neither.
|
|
601
|
-
for (const item of items.filter((each) => each.via === 'ancestor')) {
|
|
602
|
-
const upstream = line(await git(['rev-parse', '--abbrev-ref', `${item.branch}@{upstream}`], { cwd: record.projectPath })) || 'HEAD';
|
|
603
|
-
const safe = (await git(['merge-base', '--is-ancestor', item.head, upstream], { cwd: record.projectPath })).ok;
|
|
604
|
-
checks.push({ name: 'merged', ok: safe, detail: `${item.branch}: safe branch -d against ${upstream}` });
|
|
680
|
+
if (mergedOutside) {
|
|
681
|
+
const on = await onTargetCheck(subject);
|
|
682
|
+
checks.push({ name: 'on-target', ok: on.ok, detail: on.detail });
|
|
683
|
+
if (!on.ok) return refuse('NOT_ON_TARGET', on.detail, { checks });
|
|
605
684
|
}
|
|
685
|
+
const { items, checks: guardedList } = await guardedChecks(subject, target);
|
|
686
|
+
checks.push(...guardedList);
|
|
606
687
|
const failed = checks.find(c => !c.ok);
|
|
607
688
|
if (failed) return refuse(failed.name === 'merged' ? 'BRANCH_NOT_MERGED' : 'WORKTREE_DIRTY', failed.detail, { checks });
|
|
608
689
|
if (checkOnly) return { ok: true, checks };
|
|
690
|
+
// The team's work is on its base branch: that is a landing, made by somebody else. It is written before
|
|
691
|
+
// anything is removed, so a stop half-way leaves a team that says where its work is.
|
|
692
|
+
let landedOutside = null;
|
|
693
|
+
if (mergedOutside) {
|
|
694
|
+
const tests = subject.integration.tests;
|
|
695
|
+
const head = subject.integration.head;
|
|
696
|
+
landedOutside = {
|
|
697
|
+
targetBranch: target, mode: null, from: null, to: head, integrationHead: head, mergeCommit: null, checkout: null,
|
|
698
|
+
files: null, at: now(), partial: false, tested: !!tests && tests.ok === true && tests.commit === head, external: true,
|
|
699
|
+
};
|
|
700
|
+
await state.writeTeam(teamId, 'merged', { landing: null, landed: landedOutside });
|
|
701
|
+
}
|
|
609
702
|
// All checks precede all mutations. The branches are settled first, in one atomic ref transaction: every
|
|
610
703
|
// ancestor is verified at the head that was checked, and every patch-equivalent branch is deleted only if it
|
|
611
704
|
// is still at that head (`git branch -d` refuses those, and `-D` is never used). Such a branch is checked
|
|
@@ -660,7 +753,7 @@ export function createTeamWorktrees({ state, git, locks, setup: defaultSetup = n
|
|
|
660
753
|
if (!pruned.ok) throw new Error(`Cleanup prune failed: ${gitSaid(pruned)}`);
|
|
661
754
|
try { rmdirSync(join(worktreesRoot(record.projectPath), teamId)); } catch (error) { if (error.code !== 'ENOENT' && error.code !== 'ENOTEMPTY') throw error; }
|
|
662
755
|
await state.writeTeam(teamId, 'merged', { removed: true, removal: { at: now(), force: false, removed, kept: [] } });
|
|
663
|
-
return { ok: true, complete: true, removed, checks };
|
|
756
|
+
return { ok: true, complete: true, removed, checks, ...(landedOutside ? { landed: landedOutside } : {}) };
|
|
664
757
|
}
|
|
665
758
|
const registered = await listWorktrees(git, record.projectPath);
|
|
666
759
|
const integration = record.plan.readOnly ? {} : await pieceInfo(record, INTEGRATION_SLICE_ID, registered);
|
|
@@ -745,7 +838,7 @@ export function createTeamWorktrees({ state, git, locks, setup: defaultSetup = n
|
|
|
745
838
|
});
|
|
746
839
|
}
|
|
747
840
|
|
|
748
|
-
return { create, get, list, inspect, startSlice, finishReadOnlySlice, remove, prune, materialize, unmake, pieceInfo, placeOf, collisions };
|
|
841
|
+
return { create, get, list, inspect, onTarget, startSlice, finishReadOnlySlice, remove, prune, materialize, unmake, pieceInfo, placeOf, collisions };
|
|
749
842
|
}
|
|
750
843
|
|
|
751
844
|
/** A test command as it is stored: text, a list of arguments, or null. `false` for anything else. */
|
|
@@ -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,
|