refactor(kaos): move Environment into kaos, slim package API#147
Conversation
Move the `Environment` interface and detection helpers from `agent-core/src/utils/environment.ts` into `@moonshot-ai/kaos`, fold the host probe into `Kaos.osEnv`, and prune everything in the kaos package that no caller outside it relied on. Highlights: - `Environment`, `EnvironmentDeps`, `OsKind`, `ShellName`, `detectEnvironment`, and `detectEnvironmentFromNode` now live in `packages/kaos/src/environment.ts`. `detectEnvironmentFromNode` memoises its result. - `Kaos` gains `readonly osEnv: Environment`. `LocalKaos` and `SSHKaos` carry the field; `RuntimeConfig.osEnv` is dropped in favour of reading `runtime.kaos.osEnv`. - `LocalKaos` keeps a private constructor and is built via `await LocalKaos.create()`. No more singleton state — each call returns a fresh instance with its own `_cwd`, restoring the per-instance cwd isolation that was already covered by tests. - Add `KaosShellNotFoundError`; `core-impl.ts` translates it back to `KimiError(SHELL_GIT_BASH_NOT_FOUND)` so the public error code is preserved. - Delete the unused `KaosPath` class, the AsyncLocalStorage helpers nobody called from outside the package (`setCurrentKaos`, `resetCurrentKaos`, `KaosToken`), and the now-stale path tests. - Agent-core tests build `testKaos` once via a fixture (`test/fixtures/test-kaos.ts`) by bypassing the private constructor with a stub `Environment`, so no test setup file / ALS plumbing is needed there.
|
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0d513daafe
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 92652e8d3a
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| }); | ||
| if (changed.cwd !== undefined) this._cwd = changed.cwd; | ||
| if (changed.cwd) { | ||
| this.agent.runtime.kaos.chdir(changed.cwd); |
There was a problem hiding this comment.
Await cwd changes before rebuilding tools
When config.update receives a new cwd while a provider is already configured, LocalKaos.chdir() only updates _cwd after an awaited stat, but this call is fired without awaiting before initializeBuiltinTools() reads config.cwd on the next lines. In that scenario the refreshed Bash/file tools can stay rooted at the previous working directory, and failures from an invalid cwd become unhandled rejections after the config record has already been logged; make the cwd transition complete before rebuilding tools or recording success.
Useful? React with 👍 / 👎.
…b.com/MoonshotAI/kimi-code into refactor/kaos-environment-and-cleanup
Summary
Environmentand its detection helpers fromagent-core/src/utils/environment.tsinto@moonshot-ai/kaos; addreadonly osEnv: Environmentto theKaosinterface and dropRuntimeConfig.osEnv.LocalKaos.create()returns a fresh instance per call (no singleton); addKaosShellNotFoundErrorand rewrap it back toKimiError(SHELL_GIT_BASH_NOT_FOUND)incore-impl.ts.KaosPathand its tests, the AsyncLocalStorage helpers (setCurrentKaos/resetCurrentKaos/KaosToken), and assorted cleanup;detectEnvironmentFromNodeis now memoised.Test plan
pnpm typecheckinpackages/kaos,packages/agent-core,packages/migration-legacy,apps/kimi-codepnpm vitest run— kaos 243, agent-core 1870, migration-legacy 172, apps/kimi-code 965 passingdetectEnvironmentFromNodestill resolves Git Bash (cache path is identical to old behaviour)