From 173bdfdab1f484ed79927aeaac7dc8116d3fd346 Mon Sep 17 00:00:00 2001 From: Luyu Cheng <2239547+chengluyu@users.noreply.github.com> Date: Thu, 9 Jul 2026 16:46:57 +0800 Subject: [PATCH] fix: resume sessions with missing workdir (#1517) --- .changeset/fix-missing-workdir-resume.md | 5 +++++ packages/agent-core/src/agent/config/index.ts | 2 +- .../test/agent/config-state.test.ts | 19 +++++++++++++++++++ .../test/tools/fixtures/fake-kaos.ts | 6 +++--- 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 .changeset/fix-missing-workdir-resume.md diff --git a/.changeset/fix-missing-workdir-resume.md b/.changeset/fix-missing-workdir-resume.md new file mode 100644 index 000000000..187152cf0 --- /dev/null +++ b/.changeset/fix-missing-workdir-resume.md @@ -0,0 +1,5 @@ +--- +"@moonshot-ai/kimi-code": patch +--- + +Fix resuming sessions whose original working directory no longer exists. diff --git a/packages/agent-core/src/agent/config/index.ts b/packages/agent-core/src/agent/config/index.ts index 0eb7c1db1..d4724b6fc 100644 --- a/packages/agent-core/src/agent/config/index.ts +++ b/packages/agent-core/src/agent/config/index.ts @@ -48,7 +48,7 @@ export class ConfigState { }); if (changed.cwd) { this._cwd = changed.cwd; - void this.agent.kaos.chdir(changed.cwd); + this.agent.setKaos(this.agent.kaos.withCwd(changed.cwd)); } if (changed.modelAlias) { this._modelAlias = changed.modelAlias; diff --git a/packages/agent-core/test/agent/config-state.test.ts b/packages/agent-core/test/agent/config-state.test.ts index 32b127950..7a0d169f0 100644 --- a/packages/agent-core/test/agent/config-state.test.ts +++ b/packages/agent-core/test/agent/config-state.test.ts @@ -4,8 +4,27 @@ import { emptyUsage } from '@moonshot-ai/kosong'; import { ProviderManager } from '../../src/session/provider-manager'; import type { KimiConfig } from '../../src/config'; import { testAgent } from './harness'; +import { createFakeKaos } from '../tools/fixtures/fake-kaos'; describe('ConfigState model capabilities', () => { + it('updates the agent cwd without requiring the directory to exist', () => { + const chdir = vi.fn(async () => { + throw Object.assign(new Error('missing workspace'), { code: 'ENOENT' }); + }); + const ctx = testAgent({ + kaos: createFakeKaos({ + getcwd: () => '/workspace', + chdir, + }), + }); + + ctx.agent.config.update({ cwd: '/tmp/missing-workdir' }); + + expect(ctx.agent.config.cwd).toBe('/tmp/missing-workdir'); + expect(ctx.agent.kaos.getcwd()).toBe('/tmp/missing-workdir'); + expect(chdir).not.toHaveBeenCalled(); + }); + it('computes provider and model capabilities from ProviderManager metadata', () => { const ctx = testAgent({ providerManager: new ProviderManager({ diff --git a/packages/agent-core/test/tools/fixtures/fake-kaos.ts b/packages/agent-core/test/tools/fixtures/fake-kaos.ts index 78613d33a..4384ce348 100644 --- a/packages/agent-core/test/tools/fixtures/fake-kaos.ts +++ b/packages/agent-core/test/tools/fixtures/fake-kaos.ts @@ -32,9 +32,9 @@ export function createFakeKaos( overrides?: Partial, envLayers: readonly Record[] = [], ): Kaos { - // Hold cwd in a closure so `chdir` (which `config.update({cwd})` now - // routes through) can mutate it and later `getcwd()` calls see the - // update — mirroring real-kaos semantics without needing a backing fs. + // Hold cwd in a closure so tests that call `chdir` directly can mutate it + // and later `getcwd()` calls see the update — mirroring real-kaos semantics + // without needing a backing fs. let cwd = overrides?.getcwd?.() ?? '/workspace'; const base: Kaos = { name: 'fake',