synabun 2.4.5 → 2.4.7

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.
Files changed (108) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +1 -1
  3. package/docs/whatsapp.md +17 -1
  4. package/hooks/claude-code/session-start.mjs +21 -18
  5. package/lib/claude-global-config.js +214 -0
  6. package/lib/client-config-repair.js +17 -3
  7. package/mcp-server/dist/index.d.ts.map +1 -1
  8. package/mcp-server/dist/index.js +6 -1
  9. package/mcp-server/dist/index.js.map +1 -1
  10. package/mcp-server/dist/services/tool-catalog.js +1 -1
  11. package/mcp-server/dist/services/tool-catalog.js.map +1 -1
  12. package/mcp-server/dist/tools/agents.d.ts +2 -0
  13. package/mcp-server/dist/tools/agents.d.ts.map +1 -1
  14. package/mcp-server/dist/tools/agents.js +11 -5
  15. package/mcp-server/dist/tools/agents.js.map +1 -1
  16. package/mcp-server/dist/tools/skill-tools.d.ts +24 -5
  17. package/mcp-server/dist/tools/skill-tools.d.ts.map +1 -1
  18. package/mcp-server/dist/tools/skill-tools.js +88 -8
  19. package/mcp-server/dist/tools/skill-tools.js.map +1 -1
  20. package/mcp-server/dist/tools/tool-groups.d.ts.map +1 -1
  21. package/mcp-server/dist/tools/tool-groups.js +19 -4
  22. package/mcp-server/dist/tools/tool-groups.js.map +1 -1
  23. package/mcp-server/package-lock.json +2 -2
  24. package/mcp-server/package.json +1 -1
  25. package/mcp-server/src/index.ts +6 -1
  26. package/mcp-server/src/services/tool-catalog.ts +1 -1
  27. package/mcp-server/src/tools/agents.ts +11 -5
  28. package/mcp-server/src/tools/skill-tools.ts +82 -9
  29. package/mcp-server/src/tools/tool-groups.ts +19 -4
  30. package/neural-interface/i18n/en.json +153 -10
  31. package/neural-interface/i18n/pt-BR.json +153 -10
  32. package/neural-interface/lib/antigravity/panel-bridge.js +7 -3
  33. package/neural-interface/lib/assistant-dispatch.js +15 -1
  34. package/neural-interface/lib/assistant-runtime.js +79 -8
  35. package/neural-interface/lib/assistant-user-context.js +7 -3
  36. package/neural-interface/lib/browser-background-tab.js +12 -0
  37. package/neural-interface/lib/browser-guard-cdp.js +2 -1
  38. package/neural-interface/lib/browser-network-guard.js +140 -3
  39. package/neural-interface/lib/browser-own-targets.js +350 -0
  40. package/neural-interface/lib/claude-agent-bridge.js +13 -2
  41. package/neural-interface/lib/codex-thread-history.js +144 -7
  42. package/neural-interface/lib/codex-worker-app-server.js +84 -30
  43. package/neural-interface/lib/db.js +26 -0
  44. package/neural-interface/lib/greeting/sources.js +7 -3
  45. package/neural-interface/lib/jsonl-lines.js +10 -11
  46. package/neural-interface/lib/launch-block-api.js +86 -0
  47. package/neural-interface/lib/line-buffer.js +88 -0
  48. package/neural-interface/lib/loop-stall.js +15 -1
  49. package/neural-interface/lib/memory-list-read.js +105 -0
  50. package/neural-interface/lib/memory-timers.js +4 -1
  51. package/neural-interface/lib/morelogin-launch-recovery.js +506 -0
  52. package/neural-interface/lib/morelogin-profile-failures.js +48 -1
  53. package/neural-interface/lib/morelogin.js +16 -2
  54. package/neural-interface/lib/native-loop-runtime.js +7 -3
  55. package/neural-interface/lib/provider-panel-api-key-auth.js +33 -6
  56. package/neural-interface/lib/skills/api-v2.js +18 -3
  57. package/neural-interface/lib/skills/bundle.js +2 -2
  58. package/neural-interface/lib/skills/deploy.js +169 -34
  59. package/neural-interface/lib/skills/digest.js +57 -3
  60. package/neural-interface/lib/skills/health.js +55 -1
  61. package/neural-interface/lib/skills/known-bridge-digests.json +12 -0
  62. package/neural-interface/lib/skills/runtime-checks.js +7 -4
  63. package/neural-interface/lib/skills/scan-shell.js +56 -7
  64. package/neural-interface/lib/skills/scan.js +13 -12
  65. package/neural-interface/lib/skills/service.js +7 -2
  66. package/neural-interface/lib/skills/telemetry.js +2 -2
  67. package/neural-interface/lib/teams/README.md +12 -5
  68. package/neural-interface/lib/teams/api.js +15 -4
  69. package/neural-interface/lib/teams/index.js +2 -0
  70. package/neural-interface/lib/teams/landing.js +195 -75
  71. package/neural-interface/lib/teams/scheduler.js +31 -32
  72. package/neural-interface/lib/terminal-token-redaction.js +228 -58
  73. package/neural-interface/lib/terminal-tokens.js +18 -0
  74. package/neural-interface/lib/whatsapp/brain.js +21 -0
  75. package/neural-interface/lib/whatsapp/bridge.js +40 -4
  76. package/neural-interface/package-lock.json +2 -2
  77. package/neural-interface/package.json +1 -1
  78. package/neural-interface/public/index.html +117 -37
  79. package/neural-interface/public/shared/agents/studio/ax-api.js +11 -0
  80. package/neural-interface/public/shared/agents/studio/ax-create-view.js +1 -1
  81. package/neural-interface/public/shared/agents/studio/ax-icons.js +1 -0
  82. package/neural-interface/public/shared/agents/studio/ax-library-view.js +3 -2
  83. package/neural-interface/public/shared/agents/studio/ax-main.js +127 -41
  84. package/neural-interface/public/shared/agents/studio/ax-review-view.js +3 -2
  85. package/neural-interface/public/shared/agents/studio/ax-teams-view.js +137 -39
  86. package/neural-interface/public/shared/agents/studio/ax-teams.js +219 -4
  87. package/neural-interface/public/shared/agents/studio/ax-ui.js +31 -0
  88. package/neural-interface/public/shared/agents/studio/ax-views.js +67 -69
  89. package/neural-interface/public/shared/agents/studio/ax-work-answer.js +86 -0
  90. package/neural-interface/public/shared/agents/studio/ax-work-view.js +305 -0
  91. package/neural-interface/public/shared/agents/studio/ax-work.css +74 -0
  92. package/neural-interface/public/shared/agents/studio/ax-work.js +300 -0
  93. package/neural-interface/public/shared/cdx/cdx-deferred.js +90 -0
  94. package/neural-interface/public/shared/cdx/cdx-render.js +18 -7
  95. package/neural-interface/public/shared/cdx/cdx-tabs.js +2 -1
  96. package/neural-interface/public/shared/launch-block-view.js +124 -0
  97. package/neural-interface/public/shared/loading-diagnostic-entry.js +16 -1
  98. package/neural-interface/public/shared/skills/studio/sx-operations-view.js +5 -0
  99. package/neural-interface/public/shared/skills/studio/sx-plan.js +42 -3
  100. package/neural-interface/public/shared/skills/studio/sx-publish-view.js +20 -4
  101. package/neural-interface/public/shared/styles.css +17 -0
  102. package/neural-interface/public/shared/ui-launch-block-notice.js +155 -0
  103. package/neural-interface/public/shared/ui-navbar.js +11 -0
  104. package/neural-interface/public/shared/ui-sync.js +2 -0
  105. package/neural-interface/server.js +745 -270
  106. package/npm-shrinkwrap.json +2 -2
  107. package/package.json +2 -1
  108. package/skills/skill-forge/SKILL.md +7 -3
@@ -11,12 +11,22 @@
11
11
  // when a fast-forward would do), and the target is moved to it. The merge is computed without a
12
12
  // checkout (`git merge-tree`); one that conflicts is refused, never resolved.
13
13
  //
14
+ // The target is the branch the caller names, or the team's recorded base branch (the branch the project was on when
15
+ // the team was created): never the branch that happens to be checked out when somebody lands.
16
+ //
14
17
  // A landing changes nothing and answers with a report when:
15
- // NOT_FAST_FORWARD mode 'ff' and the target has commits the integration branch lacks
16
- // MERGE_CONFLICT mode 'merge' and the two do not merge cleanly
18
+ // LANDING_PREVIEW_STALE the caller said where the two branches were when the landing was confirmed
19
+ // (`expectedTargetHead`, `expectedIntegrationHead`) and one of them is elsewhere now; the
20
+ // answer carries the landing as it stands (`preview`)
21
+ // OWNER_BRANCH_MOVED `unmovedOnly` (the scheduler asks it of an Assistant's landing) and the target is not at
22
+ // the commit the team was cut from
23
+ // TEAM_NOT_READY a slice is not joined yet (`allowPartial` lands what is joined)
24
+ // TARGET_NOT_FOUND the target is not a branch of this project
25
+ // NOTHING_TO_LAND the target already has everything on the integration branch (asked before the tests)
17
26
  // TESTS_NOT_PASSED the test command has not passed on the integration branch's head (`allowUntested` lands
18
27
  // a team that has no test command, or whose head was not tested, on the caller's word)
19
- // TEAM_NOT_READY a slice is not joined yet (`allowPartial` lands what is joined)
28
+ // NOT_FAST_FORWARD mode 'ff' and the target has commits the integration branch lacks
29
+ // MERGE_CONFLICT mode 'merge' and the two do not merge cleanly
20
30
  // DIRTY_FILES_AFFECTED the landing would change a file that has uncommitted changes in the checkout that has
21
31
  // the target branch (or would write over a file that is there and untracked or ignored)
22
32
  // CHECKOUT_BUSY that checkout is in the middle of a merge, a rebase, a cherry-pick, a revert or a bisect
@@ -24,6 +34,9 @@
24
34
  // TARGET_MOVED the target moved while the landing was prepared
25
35
  // and the report says what the user can run themselves (`userShouldRun`).
26
36
  //
37
+ // `preview(teamId)` answers what a landing sent now would be told, without moving anything. Both run the same
38
+ // checks (`admit`, then `decide`), so what the preview says and what the landing does cannot differ.
39
+ //
27
40
  // Where the target is checked out (the user's own checkout, usually), the branch, the index and the files are
28
41
  // updated together by a fast-forward of that checkout to the commit prepared above. git carries every uncommitted
29
42
  // change of a file the landing does not touch across unchanged, staged or not, and refuses before it changes
@@ -43,6 +56,12 @@ const exists = (path) => existsSync(path);
43
56
 
44
57
  export const LANDING_MODES = Object.freeze(['ff', 'merge']);
45
58
 
59
+ /** A head the caller expects (a full commit id), or null when it names none. */
60
+ const headGiven = (value) => (value === undefined || value === null || value === '' ? null : String(value).trim().toLowerCase());
61
+ /** The first sentence of a refusal's message: the reason a preview gives. */
62
+ const firstSentence = (text) => { const said = String(text || '').trim(); return (/^.*?[.!?](?=\s+[A-Z]|$)/s.exec(said) || [said])[0]; };
63
+ const short = (commit) => (commit ? String(commit).slice(0, 12) : 'no commit (it is not there)');
64
+
46
65
  /** Do two project-relative paths name the same file, or is one inside the other? */
47
66
  function touches(a, b) {
48
67
  const x = a.replace(/\/+$/, '');
@@ -62,97 +81,198 @@ function touches(a, b) {
62
81
  export function createTeamLanding({ state, git, locks, claims, identity = null, now = Date.now } = {}) {
63
82
  const isAncestor = async (cwd, older, newer) => (await git(['merge-base', '--is-ancestor', older, newer], { cwd })).ok;
64
83
 
84
+ /** The branch a landing goes to: the one the caller names, or the one the team was cut from. Never the branch checked out now. */
85
+ const targetOf = (record, targetBranch) => (typeof targetBranch === 'string' && targetBranch.trim() ? targetBranch.trim() : record?.baseBranch || null);
86
+
65
87
  /**
66
- * Bring a team's integration branch into a branch of the user's.
67
- *
68
- * @param {string} teamId
69
- * @param {object} [options]
70
- * @param {string} [options.targetBranch] default: the branch the project was on when the team was created
71
- * @param {'ff'|'merge'} [options.mode] default 'ff'
72
- * @param {boolean} [options.allowUntested] land although the tests have not passed on the integration head
73
- * @param {boolean} [options.allowPartial] land although some slices are not joined
74
- * @returns {Promise<{ ok: true, landed: object, changed: object[] } | { ok: false, code: string, message: string, report?: object }>}
88
+ * What a landing checks before it asks git where the two branches are: the team, the mode and the target's name.
89
+ * → a refusal, or `{ ok: true, target, cwd, source }`.
75
90
  */
76
- async function land(teamId, { targetBranch = null, mode = 'ff', allowUntested = false, allowPartial = false } = {}) {
77
- const record = isTeamSlug(teamId) ? await state.record(teamId) : null;
91
+ async function admit(teamId, record, { targetBranch = null, mode = 'ff' } = {}) {
78
92
  if (!record) return refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is recorded.`);
79
93
  if (record.plan.readOnly) return refuse('TEAM_READ_ONLY', 'Read-only teams never commit, integrate or land.');
80
94
  if (!LANDING_MODES.includes(mode)) return refuse('INVALID_MODE', 'mode is "ff" (move the branch forward) or "merge" (make a merge commit).');
81
95
  if (record.state === 'merged') return refuse('ALREADY_LANDED', `Team "${teamId}" has landed already (${record.landed?.targetBranch || 'its target'}).`, { landed: record.landed || null });
82
96
  if (['creating', 'integrating', 'landing'].includes(record.state)) return refuse('TEAM_NEEDS_RECOVERY', `Team "${teamId}" is recorded as ${record.state}: run recover() first.`, { state: record.state });
83
97
  if (['failed', 'cancelled'].includes(record.state) || record.removed) return refuse('TEAM_NOT_ACTIVE', `Team "${teamId}" is ${record.removed ? 'removed' : record.state}: there is nothing to land.`, { state: record.state });
84
- const target = typeof targetBranch === 'string' && targetBranch.trim() ? targetBranch.trim() : record.baseBranch;
98
+ const target = targetOf(record, targetBranch);
85
99
  if (!target) return refuse('TARGET_REQUIRED', 'The project was on no branch when the team was created: name the branch to land on (targetBranch).');
86
100
  if (parseBranchName(target)) return refuse('TARGET_IS_TEAM_BRANCH', `${target} is a team's own branch: a landing goes to a branch of yours.`);
87
101
  const cwd = record.projectPath;
88
102
  if (!isDirectory(cwd)) return refuse('PROJECT_NOT_FOUND', `${cwd} is not a folder.`);
89
103
  if (!(await git(['check-ref-format', '--branch', target], { cwd })).ok) return refuse('TARGET_NOT_FOUND', `"${target.slice(0, 120)}" is not a branch name.`);
90
- const source = integrationBranchName(teamId);
104
+ return { ok: true, target, cwd, source: integrationBranchName(teamId) };
105
+ }
91
106
 
92
- return locks.run(repoLockKey(cwd), () => locks.run(`team:${teamId}`, async () => {
93
- const head = await commitOf(git, cwd, `refs/heads/${source}`);
94
- if (!head) return refuse('INTEGRATION_BRANCH_MISSING', `${source} is not there. Run recover() first.`);
95
- if (head !== record.integration.head) return refuse('INTEGRATION_MOVED', `${source} is at ${head}, not at the commit SynaBun left it on (${record.integration.head}): somebody moved it.`, { head, recorded: record.integration.head });
96
-
97
- const open = record.plan.slices.map((slice) => ({ id: slice.id, state: sliceState(record, slice.id) })).filter((slice) => !['integrated', 'cancelled'].includes(slice.state));
98
- if (open.length && !allowPartial) return refuse('TEAM_NOT_READY', `Team "${teamId}" is not finished: ${open.map((slice) => `${slice.id} is ${slice.state}`).join(', ')}.`, { report: { teamId, slices: open } });
99
-
100
- const tests = record.integration.tests;
101
- const tested = !!tests && tests.ok === true && tests.commit === head;
102
- if (!tested && !allowUntested) {
103
- const why = !record.testCommand && !tests ? 'the team has no test command' : !tests || tests.commit !== head ? 'the tests have not been run on this commit' : 'the tests failed on this commit';
104
- return refuse('TESTS_NOT_PASSED', `${source} (${head.slice(0, 12)}) has not passed its tests: ${why}. Nothing was landed.`, { report: { teamId, integrationHead: head, tests: tests || null, testCommand: record.testCommand } });
105
- }
107
+ /** Where the integration branch and the target branch are now, as git has them (null: not there). */
108
+ const headsOf = async ({ cwd, source, target }) => ({
109
+ head: await commitOf(git, cwd, `refs/heads/${source}`),
110
+ targetHead: await commitOf(git, cwd, `refs/heads/${target}`),
111
+ });
106
112
 
107
- const targetHead = await commitOf(git, cwd, `refs/heads/${target}`);
108
- if (!targetHead) return refuse('TARGET_NOT_FOUND', `There is no branch named ${target} in this project.`);
109
- if (head === targetHead || await isAncestor(cwd, head, targetHead)) return refuse('NOTHING_TO_LAND', `${target} already has everything on ${source}.`, { report: { teamId, targetBranch: target, targetHead, integrationHead: head } });
113
+ /**
114
+ * Everything a landing checks once it knows where the two branches are, in the order it answers. It moves
115
+ * nothing (mode 'merge' writes the merge commit the target would be moved to: an object no branch has yet).
116
+ * → a refusal, or the landing as it would be done: `{ ok: true, head, targetHead, to, mergeCommit, changed,
117
+ * checkout, open, tested, byHand }`.
118
+ */
119
+ async function decide(teamId, record, { target, cwd, source }, { head, targetHead }, { mode = 'ff', allowUntested = false, allowPartial = false, unmovedOnly = false } = {}) {
120
+ // `unmovedOnly`: the caller lands only what the owner would wave through, so the target must be where the team
121
+ // was cut from. Moved since: the owner decides (a merge would mix their newer commits with the team's).
122
+ const cutFrom = record.baseCommit || null;
123
+ if (unmovedOnly && targetHead && cutFrom && targetHead !== cutFrom) {
124
+ return refuse('OWNER_BRANCH_MOVED', `${target} has moved since the team was cut from ${String(cutFrom).slice(0, 10)} (it is at ${targetHead.slice(0, 10)} now). Landing is the owner's to decide: ask them, do not land.`, {
125
+ status: 409, report: { targetBranch: target, cutFrom, head: targetHead, integrationBranch: source, userShouldRun: [`git checkout ${target}`, `git merge ${source}`] },
126
+ });
127
+ }
128
+ if (!head) return refuse('INTEGRATION_BRANCH_MISSING', `${source} is not there. Run recover() first.`);
129
+ const recorded = record.integration?.head || null;
130
+ if (head !== recorded) return refuse('INTEGRATION_MOVED', `${source} is at ${head}, not at the commit SynaBun left it on (${recorded}): somebody moved it.`, { head, recorded });
110
131
 
111
- const byHand = (how) => [`git checkout ${target}`, how === 'ff' ? `git merge --ff-only ${source}` : `git merge --no-ff ${source}`];
112
- const fastForward = await isAncestor(cwd, targetHead, head);
113
- if (mode === 'ff' && !fastForward) {
114
- const counts = /^(\d+)\s+(\d+)/.exec((await git(['rev-list', '--left-right', '--count', `${targetHead}...${head}`], { cwd })).stdout || '');
115
- return refuse('NOT_FAST_FORWARD', `${target} has moved since the team started: it has ${counts ? counts[1] : 'some'} commit(s) that ${source} does not have, so it cannot be moved forward to it. Land with mode "merge", or merge it yourself.`, {
116
- report: { teamId, targetBranch: target, targetHead, integrationHead: head, baseCommit: record.baseCommit, targetOnly: counts ? Number(counts[1]) : null, integrationOnly: counts ? Number(counts[2]) : null, userShouldRun: byHand('merge') },
117
- });
118
- }
132
+ const open = record.plan.slices.map((slice) => ({ id: slice.id, state: sliceState(record, slice.id) })).filter((slice) => !['integrated', 'cancelled'].includes(slice.state));
133
+ if (open.length && !allowPartial) return refuse('TEAM_NOT_READY', `Team "${teamId}" is not finished: ${open.map((slice) => `${slice.id} is ${slice.state}`).join(', ')}.`, { report: { teamId, slices: open } });
134
+
135
+ // Is there anything to land at all? Asked before the tests: joins that changed nothing leave nothing to test.
136
+ if (!targetHead) return refuse('TARGET_NOT_FOUND', `There is no branch named ${target} in this project.`);
137
+ if (head === targetHead || await isAncestor(cwd, head, targetHead)) return refuse('NOTHING_TO_LAND', `${target} already has everything on ${source}.`, { report: { teamId, targetBranch: target, targetHead, integrationHead: head } });
138
+
139
+ const tests = record.integration.tests;
140
+ const tested = !!tests && tests.ok === true && tests.commit === head;
141
+ if (!tested && !allowUntested) {
142
+ const why = !record.testCommand && !tests ? 'the team has no test command' : !tests || tests.commit !== head ? 'the tests have not been run on this commit' : 'the tests failed on this commit';
143
+ return refuse('TESTS_NOT_PASSED', `${source} (${head.slice(0, 12)}) has not passed its tests: ${why}. Nothing was landed.`, { report: { teamId, integrationHead: head, tests: tests || null, testCommand: record.testCommand } });
144
+ }
119
145
 
120
- // The commit the target is moved to: the integration head, or a merge commit made without a checkout.
121
- let to = head;
122
- let mergeCommit = null;
123
- if (mode === 'merge') {
124
- const merged = await git(['merge-tree', '--write-tree', '--name-only', '--no-messages', '-z', targetHead, head], { cwd });
125
- const parts = nulList(merged.stdout);
126
- if (merged.status === 1) return refuse('MERGE_CONFLICT', `${source} does not merge cleanly into ${target}: ${parts.slice(1, 6).join(', ')}${parts.length > 6 ? '…' : ''}. Nothing was landed and nothing was resolved.`, { report: { teamId, targetBranch: target, targetHead, integrationHead: head, files: parts.slice(1), userShouldRun: byHand('merge') } });
127
- if (!merged.ok || !/^[0-9a-f]{40,64}$/.test(parts[0] || '')) return refuse('MERGE_UNAVAILABLE', `git could not work the merge out without a checkout (${gitSaid(merged)}); it needs git 2.38 or newer. Nothing was landed.`, { report: { userShouldRun: byHand('merge') } });
128
- const made = await git(['commit-tree', parts[0], '-p', targetHead, '-p', head, '-m', `Merge team ${teamId} (${source}) into ${target}\n\nSynaBun-Team: ${teamId}`], { cwd, env: await identityEnv(git, cwd, identity) });
129
- mergeCommit = line(made);
130
- if (!made.ok || !mergeCommit) return refuse('MERGE_UNAVAILABLE', `The merge commit could not be made: ${gitSaid(made)}. Nothing was landed.`, { report: { userShouldRun: byHand('merge') } });
131
- to = mergeCommit;
146
+ const byHand = (how) => [`git checkout ${target}`, how === 'ff' ? `git merge --ff-only ${source}` : `git merge --no-ff ${source}`];
147
+ const fastForward = await isAncestor(cwd, targetHead, head);
148
+ if (mode === 'ff' && !fastForward) {
149
+ const counts = /^(\d+)\s+(\d+)/.exec((await git(['rev-list', '--left-right', '--count', `${targetHead}...${head}`], { cwd })).stdout || '');
150
+ return refuse('NOT_FAST_FORWARD', `${target} has moved since the team started: it has ${counts ? counts[1] : 'some'} commit(s) that ${source} does not have, so it cannot be moved forward to it. Land with mode "merge", or merge it yourself.`, {
151
+ report: { teamId, targetBranch: target, targetHead, integrationHead: head, baseCommit: record.baseCommit, targetOnly: counts ? Number(counts[1]) : null, integrationOnly: counts ? Number(counts[2]) : null, userShouldRun: byHand('merge') },
152
+ });
153
+ }
154
+
155
+ // The commit the target is moved to: the integration head, or a merge commit made without a checkout.
156
+ let to = head;
157
+ let mergeCommit = null;
158
+ if (mode === 'merge') {
159
+ const merged = await git(['merge-tree', '--write-tree', '--name-only', '--no-messages', '-z', targetHead, head], { cwd });
160
+ const parts = nulList(merged.stdout);
161
+ if (merged.status === 1) return refuse('MERGE_CONFLICT', `${source} does not merge cleanly into ${target}: ${parts.slice(1, 6).join(', ')}${parts.length > 6 ? '…' : ''}. Nothing was landed and nothing was resolved.`, { report: { teamId, targetBranch: target, targetHead, integrationHead: head, files: parts.slice(1), userShouldRun: byHand('merge') } });
162
+ if (!merged.ok || !/^[0-9a-f]{40,64}$/.test(parts[0] || '')) return refuse('MERGE_UNAVAILABLE', `git could not work the merge out without a checkout (${gitSaid(merged)}); it needs git 2.38 or newer. Nothing was landed.`, { report: { userShouldRun: byHand('merge') } });
163
+ const made = await git(['commit-tree', parts[0], '-p', targetHead, '-p', head, '-m', `Merge team ${teamId} (${source}) into ${target}\n\nSynaBun-Team: ${teamId}`], { cwd, env: await identityEnv(git, cwd, identity) });
164
+ mergeCommit = line(made);
165
+ if (!made.ok || !mergeCommit) return refuse('MERGE_UNAVAILABLE', `The merge commit could not be made: ${gitSaid(made)}. Nothing was landed.`, { report: { userShouldRun: byHand('merge') } });
166
+ to = mergeCommit;
167
+ }
168
+ const changed = parseNameStatus((await git(['diff', '--name-status', '-z', '--no-renames', targetHead, to], { cwd })).stdout);
169
+
170
+ // The checkout that has the target branch, if any: the landing must not disturb what is uncommitted there.
171
+ const checkout = (await listWorktrees(git, cwd)).find((entry) => entry.branch === target) || null;
172
+ if (checkout) {
173
+ const place = checkout.path;
174
+ if (!isDirectory(place)) return refuse('CHECKOUT_REFUSED', `${target} is registered as checked out in ${place}, which is not there. Run "git worktree prune", then land again.`, { report: { checkout: place, userShouldRun: ['git worktree prune'] } });
175
+ const busy = await operationsUnderWay(git, place, { exists });
176
+ if (busy.any) {
177
+ const what = busy.merge ? 'merge' : busy.rebase ? 'rebase' : busy.cherryPick ? 'cherry-pick' : busy.revert ? 'revert' : 'bisect';
178
+ return refuse('CHECKOUT_BUSY', `${place} is in the middle of a ${what}: a landing there cannot be guaranteed to leave it as it is. Finish or abort the ${what}, then land again or merge it yourself.`, { report: { checkout: place, operation: what, userShouldRun: byHand(mode) } });
179
+ }
180
+ const changes = await changesOf(git, place);
181
+ if (changes === null) return refuse('CHECKOUT_REFUSED', `The uncommitted changes of ${place} could not be read, so the landing cannot be guaranteed to leave them alone.`, { report: { checkout: place, userShouldRun: byHand(mode) } });
182
+ const dirty = [...new Set(changes.flatMap((change) => [change.path, change.from].filter(Boolean)))];
183
+ const affected = changed.filter((entry) => dirty.some((path) => touches(entry.path, path))).map((entry) => ({ path: entry.path, status: entry.status, why: 'uncommitted changes' }));
184
+ // A file the landing adds where one already is (untracked files are in `dirty`; an ignored one is not).
185
+ const covered = changed.filter((entry) => entry.status === 'A' && !affected.some((each) => each.path === entry.path) && present(join(place, entry.path))).map((entry) => ({ path: entry.path, status: entry.status, why: 'a file that is not tracked is already there' }));
186
+ if (affected.length || covered.length) {
187
+ const files = [...affected, ...covered];
188
+ return refuse('DIRTY_FILES_AFFECTED', `The landing would change ${files.length} file(s) that have uncommitted changes in ${place}: ${files.slice(0, 5).map((file) => file.path).join(', ')}${files.length > 5 ? '…' : ''}. Nothing was landed. Commit or put those changes aside yourself, then land again.`, { report: { teamId, checkout: place, targetBranch: target, files, userShouldRun: byHand(mode) } });
132
189
  }
133
- const changed = parseNameStatus((await git(['diff', '--name-status', '-z', '--no-renames', targetHead, to], { cwd })).stdout);
190
+ }
191
+ return { ok: true, head, targetHead, to, mergeCommit, changed, checkout, open, tested, byHand };
192
+ }
134
193
 
135
- // The checkout that has the target branch, if any: the landing must not disturb what is uncommitted there.
136
- const checkout = (await listWorktrees(git, cwd)).find((entry) => entry.branch === target) || null;
137
- if (checkout) {
138
- const place = checkout.path;
139
- if (!isDirectory(place)) return refuse('CHECKOUT_REFUSED', `${target} is registered as checked out in ${place}, which is not there. Run "git worktree prune", then land again.`, { report: { checkout: place, userShouldRun: ['git worktree prune'] } });
140
- const busy = await operationsUnderWay(git, place, { exists });
141
- if (busy.any) {
142
- const what = busy.merge ? 'merge' : busy.rebase ? 'rebase' : busy.cherryPick ? 'cherry-pick' : busy.revert ? 'revert' : 'bisect';
143
- return refuse('CHECKOUT_BUSY', `${place} is in the middle of a ${what}: a landing there cannot be guaranteed to leave it as it is. Finish or abort the ${what}, then land again or merge it yourself.`, { report: { checkout: place, operation: what, userShouldRun: byHand(mode) } });
144
- }
145
- const changes = await changesOf(git, place);
146
- if (changes === null) return refuse('CHECKOUT_REFUSED', `The uncommitted changes of ${place} could not be read, so the landing cannot be guaranteed to leave them alone.`, { report: { checkout: place, userShouldRun: byHand(mode) } });
147
- const dirty = [...new Set(changes.flatMap((change) => [change.path, change.from].filter(Boolean)))];
148
- const affected = changed.filter((entry) => dirty.some((path) => touches(entry.path, path))).map((entry) => ({ path: entry.path, status: entry.status, why: 'uncommitted changes' }));
149
- // A file the landing adds where one already is (untracked files are in `dirty`; an ignored one is not).
150
- const covered = changed.filter((entry) => entry.status === 'A' && !affected.some((each) => each.path === entry.path) && present(join(place, entry.path))).map((entry) => ({ path: entry.path, status: entry.status, why: 'a file that is not tracked is already there' }));
151
- if (affected.length || covered.length) {
152
- const files = [...affected, ...covered];
153
- return refuse('DIRTY_FILES_AFFECTED', `The landing would change ${files.length} file(s) that have uncommitted changes in ${place}: ${files.slice(0, 5).map((file) => file.path).join(', ')}${files.length > 5 ? '…' : ''}. Nothing was landed. Commit or put those changes aside yourself, then land again.`, { report: { teamId, checkout: place, targetBranch: target, files, userShouldRun: byHand(mode) } });
154
- }
194
+ /**
195
+ * The landing as it stands: what a landing sent now, with no override, would be told. `known`: the two heads,
196
+ * when the caller has read them already (the landing itself, under its lock).
197
+ * → `{ targetBranch, targetHead, integrationHead, testCommand, testsPassedOnHead, canLand, code, reason }`
198
+ */
199
+ async function previewOf(teamId, record, { targetBranch = null, mode = 'ff', unmovedOnly = false } = {}, known = null) {
200
+ const admitted = await admit(teamId, record, { targetBranch, mode });
201
+ const target = admitted.ok ? admitted.target : targetOf(record, targetBranch);
202
+ let heads = known;
203
+ if (!heads) {
204
+ // A landing that is refused early still says where the branches are, when they can be asked for at all.
205
+ const cwd = record?.projectPath || null;
206
+ const askable = !!cwd && isDirectory(cwd);
207
+ const named = askable && !!target && (admitted.ok || (await git(['check-ref-format', '--branch', target], { cwd })).ok);
208
+ heads = {
209
+ head: askable ? await commitOf(git, cwd, `refs/heads/${integrationBranchName(teamId)}`) : null,
210
+ targetHead: named ? await commitOf(git, cwd, `refs/heads/${target}`) : null,
211
+ };
212
+ }
213
+ const said = admitted.ok ? await decide(teamId, record, admitted, heads, { mode, unmovedOnly }) : admitted;
214
+ const tests = record?.integration?.tests || null;
215
+ return {
216
+ targetBranch: target, targetHead: heads.targetHead || null, integrationHead: heads.head || null,
217
+ testCommand: record?.testCommand || null,
218
+ testsPassedOnHead: !!tests && tests.ok === true && !!heads.head && tests.commit === heads.head,
219
+ canLand: said.ok === true, code: said.ok ? null : said.code, reason: said.ok ? null : firstSentence(said.message),
220
+ };
221
+ }
222
+
223
+ /**
224
+ * What a landing of this team would be told now, without moving anything: the same checks as `land`, with no
225
+ * override (`allowUntested`, `allowPartial`) and no lock taken.
226
+ *
227
+ * @param {string} teamId
228
+ * @param {object} [options]
229
+ * @param {string} [options.targetBranch] default: the team's recorded base branch
230
+ * @param {'ff'|'merge'} [options.mode] default 'ff' (a preview of 'merge' writes the merge commit object)
231
+ * @param {boolean} [options.unmovedOnly] as for `land`
232
+ * @returns {Promise<{ targetBranch: string|null, targetHead: string|null, integrationHead: string|null, testCommand: string|null, testsPassedOnHead: boolean, canLand: boolean, code: string|null, reason: string|null }>}
233
+ */
234
+ async function preview(teamId, { targetBranch = null, mode = 'ff', unmovedOnly = false } = {}) {
235
+ const record = isTeamSlug(teamId) ? await state.record(teamId) : null;
236
+ return previewOf(teamId, record, { targetBranch, mode, unmovedOnly });
237
+ }
238
+
239
+ /**
240
+ * Bring a team's integration branch into a branch of the user's.
241
+ *
242
+ * @param {string} teamId
243
+ * @param {object} [options]
244
+ * @param {string} [options.targetBranch] default: the branch the project was on when the team was created
245
+ * @param {'ff'|'merge'} [options.mode] default 'ff'
246
+ * @param {boolean} [options.allowUntested] land although the tests have not passed on the integration head
247
+ * @param {boolean} [options.allowPartial] land although some slices are not joined
248
+ * @param {boolean} [options.unmovedOnly] land only while the target is at the commit the team was cut from
249
+ * @param {string} [options.expectedTargetHead] the target's tip when the landing was confirmed (full commit id)
250
+ * @param {string} [options.expectedIntegrationHead] the integration head when the landing was confirmed
251
+ * @returns {Promise<{ ok: true, landed: object, changed: object[] } | { ok: false, code: string, message: string, report?: object, preview?: object }>}
252
+ */
253
+ async function land(teamId, { targetBranch = null, mode = 'ff', allowUntested = false, allowPartial = false, unmovedOnly = false, expectedTargetHead = null, expectedIntegrationHead = null } = {}) {
254
+ const record = isTeamSlug(teamId) ? await state.record(teamId) : null;
255
+ const admitted = await admit(teamId, record, { targetBranch, mode });
256
+ if (!admitted.ok) return admitted;
257
+ const { target, cwd, source } = admitted;
258
+ const expected = { target: headGiven(expectedTargetHead), integration: headGiven(expectedIntegrationHead) };
259
+
260
+ return locks.run(repoLockKey(cwd), () => locks.run(`team:${teamId}`, async () => {
261
+ const heads = await headsOf(admitted);
262
+ // What was confirmed must be what is there: asked first, under the lock, of git itself.
263
+ const elsewhere = [
264
+ expected.target && expected.target !== heads.targetHead ? `${target} is at ${short(heads.targetHead)}, not at ${short(expected.target)}` : null,
265
+ expected.integration && expected.integration !== heads.head ? `${source} is at ${short(heads.head)}, not at ${short(expected.integration)}` : null,
266
+ ].filter(Boolean);
267
+ if (elsewhere.length) {
268
+ return refuse('LANDING_PREVIEW_STALE', `What was confirmed is not what is there any more: ${elsewhere.join('; ')}. Nothing was landed. Read the landing again and confirm it.`, {
269
+ preview: await previewOf(teamId, record, { targetBranch: target, mode, unmovedOnly }, heads),
270
+ report: { teamId, targetBranch: target, expectedTargetHead: expected.target, targetHead: heads.targetHead || null, expectedIntegrationHead: expected.integration, integrationHead: heads.head || null },
271
+ });
155
272
  }
273
+ const decided = await decide(teamId, record, admitted, heads, { mode, allowUntested, allowPartial, unmovedOnly });
274
+ if (!decided.ok) return decided;
275
+ const { head, targetHead, to, mergeCommit, changed, checkout, open, tested, byHand } = decided;
156
276
 
157
277
  const previous = record.state;
158
278
  const landing = { targetBranch: target, mode, from: targetHead, to, integrationHead: head, mergeCommit, checkout: checkout?.path || null, startedAt: now() };
@@ -180,5 +300,5 @@ export function createTeamLanding({ state, git, locks, claims, identity = null,
180
300
  }));
181
301
  }
182
302
 
183
- return { land };
303
+ return { land, preview };
184
304
  }
@@ -12,8 +12,11 @@
12
12
  // dependency order and runs the tests on exactly that commit; a conflict or failed tests go back to the
13
13
  // slice's worker as a follow-up with the report, a bounded number of times; then the team needs
14
14
  // attention and the Assistant is told
15
- // land the integration branch into the owner's branch: the owner's step, or the Assistant's when the tests
16
- // passed, the owner's branch has not moved and no locally changed file is touched
15
+ // land the integration branch into the owner's branch (the one named, or the team's recorded base branch;
16
+ // never the branch that happens to be checked out): the owner's step, or the Assistant's when the
17
+ // tests passed, the owner's branch has not moved and no locally changed file is touched. `status`
18
+ // carries `landing.preview`, what a landing sent now would be told, and a landing that names the
19
+ // heads it was confirmed for is refused when they are elsewhere (LANDING_PREVIEW_STALE)
17
20
  // cleanup after a landing, the team's worktrees and branches, once every one is merged and clean (the owner's
18
21
  // word: from Agents Studio, or the Assistant after the owner said yes in its session)
19
22
  // discard stop the workers, give the work up (what holds work is kept unless the owner says `force`); the
@@ -371,9 +374,10 @@ export function createTeamScheduler({
371
374
  /**
372
375
  * A team in full: the plan, every slice with its state, run, worktree, branch, owned paths and how far it is
373
376
  * from the base and the integration branch, the claims, the integration branch and its last test result, the
374
- * landing, and the end of the team's timeline (events and messages).
377
+ * landing, and the end of the team's timeline (events and messages). `landing.preview` is what a landing sent now
378
+ * by `by` (default: the owner), with no target named and no override, would be told: the library's own checks.
375
379
  */
376
- async function status(teamId, { timeline = limits.timeline } = {}) {
380
+ async function status(teamId, { timeline = limits.timeline, by = { kind: 'owner' } } = {}) {
377
381
  const record = records.get(String(teamId || ''));
378
382
  if (!record) return refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`, { status: 404 });
379
383
  let journal = await journalOf(record.teamId);
@@ -407,6 +411,11 @@ export function createTeamScheduler({
407
411
  // A ready team whose work is on its base branch already (merged outside land): where, and whether it is clean.
408
412
  const alreadyOnTarget = await onTargetOf(record, { full: true });
409
413
  const tests = journal?.integration?.tests || null;
414
+ // The landing as it stands: the branch that would move, both heads, and whether a landing would be accepted.
415
+ let preview = null;
416
+ if (typeof teams.landingPreview === 'function') {
417
+ try { preview = await teams.landingPreview(record.teamId, { unmovedOnly: by?.kind !== 'owner' }); } catch (error) { preview = null; say('teams:landing-preview-failed', { teamId: record.teamId, error: error?.message || String(error) }); }
418
+ }
410
419
  const events = (await tailOfLog(record.teamId, TEAM_EVENT_LOG_KINDS, timeline)).map(eventView);
411
420
  const messages = (await tailOfLog(record.teamId, [TEAM_MESSAGE_LOG_KIND], timeline)).map(messageView);
412
421
  return {
@@ -434,6 +443,7 @@ export function createTeamScheduler({
434
443
  state: journal?.state === 'merged' ? 'landed' : (record.landing?.refused ? 'refused' : 'none'),
435
444
  landed: journal?.landed || null, refused: record.landing?.refused || null,
436
445
  target: seen?.target || null, cleanup: record.cleanup || null,
446
+ preview,
437
447
  },
438
448
  events,
439
449
  messages,
@@ -460,13 +470,6 @@ export function createTeamScheduler({
460
470
  return out.ok ? String(out.stdout || '').trim() || null : null;
461
471
  } catch { return null; }
462
472
  }
463
- async function branchOf(projectPath) {
464
- if (typeof git !== 'function') return null;
465
- try {
466
- const out = await git(['symbolic-ref', '--short', '-q', 'HEAD'], { cwd: projectPath });
467
- return out.ok ? String(out.stdout || '').trim() || null : null;
468
- } catch { return null; }
469
- }
470
473
 
471
474
  /**
472
475
  * A plan as it would start: normalized, with a team id and the pinned base commit when it names none, checked
@@ -1099,7 +1102,7 @@ export function createTeamScheduler({
1099
1102
  * journal has it. A finished slice is joined; one in conflict or with failed tests goes back to a worker with a
1100
1103
  * fresh count; one whose worker ended gets a new one.
1101
1104
  */
1102
- function integrate(teamId, { sliceId = null } = {}) {
1105
+ function integrate(teamId, { sliceId = null, by = { kind: 'owner' } } = {}) {
1103
1106
  const record = records.get(String(teamId || ''));
1104
1107
  if (!record) return Promise.resolve(refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`, { status: 404 }));
1105
1108
  if (record.plan.readOnly) return Promise.resolve(refuse('TEAM_READ_ONLY', 'Research/review teams have no integration or landing.', { status: 409 }));
@@ -1142,16 +1145,20 @@ export function createTeamScheduler({
1142
1145
  });
1143
1146
  if (OPEN_STATUSES.has(record.status)) await joinFinished(record);
1144
1147
  save(record);
1145
- return status(record.teamId, { timeline: 30 });
1148
+ return status(record.teamId, { timeline: 30, by });
1146
1149
  });
1147
1150
  }
1148
1151
 
1149
1152
  /**
1150
- * Land a team: the integration branch into the owner's branch (default: the branch checked out in the project).
1151
- * The library's refusals come back unchanged. `by.kind` 'assistant': never untested, never partial, and only
1152
- * while the owner's branch is where it was when the team was cut.
1153
+ * Land a team: the integration branch into the owner's branch. The target is the branch the caller names, or
1154
+ * the team's recorded base branch (the library's own default): never the branch that happens to be checked out
1155
+ * in the project when somebody lands. `expectedTargetHead` / `expectedIntegrationHead`: where the two branches
1156
+ * were when the landing was confirmed (the `landing.preview` of `status`); elsewhere now, nothing moves and the
1157
+ * answer is LANDING_PREVIEW_STALE with the fresh `preview`. The library's refusals come back unchanged.
1158
+ * `by.kind` 'assistant': never untested, never partial, and only while the owner's branch is where it was when
1159
+ * the team was cut (OWNER_BRANCH_MOVED, the library's `unmovedOnly`).
1153
1160
  */
1154
- function land(teamId, { targetBranch = null, mode = 'ff', by = { kind: 'owner' }, allowUntested = false, allowPartial = false } = {}) {
1161
+ function land(teamId, { targetBranch = null, mode = 'ff', by = { kind: 'owner' }, allowUntested = false, allowPartial = false, expectedTargetHead = null, expectedIntegrationHead = null } = {}) {
1155
1162
  const record = records.get(String(teamId || ''));
1156
1163
  if (!record) return Promise.resolve(refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`, { status: 404 }));
1157
1164
  if (record.plan.readOnly) return Promise.resolve(refuse('TEAM_READ_ONLY', 'Research/review teams have no integration or landing.', { status: 409 }));
@@ -1159,24 +1166,16 @@ export function createTeamScheduler({
1159
1166
  const journal = await journalOf(record.teamId);
1160
1167
  if (!journal) return refuse('TEAM_NOT_FOUND', `Team "${teamId}" has no record in the journal.`, { status: 404 });
1161
1168
  const assistant = by?.kind !== 'owner';
1162
- const target = targetBranch ? String(targetBranch) : ((await branchOf(record.projectPath)) || journal.baseBranch || null);
1163
- if (assistant) {
1164
- // The Assistant lands only what the owner would wave through: the owner's branch must be where the team
1165
- // was cut from. Moved since: the owner decides (a merge would mix their newer commits with the team's).
1166
- const head = target && typeof git === 'function' ? await (async () => { const out = await git(['rev-parse', '--verify', '-q', `refs/heads/${target}^{commit}`], { cwd: record.projectPath }); return out.ok ? String(out.stdout || '').trim() : null; })() : null;
1167
- const cutFrom = journal.baseCommit || null;
1168
- if (head && cutFrom && head !== cutFrom && journal.state !== 'merged') {
1169
- const refused = refuse('OWNER_BRANCH_MOVED', `${target} has moved since the team was cut from ${String(cutFrom).slice(0, 10)} (it is at ${head.slice(0, 10)} now). Landing is the owner's to decide: ask them, do not land.`, {
1170
- status: 409, report: { targetBranch: target, cutFrom, head, integrationBranch: integrationBranchName(record.teamId), userShouldRun: [`git checkout ${target}`, `git merge ${integrationBranchName(record.teamId)}`] },
1171
- });
1172
- record.landing = { refused: { code: refused.code, message: refused.message, at: iso(now()), by: by?.kind || 'assistant' } };
1173
- save(record);
1174
- return refused;
1175
- }
1176
- }
1169
+ // A target that was named is the target, whatever it is (one that is no branch is the library's to refuse);
1170
+ // only a landing that names none goes to the recorded base branch.
1171
+ const named = targetBranch === null || targetBranch === undefined ? '' : String(targetBranch).trim();
1172
+ const target = named || journal.baseBranch || null;
1177
1173
  const landed = await teams.land(record.teamId, {
1178
1174
  ...(target ? { targetBranch: target } : {}), mode,
1179
1175
  allowUntested: !assistant && allowUntested === true, allowPartial: !assistant && allowPartial === true,
1176
+ // The Assistant lands only what the owner would wave through: the owner's branch where the team was cut.
1177
+ unmovedOnly: assistant,
1178
+ expectedTargetHead, expectedIntegrationHead,
1180
1179
  });
1181
1180
  if (!landed.ok) {
1182
1181
  record.landing = { refused: { code: landed.code, message: clip(landed.message, 600), at: iso(now()), by: by?.kind || 'owner' } };