- Introduced `/restart` and `/update` commands for managing the OpenACP application directly from Telegram. - Implemented logic to check for updates, notify users, and handle application restarts seamlessly. - Enhanced menu options to include new commands, improving user interaction and accessibility. - Updated session management to ensure proper handling of permission requests and session states during updates and restarts.
9.1 KiB
9.1 KiB
OpenACP Aggressive Refactoring Spec
Overview
Refactor OpenACP codebase for clarity, maintainability, and extensibility while preserving all existing behavior. TDD approach — write tests before modifying code for high-risk changes.
Phase 1: Event System — TypedEventEmitter
Problem
AgentInstanceuses 2 callback properties:onSessionUpdate,onPermissionRequestcore.tswireSessionEvents() assigns callbacks directly — not extensibleSession.autoName()andwarmup()swap handlers temporarily (prevHandler) — fragile, race-prone- Adding logging/filtering/transform requires modifying wireSessionEvents directly
Design
TypedEventEmitter — generic typed event emitter (no dependency on Node EventEmitter):
interface SessionEvents {
agent_event: (event: AgentEvent) => void
permission_request: (request: PermissionRequest) => void
session_end: (reason: string) => void
error: (error: Error) => void
}
Flow:
AgentInstance ──emit──► Session (TypedEventEmitter) ──► Core subscribes ──► Adapter
Key changes:
AgentInstanceno longer holdsonSessionUpdate/onPermissionRequestcallbacksAgentInstancecallssession.emit('agent_event', event)insteadSessionextendsTypedEventEmitter<SessionEvents>Session.pause()/Session.resume()— suppress event delivery (buffer events during pause, replay on resume)autoName()uses pause/resume instead of handler swappingwarmup()uses pause/resume instead of handler swapping- Core subscribes via
session.on('agent_event', handler)
Tests (write BEFORE implementation):
- Session emits
agent_event→ listener receives correct payload - Session emits
permission_request→ listener receives correct payload session.pause()→ events buffered, not deliveredsession.resume()→ buffered events replayed in order- Multiple listeners all receive events
session.removeListener()stops delivery to that listener
Files affected
src/core/session.ts— major changessrc/core/agent-instance.ts— remove callback propertiessrc/core/core.ts— wireSessionEvents rewritten to use.on()- NEW:
src/core/typed-emitter.ts— TypedEventEmitter class - NEW:
tests/core/typed-emitter.test.ts - NEW:
tests/core/session-events.test.ts
Phase 2: Session Decomposition
Problem
Sessionclass holds too many responsibilities: prompt queue, auto-naming, dangerous mode, pending permission, lifecycle, adapter referencependingPermissionis a public mutable property — no encapsulation
Design
Extract into focused components:
Session (lightweight coordinator)
├── PromptQueue — serial prompt processing, queue management
├── AutoNamer — summarize after first prompt, rename thread
└── PermissionGate — hold/resolve permission requests, typed API
PromptQueue:
class PromptQueue {
enqueue(prompt: string): void
processNext(): Promise<void>
clear(): void
get pending(): number
get isProcessing(): boolean
dangerousMode: boolean // skip permission prompts
}
AutoNamer:
class AutoNamer {
constructor(session: Session, onRename: (name: string) => void)
// Listens to agent_event after first prompt
// Sends summarize request using session.pause()/resume()
// Calls onRename when name is determined
}
PermissionGate:
class PermissionGate {
setPending(request: PermissionRequest): Promise<string> // returns optionId
resolve(optionId: string): void
reject(reason?: string): void
get isPending(): boolean
get currentRequest(): PermissionRequest | undefined
}
Tests (write BEFORE implementation):
- PromptQueue: enqueue 3 prompts → processed serially (not concurrent)
- PromptQueue: enqueue while processing → queued, not dropped
- PromptQueue: clear() removes all pending, does not cancel active
- AutoNamer: triggers rename after first prompt completion
- AutoNamer: does not trigger on subsequent prompts
- PermissionGate: setPending returns promise, resolve fulfills it
- PermissionGate: double-resolve is no-op (no error)
- PermissionGate: reject rejects the promise
Files affected
src/core/session.ts— slim down, delegate to components- NEW:
src/core/prompt-queue.ts - NEW:
src/core/auto-namer.ts - NEW:
src/core/permission-gate.ts - NEW:
tests/core/prompt-queue.test.ts - NEW:
tests/core/auto-namer.test.ts - NEW:
tests/core/permission-gate.test.ts
Phase 3: Core ↔ Adapter Contract
Problem
ChannelAdapteris abstract class withprotected core: any— not type-safe- Adapter must
extends ChannelAdapter— tight coupling, hard to test core.tswireSessionEvents contains enrichment logic (enrichWithViewerLinks,toOutgoingMessage) — transform concern, not orchestration
Design
Interface replaces abstract class:
interface IChannelAdapter {
readonly id: string
sendMessage(sessionId: string, message: OutgoingMessage): Promise<void>
sendPermissionRequest(session: Session, request: PermissionRequest): Promise<void>
sendNotification(notification: NotificationMessage): Promise<void>
createSessionThread(session: Session): Promise<string>
renameSessionThread(session: Session, name: string): Promise<void>
start(): Promise<void>
stop(): Promise<void>
}
interface ICoreForAdapter {
handleNewSession(adapterId: string, opts: NewSessionOpts): Promise<Session>
handlePrompt(sessionId: string, prompt: string): Promise<void>
getSession(sessionId: string): Session | undefined
getSessions(): Session[]
endSession(sessionId: string): Promise<void>
}
MessageTransformer — extracted from core.ts:
class MessageTransformer {
transform(event: AgentEvent, session: Session): OutgoingMessage
// Handles: enrichWithViewerLinks, toOutgoingMessage, format conversion
}
Pipeline:
AgentEvent → MessageTransformer.transform() → OutgoingMessage → adapter.sendMessage()
Tests:
- MessageTransformer: text event → correct OutgoingMessage
- MessageTransformer: tool_use event → enriched with viewer links
- MessageTransformer: completion event → includes usage data
- Mock adapter implementing IChannelAdapter — verify contract compliance
- ICoreForAdapter mock — adapter can call core methods type-safely
Files affected
src/core/channel.ts— abstract class → interfacesrc/core/core.ts— extract enrichment logic- NEW:
src/core/message-transformer.ts - NEW:
tests/core/message-transformer.test.ts src/adapters/telegram/adapter.ts— implements interface instead of extends
Phase 4: Config Migration Pipeline
Problem
config.tsload() contains inline migration logic mixed with validation- Adding new migrations requires modifying load() directly
Design
interface Migration {
version: number
up: (raw: Record<string, unknown>) => Record<string, unknown>
}
const migrations: Migration[] = [
{ version: 1, up: (config) => { /* ... */ } },
{ version: 2, up: (config) => { /* ... */ } },
]
function applyMigrations(raw: Record<string, unknown>, migrations: Migration[]): Record<string, unknown>
Config file gets a version field. load() becomes:
readFile → JSON.parse → applyMigrations → zodValidate → Config
Tests:
- Each migration function: input fixture → expected output
- applyMigrations skips already-applied migrations
- applyMigrations applies in order
- Invalid config after migration → zod validation error (not silent corruption)
Files affected
src/core/config.ts— extract migrations, add version field- NEW:
src/core/config-migrations.ts - NEW:
tests/core/config-migrations.test.ts
Phase 5: CLI Command Modules
Problem
cli.tsis one big if/else chain for all commands
Design
src/cli/
index.ts — parse args, dispatch to command module
start.ts — start command
install.ts — plugin install
uninstall.ts — plugin uninstall
plugins.ts — list plugins
setup.ts — interactive setup
Each command: (args: string[], config: ConfigManager) => Promise<void>
CLI index parses argv, resolves command, calls handler.
Tests (optional — low risk):
- Each command module callable independently with mock dependencies
Files affected
src/cli.ts→src/cli/index.ts+ individual command files
Execution Order
Phase 1 (Event System)
↓
Phase 2 (Session Decomposition) — depends on Phase 1 events
↓
Phase 3 (Core ↔ Adapter) — can overlap with Phase 2
↓
Phase 4 (Config Migration) — independent
Phase 5 (CLI Commands) — independent
Branch Strategy
- Branch:
refactor/aggressive-cleanup - Each phase gets its own commits
- Tests written and passing BEFORE implementation changes
- Existing tests must continue to pass after each phase
Risk Mitigation
- TDD for Phases 1-4: tests first, then refactor
- Each phase is independently deployable
- If a phase causes issues, it can be reverted without affecting others