---
description: Canonical Session Start and Session Exit protocol for every /opsx:* session
alwaysApply: true
---

# Session Handoff — canonical protocol (HARD STOP)

This rule is the single source of the Session Start / Session Exit protocol. Commands reference it; they do not duplicate it. A `/opsx:*` session is incomplete without persist + the fenced next-thread prompt. FORBIDDEN until persist succeeds: done/готово, next phase, or omitting the prompt.

Agents (local or cloud) write session artifacts only to git-tracked paths — never `/tmp`, never gitignored caches.

## Session Start (before any work)
1. Honor pasted `/opsx:<phase> <name>` and announce the role.
2. `npx agent-orchestrator-kit handoff <name> --restore` (or `handoff --restore` with one active change). The CLI briefing is canonical — it prints handoff.md (Done, Attach), the last 10 entries of git-tracked `openspec/changes/<name>/decisions.md` (older entries stay in the file) and the Memory JSON count. No separate Memory MCP read step. Restore also locks the session client (`cursor` / `claude` / `amp`) into `metrics.json` `pending` — persist will follow that client’s spend flow. Override with `--platform` when detection is wrong.
3. Only if the restore CLI failed → read `openspec/changes/<name>/handoff.md` directly; spawn `session-handoff` in restore mode ONLY if both are unavailable (Amp: isolated `subagent-session-handoff`) — a fallback, never a routine step. Then start phase work (spawn a specialist when the phase routing requires one); free-form continue/next/«далі» with one active change → execute `Handoff.next_command`. An opt-in agent-hook briefing is advisory and metrics-neutral; it never replaces this restore or the Exit persist.

## Session Exit (order)
1. The parent writes `openspec/changes/<name>/handoff.md` itself: Closed role, Change, Done, Decisions, Blocked, Next command, Next role, Attach, Subagents to spawn, Constraints, Runtime.
2. Put decisions in `## Decisions`; only the CLI writes `decisions.md`. Do not put token counts in `handoff.md`.
3. `npx agent-orchestrator-kit handoff <name> --model <llm-product-id>` — require exit 0 (appends non-empty Decisions into append-only `openspec/changes/<name>/decisions.md`, upserts absolute-path Memory JSON, records the session into `openspec/changes/<name>/metrics.json`, prints the expanded prompt on stdout). `--model` is the LLM product id of this chat (`claude-opus-5`, `claude-fable-5`, `gpt-5.6-sol`, `cursor-grok-4.6-xhigh-fast`, `accounts/fireworks/models/glm-5p2`) — NEVER pass a Closed role, a subagent name, or an Amp **mode** (`low`, `medium`, `high`, `ultra`) as `--model`. The parent SHOULD still pass `--model`. The parent MUST NOT guess tokens. Persist collects spend for the client locked at restore (Amp: `amp threads export` + local threads; Cursor: hook file; Claude: JSONL). Where the client has no adapter data (Cursor gaps), `--model`, `--platform` and the spend flags are the manual fallback: `--input-tokens` / `--output-tokens` / `--total-tokens` / `--cost-usd` override session-level totals only and do not wipe platform maps. Optional `--platform cursor|claude|amp` or `AOK_PLATFORM`. Optional `--collect` runs all three adapters, not only the locked client. The same `npx agent-orchestrator-kit handoff <name>` works in Cursor, Claude Code, and Amp. `decisions.md` is the git canon of change decisions; Memory `Decision:*` is a file→Memory mirror only. Cloud sessions pass `--runtime cloud` (or set `AOK_RUNTIME=cloud` / `AOK_AGENT_ID` in the cloud-agent environment).
4. Spawn `session-handoff` in persist mode ONLY if step 3 failed (Amp: isolated `subagent-session-handoff`). Fallback, never routine; the CLI prompt names it only when `handoff.spawn_handoff_subagent: true` in `orchestrator.yaml`.
5. Memory MCP is an optional mirror: if tools are available, update `Change:<name>`, `Handoff:<name>`, `Decision:*` in one call; unavailability never blocks closing.
6. Paste CLI stdout as one fenced block. First line `/opsx:…` (green apply: the CLI prints one archive line for a terminal). Body uses `project.agent_language`. Self-contained (Done/Blocked/Attach/spawn/HARD STOP; Decisions come from restore, not from the prompt). No banner.
7. If runtime is cloud: after persist, `git add openspec/changes/<name>/` → `git commit` → `git push` → `npx agent-orchestrator-kit handoff <name> --cloud-check` (exit 0 required). Closing without this is an incomplete handoff. Persist prints these steps on stderr; the CLI never runs `git commit` / `git push`.
8. Stop. Next role and any out-of-OpenSpec hotfix = new chat. Never run full persist twice; regenerate the prompt only with `handoff <name> --no-metrics`.

## Archive exception
`npx agent-orchestrator-kit archive <name>` (terminal/CI; `/opsx:archive` = fallback) writes the final `handoff.md` (`next_command: none`) in the archive folder and upserts memory itself. After a successful archive no fenced next-prompt is required — the pipeline is complete.

Memory MCP: launcher `node scripts/memory-mcp-launcher.cjs`, never a relative `MEMORY_FILE_PATH`.
