feat: replace session commands with a natural-language stop

Remove the /compact, /clear and /stop session commands. They were gated
to the main room or SESSION_COMMAND_ALLOWED_SENDERS, neither of which
exists in this deployment, so nobody could use them. The agent-runner
/compact passthrough goes with them.

Instead, a short human message whose whole intent is "stop" (그만, 멈춰,
중단해, 하지 마, 취소, stop, cancel, ...; see src/stop-intent.ts) stops the
room when it has something to stop: a running or queued run, an open
paired task, or unprocessed human messages before it. The message loop
then
- advances the room cursors past the stop message (memory + router_state)
- completes the open paired task as user_stopped, cancels its open turns
  and drops its execution lease
- closes stdin with a user-stop reason and kills the agent through the
  existing GroupQueue.closeStdin/killProcess
- replies "진행 중이던 작업을 중단했습니다." once
When nothing is running the message is handled as a normal message.

The user-stop close reason is treated like a human interruption, so the
killed paired turn releases its lease without counting a failure and
queues no follow-up, and the turn controller does not post the generic
failure text on top of the stop reply.

advanceLastAgentCursor no longer moves a cursor backwards, so a run that
finishes after the stop cannot rewind the cursor and re-dispatch the
stop message (the respawn seen when an agent was only killed).

Stale SESSION_COMMAND_* keys in .env are now ignored instead of failing
startup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-01 19:26:10 +09:00
parent fe7c5a7b23
commit ce4547daa6
40 changed files with 949 additions and 1084 deletions

View File

@@ -40,7 +40,6 @@ import {
writeOutput,
} from './output-protocol.js';
import { buildClaudeSdkEnv } from './sdk-env.js';
import { runSessionCommand } from './session-command.js';
import { runClaudeQuery } from './claude-query-runner.js';
import type { RunnerInput } from './runner-input.js';
@@ -124,9 +123,6 @@ async function main(): Promise<void> {
/* ignore */
}
// Effective working directory (WORK_DIR overrides GROUP_DIR)
const mainEffectiveCwd = WORK_DIR || GROUP_DIR;
// Build initial prompt (drain any pending IPC messages too)
let prompt = runnerInput.prompt;
if (runnerInput.isScheduledTask) {
@@ -137,35 +133,7 @@ async function main(): Promise<void> {
log(`Draining ${pending.length} pending IPC messages into initial prompt`);
prompt += '\n' + pending.join('\n');
}
// --- Slash command handling ---
// Check BEFORE prepending room role header so /compact isn't masked.
const KNOWN_SESSION_COMMANDS = new Set(['/compact']);
const isSessionSlashCommand = KNOWN_SESSION_COMMANDS.has(
runnerInput.prompt.trim(),
);
if (!isSessionSlashCommand) {
prompt = prependRoomRoleHeader(prompt, runnerInput.roomRoleContext);
}
const trimmedPrompt = prompt.trim();
if (isSessionSlashCommand) {
log(`Handling session command: ${trimmedPrompt}`);
await runSessionCommand({
prompt: trimmedPrompt,
sessionId,
cwd: mainEffectiveCwd,
sdkEnv: guardedSdkEnv,
abortController: agentAbortController,
assistantName: runnerInput.assistantName,
groupDir: GROUP_DIR,
groupFolder: GROUP_FOLDER,
hostTasksDir: HOST_TASKS_DIR,
log,
});
return;
}
// --- End slash command handling ---
prompt = prependRoomRoleHeader(prompt, runnerInput.roomRoleContext);
try {
log(`Starting query (session: ${sessionId || 'new'})...`);

View File

@@ -1,151 +0,0 @@
import { query } from '@anthropic-ai/claude-agent-sdk';
import { compactBoundaryFromMessage } from './compaction-boundary.js';
import { getClaudeCliPath } from './claude-cli.js';
import { createPreCompactHook } from './runner-hooks.js';
import {
buildCompactionOutput,
type RunnerCompaction,
writeOutput,
} from './output-protocol.js';
export interface RunSessionCommandArgs {
prompt: string;
sessionId?: string;
cwd: string;
sdkEnv: Record<string, string | undefined>;
abortController: AbortController;
assistantName?: string;
groupDir: string;
groupFolder: string;
hostTasksDir: string;
log: (message: string) => void;
}
export async function runSessionCommand({
prompt,
sessionId,
cwd,
sdkEnv,
abortController,
assistantName,
groupDir,
groupFolder,
hostTasksDir,
log,
}: RunSessionCommandArgs): Promise<void> {
let slashSessionId: string | undefined;
let compactBoundarySeen = false;
let slashCompaction: RunnerCompaction | undefined;
let hadError = false;
let resultEmitted = false;
try {
for await (const message of query({
prompt,
options: {
pathToClaudeCodeExecutable: getClaudeCliPath(log),
cwd,
resume: sessionId,
systemPrompt: undefined,
allowedTools: [],
env: sdkEnv,
permissionMode: 'bypassPermissions' as const,
allowDangerouslySkipPermissions: true,
settingSources: ['project', 'user'] as const,
abortController,
hooks: {
PreCompact: [
{
hooks: [
createPreCompactHook({
assistantName,
groupDir,
groupFolder,
hostTasksDir,
log,
writeOutput,
}),
],
},
],
},
},
})) {
const msgType =
message.type === 'system'
? `system/${(message as { subtype?: string }).subtype}`
: message.type;
log(`[slash-cmd] type=${msgType}`);
if (message.type === 'system' && message.subtype === 'init') {
slashSessionId = message.session_id;
log(`Session after slash command: ${slashSessionId}`);
}
const observedCompaction = compactBoundaryFromMessage(message, log);
if (observedCompaction) {
compactBoundarySeen = true;
slashCompaction = observedCompaction;
}
if (message.type === 'result') {
const resultSubtype = (message as { subtype?: string }).subtype;
const textResult =
'result' in message ? (message as { result?: string }).result : null;
if (resultSubtype?.startsWith('error')) {
hadError = true;
writeOutput({
status: 'error',
result: null,
error: textResult || 'Session command failed.',
newSessionId: slashSessionId,
...buildCompactionOutput(slashCompaction),
});
} else {
writeOutput({
status: 'success',
result: textResult || 'Conversation compacted.',
newSessionId: slashSessionId,
...buildCompactionOutput(slashCompaction),
});
}
resultEmitted = true;
}
}
} catch (err) {
hadError = true;
const errorMsg = err instanceof Error ? err.message : String(err);
log(`Slash command error: ${errorMsg}`);
writeOutput({ status: 'error', result: null, error: errorMsg });
}
log(
`Slash command done. compactBoundarySeen=${compactBoundarySeen}, hadError=${hadError}`,
);
if (!hadError && !compactBoundarySeen) {
log(
'WARNING: compact_boundary was not observed. Compaction may not have completed.',
);
}
if (!resultEmitted && !hadError) {
writeOutput({
status: 'success',
result: compactBoundarySeen
? 'Conversation compacted.'
: 'Compaction requested but compact_boundary was not observed.',
newSessionId: slashSessionId,
...buildCompactionOutput(slashCompaction),
});
} else if (!hadError) {
writeOutput({
status: 'success',
result: null,
newSessionId: slashSessionId,
...buildCompactionOutput(slashCompaction),
});
}
}