mirror of
https://github.com/MoonshotAI/kimi-code.git
synced 2026-07-10 01:39:25 +00:00
fix: clean up background tasks on session exit (#641)
* fix: clean up background tasks on session exit * Stop background tasks on session close Change versioning for agent-core and kimi-code to patch. Signed-off-by: 7Sageer <12210216@mail.sustech.edu.cn> --------- Signed-off-by: 7Sageer <12210216@mail.sustech.edu.cn>
This commit is contained in:
parent
71f5926d0e
commit
30459af6ab
7 changed files with 34 additions and 7 deletions
6
.changeset/stop-background-tasks-on-exit.md
Normal file
6
.changeset/stop-background-tasks-on-exit.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
"@moonshot-ai/agent-core": patch
|
||||
"@moonshot-ai/kimi-code": patch
|
||||
---
|
||||
|
||||
Stop background tasks by default when sessions close.
|
||||
|
|
@ -169,7 +169,7 @@ You can also switch models temporarily without touching the config file — by s
|
|||
| Field | Type | Default | Description |
|
||||
| --- | --- | --- | --- |
|
||||
| `max_running_tasks` | `integer` | — | Maximum number of background tasks running concurrently |
|
||||
| `keep_alive_on_exit` | `boolean` | `true` | Whether to keep still-running background tasks when the session closes. Set to `false` to request that all background tasks stop before the process exits |
|
||||
| `keep_alive_on_exit` | `boolean` | `false` | Whether to keep still-running background tasks when the session closes. By default, Kimi Code requests that all background tasks stop before the process exits; set this to `true` only when you want tasks to outlive the session |
|
||||
|
||||
`keep_alive_on_exit` can be overridden by the `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` environment variable, which takes higher priority than `config.toml`.
|
||||
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ Switches that control the behavior of subsystems such as telemetry, background t
|
|||
| Variable | Purpose | Valid values |
|
||||
| --- | --- | --- |
|
||||
| `KIMI_DISABLE_TELEMETRY` | Disable anonymous telemetry reporting | `1`, `true`, `yes`, `y` (case-insensitive) |
|
||||
| `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` | Whether to keep background tasks when the session closes; takes higher priority than `config.toml` | Truthy: `1`/`true`/`yes`/`on`; falsy: `0`/`false`/`no`/`off` |
|
||||
| `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` | Whether to keep background tasks when the session closes; takes higher priority than `config.toml`. The default is to stop them on exit | Truthy: `1`/`true`/`yes`/`on`; falsy: `0`/`false`/`no`/`off` |
|
||||
| `KIMI_CODE_PLUGIN_MARKETPLACE_URL` | Override the plugin marketplace JSON loaded by `/plugins` | URL or local path |
|
||||
| `KIMI_CODE_EXPERIMENTAL_FLAG` | Enable all registered experimental features for this process; `micro_compaction` is already enabled by default | `1`, `true`, `yes`, `on` |
|
||||
| `KIMI_CODE_EXPERIMENTAL_MICRO_COMPACTION` | Override [`[experimental].micro_compaction`](./config-files.md#experimental) for this process | Truthy or falsy |
|
||||
|
|
|
|||
|
|
@ -169,7 +169,7 @@ max_context_size = 1047576
|
|||
| 字段 | 类型 | 默认值 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `max_running_tasks` | `integer` | — | 同时运行的最大后台任务数 |
|
||||
| `keep_alive_on_exit` | `boolean` | `true` | 会话关闭时是否保留仍在运行的后台任务。设为 `false` 时,进程退出前会请求停止所有后台任务 |
|
||||
| `keep_alive_on_exit` | `boolean` | `false` | 会话关闭时是否保留仍在运行的后台任务。默认情况下,Kimi Code 会在进程退出前请求停止所有后台任务;只有希望任务在会话结束后继续运行时才设为 `true` |
|
||||
|
||||
`keep_alive_on_exit` 可被环境变量 `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` 覆盖,优先级高于配置文件。
|
||||
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ kimi
|
|||
| 环境变量 | 用途 | 合法值 |
|
||||
| --- | --- | --- |
|
||||
| `KIMI_DISABLE_TELEMETRY` | 关闭匿名遥测上报 | `1`、`true`、`yes`、`y`(不区分大小写) |
|
||||
| `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` | 会话关闭时是否保留后台任务,优先级高于 `config.toml` | 真值:`1`/`true`/`yes`/`on`;假值:`0`/`false`/`no`/`off` |
|
||||
| `KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT` | 会话关闭时是否保留后台任务,优先级高于 `config.toml`。默认会在退出时停止后台任务 | 真值:`1`/`true`/`yes`/`on`;假值:`0`/`false`/`no`/`off` |
|
||||
| `KIMI_CODE_PLUGIN_MARKETPLACE_URL` | 替换 `/plugins` 加载的 marketplace JSON | URL 或本地路径 |
|
||||
| `KIMI_CODE_EXPERIMENTAL_FLAG` | 在当前进程启用所有已注册的实验功能;`micro_compaction` 已默认开启 | `1`、`true`、`yes`、`on` |
|
||||
| `KIMI_CODE_EXPERIMENTAL_MICRO_COMPACTION` | 覆盖当前进程的 [`[experimental].micro_compaction`](./config-files.md#experimental) | 真值或假值 |
|
||||
|
|
|
|||
|
|
@ -264,7 +264,7 @@ export class Session {
|
|||
env: process.env,
|
||||
envKey: BACKGROUND_KEEP_ALIVE_ON_EXIT_ENV,
|
||||
configValue: this.options.background?.keepAliveOnExit,
|
||||
defaultValue: true,
|
||||
defaultValue: false,
|
||||
parseEnv: parseBooleanEnv,
|
||||
});
|
||||
if (keepAliveOnExit) return;
|
||||
|
|
|
|||
|
|
@ -110,7 +110,7 @@ describe('Session lifecycle hooks', () => {
|
|||
await expect(session.close()).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('stops background tasks on close when keepAliveOnExit is false', async () => {
|
||||
it('stops background tasks on close by default', async () => {
|
||||
const { sessionDir, workDir } = await hookFixture();
|
||||
const session = new Session({
|
||||
kaos: testKaos.withCwd(workDir),
|
||||
|
|
@ -118,7 +118,6 @@ describe('Session lifecycle hooks', () => {
|
|||
homedir: sessionDir,
|
||||
rpc: createSessionRpc(),
|
||||
skills: { explicitDirs: [join(workDir, 'missing-skills')] },
|
||||
background: { keepAliveOnExit: false },
|
||||
});
|
||||
const agent = await session.createMain();
|
||||
const { proc, killSpy } = pendingProcess();
|
||||
|
|
@ -132,6 +131,28 @@ describe('Session lifecycle hooks', () => {
|
|||
expect(agent.background.getTask(taskId)?.status).toBe('killed');
|
||||
});
|
||||
|
||||
it('keeps background tasks alive on close when keepAliveOnExit is true', async () => {
|
||||
const { sessionDir, workDir } = await hookFixture();
|
||||
const session = new Session({
|
||||
kaos: testKaos.withCwd(workDir),
|
||||
id: 'session-bg-keepalive',
|
||||
homedir: sessionDir,
|
||||
rpc: createSessionRpc(),
|
||||
skills: { explicitDirs: [join(workDir, 'missing-skills')] },
|
||||
background: { keepAliveOnExit: true },
|
||||
});
|
||||
const agent = await session.createMain();
|
||||
const { proc, killSpy } = pendingProcess();
|
||||
const taskId = agent.background.registerTask(
|
||||
new ProcessBackgroundTask(proc, 'sleep 60', 'keep alive'),
|
||||
);
|
||||
|
||||
await session.close();
|
||||
|
||||
expect(killSpy).not.toHaveBeenCalled();
|
||||
expect(agent.background.getTask(taskId)?.status).toBe('running');
|
||||
});
|
||||
|
||||
it('lets the environment override config when deciding background task cleanup', async () => {
|
||||
vi.stubEnv('KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT', '0');
|
||||
const { sessionDir, workDir } = await hookFixture();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue