Files
hapi/cli/src/claude/loop.ts
T
bb4b136f60 feat(cli): switch Claude effort without restarting the process (#2262)
* feat(cli): switch Claude effort without restarting the process

Changing effort in a remote Claude session made the next prompt restart
Claude with --resume and a new --effort flag, so the switch cost a
process start and a slower first reply. Claude Code's apply_flag_settings
control request changes the session effort in place (null restores the
model default), the same way set_permission_mode already changes modes.

The launcher now applies an effort-only change to the running process
before delivering the prompt. Any other process-level change, a refused
request, or no reply within 5 seconds keeps the existing restart path,
so Claude Code builds without the request still work.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): keep the prompt when Claude exits during a live effort change

The launcher awaited the effort control request with the dequeued batch
held only in a local. If Claude exited before replying, the attempt
ended, and the late continuation handed the batch to the closed
claudeRemote, which dropped it.

Park the batch in `pending` before awaiting and take it back only when
Claude applied the change and its process is still open. Otherwise the
next attempt delivers it, starting with the new effort.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: HAPI <noreply@hapi.run>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 18:00:53 +08:00

101 lines
3.4 KiB
TypeScript

import { ApiSessionClient } from "@/api/apiSession"
import { MessageQueue2 } from "@/utils/MessageQueue2"
import { logger } from "@/ui/logger"
import { runLocalRemoteSession } from "@/agent/loopBase"
import { Session } from "./session"
import { claudeLocalLauncher } from "./claudeLocalLauncher"
import { claudeRemoteLauncher } from "./claudeRemoteLauncher"
import { ApiClient } from "@/lib"
import type { SessionEffort, SessionModel } from "@/api/types"
import type { ClaudePermissionMode } from "@hapi/protocol/types"
export type PermissionMode = ClaudePermissionMode;
export interface EnhancedMode {
permissionMode: PermissionMode;
model?: string;
effort?: string;
fallbackModel?: string;
customSystemPrompt?: string;
appendSystemPrompt?: string;
allowedTools?: string[];
disallowedTools?: string[];
}
/**
* Mode fields that only take effect in a fresh Claude process. 'plan' and
* 'auto' are enforced inside Claude itself (not emulated via canCallTool like
* acceptEdits/bypassPermissions), so switching to/from them needs a matching
* --permission-mode flag. Effort is left out: it can change live.
*/
export function claudeProcessModeKey(mode: EnhancedMode) {
return {
agentEnforcedMode: mode.permissionMode === 'plan' || mode.permissionMode === 'auto' ? mode.permissionMode : null,
model: mode.model,
fallbackModel: mode.fallbackModel,
customSystemPrompt: mode.customSystemPrompt,
appendSystemPrompt: mode.appendSystemPrompt,
allowedTools: mode.allowedTools,
disallowedTools: mode.disallowedTools
};
}
interface LoopOptions {
path: string
model?: SessionModel
effort?: SessionEffort
permissionMode?: PermissionMode
startingMode?: 'local' | 'remote'
startedBy?: 'runner' | 'terminal'
onModeChange: (mode: 'local' | 'remote') => void
mcpServers: Record<string, any>
session: ApiSessionClient
api: ApiClient,
claudeEnvVars?: Record<string, string>
claudeArgs?: string[]
messageQueue: MessageQueue2<EnhancedMode>
allowedTools?: string[]
onSessionReady?: (session: Session) => void
hookSettingsPath: string
localHookSettingsPath?: string
resumeSessionId?: string
}
export async function loop(opts: LoopOptions) {
// Get log path for debug display
const logPath = logger.logFilePath;
const startedBy = opts.startedBy ?? 'terminal';
const startingMode = opts.startingMode ?? 'local';
const session = new Session({
api: opts.api,
client: opts.session,
path: opts.path,
sessionId: opts.resumeSessionId ?? null,
claudeEnvVars: opts.claudeEnvVars,
claudeArgs: opts.claudeArgs,
mcpServers: opts.mcpServers,
logPath: logPath,
messageQueue: opts.messageQueue,
allowedTools: opts.allowedTools,
onModeChange: opts.onModeChange,
mode: startingMode,
startedBy,
startingMode,
hookSettingsPath: opts.hookSettingsPath,
localHookSettingsPath: opts.localHookSettingsPath,
permissionMode: opts.permissionMode ?? 'default',
model: opts.model,
effort: opts.effort
});
await runLocalRemoteSession({
session,
startingMode: opts.startingMode,
logTag: 'loop',
runLocal: claudeLocalLauncher,
runRemote: claudeRemoteLauncher,
onSessionReady: opts.onSessionReady
});
}