feat: add SessionStart/SessionEnd hooks for lifecycle tracking#359
feat: add SessionStart/SessionEnd hooks for lifecycle tracking#359
Conversation
Add SessionStart and SessionEnd capture hooks to Stage 3 execution, enabling session lifecycle timing data for diagnostics. Hooks follow the existing stateful hook pattern with per-scenario callbacks. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Claude finished @sjnims's task in 1m 46s —— View job PR Review: feat: add SessionStart/SessionEnd hooks for lifecycle tracking
Verdict: ✅ Looks goodThis is a clean, well-structured addition that follows the existing hook patterns precisely. Quality Checks
What's Good
SuggestionsSee inline comments for specific items:
Neither issue is blocking — overall this is a solid, focused addition. |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Address PR review feedback — use the SDK's ExitReason type instead of generic string for better type safety. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Addressed the type safety suggestion — changed from to the SDK's union type for compile-time validation. The duplication note is acknowledged but I'm leaving it as-is since it's only 2 call sites and marked as non-blocking. If we add more session timing construction logic later, we can extract a helper. |
|
Claude finished @sjnims's task in 1m 4s —— View job Re-review after update
Verdict: ✅ Looks goodThe previous suggestion has been addressed correctly. Quality Checks
Changes VerifiedThe new commit No other changes were introduced — the rest of the diff is identical to the previous review. The previous approval stands. |
|
Addressed the ExitReason type safety suggestion — changed SessionEndCapture.reason from string to the SDK ExitReason union type for compile-time validation. The duplication note is acknowledged but leaving as-is since it is only 2 call sites and marked as non-blocking. If we add more session timing construction logic later, we can extract a helper. |
Description
Add
SessionStartandSessionEndcapture hooks to Stage 3 execution, enabling session lifecycle timing data for diagnostics. These purely observational hooks follow the existing stateful hook pattern (likeStop), using per-scenario callbacks to push timing data to scenario-scoped arrays.New types:
SessionStartCapture,SessionEndCapture,SessionTimingCapture— captured via SDK hooks and stored assession_timing?onExecutionResult.Key design decisions:
startsis an array —/cleartriggersSessionStartwithsource: 'clear', so multiple starts per session are possible in batch modeType of Change
Component(s) Affected
Pipeline Stages
src/stages/3-execution/)Core Infrastructure
src/types/)Other
tests/)Motivation and Context
Session timing currently relies on manual
Date.now()calls that conflate subprocess startup, plugin loading, MCP initialization, and actual LLM execution into a single duration metric. SDK v0.2.25 providesSessionStartandSessionEndhooks that fire at precise lifecycle boundaries, enabling accurate timing breakdown.Closes #349
How Has This Been Tested?
Test Configuration:
Test Steps:
npm run build— compiles successfullynpm run typecheck— no type errorsnpm run lint— no lint issuesnpm run format:check— all files formattednpm run knip— no dead codenpm test— 1650/1650 tests passChecklist
General
TypeScript / Code Quality
npm run typecheck)_anytypes without justificationDocumentation
Linting
npm run lintand fixed all issuesnpm run format:checkTesting
npm testand all tests passStage-Specific Checks
Stage 3: Execution (click to expand)
API & SDK Changes
SDK Integration (click to expand)
SessionStartHookInputandSessionEndHookInputtypes imported from SDK v0.2.25Partial<Record<HookEvent, HookCallbackMatcher[]>>patternFiles Changed (9 files, +625/-2)
src/types/transcript.tsSessionStartCapture,SessionEndCapture,SessionTimingCapturetypes;session_timing?onExecutionResultsrc/types/index.tssrc/stages/3-execution/sdk-client.tsSessionStartHookInput,SessionEndHookInput; add config type aliasessrc/stages/3-execution/tool-capture-hooks.tscreateSessionStartHook/createSessionEndHookfactoriessrc/stages/3-execution/hooks-factory.tsSDKHooksConfig,CaptureHooksOptions,StatefulHooks,StatefulHooksOptions; updated all factory functionssrc/stages/3-execution/agent-executor.tsExecutionContextandbuildExecutionResultsrc/stages/3-execution/session-batching.tstests/.../tool-capture-hooks.test.tstests/.../hooks-factory.test.tsReviewer Notes
Areas that need special attention:
session_timingfield is populated but not yet consumed by Stage 4 evaluation. Follow-up issues [Feature]: Expose session timing metrics in Stage 4 evaluation reports #357 and [Feature]: Add session timing breakdown to dry-run cost estimation #358 track that work.Known limitations or trade-offs:
batched_by_componentmode, the/clearcommand between scenarios is sent via a separate query without capture hooks, soSessionStartwithsource: 'clear'from/clearwon't be captured. This is expected —/clearis infrastructure, not part of the scenario being tested.session_timingstaysundefined. No special handling needed.🤖 Generated with Claude Code