**Session** (`session.ts`) — Wraps an AgentInstance with a prompt queue (serial processing), auto-naming (asks agent to summarize after first prompt), and lifecycle management.
- **Forum topics**: Each session gets its own topic. System topics: Notifications, Assistant
- **MessageDraft** (`streaming.ts`): Buffers text chunks, sends periodic batch updates
- **Callback routing**: Permission buttons use `p:` prefix, menu buttons use `m:` prefix. Must use `bot.callbackQuery(/^prefix/)` to avoid blocking the middleware chain
- **Topic-first creation**: Create forum topic BEFORE `core.handleNewSession()` to prevent race condition
- **Test framework**: Vitest. Test files live in `src/**/__tests__/*.test.ts` or `src/**/*.test.ts`.
- **Objective tests**: Tests must be objective — verify behavior against specifications and expected outcomes, not against current implementation details. The source code is for understanding, but tests should validate what the code *should* do.
- **Test flows, not internals**: Focus on testing user-facing flows and integration between components rather than individual private methods. Test the public API surface.
- **Edge cases matter**: Always test boundary conditions, error paths, and state machine transitions. These are where bugs hide.
### What to Test
1.**State machines**: Test ALL valid transitions AND all invalid transitions. Verify events emitted on each transition.
2.**Flow tests**: Test complete user flows end-to-end (e.g., message → session lookup → prompt → response → notification).
3.**Error recovery**: Test that errors don't leave the system in a broken state. After an error, the system should be usable again.
4.**Concurrency**: Test serial processing guarantees, queue ordering, lock behavior, and race conditions.
5.**Boundary values**: Test exact boundaries (e.g., `maxConcurrentSessions` at exactly the limit, budget at exactly the threshold).
6.**Cleanup**: Test that resources (timers, listeners, files) are cleaned up properly.
7.**Idempotency**: Test double-calls (double connect, double disconnect, double resolve) are safe.
### How to Write Tests
```typescript
// Use vi.fn() for mocks, TypedEmitter for event-based mocks
Users đã cài và chạy các version cũ sẽ có config, data, storage ở trạng thái cũ. Khi thêm hoặc thay đổi bất kỳ thứ gì liên quan, **phải đảm bảo tương thích ngược**:
- **Config** (`~/.openacp/config.json`): Khi thêm field mới vào Zod schema, luôn đặt `.default()` hoặc `.optional()` để config cũ không bị validation error. Không bao giờ đổi tên hoặc xóa field mà không có migration.
- **Storage / Data files** (`~/.openacp/`): Khi thay đổi format dữ liệu (sessions, topics, state...), phải handle được format cũ — đọc được data cũ và tự động migrate sang format mới nếu cần. Không được crash khi gặp data từ version trước.
- **CLI flags & commands**: Không xóa hoặc đổi tên command/flag đang có. Nếu deprecate thì vẫn giữ hoạt động và log warning.
- **Plugin API**: Khi thay đổi interface mà plugin dùng, phải giữ backward compat hoặc bump major version.
- **Nguyên tắc chung**: Code mới phải chạy được với data/config cũ mà không cần user làm gì thêm. Nếu cần migration, tự động chạy khi khởi động.