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
|
@@ -12,9 +12,18 @@
|
|
|
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
|
|
16
|
-
//
|
|
17
|
-
//
|
|
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)
|
|
20
|
+
// cleanup after a landing, the team's worktrees and branches, once every one is merged and clean (the owner's
|
|
21
|
+
// word: from Agents Studio, or the Assistant after the owner said yes in its session)
|
|
22
|
+
// discard stop the workers, give the work up (what holds work is kept unless the owner says `force`); the
|
|
23
|
+
// owner's Discard & remove forgets the team in the same step when nothing of it is left
|
|
24
|
+
// remove forget a closed team (its record here), only once nothing of it is left on disk or in git
|
|
25
|
+
// cleanupMerged a ready team whose work somebody merged into its base branch outside `land`: checked again
|
|
26
|
+
// against git, its landing recorded, its worktrees, branches and record removed (the owner's)
|
|
18
27
|
//
|
|
19
28
|
// Agent density targets the selected worker count; fewer requires a reason. Solo starts no
|
|
20
29
|
// team; workers never work in the owner's checkout or on the owner's branch; the owner's branch receives the work
|
|
@@ -30,8 +39,8 @@
|
|
|
30
39
|
|
|
31
40
|
import { markActivity } from '../loop-stall.js';
|
|
32
41
|
import { createHash, randomBytes } from 'node:crypto';
|
|
33
|
-
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
34
|
-
import { join } from 'node:path';
|
|
42
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
43
|
+
import { dirname, join } from 'node:path';
|
|
35
44
|
import { AGENT_DENSITY_LABELS, DEFAULT_AGENT_DENSITY, maxWorkers, normalizeAgentDensity } from '../agents/density.js';
|
|
36
45
|
import { splitTitle } from '../agents/split.js';
|
|
37
46
|
import { sessionRecipient } from '../coordination/store.js';
|
|
@@ -40,8 +49,9 @@ import {
|
|
|
40
49
|
unresolvedFollowUp,
|
|
41
50
|
} from './brief.js';
|
|
42
51
|
import {
|
|
43
|
-
worktreePath, branchName, integrationBranchName, isTeamSlug, normalizeRelPath, normalizeTeamPlan, pathOwner, validateTeamPlan,
|
|
52
|
+
BRANCH_PREFIX, INTEGRATION_SLICE_ID, worktreePath, branchName, integrationBranchName, isTeamSlug, normalizeRelPath, normalizeTeamPlan, pathOwner, validateTeamPlan,
|
|
44
53
|
} from './schema.js';
|
|
54
|
+
import { TERMINAL_TEAM_STATES } from './state.js';
|
|
45
55
|
import { TEAM_TEST_TIMEOUT_MS } from './test-runner.js';
|
|
46
56
|
import { testCommandOf } from './worktrees.js';
|
|
47
57
|
|
|
@@ -72,6 +82,11 @@ export const TEAM_CHANGED = 'assistant:team-changed';
|
|
|
72
82
|
/** A team as the scheduler sees it (its own status; the journal's state is finer). */
|
|
73
83
|
export const TEAM_STATUSES = Object.freeze(['running', 'needs_attention', 'ready', 'completed', 'landed', 'discarded', 'failed']);
|
|
74
84
|
const OPEN_STATUSES = new Set(['running', 'needs_attention', 'ready']);
|
|
85
|
+
/** A team that is over: nothing of it runs again. Only such a team can be removed. */
|
|
86
|
+
export const CLOSED_TEAM_STATUSES = Object.freeze(['landed', 'completed', 'discarded', 'failed']);
|
|
87
|
+
const CLOSED_STATUSES = new Set(CLOSED_TEAM_STATUSES);
|
|
88
|
+
/** The durable log's kind of a removal (not a TEAM_EVENTS entry: nobody's mailbox gets it, no page reads a removed team). */
|
|
89
|
+
export const TEAM_REMOVED_LOG_KIND = 'team.removed';
|
|
75
90
|
|
|
76
91
|
export const TEAM_SCHEDULER_LIMITS = Object.freeze({
|
|
77
92
|
/** How often a slice is sent back to its worker for one kind of trouble before the team needs attention. */
|
|
@@ -178,6 +193,9 @@ export function createTeamScheduler({
|
|
|
178
193
|
const runIndex = new Map(); // runId → { teamId, sliceId } (every run a slice ever had)
|
|
179
194
|
const chains = new Map(); // team or slice key → last scheduled operation
|
|
180
195
|
const postTimes = new Map(); // runId → the times of its recent posts
|
|
196
|
+
// Teams removed while the server runs. A step that was handed a team's record before it was removed must never
|
|
197
|
+
// write that record back (save) or announce it (emit): its id is never made again (the journal keeps it).
|
|
198
|
+
const removedTeams = new Set();
|
|
181
199
|
let unsubscribe = null;
|
|
182
200
|
let watchdog = null;
|
|
183
201
|
let watchdogPass = null;
|
|
@@ -200,6 +218,7 @@ export function createTeamScheduler({
|
|
|
200
218
|
// ── the scheduler's own record ─────────────────────────────────────────────
|
|
201
219
|
const fileOf = (teamId) => join(dir, `${teamId}.json`);
|
|
202
220
|
function save(record) {
|
|
221
|
+
if (removedTeams.has(record.teamId)) return record;
|
|
203
222
|
record.updatedAt = iso(now());
|
|
204
223
|
records.set(record.teamId, record);
|
|
205
224
|
try {
|
|
@@ -250,7 +269,9 @@ export function createTeamScheduler({
|
|
|
250
269
|
const inTurn = (teamId, fn) => enqueue([teamId], fn);
|
|
251
270
|
const inSlice = (record, sliceId, fn) => enqueue([`${record.teamId}/${sliceId}`], fn);
|
|
252
271
|
const withAllSlices = (record, fn) => enqueue(Object.keys(record.slices).map(id => `${record.teamId}/${id}`), fn);
|
|
253
|
-
const
|
|
272
|
+
const gone = (teamId) => refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here (it was removed).`, { status: 404 });
|
|
273
|
+
// A step queued before the team was removed finds it gone and changes nothing.
|
|
274
|
+
const teamStep = (record, fn) => inTurn(record.teamId, () => withAllSlices(record, () => (removedTeams.has(record.teamId) ? gone(record.teamId) : fn())));
|
|
254
275
|
/** Resolves once every team's queued steps have run (tests, shutdown). */
|
|
255
276
|
async function settled() {
|
|
256
277
|
while (chains.size) {
|
|
@@ -265,7 +286,7 @@ export function createTeamScheduler({
|
|
|
265
286
|
*/
|
|
266
287
|
async function emit(record, name, detail = {}, { critical = false, mailbox = null, quiet = false } = {}) {
|
|
267
288
|
const spec = TEAM_EVENTS[name];
|
|
268
|
-
if (!spec) return null;
|
|
289
|
+
if (!spec || removedTeams.has(record.teamId)) return null;
|
|
269
290
|
record.eventSeq = (Number(record.eventSeq) || 0) + 1;
|
|
270
291
|
const told = (mailbox === null ? spec.mailbox : mailbox) && !!record.sessionId && !quiet;
|
|
271
292
|
const payload = {
|
|
@@ -353,9 +374,10 @@ export function createTeamScheduler({
|
|
|
353
374
|
/**
|
|
354
375
|
* A team in full: the plan, every slice with its state, run, worktree, branch, owned paths and how far it is
|
|
355
376
|
* from the base and the integration branch, the claims, the integration branch and its last test result, the
|
|
356
|
-
* 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.
|
|
357
379
|
*/
|
|
358
|
-
async function status(teamId, { timeline = limits.timeline } = {}) {
|
|
380
|
+
async function status(teamId, { timeline = limits.timeline, by = { kind: 'owner' } } = {}) {
|
|
359
381
|
const record = records.get(String(teamId || ''));
|
|
360
382
|
if (!record) return refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`, { status: 404 });
|
|
361
383
|
let journal = await journalOf(record.teamId);
|
|
@@ -384,7 +406,16 @@ export function createTeamScheduler({
|
|
|
384
406
|
});
|
|
385
407
|
let claims = [];
|
|
386
408
|
try { claims = (await store.claimsFor({ teamId: record.teamId })).map((claim) => ({ sliceId: claim.sliceId, runId: claim.runId || null, glob: claim.glob, claimedAt: claim.claimedAt || claim.createdAt || null })); } catch { claims = []; }
|
|
409
|
+
// A closed team: what is still on disk and in git, and whether its record can go (the page's Clean up / Remove).
|
|
410
|
+
const removal = CLOSED_STATUSES.has(record.status) ? removalView(await removalOf(record, journal)) : null;
|
|
411
|
+
// A ready team whose work is on its base branch already (merged outside land): where, and whether it is clean.
|
|
412
|
+
const alreadyOnTarget = await onTargetOf(record, { full: true });
|
|
387
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
|
+
}
|
|
388
419
|
const events = (await tailOfLog(record.teamId, TEAM_EVENT_LOG_KINDS, timeline)).map(eventView);
|
|
389
420
|
const messages = (await tailOfLog(record.teamId, [TEAM_MESSAGE_LOG_KIND], timeline)).map(messageView);
|
|
390
421
|
return {
|
|
@@ -394,6 +425,8 @@ export function createTeamScheduler({
|
|
|
394
425
|
baseCommit: journal?.baseCommit || plan.baseCommit || null, baseBranch: journal?.baseBranch || null,
|
|
395
426
|
route: record.routeId ? { routeId: record.routeId, taskClass: record.taskClass || null } : null,
|
|
396
427
|
cap: maxWorkers(record.density),
|
|
428
|
+
removal,
|
|
429
|
+
alreadyOnTarget,
|
|
397
430
|
},
|
|
398
431
|
plan,
|
|
399
432
|
slices,
|
|
@@ -410,6 +443,7 @@ export function createTeamScheduler({
|
|
|
410
443
|
state: journal?.state === 'merged' ? 'landed' : (record.landing?.refused ? 'refused' : 'none'),
|
|
411
444
|
landed: journal?.landed || null, refused: record.landing?.refused || null,
|
|
412
445
|
target: seen?.target || null, cleanup: record.cleanup || null,
|
|
446
|
+
preview,
|
|
413
447
|
},
|
|
414
448
|
events,
|
|
415
449
|
messages,
|
|
@@ -436,13 +470,6 @@ export function createTeamScheduler({
|
|
|
436
470
|
return out.ok ? String(out.stdout || '').trim() || null : null;
|
|
437
471
|
} catch { return null; }
|
|
438
472
|
}
|
|
439
|
-
async function branchOf(projectPath) {
|
|
440
|
-
if (typeof git !== 'function') return null;
|
|
441
|
-
try {
|
|
442
|
-
const out = await git(['symbolic-ref', '--short', '-q', 'HEAD'], { cwd: projectPath });
|
|
443
|
-
return out.ok ? String(out.stdout || '').trim() || null : null;
|
|
444
|
-
} catch { return null; }
|
|
445
|
-
}
|
|
446
473
|
|
|
447
474
|
/**
|
|
448
475
|
* A plan as it would start: normalized, with a team id and the pinned base commit when it names none, checked
|
|
@@ -1075,7 +1102,7 @@ export function createTeamScheduler({
|
|
|
1075
1102
|
* journal has it. A finished slice is joined; one in conflict or with failed tests goes back to a worker with a
|
|
1076
1103
|
* fresh count; one whose worker ended gets a new one.
|
|
1077
1104
|
*/
|
|
1078
|
-
function integrate(teamId, { sliceId = null } = {}) {
|
|
1105
|
+
function integrate(teamId, { sliceId = null, by = { kind: 'owner' } } = {}) {
|
|
1079
1106
|
const record = records.get(String(teamId || ''));
|
|
1080
1107
|
if (!record) return Promise.resolve(refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`, { status: 404 }));
|
|
1081
1108
|
if (record.plan.readOnly) return Promise.resolve(refuse('TEAM_READ_ONLY', 'Research/review teams have no integration or landing.', { status: 409 }));
|
|
@@ -1118,16 +1145,20 @@ export function createTeamScheduler({
|
|
|
1118
1145
|
});
|
|
1119
1146
|
if (OPEN_STATUSES.has(record.status)) await joinFinished(record);
|
|
1120
1147
|
save(record);
|
|
1121
|
-
return status(record.teamId, { timeline: 30 });
|
|
1148
|
+
return status(record.teamId, { timeline: 30, by });
|
|
1122
1149
|
});
|
|
1123
1150
|
}
|
|
1124
1151
|
|
|
1125
1152
|
/**
|
|
1126
|
-
* Land a team: the integration branch into the owner's branch
|
|
1127
|
-
*
|
|
1128
|
-
*
|
|
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`).
|
|
1129
1160
|
*/
|
|
1130
|
-
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 } = {}) {
|
|
1131
1162
|
const record = records.get(String(teamId || ''));
|
|
1132
1163
|
if (!record) return Promise.resolve(refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`, { status: 404 }));
|
|
1133
1164
|
if (record.plan.readOnly) return Promise.resolve(refuse('TEAM_READ_ONLY', 'Research/review teams have no integration or landing.', { status: 409 }));
|
|
@@ -1135,24 +1166,16 @@ export function createTeamScheduler({
|
|
|
1135
1166
|
const journal = await journalOf(record.teamId);
|
|
1136
1167
|
if (!journal) return refuse('TEAM_NOT_FOUND', `Team "${teamId}" has no record in the journal.`, { status: 404 });
|
|
1137
1168
|
const assistant = by?.kind !== 'owner';
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
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;
|
|
1143
|
-
const cutFrom = journal.baseCommit || null;
|
|
1144
|
-
if (head && cutFrom && head !== cutFrom && journal.state !== 'merged') {
|
|
1145
|
-
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.`, {
|
|
1146
|
-
status: 409, report: { targetBranch: target, cutFrom, head, integrationBranch: integrationBranchName(record.teamId), userShouldRun: [`git checkout ${target}`, `git merge ${integrationBranchName(record.teamId)}`] },
|
|
1147
|
-
});
|
|
1148
|
-
record.landing = { refused: { code: refused.code, message: refused.message, at: iso(now()), by: by?.kind || 'assistant' } };
|
|
1149
|
-
save(record);
|
|
1150
|
-
return refused;
|
|
1151
|
-
}
|
|
1152
|
-
}
|
|
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;
|
|
1153
1173
|
const landed = await teams.land(record.teamId, {
|
|
1154
1174
|
...(target ? { targetBranch: target } : {}), mode,
|
|
1155
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,
|
|
1156
1179
|
});
|
|
1157
1180
|
if (!landed.ok) {
|
|
1158
1181
|
record.landing = { refused: { code: landed.code, message: clip(landed.message, 600), at: iso(now()), by: by?.kind || 'owner' } };
|
|
@@ -1176,18 +1199,26 @@ export function createTeamScheduler({
|
|
|
1176
1199
|
});
|
|
1177
1200
|
}
|
|
1178
1201
|
|
|
1179
|
-
|
|
1202
|
+
/**
|
|
1203
|
+
* Remove a landed team's worktrees and branches, all or nothing: confirmed, landed, every run ended (the final
|
|
1204
|
+
* worker of each slice done), every branch merged into the target, every worktree clean. `by.kind` 'owner' is
|
|
1205
|
+
* the owner's own page (Agents Studio, whose route only the owner reaches): its confirmation is the owner's
|
|
1206
|
+
* word. Anybody else (the Assistant's agent_team) needs, besides `userConfirmed`, an owner UI turn in the team's
|
|
1207
|
+
* session after the landing, so a worktree is never deleted on the model's word alone.
|
|
1208
|
+
*/
|
|
1209
|
+
function cleanup(teamId, { sessionId = null, userConfirmed = false, by = null } = {}) {
|
|
1180
1210
|
const record = records.get(String(teamId || ''));
|
|
1181
1211
|
if (!record) return Promise.resolve(refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`));
|
|
1182
1212
|
return teamStep(record, async () => {
|
|
1183
1213
|
if (sessionId && sessionId !== record.sessionId) return refuse('TEAM_FORBIDDEN', 'That team belongs to another Assistant session.');
|
|
1184
1214
|
if (record.cleanedAt) return { ok: true, teamId: record.teamId, alreadyClean: true, removed: [] };
|
|
1185
|
-
const
|
|
1215
|
+
const owner = by?.kind === 'owner';
|
|
1216
|
+
const verified = !owner && typeof lastOwnerTurnAt === 'function';
|
|
1186
1217
|
let ownerAt = null;
|
|
1187
1218
|
if (verified) { try { ownerAt = await lastOwnerTurnAt(record.sessionId); } catch {} }
|
|
1188
1219
|
const timestamp = value => typeof value === 'number' ? value : Date.parse(value);
|
|
1189
|
-
const confirmed = userConfirmed === true && (!verified || timestamp(ownerAt) > timestamp(record.landing?.at));
|
|
1190
|
-
const checks = [{ name: 'confirmed', ok: confirmed, detail: verified ? 'Requires explicit confirmation and an owner UI turn after landing.' : 'Owner-turn timing unverified; explicit confirmation required.' },
|
|
1220
|
+
const confirmed = userConfirmed === true && (owner || !verified || timestamp(ownerAt) > timestamp(record.landing?.at));
|
|
1221
|
+
const checks = [{ name: 'confirmed', ok: confirmed, detail: owner ? 'Requires the owner\'s explicit confirmation from SynaBun\'s own page.' : verified ? 'Requires explicit confirmation and an owner UI turn after landing.' : 'Owner-turn timing unverified; explicit confirmation required.' },
|
|
1191
1222
|
{ name: 'landed', ok: record.status === 'landed', detail: `Team status: ${record.status}` }];
|
|
1192
1223
|
const dispatcher = dispatcherOf();
|
|
1193
1224
|
const finalIds = new Set();
|
|
@@ -1227,9 +1258,9 @@ export function createTeamScheduler({
|
|
|
1227
1258
|
const out = await teams.remove(record.teamId, { guarded: true });
|
|
1228
1259
|
if (!out.ok) return { ...out, checks: [...checks, ...(out.checks || [])] };
|
|
1229
1260
|
record.cleanedAt = iso(now());
|
|
1230
|
-
record.cleanup = { complete: true, removed: out.removed };
|
|
1231
|
-
await emit(record, 'team_cleaned', { text: 'Confirmed cleanup removed the team worktrees and branches.' });
|
|
1232
|
-
return { ok: true, teamId: record.teamId, removed: out.removed, ...(!verified ? { confirmation: 'unverified' } : {}) };
|
|
1261
|
+
record.cleanup = { complete: true, removed: out.removed, by: owner ? 'owner' : 'assistant' };
|
|
1262
|
+
await emit(record, 'team_cleaned', { text: owner ? 'The owner cleaned up the team\'s worktrees and branches from Agents Studio.' : 'Confirmed cleanup removed the team worktrees and branches.', by: owner ? 'owner' : 'assistant' });
|
|
1263
|
+
return { ok: true, teamId: record.teamId, removed: out.removed, ...(!owner && !verified ? { confirmation: 'unverified' } : {}) };
|
|
1233
1264
|
});
|
|
1234
1265
|
}
|
|
1235
1266
|
|
|
@@ -1241,28 +1272,305 @@ export function createTeamScheduler({
|
|
|
1241
1272
|
try { return (await dispatcher.killAll({ workflowId: record.workflowId, reason })).stopped || []; } catch (error) { say('teams:stop-failed', { teamId: record.teamId, error: error?.message || String(error) }); return []; }
|
|
1242
1273
|
}
|
|
1243
1274
|
|
|
1244
|
-
/**
|
|
1245
|
-
|
|
1275
|
+
/**
|
|
1276
|
+
* Give a team up: its workers are stopped, its worktrees and branches removed (what holds work is kept unless the
|
|
1277
|
+
* owner says `force`). Asked again of a discarded team whose work was kept, with `force`, it deletes that work:
|
|
1278
|
+
* the owner's step that lets the team be removed afterwards.
|
|
1279
|
+
*/
|
|
1280
|
+
function discard(teamId, { by = { kind: 'owner' }, force = false, reason = '', remove: andRemove = false } = {}) {
|
|
1281
|
+
const record = records.get(String(teamId || ''));
|
|
1282
|
+
if (!record) return Promise.resolve(refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`, { status: 404 }));
|
|
1283
|
+
return teamStep(record, async () => {
|
|
1284
|
+
const out = await discardNow(record, { by, force, reason });
|
|
1285
|
+
// Discard & remove (the owner's): the record goes too, in the same step, only once the discard left nothing on
|
|
1286
|
+
// disk or in git and no worker lives (removalOf). Otherwise the team stays, discarded, and the answer says why.
|
|
1287
|
+
if (!out.ok || andRemove !== true || by?.kind !== 'owner') return out;
|
|
1288
|
+
if (!out.complete) return { ...out, teamRemoved: false, removal: removalView(await removalOf(record)) };
|
|
1289
|
+
const check = await removalOf(record);
|
|
1290
|
+
if (!check.ok) return { ...out, teamRemoved: false, removal: removalView(check) };
|
|
1291
|
+
const gone = await removeNow(record, { by, check });
|
|
1292
|
+
return { ...out, teamRemoved: gone.ok === true, ...(gone.ok ? {} : { removal: { removable: false, code: gone.code, message: gone.message, next: null, left: [] } }) };
|
|
1293
|
+
});
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
/** The discard itself, inside the team's step. */
|
|
1297
|
+
async function discardNow(record, { by = { kind: 'owner' }, force = false, reason = '' } = {}) {
|
|
1298
|
+
const teamId = record.teamId;
|
|
1299
|
+
const journal = await journalOf(record.teamId);
|
|
1300
|
+
if (journal?.state === 'merged') return refuse('ALREADY_LANDED', `Team "${teamId}" has landed: there is nothing to discard.`, { status: 409 });
|
|
1301
|
+
if (record.status === 'discarded' && journal?.removed) return { ok: true, already: true, complete: true, removed: [], kept: [], cleanup: record.cleanup };
|
|
1302
|
+
const assistant = by?.kind !== 'owner';
|
|
1303
|
+
const forced = !assistant && force === true;
|
|
1304
|
+
const again = record.status === 'discarded';
|
|
1305
|
+
const stopped = await stopRuns(record, 'team_discarded');
|
|
1306
|
+
const removed = await teams.remove(record.teamId, { discard: true, force: forced });
|
|
1307
|
+
if (!removed.ok) return removed; // the library's refusal, unchanged
|
|
1308
|
+
record.status = 'discarded';
|
|
1309
|
+
record.attention = null;
|
|
1310
|
+
record.cleanup = { complete: removed.complete === true, kept: (removed.kept || []).map((item) => ({ sliceId: item.sliceId, path: item.path, branch: item.branch, reason: item.reason, files: (item.files || []).slice(0, 20), commits: item.commits ?? null })), ...(forced ? { forced: true } : {}) };
|
|
1311
|
+
const kept = record.cleanup.kept.length;
|
|
1312
|
+
const outcome = removed.complete ? (again ? 'The work that was kept is deleted: worktrees and branches removed.' : 'Worktrees and branches removed.') : `${kept} worktree${kept === 1 ? '' : 's'} or branch${kept === 1 ? '' : 'es'} kept because work is in them.`;
|
|
1313
|
+
await emit(record, 'discarded', {
|
|
1314
|
+
text: `${again ? 'Discarded again' : 'Discarded'}${forced ? ' (force)' : ''}${reason ? `: ${clip(reason, 200)}` : ''}. ${outcome}`, by: by?.kind || 'owner',
|
|
1315
|
+
}, { quiet: assistant && by?.sessionId === record.sessionId });
|
|
1316
|
+
return { ok: true, stopped, complete: removed.complete === true, removed: removed.removed || [], kept: record.cleanup.kept };
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
// ── removing a closed team ─────────────────────────────────────────────────
|
|
1320
|
+
/** The runs of a team that are still alive (working, waiting warm, queued, suspended, resuming). */
|
|
1321
|
+
function liveRunsOf(record) {
|
|
1322
|
+
const dispatcher = dispatcherOf();
|
|
1323
|
+
if (!dispatcher || typeof dispatcher.get !== 'function') return null;
|
|
1324
|
+
const ids = new Set();
|
|
1325
|
+
for (const slice of Object.values(record.slices || {})) {
|
|
1326
|
+
for (const id of slice.runIds || []) ids.add(id);
|
|
1327
|
+
if (slice.runId) ids.add(slice.runId);
|
|
1328
|
+
}
|
|
1329
|
+
let listed = null;
|
|
1330
|
+
try { listed = dispatcher.teamRuns?.(record.teamId, record.workflowId); } catch { listed = null; }
|
|
1331
|
+
if (Array.isArray(listed)) for (const run of listed) if (run?.runId) ids.add(run.runId);
|
|
1332
|
+
const live = [];
|
|
1333
|
+
for (const id of ids) {
|
|
1334
|
+
const run = runOf(id);
|
|
1335
|
+
if (run && (run.active === true || run.state === 'running' || run.state === 'queued')) live.push(`${id}: ${run.state || 'active'}`);
|
|
1336
|
+
}
|
|
1337
|
+
return live;
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
const present = (path) => { try { lstatSync(path); return true; } catch { return false; } };
|
|
1341
|
+
const isFolder = (path) => { try { return lstatSync(path).isDirectory(); } catch { return false; } };
|
|
1342
|
+
|
|
1343
|
+
/**
|
|
1344
|
+
* What of a team is still on disk and in git, asked of git and the disk (never of what the records say): each
|
|
1345
|
+
* worktree's folder, each branch under the team's prefix, anything else in the team's folder of `.worktrees/`.
|
|
1346
|
+
* → { checked: true, left: [{ sliceId, path, branch, worktree, branchExists }] } or { checked: false, code, message }
|
|
1347
|
+
*/
|
|
1348
|
+
async function leftoversOf(record, journal) {
|
|
1349
|
+
const cwd = record.projectPath;
|
|
1350
|
+
if (!cwd || !isFolder(cwd)) return { checked: false, code: 'PROJECT_NOT_FOUND', message: `The project folder ${cwd || '(none)'} is not there, so SynaBun cannot check that nothing of the team is left. Put the folder back (or mount its disk) and try again.` };
|
|
1351
|
+
if (typeof git !== 'function') return { checked: false, code: 'GIT_UNAVAILABLE', message: 'SynaBun cannot ask git about this team here.' };
|
|
1352
|
+
const prefix = `refs/heads/${BRANCH_PREFIX}/${record.teamId}/`;
|
|
1353
|
+
let refs;
|
|
1354
|
+
try { refs = await git(['for-each-ref', '--format=%(refname)', prefix], { cwd }); } catch { refs = null; }
|
|
1355
|
+
if (!refs?.ok) return { checked: false, code: 'NOT_A_REPOSITORY', message: `${cwd} could not be read as a git repository, so SynaBun cannot check that the team's branches are gone.` };
|
|
1356
|
+
const branches = new Set(String(refs.stdout || '').split('\n').map((line) => line.trim()).filter(Boolean).map((ref) => ref.slice('refs/heads/'.length)));
|
|
1357
|
+
const plan = journal?.plan || record.plan || { slices: [] };
|
|
1358
|
+
const pieces = [...(plan.slices || []).map((slice) => slice.id), ...(plan.readOnly ? [] : [INTEGRATION_SLICE_ID])];
|
|
1359
|
+
const left = [];
|
|
1360
|
+
const paths = new Set();
|
|
1361
|
+
for (const sliceId of pieces) {
|
|
1362
|
+
let path;
|
|
1363
|
+
try { path = worktreePath(cwd, record.teamId, sliceId); } catch { continue; }
|
|
1364
|
+
const branch = sliceId === INTEGRATION_SLICE_ID ? integrationBranchName(record.teamId) : branchName(record.teamId, sliceId);
|
|
1365
|
+
paths.add(path);
|
|
1366
|
+
const worktree = present(path);
|
|
1367
|
+
const branchExists = branches.delete(branch);
|
|
1368
|
+
if (worktree || branchExists) left.push({ sliceId, path, branch, worktree, branchExists });
|
|
1369
|
+
}
|
|
1370
|
+
// A branch under the team's prefix that is no slice of its plan is the team's too.
|
|
1371
|
+
for (const branch of branches) left.push({ sliceId: null, path: null, branch, worktree: false, branchExists: true });
|
|
1372
|
+
let folder = null;
|
|
1373
|
+
try { folder = dirname(worktreePath(cwd, record.teamId, INTEGRATION_SLICE_ID)); } catch { folder = null; }
|
|
1374
|
+
if (folder && isFolder(folder)) {
|
|
1375
|
+
let names = [];
|
|
1376
|
+
try { names = readdirSync(folder); } catch { names = []; }
|
|
1377
|
+
for (const name of names) {
|
|
1378
|
+
const path = join(folder, name);
|
|
1379
|
+
if (!paths.has(path)) left.push({ sliceId: null, path, branch: null, worktree: true, branchExists: false });
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
return { checked: true, left, folder };
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
/**
|
|
1386
|
+
* Can a team's record go? Only a closed team (landed, completed, discarded, failed) with no live run and nothing
|
|
1387
|
+
* left on disk or in git: a landed team that was not cleaned up, a discarded one whose work was kept, are refused,
|
|
1388
|
+
* so work is never left behind without a record that points at it.
|
|
1389
|
+
*/
|
|
1390
|
+
async function removalOf(record, journal = undefined) {
|
|
1391
|
+
const known = journal === undefined ? await journalOf(record.teamId) : journal;
|
|
1392
|
+
if (!CLOSED_STATUSES.has(record.status)) return refuse('TEAM_OPEN', `Team "${record.teamId}" is ${record.status}: only a closed team (landed, completed, discarded or failed) can be removed. Land or discard it first.`, { status: 409 });
|
|
1393
|
+
// The journal must be over too: the library's recovery acts on a team that is not (it could make a worktree
|
|
1394
|
+
// again), and nothing would point at what it made once the record is gone. A team with no journal entry is over.
|
|
1395
|
+
if (known && !TERMINAL_TEAM_STATES.includes(known.state)) return refuse('TEAM_NEEDS_RECOVERY', `Team "${record.teamId}" is recorded as ${known.state} in the journal: restart SynaBun so its recovery runs before it is removed.`, { status: 409 });
|
|
1396
|
+
const live = liveRunsOf(record);
|
|
1397
|
+
if (live === null) return refuse('DISPATCH_UNAVAILABLE', 'The Assistant\'s dispatcher is not running, so SynaBun cannot check that no worker of the team is still alive.', { status: 503 });
|
|
1398
|
+
if (live.length) return refuse('TEAM_RUNS_ACTIVE', `A worker of team "${record.teamId}" is still alive (${live.join(', ')}): stop it before the team is removed.`, { status: 409, runs: live });
|
|
1399
|
+
const disk = await leftoversOf(record, known);
|
|
1400
|
+
if (!disk.checked) return refuse(disk.code, disk.message, { status: 409 });
|
|
1401
|
+
if (disk.left.length) {
|
|
1402
|
+
const readOnly = (known?.plan || record.plan)?.readOnly === true;
|
|
1403
|
+
const next = record.status === 'landed' && !readOnly ? 'cleanup' : (record.status === 'discarded' || record.status === 'failed') && !readOnly ? 'force_discard' : 'by_hand';
|
|
1404
|
+
const what = `${disk.left.length} worktree${disk.left.length === 1 ? '' : 's'} or branch${disk.left.length === 1 ? '' : 'es'} of it ${disk.left.length === 1 ? 'is' : 'are'} still there`;
|
|
1405
|
+
const how = next === 'cleanup' ? 'Clean up its worktrees first (they are removed only when every branch is merged and every worktree is clean).'
|
|
1406
|
+
: next === 'force_discard' ? 'They hold work that was not landed: delete the kept work first (discard with force), or land it another way.'
|
|
1407
|
+
: 'Remove them by hand (git worktree remove, git branch -d) once you no longer need them.';
|
|
1408
|
+
return refuse('TEAM_WORKTREES_LEFT', `Team "${record.teamId}" cannot be removed: ${what} (${disk.left.map((item) => item.path || item.branch).slice(0, 6).join(', ')}). ${how}`, { status: 409, next, left: disk.left });
|
|
1409
|
+
}
|
|
1410
|
+
return { ok: true, teamId: record.teamId, removable: true, left: [], folder: disk.folder };
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
/** A removal answer as the page reads it. */
|
|
1414
|
+
const removalView = (out) => ({
|
|
1415
|
+
removable: out.ok === true, code: out.ok ? null : out.code || null, message: out.ok ? null : out.message || null,
|
|
1416
|
+
next: out.next || null, left: (out.left || []).map((item) => ({ sliceId: item.sliceId ?? null, path: item.path ?? null, branch: item.branch ?? null, worktree: item.worktree === true, branchExists: item.branchExists === true })),
|
|
1417
|
+
});
|
|
1418
|
+
|
|
1419
|
+
/** Whether a team's record can be removed now, and what is left of it when not (no change is made). */
|
|
1420
|
+
async function removability(teamId) {
|
|
1421
|
+
const record = records.get(String(teamId || ''));
|
|
1422
|
+
if (!record) return { removable: false, code: 'TEAM_NOT_FOUND', message: `No team named "${teamId}" is known here.`, next: null, left: [] };
|
|
1423
|
+
return removalView(await removalOf(record));
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
/**
|
|
1427
|
+
* Forget a closed team: its record here (`<dataDir>/teams/<teamId>.json`) and its test sandbox profile go, and it
|
|
1428
|
+
* leaves the list. Nothing in git or on disk is touched (removalOf refuses while anything of it is there); the
|
|
1429
|
+
* journal and the durable log keep their entries, so the team's id is never made again. The record is written
|
|
1430
|
+
* nowhere after this: never through emit(), which saves the record again.
|
|
1431
|
+
*/
|
|
1432
|
+
function remove(teamId, { by = { kind: 'owner' } } = {}) {
|
|
1246
1433
|
const record = records.get(String(teamId || ''));
|
|
1247
1434
|
if (!record) return Promise.resolve(refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`, { status: 404 }));
|
|
1248
1435
|
return teamStep(record, async () => {
|
|
1436
|
+
const check = await removalOf(record);
|
|
1437
|
+
if (!check.ok) return check;
|
|
1438
|
+
return removeNow(record, { by, check });
|
|
1439
|
+
});
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
/** The removal itself, inside the team's step, once removalOf said yes (`check`). */
|
|
1443
|
+
async function removeNow(record, { by = { kind: 'owner' }, check } = {}) {
|
|
1444
|
+
// The team's own empty folder of .worktrees/ (nothing is in it: removalOf looked).
|
|
1445
|
+
if (check?.folder) { try { rmdirSync(check.folder); } catch { /* not there */ } }
|
|
1446
|
+
try { unlinkSync(fileOf(record.teamId)); } catch (error) {
|
|
1447
|
+
if (error?.code !== 'ENOENT') return refuse('TEAM_REMOVE_FAILED', `The record of team "${record.teamId}" could not be deleted: ${error?.message || error}`, { status: 500 });
|
|
1448
|
+
}
|
|
1449
|
+
removedTeams.add(record.teamId);
|
|
1450
|
+
try { unlinkSync(join(dir, 'sandbox', `${record.teamId}.sb`)); } catch { /* none was written */ }
|
|
1451
|
+
records.delete(record.teamId);
|
|
1452
|
+
const runIds = [];
|
|
1453
|
+
for (const [runId, ref] of runIndex) if (ref.teamId === record.teamId) runIds.push(runId);
|
|
1454
|
+
for (const runId of runIds) { runIndex.delete(runId); postTimes.delete(runId); }
|
|
1455
|
+
try {
|
|
1456
|
+
await store.append({
|
|
1457
|
+
id: `team:${record.teamId}:removed`, kind: TEAM_REMOVED_LOG_KIND, priority: 'low', teamId: record.teamId, sessionId: record.sessionId || null, runId: null,
|
|
1458
|
+
payload: { kind: 'team_removed', teamId: record.teamId, title: record.title || null, status: record.status, by: by?.kind || 'owner' }, to: [],
|
|
1459
|
+
});
|
|
1460
|
+
} catch (error) { say('teams:event-failed', { teamId: record.teamId, event: 'team_removed', error: error?.message || String(error) }); }
|
|
1461
|
+
try { broadcast({ type: TEAM_CHANGED, teamId: record.teamId, event: 'removed', status: null, sliceId: null }); } catch { /* no page is listening */ }
|
|
1462
|
+
say('teams:removed', { teamId: record.teamId, status: record.status, by: by?.kind || 'owner' });
|
|
1463
|
+
// `closedAs`, never `status`: a route's answer takes `status` for the HTTP status (lib/teams/api.js).
|
|
1464
|
+
return { ok: true, teamId: record.teamId, removed: true, closedAs: record.status };
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
// ── a team whose work is on its base branch already ────────────────────────
|
|
1468
|
+
/**
|
|
1469
|
+
* A `ready` code team whose joined work reached its base branch outside `land` (somebody merged the integration
|
|
1470
|
+
* branch by hand), its worktrees and branches still there: where, and whether they could go now (`clean`: every
|
|
1471
|
+
* worktree clean, every branch merged into the base branch, no worker alive). Asked of git (teams.onTarget), never
|
|
1472
|
+
* of the records; null for every other team. `full`: also what keeps it from being clean.
|
|
1473
|
+
*/
|
|
1474
|
+
async function onTargetOf(record, { full = false } = {}) {
|
|
1475
|
+
if (record.status !== 'ready' || record.plan?.readOnly === true || typeof teams.onTarget !== 'function') return null;
|
|
1476
|
+
let found = null;
|
|
1477
|
+
try { found = await teams.onTarget(record.teamId); } catch (error) { say('teams:on-target-failed', { teamId: record.teamId, error: error?.message || String(error) }); return null; }
|
|
1478
|
+
if (!found) return null;
|
|
1479
|
+
const live = liveRunsOf(record);
|
|
1480
|
+
const runs = live === null ? null : live.length;
|
|
1481
|
+
const brief = { branch: found.branch, head: found.head, clean: found.clean === true && runs === 0 };
|
|
1482
|
+
if (!full) return brief;
|
|
1483
|
+
return { ...brief, targetHead: found.targetHead, live: found.live, moved: found.moved === true, runs, dirty: found.dirty || [], unmerged: found.unmerged || [], other: found.other || [] };
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
/**
|
|
1487
|
+
* What the Agents Studio list says of a team beside its summary, asked of git, the disk and the dispatcher: a
|
|
1488
|
+
* closed team's `removable`; a team that waits on the owner (needs attention, ready) its live workers (`liveRuns`),
|
|
1489
|
+
* how much of it is on disk (`left`: worktrees and branches) and, ready, whether its work is on its base branch
|
|
1490
|
+
* already (`alreadyOnTarget`). A running team: nothing (the page offers it nothing from the list).
|
|
1491
|
+
*/
|
|
1492
|
+
async function overview(teamId) {
|
|
1493
|
+
const record = records.get(String(teamId || ''));
|
|
1494
|
+
if (!record) return null;
|
|
1495
|
+
if (CLOSED_STATUSES.has(record.status)) return { removable: (await removability(record.teamId)).removable === true };
|
|
1496
|
+
if (record.status !== 'needs_attention' && record.status !== 'ready') return {};
|
|
1497
|
+
const live = liveRunsOf(record);
|
|
1498
|
+
const disk = await leftoversOf(record, await journalOf(record.teamId));
|
|
1499
|
+
return {
|
|
1500
|
+
liveRuns: live === null ? null : live.length,
|
|
1501
|
+
left: disk.checked ? disk.left.length : null,
|
|
1502
|
+
alreadyOnTarget: await onTargetOf(record),
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
/**
|
|
1507
|
+
* The owner's Clean up & remove of a team whose work is on its base branch already: everything is asked again of
|
|
1508
|
+
* git as it runs (on the base branch, every worktree clean, every branch merged, no worker alive); refused, nothing
|
|
1509
|
+
* is removed and the answer says why. Then the landing somebody else made is recorded (`landed.external`), the
|
|
1510
|
+
* worktrees and branches go, and the team's record with them. Only from the owner's page (`by.kind` 'owner',
|
|
1511
|
+
* `userConfirmed`).
|
|
1512
|
+
*/
|
|
1513
|
+
function cleanupMerged(teamId, { by = null, userConfirmed = false } = {}) {
|
|
1514
|
+
const record = records.get(String(teamId || ''));
|
|
1515
|
+
if (!record) return Promise.resolve(refuse('TEAM_NOT_FOUND', `No team named "${teamId}" is known here.`, { status: 404 }));
|
|
1516
|
+
return teamStep(record, async () => {
|
|
1517
|
+
if (by?.kind !== 'owner') return refuse('TEAM_FORBIDDEN', 'Cleaning up a team that is on its base branch already is the owner\'s, from Agents Studio.', { status: 403 });
|
|
1518
|
+
if (userConfirmed !== true) return refuse('CLEANUP_NOT_CONFIRMED', 'Requires the owner\'s explicit confirmation from SynaBun\'s own page.');
|
|
1519
|
+
if (record.plan.readOnly) return refuse('TEAM_READ_ONLY', 'Research and review teams have no integration branch.');
|
|
1520
|
+
if (record.status !== 'ready') return refuse('TEAM_NOT_READY', `Team "${record.teamId}" is ${record.status}: only a ready team whose work is on its base branch already is cleaned up this way.`);
|
|
1521
|
+
const live = liveRunsOf(record);
|
|
1522
|
+
if (live === null) return refuse('DISPATCH_UNAVAILABLE', 'The Assistant\'s dispatcher is not running, so SynaBun cannot check that no worker of the team is still alive.', { status: 503 });
|
|
1523
|
+
if (live.length) return refuse('TEAM_RUNS_ACTIVE', `A worker of team "${record.teamId}" is still alive (${live.join(', ')}): nothing was removed.`, { runs: live });
|
|
1524
|
+
let out;
|
|
1525
|
+
try {
|
|
1526
|
+
out = await teams.remove(record.teamId, { guarded: true, mergedOutside: true });
|
|
1527
|
+
} catch (error) {
|
|
1528
|
+
out = refuse('CLEANUP_INTERRUPTED', `${error?.message || String(error)}. Look at the team's worktrees before trying again.`, { status: 500 });
|
|
1529
|
+
}
|
|
1530
|
+
// The landing is recorded before anything is removed: whatever came of the removal, the journal may say so now.
|
|
1249
1531
|
const journal = await journalOf(record.teamId);
|
|
1250
|
-
|
|
1251
|
-
if (
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1532
|
+
const landed = journal?.state === 'merged' ? (out.landed || journal.landed || null) : null;
|
|
1533
|
+
if (landed) {
|
|
1534
|
+
record.status = 'landed';
|
|
1535
|
+
record.attention = null;
|
|
1536
|
+
record.landing = { landed, at: iso(now()), by: 'owner', external: true };
|
|
1537
|
+
}
|
|
1538
|
+
if (!out.ok) {
|
|
1539
|
+
if (landed) {
|
|
1540
|
+
record.cleanup = { available: true, next: 'The team\'s work is on its base branch; its worktrees and branches are kept until a clean-up can remove them.' };
|
|
1541
|
+
await emit(record, 'landed', { targetBranch: landed.targetBranch, mode: null, to: landed.to || null, text: `Found on ${landed.targetBranch} already (merged outside SynaBun); its worktrees and branches were kept: ${clip(out.message, 300)}`, by: 'owner' });
|
|
1542
|
+
}
|
|
1543
|
+
return out; // nothing was removed (the library's refusal, unchanged)
|
|
1544
|
+
}
|
|
1545
|
+
const branch = landed?.targetBranch || journal?.baseBranch || null;
|
|
1546
|
+
record.cleanedAt = iso(now());
|
|
1547
|
+
record.cleanup = { complete: true, removed: out.removed, by: 'owner', external: true };
|
|
1548
|
+
await emit(record, 'team_cleaned', { text: `Already on ${branch}: the owner cleaned up the team's worktrees and branches from Agents Studio and removed the team.`, by: 'owner', targetBranch: branch });
|
|
1549
|
+
const check = await removalOf(record, journal);
|
|
1550
|
+
if (!check.ok) return { ok: true, teamId: record.teamId, branch, removed: out.removed, teamRemoved: false, removal: removalView(check) };
|
|
1551
|
+
const gone = await removeNow(record, { by, check });
|
|
1552
|
+
return { ok: true, teamId: record.teamId, branch, removed: out.removed, teamRemoved: gone.ok === true, ...(gone.ok ? {} : { removal: { removable: false, code: gone.code, message: gone.message, next: null, left: [] } }) };
|
|
1263
1553
|
});
|
|
1264
1554
|
}
|
|
1265
1555
|
|
|
1556
|
+
/**
|
|
1557
|
+
* Remove every closed team that can go (the page's Clear finished). A team that cannot is never dropped quietly:
|
|
1558
|
+
* it is listed in `skipped` with why (`code`, `next`: cleanup, force_discard, by_hand).
|
|
1559
|
+
*/
|
|
1560
|
+
async function clearFinished({ sessionId = null, projectPath = null, by = { kind: 'owner' } } = {}) {
|
|
1561
|
+
const removed = [];
|
|
1562
|
+
const skipped = [];
|
|
1563
|
+
const closedTeams = [...records.values()]
|
|
1564
|
+
.filter((record) => CLOSED_STATUSES.has(record.status) && (!sessionId || record.sessionId === String(sessionId)) && (!projectPath || record.projectPath === projectPath))
|
|
1565
|
+
.sort((a, b) => String(b.createdAt).localeCompare(String(a.createdAt)));
|
|
1566
|
+
for (const record of closedTeams) {
|
|
1567
|
+
const out = await remove(record.teamId, { by });
|
|
1568
|
+
if (out.ok) removed.push(record.teamId);
|
|
1569
|
+
else skipped.push({ teamId: record.teamId, title: record.title || record.teamId, status: record.status, code: out.code || null, message: out.message || null, next: out.next || null, left: (out.left || []).length });
|
|
1570
|
+
}
|
|
1571
|
+
return { ok: true, removed, skipped };
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1266
1574
|
// ── the channel (a worker's `team` tool) ───────────────────────────────────
|
|
1267
1575
|
/** The team and slice a run works on now, or null: only the slice's current run, while it lives, is a member. */
|
|
1268
1576
|
function member(runId) {
|
|
@@ -1489,7 +1797,14 @@ export function createTeamScheduler({
|
|
|
1489
1797
|
await withAllSlices(record, async () => {
|
|
1490
1798
|
const journal = await journalOf(record.teamId);
|
|
1491
1799
|
if (!journal) { record.status = 'failed'; record.attention = { code: 'TEAM_RECORD_MISSING', sliceId: null, message: 'The team has no record in the journal any more.', at: iso(now()) }; save(record); return { teamId: record.teamId, status: record.status }; }
|
|
1492
|
-
if (journal.state === 'merged') {
|
|
1800
|
+
if (journal.state === 'merged') {
|
|
1801
|
+
record.status = 'landed';
|
|
1802
|
+
// Closed by the library's recovery with no branch or worktree of it left (merged outside land and tidied
|
|
1803
|
+
// by hand): there is nothing for a confirmed cleanup to remove.
|
|
1804
|
+
if (journal.removed && !record.cleanedAt) { record.cleanedAt = iso(now()); record.cleanup = { complete: true, removed: [] }; }
|
|
1805
|
+
save(record);
|
|
1806
|
+
return { teamId: record.teamId, status: record.status };
|
|
1807
|
+
}
|
|
1493
1808
|
if (journal.state === 'cancelled' || journal.state === 'failed') { record.status = journal.state === 'failed' ? 'failed' : 'discarded'; save(record); return { teamId: record.teamId, status: record.status }; }
|
|
1494
1809
|
for (const slice of record.plan.slices) {
|
|
1495
1810
|
const mine = record.slices[slice.id];
|
|
@@ -1556,6 +1871,10 @@ export function createTeamScheduler({
|
|
|
1556
1871
|
return Object.freeze({
|
|
1557
1872
|
limits,
|
|
1558
1873
|
plan, start, status, list, get, ownedBy, assign, setTestCommand, integrate, land, cleanup, discard,
|
|
1874
|
+
// a closed team's record (the owner's, from Agents Studio)
|
|
1875
|
+
removability, remove, clearFinished,
|
|
1876
|
+
// what the Studio's list says beside a summary; a ready team whose work is on its base branch already
|
|
1877
|
+
overview, cleanupMerged,
|
|
1559
1878
|
// the channel of a worker's `team` tool (the caller's proven run id decides the team)
|
|
1560
1879
|
member, post, read, wait, ack, claims, request,
|
|
1561
1880
|
recover: () => track(recover()), attach, shutdown, settled,
|