-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(agent-core-v2): spawn tower workers on dirty base #3346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sailist
merged 9 commits into
MoonshotAI:main
from
tpoisonooo:feat/tower-spawn-dirty-base
Aug 31, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7e454a3
feat(agent-core-v2): spawn tower workers on a snapshot of base checko…
bae044a
fix(agent-core-v2): harden tower base-WIP snapshot edge cases
79ab205
fix(agent-core-v2): let tower mode take over from a live but idle own…
664b1f6
feat(agent-core-v2): keep tower mode active after tower teardown
23d20f4
chore: add changeset for tower teardown staying active
d6cd785
docs(tower): merge changeset
2d2d26a
docs(tower): merge changeset
b1a1f16
fix(agent-core-v2): make tower mode mutually exclusive with plan and …
59c3824
chore: add changeset for tower mode mutual exclusion
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Tower mode (experimental, `KIMI_CODE_EXPERIMENTAL_TOWER=1`): spawned workers now start from the base checkout's uncommitted changes instead of missing them, and TowerMerge refuses to merge while the checkout still holds those changes uncommitted. Also, a new session can now enter tower mode after the previous owning session stopped without exiting, instead of being refused while that session stays open. Tower mode now stays on after tower teardown; turn it off explicitly with /tower off. Tower mode is now mutually exclusive with plan mode and swarm mode: entering any one of them exits the others. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { createDecorator, type ServiceIdentifier } from '#/_base/di/instantiation'; | ||
|
|
||
| export interface IAgentModeMutexService { | ||
| readonly _serviceBrand: undefined; | ||
| } | ||
|
|
||
| export const IAgentModeMutexService: ServiceIdentifier<IAgentModeMutexService> = | ||
| createDecorator<IAgentModeMutexService>('agentModeMutexService'); |
51 changes: 51 additions & 0 deletions
51
packages/agent-core-v2/src/agent/modeMutex/modeMutexService.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { Disposable } from '#/_base/di/lifecycle'; | ||
| import { ScopeActivation, registerScopedService } from '#/_base/di/scope'; | ||
| import { IAgentStateService } from '#/agent/state/agentState'; | ||
| import { IEventBus } from '#/app/event/eventBus'; | ||
| import { LifecycleScope } from '#/app/scopes'; | ||
| import { IAgentPlanService } from '#/features/plan/plan'; | ||
| import { PlanModeEnter, planKey } from '#/features/plan/planOps'; | ||
| import { IAgentSwarmService } from '#/features/swarm/agent/swarm'; | ||
| import { SwarmModeEnter } from '#/features/swarm/swarmOps'; | ||
| import { IAgentTowerService } from '#/features/tower/tower'; | ||
| import { TowerModeEnter } from '#/features/tower/towerOps'; | ||
|
|
||
| import { IAgentModeMutexService } from './modeMutex'; | ||
|
|
||
| export class AgentModeMutexService extends Disposable implements IAgentModeMutexService { | ||
| declare readonly _serviceBrand: undefined; | ||
|
|
||
| constructor( | ||
| @IAgentPlanService private readonly plan: IAgentPlanService, | ||
| @IAgentSwarmService private readonly swarm: IAgentSwarmService, | ||
| @IAgentTowerService private readonly tower: IAgentTowerService, | ||
| @IAgentStateService private readonly agentState: IAgentStateService, | ||
| @IEventBus eventBus: IEventBus, | ||
| ) { | ||
| super(); | ||
| this._register( | ||
| eventBus.subscribe(PlanModeEnter, () => { | ||
| if (this.tower.isActive) this.tower.exit(); | ||
| }), | ||
| ); | ||
| this._register( | ||
| eventBus.subscribe(SwarmModeEnter, () => { | ||
| if (this.tower.isActive) this.tower.exit(); | ||
| }), | ||
| ); | ||
| this._register( | ||
| eventBus.subscribe(TowerModeEnter, () => { | ||
| if (this.agentState.get(planKey).active) this.plan.exit(); | ||
| if (this.swarm.isActive) this.swarm.exit(); | ||
| }), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| registerScopedService( | ||
| LifecycleScope.Agent, | ||
| IAgentModeMutexService, | ||
| AgentModeMutexService, | ||
| ScopeActivation.OnScopeCreated, | ||
| 'modeMutex', | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/agent-core-v2/src/features/tower/protocol/baseWip.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { mkdtemp, rm } from 'node:fs/promises'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| import { git } from './git'; | ||
| import { TOWER_ROOT } from './paths'; | ||
|
|
||
| export interface BaseDirtyEntry { | ||
| readonly path: string; | ||
| readonly unmerged: boolean; | ||
| } | ||
|
|
||
| const UNMERGED_CODES = new Set(['DD', 'AU', 'UD', 'UA', 'DU', 'AA', 'UU']); | ||
| const ADD_PATHS_CHUNK = 100; | ||
|
|
||
| export async function listBaseDirtyEntries(cwd: string): Promise<readonly BaseDirtyEntry[]> { | ||
| const out = await git(cwd, ['status', '--porcelain', '-z', '--no-renames', '--untracked-files=normal']); | ||
| const entries: BaseDirtyEntry[] = []; | ||
| for (const record of out.split('\0')) { | ||
| if (record.length < 4) continue; | ||
| const code = record.slice(0, 2); | ||
| const raw = record.slice(3).replace(/\/+$/, ''); | ||
| if (raw.length === 0 || raw.split('/').includes(TOWER_ROOT)) continue; | ||
| entries.push({ path: raw, unmerged: UNMERGED_CODES.has(code) }); | ||
| } | ||
| return entries; | ||
| } | ||
|
|
||
| export async function snapshotBaseWip( | ||
| cwd: string, | ||
| base: string, | ||
| paths: readonly string[], | ||
| message: string, | ||
| ): Promise<string | null> { | ||
| if (paths.length === 0) return null; | ||
| const topLevel = await git(cwd, ['rev-parse', '--show-toplevel']); | ||
| const baseTip = await git(topLevel, ['rev-parse', base]); | ||
| const indexDir = await mkdtemp(join(tmpdir(), 'tower-wip-index-')); | ||
| const env = { | ||
| GIT_INDEX_FILE: join(indexDir, 'index'), | ||
| GIT_LITERAL_PATHSPECS: '1', | ||
| }; | ||
| try { | ||
| await git(topLevel, ['read-tree', baseTip], { env }); | ||
| for (let i = 0; i < paths.length; i += ADD_PATHS_CHUNK) { | ||
| await git(topLevel, ['add', '-A', '--', ...paths.slice(i, i + ADD_PATHS_CHUNK)], { env }); | ||
| } | ||
| const tree = await git(topLevel, ['write-tree'], { env }); | ||
| const baseTree = await git(topLevel, ['rev-parse', `${baseTip}^{tree}`]); | ||
| if (tree === baseTree) return null; | ||
| return await git(topLevel, ['commit-tree', tree, '-p', baseTip, '-m', message], { env }); | ||
| } finally { | ||
| await rm(indexDir, { recursive: true, force: true }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export * from './baseWip'; | ||
| export * from './frontmatter'; | ||
| export * from './git'; | ||
| export * from './paths'; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
packages/agent-core-v2/src/features/tower/tools/merge/merge.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| Merge a tower mission branch into the base branch (--no-ff). | ||
|
|
||
| Hard gate, enforced by the store — the merge is refused unless: the branch's latest review is "clean" and was written against the current branch tip, all dependency missions are already merged, and every changed file falls inside the mission's declared scope. On refusal, the error message tells you exactly what to do next (assign a reviewer, wait for fixes, re-review a moved tip, merge deps first, widen the scope or revert the extra changes). After a merge, branches reported as conflicting must rebase onto the new base and be re-reviewed before they can merge. | ||
| Hard gate, enforced by the store — the merge is refused unless: the branch's latest review is "clean" and was written against the current branch tip, all dependency missions are already merged, and every changed file falls inside the mission's declared scope. The scope diff starts from the mission's recorded spawn base while that snapshot commit is still part of the branch's history, so base-checkout WIP captured as a snapshot commit at spawn time is never mistaken for a worker scope violation; once a rebase drops the snapshot (typically because the WIP has since been committed on the base branch), the diff falls back to the base branch. On refusal, the error message tells you exactly what to do next (assign a reviewer, wait for fixes, re-review a moved tip, merge deps first, widen the scope or revert the extra changes). After a merge, branches reported as conflicting must rebase onto the new base and be re-reviewed before they can merge. | ||
|
|
||
| The main checkout must be clean for the files the merge touches: if it still has uncommitted changes in any file the merge would overwrite, the merge is refused and nothing is merged — commit or stash those changes first, then retry. This matters when a mission branch carries a snapshot of the checkout's WIP: that WIP merges into the base history, so the checkout must not still hold the same changes uncommitted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.