# fix: harden session adopt review cases

## Sessions

## Changes

- cmd/entire/cli

- Msession_adopt.go+16/-4

- Msession_adopt_test.go+162

```
1
2
3
4
5
6
7
105 unmodified lines

```

```go
package cli

import (
	"bytes"
	"context"
	"errors"
	"fmt"
)

func isRecentAdoptCandidate(state *session.State) bool {
	if state == nil || state.Phase == session.PhaseEnded || state.FullyCondensed {
		return false
	}
	lastSeen := sessionLastSeen(state)
	return time.Since(lastSeen) <= adoptRecentWindow
}

func isAdoptableSourceSession(state *session.State) bool {
	return state != nil &&
		state.Phase != session.PhaseEnded &&
		state.EndedAt == nil &&
		!state.FullyCondensed
}

func sessionLastSeen(state *session.State) time.Time {
	if state.LastInteractionTime != nil {
		return *state.LastInteractionTime
	}

return time.Time{}
}

```

```go
func TestSessionAdopt_IdleSourceSurvivesPrepareCommitMsgTrailer(t *testing.T) {
	sourceRepo := setupAdoptRepo(t)
	targetRepo := setupAdoptRepo(t)

sessionID := "test-adopt-idle-source"
	targetRelPath := "src/idle.go"
	targetAbsPath := filepath.Join(targetRepo, targetRelPath)
	transcriptPath := filepath.Join(sourceRepo, ".claude", sessionID+".jsonl")
	if err := os.MkdirAll(filepath.Dir(transcriptPath), 0o750); err != nil {
		t.Fatal(err)
	}
	transcript := `{"type":"human","message":{"content":"write idle.go"}}`
	if err := os.WriteFile(transcriptPath, []byte(transcript), 0o600); err != nil {
		t.Fatal(err)
	}

lastInteraction := time.Now().Add(-1 * time.Minute)
	sourceStore := session.NewStateStoreWithDir(filepath.Join(sourceRepo, ".git", session.SessionStateDirName))
	if err := sourceStore.Save(context.Background(), &session.State{
		SessionID:             sessionID,
		AgentType:             agent.AgentTypeClaudeCode,
		StartedAt:             time.Now().Add(-5 * time.Minute),
		LastInteractionTime:   &lastInteraction,
		Phase:                 session.PhaseIdle,
		BaseCommit:            testutil.GetHeadHash(t, sourceRepo),
		AttributionBaseCommit: testutil.GetHeadHash(t, sourceRepo),
		WorktreePath:          sourceRepo,
	}); err != nil {
		t.Fatal(err)
	}
	
	// Further testing logic here...
}
```
