Files
AirCoding/packages/contracts/src/ui.ts
AirCoding a773bac28c P0-P8: Full V1.0.0 Alpha implementation + audit reports
Implements 123 tasks across 9 phases (T-001..T-809) totaling 146 source files.

Monorepo (P0):
- 7-package Bun + Turborepo + TypeScript monorepo
- dependency-cruiser enforcing 7 forbidden edges + 5 deep-import rules

Contracts (P0):
- 16 type files (ids/error/event/runtime/ipc/task/worker-result/tool/artifact/evidence/project/provider/permission/ui/capability/platform)

Storage & Events (P1):
- DatabaseManager + MigrationRunner (19 tables, 22 indexes, 5 schema_meta seeds)
- 16 repositories (Repository<T,I,U> pattern, INV-1 status columns via EventStore.project only)
- EventSchemaRegistry (54 durable + 7 ephemeral), EventStore, EventBus, EventIngestor
- Project/Session/Artifact/Evidence stores + 8-step Recovery

Tools & Permission (P2):
- PathClassifier (8 categories), CommandRiskAnalyzer (10 categories), SecretRedactor
- PermissionEngine 6-layer evaluation (capability→profile→task_scope→risk→credential→user_prompt)
- ToolRegistry with 20+ tools across fs/shell/git/project/artifact/context/permission/doctor
- CapabilityManifestValidator + CapabilityRegistry

LLM & Context (P3):
- ModelConfigLoader, CapabilityMatrix, AnthropicCanonicalConverter
- AnthropicAdapter + OpenAICompatibleAdapter
- ProviderManager facade
- PromptLayerLoader (L0/L1/L3/L5), CompactionPolicy, ContextAssembler

Worker IPC & Scheduler (P4):
- WorkerProtocol (NDJSON), WorkerProcess (exit codes 0-5), WorkerManager (spawn/handshake)
- WorkerRuntime (INV-3: IPC only, no direct fs/shell/SQLite)
- 5 worker roles (Executor/Reviewer/Debugger/Compactor/ExperienceMiner)
- TaskGraph, WavePlanner, RetryPlanner, AgentMonitor, WorkspaceManager
- Scheduler (state machine), 8-step Recovery

C++ Toolchain (P5):
- DiagnosticParser, CppProjectDetector, CMakeConfigurator, CppBuilder
- CppTestRunner, CppcheckRunner, ClangdClient
- CppToolRegistrar + capability manifest

Projection & TUI (P6):
- ProjectionStore (hydrate/apply/snapshot/subscribe)
- TuiApp + 8 components (Session/Task/Agent/Tool/Diff/Evidence/Permission/Blocker/Hud)
- ProjectionClient in-process ref

Agents & Knowledge (P7):
- MainAgent, ArchitectureDesigner
- DebugKnowledgeStore + LearnedMemoryStore (single-writer, outbox model)
- Role integration wiring

CLI & Doctor & Release (P8):
- Logger + DeveloperLogEncryptor (AES-256-GCM)
- DoctorService (self_bootstrap first)
- RuntimeApp + ServiceRegistry
- 11 CLI commands: run/init/doctor/provider/resume/compact/history/session/restore/e2e/release
- CliEntrypoint + air<TODO>

Audit (in AirPlan/docs/):
- Deepseek开发阶段审计.md (97 findings)
- Opus开发阶段审计.md (140+ findings, 18 P0 blockers)
- MiniMaxM3开发阶段审计.md (18 P0 blockers, focuses on executability)
- AirPlan/TODO.md (technical debt + 42 TODOs by phase)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 19:19:55 +08:00

214 lines
6.5 KiB
TypeScript
Executable File

// contracts §17 — Projection/UI Contracts
// File: ui.ts — all *Projection types, ProjectionSnapshot, ProjectionStore,
// ProjectionClient, UiCommandChannel; projection.ts symbols merged per DD §3.
import type {
SessionID,
TaskID,
AgentID,
ToolRunID,
CommandRunID,
ArtifactID,
ISOTimeString,
} from './ids.js'
import type { RuntimeEvent } from './event.js'
import type { TaskStatus } from './task.js'
import type { AgentType } from './runtime.js'
// Re-export for external consumers of this module
export type {
SessionID,
TaskID,
AgentID,
ToolRunID,
CommandRunID,
ArtifactID,
ISOTimeString,
}
export type { TaskStatus } from './task.js'
export type { AgentType } from './runtime.js'
export type { RuntimeEvent } from './event.js'
// =============================================================================
// §17 — Projection Types
// =============================================================================
/**
* Derived status for CommandRunProjection.
* Matches DD §4.4 derivation:
* completed_at == null -> "running"
* cancellation metadata present -> "cancelled"
* exit_code === 0 -> "ok"
* exit_code != 0 (non-null) -> "error"
* otherwise -> "unknown"
*/
export type CommandRunStatus = 'running' | 'ok' | 'error' | 'cancelled' | 'unknown'
/**
* Pure derivation function for command run status.
* Implements DD §4.4 logic so that CommandRunProjection.status
* never invents a value outside the defined union.
*/
export function derive_command_status(fields: {
completed_at: ISOTimeString | null | undefined
exit_code: number | null | undefined
cancelled: boolean
}): CommandRunStatus {
if (fields.completed_at == null) return 'running'
if (fields.cancelled) return 'cancelled'
if (fields.exit_code === 0) return 'ok'
if (fields.exit_code != null) return 'error'
return 'unknown'
}
export interface SessionProjection {
session_id: SessionID
title?: string
status: string
}
export interface TaskProjection {
task_id: TaskID
title: string
status: TaskStatus
progress_text?: string
}
export interface AgentProjection {
agent_id: AgentID
agent_type: AgentType
status: string
task_id?: TaskID
progress_text?: string
}
export interface ToolRunProjection {
tool_run_id: ToolRunID
tool_name: string
status: string
task_id?: TaskID
}
export interface CommandRunProjection {
command_run_id: CommandRunID
command: string
status: CommandRunStatus
exit_code?: number
}
export interface ArtifactProjection {
artifact_id: ArtifactID
type: string
uri: string
}
export interface PermissionPromptProjection {
prompt_id: string
subject: string
risk_level: string
reason: string
options: string[]
}
export interface BlockerProjection {
task_id?: TaskID
reason: string
required_decision: string
}
// =============================================================================
// §17 — ProjectionSnapshot
// =============================================================================
export interface ProjectionSnapshot {
session?: SessionProjection
tasks: TaskProjection[]
agents: AgentProjection[]
tool_runs: ToolRunProjection[]
command_runs: CommandRunProjection[]
artifacts: ArtifactProjection[]
permission_prompts: PermissionPromptProjection[]
blockers: BlockerProjection[]
updated_at: ISOTimeString
}
// =============================================================================
// §17 — Subscription (re-declared here for projection consumers)
// =============================================================================
/**
* Subscription handle returned by ProjectionStore.subscribe and
* ProjectionClient.subscribe. Mirrors the EventBus Subscription
* contract (§7) for standalone projection consumers.
*/
export interface Subscription {
unsubscribe(): void
}
// =============================================================================
// §17 — ProjectionStore
// =============================================================================
/**
* ProjectionStore.apply handles all durable events and key ephemeral events
* (agent.heartbeat, task.progress, assistant.message.delta, tool.progress,
* command.stdout.delta, command.stderr.delta). Unknown event types are ignored.
*
* command_runs projection status uses the derivation in DD §4.4
* (derive_command_status). ProjectionStore is never a scheduling/recovery
* source of truth (overview §9.3).
*/
export interface ProjectionStore {
hydrate(session_id: SessionID): Promise<void>
apply(event: RuntimeEvent): void
snapshot(): ProjectionSnapshot
subscribe(handler: (snapshot: ProjectionSnapshot) => void): Subscription
}
// =============================================================================
// §17 — ProjectionClient
// =============================================================================
/**
* Read-only client for the TUI. V1 transport: TUI runs in-process with runtime;
* ProjectionClient is a direct interface reference, not IPC.
* TUI may consume only ProjectionClient or projection contracts, never runtime
* internals, SQLite, or EventBus directly.
*/
export interface ProjectionClient {
snapshot(): ProjectionSnapshot
subscribe(handler: (snapshot: ProjectionSnapshot) => void): Subscription
}
// =============================================================================
// §17 — UiCommandChannel
// =============================================================================
/**
* Narrow interface through which the TUI emits user decisions back to the
* runtime. Covers two V1 command types:
*
* - Permission prompt responses (user selects an option for a
* permission.prompt.requested event; runtime emits
* permission.prompt.resolved).
* - Blocker decisions (user resolves a BlockerProjection; runtime
* processes the decision through the scheduler/task system).
*
* Boundary rule (code-view §7): packages/tui may only import from
* packages/contracts, never from packages/runtime/src/*.
*/
export interface UiCommandChannel {
/**
* Resolve a permission prompt by selecting an option.
* The runtime will emit a permission.prompt.resolved durable event
* and resume the suspended tool call.
*/
resolve_permission_prompt(prompt_id: string, selected_option: string): Promise<void>
/**
* Resolve a blocker by providing the user's decision.
* The runtime processes the decision through the task system
* to unblock the affected task.
*/
resolve_blocker(blocker: BlockerProjection, decision: string): Promise<void>
}