# Feature: Plan attachment (Phase 1, after the approval gate)

The approved plan goes onto the issue the run started from, so teammates and reviewers read the plan the code follows on the ticket itself. `scripts/plan-attachment.mjs` does the work; the orchestrator only asks the question when the script says to.

## When it runs

Once per approved plan, after Step 13 passes: the Approve option (which sets `phases["1"].planApprovedAt`), or, in autopilot, the policy skip (which sets `phases["1"].gateSkippedAt`). plan.json's own `approved` flag is not evidence. A draft never leaves the machine: a plan with neither record, including an autopilot plan whose gate was shown and not yet answered, is refused with `plan not attached: the plan has not passed the approval gate`. The guarantee does not rest on step order alone: every write of plan.json clears both records first, and the script also refuses a record older than plan.json's last write (compared to the second), so an approval never covers a plan written after it. A plan redone later (an edit after approval, a resume that re-plans) runs it again and becomes the next version.

## What goes where

| Run started from | What the issue gets |
|------------------|---------------------|
| Jira issue | the attachment `plan-<taskId>-v<N>.md`, uploaded by `plan-attachment.mjs upload` |
| GitHub issue | one comment through `gh api`: a summary line (`Approved development plan vN`, task count, approval time), then the plan inside `<details>` |
| Free text | nothing, and nothing is recorded |

The input type decides the target when a run has both (a GitHub issue with a linked Jira key goes to the GitHub issue). The Jira host is a configured one, `prefs.global.hosts.jira` or a `prefs.global.accounts[].jiraHost`, and must be https (loopback excepted). `JIRA_HOST` or `ACCOUNT_JIRA_HOST` picks one of them in every mode; any other value is refused with `plan not attached: the host in the environment is not a configured host`. Host and token are one account's pair and are never mixed: `ACCOUNT_JIRA_TOKEN_KEY` names its account by its prefix (`<Prefix>_Jira_..._Token` is account `<prefix>`, as `account-resolver.sh` derives it) and that account's host is used (its `jiraHost`, else `hosts.jira`); with no key, `keychainMapping.jira` goes with `hosts.jira` only. A host from another account than the token is refused with `plan not attached: the Jira host and token belong to different accounts`. A `JIRA_TOKEN` value is the caller's own pair with the host it picked. On the GitHub path, `GH_HOST` other than `github.com` is refused when attended and dropped when not. The token travels only in a request header (a 0600 header file on the curl fallback, removed afterwards), never on argv or in output. curl is used only when fetch failed before the request went out (no fetch, connection refused, DNS, TLS); a timeout or an error after the request started is recorded as `failed` with the status unknown and is not resent. The GitHub comment makes `@name` mentions and `<details>`/`<summary>` tags inert outside code, and carries no `Ref: #N` footer, so it is never taken for the Phase 5 issue comment.

## Versions

Each changed plan is written to `<run dir>/plan-attachments/plan-<taskId>-v<N>.md`, N one above the highest file there. Earlier versions are never deleted, locally or on the issue. Uploading a plan identical to the last version, already attached, does nothing (a resumed step does not post twice); a version whose upload failed is retried under the same number. Before a Jira upload the issue's attachments are listed: a file with the same name and size counts as attached (`reason: already on the issue`) and is not sent again, a same-named file with other content moves the upload to the next free version, and a failed listing is recorded as failed without uploading. A GitHub comment ends with a hidden marker, `<!-- multi-agent-plan:<taskId>:v<N>:<first 12 hex of the plan's sha256> -->`; before posting, every page of the issue's comments is read (100 per page), a comment carrying the same marker counts as attached and nothing is posted, and a failed listing posts nothing and is recorded as failed with the status unknown. A gh call that times out, or exits without an HTTP status, is recorded the same way and is not retried.

## The preference

`prefs.projects.<project>.planAttachment`, else `prefs.global.planAttachment`, else `ask`:

| Value | Attended run | Run that cannot ask (`MULTI_AGENT_UNATTENDED=1`, or autopilot) |
|-------|--------------|------------------------------------------------------------------|
| `ask` | one `AskUserQuestion`, then the answer holds for the rest of the run | skipped: `plan not attached: planAttachment=ask in an unattended run` |
| `always` | attached | attached, except with `MULTI_AGENT_UNATTENDED=1` when `unattended-policy.json` lists `plan-attachment.mjs` with `upload` (the shipped default): skipped and reported |
| `never` | skipped | skipped |

An unsupported value is treated as a refusal. To let unattended runs attach, a deployment removes the `plan-attachment.mjs` entry from `publisherScripts`; agent-guard then lets the call through as well.

## Steps

```bash
node $HOME/.claude/scripts/plan-attachment.mjs decide --state "$STATE_FILE"
```

It prints `{"action": ...}`:

- `none` (free text) or `skip` (already recorded with its reason): go on to Phase 2.
- `ask`: one native `AskUserQuestion` that follows `$HOME/.claude/multi-agent-refs/picker-contract.md`, `header: "Plan attach"`; `question`, option labels and descriptions in `outputLanguage` (rules.md): attach the approved plan to `{issue}`, or keep it local. Then `upload ... --answer yes|no`.
- `attach`: `upload` without `--answer`.

```bash
node $HOME/.claude/scripts/plan-attachment.mjs upload --state "$STATE_FILE" --plan "$PLAN_FILE" [--answer yes|no]
```

Before anything is sent, the rendered text goes through the outbound gate's rules (`lib/outbound-gate.mjs` `scan`). Any finding stops the upload and records `plan not attached: the secret scan matched <rule> (line N)`; the matched value is never written anywhere, and no version file is kept.

## Outcome

Every attempt appends to `state.planAttachment.outcomes[]`: `attached` (attachment or comment id and URL), `skipped` (reason) or `failed` (HTTP status, or the transport cause). A failure never fails the run; every subcommand exits 0 except on a usage error. Phase 5 prints the last outcome with `plan-attachment.mjs line --state "$STATE_FILE"`, for example `Plan: v2 attached to PROJ-1 (<url>)`, `Plan: not attached (planAttachment=ask in an unattended run)` or `Plan: v1 upload failed (HTTP 500), the run went on`.
