Files
hapi/cli/src/opencode/loop.ts
T
Junmo KimandGitHub c4847b241e feat(opencode): support mid-turn steering (#1981)
* feat(opencode): support mid-turn steering

Register the per-message Queue/Steer RPC on the OpenCode remote launcher so
a waiting-queue row can be delivered into the active turn as a concurrent
ACP session/prompt (soft inject, no cancel, no mode/model/effort mutation),
reusing the existing reservation/delivery-state protocol shared with Codex
and Cursor ACP.

The steer gate is closed synchronously by every cancelling or terminal path
(normal-prompt finally, abort, switch/exit, teardown, stderr stall cancel),
each steer operation is tracked from the moment its reservation is taken
until completion plus all restore/consumed bookkeeping settles, and the
foreground prompt drains those operations — abort-aware through the original
turn signal — before ready, thinking=false or the next batch. steerability is
published through the existing agentState.steeringActive flag so the web's
queued Steer button is reachable.

Gate the feature on the shared steering flavors (now including opencode) and
update the hub's supported-flavor wording.

* fix(opencode): acknowledge native steering acceptance

* fix(opencode): preserve steering delivery outcomes

* docs(opencode): remove obsolete abort-race comment

* fix(opencode): preserve steering resolution ownership

* fix(opencode): isolate ambiguous steering attempts

* fix(opencode): compare resolved steering settings

* fix(opencode): reconcile steering receipts after dispatch

* fix(opencode): distinguish pending steering delivery outcomes

* fix(opencode): restore receipts after successful native responses

Track native input requests separately from detached steer operations. Resume receipt correlation after inputs quarantined by cancellation return their own successful responses, while keeping uncertain writes on completion-based confirmation.

* fix(opencode): bound receipt proof to unambiguous native inputs

* feat(opencode): steer v2 inputs through native inbox

* fix(opencode): preserve v2 restoration and cancellation ownership

* docs(opencode): describe v2 inbox steering confirmation

* fix(steer): expire Claude confirmation with its owning worker
2026-11-03 18:37:53 +08:00

99 lines
4.4 KiB
TypeScript

import type { OpencodeSteerCancelHandler } from './utils/opencodeV2Steer';
import { MessageQueue2 } from '@/utils/MessageQueue2';
import { logger } from '@/ui/logger';
import { runLocalRemoteSession } from '@/agent/loopBase';
import { OpencodeSession } from './session';
import { opencodeLocalLauncher } from './opencodeLocalLauncher';
import { opencodeRemoteLauncher } from './opencodeRemoteLauncher';
import { ApiClient, ApiSessionClient } from '@/lib';
import type { OpencodeMode, PermissionMode } from './types';
import type { OpencodeHookServer } from './utils/startOpencodeHookServer';
interface OpencodeLoopOptions {
path: string;
startingMode?: 'local' | 'remote';
startedBy?: 'runner' | 'terminal';
onModeChange: (mode: 'local' | 'remote') => void;
messageQueue: MessageQueue2<OpencodeMode>;
session: ApiSessionClient;
api: ApiClient;
permissionMode?: PermissionMode;
model?: string;
modelReasoningEffort?: string | null;
resumeSessionId?: string;
hookServer: OpencodeHookServer;
hookUrl: string;
onSessionReady?: (session: OpencodeSession) => void;
onModelRollback?: (model: string | null) => void;
onReasoningEffortRollback?: (effort: string | null) => void;
onSteerCancelHandlerChange?: (handler: OpencodeSteerCancelHandler | null) => void;
onCompactAvailabilityChange?: (available: boolean) => void;
onClearRequested?: () => Promise<void>;
onClearCleanupComplete?: () => Promise<void>;
onClearCleanupFailed?: () => Promise<void>;
// Consumes (delete-and-return) whether the given localId was cancelled
// after already being dequeued — needed because a queued /compact can
// still be running (its REST call can take minutes) by the time a
// cancel arrives, well past the point `messageQueue.cancelByLocalId`
// can do anything about it.
isLocalIdCancelled?: (localId: string) => boolean;
}
export async function opencodeLoop(opts: OpencodeLoopOptions): Promise<void> {
const logPath = logger.getLogPath();
const startedBy = opts.startedBy ?? 'terminal';
const startingMode = opts.startingMode ?? 'local';
const session = new OpencodeSession({
api: opts.api,
client: opts.session,
path: opts.path,
sessionId: opts.resumeSessionId ?? null,
logPath,
messageQueue: opts.messageQueue,
onModeChange: opts.onModeChange,
mode: startingMode,
startedBy,
startingMode,
permissionMode: opts.permissionMode ?? 'default',
modelReasoningEffort: opts.modelReasoningEffort
});
if (opts.resumeSessionId) {
session.onSessionFound(opts.resumeSessionId);
}
await runLocalRemoteSession({
session,
startingMode: opts.startingMode,
logTag: 'opencode-loop',
// /compact only exists in remote mode (it needs the ACP backend +
// internal HTTP baseUrl that only opencodeRemoteLauncher owns).
// Availability is reset to false as part of *leaving* remote mode,
// not on *entering* local mode — see OpencodeRemoteLauncher's
// onLeavingRemote() override — so it's already false by the time
// runLocal below ever runs; no reset needed here. That decoupling is
// deliberate: resetting on local-entry left a window between "a
// switch/exit was requested" and "the next runLocal() call actually
// happened" where availability was still stale-true, which a
// PR-review round found could let a /compact queued in that window
// run anyway once local mode bounced straight back to remote to
// drain a non-empty queue.
runLocal: (instance) => opencodeLocalLauncher(instance, {
hookServer: opts.hookServer,
hookUrl: opts.hookUrl
}),
runRemote: (instance) => opencodeRemoteLauncher(instance, {
onModelRollback: opts.onModelRollback,
onReasoningEffortRollback: opts.onReasoningEffortRollback,
onCompactAvailabilityChange: opts.onCompactAvailabilityChange,
onSteerCancelHandlerChange: opts.onSteerCancelHandlerChange,
isLocalIdCancelled: opts.isLocalIdCancelled,
onClearRequested: opts.onClearRequested,
onClearCleanupComplete: opts.onClearCleanupComplete,
onClearCleanupFailed: opts.onClearCleanupFailed
}),
onSessionReady: opts.onSessionReady
});
}