#15571: feat: infrastructure foundation — hooks, model failover, sessions, embedded runner
channel: telegram
gateway
cli
commands
agents
stale
size: XL
Cluster:
Security Enhancements and Guardrails
## 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
#6405: feat(security): Add HTTP API security hooks for plugin scanning
by masterfung · 2026-02-01
80.6%
#13889: feat: Slack channel cache, session cost alerts & checkpoint/recover...
by trevorgordon981 · 2026-02-11
80.2%
#10679: fix(hooks): invoke gateway_start and gateway_stop in lifecycle
by yassinebkr · 2026-02-06
79.5%
#11153: refactor(hooks): replace console.warn/error with subsystem logger
by hclsys · 2026-02-07
79.0%
#6095: feat(gateway): support modular guardrails extensions for securing a...
by Reapor-Yurnero · 2026-02-01
78.9%
#7771: Hooks: wire lifecycle events and tests
by rabsef-bicrym · 2026-02-03
78.7%
#16915: fix: await compaction hooks with timeout to prevent cross-session d...
by maximalmargin · 2026-02-15
78.6%
#16618: feat: bridge message lifecycle hooks to workspace hook system
by DarlingtonDeveloper · 2026-02-14
78.1%
#19565: feat: add agent lifecycle hook events (session, message, error)
by tag-assistant · 2026-02-17
78.0%
#9914: fix(hooks): resolve bundled hook dist paths and packaging checks
by zimmra · 2026-02-05
77.7%