Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/release-lease-on-failed-spawn-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Fix quitting, or changing a remote environment, taking about five extra seconds after a git, gh or ripgrep command failed to start on that host, for example when gh is not installed there.
5 changes: 5 additions & 0 deletions .changeset/release-lease-on-failed-spawn-desktop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kimi-code-app": patch
---

Fix quitting, or changing a remote environment, taking about five extra seconds after a git, gh or ripgrep command failed to start on that host, for example when gh is not installed there.
5 changes: 5 additions & 0 deletions .changeset/release-lease-on-failed-spawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/agent-core-v2": patch
---

Release the environment lease when a git, gh or ripgrep command fails to start, so removing that environment no longer waits out the lease drain timeout.
1 change: 1 addition & 0 deletions packages/agent-core-v2/src/app/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class GitService implements IGitService {
() => ({ ok: false as const }),
);
if (!spawned.ok) {
lease.dispose();
return { exitCode: -1, stdout: '', stderr: '' };
}
const { proc } = spawned;
Expand Down
5 changes: 4 additions & 1 deletion packages/agent-core-v2/src/project/projectFs/fsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,10 @@ export class ProjectFsService implements IProjectFsService {
onLine: (line: string, stop: () => void) => void,
): Promise<{ readonly exitCode: number; readonly killed: boolean }> {
const lease = this.resolver.acquire({ environmentId: this.environmentId }, ['process']);
const proc = await lease.environment.process!.spawn(binary, args, { cwd: this.workDir });
const proc = await lease.environment.process!.spawn(binary, args, { cwd: this.workDir }).catch((error: unknown) => {
lease.dispose();
throw error;
});
let killed = false;
const kill = (): void => {
if (killed) return;
Expand Down
47 changes: 47 additions & 0 deletions packages/agent-core-v2/test/app/git/gitService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,53 @@ describe('GitService', () => {
});
});

describe('commands that cannot start in a remote environment', () => {
function remoteGitService(missing: ReadonlySet<string>): { readonly service: GitService; activeLeases(): number } {
const host = new HostProcessService();
const environment = {
process: {
spawn: (command: string, args: readonly string[], options: Parameters<HostProcessService['spawn']>[2]) =>
missing.has(command)
? Promise.reject(Object.assign(new Error(`spawn ${command} ENOENT`), { code: 'ENOENT' }))
: host.spawn(command, args, options),
},
} as unknown as Environment;
let active = 0;
const resolver = {
acquire: () => {
active += 1;
let released = false;
return {
environment,
track: <T>(resource: T): T => resource,
dispose: () => {
if (released) return;
released = true;
active -= 1;
},
};
},
} as unknown as EnvironmentResolver;
return { service: new GitService(resolver, new HostFileSystem(), 'remote-box'), activeLeases: () => active };
}

it('releases the lease when gh is missing and still reports the status without a pull request', async () => {
writeFileSync(join(repo, 'a.txt'), 'hello\n');
commitAll('init');
const remote = remoteGitService(new Set(['gh']));

await expect(remote.service.status(repo)).resolves.toMatchObject({ pullRequest: null });
expect(remote.activeLeases()).toBe(0);
});

it.each(['status', 'pullRequest'] as const)('releases the lease when git is missing and %s reports git unavailable', async (method) => {
const remote = remoteGitService(new Set(['git', 'gh']));

await expect(remote.service[method](repo)).rejects.toMatchObject({ code: ErrorCodes.FS_GIT_UNAVAILABLE });
expect(remote.activeLeases()).toBe(0);
});
});

describe('diff', () => {
it('returns the unified diff for a tracked modified file', async () => {
writeFileSync(join(repo, 'a.txt'), 'old\n');
Expand Down
47 changes: 47 additions & 0 deletions packages/agent-core-v2/test/project/projectFs/fsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,53 @@ describe('WorkspaceFsService.suggest', () => {
});
});

function rgSpawnFailureSession(): { readonly fs: IProjectFsService; activeLeases(): number } {
const runner: IHostProcessService = {
_serviceBrand: undefined,
spawn: async (_command, args) => {
if (args?.[0] === '--version') return fakeProcess('ripgrep 15.0.0', '', 0);
throw new Error('spawn EAGAIN');
},
};
const fs = makeSession({ 'src/foo.ts': 'hello\n' }, emptyHandler, [], defaultGitStub(), [], runner);
let active = 0;
Object.defineProperty(fs, 'resolver', {
value: {
acquire: () => {
active += 1;
let released = false;
return {
environment: { process: runner },
track: (resource: unknown) => resource,
dispose: () => {
if (released) return;
released = true;
active -= 1;
},
};
},
},
});
Object.defineProperty(fs, 'rgResolution', { value: { path: 'rg' } });
return { fs, activeLeases: () => active };
}

it('releases the environment lease when rg fails to spawn for suggest', async () => {
const session = rgSpawnFailureSession();

const result = await session.fs.suggest({ query: 'foo', limit: 50, follow_gitignore: true, show_hidden: false });

expect(result.items.map((item) => item.path)).toContain('src/foo.ts');
expect(session.activeLeases()).toBe(0);
});

it('releases the environment lease when rg fails to spawn for grep', async () => {
const session = rgSpawnFailureSession();

await expect(session.fs.grep(WALK_GREP_REQUEST)).rejects.toThrow('spawn EAGAIN');
expect(session.activeLeases()).toBe(0);
});

it('falls back to the node walk when rg exits with an error status', async () => {
const events: Array<{ event: string; properties: Record<string, unknown> }> = [];
const runner: IHostProcessService = {
Expand Down
1 change: 0 additions & 1 deletion scripts/checks/baselines/oxlint-ratchet.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@
"packages/agent-core-v2/test/app/agentProfileCatalog/profile-shared.test.ts": 1,
"packages/agent-core-v2/test/app/file/fileService.test.ts": 1,
"packages/agent-core-v2/test/app/gateway/gateway.test.ts": 3,
"packages/agent-core-v2/test/app/git/gitService.test.ts": 1,
"packages/agent-core-v2/test/app/kosongConfig/discovery.test.ts": 1,
"packages/agent-core-v2/test/app/mcpManagement/mcpManagement.test.ts": 3,
"packages/agent-core-v2/test/app/project/projectService.test.ts": 1,
Expand Down
Loading