File size: 22,370 Bytes
064bfd6 fdc8b59 064bfd6 346a09e 064bfd6 fdc8b59 064bfd6 fdc8b59 064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | import type { Notification } from 'src/context/notifications.js'
import type { TodoList } from 'src/utils/todo/types.js'
import type { BridgePermissionCallbacks } from '../bridge/bridgePermissionCallbacks.js'
import type { Command } from '../commands.js'
import type { ChannelPermissionCallbacks } from '../services/mcp/channelPermissions.js'
import type { ElicitationRequestEvent } from '../services/mcp/elicitationHandler.js'
import type {
MCPServerConnection,
ServerResource,
} from '../services/mcp/types.js'
import { shouldEnablePromptSuggestion } from '../services/PromptSuggestion/promptSuggestion.js'
import {
getEmptyToolPermissionContext,
type Tool,
type ToolPermissionContext,
} from '../Tool.js'
import type { TaskState } from '../tasks/types.js'
import type { AgentColorName } from '../tools/AgentTool/agentColorManager.js'
import type { AgentDefinitionsResult } from '../tools/AgentTool/loadAgentsDir.js'
import type { AllowedPrompt } from '../tools/ExitPlanModeTool/ExitPlanModeV2Tool.js'
import type { AgentId } from '../types/ids.js'
import type { Message, UserMessage } from '../types/message.js'
import type { LoadedPlugin, PluginError } from '../types/plugin.js'
import type { DeepImmutable } from '../types/utils.js'
import {
type AttributionState,
createEmptyAttributionState,
} from '../utils/commitAttribution.js'
import type { EffortValue } from '../utils/effort.js'
import type { FileHistoryState } from '../utils/fileHistory.js'
import type { REPLHookContext } from '../utils/hooks/postSamplingHooks.js'
import type { SessionHooksState } from '../utils/hooks/sessionHooks.js'
import type { ModelSetting } from '../utils/model/model.js'
import type { DenialTrackingState } from '../utils/permissions/denialTracking.js'
import type { PermissionMode } from '../utils/permissions/PermissionMode.js'
import { getInitialSettings } from '../utils/settings/settings.js'
import type { SettingsJson } from '../utils/settings/types.js'
import { shouldEnableThinkingByDefault } from '../utils/thinking.js'
import type { Store } from './store.js'
export type CompletionBoundary =
| { type: 'complete'; completedAt: number; outputTokens: number }
| { type: 'bash'; command: string; completedAt: number }
| { type: 'edit'; toolName: string; filePath: string; completedAt: number }
| {
type: 'denied_tool'
toolName: string
detail: string
completedAt: number
}
export type SpeculationResult = {
messages: Message[]
boundary: CompletionBoundary | null
timeSavedMs: number
}
export type SpeculationState =
| { status: 'idle' }
| {
status: 'active'
id: string
abort: () => void
startTime: number
messagesRef: { current: Message[] } // Mutable ref - avoids array spreading per message
writtenPathsRef: { current: Set<string> } // Mutable ref - relative paths written to overlay
boundary: CompletionBoundary | null
suggestionLength: number
toolUseCount: number
isPipelined: boolean
contextRef: { current: REPLHookContext }
pipelinedSuggestion?: {
text: string
promptId: 'user_intent' | 'stated_intent'
generationRequestId: string | null
} | null
}
export const IDLE_SPECULATION_STATE: SpeculationState = { status: 'idle' }
export type FooterItem =
| 'tasks'
| 'tmux'
| 'bagel'
| 'teams'
| 'bridge'
| 'companion'
// /goal β long-running autonomous objective. Set via `/goal <objective>`.
export type GoalStatus =
| 'pursuing'
| 'paused'
| 'achieved'
| 'blocked'
| 'usage-limited'
| 'budget-limited'
export type Goal = {
id: string
objective: string
status: GoalStatus
startedAt: number
startCostUSD: number
startTokensUsed: number
continuationCount: number
lastReason?: string
lastUpdatedAt: number
}
export type AppState = DeepImmutable<{
settings: SettingsJson
verbose: boolean
mainLoopModel: ModelSetting
mainLoopModelForSession: ModelSetting
statusLineText: string | undefined
expandedView: 'none' | 'tasks' | 'teammates'
isBriefOnly: boolean
// Optional - only present when ENABLE_AGENT_SWARMS is true (for dead code elimination)
showTeammateMessagePreview?: boolean
selectedIPAgentIndex: number
// CoordinatorTaskPanel selection: -1 = pill, 0 = main, 1..N = agent rows.
// AppState (not local) so the panel can read it directly without prop-drilling
// through PromptInput β PromptInputFooter.
coordinatorTaskIndex: number
viewSelectionMode: 'none' | 'selecting-agent' | 'viewing-agent'
// Which footer pill is focused (arrow-key navigation below the prompt).
// Lives in AppState so pill components rendered outside PromptInput
// (CompanionSprite in REPL.tsx) can read their own focused state.
footerSelection: FooterItem | null
toolPermissionContext: ToolPermissionContext
spinnerTip?: string
// Agent name from --agent CLI flag or settings (for logo display)
agent: string | undefined
// Assistant mode fully enabled (settings + GrowthBook gate + trust).
// Single source of truth - computed once in main.tsx before option
// mutation, consumers read this instead of re-calling isAssistantMode().
kairosEnabled: boolean
// Remote session URL for --remote mode (shown in footer indicator)
remoteSessionUrl: string | undefined
// Remote session WS state (`claude assistant` viewer). 'connected' means the
// live event stream is open; 'reconnecting' = transient WS drop, backoff
// in progress; 'disconnected' = permanent close or reconnects exhausted.
remoteConnectionStatus:
| 'connecting'
| 'connected'
| 'reconnecting'
| 'disconnected'
// `claude assistant`: count of background tasks (Agent calls, teammates,
// workflows) running inside the REMOTE daemon child. Event-sourced from
// system/task_started and system/task_notification on the WS. The local
// AppState.tasks is always empty in viewer mode β the tasks live in a
// different process.
remoteBackgroundTaskCount: number
// Always-on bridge: desired state (controlled by /config or footer toggle)
replBridgeEnabled: boolean
// Always-on bridge: true when activated via /remote-control command, false when config-driven
replBridgeExplicit: boolean
// Outbound-only mode: forward events to CCR but reject inbound prompts/control
replBridgeOutboundOnly: boolean
// Always-on bridge: env registered + session created (= "Ready")
replBridgeConnected: boolean
// Always-on bridge: ingress WebSocket is open (= "Connected" - user on claude.ai)
replBridgeSessionActive: boolean
// Always-on bridge: poll loop is in error backoff (= "Reconnecting")
replBridgeReconnecting: boolean
// Always-on bridge: connect URL for Ready state (?bridge=envId)
replBridgeConnectUrl: string | undefined
// Always-on bridge: session URL on claude.ai (set when connected)
replBridgeSessionUrl: string | undefined
// Always-on bridge: IDs for debugging (shown in dialog when --verbose)
replBridgeEnvironmentId: string | undefined
replBridgeSessionId: string | undefined
// Always-on bridge: error message when connection fails (shown in BridgeDialog)
replBridgeError: string | undefined
// Always-on bridge: session name set via `/remote-control <name>` (used as session title)
replBridgeInitialName: string | undefined
// Always-on bridge: first-time remote dialog pending (set by /remote-control command)
showRemoteCallout: boolean
}> & {
// Unified task state - excluded from DeepImmutable because TaskState contains function types
tasks: { [taskId: string]: TaskState }
// Name β AgentId registry populated by Agent tool when `name` is provided.
// Latest-wins on collision. Used by SendMessage to route by name.
agentNameRegistry: Map<string, AgentId>
// Task ID that has been foregrounded - its messages are shown in main view
foregroundedTaskId?: string
// Task ID of in-process teammate whose transcript is being viewed (undefined = leader's view)
viewingAgentTaskId?: string
// Latest companion reaction (set by buddy companion)
companionReaction?: string
// Timestamp of last /buddy pet β CompanionSprite renders hearts while recent
companionPetAt?: number
// TODO (ashwin): see if we can use utility-types DeepReadonly for this
mcp: {
clients: MCPServerConnection[]
tools: Tool[]
commands: Command[]
resources: Record<string, ServerResource[]>
/**
* Incremented by /reload-plugins to trigger MCP effects to re-run
* and pick up newly-enabled plugin MCP servers. Effects read this
* as a dependency; the value itself is not consumed.
*/
pluginReconnectKey: number
}
plugins: {
enabled: LoadedPlugin[]
disabled: LoadedPlugin[]
commands: Command[]
/**
* Plugin system errors collected during loading and initialization.
* See {@link PluginError} type documentation for complete details on error
* structure, context fields, and display format.
*/
errors: PluginError[]
// Installation status for background plugin/marketplace installation
installationStatus: {
marketplaces: Array<{
name: string
status: 'pending' | 'installing' | 'installed' | 'failed'
error?: string
}>
plugins: Array<{
id: string
name: string
status: 'pending' | 'installing' | 'installed' | 'failed'
error?: string
}>
}
/**
* Set to true when plugin state on disk has changed (background reconcile,
* /plugin menu install, external settings edit) and active components are
* stale. In interactive mode, user runs /reload-plugins to consume. In
* headless mode, refreshPluginState() auto-consumes via refreshActivePlugins().
*/
needsRefresh: boolean
}
agentDefinitions: AgentDefinitionsResult
fileHistory: FileHistoryState
attribution: AttributionState
todos: { [agentId: string]: TodoList }
remoteAgentTaskSuggestions: { summary: string; task: string }[]
notifications: {
current: Notification | null
queue: Notification[]
}
elicitation: {
queue: ElicitationRequestEvent[]
}
thinkingEnabled: boolean | undefined
promptSuggestionEnabled: boolean
sessionHooks: SessionHooksState
tungstenActiveSession?: {
sessionName: string
socketName: string
target: string // The tmux target (e.g., "session:window.pane")
}
tungstenLastCapturedTime?: number // Timestamp when frame was captured for model
tungstenLastCommand?: {
command: string // The command string to display (e.g., "Enter", "echo hello")
timestamp: number // When the command was sent
}
// Sticky tmux panel visibility β mirrors globalConfig.tungstenPanelVisible for reactivity.
tungstenPanelVisible?: boolean
// Transient auto-hide at turn end β separate from tungstenPanelVisible so the
// pill stays in the footer (user can reopen) but the panel content doesn't take
// screen space when idle. Cleared on next Tmux tool use or user toggle. NOT persisted.
tungstenPanelAutoHidden?: boolean
// WebBrowser tool (codename bagel): pill visible in footer
bagelActive?: boolean
// WebBrowser tool: current page URL shown in pill label
bagelUrl?: string
// WebBrowser tool: sticky panel visibility toggle
bagelPanelVisible?: boolean
// /goal β long-running autonomous objective. Set via `/goal <objective>`.
goal: Goal | undefined
// chicago MCP session state. Types inlined (not imported from
// @ant/computer-use-mcp/types) so external typecheck passes without the
// ant-scoped dep resolved. Shapes match `AppGrant`/`CuGrantFlags`
// structurally β wrapper.tsx assigns via structural compatibility. Only
// populated when feature('CHICAGO_MCP') is active.
computerUseMcpState?: {
// Session-scoped app allowlist. NOT persisted across resume.
allowedApps?: readonly {
bundleId: string
displayName: string
grantedAt: number
}[]
// Clipboard/system-key grant flags (orthogonal to allowlist).
grantFlags?: {
clipboardRead: boolean
clipboardWrite: boolean
systemKeyCombos: boolean
}
// Dims-only (NOT the blob) for scaleCoord after compaction. The full
// `ScreenshotResult` including base64 is process-local in wrapper.tsx.
lastScreenshotDims?: {
width: number
height: number
displayWidth: number
displayHeight: number
displayId?: number
originX?: number
originY?: number
}
// Accumulated by onAppsHidden, cleared + unhidden at turn end.
hiddenDuringTurn?: ReadonlySet<string>
// Which display CU targets. Written back by the package's
// `autoTargetDisplay` resolver via `onResolvedDisplayUpdated`. Persisted
// across resume so clicks stay on the display the model last saw.
selectedDisplayId?: number
// True when the model explicitly picked a display via `switch_display`.
// Makes `handleScreenshot` skip the resolver chase chain and honor
// `selectedDisplayId` directly. Cleared on resolver writeback (pinned
// display unplugged β Swift fell back to main) and on
// `switch_display("auto")`.
displayPinnedByModel?: boolean
// Sorted comma-joined bundle-ID set the display was last auto-resolved
// for. `handleScreenshot` only re-resolves when the allowed set has
// changed since β keeps the resolver from yanking on every screenshot.
displayResolvedForApps?: string
}
// REPL tool VM context - persists across REPL calls for state sharing
replContext?: {
vmContext: import('vm').Context
registeredTools: Map<
string,
{
name: string
description: string
schema: Record<string, unknown>
handler: (args: Record<string, unknown>) => Promise<unknown>
}
>
console: {
log: (...args: unknown[]) => void
error: (...args: unknown[]) => void
warn: (...args: unknown[]) => void
info: (...args: unknown[]) => void
debug: (...args: unknown[]) => void
getStdout: () => string
getStderr: () => string
clear: () => void
}
}
teamContext?: {
teamName: string
teamFilePath: string
leadAgentId: string
// Self-identity for swarm members (separate processes in tmux panes)
// Note: This is different from toolUseContext.agentId which is for in-process subagents
selfAgentId?: string // Swarm member's own ID (same as leadAgentId for leaders)
selfAgentName?: string // Swarm member's name ('team-lead' for leaders)
isLeader?: boolean // True if this swarm member is the team leader
selfAgentColor?: string // Assigned color for UI (used by dynamically joined sessions)
teammates: {
[teammateId: string]: {
name: string
agentType?: string
color?: string
tmuxSessionName: string
tmuxPaneId: string
cwd: string
worktreePath?: string
spawnedAt: number
}
}
}
// Standalone agent context for non-swarm sessions with custom name/color
standaloneAgentContext?: {
name: string
color?: AgentColorName
}
inbox: {
messages: Array<{
id: string
from: string
text: string
timestamp: string
status: 'pending' | 'processing' | 'processed'
color?: string
summary?: string
}>
}
// Worker sandbox permission requests (leader side) - for network access approval
workerSandboxPermissions: {
queue: Array<{
requestId: string
workerId: string
workerName: string
workerColor?: string
host: string
createdAt: number
}>
selectedIndex: number
}
// Pending permission request on worker side (shown while waiting for leader approval)
pendingWorkerRequest: {
toolName: string
toolUseId: string
description: string
} | null
// Pending sandbox permission request on worker side
pendingSandboxRequest: {
requestId: string
host: string
} | null
promptSuggestion: {
text: string | null
promptId: 'user_intent' | 'stated_intent' | null
shownAt: number
acceptedAt: number
generationRequestId: string | null
}
speculation: SpeculationState
speculationSessionTimeSavedMs: number
skillImprovement: {
suggestion: {
skillName: string
updates: { section: string; change: string; reason: string }[]
} | null
}
// Auth version - incremented on login/logout to trigger re-fetching of auth-dependent data
authVersion: number
// Initial message to process (from CLI args or plan mode exit)
// When set, REPL will process the message and trigger a query
initialMessage: {
message: UserMessage
clearContext?: boolean
mode?: PermissionMode
// Session-scoped permission rules from plan mode (e.g., "run tests", "install dependencies")
allowedPrompts?: AllowedPrompt[]
} | null
// Pending plan verification state (set when exiting plan mode)
// Used by VerifyPlanExecution tool to trigger background verification
pendingPlanVerification?: {
plan: string
verificationStarted: boolean
verificationCompleted: boolean
}
// Denial tracking for classifier modes (YOLO, headless, etc.) - falls back to prompting when limits exceeded
denialTracking?: DenialTrackingState
// Active overlays (Select dialogs, etc.) for Escape key coordination
activeOverlays: ReadonlySet<string>
// Fast mode
fastMode?: boolean
// Advisor model for server-side advisor tool (undefined = disabled).
advisorModel?: string
// Effort value
effortValue?: EffortValue
// Set synchronously in launchUltraplan before the detached flow starts.
// Prevents duplicate launches during the ~5s window before
// ultraplanSessionUrl is set by teleportToRemote. Cleared by launchDetached
// once the URL is set or on failure.
ultraplanLaunching?: boolean
// Active ultraplan CCR session URL. Set while the RemoteAgentTask runs;
// truthy disables the keyword trigger + rainbow. Cleared when the poll
// reaches terminal state.
ultraplanSessionUrl?: string
// Approved ultraplan awaiting user choice (implement here vs fresh session).
// Set by RemoteAgentTask poll on approval; cleared by UltraplanChoiceDialog.
ultraplanPendingChoice?: { plan: string; sessionId: string; taskId: string }
// Pre-launch permission dialog. Set by /ultraplan (slash or keyword);
// cleared by UltraplanLaunchDialog on choice.
ultraplanLaunchPending?: { blurb: string }
// Remote-harness side: set via set_permission_mode control_request,
// pushed to CCR external_metadata.is_ultraplan_mode by onChangeAppState.
isUltraplanMode?: boolean
// Always-on bridge: permission callbacks for bidirectional permission checks
replBridgePermissionCallbacks?: BridgePermissionCallbacks
// Channel permission callbacks β permission prompts over Telegram/iMessage/etc.
// Races against local UI + bridge + hooks + classifier via claim() in
// interactiveHandler.ts. Constructed once in useManageMCPConnections.
channelPermissionCallbacks?: ChannelPermissionCallbacks
}
export type AppStateStore = Store<AppState>
export function getDefaultAppState(): AppState {
// Determine initial permission mode for teammates spawned with plan_mode_required
// Use lazy require to avoid circular dependency with teammate.ts
/* eslint-disable @typescript-eslint/no-require-imports */
const teammateUtils =
require('../utils/teammate.js') as typeof import('../utils/teammate.js')
/* eslint-enable @typescript-eslint/no-require-imports */
const initialMode: PermissionMode =
teammateUtils.isTeammate() && teammateUtils.isPlanModeRequired()
? 'plan'
: 'default'
return {
settings: getInitialSettings(),
tasks: {},
agentNameRegistry: new Map(),
verbose: false,
mainLoopModel: null, // alias, full name (as with --model or env var), or null (default)
mainLoopModelForSession: null,
statusLineText: undefined,
expandedView: 'none',
isBriefOnly: false,
showTeammateMessagePreview: false,
selectedIPAgentIndex: -1,
coordinatorTaskIndex: -1,
viewSelectionMode: 'none',
footerSelection: null,
kairosEnabled: false,
remoteSessionUrl: undefined,
remoteConnectionStatus: 'connecting',
remoteBackgroundTaskCount: 0,
replBridgeEnabled: false,
replBridgeExplicit: false,
replBridgeOutboundOnly: false,
replBridgeConnected: false,
replBridgeSessionActive: false,
replBridgeReconnecting: false,
replBridgeConnectUrl: undefined,
replBridgeSessionUrl: undefined,
replBridgeEnvironmentId: undefined,
replBridgeSessionId: undefined,
replBridgeError: undefined,
replBridgeInitialName: undefined,
showRemoteCallout: false,
toolPermissionContext: {
...getEmptyToolPermissionContext(),
mode: initialMode,
},
agent: undefined,
agentDefinitions: { activeAgents: [], allAgents: [] },
fileHistory: {
snapshots: [],
trackedFiles: new Set(),
snapshotSequence: 0,
},
attribution: createEmptyAttributionState(),
mcp: {
clients: [],
tools: [],
commands: [],
resources: {},
pluginReconnectKey: 0,
},
plugins: {
enabled: [],
disabled: [],
commands: [],
errors: [],
installationStatus: {
marketplaces: [],
plugins: [],
},
needsRefresh: false,
},
todos: {},
remoteAgentTaskSuggestions: [],
notifications: {
current: null,
queue: [],
},
elicitation: {
queue: [],
},
thinkingEnabled: shouldEnableThinkingByDefault(),
promptSuggestionEnabled: shouldEnablePromptSuggestion(),
sessionHooks: new Map(),
inbox: {
messages: [],
},
workerSandboxPermissions: {
queue: [],
selectedIndex: 0,
},
pendingWorkerRequest: null,
pendingSandboxRequest: null,
promptSuggestion: {
text: null,
promptId: null,
shownAt: 0,
acceptedAt: 0,
generationRequestId: null,
},
speculation: IDLE_SPECULATION_STATE,
speculationSessionTimeSavedMs: 0,
skillImprovement: {
suggestion: null,
},
authVersion: 0,
initialMessage: null,
effortValue: undefined,
activeOverlays: new Set<string>(),
fastMode: false,
goal: undefined,
}
}
|