Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/entire/cli/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ func handleSessionStartCommon() error {
if state, loadErr := strategy.LoadSessionState(input.SessionID); loadErr != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to load session state on start: %v\n", loadErr)
} else if state != nil {
strategy.TransitionAndLog(state, session.EventSessionStart, session.TransitionContext{})
if transErr := strategy.TransitionAndLog(state, session.EventSessionStart, session.TransitionContext{}, session.NoOpActionHandler{}); transErr != nil {
fmt.Fprintf(os.Stderr, "Warning: session start transition failed: %v\n", transErr)
}
if saveErr := strategy.SaveSessionState(state); saveErr != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to update session state on start: %v\n", saveErr)
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/entire/cli/hooks_claudecode_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,9 @@ func transitionSessionTurnEnd(sessionID string) {
if turnState == nil {
return
}
strategy.TransitionAndLog(turnState, session.EventTurnEnd, session.TransitionContext{})
if err := strategy.TransitionAndLog(turnState, session.EventTurnEnd, session.TransitionContext{}, session.NoOpActionHandler{}); err != nil {
fmt.Fprintf(os.Stderr, "Warning: turn-end transition failed: %v\n", err)
}

// Always dispatch to strategy for turn-end handling. The strategy reads
// work items from state (e.g. TurnCheckpointIDs), not the action list.
Expand All @@ -774,7 +776,9 @@ func markSessionEnded(sessionID string) error {
return nil // No state file, nothing to update
}

strategy.TransitionAndLog(state, session.EventSessionStop, session.TransitionContext{})
if transErr := strategy.TransitionAndLog(state, session.EventSessionStop, session.TransitionContext{}, session.NoOpActionHandler{}); transErr != nil {
fmt.Fprintf(os.Stderr, "Warning: session stop transition failed: %v\n", transErr)
}

now := time.Now()
state.EndedAt = &now
Expand Down
73 changes: 59 additions & 14 deletions cmd/entire/cli/session/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,31 +249,76 @@ func transitionFromEnded(event Event, ctx TransitionContext) TransitionResult {
}
}

// ApplyCommonActions applies the common (non-strategy-specific) actions from a
// TransitionResult to the given State. It updates Phase, LastInteractionTime,
// and EndedAt as indicated by the transition.
//
// Returns the subset of actions that require strategy-specific handling
// (e.g., ActionCondense, ActionWarnStaleSession).
// The caller is responsible for dispatching those.
func ApplyCommonActions(state *State, result TransitionResult) []Action {
// ActionHandler defines strategy-specific side effects for state transitions.
// The compiler enforces that every strategy-specific action has a handler.
type ActionHandler interface {
HandleCondense(state *State) error
HandleCondenseIfFilesTouched(state *State) error
HandleDiscardIfNoFiles(state *State) error
HandleWarnStaleSession(state *State) error
}

// NoOpActionHandler is a default ActionHandler where all methods are no-ops.
// Embed this in handler structs to only override the methods you need.
type NoOpActionHandler struct{}

func (NoOpActionHandler) HandleCondense(_ *State) error { return nil }
func (NoOpActionHandler) HandleCondenseIfFilesTouched(_ *State) error { return nil }
func (NoOpActionHandler) HandleDiscardIfNoFiles(_ *State) error { return nil }
func (NoOpActionHandler) HandleWarnStaleSession(_ *State) error { return nil }

// ApplyTransition applies a TransitionResult to state: sets the new phase
// unconditionally, then executes all actions. Common actions
// (UpdateLastInteraction, ClearEndedAt) always run regardless of handler
// errors so that bookkeeping fields stay consistent with the new phase.
// Strategy-specific handler actions stop on the first error; subsequent
// handler actions are skipped but common actions continue. Returns the
// first handler error, or nil.
func ApplyTransition(state *State, result TransitionResult, handler ActionHandler) error {
state.Phase = result.NewPhase

var remaining []Action
var handlerErr error
for _, action := range result.Actions {
switch action {
// Common actions: always applied, even after a handler error.
case ActionUpdateLastInteraction:
now := time.Now()
state.LastInteractionTime = &now
case ActionClearEndedAt:
state.EndedAt = nil
case ActionCondense, ActionCondenseIfFilesTouched, ActionDiscardIfNoFiles,
ActionWarnStaleSession:
// Strategy-specific actions — pass through to caller.
remaining = append(remaining, action)

// Strategy-specific actions: skip remaining after the first handler error.
case ActionCondense:
if handlerErr == nil {
if err := handler.HandleCondense(state); err != nil {
handlerErr = fmt.Errorf("%s: %w", action, err)
}
}
case ActionCondenseIfFilesTouched:
if handlerErr == nil {
if err := handler.HandleCondenseIfFilesTouched(state); err != nil {
handlerErr = fmt.Errorf("%s: %w", action, err)
}
}
case ActionDiscardIfNoFiles:
if handlerErr == nil {
if err := handler.HandleDiscardIfNoFiles(state); err != nil {
handlerErr = fmt.Errorf("%s: %w", action, err)
}
}
case ActionWarnStaleSession:
if handlerErr == nil {
if err := handler.HandleWarnStaleSession(state); err != nil {
handlerErr = fmt.Errorf("%s: %w", action, err)
}
}
default:
if handlerErr == nil {
handlerErr = fmt.Errorf("unhandled action: %s", action)
}
}
}
return remaining
return handlerErr
}

// MermaidDiagram generates a Mermaid state diagram from the transition table.
Expand Down
Loading