Skip to content

refactor(kaos): move Environment into kaos, slim package API#147

Merged
kermanx merged 6 commits into
mainfrom
refactor/kaos-environment-and-cleanup
May 28, 2026
Merged

refactor(kaos): move Environment into kaos, slim package API#147
kermanx merged 6 commits into
mainfrom
refactor/kaos-environment-and-cleanup

Conversation

@kermanx
Copy link
Copy Markdown
Collaborator

@kermanx kermanx commented May 28, 2026

Summary

  • Move Environment and its detection helpers from agent-core/src/utils/environment.ts into @moonshot-ai/kaos; add readonly osEnv: Environment to the Kaos interface and drop RuntimeConfig.osEnv.
  • LocalKaos.create() returns a fresh instance per call (no singleton); add KaosShellNotFoundError and rewrap it back to KimiError(SHELL_GIT_BASH_NOT_FOUND) in core-impl.ts.
  • Remove dead kaos API surface that no consumer used: KaosPath and its tests, the AsyncLocalStorage helpers (setCurrentKaos/resetCurrentKaos/KaosToken), and assorted cleanup; detectEnvironmentFromNode is now memoised.

Test plan

  • pnpm typecheck in packages/kaos, packages/agent-core, packages/migration-legacy, apps/kimi-code
  • pnpm vitest run — kaos 243, agent-core 1870, migration-legacy 172, apps/kimi-code 965 passing
  • Sanity-check on Windows that detectEnvironmentFromNode still resolves Git Bash (cache path is identical to old behaviour)

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.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 28, 2026

⚠️ No Changeset found

Latest commit: 61a7eac

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 28, 2026

pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@61a7eac
npx https://pkg.pr.new/@moonshot-ai/kimi-code@61a7eac

commit: 61a7eac

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread packages/kaos/src/ssh.ts
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@kermanx kermanx merged commit 36add70 into main May 28, 2026
6 checks passed
@kermanx kermanx deleted the refactor/kaos-environment-and-cleanup branch May 28, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant