Skip to content

feat: handoff drain policy for session lifecycle #679

@rileywhite

Description

@rileywhite

Before you continue

  • I searched existing issues and did not find a duplicate.
  • I can explain the user problem this solves, not just the implementation I prefer.

Problem to solve

With defer_until_idle (from #504 / PR #654), the reconciler can delay draining an active session — but when the grace timeout expires, it still force-drains. For sessions where context continuity matters (crew-owner sessions with a human attached, long-running coordination agents like mayor), a forced drain loses accumulated context that is expensive or impossible to reconstruct.

There is currently no way to tell the reconciler "drain this session, but preserve its context first."

Proposed change

Add a third drain_policy value: handoff.

[agent.lifecycle]
drain_policy = "handoff"
grace_timeout = "5m"

When the reconciler decides to drain a session with drain_policy = "handoff":

  1. Trigger gc handoff on the session instead of a bare drain+stop
  2. The session dumps its context (conversation state, in-progress work references) before terminating
  3. On restart, the new session can recover from the handoff artifact

This extends the guard-clause pattern established by immediate and defer_until_idle but requires new plumbing: the reconciler must orchestrate a handoff before proceeding with the drain, rather than simply skipping or proceeding.

Alternatives considered

  • defer_until_idle with long grace timeout: Delays the problem but doesn't solve it — eventually the timeout fires and context is lost anyway.
  • PostToolUse hook checking env var (from config-drift can drain active named sessions without graceful handoff #119): Couples hook system to reconciler internals, fires only after tool completion, fails silently if hooks aren't configured.
  • Prompt injection signaling imminent drain: Wastes context every turn for a rare event, requires session cooperation.

Scope and impact

Blocked by: #504 (PR #654)

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureNew capabilitypriority/p3Backlog — good idea, reviewed when there's bandwidth

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions