feat: 添加 autonomy 自主模式命令系统

- 新增 autonomy CLI handler 和交互式面板
- 新增 autonomyCommandSpec 命令规范定义
- 新增 autonomyAuthority 权限控制
- 新增 autonomyStatus 状态管理
- 注册 CLI 子命令 (claude autonomy status/runs/flows/flow)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
unraid
2026-04-22 22:38:09 +08:00
parent 31b2fdd97a
commit c4775fff58
10 changed files with 1152 additions and 163 deletions

View File

@@ -0,0 +1,132 @@
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
import { mkdir, rm, writeFile } from 'fs/promises'
import { tmpdir } from 'os'
import { join } from 'path'
import {
resetStateForTests,
setOriginalCwd,
setProjectRoot,
} from '../../../bootstrap/state'
import { createAutonomyQueuedPrompt } from '../../../utils/autonomyRuns'
import {
cancelAutonomyFlowText,
getAutonomyDeepSectionText,
getAutonomyFlowText,
getAutonomyFlowsText,
getAutonomyStatusText,
resumeAutonomyFlowText,
} from '../autonomy'
import {
listAutonomyFlows,
startManagedAutonomyFlow,
} from '../../../utils/autonomyFlows'
let tempDir: string
let previousConfigDir: string | undefined
beforeEach(async () => {
previousConfigDir = process.env.CLAUDE_CONFIG_DIR
tempDir = join(
tmpdir(),
`autonomy-cli-${Date.now()}-${Math.random().toString(16).slice(2)}`,
)
await mkdir(tempDir, { recursive: true })
process.env.CLAUDE_CONFIG_DIR = join(tempDir, 'config')
resetStateForTests()
setOriginalCwd(tempDir)
setProjectRoot(tempDir)
})
afterEach(async () => {
resetStateForTests()
if (previousConfigDir === undefined) {
delete process.env.CLAUDE_CONFIG_DIR
} else {
process.env.CLAUDE_CONFIG_DIR = previousConfigDir
}
await rm(tempDir, { recursive: true, force: true })
})
describe('autonomy CLI handler', () => {
test('prints the same basic status surfaces as the slash command', async () => {
await createAutonomyQueuedPrompt({
basePrompt: 'scheduled prompt',
trigger: 'scheduled-task',
rootDir: tempDir,
currentDir: tempDir,
sourceLabel: 'nightly',
})
const output = await getAutonomyStatusText()
expect(output).toContain('Autonomy runs: 1')
expect(output).toContain('Queued: 1')
expect(output).toContain('Autonomy flows: 0')
})
test('prints deep status for CLI status --deep', async () => {
await mkdir(join(tempDir, '.claude'), { recursive: true })
await writeFile(
join(tempDir, '.claude', 'remote-trigger-audit.jsonl'),
`${JSON.stringify({
auditId: 'audit-1',
createdAt: 1,
action: 'list',
ok: true,
status: 200,
})}\n`,
)
const output = await getAutonomyStatusText({ deep: true })
expect(output).toContain('# Autonomy Deep Status')
expect(output).toContain('## Workflow Runs')
expect(output).toContain('## Pipes')
expect(output).toContain('## Remote Control')
expect(output).toContain('## RemoteTrigger')
})
test('prints individual deep status sections for panel actions', async () => {
const pipes = await getAutonomyDeepSectionText('pipes')
const remoteControl = await getAutonomyDeepSectionText('remote-control')
expect(pipes).toContain('# Pipes')
expect(pipes).toContain('Pipe registry:')
expect(remoteControl).toContain('# Remote Control')
expect(remoteControl).toContain('Remote Control:')
})
test('lists, inspects, cancels, and resumes flows from CLI handlers', async () => {
await startManagedAutonomyFlow({
trigger: 'proactive-tick',
goal: 'ship managed flow',
rootDir: tempDir,
currentDir: tempDir,
steps: [
{
name: 'wait',
prompt: 'Wait for manual signal',
waitFor: 'manual',
},
{
name: 'run',
prompt: 'Run the next step',
},
],
})
const [waitingFlow] = await listAutonomyFlows(tempDir)
expect(await getAutonomyFlowsText()).toContain(waitingFlow!.flowId)
expect(await getAutonomyFlowText(waitingFlow!.flowId)).toContain(
'Current step: wait',
)
const resumed = await resumeAutonomyFlowText(waitingFlow!.flowId)
expect(resumed).toContain('Prepared the next managed step')
expect(resumed).toContain('Prompt:')
expect(resumed).toContain('Wait for manual signal')
const cancelled = await cancelAutonomyFlowText(waitingFlow!.flowId)
expect(cancelled).toContain('Cancelled flow')
})
})

View File

@@ -0,0 +1,213 @@
import {
formatAutonomyFlowDetail,
formatAutonomyFlowsList,
formatAutonomyFlowsStatus,
getAutonomyFlowById,
listAutonomyFlows,
requestManagedAutonomyFlowCancel,
} from '../../utils/autonomyFlows.js'
import {
formatAutonomyRunsList,
formatAutonomyRunsStatus,
listAutonomyRuns,
markAutonomyRunCancelled,
resumeManagedAutonomyFlowPrompt,
} from '../../utils/autonomyRuns.js'
import {
formatAutonomyDeepStatus,
formatAutonomyDeepStatusSections,
type AutonomyDeepStatusSectionId,
} from '../../utils/autonomyStatus.js'
import {
AUTONOMY_USAGE,
parseAutonomyArgs,
} from '../../utils/autonomyCommandSpec.js'
import {
enqueuePendingNotification,
removeByFilter,
} from '../../utils/messageQueueManager.js'
export function parseAutonomyLimit(raw?: string | number): number {
const parsed = typeof raw === 'number' ? raw : Number.parseInt(raw ?? '', 10)
if (!Number.isFinite(parsed) || parsed <= 0) {
return 10
}
return Math.min(parsed, 50)
}
export async function getAutonomyStatusText(options?: {
deep?: boolean
}): Promise<string> {
const [runs, flows] = await Promise.all([
listAutonomyRuns(),
listAutonomyFlows(),
])
if (options?.deep) {
return formatAutonomyDeepStatus({ runs, flows })
}
return [
formatAutonomyRunsStatus(runs),
formatAutonomyFlowsStatus(flows),
].join('\n')
}
export async function getAutonomyDeepSectionText(
sectionId: AutonomyDeepStatusSectionId,
): Promise<string> {
const [runs, flows] = await Promise.all([
listAutonomyRuns(),
listAutonomyFlows(),
])
const sections = await formatAutonomyDeepStatusSections({ runs, flows })
const section = sections.find(item => item.id === sectionId)
if (!section) {
return `Autonomy deep status section not found: ${sectionId}`
}
return [`# ${section.title}`, section.content].join('\n')
}
export async function autonomyStatusHandler(options?: {
deep?: boolean
}): Promise<void> {
process.stdout.write(`${await getAutonomyStatusText(options)}\n`)
}
export async function getAutonomyRunsText(
limit?: string | number,
): Promise<string> {
return formatAutonomyRunsList(
await listAutonomyRuns(),
parseAutonomyLimit(limit),
)
}
export async function autonomyRunsHandler(
limit?: string | number,
): Promise<void> {
process.stdout.write(`${await getAutonomyRunsText(limit)}\n`)
}
export async function getAutonomyFlowsText(
limit?: string | number,
): Promise<string> {
return formatAutonomyFlowsList(
await listAutonomyFlows(),
parseAutonomyLimit(limit),
)
}
export async function autonomyFlowsHandler(
limit?: string | number,
): Promise<void> {
process.stdout.write(`${await getAutonomyFlowsText(limit)}\n`)
}
export async function getAutonomyFlowText(flowId: string): Promise<string> {
return formatAutonomyFlowDetail(await getAutonomyFlowById(flowId))
}
export async function autonomyFlowHandler(flowId: string): Promise<void> {
process.stdout.write(`${await getAutonomyFlowText(flowId)}\n`)
}
export async function cancelAutonomyFlowText(
flowId: string,
options?: {
removeQueuedInMemory?: boolean
},
): Promise<string> {
const cancelled = await requestManagedAutonomyFlowCancel({ flowId })
if (!cancelled) {
return 'Autonomy flow not found.'
}
if (!cancelled.accepted) {
return `Autonomy flow ${flowId} is already terminal (${cancelled.flow.status}).`
}
let removedCount = 0
if (options?.removeQueuedInMemory) {
const removed = removeByFilter(cmd => cmd.autonomy?.flowId === flowId)
removedCount = removed.length
for (const command of removed) {
if (command.autonomy?.runId) {
await markAutonomyRunCancelled(command.autonomy.runId)
}
}
} else {
for (const runId of cancelled.queuedRunIds) {
await markAutonomyRunCancelled(runId)
}
removedCount = cancelled.queuedRunIds.length
}
return cancelled.flow.status === 'running'
? `Cancellation requested for flow ${flowId}. The current step is still running, and no new steps will be started.`
: `Cancelled flow ${flowId}. Removed ${removedCount} queued step(s).`
}
export async function autonomyFlowCancelHandler(flowId: string): Promise<void> {
process.stdout.write(`${await cancelAutonomyFlowText(flowId)}\n`)
}
export async function resumeAutonomyFlowText(
flowId: string,
options?: {
enqueueInMemory?: boolean
},
): Promise<string> {
const command = await resumeManagedAutonomyFlowPrompt({ flowId })
if (!command) {
return 'Autonomy flow is not waiting or was not found.'
}
if (options?.enqueueInMemory) {
enqueuePendingNotification(command)
return `Queued the next managed step for flow ${flowId}.`
}
const runId = command.autonomy?.runId ?? 'unknown'
return [
`Prepared the next managed step for flow ${flowId}.`,
`Run ID: ${runId}`,
'',
'Prompt:',
typeof command.value === 'string' ? command.value : String(command.value),
].join('\n')
}
export async function autonomyFlowResumeHandler(flowId: string): Promise<void> {
process.stdout.write(`${await resumeAutonomyFlowText(flowId)}\n`)
}
export async function getAutonomyCommandText(
args: string,
options?: {
enqueueInMemory?: boolean
removeQueuedInMemory?: boolean
},
): Promise<string> {
const parsed = parseAutonomyArgs(args)
switch (parsed.type) {
case 'status':
return getAutonomyStatusText({ deep: parsed.deep })
case 'runs':
return getAutonomyRunsText(parsed.limit)
case 'flows':
return getAutonomyFlowsText(parsed.limit)
case 'flow-detail':
return getAutonomyFlowText(parsed.flowId)
case 'flow-cancel':
return cancelAutonomyFlowText(parsed.flowId, {
removeQueuedInMemory: options?.removeQueuedInMemory,
})
case 'flow-resume':
return resumeAutonomyFlowText(parsed.flowId, {
enqueueInMemory: options?.enqueueInMemory,
})
case 'usage':
return AUTONOMY_USAGE
}
}