kimi-code/packages/node-sdk/test/session-goal.test.ts
Haozhe 2f97917bb5
Some checks are pending
CI / typecheck (push) Waiting to run
CI / lint (push) Waiting to run
CI / test-windows (push) Waiting to run
CI / build (push) Waiting to run
CI / test (push) Waiting to run
CI / test-pi-tui (push) Waiting to run
Nix Build / Check flake.nix workspace sync (push) Waiting to run
Nix Build / nix build .#kimi-code (push) Blocked by required conditions
Release / Publish native release assets (push) Blocked by required conditions
Release / Release (push) Waiting to run
Release / Deploy docs (push) Blocked by required conditions
Release / Native release artifact (push) Blocked by required conditions
Release / Desktop release artifact (push) Blocked by required conditions
feat(cli): keep kimi -p running while a goal is active or cron tasks are pending (#1555)
A kimi -p run settled the moment the main agent's turn ended (end_turn),
so a goal created mid-run was cancelled during cleanup and a scheduled
cron task never fired in the same run.

- runPromptTurn now re-evaluates completion when the main agent goes idle
  and stays alive while a goal is still active (the goal driver runs the
  continuation turns) or while cron tasks with a future fire remain (their
  fire steers a fresh turn). A ref'd handle keeps the event loop alive
  during the wait since the cron scheduler tick is unref'd.
- a terminal goal.updated (e.g. the driver blocking a goal on a hard
  budget, which emits no further turn.ended) also re-evaluates so the run
  cannot hang.
- add getCronTasks RPC and Session.getCronTasks() so the print flow can
  enumerate pending cron tasks.
2026-07-11 21:16:27 +08:00

70 lines
2.5 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import { Session } from '#/session';
import type { SDKRpcClientBase } from '#/rpc';
function makeSession() {
const rpc = {
createGoal: vi.fn(async () => ({ goalId: 'g1' })),
getGoal: vi.fn(async () => ({ goal: null })),
pauseGoal: vi.fn(async () => ({ goalId: 'g1' })),
resumeGoal: vi.fn(async () => ({ goalId: 'g1' })),
cancelGoal: vi.fn(async () => ({ goalId: 'g1' })),
getCronTasks: vi.fn(async () => ({ tasks: [] })),
clearSessionHandlers: vi.fn(),
} as unknown as SDKRpcClientBase;
const session = new Session({ id: 'ses_goal', workDir: '/tmp/work', rpc });
return { session, rpc };
}
describe('Session goal methods', () => {
it('createGoal forwards the supported payload with sessionId', async () => {
const { session, rpc } = makeSession();
await session.createGoal({
objective: 'Ship feature X',
replace: true,
});
expect(rpc.createGoal).toHaveBeenCalledWith({
sessionId: 'ses_goal',
objective: 'Ship feature X',
replace: true,
});
});
it('getGoal forwards sessionId', async () => {
const { session, rpc } = makeSession();
await session.getGoal();
expect(rpc.getGoal).toHaveBeenCalledWith({ sessionId: 'ses_goal' });
});
it('pauseGoal forwards sessionId', async () => {
const { session, rpc } = makeSession();
await session.pauseGoal();
expect(rpc.pauseGoal).toHaveBeenCalledWith({ sessionId: 'ses_goal' });
});
it('resumeGoal forwards sessionId', async () => {
const { session, rpc } = makeSession();
await session.resumeGoal();
expect(rpc.resumeGoal).toHaveBeenCalledWith({ sessionId: 'ses_goal' });
});
it('cancelGoal forwards sessionId', async () => {
const { session, rpc } = makeSession();
await session.cancelGoal();
expect(rpc.cancelGoal).toHaveBeenCalledWith({ sessionId: 'ses_goal' });
});
it('getCronTasks forwards sessionId and returns the task list', async () => {
const { session, rpc } = makeSession();
const result = await session.getCronTasks();
expect(rpc.getCronTasks).toHaveBeenCalledWith({ sessionId: 'ses_goal' });
expect(result).toEqual({ tasks: [] });
});
it('does not expose a public clearGoal or updateGoal method', () => {
const { session } = makeSession();
expect((session as unknown as { clearGoal?: unknown }).clearGoal).toBeUndefined();
expect((session as unknown as { updateGoal?: unknown }).updateGoal).toBeUndefined();
});
});