mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-22 04:17:05 +00:00
feat(opencode): open a fresh session on clear (#1300)
* test(opencode): specify fresh-session clear * feat(opencode): open a fresh session on clear * fix(opencode): release clear latch on cancel * fix(opencode): retry transient clear handoffs * fix(opencode): confirm clear archive delivery * fix(web): preserve superseded session access * fix(clear): invalidate transferred schedules * fix(runner): restore live spawn dedupe * fix(clear): preserve latched scheduled prompts * fix(runner): quarantine unverified children * fix(clear): retain handoff retry ownership * fix(runner): release recovered spawn dedupe * fix(clear): retain archive retry ownership * fix(clear): settle rejected immediate prompts * fix(clear): block reopening replaced sources * fix(clear): settle prompts when clear is cancelled * fix(clear): make fresh-session handoff durable * fix(clear): finalize only after native cleanup * fix(clear): abort failed native handoffs * fix(clear): gate recovery on cleanup proof * fix(clear): retry metadata persistence failures * fix(clear): preserve handoff ownership through teardown * fix(clear): abort incomplete cleanup reservations * fix(clear): require explicit exit before abort * fix(clear): verify owner exit before recovery * fix(clear): guard recovery handoff races * fix(clear): serialize cleanup callbacks * fix(clear): make callback retries idempotent * fix(clear): bind callbacks to reservations * fix(clear): recover pending spawns * fix(clear): deduplicate held prompts * fix(clear): validate redirect ownership * fix(clear): replay prompts in FIFO order * fix(clear): gate replacement delivery
This commit is contained in:
@@ -16,6 +16,8 @@ import { formatMessageWithAttachments } from '@/utils/attachmentFormatter';
|
||||
import { getInvokedCwd } from '@/utils/invokedCwd';
|
||||
import { listSlashCommands } from '@/modules/common/slashCommands';
|
||||
import { resolveOpencodeSlashCommand } from './utils/slashCommands';
|
||||
import { isRetryableConnectionError } from '@/utils/errorUtils';
|
||||
import { withRetry } from '@/utils/time';
|
||||
|
||||
export async function runOpencode(opts: {
|
||||
startedBy?: 'runner' | 'terminal';
|
||||
@@ -108,6 +110,14 @@ export async function runOpencode(opts: {
|
||||
// onLeavingRemote exists to protect, since `mode` stays 'remote'
|
||||
// throughout it.
|
||||
let compactSupported = false;
|
||||
let clearRequested = false;
|
||||
let clearReplacementSessionId: string | null = null;
|
||||
// Once a runner-backed /clear is accepted, hold later payloads until the
|
||||
// transition commits or the queued clear is cancelled. The hub redirects
|
||||
// their durable rows to the reserved replacement on success.
|
||||
let clearTransitionLatched = false;
|
||||
let queuedClearLocalId: string | null = null;
|
||||
const heldDuringClear: Array<{ message: Parameters<Parameters<typeof session.onUserMessage>[0]>[0]; localId?: string }> = [];
|
||||
// True from the moment onCompactAvailabilityChange(false) fires (which,
|
||||
// per onLeavingRemote's contract, only ever happens because remote mode
|
||||
// is being left — never because remote just started) until this session
|
||||
@@ -236,6 +246,11 @@ export async function runOpencode(opts: {
|
||||
};
|
||||
try {
|
||||
if (wasCancelled()) return;
|
||||
if (clearTransitionLatched) {
|
||||
heldDuringClear.push({ message, localId });
|
||||
sessionWrapperRef.current?.onThinkingChange(false);
|
||||
return;
|
||||
}
|
||||
let text = message.content.text;
|
||||
const commands = await listSlashCommands('opencode', workingDirectory).catch(() => []);
|
||||
if (wasCancelled()) return;
|
||||
@@ -246,6 +261,30 @@ export async function runOpencode(opts: {
|
||||
modelReasoningEffort: sessionModelReasoningEffort
|
||||
});
|
||||
|
||||
if (slash.kind === 'clear') {
|
||||
if (startedBy !== 'runner') {
|
||||
if (localId) {
|
||||
session.emitMessagesConsumed([localId], { clearQueuedThinkingGrace: true });
|
||||
}
|
||||
session.sendAgentMessage({
|
||||
type: 'message',
|
||||
message: '/clear is available only for runner-backed OpenCode sessions.',
|
||||
id: randomUUID()
|
||||
});
|
||||
sessionWrapperRef.current?.onThinkingChange(false);
|
||||
return;
|
||||
}
|
||||
// Latch before enqueueing. userMessageChain serializes later
|
||||
// messages behind this resolver, including the async
|
||||
// listSlashCommands race, so they take the rejection path.
|
||||
clearTransitionLatched = true;
|
||||
queuedClearLocalId = localId ?? null;
|
||||
// A clear is isolated but retains its FIFO position:
|
||||
// older prompts and native /compact work finish first.
|
||||
messageQueue.pushIsolated('', { ...buildMode(), operation: 'clear' }, localId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (slash.kind === 'compact') {
|
||||
// `compactSupported` alone conflates two different
|
||||
// situations: a genuinely local-mode session (compact
|
||||
@@ -399,6 +438,19 @@ export async function runOpencode(opts: {
|
||||
session.onCancelQueuedMessage((localId) => {
|
||||
const removedFromQueue = messageQueue.cancelByLocalId(localId);
|
||||
if (removedFromQueue) {
|
||||
if (queuedClearLocalId === localId) {
|
||||
queuedClearLocalId = null;
|
||||
clearTransitionLatched = false;
|
||||
for (const held of heldDuringClear) {
|
||||
const formattedText = formatMessageWithAttachments(held.message.content.text, held.message.content.attachments);
|
||||
messageQueue.push(formattedText, {
|
||||
permissionMode: currentPermissionMode,
|
||||
model: sessionModel,
|
||||
modelReasoningEffort: sessionModelReasoningEffort
|
||||
}, held.localId);
|
||||
}
|
||||
heldDuringClear.length = 0;
|
||||
}
|
||||
logger.debug(`[opencode] cancelByLocalId(${localId}): removed from queue`);
|
||||
return true;
|
||||
}
|
||||
@@ -407,6 +459,11 @@ export async function runOpencode(opts: {
|
||||
logger.debug(`[opencode] cancelByLocalId(${localId}): marked for cancellation before enqueue`);
|
||||
return true;
|
||||
}
|
||||
const heldIndex = heldDuringClear.findIndex((held) => held.localId === localId);
|
||||
if (heldIndex >= 0) {
|
||||
heldDuringClear.splice(heldIndex, 1);
|
||||
return true;
|
||||
}
|
||||
// Not in the queue and not in the pre-enqueue preparing window. As
|
||||
// explained where `cancelledDequeuedLocalIds` is declared above, the
|
||||
// hub only calls this at all while its own row is still queued, so
|
||||
@@ -491,6 +548,24 @@ export async function runOpencode(opts: {
|
||||
compactTeardownInProgress = true;
|
||||
}
|
||||
},
|
||||
onClearRequested: async () => {
|
||||
clearReplacementSessionId = await withRetry(() => api.reserveOpenCodeClearSession(session.sessionId), {
|
||||
minDelay: 500, maxDelay: 30_000, shouldRetry: isRetryableConnectionError
|
||||
});
|
||||
},
|
||||
onClearCleanupComplete: async () => {
|
||||
if (!clearReplacementSessionId) throw new Error('OpenCode clear cleanup completed without a reservation')
|
||||
await withRetry(() => api.confirmOpenCodeClearCleanup(session.sessionId, clearReplacementSessionId!), {
|
||||
minDelay: 500, maxDelay: 30_000, shouldRetry: isRetryableConnectionError
|
||||
});
|
||||
clearRequested = true;
|
||||
},
|
||||
onClearCleanupFailed: async () => {
|
||||
if (!clearReplacementSessionId) throw new Error('OpenCode clear cleanup failed without a reservation')
|
||||
await withRetry(() => api.abortOpenCodeClearSession(session.sessionId, clearReplacementSessionId!), {
|
||||
minDelay: 500, maxDelay: 30_000, shouldRetry: isRetryableConnectionError
|
||||
});
|
||||
},
|
||||
isLocalIdCancelled: (localId) => cancelledDequeuedLocalIds.delete(localId)
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -499,13 +574,55 @@ export async function runOpencode(opts: {
|
||||
logger.debug('[opencode] Loop error:', error);
|
||||
} finally {
|
||||
const localFailure = sessionWrapperRef.current?.localLaunchFailure;
|
||||
if (localFailure?.exitReason === 'exit') {
|
||||
if (clearRequested) {
|
||||
lifecycle.setArchiveReason('Cleared by /clear');
|
||||
lifecycle.setSessionEndReason('cleared');
|
||||
} else if (localFailure?.exitReason === 'exit') {
|
||||
lifecycle.setExitCode(1);
|
||||
lifecycle.setArchiveReason(`Local launch failed: ${localFailure.message.slice(0, 200)}`);
|
||||
lifecycle.setSessionEndReason('error');
|
||||
} else if (!crashed) {
|
||||
lifecycle.setSessionEndReason('completed');
|
||||
}
|
||||
await lifecycle.cleanupAndExit();
|
||||
if (!clearRequested) {
|
||||
await lifecycle.cleanupAndExit();
|
||||
return;
|
||||
}
|
||||
|
||||
// Keep the source socket open until the hub acknowledges the ordered
|
||||
// archive/session-end boundary. A transient disconnect must not turn
|
||||
// the following clear request into a non-retryable active-source 409.
|
||||
await withRetry(
|
||||
() => lifecycle.cleanupConfirmed({ timeoutMs: 5_000 }),
|
||||
{
|
||||
minDelay: 500,
|
||||
maxDelay: 30_000,
|
||||
shouldRetry: isRetryableConnectionError,
|
||||
onRetry: (error, attempt, nextDelayMs) => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
logger.debug(`[opencode] Session archive confirmation failed (attempt ${attempt}), retrying in ${nextDelayMs}ms: ${message}`);
|
||||
}
|
||||
}
|
||||
);
|
||||
try {
|
||||
await withRetry(
|
||||
() => api.clearOpenCodeSession(session.sessionId),
|
||||
{
|
||||
minDelay: 500,
|
||||
maxDelay: 30_000,
|
||||
shouldRetry: isRetryableConnectionError,
|
||||
onRetry: (error, attempt, nextDelayMs) => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
logger.debug(`[opencode] Fresh-session clear handoff failed (attempt ${attempt}), retrying in ${nextDelayMs}ms: ${message}`);
|
||||
}
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
// Only non-retryable failures reach here. Retryable transport and
|
||||
// hub failures retain ownership in the loop above until recovery.
|
||||
logger.debug('[opencode] Fresh-session clear spawn failed', error);
|
||||
throw error;
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user