← Back to PRs

#15571: feat: infrastructure foundation — hooks, model failover, sessions, embedded runner

by tangcruz open 2026-02-13 16:18 View on GitHub →
channel: telegram gateway cli commands agents stale size: XL
## Summary Foundational infrastructure layer adding several interconnected systems. Grouped as a single PR because these modules share type dependencies. ### Hook lifecycle system - `src/hooks/internal-hooks.ts`: event bus emitting `message:received`, `message:sent`, `model:request`, `model:response`, `session:updated`, etc. - `src/plugins/hooks.ts` + `types.ts`: plugin hook bridge + `on_model_failover` interface - Integration points in gateway startup, server channels, node events, outbound delivery, Telegram dispatch ### Model failover + task classifier - `src/agents/model-fallback.ts`: multi-tier failover with configurable fallback chains, synthetic model routing, provider-aware retry - `src/agents/task-classifier.ts`: classifies tasks for optimal model selection - `src/agents/agent-scope.ts`: scope-aware model selection ### Embedded runner improvements - `background-optimization.ts`: background task optimization for embedded sessions - Enhanced `history.ts`, `pi-settings.ts`, `run.ts` with structured context integration ### In-flight tracker + graceful shutdown - `src/infra/in-flight-tracker.ts`: tracks pending HTTP/WebSocket requests - `src/cli/gateway-cli/run-loop.ts`: waits for in-flight completion before shutdown ### Session store enhancements - TTL-based cache, path mapping (host↔container), session maintenance (prune/cap/rotate) - `src/agents/path-map.ts`: bidirectional path translation ### Config & plumbing - Extended types (`types.agents.ts`, `types.openclaw.ts`) and schemas - Various integration points: memory tool, dispatch, commands, onboarding ## Test plan - [ ] Type check passes (`tsgo`) - [ ] Lint passes (`oxlint`) - [ ] Unit tests pass for model-fallback, pi-settings, background-optimization, in-flight-tracker, session keys, cache utils - [ ] Gateway shutdown gracefully waits for in-flight requests - [ ] Hook events fire correctly on message/model lifecycle - [ ] Session store cache invalidates on write 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR introduces a foundational infrastructure layer encompassing hook lifecycle, model failover with smart routing, session management, embedded runner optimization, and graceful shutdown. The changes are well-structured with comprehensive test coverage. Previous thread concerns about hook type safety and in-flight tracker cleanup have been addressed. Key additions: - **Hook system**: `internal-hooks.ts` provides event-driven hooks (`message:received`, `model:select`, `model:failover`) with plugin bridge integration - **Model failover**: Multi-tier failback with task classification, auth profile cooldown awareness, and hook-based smart routing - **Embedded runner**: Background optimization triggers compaction proactively based on turn count and time thresholds - **In-flight tracker**: Tracks pending requests and ensures graceful shutdown with configurable timeout (previous cleanup issues resolved) - **Session store**: TTL-based caching, path mapping for container environments, and maintenance utilities - **Spawn patch**: Global `closeOnExec: true` injection to prevent fd leaks across all spawn calls The implementation follows project conventions with colocated tests, proper error handling, and clear separation of concerns. <h3>Confidence Score: 4/5</h3> - This PR is safe to merge with minor considerations - Score reflects solid implementation with comprehensive tests, proper error handling, and resolved previous concerns. The infrastructural changes are well-integrated across multiple subsystems. Minor points to verify: spawn-patch global mutation impact, path-map edge cases with Windows paths, and task classifier pattern effectiveness in production. - Pay close attention to `src/infra/spawn-patch.ts` (global mutation), `src/agents/path-map.ts` (cross-platform path handling), and `src/agents/task-classifier.ts` (pattern effectiveness) <sub>Last reviewed commit: a1ac166</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs