diff --git a/.claude/.idd/attachments/issue-547/_manifest.json b/.claude/.idd/attachments/issue-547/_manifest.json new file mode 100644 index 00000000..964bd93d --- /dev/null +++ b/.claude/.idd/attachments/issue-547/_manifest.json @@ -0,0 +1,6 @@ +{ + "issue": 547, + "fetched_at": "2026-11-14T01:43:28Z", + "fetched_by": "idd-diagnose", + "files": [] +} diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix1/.openspec.yaml b/openspec/changes/archive/2026-11-14-idd-547-fix1/.openspec.yaml new file mode 100644 index 00000000..1a60d392 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix1/.openspec.yaml @@ -0,0 +1,5 @@ +schema: spec-driven +created: 2026-11-14 +created_by: che cheng +archived_by: che cheng +archived_at: 2026-11-14 diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix1/proposal.md b/openspec/changes/archive/2026-11-14-idd-547-fix1/proposal.md new file mode 100644 index 00000000..c0b027f3 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix1/proposal.md @@ -0,0 +1,47 @@ +**GitHub-side tracker**: #547(第 1 輪 verify:comment 6871443624;本輪計畫:comment 6871456743) + +## Problem + +第 1 輪 verify 找到 1 項 Blocking 與 24 項 In-scope fix:`archive-ref` 對兩個指向同一路徑的 key 回 `error`,requirement 卻沒寫這一種;沒設 `spec_archive` 的 repo,設定檔只要是空檔、`null`、帶 BOM 或非 UTF-8 就被擋下,違反「沒設就不變」;`link-base` 先驗 `--issue` 再看設定,讓沒設定的 repo 也可能在連結檢查停下;`branch` 接受 revision 語法;`#N` 沒有左邊界;有幾項規則沒有測試能分辨對錯(讀的是設定的 branch 還是 HEAD、Step 1.5 有沒有真的把旗標傳給 helper)。 + +## Root Cause + +第一版把「設定讀不了」與「設定寫錯」一起當成 `error`,但前者在沒設 `spec_archive` 的 repo 也會發生;把 `--issue` 的驗證放在設定解析之前;branch 只檢查存在;測試的 fixture 只有一條 branch、`HEAD` 永遠在設定的 branch 上,分不出兩種讀法。 + +## Proposed Solution + +修改 `spec-on-every-issue` 的兩條 requirement(見 specs/),並照著改 `spec-change.py`、`check-existing-work.sh`、idd-close Step 1.57、spec-wrapper 第 5 步、idd-all Step 3b.6、config-protocol,補齊各 Example 的測試。 + +## Non-Goals + +- 設定檔的擁有者與權限檢查、`idd-all`/`idd-implement` 在沒有 remote 的 repo 的 `unknown`:Follow-up。 +- ASSUMPTION: 不升版號,CHANGELOG 的 Unreleased 條目照實補充。 + +## Success Criteria + +- 新增與改動的測試在改程式之前失敗,改之後通過;run-all-tests 全過;links 回 ok。 + +## Capabilities + +### New Capabilities + +(none) + +### Modified Capabilities + +- `spec-on-every-issue`: 「Close requires the issue's change to be archived」與「A repository without a remote can opt into its local default branch」。 + +## Impact + +- Affected specs: spec-on-every-issue(MODIFIED 兩條) +- Affected code: + - Modified: plugins/issue-driven-dev/scripts/spec-change.py + - Modified: plugins/issue-driven-dev/scripts/check-existing-work.sh + - Modified: plugins/issue-driven-dev/skills/idd-close/SKILL.md + - Modified: plugins/issue-driven-dev/skills/idd-all/SKILL.md + - Modified: plugins/issue-driven-dev/references/spec-wrapper.md + - Modified: plugins/issue-driven-dev/references/config-protocol.md + - Modified: plugins/issue-driven-dev/scripts/tests/spec-archive-local/test.sh + - Modified: plugins/issue-driven-dev/scripts/tests/spec-close-gate/test.sh + - Modified: plugins/issue-driven-dev/scripts/tests/check-existing-work/test.sh + - Modified: plugins/issue-driven-dev/CHANGELOG.md diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix1/specs/spec-on-every-issue/spec.md b/openspec/changes/archive/2026-11-14-idd-547-fix1/specs/spec-on-every-issue/spec.md new file mode 100644 index 00000000..47e8e9ae --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix1/specs/spec-on-every-issue/spec.md @@ -0,0 +1,99 @@ +## MODIFIED Requirements + +### Requirement: Close requires the issue's change to be archived + +`idd-close` SHALL fetch the issue's comments with `gh api --paginate`, SHALL choose the Implementation Complete comment with `spec-change.py ic` (requirement "One way to choose the Implementation Complete comment"), and SHALL run `spec-change.py archived` on it with `--ref` set to the default branch as the remote has it. It SHALL close the issue only when the command prints `pass`. On `refuse` it SHALL stop and name the changes that are not archived or the fault in the `**Spec**:` line; on `error` it SHALL stop and say the default branch could not be read. For a comment that declares a change, it SHALL first ask `spec-change.py archive-ref` which branch to read (requirement "A repository without a remote can opt into its local default branch"). When the answer is `remote`, it SHALL read the default branch by fetching it from the remote and checking the commit that fetch returned, and SHALL stop when `gh repo view` cannot name the default branch or the fetch fails; the reason it prints SHALL then include the reason `archive-ref` gave for answering `remote`, so that a repository meant for the local branch whose entry was not found says why. When the answer is `local`, it SHALL check the commit `archive-ref` returned for the configured branch, not `HEAD`, SHALL NOT fetch, and SHALL print that a local ref was used, with the branch, the commit and the entry's key, and without the configuration file's path. When the answer is `error`, or `archive-ref` prints nothing a JSON reader can take, it SHALL stop and print the reason. For any other comment the default branch is not read, since `archived` answers from the comment alone. When the comments cannot be fetched or `ic` exits 2, it SHALL stop and say the comments could not be read; it SHALL NOT treat that as an issue with no Implementation Complete comment. An issue with no Implementation Complete comment is not held to this rule. A retroactive close (`idd-close --retroactive`, for an issue that is already closed) skips this gate, as it skips the gates before it. + +#### Scenario: An unarchived change holds the close + +- **GIVEN** an Implementation Complete comment with the marker and `**Spec**: change idd-42-fix-timeout` +- **AND** `openspec/changes/idd-42-fix-timeout/` still exists on the default branch +- **WHEN** `idd-close` runs its gates +- **THEN** it does not close the issue and says the change is not archived + +#### Scenario: A repository without a remote, opted in + +- **GIVEN** the code repository has no remote and the IDD configuration above it maps the repository's path to `{"ref": "local", "branch": "master"}` +- **AND** the change is archived on `master` +- **WHEN** `idd-close` runs its gates +- **THEN** the gate passes without fetching and prints the local branch and commit it checked + +##### Example: the close gate reads the default branch + +| Newest Implementation Complete comment | On the default branch | Close gate | +| --- | --- | --- | +| marker, `**Spec**: change idd-42-fix-timeout` | the change is archived | passes | +| marker, `**Spec**: change idd-42-fix-timeout` | archived on the issue's branch only, not yet merged | stops, naming the change | +| marker, no `**Spec**:` line | not read | stops, naming the missing line | +| no marker, no `**Spec**:` line | not read | passes | +| the issue has no Implementation Complete comment | not read | passes | +| the comments could not be fetched | not read | stops, saying the comments could not be read | +| marker, `**Spec**: change idd-42-fix-timeout` | the default branch cannot be fetched | stops, saying the default branch could not be read | +| marker, `**Spec**: no behaviour change` | the default branch cannot be fetched | passes; the branch is not read | +| marker, `**Spec**: change idd-42-fix-timeout` | the remote's default branch dropped the archive, the local tracking ref did not move | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts the repository into its local `master`, where the change is archived | passes; prints `local ref master@` | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts in, the change is not archived on `master` | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; opted into `master`; `HEAD` is on the issue's branch, where the change is archived, and `master` does not have it | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote and no `spec_archive` entry for the repository | stops, saying the default branch could not be read | +| marker, `**Spec**: change idd-42-fix-timeout` | the repository has a remote and the configuration opts it into `local` | stops and prints the reason | + +### Requirement: A repository without a remote can opt into its local default branch + +`spec-change.py archive-ref --cwd ` SHALL decide which branch the spec checks read for the repository that holds ``. The repository's path is its main working tree: for a linked worktree, the directory that holds the repository's common `.git` directory; otherwise its top level. It SHALL find the IDD configuration by walking up from that path, the way the skills walk up: at each directory below `/`, `.claude/.idd/local.json` first and then `.claude/issue-driven-dev.local.json`, stopping after the home directory, and SHALL use the first file found. The key it looks up is that path relative to the directory that holds the configuration's `.claude/` directory, with both paths resolved through symbolic links. A key in the file SHALL match when it names the same path after `.` and `..` segments and trailing `/` are normalised; `.` names the directory of the configuration itself; a key that is empty, only `/`, or has leading or trailing whitespace matches nothing. It SHALL print one JSON line and exit as follows: + +- `remote` (exit 0), with a `reason`, when no configuration file is found; when the file found cannot be read as a JSON object (it is empty, `null`, not JSON, not valid UTF-8, or nested too deeply; a UTF-8 byte order mark is read as usual); when the file has no `spec_archive` field; or when `spec_archive` has no entry for the key. Callers then read the default branch as the remote has it, as before; a repository without any remote then stops there, with this reason. +- `local` (exit 0), with `branch`, `ref` (the commit `refs/heads/` points at, whatever `HEAD` is), `config` and `key`, when exactly one entry matches the key, it is `{"ref": "local", "branch": ""}`, the repository has no remote, and the branch exists. +- `error` (exit 2), with a `reason`, when `spec_archive` is not an object; when more than one key matches; when the matching entry is not an object, its `ref` is not `local`, or its `branch` is missing, not a string, or not a valid branch name (`git check-ref-format --branch` must accept it unchanged, so revision syntax such as `master~1` or `master@{0}` is refused); when the repository has a remote; when the branch does not exist; or when `` is not inside a git repository. + +A repository has a remote when any `remote..url` is set in the git configuration it reads; a `remote` section without a URL (for example a global `remote.origin.prune`) does not count. The repository having a remote turns an entry into an error, not a silent `remote`, so that an entry written for a repository without a remote cannot outlive the reason it was written for. + +`spec-change.py link-base --cwd --issue ` SHALL give the base of the link check (`spec-change.py links --base`) in local mode. It SHALL resolve the configuration as `archive-ref` does first, and print `remote` (exit 0) or the same `error` (exit 2) when `archive-ref` would, without reading ``. In local mode it SHALL print `ok` (exit 0) with `base` and `from`: the commit of the baseline tag when that tag exists and is an ancestor of `HEAD` (`from: baseline-tag`); otherwise the parent of the oldest commit in the history of `HEAD` whose message contains `#` with no letter, digit, `_`, `/` or `-` right before the `#` and no digit after the number, the rule of `references/pr-issue-matching.md` (`from: first-commit`). The baseline tag is the configuration's `auto_tag.baseline_format` with `{N}` replaced by ``, or `idd--baseline` when that field is absent or not a string containing `{N}`. Commit messages SHALL be read so that no byte a message can hold hides a commit. It SHALL print `error` (exit 2) when neither base exists, when that oldest commit has no parent, or when `` is not a positive integer written in digits; an empty `` is a usage error (exit 64), like any empty argument of `spec-change.py`. It SHALL NOT fall back to `merge-base` with the branch: on a direct-commit run that is `HEAD` itself, and the range would be empty. + +`references/spec-wrapper.md` step 5 SHALL ask `link-base` first, with the issue number taken from `NUMBER`, or from `N` when `NUMBER` is empty, and SHALL use its `base` when it prints `ok`, SHALL keep the fetch of the default branch when it prints `remote`, and SHALL stop with the reason when it prints `error` or nothing a JSON reader can take, without also printing the message about a failed fetch. `references/config-protocol.md` SHALL document the `spec_archive` field, the key, and why an entry is accepted only for a repository without a remote. + +#### Scenario: An entry for a repository that later gained a remote + +- **GIVEN** a configuration entry `{"ref": "local", "branch": "master"}` for a repository +- **AND** a remote was added to that repository afterwards +- **WHEN** `archive-ref` runs for it +- **THEN** it prints `error` and names the remote, instead of reading the local branch + +##### Example: how a spec_archive entry resolves + +| Configuration found | Entry for the repository's path | Repository | `archive-ref` | +| --- | --- | --- | --- | +| none | — | no remote | `remote` | +| a file without `spec_archive` | — | no remote | `remote` | +| `spec_archive` names another path only | — | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local`, `master` and its commit | +| `spec_archive` names this path as `./sub/` | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | has a remote `origin` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "trunk"}` | no remote, no branch `trunk` | `error` | +| `spec_archive` names this path | `{"ref": "remote", "branch": "master"}` | no remote | `error` | +| `spec_archive` names this path | `{"ref": "local"}` | no remote | `error` | +| `spec_archive` is a list | — | no remote | `error` | +| the file is not JSON | — | no remote | `remote`, saying the file could not be read | +| the file is empty, or holds `null` | — | no remote | `remote` | +| the file starts with a UTF-8 byte order mark | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path as `sub` and as `./sub/` | both `{"ref": "local", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master~1"}` | no remote | `error`, not a branch name | +| `spec_archive` names this path as ` sub` | `{"ref": "local", "branch": "master"}` | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote; the global git configuration has `[remote "origin"]` with only `prune = true` | `local` | +| `spec_archive` names the main working tree | `{"ref": "local", "branch": "master"}` | asked from a linked worktree of it | `local` | +| the new file without `spec_archive`, the legacy file beside it with an entry | — | no remote | `remote` (the new file wins) | +| a file above the home directory with an entry | — | no remote | `remote` (not read) | + +##### Example: where the local link check starts + +| Repository (opted into local `master`) | `link-base --issue 14` | +| --- | --- | +| tag `idd-14-baseline` on an ancestor of `HEAD` | `ok`, the tag's commit, `from: baseline-tag` | +| no tag; commits `base`, `a (#14)`, `b (Refs #14)` | `ok`, the commit `base`, `from: first-commit` | +| no tag; the only commit naming a 14 says `#142` | `error` | +| no tag; the first commit with `#14` is the root commit | `error` | +| the repository is not opted in | `remote` | +| the repository is not opted in, `--issue abc` | `remote` | +| no tag; commits `base`, `see o/r#14`, `x#14`, `fix (#14)` | `ok`, the parent of `fix (#14)` | +| tag `idd-14-baseline` on a commit that is not an ancestor of `HEAD`; commits `base`, `a (#14)` | `ok`, the commit `base`, `from: first-commit` | +| `auto_tag.baseline_format` is `start-{N}`, tag `start-14` on an ancestor | `ok`, that tag's commit, `from: baseline-tag` | +| no tag; a commit before `a (#14)` has the byte 0x1e in its message | `ok`, the parent of `a (#14)` | diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix1/tasks.md b/openspec/changes/archive/2026-11-14-idd-547-fix1/tasks.md new file mode 100644 index 00000000..71197b61 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix1/tasks.md @@ -0,0 +1,21 @@ +## 1. 測試先行 + +- [x] 1.1 requirement「A repository without a remote can opt into its local default branch」— spec-archive-local:Example「how a spec_archive entry resolves」改動與新增的列(讀不了的設定回 remote、BOM、重複 key、revision 語法、空白 key、沒有 URL 的全域 remote 區段、linked worktree、新檔先於舊檔、HOME 以上不讀),帶標記 spec-example: spec-on-every-issue / how a spec_archive entry resolves。驗證:改之前新的列失敗。 +- [x] 1.2 spec-archive-local:Example「where the local link check starts」新增的列(remote 時不讀 `--issue`、左邊界、baseline 不是祖先時的退路、`baseline_format`、0x1e),並檢查 spec-wrapper 第 5 步呼叫 `link-base`,帶標記 spec-example: spec-on-every-issue / where the local link check starts。驗證:改之前新的列失敗。 +- [x] 1.3 requirement「Close requires the issue's change to be archived」— spec-close-gate:Example「the close gate reads the default branch」新增的兩列(HEAD 在 issue branch、沒有條目),測試設 HOME;remote 而 fetch 失敗時原因帶出 archive-ref 的說明;本機模式訊息不含設定檔路徑。驗證:改之前 HEAD 那一列與原因那一項失敗。 +- [x] 1.4 check-existing-work:Step 1.5 在本機模式真的把 `--skip-branches` 傳給 helper(`EW_JSON` 有 notes),open PR 仍被算到;重複 `--cwd` 回 2。驗證:拿掉旗標的變異會失敗。 + +## 2. 程式 + +- [x] 2.1 `spec-change.py` 照 requirement 改 `archive-ref` 與 `link-base`。驗證:1.1、1.2 通過;spec-change suite 照舊。 +- [x] 2.2 `check-existing-work.sh` 拒絕重複的 `--cwd`。驗證:1.4 通過。 + +## 3. skill 與文件 + +- [x] 3.1 idd-close Step 1.57 的原因與本機訊息。驗證:1.3 通過。 +- [x] 3.2 spec-wrapper 第 5 步(`${NUMBER:-${N:-}}`、錯誤訊息)、idd-all Step 3b.6 補一句。驗證:spec-wrapper-contract 照舊。 +- [x] 3.3 config-protocol 與 CHANGELOG。驗證:git diff 只改這一節與 Unreleased。 + +## 4. 收尾 + +- [x] 4.1 跑 run-all-tests.sh 與連結檢查。驗證:結束碼 0,links 回 ok。 diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix2/.openspec.yaml b/openspec/changes/archive/2026-11-14-idd-547-fix2/.openspec.yaml new file mode 100644 index 00000000..1a60d392 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix2/.openspec.yaml @@ -0,0 +1,5 @@ +schema: spec-driven +created: 2026-11-14 +created_by: che cheng +archived_by: che cheng +archived_at: 2026-11-14 diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix2/proposal.md b/openspec/changes/archive/2026-11-14-idd-547-fix2/proposal.md new file mode 100644 index 00000000..cbe56eb5 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix2/proposal.md @@ -0,0 +1,45 @@ +**GitHub-side tracker**: #547(第 2 輪 verify:comment 6872782744;本輪計畫:comment 6872785819) + +## Problem + +第 2 輪 verify 找到 1 項 Blocking:issue repo 與程式 repo 分開時,程式 repo 的 commit 以 `owner/repo#N` 引用 issue,`link-base` 的左邊界把這種寫法排除,本機模式的連結檢查起點永遠找不到。另有 19 項 In-scope fix:`git log` 沒關簽章顯示也沒分隔路徑、key 當不了時退回 `.`(放寬方向)、remote 的判定漏了 pushurl 與舊式檔案、baseline tag 可以就在 HEAD、`spec_archive` 字面重複的 key 被吞掉、原因文字帶絕對路徑與錯誤的標籤,以及幾項測試沒有真正測到 Example。 + +## Root Cause + +第 1 輪把 `#N` 的判斷照搬 `pr-issue-matching.md`,那份契約是給「PR 與 issue 在同一個 repo」用的;這個功能的前提正好是兩個 repo 分開。其餘是邊界沒有逐一列出。 + +## Proposed Solution + +修改 `spec-on-every-issue`「A repository without a remote can opt into its local default branch」(見 specs/),照著改 `spec-change.py`、idd-close Step 1.5/1.57、spec-wrapper 第 5 步、config-protocol,並補測試。 + +## Non-Goals + +- 設定檔的擁有者與權限檢查、`idd-all`/`idd-implement` 的 `unknown` 雜訊、`origin` 被 git 當成路徑:Follow-up。 +- ASSUMPTION: 不升版號。 + +## Success Criteria + +- 新的測試在改程式之前失敗,改之後通過;run-all-tests 全過;links 回 ok。 + +## Capabilities + +### New Capabilities + +(none) + +### Modified Capabilities + +- `spec-on-every-issue`: 「A repository without a remote can opt into its local default branch」。 + +## Impact + +- Affected specs: spec-on-every-issue(MODIFIED 一條) +- Affected code: + - Modified: plugins/issue-driven-dev/scripts/spec-change.py + - Modified: plugins/issue-driven-dev/skills/idd-close/SKILL.md + - Modified: plugins/issue-driven-dev/references/spec-wrapper.md + - Modified: plugins/issue-driven-dev/references/config-protocol.md + - Modified: plugins/issue-driven-dev/scripts/tests/spec-archive-local/test.sh + - Modified: plugins/issue-driven-dev/scripts/tests/spec-close-gate/test.sh + - Modified: plugins/issue-driven-dev/scripts/tests/check-existing-work/test.sh + - Modified: plugins/issue-driven-dev/CHANGELOG.md diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix2/specs/spec-on-every-issue/spec.md b/openspec/changes/archive/2026-11-14-idd-547-fix2/specs/spec-on-every-issue/spec.md new file mode 100644 index 00000000..bf89c0df --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix2/specs/spec-on-every-issue/spec.md @@ -0,0 +1,71 @@ +## MODIFIED Requirements + +### Requirement: A repository without a remote can opt into its local default branch + +`spec-change.py archive-ref --cwd ` SHALL decide which branch the spec checks read for the repository that holds ``. The repository's path is its main working tree: for a linked worktree (its git directory differs from the common git directory), the directory that holds the common `.git` directory; otherwise its top level, including a repository made with `--separate-git-dir`. It SHALL find the IDD configuration by walking up from that path, the way the skills walk up: at each directory below `/`, `.claude/.idd/local.json` first and then `.claude/issue-driven-dev.local.json`, stopping after the home directory, and SHALL use the first file found. The key it looks up is that path relative to the directory that holds the configuration's `.claude/` directory, with both paths resolved through symbolic links. A key in the file SHALL match when it names the same path after `.` and `..` segments and trailing `/` are normalised; `.` names the directory of the configuration itself; a key that is empty, only `/`, or has leading or trailing whitespace matches nothing. When the repository's own path cannot be written as a key (it has leading or trailing whitespace), the answer is `remote`; it SHALL NOT fall back to `.`. Reasons name the configuration file by its path relative to the repository, not by an absolute path. It SHALL print one JSON line and exit as follows: + +- `remote` (exit 0), with a `reason`, when no configuration file is found; when the file found cannot be read as a JSON object (it is empty, `null`, not JSON, not valid UTF-8, or nested too deeply; a UTF-8 byte order mark is read as usual); when the file has no `spec_archive` field; when `spec_archive` has no entry for the key; or when the repository's path cannot be written as a key. Callers then read the default branch as the remote has it, as before; a repository without any remote then stops there, with this reason. +- `local` (exit 0), with `branch`, `ref` (the commit `refs/heads/` points at, whatever `HEAD` is), `config` and `key`, when exactly one entry matches the key, it is `{"ref": "local", "branch": ""}`, the repository has no remote, and the branch exists. +- `error` (exit 2), with a `reason`, when `spec_archive` is not an object; when more than one key matches, including the same key written twice in the file; when the matching entry is not an object, its `ref` is not `local`, or its `branch` is missing, not a string, or not a valid branch name (`git check-ref-format --branch` must accept it unchanged, so revision syntax such as `master~1` or `master@{0}` is refused); when the repository has a remote; when the branch does not exist; or when `` is not inside a git repository. + +A repository has a remote when `git remote` lists a name that has a `url` or a `pushurl` in the git configuration it reads, or a legacy `remotes/` or `branches/` file in its git directory; a `remote` section without either (for example a global `remote.origin.prune`) does not count. The repository having a remote turns an entry into an error, not a silent `remote`, so that an entry written for a repository without a remote cannot outlive the reason it was written for. + +`spec-change.py link-base --cwd --issue ` SHALL give the base of the link check (`spec-change.py links --base`) in local mode. It SHALL resolve the configuration as `archive-ref` does first, and print `remote` (exit 0) or the same `error` (exit 2) when `archive-ref` would, without reading ``. In local mode it SHALL print `ok` (exit 0) with `base` and `from`: the commit of the baseline tag when that tag exists and is an ancestor of `HEAD` other than `HEAD` itself (`from: baseline-tag`); otherwise the parent of the oldest commit in the history of `HEAD` whose message names issue ``: `#` with no letter, digit, `_`, `/` or `-` right before the `#`, the rule of `references/pr-issue-matching.md`, or `/#` where `/` is the configuration's `github_repo` (letter case ignored), the form a commit in a separate code repository uses for the issue repository's issues; in both forms no digit follows the number (`from: first-commit`). `/#` for any other repository does not count. The baseline tag is the configuration's `auto_tag.baseline_format` with `{N}` replaced by ``, or `idd--baseline` when that field is absent, not a string containing `{N}`, or does not give a valid tag name. Commit messages SHALL be read so that no byte a message can hold hides a commit, with signature display turned off and `HEAD` separated from paths, and every commit and parent read SHALL be a hexadecimal object id. It SHALL print `error` (exit 2) when neither base exists, when that oldest commit has no parent, or when `` is not a positive integer written in digits; an empty `` is a usage error (exit 64), like any empty argument of `spec-change.py`. It SHALL NOT fall back to `merge-base` with the branch: on a direct-commit run that is `HEAD` itself, and the range would be empty. + +`references/spec-wrapper.md` step 5 SHALL ask `link-base` first, with the issue number taken from `NUMBER`, or from `N` when `NUMBER` is empty, and SHALL use its `base` when it prints `ok`, SHALL keep the fetch of the default branch when it prints `remote`, and SHALL stop with the reason when it prints `error` or nothing a JSON reader can take, without also printing the message about a failed fetch. `references/config-protocol.md` SHALL document the `spec_archive` field, the key, and why an entry is accepted only for a repository without a remote. + +#### Scenario: An entry for a repository that later gained a remote + +- **GIVEN** a configuration entry `{"ref": "local", "branch": "master"}` for a repository +- **AND** a remote was added to that repository afterwards +- **WHEN** `archive-ref` runs for it +- **THEN** it prints `error` and names the remote, instead of reading the local branch + +##### Example: how a spec_archive entry resolves + +| Configuration found | Entry for the repository's path | Repository | `archive-ref` | +| --- | --- | --- | --- | +| none | — | no remote | `remote` | +| a file without `spec_archive` | — | no remote | `remote` | +| `spec_archive` names another path only | — | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local`, `master` and its commit | +| `spec_archive` names this path as `./sub/` | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | has a remote `origin` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "trunk"}` | no remote, no branch `trunk` | `error` | +| `spec_archive` names this path | `{"ref": "remote", "branch": "master"}` | no remote | `error` | +| `spec_archive` names this path | `{"ref": "local"}` | no remote | `error` | +| `spec_archive` is a list | — | no remote | `error` | +| the file is not JSON | — | no remote | `remote`, saying the file could not be read | +| the file is empty, or holds `null` | — | no remote | `remote` | +| the file starts with a UTF-8 byte order mark | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path as `sub` and as `./sub/` | both `{"ref": "local", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master~1"}` | no remote | `error`, not a branch name | +| `spec_archive` names this path as ` sub` | `{"ref": "local", "branch": "master"}` | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote; the global git configuration has `[remote "origin"]` with only `prune = true` | `local` | +| `spec_archive` names the main working tree | `{"ref": "local", "branch": "master"}` | asked from a linked worktree of it | `local` | +| the new file without `spec_archive`, the legacy file beside it with an entry | — | no remote | `remote` (the new file wins) | +| a file above the home directory with an entry | — | no remote | `remote` (not read) | +| `spec_archive` has `.` | `{"ref": "local", "branch": "master"}` | another repository below, whose directory name ends in a space | `remote` (no fallback to `.`) | +| `spec_archive` names this path twice, written as the same key | `{"ref": "local", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has a `pushurl` and no `url` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | a legacy `remotes/origin` file in its git directory | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | made with `--separate-git-dir` | `local`, keyed by its top level | + +##### Example: where the local link check starts + +| Repository (opted into local `master`) | `link-base --issue 14` | +| --- | --- | +| tag `idd-14-baseline` on an ancestor of `HEAD` | `ok`, the tag's commit, `from: baseline-tag` | +| no tag; commits `base`, `a (#14)`, `b (Refs #14)` | `ok`, the commit `base`, `from: first-commit` | +| no tag; the only commit naming a 14 says `#142` | `error` | +| no tag; the first commit with `#14` is the root commit | `error` | +| the repository is not opted in | `remote` | +| the repository is not opted in, `--issue abc` | `remote` | +| no tag; commits `base`, `see o/r#14`, `x#14`, `fix (#14)` | `ok`, the parent of `fix (#14)` | +| tag `idd-14-baseline` on a commit that is not an ancestor of `HEAD`; commits `base`, `a (#14)` | `ok`, the commit `base`, `from: first-commit` | +| `auto_tag.baseline_format` is `start-{N}`, tag `start-14` on an ancestor | `ok`, that tag's commit, `from: baseline-tag` | +| no tag; a commit before `a (#14)` has the byte 0x1e in its message | `ok`, the parent of `a (#14)` | +| `github_repo` is `o/issues`; no tag; commits `base`, `see other/r#14`, `fix (O/ISSUES#14)` | `ok`, the parent of `fix (O/ISSUES#14)` | +| tag `idd-14-baseline` on `HEAD` itself; commits `base`, `a (#14)` | `ok`, the commit `base`, `from: first-commit` | +| `log.showSignature` is on and a commit before `a (#14)` is signed | `ok`, the parent of `a (#14)` | +| a file named `HEAD` at the repository's top level; no tag; commits `base`, `a (#14)` | `ok`, the commit `base` | diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix2/tasks.md b/openspec/changes/archive/2026-11-14-idd-547-fix2/tasks.md new file mode 100644 index 00000000..c1aacfb0 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix2/tasks.md @@ -0,0 +1,15 @@ +## 1. 測試先行 + +- [x] 1.1 requirement「A repository without a remote can opt into its local default branch」— spec-archive-local:Example「how a spec_archive entry resolves」與「where the local link check starts」新增的各列(`.` 不再是退路、字面重複的 key、只有 pushurl、舊式 remotes 檔、separate-git-dir、`github_repo` 的 `owner/repo#N`、tag 在 HEAD、簽章、名為 HEAD 的檔),帶兩個既有標記。驗證:改之前新的列失敗。 +- [x] 1.2 check-existing-work:`--skip-branches` 不跑 fetch、不讀 tracking ref(git shim 記錄呼叫,tracking ref 不產生 E4);既有 Step 1.5 列設 HOME;Step 1.5 在 archive-ref 回 error 時不帶旗標;拿掉永遠不會失敗的斷言。驗證:帶旗標仍 fetch 的變異會失敗。 +- [x] 1.3 spec-close-gate:「沒有條目」列驗原因含「default branch could not be read」;與設定無關的錯誤標 `archive-ref:`;原因不含設定檔的絕對路徑。 + +## 2. 程式與文件 + +- [x] 2.1 `spec-change.py` 照 requirement 改。驗證:1.1 通過;spec-change suite 照舊。 +- [x] 2.2 idd-close Step 1.5/1.57(`python3 -I`、原因標籤、error 表說明)、spec-wrapper 第 5 步(`python3 -I`、說明文字)。驗證:1.2、1.3 通過;spec-wrapper-contract 照舊。 +- [x] 2.3 config-protocol 的矛盾句與新規則、CHANGELOG。 + +## 3. 收尾 + +- [x] 3.1 run-all-tests.sh 與連結檢查。驗證:結束碼 0,links 回 ok。 diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix3/.openspec.yaml b/openspec/changes/archive/2026-11-14-idd-547-fix3/.openspec.yaml new file mode 100644 index 00000000..1a60d392 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix3/.openspec.yaml @@ -0,0 +1,5 @@ +schema: spec-driven +created: 2026-11-14 +created_by: che cheng +archived_by: che cheng +archived_at: 2026-11-14 diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix3/proposal.md b/openspec/changes/archive/2026-11-14-idd-547-fix3/proposal.md new file mode 100644 index 00000000..c6fc6243 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix3/proposal.md @@ -0,0 +1,46 @@ +**GitHub-side tracker**: #547(第 3 輪 verify:comment 6875253859;本輪計畫:comment 6875265846;不在範圍的三項合成 #552) + +## Problem + +第 3 輪 verify 找到 1 項 Blocking:第 2 輪把 `repo_top()` 改成只去換行,連帶讓 `archived`、`links`、`preflight` 對結尾有空白的路徑讀對了 repo(原本會讀到同名的兄弟目錄),但沒有 requirement 寫明、沒有測試。另有 11 項 In-scope fix:`--separate-git-dir` 主工作樹的 linked worktree 算錯 key(git 不記錄這種主工作樹在哪,改成不猜 key、說明原因)、`git log` 的輸出編碼沒固定、重複鍵的記號放在資料裡、沒有 remote 的 repo 仍去 fetch(git 會把名為 `origin` 的本機路徑當成 remote)、`"spec_archive": null` 擋住普通 repo,以及幾處標籤、文件與測試細節。 + +## Root Cause + +前兩輪逐項修邊界時,共用的 helper 改動沒有回頭補到其他子命令的規格;主工作樹與重複鍵的判斷用了推論而不是 git 與 parser 直接給的答案。 + +## Proposed Solution + +新增 requirement「spec-change.py reads the repository that holds --cwd」,修改「Close requires the issue's change to be archived」與「A repository without a remote can opt into its local default branch」(見 specs/),照著改程式、文件與測試。 + +## Non-Goals + +- 設定檔擁有者檢查、開工檢查的 `unknown` 雜訊、Step 1.55 的 fetch:#552。 +- ASSUMPTION: 不升版號。 + +## Success Criteria + +- 新的測試在改程式之前失敗,改之後通過;run-all-tests 全過;links 回 ok。 + +## Capabilities + +### New Capabilities + +(none) + +### Modified Capabilities + +- `spec-on-every-issue`: 新增一條、修改兩條 requirement。 + +## Impact + +- Affected specs: spec-on-every-issue +- Affected code: + - Modified: plugins/issue-driven-dev/scripts/spec-change.py + - Modified: plugins/issue-driven-dev/scripts/check-existing-work.sh + - Modified: plugins/issue-driven-dev/skills/idd-close/SKILL.md + - Modified: plugins/issue-driven-dev/references/spec-wrapper.md + - Modified: plugins/issue-driven-dev/references/config-protocol.md + - Modified: plugins/issue-driven-dev/scripts/tests/spec-archive-local/test.sh + - Modified: plugins/issue-driven-dev/scripts/tests/spec-close-gate/test.sh + - Modified: plugins/issue-driven-dev/scripts/tests/check-existing-work/test.sh + - Modified: plugins/issue-driven-dev/CHANGELOG.md diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix3/specs/idd-existing-work-lookup/spec.md b/openspec/changes/archive/2026-11-14-idd-547-fix3/specs/idd-existing-work-lookup/spec.md new file mode 100644 index 00000000..b8d8fffa --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix3/specs/idd-existing-work-lookup/spec.md @@ -0,0 +1,22 @@ +## MODIFIED Requirements + +### Requirement: The helper can skip the branch lookup + +`check-existing-work.sh` SHALL accept `--skip-branches` before the repository argument. With it, the helper SHALL NOT run `git fetch` or list remote-tracking branches in `--cwd`, SHALL treat the clone as having no branches, SHALL NOT look up the default branch (it asks neither GitHub nor `refs/remotes/origin/HEAD`, a remote-tracking ref; with no branches to compare it is not needed, so no default-branch error is added), SHALL NOT add a `git fetch failed` error, and SHALL add a top-level `notes` list that says the branch lookup was skipped. The PR lookups on the repository argument SHALL run as without the flag, and their failures and limits SHALL be reported as before. Without the flag the helper's output SHALL be as before, without a `notes` field. + +`idd-close` Step 1.5 SHALL pass `--skip-branches` only when `spec-change.py archive-ref` prints `local` for its working directory (requirement "A repository without a remote can opt into its local default branch" of `spec-on-every-issue`), and SHALL then print that the branch lookup was skipped and that the open PRs were checked on the issue's repository. The open-PR gate itself is unchanged. + +#### Scenario: idd-close in a repository opted into its local branch + +- **GIVEN** the working directory of `/idd-close` is a repository without a remote, opted into its local branch +- **WHEN** Step 1.5 runs +- **THEN** it calls the helper with `--skip-branches`, prints that the branch lookup was skipped, and still refuses to close while an open PR on the issue's repository references the issue + +##### Example: the branch lookup can be skipped + +| Clone | Flag | `git fetch` run | `errors` | `notes` | An open PR declaring the issue | +| --- | --- | --- | --- | --- | --- | +| has an `origin` that cannot be fetched | none | yes | has `git fetch failed` | absent | reported as E1 | +| has no remote | `--skip-branches` | no | no `git fetch failed` | says the branch lookup was skipped | reported as E1 | +| has no remote, the open PR list fails | `--skip-branches` | no | has the PR list failure | says the branch lookup was skipped | verdict `unknown` | +| has an `origin` and its `origin/HEAD`; `gh repo view` fails | `--skip-branches` | no | no default-branch error | says the branch lookup was skipped | none open; `origin/HEAD` is not read | diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix3/specs/spec-on-every-issue/spec.md b/openspec/changes/archive/2026-11-14-idd-547-fix3/specs/spec-on-every-issue/spec.md new file mode 100644 index 00000000..7e49bc54 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix3/specs/spec-on-every-issue/spec.md @@ -0,0 +1,138 @@ +## MODIFIED Requirements + +### Requirement: Close requires the issue's change to be archived + +`idd-close` SHALL fetch the issue's comments with `gh api --paginate`, SHALL choose the Implementation Complete comment with `spec-change.py ic` (requirement "One way to choose the Implementation Complete comment"), and SHALL run `spec-change.py archived` on it with `--ref` set to the default branch as the remote has it. It SHALL close the issue only when the command prints `pass`. On `refuse` it SHALL stop and name the changes that are not archived or the fault in the `**Spec**:` line; on `error` it SHALL stop and say the default branch could not be read. For a comment that declares a change, it SHALL first ask `spec-change.py archive-ref` which branch to read (requirement "A repository without a remote can opt into its local default branch"). When the answer is `remote` and `has_remote` is false (the repository has no remote at all), it SHALL stop without fetching and say that the repository has no remote, with the reason `archive-ref` gave; git would otherwise read a local path named `origin` as the remote. When the answer is `remote` otherwise, it SHALL read the default branch by fetching it from the remote and checking the commit that fetch returned, and SHALL stop when `gh repo view` cannot name the default branch or the fetch fails; the reason it prints SHALL then include the reason `archive-ref` gave for answering `remote`, so that a repository meant for the local branch whose entry was not found says why. When the answer is `local`, it SHALL check the commit `archive-ref` returned for the configured branch, not `HEAD`, SHALL NOT fetch, and SHALL print that a local ref was used, with the branch, the commit and the entry's key, and without the configuration file's path. When the answer is `error`, or `archive-ref` prints nothing a JSON reader can take, it SHALL stop and print the reason. For any other comment the default branch is not read, since `archived` answers from the comment alone. When the comments cannot be fetched or `ic` exits 2, it SHALL stop and say the comments could not be read; it SHALL NOT treat that as an issue with no Implementation Complete comment. An issue with no Implementation Complete comment is not held to this rule. A retroactive close (`idd-close --retroactive`, for an issue that is already closed) skips this gate, as it skips the gates before it. + +#### Scenario: An unarchived change holds the close + +- **GIVEN** an Implementation Complete comment with the marker and `**Spec**: change idd-42-fix-timeout` +- **AND** `openspec/changes/idd-42-fix-timeout/` still exists on the default branch +- **WHEN** `idd-close` runs its gates +- **THEN** it does not close the issue and says the change is not archived + +#### Scenario: A repository without a remote, opted in + +- **GIVEN** the code repository has no remote and the IDD configuration above it maps the repository's path to `{"ref": "local", "branch": "master"}` +- **AND** the change is archived on `master` +- **WHEN** `idd-close` runs its gates +- **THEN** the gate passes without fetching and prints the local branch and commit it checked + +##### Example: the close gate reads the default branch + +| Newest Implementation Complete comment | On the default branch | Close gate | +| --- | --- | --- | +| marker, `**Spec**: change idd-42-fix-timeout` | the change is archived | passes | +| marker, `**Spec**: change idd-42-fix-timeout` | archived on the issue's branch only, not yet merged | stops, naming the change | +| marker, no `**Spec**:` line | not read | stops, naming the missing line | +| no marker, no `**Spec**:` line | not read | passes | +| the issue has no Implementation Complete comment | not read | passes | +| the comments could not be fetched | not read | stops, saying the comments could not be read | +| marker, `**Spec**: change idd-42-fix-timeout` | the default branch cannot be fetched | stops, saying the default branch could not be read | +| marker, `**Spec**: no behaviour change` | the default branch cannot be fetched | passes; the branch is not read | +| marker, `**Spec**: change idd-42-fix-timeout` | the remote's default branch dropped the archive, the local tracking ref did not move | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts the repository into its local `master`, where the change is archived | passes; prints `local ref master@` | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts in, the change is not archived on `master` | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; opted into `master`; `HEAD` is on the issue's branch, where the change is archived, and `master` does not have it | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote and no `spec_archive` entry for the repository | stops, saying the default branch could not be read | +| marker, `**Spec**: change idd-42-fix-timeout` | the repository has a remote and the configuration opts it into `local` | stops and prints the reason | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote and no entry; a directory named `origin` beside it holds a repository whose default branch has the change archived | stops, saying the repository has no remote; nothing is fetched | + + +### Requirement: A repository without a remote can opt into its local default branch + +`spec-change.py archive-ref --cwd ` SHALL decide which branch the spec checks read for the repository that holds ``. The repository's path is its main working tree: for a linked worktree (its git directory differs from the common git directory), the first worktree `git worktree list --porcelain` lists; when that entry is bare, the linked worktree's own top level; when that entry is the common git directory itself (a main working tree made with `--separate-git-dir`, to which git keeps no path back), no key is guessed and the answer is `remote`, with a reason saying the main working tree is not recorded; otherwise (not a linked worktree) its top level, including a repository made with `--separate-git-dir`. It SHALL find the IDD configuration by walking up from that path, the way the skills walk up: at each directory below `/`, `.claude/.idd/local.json` first and then `.claude/issue-driven-dev.local.json`, stopping after the home directory, and SHALL use the first file found. The key it looks up is that path relative to the directory that holds the configuration's `.claude/` directory, with both paths resolved through symbolic links. A key in the file SHALL match when it names the same path after `.` and `..` segments and trailing `/` are normalised; `.` names the directory of the configuration itself; a key that is empty, only `/`, or has leading or trailing whitespace matches nothing. When the repository's own path cannot be written as a key (it has leading or trailing whitespace), the answer is `remote`; it SHALL NOT fall back to `.`. Reasons name the configuration file by its path relative to the repository, not by an absolute path. It SHALL print one JSON line and exit as follows: + +- `remote` (exit 0), with a `reason`, when no configuration file is found; when the file found cannot be read as a JSON object (it is empty, `null`, not JSON, not valid UTF-8, or nested too deeply; a UTF-8 byte order mark is read as usual); when the file has no `spec_archive` field or the field is `null`; when `spec_archive` has no entry for the key; or when the repository's path cannot be written as a key. Every `remote` answer carries `has_remote`, true when the repository has a remote; callers read the default branch as the remote has it only when it is true, and a repository without any remote stops there, with this reason. +- `local` (exit 0), with `branch`, `ref` (the commit `refs/heads/` points at, whatever `HEAD` is), `config` and `key`, when exactly one entry matches the key, it is `{"ref": "local", "branch": ""}`, the repository has no remote, and the branch exists. +- `error` (exit 2), with a `reason`, when `spec_archive` is not an object; when more than one key matches, including the same key written twice; when `spec_archive` itself, or a key of the matching entry, is written twice in the file (duplicates are found while the file is parsed, never by a marker kept in the data, so no key the file holds can imitate one); when the matching entry is not an object, its `ref` is not `local`, or its `branch` is missing, not a string, or not a valid branch name (`git check-ref-format --branch` must accept it unchanged, so revision syntax such as `master~1` or `master@{0}` is refused); when the repository has a remote; when the branch does not exist; or when `` is not inside a git repository. + +A repository has a remote when `git remote` lists a name that has a `url`, a `pushurl` or a `vcs` (a remote helper) in the git configuration it reads, or a legacy `remotes/` or `branches/` file in its git directory; a `remote` section without either (for example a global `remote.origin.prune`) does not count. The repository having a remote turns an entry into an error, not a silent `remote`, so that an entry written for a repository without a remote cannot outlive the reason it was written for. + +`spec-change.py link-base --cwd --issue ` SHALL give the base of the link check (`spec-change.py links --base`) in local mode. It SHALL resolve the configuration as `archive-ref` does first, and print `remote` (exit 0) or the same `error` (exit 2) when `archive-ref` would, without reading ``. In local mode it SHALL print `ok` (exit 0) with `base` and `from`: the commit of the baseline tag when that tag exists and is an ancestor of `HEAD` other than `HEAD` itself (`from: baseline-tag`); otherwise the parent of the oldest commit in the history of `HEAD` whose message names issue ``: `#` with no letter, digit, `_`, `/` or `-` right before the `#`, the rule of `references/pr-issue-matching.md`, or `/#` where `/` is the configuration's `github_repo` (letter case ignored), the form a commit in a separate code repository uses for the issue repository's issues; in both forms no digit follows the number (`from: first-commit`). `/#` for any other repository does not count. The baseline tag is the configuration's `auto_tag.baseline_format` with `{N}` replaced by ``, or `idd--baseline` when that field is absent, not a string containing `{N}`, or does not give a valid tag name. Commit messages SHALL be read so that no byte a message can hold hides a commit, with signature display turned off, the output encoding fixed to UTF-8 whatever `i18n.logOutputEncoding` says, and `HEAD` separated from paths, and every commit and parent read SHALL be a hexadecimal object id. It SHALL print `error` (exit 2) when neither base exists, when that oldest commit has no parent, or when `` is not a positive integer written in digits; an empty `` is a usage error (exit 64), like any empty argument of `spec-change.py`. It SHALL NOT fall back to `merge-base` with the branch: on a direct-commit run that is `HEAD` itself, and the range would be empty. + +`references/spec-wrapper.md` step 5 SHALL ask `link-base` first, with the issue number taken from `NUMBER`, or from `N` when `NUMBER` is empty, and SHALL use its `base` when it prints `ok`, SHALL keep the fetch of the default branch when it prints `remote`, and SHALL stop with the reason when it prints `error` or nothing a JSON reader can take, without also printing the message about a failed fetch. `references/config-protocol.md` SHALL document the `spec_archive` field, the key, and why an entry is accepted only for a repository without a remote. + +#### Scenario: An entry for a repository that later gained a remote + +- **GIVEN** a configuration entry `{"ref": "local", "branch": "master"}` for a repository +- **AND** a remote was added to that repository afterwards +- **WHEN** `archive-ref` runs for it +- **THEN** it prints `error` and names the remote, instead of reading the local branch + +##### Example: how a spec_archive entry resolves + +| Configuration found | Entry for the repository's path | Repository | `archive-ref` | +| --- | --- | --- | --- | +| none | — | no remote | `remote` | +| a file without `spec_archive` | — | no remote | `remote` | +| `spec_archive` names another path only | — | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local`, `master` and its commit | +| `spec_archive` names this path as `./sub/` | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | has a remote `origin` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "trunk"}` | no remote, no branch `trunk` | `error` | +| `spec_archive` names this path | `{"ref": "remote", "branch": "master"}` | no remote | `error` | +| `spec_archive` names this path | `{"ref": "local"}` | no remote | `error` | +| `spec_archive` is a list | — | no remote | `error` | +| the file is not JSON | — | no remote | `remote`, saying the file could not be read | +| the file is empty, or holds `null` | — | no remote | `remote` | +| the file starts with a UTF-8 byte order mark | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path as `sub` and as `./sub/` | both `{"ref": "local", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master~1"}` | no remote | `error`, not a branch name | +| `spec_archive` names this path as ` sub` | `{"ref": "local", "branch": "master"}` | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote; the global git configuration has `[remote "origin"]` with only `prune = true` | `local` | +| `spec_archive` names the main working tree | `{"ref": "local", "branch": "master"}` | asked from a linked worktree of it | `local` | +| the new file without `spec_archive`, the legacy file beside it with an entry | — | no remote | `remote` (the new file wins) | +| a file above the home directory with an entry | — | no remote | `remote` (not read) | +| `spec_archive` has `.` | `{"ref": "local", "branch": "master"}` | another repository below, whose directory name ends in a space | `remote` (no fallback to `.`) | +| `spec_archive` names this path twice, written as the same key | `{"ref": "local", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has a `pushurl` and no `url` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | a legacy `remotes/origin` file in its git directory | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | made with `--separate-git-dir` | `local`, keyed by its top level | +| `spec_archive` names the linked worktree's own path; the main working tree was made with `--separate-git-dir` | `{"ref": "local", "branch": "master"}` | asked from that linked worktree; no remote | `remote`, `has_remote` false, saying the main working tree is not recorded | +| `spec_archive` written twice at the top level | — | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path, and the file also holds a key that looks like an internal marker | `{"ref": "local", "branch": "master"}` | no remote | `local` | +| `"spec_archive": null` | — | has a remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has a `vcs` and no `url` | `error`, naming the remote | +| none | — | no remote | `remote`, `has_remote` false | + +##### Example: where the local link check starts + +| Repository (opted into local `master`) | `link-base --issue 14` | +| --- | --- | +| tag `idd-14-baseline` on an ancestor of `HEAD` | `ok`, the tag's commit, `from: baseline-tag` | +| no tag; commits `base`, `a (#14)`, `b (Refs #14)` | `ok`, the commit `base`, `from: first-commit` | +| no tag; the only commit naming a 14 says `#142` | `error` | +| no tag; the first commit with `#14` is the root commit | `error` | +| the repository is not opted in | `remote` | +| the repository is not opted in, `--issue abc` | `remote` | +| no tag; commits `base`, `see o/r#14`, `x#14`, `fix (#14)` | `ok`, the parent of `fix (#14)` | +| tag `idd-14-baseline` on a commit that is not an ancestor of `HEAD`; commits `base`, `a (#14)` | `ok`, the commit `base`, `from: first-commit` | +| `auto_tag.baseline_format` is `start-{N}`, tag `start-14` on an ancestor | `ok`, that tag's commit, `from: baseline-tag` | +| no tag; a commit before `a (#14)` has the byte 0x1e in its message | `ok`, the parent of `a (#14)` | +| `github_repo` is `o/issues`; no tag; commits `base`, `see other/r#14`, `fix (O/ISSUES#14)` | `ok`, the parent of `fix (O/ISSUES#14)` | +| tag `idd-14-baseline` on `HEAD` itself; commits `base`, `a (#14)` | `ok`, the commit `base`, `from: first-commit` | +| `log.showSignature` is on and a commit before `a (#14)` is signed | `ok`, the parent of `a (#14)` | +| a file named `HEAD` at the repository's top level; no tag; commits `base`, `a (#14)` | `ok`, the commit `base` | +| `i18n.logOutputEncoding` is `UTF-16`; no tag; commits `base`, `a (#14)` | `ok`, the commit `base` | + +## ADDED Requirements + +### Requirement: spec-change.py reads the repository that holds --cwd + +Every subcommand of `scripts/spec-change.py` that takes `--cwd` (`archived`, `links`, `preflight`, `archive-ref`, `link-base`) SHALL read the repository whose top level `git rev-parse --show-toplevel` reports for `--cwd`, and SHALL take that path exactly as git prints it, removing only the line ending. A top level whose name ends in whitespace is that directory, never a sibling directory whose name lacks the whitespace. + +#### Scenario: A top level whose name ends in a space + +- **GIVEN** two repositories side by side, `odd ` (with a trailing space) and `odd` +- **WHEN** a subcommand runs with `--cwd` inside `odd ` +- **THEN** it reads `odd ` + +##### Example: a top level whose name ends in a space + +| Repositories | Command | Result | +| --- | --- | --- | +| `odd ` has the change archived, the sibling `odd` does not | `archived --cwd 'odd ' --ref HEAD` | `pass` | +| `odd ` alone, with the change archived | `archived --cwd 'odd ' --ref HEAD` | `pass` | +| `odd ` has a spec added on a branch without an Example, the sibling `odd` has none | `links --base --cwd 'odd '` | `missing` | diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix3/tasks.md b/openspec/changes/archive/2026-11-14-idd-547-fix3/tasks.md new file mode 100644 index 00000000..49708be9 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix3/tasks.md @@ -0,0 +1,15 @@ +## 1. 測試先行 + +- [x] 1.1 requirement「spec-change.py reads the repository that holds --cwd」— spec-archive-local:Example「a top level whose name ends in a space」三列,帶標記 spec-example: spec-on-every-issue / a top level whose name ends in a space。 +- [x] 1.2 requirement「A repository without a remote can opt into its local default branch」— spec-archive-local:Example 新增的各列(separate-git-dir 加 linked worktree、各種重複鍵、偽造記號、`null`、`vcs`、`has_remote`、UTF-16 log),以及第 3 輪第 4 列要補的三個細節斷言。驗證:改之前新的列失敗。 +- [x] 1.3 requirement「Close requires the issue's change to be archived」— spec-close-gate:名為 `origin` 的本機 repo 不會讓沒有 remote 的 repo 通過,也不 fetch。驗證:改之前失敗。 +- [x] 1.4 requirement「The helper can skip the branch lookup」— check-existing-work:`--skip-branches` 而 `gh repo view` 失敗時不讀 `origin/HEAD`、不記預設 branch 的錯誤;spec-wrapper 第 5 步 `link-base:` 標籤。 + +## 2. 程式與文件 + +- [x] 2.1 `spec-change.py`、`check-existing-work.sh`、idd-close Step 1.57、spec-wrapper 第 5 步。驗證:1.1–1.4 通過。 +- [x] 2.2 docstring、config-protocol、CHANGELOG 寫到目前的規則。 + +## 3. 收尾 + +- [x] 3.1 run-all-tests.sh 與連結檢查。驗證:結束碼 0,links 回 ok。 diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix4/.openspec.yaml b/openspec/changes/archive/2026-11-14-idd-547-fix4/.openspec.yaml new file mode 100644 index 00000000..1a60d392 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix4/.openspec.yaml @@ -0,0 +1,5 @@ +schema: spec-driven +created: 2026-11-14 +created_by: che cheng +archived_by: che cheng +archived_at: 2026-11-14 diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix4/proposal.md b/openspec/changes/archive/2026-11-14-idd-547-fix4/proposal.md new file mode 100644 index 00000000..52b38e3f --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix4/proposal.md @@ -0,0 +1,16 @@ +## Why + +第 4 輪 verify(#547 comment 6879033245)判定 PASS、0 Blocking,但有 4 列 MEDIUM 的 In-scope fix:`remotes_of()` 用去掉空白後的值判斷 remote 是否存在(url 設成空白或空字串時漏算)、`repo_top()` 去掉所有結尾換行而不是只去 git 加的那一個、幾列 Example 的測試只驗 status、spec-wrapper 第 5 步的 remote 路徑沒有測試。另有 7 列 LOW/INFO:規格兩處對 `has_remote` 的說法不一致、主工作樹的規則沒有寫出 `.git` 命名與 submodule 的情況、bare 主 repo 沒有寫明、第 5 步 fetch 失敗時丟掉 `link-base` 的原因,以及 docstring 與 issue 文字。 + +## What Changes + +- `has_remote` 正好是 true 才 fetch,close 與 archive-ref 兩條 requirement 說法一致。 +- remote 是否存在看 `url`、`pushurl`、`vcs` 是否設定,不看值。 +- 主工作樹:照實寫出 git 不記錄主工作樹的兩種情況(其他名稱的 separate git dir、submodule),以及 `.git` 命名時 git 回報的路徑;bare 主 repo 的 linked worktree 以自己的路徑為 key。 +- 讀 git 印出的路徑只去一個結尾換行;Example 補換行結尾與 `preflight` 的列。 +- 第 5 步 fetch 失敗時附上 `link-base` 的原因。 + +## Impact + +- `plugins/issue-driven-dev/scripts/spec-change.py`、`references/spec-wrapper.md`、`references/config-protocol.md`、`CHANGELOG.md` +- 測試:`scripts/tests/spec-archive-local`、`spec-close-gate`、`check-existing-work` diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix4/specs/spec-on-every-issue/spec.md b/openspec/changes/archive/2026-11-14-idd-547-fix4/specs/spec-on-every-issue/spec.md new file mode 100644 index 00000000..efc9b21c --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix4/specs/spec-on-every-issue/spec.md @@ -0,0 +1,144 @@ +## MODIFIED Requirements + +### Requirement: Close requires the issue's change to be archived + +`idd-close` SHALL fetch the issue's comments with `gh api --paginate`, SHALL choose the Implementation Complete comment with `spec-change.py ic` (requirement "One way to choose the Implementation Complete comment"), and SHALL run `spec-change.py archived` on it with `--ref` set to the default branch as the remote has it. It SHALL close the issue only when the command prints `pass`. On `refuse` it SHALL stop and name the changes that are not archived or the fault in the `**Spec**:` line; on `error` it SHALL stop and say the default branch could not be read. For a comment that declares a change, it SHALL first ask `spec-change.py archive-ref` which branch to read (requirement "A repository without a remote can opt into its local default branch"). When the answer is `remote` and `has_remote` is not exactly true (false when the repository has no remote at all; missing or not a boolean when the answer cannot be trusted), it SHALL stop without fetching and say that the repository has no remote, with the reason `archive-ref` gave; git would otherwise read a local path named `origin` as the remote. When the answer is `remote` and `has_remote` is true, it SHALL read the default branch by fetching it from the remote and checking the commit that fetch returned, and SHALL stop when `gh repo view` cannot name the default branch or the fetch fails; the reason it prints SHALL then include the reason `archive-ref` gave for answering `remote`, so that a repository meant for the local branch whose entry was not found says why. When the answer is `local`, it SHALL check the commit `archive-ref` returned for the configured branch, not `HEAD`, SHALL NOT fetch, and SHALL print that a local ref was used, with the branch, the commit and the entry's key, and without the configuration file's path. When the answer is `error`, or `archive-ref` prints nothing a JSON reader can take, it SHALL stop and print the reason. For any other comment the default branch is not read, since `archived` answers from the comment alone. When the comments cannot be fetched or `ic` exits 2, it SHALL stop and say the comments could not be read; it SHALL NOT treat that as an issue with no Implementation Complete comment. An issue with no Implementation Complete comment is not held to this rule. A retroactive close (`idd-close --retroactive`, for an issue that is already closed) skips this gate, as it skips the gates before it. + +#### Scenario: An unarchived change holds the close + +- **GIVEN** an Implementation Complete comment with the marker and `**Spec**: change idd-42-fix-timeout` +- **AND** `openspec/changes/idd-42-fix-timeout/` still exists on the default branch +- **WHEN** `idd-close` runs its gates +- **THEN** it does not close the issue and says the change is not archived + +#### Scenario: A repository without a remote, opted in + +- **GIVEN** the code repository has no remote and the IDD configuration above it maps the repository's path to `{"ref": "local", "branch": "master"}` +- **AND** the change is archived on `master` +- **WHEN** `idd-close` runs its gates +- **THEN** the gate passes without fetching and prints the local branch and commit it checked + +##### Example: the close gate reads the default branch + +| Newest Implementation Complete comment | On the default branch | Close gate | +| --- | --- | --- | +| marker, `**Spec**: change idd-42-fix-timeout` | the change is archived | passes | +| marker, `**Spec**: change idd-42-fix-timeout` | archived on the issue's branch only, not yet merged | stops, naming the change | +| marker, no `**Spec**:` line | not read | stops, naming the missing line | +| no marker, no `**Spec**:` line | not read | passes | +| the issue has no Implementation Complete comment | not read | passes | +| the comments could not be fetched | not read | stops, saying the comments could not be read | +| marker, `**Spec**: change idd-42-fix-timeout` | the default branch cannot be fetched | stops, saying the default branch could not be read | +| marker, `**Spec**: no behaviour change` | the default branch cannot be fetched | passes; the branch is not read | +| marker, `**Spec**: change idd-42-fix-timeout` | the remote's default branch dropped the archive, the local tracking ref did not move | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts the repository into its local `master`, where the change is archived | passes; prints `local ref master@` | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts in, the change is not archived on `master` | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; opted into `master`; `HEAD` is on the issue's branch, where the change is archived, and `master` does not have it | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote and no `spec_archive` entry for the repository | stops, saying the default branch could not be read | +| marker, `**Spec**: change idd-42-fix-timeout` | the repository has a remote and the configuration opts it into `local` | stops and prints the reason | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote and no entry; a directory named `origin` beside it holds a repository whose default branch has the change archived | stops, saying the repository has no remote; nothing is fetched | + + +### Requirement: A repository without a remote can opt into its local default branch + +`spec-change.py archive-ref --cwd ` SHALL decide which branch the spec checks read for the repository that holds ``. The repository's path is its main working tree: for a linked worktree (its git directory differs from the common git directory), the first worktree `git worktree list --porcelain` lists; when that entry is bare, the linked worktree's own top level; when that entry is the common git directory itself, git does not record where the main working tree is (a repository whose git directory was made apart with `--separate-git-dir` under a name other than `.git`, or a submodule), so no key is guessed and the answer is `remote`, with a reason saying the main working tree is not recorded; a git directory made apart under the name `.git` is reported by git as the directory that holds it, and that directory is then the key, since git treats it as a working tree of the same repository; otherwise (not a linked worktree) its top level, including a repository made with `--separate-git-dir`. It SHALL find the IDD configuration by walking up from that path, the way the skills walk up: at each directory below `/`, `.claude/.idd/local.json` first and then `.claude/issue-driven-dev.local.json`, stopping after the home directory, and SHALL use the first file found. The key it looks up is that path relative to the directory that holds the configuration's `.claude/` directory, with both paths resolved through symbolic links. A key in the file SHALL match when it names the same path after `.` and `..` segments and trailing `/` are normalised; `.` names the directory of the configuration itself; a key that is empty, only `/`, or has leading or trailing whitespace matches nothing. When the repository's own path cannot be written as a key (it has leading or trailing whitespace), the answer is `remote`; it SHALL NOT fall back to `.`. Reasons name the configuration file by its path relative to the repository, not by an absolute path. It SHALL print one JSON line and exit as follows: + +- `remote` (exit 0), with a `reason`, when no configuration file is found; when the file found cannot be read as a JSON object (it is empty, `null`, not JSON, not valid UTF-8, or nested too deeply; a UTF-8 byte order mark is read as usual); when the file has no `spec_archive` field or the field is `null`; when `spec_archive` has no entry for the key; or when the repository's path cannot be written as a key. Every `remote` answer carries `has_remote`, true when the repository has a remote; callers read the default branch as the remote has it only when it is true, and a repository without any remote stops there, with this reason. +- `local` (exit 0), with `branch`, `ref` (the commit `refs/heads/` points at, whatever `HEAD` is), `config` and `key`, when exactly one entry matches the key, it is `{"ref": "local", "branch": ""}`, the repository has no remote, and the branch exists. +- `error` (exit 2), with a `reason`, when `spec_archive` is not an object; when more than one key matches, including the same key written twice; when `spec_archive` itself, or a key of the matching entry, is written twice in the file (duplicates are found while the file is parsed, never by a marker kept in the data, so no key the file holds can imitate one); when the matching entry is not an object, its `ref` is not `local`, or its `branch` is missing, not a string, or not a valid branch name (`git check-ref-format --branch` must accept it unchanged, so revision syntax such as `master~1` or `master@{0}` is refused); when the repository has a remote; when the branch does not exist; or when `` is not inside a git repository. + +A repository has a remote when `git remote` lists a name for which a `url`, a `pushurl` or a `vcs` (a remote helper) is set in the git configuration it reads, whatever its value (an empty or blank `url` counts), or a legacy `remotes/` or `branches/` file in its git directory; a `remote` section without either (for example a global `remote.origin.prune`) does not count. The repository having a remote turns an entry into an error, not a silent `remote`, so that an entry written for a repository without a remote cannot outlive the reason it was written for. + +`spec-change.py link-base --cwd --issue ` SHALL give the base of the link check (`spec-change.py links --base`) in local mode. It SHALL resolve the configuration as `archive-ref` does first, and print `remote` (exit 0) or the same `error` (exit 2) when `archive-ref` would, without reading ``. In local mode it SHALL print `ok` (exit 0) with `base` and `from`: the commit of the baseline tag when that tag exists and is an ancestor of `HEAD` other than `HEAD` itself (`from: baseline-tag`); otherwise the parent of the oldest commit in the history of `HEAD` whose message names issue ``: `#` with no letter, digit, `_`, `/` or `-` right before the `#`, the rule of `references/pr-issue-matching.md`, or `/#` where `/` is the configuration's `github_repo` (letter case ignored), the form a commit in a separate code repository uses for the issue repository's issues; in both forms no digit follows the number (`from: first-commit`). `/#` for any other repository does not count. The baseline tag is the configuration's `auto_tag.baseline_format` with `{N}` replaced by ``, or `idd--baseline` when that field is absent, not a string containing `{N}`, or does not give a valid tag name. Commit messages SHALL be read so that no byte a message can hold hides a commit, with signature display turned off, the output encoding fixed to UTF-8 whatever `i18n.logOutputEncoding` says, and `HEAD` separated from paths, and every commit and parent read SHALL be a hexadecimal object id. It SHALL print `error` (exit 2) when neither base exists, when that oldest commit has no parent, or when `` is not a positive integer written in digits; an empty `` is a usage error (exit 64), like any empty argument of `spec-change.py`. It SHALL NOT fall back to `merge-base` with the branch: on a direct-commit run that is `HEAD` itself, and the range would be empty. + +`references/spec-wrapper.md` step 5 SHALL ask `link-base` first, with the issue number taken from `NUMBER`, or from `N` when `NUMBER` is empty, and SHALL use its `base` when it prints `ok`, SHALL keep the fetch of the default branch when it prints `remote` with `has_remote` true, SHALL stop without fetching when it prints `remote` with `has_remote` not exactly true, saying the repository has no remote, and SHALL stop with the reason when it prints `error` or nothing a JSON reader can take, without also printing the message about a failed fetch. When the fetch of the default branch fails it SHALL stop with the message about a failed fetch and add `link-base`'s reason when there is one. `references/config-protocol.md` SHALL document the `spec_archive` field, the key, and why an entry is accepted only for a repository without a remote. + +#### Scenario: An entry for a repository that later gained a remote + +- **GIVEN** a configuration entry `{"ref": "local", "branch": "master"}` for a repository +- **AND** a remote was added to that repository afterwards +- **WHEN** `archive-ref` runs for it +- **THEN** it prints `error` and names the remote, instead of reading the local branch + +##### Example: how a spec_archive entry resolves + +| Configuration found | Entry for the repository's path | Repository | `archive-ref` | +| --- | --- | --- | --- | +| none | — | no remote | `remote` | +| a file without `spec_archive` | — | no remote | `remote` | +| `spec_archive` names another path only | — | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local`, `master` and its commit | +| `spec_archive` names this path as `./sub/` | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | has a remote `origin` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "trunk"}` | no remote, no branch `trunk` | `error` | +| `spec_archive` names this path | `{"ref": "remote", "branch": "master"}` | no remote | `error` | +| `spec_archive` names this path | `{"ref": "local"}` | no remote | `error` | +| `spec_archive` is a list | — | no remote | `error` | +| the file is not JSON | — | no remote | `remote`, saying the file could not be read | +| the file is empty, or holds `null` | — | no remote | `remote` | +| the file starts with a UTF-8 byte order mark | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path as `sub` and as `./sub/` | both `{"ref": "local", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master~1"}` | no remote | `error`, not a branch name | +| `spec_archive` names this path as ` sub` | `{"ref": "local", "branch": "master"}` | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote; the global git configuration has `[remote "origin"]` with only `prune = true` | `local` | +| `spec_archive` names the main working tree | `{"ref": "local", "branch": "master"}` | asked from a linked worktree of it | `local` | +| the new file without `spec_archive`, the legacy file beside it with an entry | — | no remote | `remote` (the new file wins) | +| a file above the home directory with an entry | — | no remote | `remote` (not read) | +| `spec_archive` has `.` | `{"ref": "local", "branch": "master"}` | another repository below, whose directory name ends in a space | `remote` (no fallback to `.`) | +| `spec_archive` names this path twice, written as the same key | `{"ref": "local", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has a `pushurl` and no `url` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | a legacy `remotes/origin` file in its git directory | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | made with `--separate-git-dir` | `local`, keyed by its top level | +| `spec_archive` names the linked worktree's own path; the main working tree was made with `--separate-git-dir meta.git` | `{"ref": "local", "branch": "master"}` | asked from that linked worktree; no remote | `remote`, `has_remote` false, saying the main working tree is not recorded | +| `spec_archive` names `store`; the main working tree was made with `--separate-git-dir store/.git` | `{"ref": "local", "branch": "master"}` | asked from a linked worktree; no remote | `local`, keyed by `store`, at the same repository's `master` | +| `spec_archive` names the linked worktree's own path; the main repository is bare | `{"ref": "local", "branch": "master"}` | asked from that linked worktree; no remote | `local`, keyed by its own path | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has the `url` ` ` (a blank) | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has an empty `url` | `error`, naming the remote | +| `spec_archive` written twice at the top level | — | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path, and the file also holds a key that looks like an internal marker | `{"ref": "local", "branch": "master"}` | no remote | `local` | +| `"spec_archive": null` | — | has a remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has a `vcs` and no `url` | `error`, naming the remote | +| none | — | no remote | `remote`, `has_remote` false | + +##### Example: where the local link check starts + +| Repository (opted into local `master`) | `link-base --issue 14` | +| --- | --- | +| tag `idd-14-baseline` on an ancestor of `HEAD` | `ok`, the tag's commit, `from: baseline-tag` | +| no tag; commits `base`, `a (#14)`, `b (Refs #14)` | `ok`, the commit `base`, `from: first-commit` | +| no tag; the only commit naming a 14 says `#142` | `error` | +| no tag; the first commit with `#14` is the root commit | `error` | +| the repository is not opted in | `remote` | +| the repository is not opted in, `--issue abc` | `remote` | +| no tag; commits `base`, `see o/r#14`, `x#14`, `fix (#14)` | `ok`, the parent of `fix (#14)` | +| tag `idd-14-baseline` on a commit that is not an ancestor of `HEAD`; commits `base`, `a (#14)` | `ok`, the commit `base`, `from: first-commit` | +| `auto_tag.baseline_format` is `start-{N}`, tag `start-14` on an ancestor | `ok`, that tag's commit, `from: baseline-tag` | +| no tag; a commit before `a (#14)` has the byte 0x1e in its message | `ok`, the parent of `a (#14)` | +| `github_repo` is `o/issues`; no tag; commits `base`, `see other/r#14`, `fix (O/ISSUES#14)` | `ok`, the parent of `fix (O/ISSUES#14)` | +| tag `idd-14-baseline` on `HEAD` itself; commits `base`, `a (#14)` | `ok`, the commit `base`, `from: first-commit` | +| `log.showSignature` is on and a commit before `a (#14)` is signed | `ok`, the parent of `a (#14)` | +| a file named `HEAD` at the repository's top level; no tag; commits `base`, `a (#14)` | `ok`, the commit `base` | +| `i18n.logOutputEncoding` is `UTF-16`; no tag; commits `base`, `a (#14)` | `ok`, the commit `base` | + + +### Requirement: spec-change.py reads the repository that holds --cwd + +Every subcommand of `scripts/spec-change.py` that takes `--cwd` (`archived`, `links`, `preflight`, `archive-ref`, `link-base`) SHALL read the repository whose top level `git rev-parse --show-toplevel` reports for `--cwd`, and SHALL take that path exactly as git prints it, removing only the one line ending git appends (the name of the directory can itself end in a space or a newline). A top level whose name ends in whitespace is that directory, never a sibling directory whose name lacks the whitespace. + +#### Scenario: A top level whose name ends in whitespace + +- **GIVEN** two repositories side by side, `odd ` (with a trailing space) and `odd` +- **WHEN** a subcommand runs with `--cwd` inside `odd ` +- **THEN** it reads `odd ` + +##### Example: a top level whose name ends in whitespace + +| Repositories | Command | Result | +| --- | --- | --- | +| `odd ` has the change archived, the sibling `odd` does not | `archived --cwd 'odd ' --ref HEAD` | `pass` | +| `odd ` alone, with the change archived | `archived --cwd 'odd ' --ref HEAD` | `pass` | +| `odd ` has a spec added on a branch without an Example, the sibling `odd` has none | `links --base --cwd 'odd '` | `missing` | +| `pf ` has an `openspec/` directory, the sibling `pf` does not | `preflight --cwd 'pf ' --can-write yes` | `ready` | +| `pf ` alone, with an `openspec/` directory | `preflight --cwd 'pf ' --can-write yes` | `ready` | +| `nl` followed by a newline has the change archived, the sibling `nl` does not | `archived` with `--cwd` that directory, `--ref HEAD` | `pass` | diff --git a/openspec/changes/archive/2026-11-14-idd-547-fix4/tasks.md b/openspec/changes/archive/2026-11-14-idd-547-fix4/tasks.md new file mode 100644 index 00000000..d06b81ea --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-fix4/tasks.md @@ -0,0 +1,14 @@ +## 1. 測試先行 + +- [x] 1.1 requirement「A repository without a remote can opt into its local default branch」— spec-archive-local:Example 新增的列(`.git` 命名的 separate git dir、bare 主 repo、空白與空字串的 url),`master~1` 與條目內重複鍵兩列補原因斷言,linked worktree 那一列補 `has_remote` 與原因;第 5 步 remote 路徑照原文執行一次(有可 fetch 的 origin、沒有條目),fetch 失敗時附上 `link-base` 的原因。驗證:空白與空字串的 url、fetch 失敗的原因在改之前失敗;第 5 步 remote 路徑在「永遠不 fetch」的變異上失敗。 +- [x] 1.2 requirement「spec-change.py reads the repository that holds --cwd」— spec-archive-local:Example 改名為 a top level whose name ends in whitespace,補換行結尾與 `preflight` 的列。驗證:換行結尾那列在改之前失敗。 +- [x] 1.3 requirement「Close requires the issue's change to be archived」— spec-close-gate:「stops, naming the change」各列補 `not_archived` 的斷言;requirement「The helper can skip the branch lookup」— check-existing-work 第 4 列補 fetch、notes 與 open PR 的斷言。 + +## 2. 程式與文件 + +- [x] 2.1 `spec-change.py`(`one_line`、`remotes_of`、原因文字、link-base docstring)、spec-wrapper 第 5 步。驗證:1.1–1.3 通過。 +- [x] 2.2 config-protocol 與 CHANGELOG 寫到目前的規則;issue 的 Proposal 與 Expected 第 3 項註明沒有 remote 時不 fetch。 + +## 3. 收尾 + +- [x] 3.1 run-all-tests.sh 與連結檢查。驗證:結束碼 0,links 回 ok。 diff --git a/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/.openspec.yaml b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/.openspec.yaml new file mode 100644 index 00000000..1a60d392 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/.openspec.yaml @@ -0,0 +1,5 @@ +schema: spec-driven +created: 2026-11-14 +created_by: che cheng +archived_by: che cheng +archived_at: 2026-11-14 diff --git a/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/proposal.md b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/proposal.md new file mode 100644 index 00000000..b05e62ca --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/proposal.md @@ -0,0 +1,60 @@ +**GitHub-side tracker**: #547(設計決定見 issue comment 6869531838;症狀見 #546) + +## Problem + +`/idd-close` Step 1.57 對宣告了 change 的 issue 一律 `git fetch origin -- `,對 fetch 回來的 commit 跑 `spec-change.py archived`;fetch 失敗就是 `error`、硬性停下。issue 開在 repo A(有 GitHub remote)、程式與規格在 A 底下另一個**刻意沒有 remote** 的本機 git repo B 時,fetch 永遠失敗,這種佈局裡宣告了 change 的 issue 都無法結案,即使 change 已經封存在 B 的預設 branch 上。同一件事也讓 `spec-wrapper.md` 第 5 步的連結檢查找不到起點,`check-existing-work.sh` 在 Step 1.5 多記一筆 `git fetch failed`。 + +## Root Cause + +這三處都假設程式所在的 repo 有一個 remote,預設 branch「以遠端為準」。對沒有 remote 的 repo,這個假設沒有替代:沒有任何方式說「這個 repo 的預設 branch 就是本機的這一條」。 + +## Proposed Solution + +維護者已決定(2026-11-14,issue comment 6869531838):放寬只能靠設定明確打開,不做自動判斷。 + +- IDD 設定加欄位 `spec_archive`:一個以「程式 repo 相對於設定檔所在目錄的路徑」為 key 的物件,值是 `{"ref": "local", "branch": "<本機 branch>"}`。設定寫在 issue 所在 repo 的設定檔;從程式 repo 的 top level 往上找第一份 IDD 設定。 +- 只在程式 repo **沒有任何 remote** 時接受;有 remote 卻設了,回 `error`。沒有條目時行為完全不變。 +- `spec-change.py` 加兩個子命令:`archive-ref`(這個 repo 要用哪個 ref 判定封存:`remote`、`local` 或 `error`)與 `link-base`(本機模式的連結檢查起點:baseline tag → HEAD 歷史中第一個引用 `#N` 的 commit 的父 commit → `error`)。三處共用這兩個子命令,不各寫一份 bash。 +- Step 1.57:本機模式用本機 branch 的 commit 跑 `archived`,輸出寫明用的是本機 ref。 +- Step 1.5:本機模式呼叫 `check-existing-work.sh --skip-branches`,略過程式 repo 的 fetch 與 branch 列舉、只查 issue repo 的 PR,輸出寫明略過了。 +- `spec-wrapper.md` 第 5 步:本機模式用 `link-base` 的起點。 +- `config-protocol.md` 記這個欄位。 + +## Non-Goals + +- 自動偵測「沒有 remote」並放寬:維護者選了明確設定。 +- `idd-all` direct-commit 的推送、`close-stage.py` 的檢查:推送本來就需要 remote。 +- `idd-all` Step 0.5.1、`idd-implement` Step 0.37 呼叫 `check-existing-work.sh` 時的 `unknown`:不擋,只是多一行雜訊。 +- 程式 repo 不在 issue repo 底下的佈局:找不到設定,照舊 `error`。 +- ASSUMPTION: 不升版號,CHANGELOG 加在既有的 Unreleased 區塊(版號留給 release)。 + +## Success Criteria + +- 新增的測試在改程式之前失敗,改之後通過。 +- spec-close-gate、check-existing-work 原有的檢查照舊通過;run-all-tests 的全部 suite 通過。 +- 連結檢查對新增與修改的 requirement 回 ok。 + +## Capabilities + +### New Capabilities + +(none) + +### Modified Capabilities + +- `spec-on-every-issue`: 封存檢查的 requirement 加本機模式;新增 requirement 寫明 `spec_archive` 怎麼解析、本機模式的連結檢查起點。 +- `idd-existing-work-lookup`: 新增 requirement:helper 可以略過 branch 查詢;idd-close 在本機模式這樣呼叫它。 + +## Impact + +- Affected specs: spec-on-every-issue(MODIFIED「Close requires the issue's change to be archived」;ADDED「A repository without a remote can opt into its local default branch」)、idd-existing-work-lookup(ADDED「The helper can skip the branch lookup」) +- Affected code: + - Modified: plugins/issue-driven-dev/scripts/spec-change.py + - Modified: plugins/issue-driven-dev/scripts/check-existing-work.sh + - Modified: plugins/issue-driven-dev/skills/idd-close/SKILL.md + - Modified: plugins/issue-driven-dev/references/spec-wrapper.md + - Modified: plugins/issue-driven-dev/references/config-protocol.md + - Modified: plugins/issue-driven-dev/scripts/tests/spec-close-gate/test.sh + - Modified: plugins/issue-driven-dev/scripts/tests/check-existing-work/test.sh + - New: plugins/issue-driven-dev/scripts/tests/spec-archive-local/test.sh + - Modified: plugins/issue-driven-dev/CHANGELOG.md diff --git a/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/specs/idd-existing-work-lookup/spec.md b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/specs/idd-existing-work-lookup/spec.md new file mode 100644 index 00000000..9d06ee03 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/specs/idd-existing-work-lookup/spec.md @@ -0,0 +1,21 @@ +## ADDED Requirements + +### Requirement: The helper can skip the branch lookup + +`check-existing-work.sh` SHALL accept `--skip-branches` before the repository argument. With it, the helper SHALL NOT run `git fetch` or list remote-tracking branches in `--cwd`, SHALL treat the clone as having no branches, SHALL NOT add a `git fetch failed` error, and SHALL add a top-level `notes` list that says the branch lookup was skipped. The PR lookups on the repository argument SHALL run as without the flag, and their failures and limits SHALL be reported as before. Without the flag the helper's output SHALL be as before, without a `notes` field. + +`idd-close` Step 1.5 SHALL pass `--skip-branches` only when `spec-change.py archive-ref` prints `local` for its working directory (requirement "A repository without a remote can opt into its local default branch" of `spec-on-every-issue`), and SHALL then print that the branch lookup was skipped and that the open PRs were checked on the issue's repository. The open-PR gate itself is unchanged. + +#### Scenario: idd-close in a repository opted into its local branch + +- **GIVEN** the working directory of `/idd-close` is a repository without a remote, opted into its local branch +- **WHEN** Step 1.5 runs +- **THEN** it calls the helper with `--skip-branches`, prints that the branch lookup was skipped, and still refuses to close while an open PR on the issue's repository references the issue + +##### Example: the branch lookup can be skipped + +| Clone | Flag | `git fetch` run | `errors` | `notes` | An open PR declaring the issue | +| --- | --- | --- | --- | --- | --- | +| has an `origin` that cannot be fetched | none | yes | has `git fetch failed` | absent | reported as E1 | +| has no remote | `--skip-branches` | no | no `git fetch failed` | says the branch lookup was skipped | reported as E1 | +| has no remote, the open PR list fails | `--skip-branches` | no | has the PR list failure | says the branch lookup was skipped | verdict `unknown` | diff --git a/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/specs/spec-on-every-issue/spec.md b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/specs/spec-on-every-issue/spec.md new file mode 100644 index 00000000..e1e0c366 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/specs/spec-on-every-issue/spec.md @@ -0,0 +1,85 @@ +## MODIFIED Requirements + +### Requirement: Close requires the issue's change to be archived + +`idd-close` SHALL fetch the issue's comments with `gh api --paginate`, SHALL choose the Implementation Complete comment with `spec-change.py ic` (requirement "One way to choose the Implementation Complete comment"), and SHALL run `spec-change.py archived` on it with `--ref` set to the default branch as the remote has it. It SHALL close the issue only when the command prints `pass`. On `refuse` it SHALL stop and name the changes that are not archived or the fault in the `**Spec**:` line; on `error` it SHALL stop and say the default branch could not be read. For a comment that declares a change, it SHALL first ask `spec-change.py archive-ref` which branch to read (requirement "A repository without a remote can opt into its local default branch"). When the answer is `remote`, it SHALL read the default branch by fetching it from the remote and checking the commit that fetch returned, and SHALL stop when `gh repo view` cannot name the default branch or the fetch fails. When the answer is `local`, it SHALL check the commit `archive-ref` returned, SHALL NOT fetch, and SHALL print that a local ref was used, with the branch and the commit. When the answer is `error`, or `archive-ref` prints nothing a JSON reader can take, it SHALL stop and print the reason. For any other comment the default branch is not read, since `archived` answers from the comment alone. When the comments cannot be fetched or `ic` exits 2, it SHALL stop and say the comments could not be read; it SHALL NOT treat that as an issue with no Implementation Complete comment. An issue with no Implementation Complete comment is not held to this rule. A retroactive close (`idd-close --retroactive`, for an issue that is already closed) skips this gate, as it skips the gates before it. + +#### Scenario: An unarchived change holds the close + +- **GIVEN** an Implementation Complete comment with the marker and `**Spec**: change idd-42-fix-timeout` +- **AND** `openspec/changes/idd-42-fix-timeout/` still exists on the default branch +- **WHEN** `idd-close` runs its gates +- **THEN** it does not close the issue and says the change is not archived + +#### Scenario: A repository without a remote, opted in + +- **GIVEN** the code repository has no remote and the IDD configuration above it maps the repository's path to `{"ref": "local", "branch": "master"}` +- **AND** the change is archived on `master` +- **WHEN** `idd-close` runs its gates +- **THEN** the gate passes without fetching and prints the local branch and commit it checked + +##### Example: the close gate reads the default branch + +| Newest Implementation Complete comment | On the default branch | Close gate | +| --- | --- | --- | +| marker, `**Spec**: change idd-42-fix-timeout` | the change is archived | passes | +| marker, `**Spec**: change idd-42-fix-timeout` | archived on the issue's branch only, not yet merged | stops, naming the change | +| marker, no `**Spec**:` line | not read | stops, naming the missing line | +| no marker, no `**Spec**:` line | not read | passes | +| the issue has no Implementation Complete comment | not read | passes | +| the comments could not be fetched | not read | stops, saying the comments could not be read | +| marker, `**Spec**: change idd-42-fix-timeout` | the default branch cannot be fetched | stops, saying the default branch could not be read | +| marker, `**Spec**: no behaviour change` | the default branch cannot be fetched | passes; the branch is not read | +| marker, `**Spec**: change idd-42-fix-timeout` | the remote's default branch dropped the archive, the local tracking ref did not move | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts the repository into its local `master`, where the change is archived | passes; prints `local ref master@` | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts in, the change is not archived on `master` | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | the repository has a remote and the configuration opts it into `local` | stops and prints the reason | + +## ADDED Requirements + +### Requirement: A repository without a remote can opt into its local default branch + +`spec-change.py archive-ref --cwd ` SHALL decide which branch the spec checks read for the repository that holds ``. It SHALL find the IDD configuration by walking up from the repository's top level: at each directory, `.claude/.idd/local.json` first and then `.claude/issue-driven-dev.local.json`, stopping after the home directory or at `/`, and SHALL use the first file found. The key it looks up is the path of the repository's top level relative to the directory that holds that `.claude/` directory, with both paths resolved through symbolic links; a key in the file SHALL match when it names the same path after `.` and `..` segments and a trailing `/` are normalised, and `.` names the directory of the configuration itself. It SHALL print one JSON line and exit as follows: + +- `remote` (exit 0) when no configuration file is found, when the file has no `spec_archive` field, or when `spec_archive` has no entry for the key. Callers then read the default branch as the remote has it, as before. +- `local` (exit 0), with `branch`, `ref` (the commit `refs/heads/` points at), `config` and `key`, when the entry is `{"ref": "local", "branch": ""}`, the repository has no remote at all (`git remote` lists nothing), and the branch exists. +- `error` (exit 2), with a `reason`, when the file found cannot be read as a JSON object; when `spec_archive` is not an object; when the entry is not an object, its `ref` is not `local`, or its `branch` is missing, empty, not a string or starts with `-`; when the repository has any remote; when the branch does not exist; or when `` is not inside a git repository. + +The repository having a remote turns an entry into an error, not a silent `remote`, so that an entry written for a repository without a remote cannot outlive the reason it was written for. + +`spec-change.py link-base --cwd --issue ` SHALL give the base of the link check (`spec-change.py links --base`) in local mode. It SHALL resolve the configuration as `archive-ref` does and print `remote` (exit 0) or the same `error` (exit 2) when `archive-ref` would. In local mode it SHALL print `ok` (exit 0) with `base` and `from`: the commit of the tag `idd--baseline` when that tag exists and is an ancestor of `HEAD` (`from: baseline-tag`); otherwise the parent of the oldest commit in the history of `HEAD` whose message contains `#` not followed by another digit (`from: first-commit`). It SHALL print `error` (exit 2) when neither exists, when that oldest commit has no parent, or when `` is not a positive integer written in digits. It SHALL NOT fall back to `merge-base` with the branch: on a direct-commit run that is `HEAD` itself, and the range would be empty. + +`references/spec-wrapper.md` step 5 SHALL ask `link-base` first and use its `base` when it prints `ok`, SHALL keep the fetch of the default branch when it prints `remote`, and SHALL stop when it prints `error` or nothing a JSON reader can take. `references/config-protocol.md` SHALL document the `spec_archive` field, the key, and why an entry is accepted only for a repository without a remote. + +#### Scenario: An entry for a repository that later gained a remote + +- **GIVEN** a configuration entry `{"ref": "local", "branch": "master"}` for a repository +- **AND** a remote was added to that repository afterwards +- **WHEN** `archive-ref` runs for it +- **THEN** it prints `error` and names the remote, instead of reading the local branch + +##### Example: how a spec_archive entry resolves + +| Configuration found | Entry for the repository's path | Repository | `archive-ref` | +| --- | --- | --- | --- | +| none | — | no remote | `remote` | +| a file without `spec_archive` | — | no remote | `remote` | +| `spec_archive` names another path only | — | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local`, `master` and its commit | +| `spec_archive` names this path as `./sub/` | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | has a remote `origin` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "trunk"}` | no remote, no branch `trunk` | `error` | +| `spec_archive` names this path | `{"ref": "remote", "branch": "master"}` | no remote | `error` | +| `spec_archive` names this path | `{"ref": "local"}` | no remote | `error` | +| `spec_archive` is a list | — | no remote | `error` | +| the file is not JSON | — | no remote | `error` | + +##### Example: where the local link check starts + +| Repository (opted into local `master`) | `link-base --issue 14` | +| --- | --- | +| tag `idd-14-baseline` on an ancestor of `HEAD` | `ok`, the tag's commit, `from: baseline-tag` | +| no tag; commits `base`, `a (#14)`, `b (Refs #14)` | `ok`, the commit `base`, `from: first-commit` | +| no tag; the only commit naming a 14 says `#142` | `error` | +| no tag; the first commit with `#14` is the root commit | `error` | +| the repository is not opted in | `remote` | diff --git a/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/tasks.md b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/tasks.md new file mode 100644 index 00000000..107f7a82 --- /dev/null +++ b/openspec/changes/archive/2026-11-14-idd-547-spec-archive-local/tasks.md @@ -0,0 +1,23 @@ +## 1. 測試先行 + +- [x] 1.1 `spec_archive` 的解析:依 requirement「A repository without a remote can opt into its local default branch」的 Example「how a spec_archive entry resolves」,新 suite spec-archive-local 對 `spec-change.py archive-ref` 跑 Example 十一列,測試檔帶標記 spec-example: spec-on-every-issue / how a spec_archive entry resolves。驗證:改 spec-change.py 之前失敗(子命令不存在)。 +- [x] 1.2 本機模式的連結檢查起點:依同一條 requirement 的 Example「where the local link check starts」,spec-archive-local 對 `spec-change.py link-base` 跑 Example 五列,帶標記 spec-example: spec-on-every-issue / where the local link check starts。驗證:改之前失敗。 +- [x] 1.3 封存檢查的本機模式:依 requirement「Close requires the issue's change to be archived」的 Example「the close gate reads the default branch」新增的三列,spec-close-gate 照 SKILL.md 原文執行 Step 1.57 的區塊(有設定、沒有 remote 的 repo),帶既有標記。驗證:改 SKILL.md 之前,新的三列失敗;原有各列照舊通過。 +- [x] 1.4 略過 branch 查詢:依 requirement「The helper can skip the branch lookup」的 Example「the branch lookup can be skipped」,check-existing-work suite 跑三列,並檢查 idd-close Step 1.5 在本機模式帶 `--skip-branches`,帶標記 spec-example: idd-existing-work-lookup / the branch lookup can be skipped。驗證:改之前失敗。 + +## 2. 程式 + +- [x] 2.1 `spec-change.py` 加 `archive-ref` 與 `link-base` 子命令(docstring 一併更新)。驗證:1.1、1.2 通過;spec-change suite 照舊通過。 +- [x] 2.2 `check-existing-work.sh` 加 `--skip-branches`。驗證:1.4 的 helper 部分通過;check-existing-work 原有各項照舊通過。 + +## 3. skill 與文件 + +- [x] 3.1 idd-close Step 1.57:宣告了 change 時先問 `archive-ref`,`local` 用回傳的 commit 並印出本機 ref,`remote` 照舊 fetch,`error` 停下。驗證:1.3 通過;spec-close-gate 原有的文字檢查照舊通過。 +- [x] 3.2 idd-close Step 1.5:`archive-ref` 回 `local` 時帶 `--skip-branches` 並印出說明。驗證:1.4 的 skill 部分通過。 +- [x] 3.3 spec-wrapper.md 第 5 步:先問 `link-base`,`ok` 用它的起點、`remote` 照舊、`error` 停下。驗證:spec-wrapper-contract suite 照舊通過。 +- [x] 3.4 config-protocol.md 加 `spec_archive` 欄位一節(key 的算法、只在沒有 remote 時接受的理由、找不到設定時照舊)。驗證:git diff 只有新增。 + +## 4. 收尾 + +- [x] 4.1 CHANGELOG 的 Unreleased 加 Added 條目(#547)。驗證:已發布版本的條目沒有改動。 +- [x] 4.2 跑 run-all-tests.sh 與連結檢查。驗證:結束碼都是 0,links 回 ok。 diff --git a/openspec/specs/idd-existing-work-lookup/spec.md b/openspec/specs/idd-existing-work-lookup/spec.md index 5eaa4417..2a4aae19 100644 --- a/openspec/specs/idd-existing-work-lookup/spec.md +++ b/openspec/specs/idd-existing-work-lookup/spec.md @@ -217,3 +217,25 @@ When `gh` or `git ls-remote` fails, or when a PR list reaches its limit, the hel - **AND** an open PR declares `#N` - **WHEN** `/idd-close #N` runs - **THEN** it SHALL refuse to close and print the reason + +--- +### Requirement: The helper can skip the branch lookup + +`check-existing-work.sh` SHALL accept `--skip-branches` before the repository argument. With it, the helper SHALL NOT run `git fetch` or list remote-tracking branches in `--cwd`, SHALL treat the clone as having no branches, SHALL NOT look up the default branch (it asks neither GitHub nor `refs/remotes/origin/HEAD`, a remote-tracking ref; with no branches to compare it is not needed, so no default-branch error is added), SHALL NOT add a `git fetch failed` error, and SHALL add a top-level `notes` list that says the branch lookup was skipped. The PR lookups on the repository argument SHALL run as without the flag, and their failures and limits SHALL be reported as before. Without the flag the helper's output SHALL be as before, without a `notes` field. + +`idd-close` Step 1.5 SHALL pass `--skip-branches` only when `spec-change.py archive-ref` prints `local` for its working directory (requirement "A repository without a remote can opt into its local default branch" of `spec-on-every-issue`), and SHALL then print that the branch lookup was skipped and that the open PRs were checked on the issue's repository. The open-PR gate itself is unchanged. + +#### Scenario: idd-close in a repository opted into its local branch + +- **GIVEN** the working directory of `/idd-close` is a repository without a remote, opted into its local branch +- **WHEN** Step 1.5 runs +- **THEN** it calls the helper with `--skip-branches`, prints that the branch lookup was skipped, and still refuses to close while an open PR on the issue's repository references the issue + +##### Example: the branch lookup can be skipped + +| Clone | Flag | `git fetch` run | `errors` | `notes` | An open PR declaring the issue | +| --- | --- | --- | --- | --- | --- | +| has an `origin` that cannot be fetched | none | yes | has `git fetch failed` | absent | reported as E1 | +| has no remote | `--skip-branches` | no | no `git fetch failed` | says the branch lookup was skipped | reported as E1 | +| has no remote, the open PR list fails | `--skip-branches` | no | has the PR list failure | says the branch lookup was skipped | verdict `unknown` | +| has an `origin` and its `origin/HEAD`; `gh repo view` fails | `--skip-branches` | no | no default-branch error | says the branch lookup was skipped | none open; `origin/HEAD` is not read | diff --git a/openspec/specs/spec-on-every-issue/spec.md b/openspec/specs/spec-on-every-issue/spec.md index 56d32660..98c2cc79 100644 --- a/openspec/specs/spec-on-every-issue/spec.md +++ b/openspec/specs/spec-on-every-issue/spec.md @@ -325,7 +325,7 @@ Before a wrapper creates any artifact or commit for an implementation, it SHALL --- ### Requirement: Close requires the issue's change to be archived -`idd-close` SHALL fetch the issue's comments with `gh api --paginate`, SHALL choose the Implementation Complete comment with `spec-change.py ic` (requirement "One way to choose the Implementation Complete comment"), and SHALL run `spec-change.py archived` on it with `--ref` set to the default branch as the remote has it. It SHALL close the issue only when the command prints `pass`. On `refuse` it SHALL stop and name the changes that are not archived or the fault in the `**Spec**:` line; on `error` it SHALL stop and say the default branch could not be read. For a comment that declares a change, it SHALL read the default branch by fetching it from the remote and checking the commit that fetch returned, and SHALL stop when `gh repo view` cannot name the default branch or the fetch fails; for any other comment the default branch is not read, since `archived` answers from the comment alone. When the comments cannot be fetched or `ic` exits 2, it SHALL stop and say the comments could not be read; it SHALL NOT treat that as an issue with no Implementation Complete comment. An issue with no Implementation Complete comment is not held to this rule. A retroactive close (`idd-close --retroactive`, for an issue that is already closed) skips this gate, as it skips the gates before it. +`idd-close` SHALL fetch the issue's comments with `gh api --paginate`, SHALL choose the Implementation Complete comment with `spec-change.py ic` (requirement "One way to choose the Implementation Complete comment"), and SHALL run `spec-change.py archived` on it with `--ref` set to the default branch as the remote has it. It SHALL close the issue only when the command prints `pass`. On `refuse` it SHALL stop and name the changes that are not archived or the fault in the `**Spec**:` line; on `error` it SHALL stop and say the default branch could not be read. For a comment that declares a change, it SHALL first ask `spec-change.py archive-ref` which branch to read (requirement "A repository without a remote can opt into its local default branch"). When the answer is `remote` and `has_remote` is not exactly true (false when the repository has no remote at all; missing or not a boolean when the answer cannot be trusted), it SHALL stop without fetching and say that the repository has no remote, with the reason `archive-ref` gave; git would otherwise read a local path named `origin` as the remote. When the answer is `remote` and `has_remote` is true, it SHALL read the default branch by fetching it from the remote and checking the commit that fetch returned, and SHALL stop when `gh repo view` cannot name the default branch or the fetch fails; the reason it prints SHALL then include the reason `archive-ref` gave for answering `remote`, so that a repository meant for the local branch whose entry was not found says why. When the answer is `local`, it SHALL check the commit `archive-ref` returned for the configured branch, not `HEAD`, SHALL NOT fetch, and SHALL print that a local ref was used, with the branch, the commit and the entry's key, and without the configuration file's path. When the answer is `error`, or `archive-ref` prints nothing a JSON reader can take, it SHALL stop and print the reason. For any other comment the default branch is not read, since `archived` answers from the comment alone. When the comments cannot be fetched or `ic` exits 2, it SHALL stop and say the comments could not be read; it SHALL NOT treat that as an issue with no Implementation Complete comment. An issue with no Implementation Complete comment is not held to this rule. A retroactive close (`idd-close --retroactive`, for an issue that is already closed) skips this gate, as it skips the gates before it. #### Scenario: An unarchived change holds the close @@ -334,6 +334,13 @@ Before a wrapper creates any artifact or commit for an implementation, it SHALL - **WHEN** `idd-close` runs its gates - **THEN** it does not close the issue and says the change is not archived +#### Scenario: A repository without a remote, opted in + +- **GIVEN** the code repository has no remote and the IDD configuration above it maps the repository's path to `{"ref": "local", "branch": "master"}` +- **AND** the change is archived on `master` +- **WHEN** `idd-close` runs its gates +- **THEN** the gate passes without fetching and prints the local branch and commit it checked + ##### Example: the close gate reads the default branch | Newest Implementation Complete comment | On the default branch | Close gate | @@ -347,6 +354,12 @@ Before a wrapper creates any artifact or commit for an implementation, it SHALL | marker, `**Spec**: change idd-42-fix-timeout` | the default branch cannot be fetched | stops, saying the default branch could not be read | | marker, `**Spec**: no behaviour change` | the default branch cannot be fetched | passes; the branch is not read | | marker, `**Spec**: change idd-42-fix-timeout` | the remote's default branch dropped the archive, the local tracking ref did not move | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts the repository into its local `master`, where the change is archived | passes; prints `local ref master@` | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; the configuration opts in, the change is not archived on `master` | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote; opted into `master`; `HEAD` is on the issue's branch, where the change is archived, and `master` does not have it | stops, naming the change | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote and no `spec_archive` entry for the repository | stops, saying the default branch could not be read | +| marker, `**Spec**: change idd-42-fix-timeout` | the repository has a remote and the configuration opts it into `local` | stops and prints the reason | +| marker, `**Spec**: change idd-42-fix-timeout` | no remote and no entry; a directory named `origin` beside it holds a repository whose default branch has the change archived | stops, saying the repository has no remote; nothing is fetched | --- ### Requirement: One way to choose the Implementation Complete comment @@ -376,3 +389,106 @@ Before a wrapper creates any artifact or commit for an implementation, it SHALL | JSON nested a hundred thousand arrays deep | nothing | 2 | --- +### Requirement: A repository without a remote can opt into its local default branch + +`spec-change.py archive-ref --cwd ` SHALL decide which branch the spec checks read for the repository that holds ``. The repository's path is its main working tree: for a linked worktree (its git directory differs from the common git directory), the first worktree `git worktree list --porcelain` lists; when that entry is bare, the linked worktree's own top level; when that entry is the common git directory itself, git does not record where the main working tree is (a repository whose git directory was made apart with `--separate-git-dir` under a name other than `.git`, or a submodule), so no key is guessed and the answer is `remote`, with a reason saying the main working tree is not recorded; a git directory made apart under the name `.git` is reported by git as the directory that holds it, and that directory is then the key, since git treats it as a working tree of the same repository; otherwise (not a linked worktree) its top level, including a repository made with `--separate-git-dir`. It SHALL find the IDD configuration by walking up from that path, the way the skills walk up: at each directory below `/`, `.claude/.idd/local.json` first and then `.claude/issue-driven-dev.local.json`, stopping after the home directory, and SHALL use the first file found. The key it looks up is that path relative to the directory that holds the configuration's `.claude/` directory, with both paths resolved through symbolic links. A key in the file SHALL match when it names the same path after `.` and `..` segments and trailing `/` are normalised; `.` names the directory of the configuration itself; a key that is empty, only `/`, or has leading or trailing whitespace matches nothing. When the repository's own path cannot be written as a key (it has leading or trailing whitespace), the answer is `remote`; it SHALL NOT fall back to `.`. Reasons name the configuration file by its path relative to the repository, not by an absolute path. It SHALL print one JSON line and exit as follows: + +- `remote` (exit 0), with a `reason`, when no configuration file is found; when the file found cannot be read as a JSON object (it is empty, `null`, not JSON, not valid UTF-8, or nested too deeply; a UTF-8 byte order mark is read as usual); when the file has no `spec_archive` field or the field is `null`; when `spec_archive` has no entry for the key; or when the repository's path cannot be written as a key. Every `remote` answer carries `has_remote`, true when the repository has a remote; callers read the default branch as the remote has it only when it is true, and a repository without any remote stops there, with this reason. +- `local` (exit 0), with `branch`, `ref` (the commit `refs/heads/` points at, whatever `HEAD` is), `config` and `key`, when exactly one entry matches the key, it is `{"ref": "local", "branch": ""}`, the repository has no remote, and the branch exists. +- `error` (exit 2), with a `reason`, when `spec_archive` is not an object; when more than one key matches, including the same key written twice; when `spec_archive` itself, or a key of the matching entry, is written twice in the file (duplicates are found while the file is parsed, never by a marker kept in the data, so no key the file holds can imitate one); when the matching entry is not an object, its `ref` is not `local`, or its `branch` is missing, not a string, or not a valid branch name (`git check-ref-format --branch` must accept it unchanged, so revision syntax such as `master~1` or `master@{0}` is refused); when the repository has a remote; when the branch does not exist; or when `` is not inside a git repository. + +A repository has a remote when `git remote` lists a name for which a `url`, a `pushurl` or a `vcs` (a remote helper) is set in the git configuration it reads, whatever its value (an empty or blank `url` counts), or a legacy `remotes/` or `branches/` file in its git directory; a `remote` section without either (for example a global `remote.origin.prune`) does not count. The repository having a remote turns an entry into an error, not a silent `remote`, so that an entry written for a repository without a remote cannot outlive the reason it was written for. + +`spec-change.py link-base --cwd --issue ` SHALL give the base of the link check (`spec-change.py links --base`) in local mode. It SHALL resolve the configuration as `archive-ref` does first, and print `remote` (exit 0) or the same `error` (exit 2) when `archive-ref` would, without reading ``. In local mode it SHALL print `ok` (exit 0) with `base` and `from`: the commit of the baseline tag when that tag exists and is an ancestor of `HEAD` other than `HEAD` itself (`from: baseline-tag`); otherwise the parent of the oldest commit in the history of `HEAD` whose message names issue ``: `#` with no letter, digit, `_`, `/` or `-` right before the `#`, the rule of `references/pr-issue-matching.md`, or `/#` where `/` is the configuration's `github_repo` (letter case ignored), the form a commit in a separate code repository uses for the issue repository's issues; in both forms no digit follows the number (`from: first-commit`). `/#` for any other repository does not count. The baseline tag is the configuration's `auto_tag.baseline_format` with `{N}` replaced by ``, or `idd--baseline` when that field is absent, not a string containing `{N}`, or does not give a valid tag name. Commit messages SHALL be read so that no byte a message can hold hides a commit, with signature display turned off, the output encoding fixed to UTF-8 whatever `i18n.logOutputEncoding` says, and `HEAD` separated from paths, and every commit and parent read SHALL be a hexadecimal object id. It SHALL print `error` (exit 2) when neither base exists, when that oldest commit has no parent, or when `` is not a positive integer written in digits; an empty `` is a usage error (exit 64), like any empty argument of `spec-change.py`. It SHALL NOT fall back to `merge-base` with the branch: on a direct-commit run that is `HEAD` itself, and the range would be empty. + +`references/spec-wrapper.md` step 5 SHALL ask `link-base` first, with the issue number taken from `NUMBER`, or from `N` when `NUMBER` is empty, and SHALL use its `base` when it prints `ok`, SHALL keep the fetch of the default branch when it prints `remote` with `has_remote` true, SHALL stop without fetching when it prints `remote` with `has_remote` not exactly true, saying the repository has no remote, and SHALL stop with the reason when it prints `error` or nothing a JSON reader can take, without also printing the message about a failed fetch. When the fetch of the default branch fails it SHALL stop with the message about a failed fetch and add `link-base`'s reason when there is one. `references/config-protocol.md` SHALL document the `spec_archive` field, the key, and why an entry is accepted only for a repository without a remote. + +#### Scenario: An entry for a repository that later gained a remote + +- **GIVEN** a configuration entry `{"ref": "local", "branch": "master"}` for a repository +- **AND** a remote was added to that repository afterwards +- **WHEN** `archive-ref` runs for it +- **THEN** it prints `error` and names the remote, instead of reading the local branch + +##### Example: how a spec_archive entry resolves + +| Configuration found | Entry for the repository's path | Repository | `archive-ref` | +| --- | --- | --- | --- | +| none | — | no remote | `remote` | +| a file without `spec_archive` | — | no remote | `remote` | +| `spec_archive` names another path only | — | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local`, `master` and its commit | +| `spec_archive` names this path as `./sub/` | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | has a remote `origin` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "trunk"}` | no remote, no branch `trunk` | `error` | +| `spec_archive` names this path | `{"ref": "remote", "branch": "master"}` | no remote | `error` | +| `spec_archive` names this path | `{"ref": "local"}` | no remote | `error` | +| `spec_archive` is a list | — | no remote | `error` | +| the file is not JSON | — | no remote | `remote`, saying the file could not be read | +| the file is empty, or holds `null` | — | no remote | `remote` | +| the file starts with a UTF-8 byte order mark | `{"ref": "local", "branch": "master"}` | no remote, `master` exists | `local` | +| `spec_archive` names this path as `sub` and as `./sub/` | both `{"ref": "local", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master~1"}` | no remote | `error`, not a branch name | +| `spec_archive` names this path as ` sub` | `{"ref": "local", "branch": "master"}` | no remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | no remote; the global git configuration has `[remote "origin"]` with only `prune = true` | `local` | +| `spec_archive` names the main working tree | `{"ref": "local", "branch": "master"}` | asked from a linked worktree of it | `local` | +| the new file without `spec_archive`, the legacy file beside it with an entry | — | no remote | `remote` (the new file wins) | +| a file above the home directory with an entry | — | no remote | `remote` (not read) | +| `spec_archive` has `.` | `{"ref": "local", "branch": "master"}` | another repository below, whose directory name ends in a space | `remote` (no fallback to `.`) | +| `spec_archive` names this path twice, written as the same key | `{"ref": "local", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has a `pushurl` and no `url` | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | a legacy `remotes/origin` file in its git directory | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | made with `--separate-git-dir` | `local`, keyed by its top level | +| `spec_archive` names the linked worktree's own path; the main working tree was made with `--separate-git-dir meta.git` | `{"ref": "local", "branch": "master"}` | asked from that linked worktree; no remote | `remote`, `has_remote` false, saying the main working tree is not recorded | +| `spec_archive` names `store`; the main working tree was made with `--separate-git-dir store/.git` | `{"ref": "local", "branch": "master"}` | asked from a linked worktree; no remote | `local`, keyed by `store`, at the same repository's `master` | +| `spec_archive` names the linked worktree's own path; the main repository is bare | `{"ref": "local", "branch": "master"}` | asked from that linked worktree; no remote | `local`, keyed by its own path | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has the `url` ` ` (a blank) | `error`, naming the remote | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has an empty `url` | `error`, naming the remote | +| `spec_archive` written twice at the top level | — | no remote | `error`, more than one entry | +| `spec_archive` names this path | `{"ref": "local", "branch": "master", "branch": "master"}` | no remote | `error`, more than one entry | +| `spec_archive` names this path, and the file also holds a key that looks like an internal marker | `{"ref": "local", "branch": "master"}` | no remote | `local` | +| `"spec_archive": null` | — | has a remote | `remote` | +| `spec_archive` names this path | `{"ref": "local", "branch": "master"}` | its only remote has a `vcs` and no `url` | `error`, naming the remote | +| none | — | no remote | `remote`, `has_remote` false | + +##### Example: where the local link check starts + +| Repository (opted into local `master`) | `link-base --issue 14` | +| --- | --- | +| tag `idd-14-baseline` on an ancestor of `HEAD` | `ok`, the tag's commit, `from: baseline-tag` | +| no tag; commits `base`, `a (#14)`, `b (Refs #14)` | `ok`, the commit `base`, `from: first-commit` | +| no tag; the only commit naming a 14 says `#142` | `error` | +| no tag; the first commit with `#14` is the root commit | `error` | +| the repository is not opted in | `remote` | +| the repository is not opted in, `--issue abc` | `remote` | +| no tag; commits `base`, `see o/r#14`, `x#14`, `fix (#14)` | `ok`, the parent of `fix (#14)` | +| tag `idd-14-baseline` on a commit that is not an ancestor of `HEAD`; commits `base`, `a (#14)` | `ok`, the commit `base`, `from: first-commit` | +| `auto_tag.baseline_format` is `start-{N}`, tag `start-14` on an ancestor | `ok`, that tag's commit, `from: baseline-tag` | +| no tag; a commit before `a (#14)` has the byte 0x1e in its message | `ok`, the parent of `a (#14)` | +| `github_repo` is `o/issues`; no tag; commits `base`, `see other/r#14`, `fix (O/ISSUES#14)` | `ok`, the parent of `fix (O/ISSUES#14)` | +| tag `idd-14-baseline` on `HEAD` itself; commits `base`, `a (#14)` | `ok`, the commit `base`, `from: first-commit` | +| `log.showSignature` is on and a commit before `a (#14)` is signed | `ok`, the parent of `a (#14)` | +| a file named `HEAD` at the repository's top level; no tag; commits `base`, `a (#14)` | `ok`, the commit `base` | +| `i18n.logOutputEncoding` is `UTF-16`; no tag; commits `base`, `a (#14)` | `ok`, the commit `base` | + +--- +### Requirement: spec-change.py reads the repository that holds --cwd + +Every subcommand of `scripts/spec-change.py` that takes `--cwd` (`archived`, `links`, `preflight`, `archive-ref`, `link-base`) SHALL read the repository whose top level `git rev-parse --show-toplevel` reports for `--cwd`, and SHALL take that path exactly as git prints it, removing only the one line ending git appends (the name of the directory can itself end in a space or a newline). A top level whose name ends in whitespace is that directory, never a sibling directory whose name lacks the whitespace. + +#### Scenario: A top level whose name ends in whitespace + +- **GIVEN** two repositories side by side, `odd ` (with a trailing space) and `odd` +- **WHEN** a subcommand runs with `--cwd` inside `odd ` +- **THEN** it reads `odd ` + +##### Example: a top level whose name ends in whitespace + +| Repositories | Command | Result | +| --- | --- | --- | +| `odd ` has the change archived, the sibling `odd` does not | `archived --cwd 'odd ' --ref HEAD` | `pass` | +| `odd ` alone, with the change archived | `archived --cwd 'odd ' --ref HEAD` | `pass` | +| `odd ` has a spec added on a branch without an Example, the sibling `odd` has none | `links --base --cwd 'odd '` | `missing` | +| `pf ` has an `openspec/` directory, the sibling `pf` does not | `preflight --cwd 'pf ' --can-write yes` | `ready` | +| `pf ` alone, with an `openspec/` directory | `preflight --cwd 'pf ' --can-write yes` | `ready` | +| `nl` followed by a newline has the change archived, the sibling `nl` does not | `archived` with `--cwd` that directory, `--ref HEAD` | `pass` | diff --git a/plugins/issue-driven-dev/CHANGELOG.md b/plugins/issue-driven-dev/CHANGELOG.md index e8f201bd..1d5ad2d8 100644 --- a/plugins/issue-driven-dev/CHANGELOG.md +++ b/plugins/issue-driven-dev/CHANGELOG.md @@ -9,8 +9,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Version number left to the release. +### Added + +- **A code repository without a remote can be closed against its local branch (#547, #546).** The IDD + configuration takes a `spec_archive` field that maps a code repository's path (relative to the configuration's + directory) to `{"ref": "local", "branch": ""}`. For that repository `idd-close` Step 1.57 checks the archive + on the local branch instead of fetching the default branch from a remote that does not exist, and says so + (`local ref @`); Step 1.5 skips the branch lookup (`check-existing-work.sh --skip-branches`) and + still checks open PRs on the issue repository; the link check of `references/spec-wrapper.md` step 5 starts at + the issue's baseline tag, or at the parent of the oldest commit naming `#N`. An entry is accepted only while the + repository has no remote at all (a remote with a url, a pushurl or a vcs set, whatever the value); with a remote + it is an error, never a silent fallback. Without an entry nothing changes, and a configuration file that cannot be read as a JSON object, + or `"spec_archive": null`, counts as having no entry, so it does not block a repository that never set the field. + A key written twice is an error, found while the file is parsed. The branch must be a branch name, and the check + reads it whatever `HEAD` is; a linked worktree uses its main working tree's key (its own path when the main + repository is bare), and a linked worktree whose main working tree git does not record (a `--separate-git-dir` + repository whose git directory is not named `.git`, or a submodule) gets no entry. A + repository with no remote and no entry now stops without fetching, in Step 1.57 and in the link check: git would + read a local path named `origin` as the remote. The link check finds the issue in a separate code repository's + commits written as `owner/repo#N` for the configured `github_repo`, reading `git log` in UTF-8 whatever + `i18n.logOutputEncoding` says. With `--skip-branches` the helper does not look up the default branch either. + New `spec-change.py` subcommands `archive-ref` and `link-base`; documented in `config-protocol.md`. + ### Fixed +- **`spec-change.py` reads the repository whose top level ends in whitespace (#547).** `archived`, `links` and + `preflight` stripped the top level `git rev-parse --show-toplevel` printed, so a repository named `odd ` (with a + trailing space) was read as a sibling directory `odd`, or failed when there was none. They now take the path as + git prints it, removing only the one line ending git appends. + - **The edit-run suite has a time limit of its own (#508, #519).** Most of its run time is CPU time (about 65 s on an 18-core Mac), so it takes longer on a busy machine: run alone it took 84 s at a load near 20 and 123 s at a load of 60 to 100, and inside full runs up to 168 s (#512). Once, at a load near 50 and with another full run using diff --git a/plugins/issue-driven-dev/references/config-protocol.md b/plugins/issue-driven-dev/references/config-protocol.md index 837a2921..427ddeaf 100644 --- a/plugins/issue-driven-dev/references/config-protocol.md +++ b/plugins/issue-driven-dev/references/config-protocol.md @@ -487,6 +487,32 @@ Exit codes of `idd-ci-gate.sh run --commit [--cwd ]`: `0` pass, `1` In `local` mode a red remote check is reported and does not block merging. A release still requires green remote checks whatever the mode (`idd-close` Step 6.5), because only remote CI sees the older environments that users run. +### `spec_archive` field (#547) + +Which branch the spec checks read for a code repository that **deliberately has no remote** — typically a local-only sub-project inside the issue repository's working directory, holding data that must not go to GitHub. Without an entry, `idd-close` Step 1.57 fetches the default branch from the remote, and a repository without a remote can never pass it. + +```json +{ "spec_archive": { "projects/sub-repo": { "ref": "local", "branch": "master" } } } +``` + +| Part | Meaning | +|------|---------| +| key | The code repository's main working tree (for a linked worktree, the first entry `git worktree list` prints. When the main repository is bare, each linked worktree is keyed by its own path, so each needs its own entry. When git does not record the main working tree — a git directory made apart with `--separate-git-dir` under a name other than `.git`, or a submodule — a linked worktree gets no entry: run from the main working tree. A git directory made apart under the name `.git` is reported by git as the directory that holds it, and that directory is the key), relative to the directory that holds this `.claude/` (both resolved through symbolic links). `./sub/`, `sub/` and `sub` name the same path; `.` names the directory of this configuration itself; a key that is empty or has leading or trailing whitespace names nothing. Two keys that name the same path are an error | +| `ref` | Must be `local`. Any other value is an error | +| `branch` | The local branch that plays the default branch: a branch name that `git check-ref-format --branch` accepts unchanged (not `master~1` or `master@{0}`). Required: without a remote there is no `origin/HEAD` to read, and `HEAD` is only the branch checked out now. The check reads this branch whatever `HEAD` is | + +Read by `scripts/spec-change.py archive-ref` (and `link-base`), which walks up from the **code repository's** main working tree to the first IDD configuration — the same walk as the other fields, new file before legacy file at each level, stopping after `$HOME` (`/` itself is not read). A file that cannot be read as a JSON object (empty, `null`, not JSON, not UTF-8), and `"spec_archive": null`, are treated as having no entry, so a repository that never set the field is not blocked by it. Every `remote` answer carries `has_remote`: when it is false the repository has no remote at all, and Step 1.57 and the link check stop without fetching, with that reason (git would otherwise read a local path named `origin` as the remote). Write the entry in the issue repository's configuration; do not add a configuration to the code repository just for this, because a file without `github_repo` there would be the first one the other skills find. + +**Accepted only while the repository has no remote at all** — no name that `git remote` lists with a `url`, a `pushurl` or a `vcs` (a remote helper) set, whatever the value (an empty or blank `url` counts), and no legacy `remotes/` or `branches/` file in its git directory; a `remote` section with none of them (a global `remote.origin.prune`, say) does not count. An entry for a repository that has any remote is an error, not a silent fallback to the remote: an entry written because there was no remote must not outlive that reason. Other errors: `spec_archive` is not an object, two entries name the repository's path, a key is written twice (`spec_archive` itself, a key of it that names this path, or a key of the matching entry — found while the file is parsed, so no key the file holds can imitate a duplicate), the entry is not `{"ref": "local", "branch": ""}`, or the branch does not exist. A repository whose own path cannot be written as a key (it ends in a space, say) gets no entry: it never borrows the `.` entry of the configuration's own directory. Reasons name the configuration file relative to the code repository, never by an absolute path. + +What it changes, and nothing else: + +- `idd-close` Step 1.57 checks the archive on `refs/heads/` instead of fetching, and prints `local ref @`. +- `idd-close` Step 1.5 calls `check-existing-work.sh --skip-branches`: no fetch in the code repository, open PRs still checked on the issue repository. +- `references/spec-wrapper.md` step 5 starts the link check at the issue's baseline tag (`auto_tag.baseline_format`, default `idd--baseline`) when it is an ancestor of `HEAD` other than `HEAD` itself, or else at the parent of the oldest commit naming the issue: `#` with the left and right guards of `pr-issue-matching.md`, or the issue repository's own `#` — the form a commit in a separate code repository uses (`other/repo#N` does not count); neither → stop. + +A code repository that is not under the issue repository's directory finds no entry and keeps the remote behaviour (and its error). + ### `collaborators[]` field An OPTIONAL identity registry so IDD can resolve a person's alias / email / display-name → their GitHub `@login` **without guessing** — the hard rule set by [tagging-collaborators.md](../rules/tagging-collaborators.md). It is a *resolution accelerator*, **not** an authority: a table hit is still existence-verified via `gh api users/` before any mention is posted, because the table can go stale. diff --git a/plugins/issue-driven-dev/references/spec-wrapper.md b/plugins/issue-driven-dev/references/spec-wrapper.md index 1f3c515b..01cd8f5e 100644 --- a/plugins/issue-driven-dev/references/spec-wrapper.md +++ b/plugins/issue-driven-dev/references/spec-wrapper.md @@ -127,19 +127,45 @@ fi 5. 連結檢查: ```bash -DEFAULT_BRANCH=$(gh repo view "$GITHUB_REPO" --json defaultBranchRef -q .defaultBranchRef.name 2>/dev/null) || DEFAULT_BRANCH="" +# #547: a repository without a remote, opted into its local branch by spec_archive, starts at the issue's +# baseline (link-base prints ok); with no entry link-base prints remote and the default branch is fetched as before. +# The issue number: NUMBER in the skills that call this contract, N in idd-all. A remote repository never reads it. +ISSUE_N="${NUMBER:-${N:-}}" +LB=$(python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" link-base --cwd "$CWD" --issue "${ISSUE_N:-none}" 2>/dev/null) +LB_STATUS=$(printf '%s' "$LB" | python3 -I -c 'import json,sys; print(json.loads(sys.stdin.read() or "{}").get("status", ""))' 2>/dev/null) SPEC_BASE="" -if [ -n "$DEFAULT_BRANCH" ] && git -C "$CWD" fetch -q origin -- "$DEFAULT_BRANCH" 2>/dev/null; then - SPEC_BASE=$(git -C "$CWD" merge-base HEAD FETCH_HEAD 2>/dev/null) +if [ "$LB_STATUS" = ok ]; then + SPEC_BASE=$(printf '%s' "$LB" | python3 -I -c 'import json,sys; print(json.loads(sys.stdin.read()).get("base", ""))') + printf '%s' "$LB" | python3 -I -c 'import json,sys; d=json.loads(sys.stdin.read()); print("→ link check from %s (%s; spec_archive local mode)" % (d["base"][:7], d["from"]))' +elif [ "$LB_STATUS" = remote ]; then + # Only a repository that has a remote is fetched: without one, git would read a local path named origin. + LB_HAS_REMOTE=$(printf '%s' "$LB" | python3 -I -c 'import json,sys; print(json.loads(sys.stdin.read() or "{}").get("has_remote") is True)' 2>/dev/null) + if [ "$LB_HAS_REMOTE" = True ]; then + DEFAULT_BRANCH=$(gh repo view "$GITHUB_REPO" --json defaultBranchRef -q .defaultBranchRef.name 2>/dev/null) || DEFAULT_BRANCH="" + if [ -n "$DEFAULT_BRANCH" ] && git -C "$CWD" fetch -q origin -- "$DEFAULT_BRANCH" 2>/dev/null; then + SPEC_BASE=$(git -C "$CWD" merge-base HEAD FETCH_HEAD 2>/dev/null) + fi + else + echo "✗ spec close-out: link-base: this repository has no remote, so the default branch is not fetched; $(printf '%s' "$LB" | python3 -I -c 'import json,sys +try: print(json.loads(sys.stdin.read()).get("reason", "")) +except Exception: print("")' 2>/dev/null); stopping" >&2 + fi +else + echo "✗ spec close-out: link-base: $(printf '%s' "$LB" | python3 -I -c 'import json,sys +try: print(json.loads(sys.stdin.read()).get("reason", "") or "link-base printed nothing readable") +except Exception: print("link-base printed nothing readable")' 2>/dev/null); stopping" >&2 fi if [ -n "$SPEC_BASE" ]; then python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" links --base "$SPEC_BASE" --cwd "$CWD" -else - echo "✗ spec close-out: cannot find where this branch left the default branch (gh repo view, git fetch or git merge-base failed); stopping" >&2 +elif [ "$LB_STATUS" = remote ] && [ "${LB_HAS_REMOTE:-}" = True ]; then + echo "✗ spec close-out: cannot find where this branch left the default branch (gh repo view, git fetch or git merge-base failed)$(printf '%s' "$LB" | python3 -I -c 'import json,sys +try: r = json.loads(sys.stdin.read()).get("reason", "") +except Exception: r = "" +print("; link-base: " + r if r else "")' 2>/dev/null); stopping" >&2 fi ``` -`git merge-base HEAD FETCH_HEAD`(剛從遠端 fetch 的預設 branch)在 PR path 是分岔點;在 direct-commit path 是 run 還沒推的 commit 之前那一個。沒有印出 JSON(`SPEC_BASE` 取不到)就停下回報。`missing` 就補測試或標記再跑一次。要改的是 requirement 本身(例如寫了一個寫不出檢查的 Example)時,不改 `archive/` 底下的檔案,也不繞過 Spectra 直接改主規格:照第 7 步另建一個 change 修改那條 requirement,封存後再跑。`error` 停下回報。 +`git merge-base HEAD FETCH_HEAD`(剛從遠端 fetch 的預設 branch)在 PR path 是分岔點;在 direct-commit path 是 run 還沒推的 commit 之前那一個。程式 repo 沒有 remote、用 `spec_archive` 指到本機 branch 時(#547,見 `config-protocol.md`),沒有「還沒推的 commit」這條界線,起點改由 `link-base` 給:這張 issue 的 baseline tag(`auto_tag.baseline_format`,預設 `idd--baseline`;是 HEAD 的祖先、而且不是 HEAD 本身時),否則 HEAD 歷史裡第一個引用這張 issue 的 commit 的父 commit——引用指 `#`(左右邊界同 `pr-issue-matching.md`),或設定檔 `github_repo` 那個 repo 的 `/#`(程式 repo 與 issue repo 分開時 commit 就是這樣寫的);兩者都沒有就停下,不退回 `merge-base`(direct-commit 時那是 HEAD,範圍是空的)。沒有設定的 repo,`link-base` 回 `remote`、不讀 issue 號,照舊 fetch;但回答裡的 `has_remote` 不是 true(repo 根本沒有 remote)時不 fetch,停下並說明(git 會把名為 `origin` 的本機路徑當成 remote)。錯誤訊息以 `link-base:` 開頭。沒有印出 JSON(`SPEC_BASE` 取不到)就停下回報。`missing` 就補測試或標記再跑一次。要改的是 requirement 本身(例如寫了一個寫不出檢查的 Example)時,不改 `archive/` 底下的檔案,也不繞過 Spectra 直接改主規格:照第 7 步另建一個 change 修改那條 requirement,封存後再跑。`error` 停下回報。 6. 確認宣告與 HEAD 一致:把第 6 步要貼的 `**Spec**:` 行寫進一個暫存檔,跑 `python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" archived --file <暫存檔> --cwd "$CWD" --ref HEAD`,要 `pass`。 diff --git a/plugins/issue-driven-dev/scripts/check-existing-work.sh b/plugins/issue-driven-dev/scripts/check-existing-work.sh index ad449091..3db26755 100755 --- a/plugins/issue-driven-dev/scripts/check-existing-work.sh +++ b/plugins/issue-driven-dev/scripts/check-existing-work.sh @@ -2,7 +2,9 @@ # check-existing-work.sh — does a PR or a branch already address issue N? (#366) # # Usage: -# check-existing-work.sh [--cwd DIR] [...] +# check-existing-work.sh [--cwd DIR] [--skip-branches] [...] +# --skip-branches (#547): no git fetch and no remote-tracking branches in DIR (a clone without a remote, opted +# into its local branch); the PR lookups run as usual and a top-level "notes" list says the lookup was skipped. # # Stdout: JSON # {"repo":..., "truncated":bool, "errors":[...], @@ -36,16 +38,23 @@ COMMIT_REFS = re.compile(r'^\s*refs\b', re.I) def usage(msg): print(f"check-existing-work.sh: {msg}", file=sys.stderr) - print("usage: check-existing-work.sh [--cwd DIR] ...", file=sys.stderr) + print("usage: check-existing-work.sh [--cwd DIR] [--skip-branches] ...", file=sys.stderr) sys.exit(2) args = sys.argv[1:] cwd = "." -if args[:1] == ["--cwd"]: +skip_branches = False +seen_cwd = False +while args[:1] in (["--cwd"], ["--skip-branches"]): + if args[0] == "--skip-branches": + skip_branches, args = True, args[1:] + continue + if seen_cwd: + usage("--cwd given more than once") if len(args) < 2: usage("--cwd needs a directory") - cwd, args = args[1], args[2:] + cwd, args, seen_cwd = args[1], args[2:], True if len(args) < 2: usage("need and at least one issue number") repo, issues = args[0], args[1:] @@ -108,30 +117,37 @@ if len(merged_prs) >= LIMIT: errors.append(f"merged PR list reached its limit ({LIMIT}); older merged PRs were not checked") branches, default = {}, None -f = run(["git", "-C", cwd, "fetch", "--quiet", "--prune", "origin"]) -if f.returncode != 0: - errors.append(f"git fetch failed: {(f.stderr or '').strip()[:160]}") -r = run(["git", "-C", cwd, "for-each-ref", "--format=%(refname:short)\t%(objectname)", "refs/remotes/origin"]) -if r.returncode == 0: - for line in r.stdout.splitlines(): - name, _, oid = line.partition("\t") - if name.startswith("origin/") and name != "origin/HEAD": - branches[name[len("origin/"):]] = oid +notes = [] +if skip_branches: + # #547: a clone without a remote (spec_archive.ref=local) has no remote-tracking branches to read + notes.append("branch lookup skipped (--skip-branches): no git fetch, no remote-tracking branches read in --cwd") else: - errors.append("git for-each-ref failed") + f = run(["git", "-C", cwd, "fetch", "--quiet", "--prune", "origin"]) + if f.returncode != 0: + errors.append(f"git fetch failed: {(f.stderr or '').strip()[:160]}") + r = run(["git", "-C", cwd, "for-each-ref", "--format=%(refname:short)\t%(objectname)", "refs/remotes/origin"]) + if r.returncode == 0: + for line in r.stdout.splitlines(): + name, _, oid = line.partition("\t") + if name.startswith("origin/") and name != "origin/HEAD": + branches[name[len("origin/"):]] = oid + else: + errors.append("git for-each-ref failed") # The default branch comes from GitHub. The clone's refs/remotes/origin/HEAD is a snapshot from clone time and can # point at an old feature branch (seen on a real clone), which makes every commit on the real default branch look -# unmerged. It is only the fallback when GitHub cannot be asked. -rv = gh_json(["gh", "repo", "view", repo, "--json", "defaultBranchRef"], "gh repo view") -default = ((rv or {}).get("defaultBranchRef") or {}).get("name") -if default is None: - h = run(["git", "-C", cwd, "symbolic-ref", "--short", "refs/remotes/origin/HEAD"]) - if h.returncode == 0 and h.stdout.strip().startswith("origin/"): - default = h.stdout.strip()[len("origin/"):] - else: - default = next((d for d in ("main", "master") if d in branches), None) -if default is None: - errors.append("could not determine the default branch") +# unmerged. It is only the fallback when GitHub cannot be asked. With --skip-branches there are no branches to +# compare, so the default branch is not looked up at all (and origin/HEAD, a remote-tracking ref, is not read; #547). +if not skip_branches: + rv = gh_json(["gh", "repo", "view", repo, "--json", "defaultBranchRef"], "gh repo view") + default = ((rv or {}).get("defaultBranchRef") or {}).get("name") + if default is None: + h = run(["git", "-C", cwd, "symbolic-ref", "--short", "refs/remotes/origin/HEAD"]) + if h.returncode == 0 and h.stdout.strip().startswith("origin/"): + default = h.stdout.strip()[len("origin/"):] + else: + default = next((d for d in ("main", "master") if d in branches), None) + if default is None: + errors.append("could not determine the default branch") merged_heads = {p["headRefOid"] for p in merged_prs} stale = {b for b, oid in branches.items() if oid in merged_heads} @@ -207,5 +223,8 @@ for n in issues: verdict, reason = "clear", "" out[str(n)] = {"verdict": verdict, "reason": reason, "evidence": ev} -print(json.dumps({"repo": repo, "truncated": truncated, "errors": errors, "issues": out}, ensure_ascii=False)) +result = {"repo": repo, "truncated": truncated, "errors": errors, "issues": out} +if notes: + result["notes"] = notes +print(json.dumps(result, ensure_ascii=False)) PY diff --git a/plugins/issue-driven-dev/scripts/spec-change.py b/plugins/issue-driven-dev/scripts/spec-change.py index 10b0751a..e76fbce5 100755 --- a/plugins/issue-driven-dev/scripts/spec-change.py +++ b/plugins/issue-driven-dev/scripts/spec-change.py @@ -35,6 +35,27 @@ {"form": proposed|change|no-behaviour-change|third-party-clone|none|invalid, "change": "...", "reason": "..."} exit 0, 1 (invalid), 2 (unreadable; no JSON) + archive-ref --cwd (#547) + Which branch do the spec checks read for this repository? The first IDD configuration walking up + from the repository's main working tree may map its path (relative to the directory holding .claude/) + in `spec_archive` to {"ref": "local", "branch": ""}; accepted only without any remote (a url, pushurl + or vcs, or a legacy remotes/branches file). A configuration that cannot be read as a JSON object, and + `"spec_archive": null`, count as having no entry (remote, with a reason). A key written twice (spec_archive + itself, a key of it, a key of the matching entry) is an error. Every remote answer carries has_remote; + false means the repository has no remote at all, and callers stop instead of fetching. + {"status": remote|local|error, "branch": "...", "ref": "", "config": "...", "key": "...", + "has_remote": true|false, "reason": "..."} exit 0 (remote, local), 2 (error) + + link-base --cwd --issue (#547) + Where does the link check start in local mode? The baseline tag (auto_tag.baseline_format, default + idd--baseline) when it is an ancestor of HEAD other than HEAD itself, else the parent of the oldest + commit of HEAD's history naming the issue: # (pr-issue-matching.md guards) or the configuration's + github_repo as /# (letter case ignored); neither, or a root commit → error. git log is read + in UTF-8 with signatures off, and every id must be hexadecimal. The configuration is resolved as archive-ref + does first: remote (with has_remote) and error never read --issue. + {"status": remote|ok|error, "base": "", "from": baseline-tag|first-commit, "reason": "..."} + exit 0 (remote, ok), 2 (error) + Paths named openspec/ above are relative to the spec directory Spectra uses: the `spec_dir:` line of .spectra.yaml, or `openspec` when there is none (read at the revision checked; preflight reads the working tree). @@ -114,13 +135,18 @@ def git_text(cwd, *args): return git(cwd, *args).stdout.decode("utf-8") +def one_line(text): + """A path git printed on one line: only the line ending git appends goes (a name may end in a space or a newline).""" + return text[:-1] if text.endswith("\n") else text + + def repo_top(cwd): if not os.path.isdir(cwd): raise Fail(f"{cwd} is not a directory") p = git(cwd, "rev-parse", "--show-toplevel", check=False) if p.returncode != 0: raise Fail(f"{cwd} is not inside a git repository") - return p.stdout.decode("utf-8").strip() + return one_line(p.stdout.decode("utf-8")) # a path may end in whitespace: only git's line ending goes SPEC_DIR_RE = re.compile(r"^spec_dir:(.*)$") @@ -451,8 +477,8 @@ def cmd_preflight(argv): return emit({"status": "third-party", "message": "the account cannot write to this repository: " "implement without a spec change and declare `**Spec**: third-party clone`"}, 0) # Plain --git-dir / --git-common-dir (relative to top when not absolute): --path-format needs git 2.31. - gd = os.path.join(top, git_text(top, "rev-parse", "--git-dir").strip()) - cd = os.path.join(top, git_text(top, "rev-parse", "--git-common-dir").strip()) + gd = os.path.join(top, one_line(git_text(top, "rev-parse", "--git-dir"))) + cd = os.path.join(top, one_line(git_text(top, "rev-parse", "--git-common-dir"))) if os.path.realpath(gd) != os.path.realpath(cd): return emit({"status": "linked-worktree", "message": "this is a git linked worktree; spectra writes into " "the main working tree from here. Run in the main tree or a standalone clone"}, 1) @@ -556,11 +582,281 @@ def cmd_plan(argv): return emit({"form": form, "change": change, "reason": reason}, 1 if form == "invalid" else 0) +# ── archive-ref / link-base (#547) ────────────────────────────────────────── + +CONFIG_NAMES = (os.path.join(".claude", ".idd", "local.json"), os.path.join(".claude", "issue-driven-dev.local.json")) +ISSUE_REF_RE = r"(? or branches/ file in the git directory. A section with none of them + (remote.origin.prune) is no remote.""" + gd = os.path.join(top, one_line(git_text(top, "rev-parse", "--git-common-dir"))) + names = set(n for n in git_text(top, "remote").splitlines() if n) + for d in ("remotes", "branches"): # legacy files: newer git no longer lists them, fetch may still read them + try: + names.update(f for f in os.listdir(os.path.join(gd, d)) if os.path.isfile(os.path.join(gd, d, f))) + except OSError: + pass + out = [] + for name in names: + # the key being set is what counts, whatever its value: a url of " " names a directory git can fetch from + has = any(git(top, "config", "--get-all", f"remote.{name}.{k}", check=False).returncode == 0 + for k in ("url", "pushurl", "vcs")) + has = has or any(os.path.isfile(os.path.join(gd, d, name)) for d in ("remotes", "branches")) + if has: + out.append(name) + return sorted(out) + + +def archive_ref(cwd): + """{"status": "remote", reason, has_remote} | {"status": "local", branch, ref, config, key}; raises Fail for an error. + has_remote is false when the repository has no remote at all: callers then stop instead of fetching (git would + read a local path named like the remote).""" + top = repo_top(cwd) + tree = main_tree(top) + + def remote(reason): + return {"status": "remote", "reason": reason, "has_remote": bool(remotes_of(top))} + + if tree is None: + return remote("this is a linked worktree whose main working tree git does not record (a repository whose " + "git directory was made apart with --separate-git-dir, or a submodule); no spec_archive key is " + "guessed: run from the main working tree") + path, root = find_config(tree) + if path is None: + return remote("no IDD configuration found above the repository") + shown = rel_to(path, tree) # messages name the file as seen from the repository + data, why = read_config(path, shown) + if data is None: + return remote(why) + if "spec_archive" in data.dups: + raise Fail(f"{shown} has more than one entry for spec_archive (the field is written twice)") + if data.get("spec_archive") is None: + return remote(f"{shown} has no spec_archive field") + table = data["spec_archive"] + if not isinstance(table, dict): + raise Fail(f"spec_archive in {shown} must be an object keyed by repository path") + rel = os.path.relpath(tree, root) + key = norm_key(rel) + if key is None: # never fall back to "." (that is another directory's entry) + return remote(f"this repository's path {rel!r} cannot be written as a spec_archive key") + dup = [k for k in table.dups if norm_key(k) == key] + entries = [v for k, v in table.items() if norm_key(k) == key] + if not entries: + return remote(f"spec_archive in {shown} has no entry for {key}") + if len(entries) > 1 or dup: + raise Fail(f"spec_archive in {shown} has more than one entry for {key}") + entry = entries[0] + if isinstance(entry, JsonObject) and entry.dups: + raise Fail(f"spec_archive[{key!r}] in {shown} has more than one entry for {', '.join(map(repr, entry.dups))}") + if not isinstance(entry, dict) or entry.get("ref") != "local": + raise Fail(f"spec_archive[{key!r}] in {shown}: only {{\"ref\": \"local\", \"branch\": \"\"}} is supported") + branch = entry.get("branch") + if (not isinstance(branch, str) or not branch or branch.startswith("-") + or any(ord(c) < 32 or ord(c) == 127 for c in branch)): + raise Fail(f"spec_archive[{key!r}] in {shown} needs a branch name") + fmt = git(top, "check-ref-format", "--branch", branch, check=False) + if fmt.returncode != 0 or fmt.stdout.decode("utf-8", "replace").strip() != branch: + raise Fail(f"spec_archive[{key!r}] in {shown}: {branch!r} is not a branch name") + remotes = remotes_of(top) + if remotes: + raise Fail(f"spec_archive[{key!r}] in {shown} asks for the local branch, but this repository has a remote " + f"({', '.join(remotes)}); the local branch is accepted only for a repository without any remote") + p = git(top, "rev-parse", "--verify", "--quiet", f"refs/heads/{branch}^{{commit}}", check=False) + if p.returncode != 0: + raise Fail(f"spec_archive[{key!r}] in {shown} names the branch {branch!r}, which does not exist here") + return {"status": "local", "branch": branch, "ref": p.stdout.decode("utf-8").strip(), "config": path, "config_shown": shown, "key": key} + + +def cmd_archive_ref(argv): + a = parse(argv, {"--cwd"}, ["--cwd"]) + try: + out = archive_ref(a["--cwd"]) + except Fail as exc: + return emit({"status": "error", "reason": str(exc)}, 2) + return emit(out, 0) + + +def config_of(config_path): + data, _ = read_config(config_path, "") + return data or {} + + +def baseline_tag(top, data, n): + at = data.get("auto_tag") + fmt = at.get("baseline_format") if isinstance(at, dict) else None + if isinstance(fmt, str) and "{N}" in fmt: + name = fmt.replace("{N}", n) + if git(top, "check-ref-format", f"refs/tags/{name}", check=False).returncode == 0: + return name + return f"idd-{n}-baseline" + + +def names_issue(data, n): + """A regex for a commit message that names issue n: #n with the pr-issue-matching guards, or the issue + repository's own owner/repo#n (the form a separate code repository uses).""" + parts = [ISSUE_REF_RE % n] + gh = data.get("github_repo") + if isinstance(gh, str) and re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", gh): + parts.append(r"(?&1); CLOSE_RC=$? assert_eq "idd-close: an issue the lookup could not read does not pass the PR gate" 1 "$CLOSE_RC" assert_grep "idd-close: and says why" "refusing to close" "$CLOSE_OUT" +# ── the branch lookup can be skipped (#547) ───────────────────────────────── +# spec-example: idd-existing-work-lookup / the branch lookup can be skipped +NR="$W/noremote"; git "${GIT_ENV[@]}" init -q "$NR"; ( cd "$NR" && git "${GIT_ENV[@]}" commit -q --allow-empty -m init ) +BR="$W/badremote"; git "${GIT_ENV[@]}" init -q "$BR"; ( cd "$BR" && git "${GIT_ENV[@]}" commit -q --allow-empty -m init ) +git -C "$BR" remote add origin "$W/no-such-origin.git" +runin() { PATH="$W/bin:$PATH" bash "$SCRIPT" "$@" 2>"$W/stderr"; } +reset_fx; issue 61 OPEN $ISSUE_T +pr open 30 "codex/a" fff $NEWER "" $'Refs #61\n\nbody' +OUT=$(runin --cwd "$BR" test/repo 61) +assert_grep "an origin that cannot be fetched, no flag → git fetch failed" "git fetch failed" "$(field "$OUT" "d['errors']")" +assert_eq "… no notes field" "False" "$(field "$OUT" "'notes' in d")" +assert_eq "… the open PR is E1" "['E1']" "$(kinds "$OUT" 61)" +OUT=$(runin --cwd "$NR" --skip-branches test/repo 61) +refute_grep "no remote, --skip-branches → no git fetch failed" "git fetch failed" "$(field "$OUT" "d['errors']")" +assert_grep "… notes say the branch lookup was skipped" "branch lookup skipped" "$(field "$OUT" "d.get('notes')")" +assert_eq "… the open PR is still E1" "['E1']" "$(kinds "$OUT" 61)" +OUT=$(runin --skip-branches --cwd "$NR" test/repo 61) +assert_eq "… the flag before --cwd works too" "['E1']" "$(kinds "$OUT" 61)" +touch "$FX/fail_pr_open" +OUT=$(runin --cwd "$NR" --skip-branches test/repo 61) +assert_grep "no remote, --skip-branches, the open PR list fails → reported" "--state open" "$(field "$OUT" "d['errors']")" +assert_grep "… notes still say the branch lookup was skipped" "branch lookup skipped" "$(field "$OUT" "d.get('notes')")" +assert_eq "… verdict unknown" "unknown" "$(verdict "$OUT" 61)" +rm -f "$FX/fail_pr_open" + +# idd-close Step 1.5 passes --skip-branches only when archive-ref says local +OPTP="$W/opted"; mkdir -p "$OPTP/.claude/.idd"; git "${GIT_ENV[@]}" init -q "$OPTP/code" +( cd "$OPTP/code" && git "${GIT_ENV[@]}" checkout -q -b master && git "${GIT_ENV[@]}" commit -q --allow-empty -m init ) +printf '%s\n' '{"spec_archive": {"code": {"ref": "local", "branch": "master"}}}' > "$OPTP/.claude/.idd/local.json" +OPT_OUT=$(HOME="$W" CLAUDE_PLUGIN_ROOT="$HERE/../../.." WORKDIR="$OPTP/code" GITHUB_REPO=test/repo NUMBER=61 PATH="$W/bin:$PATH" \ + bash -c "$(snippet "$CSEC") +printf 'EWNOTES=%s\\n' \"\$(printf '%s' \"\$EW_JSON\" | jq -c '.notes // []')\" +printf 'OPENPRS=%s\\n' \"\$(printf '%s' \"\$OPEN_PRS\" | jq -c '[.[].number]')\"" 2>&1) +assert_grep "idd-close: in a repository opted into its local branch, Step 1.5 says the branch lookup was skipped" \ + "branch lookup skipped" "$OPT_OUT" +NOPT_OUT=$(HOME="$W" CLAUDE_PLUGIN_ROOT="$HERE/../../.." WORKDIR="$BR" GITHUB_REPO=test/repo NUMBER=61 PATH="$W/bin:$PATH" \ + bash -c "$(snippet "$CSEC") +printf 'EWNOTES=%s\\n' \"\$(printf '%s' \"\$EW_JSON\" | jq -c '.notes // []')\"" 2>&1) +refute_grep "idd-close: in a repository not opted in, Step 1.5 does not skip the branch lookup" "branch lookup skipped" "$NOPT_OUT" +assert_grep "idd-close: Step 1.5 asks archive-ref" 'spec-change.py" archive-ref' "$CSEC" +# idd-547-fix1: the helper itself received --skip-branches (its notes say so), and the open PR still reaches the gate +assert_grep "idd-close: in local mode the helper ran with --skip-branches (its notes say so)" "branch lookup skipped" \ + "$(printf '%s\n' "$OPT_OUT" | sed -n 's/^EWNOTES=//p')" +assert_eq "idd-close: … and the open PR declaring the issue still reaches the gate" "[30]" \ + "$(printf '%s\n' "$OPT_OUT" | sed -n 's/^OPENPRS=//p')" +assert_eq "idd-close: not opted in → the helper ran without the flag (no notes)" "[]" \ + "$(printf '%s\n' "$NOPT_OUT" | sed -n 's/^EWNOTES=//p')" +runin --cwd "$NR" --cwd "$BR" test/repo 61 >/dev/null; RC_DUP=$? +# idd-547-fix2: a logging git in front of the real one shows what the helper ran. +REALGIT=$(command -v git); mkdir -p "$W/gitlog" +printf '#!/bin/sh\necho "$*" >> "%s/gitlog/calls"\nexec "%s" "$@"\n' "$W" "$REALGIT" > "$W/gitlog/git" +python3 -c 'import os,sys; os.chmod(sys.argv[1], 0o755)' "$W/gitlog/git" +TR="$W/tracked"; git "${GIT_ENV[@]}" clone -q "$W/origin.git" "$TR" 2>/dev/null +mkbr "idd/61-x" "work for 61" >/dev/null; git -C "$TR" fetch -q origin +reset_fx; issue 61 OPEN $ISSUE_T +: > "$W/gitlog/calls" +OUT=$(PATH="$W/gitlog:$W/bin:$PATH" bash "$SCRIPT" --cwd "$TR" --skip-branches test/repo 61 2>"$W/stderr") +refute_grep "--skip-branches runs no git fetch" "fetch" "$(cat "$W/gitlog/calls")" +refute_grep "--skip-branches lists no remote-tracking branches" "refs/remotes/origin" "$(cat "$W/gitlog/calls")" +assert_eq "--skip-branches: a tracking ref idd/61-x is not read as E4" "[]" "$(kinds "$OUT" 61)" +: > "$W/gitlog/calls" +OUT=$(PATH="$W/gitlog:$W/bin:$PATH" bash "$SCRIPT" --cwd "$TR" test/repo 61 2>"$W/stderr") +assert_grep "without the flag the helper fetches" "fetch" "$(cat "$W/gitlog/calls")" +assert_eq "… and reads idd/61-x as E4" "['E4']" "$(kinds "$OUT" 61)" +# archive-ref answers error (spec_archive is a list): Step 1.5 does not pass the flag +ERRP="$W/errconf"; mkdir -p "$ERRP/.claude/.idd"; git "${GIT_ENV[@]}" init -q "$ERRP/code" +( cd "$ERRP/code" && git "${GIT_ENV[@]}" commit -q --allow-empty -m init ) +printf '%s\n' '{"spec_archive": ["code"]}' > "$ERRP/.claude/.idd/local.json" +ERR_OUT=$(HOME="$W" CLAUDE_PLUGIN_ROOT="$HERE/../../.." WORKDIR="$ERRP/code" GITHUB_REPO=test/repo NUMBER=61 PATH="$W/bin:$PATH" \ + bash -c "$(snippet "$CSEC") +printf 'EWNOTES=%s\\n' \"\$(printf '%s' \"\$EW_JSON\" | jq -c '.notes // []')\"" 2>&1) +# idd-547-fix3: --skip-branches with gh repo view failing does not read origin/HEAD in --cwd +touch "$FX/fail_repo_view"; : > "$W/gitlog/calls" +OUT=$(PATH="$W/gitlog:$W/bin:$PATH" bash "$SCRIPT" --cwd "$TR" --skip-branches test/repo 61 2>"$W/stderr") +refute_grep "--skip-branches with gh repo view failing reads no origin/HEAD" "refs/remotes/origin/HEAD" "$(cat "$W/gitlog/calls")" +refute_grep "… and adds no default-branch error (the default branch is not needed)" "default branch" "$(field "$OUT" "d['errors']")" +refute_grep "… nor asks GitHub for it" "repo view" "$(field "$OUT" "d['errors']")" +refute_grep "… runs no git fetch (idd-547-fix4)" "fetch" "$(cat "$W/gitlog/calls")" +assert_grep "… notes say the branch lookup was skipped (idd-547-fix4)" "branch lookup skipped" "$(field "$OUT" "d.get('notes')")" +assert_eq "… none open (idd-547-fix4)" "[]" "$(kinds "$OUT" 61)" +rm -f "$FX/fail_repo_view" +assert_eq "idd-close: archive-ref answers error → Step 1.5 does not pass --skip-branches" "[]" \ + "$(printf '%s\n' "$ERR_OUT" | sed -n 's/^EWNOTES=//p')" +assert_eq "a repeated --cwd is a usage error (exit 2)" 2 "$RC_DUP" + print_summary "check-existing-work" diff --git a/plugins/issue-driven-dev/scripts/tests/spec-archive-local/test.sh b/plugins/issue-driven-dev/scripts/tests/spec-archive-local/test.sh new file mode 100755 index 00000000..593e3191 --- /dev/null +++ b/plugins/issue-driven-dev/scripts/tests/spec-archive-local/test.sh @@ -0,0 +1,446 @@ +#!/usr/bin/env bash +# spec-archive-local suite (#547) — a repository without a remote can opt into its local +# default branch: `spec-change.py archive-ref` decides which branch the spec checks read, +# `spec-change.py link-base` gives the base of the link check in that mode. +# +# spec-example: spec-on-every-issue / how a spec_archive entry resolves +# spec-example: spec-on-every-issue / where the local link check starts +# spec-example: spec-on-every-issue / a top level whose name ends in whitespace +# +# Real git repositories under a temporary HOME, so the walk up for the IDD configuration +# stops inside the fixture and never reads the developer's own files. + +set -u +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +. "$HERE/../../lib/assert-helpers.sh" +PLUGIN="$(cd "$HERE/../../.." && pwd)" +SC="$PLUGIN/scripts/spec-change.py" +WORK=$(mktemp -d "${TMPDIR:-/tmp}/idd-spec-archive-local.XXXXXX") +WORK=$(cd "$WORK" && pwd -P) +trap 'rm -rf "$WORK"' EXIT +export HOME="$WORK" GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 +export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@example.test GIT_COMMITTER_NAME=t GIT_COMMITTER_EMAIL=t@example.test +field() { python3 -c 'import json,sys; d=json.loads(sys.stdin.read() or "{}"); print(d.get(sys.argv[1], ""))' "$1"; } + +mkrepo() { # dir → a repository with one commit on master + mkdir -p "$1"; git init -q -b master "$1" + git -C "$1" commit -q --allow-empty -m base +} +conf() { # parent-dir json → writes /.claude/.idd/local.json + mkdir -p "$1/.claude/.idd"; printf '%s\n' "$2" > "$1/.claude/.idd/local.json" +} +ar() { OUT=$(python3 "$SC" archive-ref --cwd "$1" 2>/dev/null); RC=$?; ST=$(printf '%s' "$OUT" | field status); } +OPT='{"ref": "local", "branch": "master"}' + +echo "spec-archive-local: how a spec_archive entry resolves" +mkrepo "$WORK/none/sub" +ar "$WORK/none/sub" +assert_eq "no configuration found → remote" remote "$ST" +assert_exit "… exit 0" 0 "$RC" + +mkrepo "$WORK/a2/sub"; conf "$WORK/a2" '{"github_repo": "o/r"}' +ar "$WORK/a2/sub" +assert_eq "a file without spec_archive → remote" remote "$ST" + +mkrepo "$WORK/a3/sub"; conf "$WORK/a3" "{\"spec_archive\": {\"other\": $OPT}}" +ar "$WORK/a3/sub" +assert_eq "spec_archive names another path only → remote" remote "$ST" + +mkrepo "$WORK/a4/sub"; conf "$WORK/a4" "{\"spec_archive\": {\"sub\": $OPT}}" +ar "$WORK/a4/sub" +assert_eq "spec_archive names this path → local" local "$ST" +assert_exit "… exit 0" 0 "$RC" +assert_eq "… branch master" master "$(printf '%s' "$OUT" | field branch)" +assert_eq "… and its commit" "$(git -C "$WORK/a4/sub" rev-parse master)" "$(printf '%s' "$OUT" | field ref)" +assert_eq "… key is the path relative to the configuration" sub "$(printf '%s' "$OUT" | field key)" +mkdir -p "$WORK/a4/sub/deeper" +ar "$WORK/a4/sub/deeper" +assert_eq "… also from a directory inside the repository" local "$ST" + +mkrepo "$WORK/a5/sub"; conf "$WORK/a5" "{\"spec_archive\": {\"./sub/\": $OPT}}" +ar "$WORK/a5/sub" +assert_eq "the path written as ./sub/ → local" local "$ST" + +mkrepo "$WORK/a6/sub"; conf "$WORK/a6" "{\"spec_archive\": {\"sub\": $OPT}}" +git -C "$WORK/a6/sub" remote add origin "$WORK/nowhere.git" +ar "$WORK/a6/sub" +assert_eq "the repository has a remote → error" error "$ST" +assert_exit "… exit 2" 2 "$RC" +assert_grep "… naming the remote" "origin" "$(printf '%s' "$OUT" | field reason)" + +mkrepo "$WORK/a7/sub"; conf "$WORK/a7" '{"spec_archive": {"sub": {"ref": "local", "branch": "trunk"}}}' +ar "$WORK/a7/sub" +assert_eq "the branch does not exist → error" error "$ST" + +mkrepo "$WORK/a8/sub"; conf "$WORK/a8" '{"spec_archive": {"sub": {"ref": "remote", "branch": "master"}}}' +ar "$WORK/a8/sub" +assert_eq "ref is not local → error" error "$ST" + +mkrepo "$WORK/a9/sub"; conf "$WORK/a9" '{"spec_archive": {"sub": {"ref": "local"}}}' +ar "$WORK/a9/sub" +assert_eq "no branch → error" error "$ST" + +mkrepo "$WORK/a10/sub"; conf "$WORK/a10" '{"spec_archive": ["sub"]}' +ar "$WORK/a10/sub" +assert_eq "spec_archive is a list → error" error "$ST" + +mkrepo "$WORK/a11/sub"; conf "$WORK/a11" '{"spec_archive": ' +ar "$WORK/a11/sub" +assert_eq "the file is not JSON → remote (idd-547-fix1)" remote "$ST" +assert_exit "… exit 0" 0 "$RC" +assert_grep "… saying the file could not be read" "not readable" "$(printf '%s' "$OUT" | field reason)" + +# idd-547-fix1 rows +mkrepo "$WORK/b1/sub"; mkdir -p "$WORK/b1/.claude/.idd"; : > "$WORK/b1/.claude/.idd/local.json" +ar "$WORK/b1/sub" +assert_eq "an empty configuration file → remote" remote "$ST" +mkrepo "$WORK/b2/sub"; conf "$WORK/b2" 'null' +ar "$WORK/b2/sub" +assert_eq "a configuration holding null → remote" remote "$ST" +mkrepo "$WORK/b3/sub"; mkdir -p "$WORK/b3/.claude/.idd" +printf '\357\273\277%s\n' "{\"spec_archive\": {\"sub\": $OPT}}" > "$WORK/b3/.claude/.idd/local.json" +ar "$WORK/b3/sub" +assert_eq "a UTF-8 byte order mark → read as usual, local" local "$ST" +mkrepo "$WORK/b4/sub"; mkdir -p "$WORK/b4/.claude/.idd"; printf '{"a": "\377"}\n' > "$WORK/b4/.claude/.idd/local.json" +ar "$WORK/b4/sub" +assert_eq "a file that is not valid UTF-8 → remote" remote "$ST" +mkrepo "$WORK/b5/sub"; mkdir -p "$WORK/b5/.claude/.idd" +python3 -c 'print("[" * 100000 + "]" * 100000)' > "$WORK/b5/.claude/.idd/local.json" +ar "$WORK/b5/sub" +assert_eq "a file nested too deeply → remote, not a traceback" remote "$ST" +mkrepo "$WORK/b6/sub"; conf "$WORK/b6" "{\"spec_archive\": {\"sub\": $OPT, \"./sub/\": $OPT}}" +ar "$WORK/b6/sub" +assert_eq "two keys naming the same path → error" error "$ST" +assert_grep "… more than one entry" "more than one entry" "$(printf '%s' "$OUT" | field reason)" +mkrepo "$WORK/b7/sub"; conf "$WORK/b7" '{"spec_archive": {"sub": {"ref": "local", "branch": "master~1"}}}' +git -C "$WORK/b7/sub" commit -q --allow-empty -m second +ar "$WORK/b7/sub" +assert_eq "branch master~1 (revision syntax) → error" error "$ST" +assert_grep "… not a branch name (idd-547-fix4)" "is not a branch name" "$(printf '%s' "$OUT" | field reason)" +mkrepo "$WORK/b8/sub"; conf "$WORK/b8" '{"spec_archive": {"sub": {"ref": "local", "branch": "master@{0}"}}}' +ar "$WORK/b8/sub" +assert_eq "branch master@{0} → error" error "$ST" +mkrepo "$WORK/b9/sub"; conf "$WORK/b9" "{\"spec_archive\": {\" sub\": $OPT, \"\": $OPT}}" +ar "$WORK/b9/sub" +assert_eq "keys with surrounding whitespace or empty match nothing → remote" remote "$ST" +mkrepo "$WORK/b10/sub"; conf "$WORK/b10" "{\"spec_archive\": {\"sub\": $OPT}}" +printf '[remote "origin"]\n\tprune = true\n' > "$WORK/gitconfig-prune" +OUT=$(GIT_CONFIG_GLOBAL="$WORK/gitconfig-prune" python3 "$SC" archive-ref --cwd "$WORK/b10/sub" 2>/dev/null); ST=$(printf '%s' "$OUT" | field status) +assert_eq "a global remote section without a URL is not a remote → local" local "$ST" +mkrepo "$WORK/b11/sub"; conf "$WORK/b11" "{\"spec_archive\": {\"sub\": $OPT}}" +git -C "$WORK/b11/sub" worktree add -q "$WORK/b11/sub/.claude/worktrees/wt" -b wt 2>/dev/null +ar "$WORK/b11/sub/.claude/worktrees/wt" +assert_eq "asked from a linked worktree of the opted-in repository → local" local "$ST" +assert_eq "… with the main working tree's key" sub "$(printf '%s' "$OUT" | field key)" +mkrepo "$WORK/b12/sub"; conf "$WORK/b12" '{"github_repo": "o/r"}' +printf '%s\n' "{\"spec_archive\": {\"sub\": $OPT}}" > "$WORK/b12/.claude/issue-driven-dev.local.json" +ar "$WORK/b12/sub" +assert_eq "the new file wins over the legacy file at the same level → remote" remote "$ST" +mkdir -p "$WORK/abovehome/.claude/.idd"; printf '%s\n' "{\"spec_archive\": {\"home/sub\": $OPT}}" > "$WORK/abovehome/.claude/.idd/local.json" +mkrepo "$WORK/abovehome/home/sub" +OUT=$(HOME="$WORK/abovehome/home" python3 "$SC" archive-ref --cwd "$WORK/abovehome/home/sub" 2>/dev/null); ST=$(printf '%s' "$OUT" | field status) +assert_eq "a configuration above the home directory is not read → remote" remote "$ST" + +# The legacy file name is read too, after the new one at the same level. +mkrepo "$WORK/a12/sub"; mkdir -p "$WORK/a12/.claude" +printf '%s\n' "{\"spec_archive\": {\"sub\": $OPT}}" > "$WORK/a12/.claude/issue-driven-dev.local.json" +ar "$WORK/a12/sub" +assert_eq "the legacy configuration file → local" local "$ST" + +# The repository's own configuration, key "." +mkrepo "$WORK/a13"; conf "$WORK/a13" "{\"spec_archive\": {\".\": $OPT}}" +ar "$WORK/a13" +assert_eq "the repository's own configuration, key . → local" local "$ST" + +ar "$WORK" +assert_eq "not inside a git repository → error" error "$ST" + +echo "spec-archive-local: where the local link check starts" +lb() { OUT=$(cd "$1" && python3 "$SC" link-base --cwd "$1" --issue "$2" 2>/dev/null); RC=$?; ST=$(printf '%s' "$OUT" | field status); } +B="$WORK/lb1/repo"; mkrepo "$B"; conf "$WORK/lb1" "{\"spec_archive\": {\"repo\": $OPT}}" +git -C "$B" commit -q --allow-empty -m "tagged"; git -C "$B" tag idd-14-baseline +TAGGED=$(git -C "$B" rev-parse HEAD) +git -C "$B" commit -q --allow-empty -m "a (#14)" +lb "$B" 14 +assert_eq "tag on an ancestor of HEAD → ok" ok "$ST" +assert_eq "… the tag's commit" "$TAGGED" "$(printf '%s' "$OUT" | field base)" +assert_eq "… from baseline-tag" baseline-tag "$(printf '%s' "$OUT" | field from)" + +B="$WORK/lb2/repo"; mkrepo "$B"; conf "$WORK/lb2" "{\"spec_archive\": {\"repo\": $OPT}}" +BASE=$(git -C "$B" rev-parse HEAD) +git -C "$B" commit -q --allow-empty -m "a (#14)"; git -C "$B" commit -q --allow-empty -m $'b\n\nRefs #14' +lb "$B" 14 +assert_eq "no tag; the parent of the first commit naming #14 → ok" ok "$ST" +assert_eq "… the commit before it" "$BASE" "$(printf '%s' "$OUT" | field base)" +assert_eq "… from first-commit" first-commit "$(printf '%s' "$OUT" | field from)" + +B="$WORK/lb3/repo"; mkrepo "$B"; conf "$WORK/lb3" "{\"spec_archive\": {\"repo\": $OPT}}" +git -C "$B" commit -q --allow-empty -m "a (#142)" +lb "$B" 14 +assert_eq "only #142 names a 14 → error" error "$ST" +assert_exit "… exit 2" 2 "$RC" + +B="$WORK/lb4/repo"; mkdir -p "$B"; git init -q -b master "$B"; conf "$WORK/lb4" "{\"spec_archive\": {\"repo\": $OPT}}" +git -C "$B" commit -q --allow-empty -m "start (#14)" +lb "$B" 14 +assert_eq "the first commit with #14 is the root commit → error" error "$ST" + +B="$WORK/lb5/repo"; mkrepo "$B" +git -C "$B" commit -q --allow-empty -m "a (#14)" +lb "$B" 14 +assert_eq "not opted in → remote" remote "$ST" +assert_exit "… exit 0" 0 "$RC" + +lb "$WORK/lb2/repo" 14x +assert_eq "an issue number that is not digits → error" error "$ST" +assert_exit "… exit 2" 2 "$RC" + +# idd-547-fix1 rows +lb "$WORK/lb5/repo" abc +assert_eq "not opted in, --issue abc → remote (the issue is not read)" remote "$ST" +assert_exit "… exit 0" 0 "$RC" + +B="$WORK/lb6/repo"; mkrepo "$B"; conf "$WORK/lb6" "{\"spec_archive\": {\"repo\": $OPT}}" +git -C "$B" commit -q --allow-empty -m "see o/r#14"; git -C "$B" commit -q --allow-empty -m "x#14" +BEFORE=$(git -C "$B" rev-parse HEAD); git -C "$B" commit -q --allow-empty -m "fix (#14)" +lb "$B" 14 +assert_eq "o/r#14 and x#14 do not count; the parent of 'fix (#14)'" "$BEFORE" "$(printf '%s' "$OUT" | field base)" + +B="$WORK/lb7/repo"; mkrepo "$B"; conf "$WORK/lb7" "{\"spec_archive\": {\"repo\": $OPT}}" +BASE=$(git -C "$B" rev-parse HEAD) +git -C "$B" checkout -q -b side; git -C "$B" commit -q --allow-empty -m side; git -C "$B" tag idd-14-baseline; git -C "$B" checkout -q master +git -C "$B" commit -q --allow-empty -m "a (#14)" +lb "$B" 14 +assert_eq "a baseline tag that is not an ancestor of HEAD → falls back to the first commit" "$BASE" "$(printf '%s' "$OUT" | field base)" +assert_eq "… from first-commit" first-commit "$(printf '%s' "$OUT" | field from)" + +B="$WORK/lb8/repo"; mkrepo "$B"; conf "$WORK/lb8" "{\"auto_tag\": {\"baseline_format\": \"start-{N}\"}, \"spec_archive\": {\"repo\": $OPT}}" +git -C "$B" commit -q --allow-empty -m "tagged"; git -C "$B" tag start-14; T=$(git -C "$B" rev-parse HEAD) +git -C "$B" commit -q --allow-empty -m "a (#14)" +lb "$B" 14 +assert_eq "auto_tag.baseline_format start-{N} → that tag" "$T" "$(printf '%s' "$OUT" | field base)" +assert_eq "… from baseline-tag" baseline-tag "$(printf '%s' "$OUT" | field from)" + +B="$WORK/lb9/repo"; mkrepo "$B"; conf "$WORK/lb9" "{\"spec_archive\": {\"repo\": $OPT}}" +git -C "$B" commit -q --allow-empty -m "$(printf 'odd \036 byte')"; BEFORE=$(git -C "$B" rev-parse HEAD) +git -C "$B" commit -q --allow-empty -m "a (#14)" +lb "$B" 14 +assert_eq "a 0x1e byte in an earlier message does not hide a commit" "$BEFORE" "$(printf '%s' "$OUT" | field base)" + +# archive-ref reports the configured branch, not HEAD (idd-547-fix1) +git -C "$WORK/a4/sub" checkout -q -b elsewhere; git -C "$WORK/a4/sub" commit -q --allow-empty -m elsewhere +ar "$WORK/a4/sub" +assert_eq "HEAD on another branch → ref is still master's commit" "$(git -C "$WORK/a4/sub" rev-parse master)" "$(printf '%s' "$OUT" | field ref)" +git -C "$WORK/a4/sub" checkout -q master + +# idd-547-fix2 rows — how a spec_archive entry resolves +mkdir -p "$WORK/c1"; conf "$WORK/c1" "{\"spec_archive\": {\".\": $OPT}}"; git init -q -b master "$WORK/c1"; git -C "$WORK/c1" commit -q --allow-empty -m base +mkrepo "$WORK/c1/odd " +ar "$WORK/c1/odd " +assert_eq "a repository whose path cannot be a key does not fall back to . → remote" remote "$ST" +mkrepo "$WORK/c2/sub"; mkdir -p "$WORK/c2/.claude/.idd" +printf '{"spec_archive": {"sub": %s, "sub": %s}}\n' "$OPT" "$OPT" > "$WORK/c2/.claude/.idd/local.json" +ar "$WORK/c2/sub" +assert_eq "the same key written twice in the file → error" error "$ST" +assert_grep "… more than one entry (idd-547-fix3)" "more than one entry" "$(printf '%s' "$OUT" | field reason)" +mkrepo "$WORK/c3/sub"; conf "$WORK/c3" "{\"spec_archive\": {\"sub\": $OPT}}"; git -C "$WORK/c3/sub" config remote.origin.pushurl "$WORK/nowhere.git" +ar "$WORK/c3/sub" +assert_eq "a remote with only a pushurl counts → error" error "$ST" +assert_grep "… naming it" "origin" "$(printf '%s' "$OUT" | field reason)" +mkrepo "$WORK/c4/sub"; conf "$WORK/c4" "{\"spec_archive\": {\"sub\": $OPT}}"; mkdir -p "$WORK/c4/sub/.git/remotes" +printf 'URL: %s\n' "$WORK/nowhere.git" > "$WORK/c4/sub/.git/remotes/origin" +ar "$WORK/c4/sub" +assert_eq "a legacy remotes/origin file counts → error" error "$ST" +assert_grep "… naming the remote (idd-547-fix3)" "origin" "$(printf '%s' "$OUT" | field reason)" +mkdir -p "$WORK/c5" "$WORK/c5gd"; conf "$WORK/c5" "{\"spec_archive\": {\"sub\": $OPT}}" +git init -q -b master --separate-git-dir "$WORK/c5gd/.git" "$WORK/c5/sub"; git -C "$WORK/c5/sub" commit -q --allow-empty -m base +ar "$WORK/c5/sub" +assert_eq "a repository made with --separate-git-dir is keyed by its top level → local" local "$ST" +assert_eq "… with the key sub (idd-547-fix3)" sub "$(printf '%s' "$OUT" | field key)" +OUT=$(python3 "$SC" archive-ref --cwd "$WORK/a3/sub" 2>/dev/null) +refute_grep "reasons do not carry the configuration's absolute path" "$WORK/a3/.claude" "$(printf '%s' "$OUT" | field reason)" + +# idd-547-fix2 rows — where the local link check starts +B="$WORK/ld1/repo"; mkrepo "$B"; conf "$WORK/ld1" "{\"github_repo\": \"o/issues\", \"spec_archive\": {\"repo\": $OPT}}" +git -C "$B" commit -q --allow-empty -m "see other/r#14"; BEFORE=$(git -C "$B" rev-parse HEAD) +git -C "$B" commit -q --allow-empty -m "fix (O/ISSUES#14)" +lb "$B" 14 +assert_eq "github_repo o/issues: O/ISSUES#14 counts, other/r#14 does not" "$BEFORE" "$(printf '%s' "$OUT" | field base)" +B="$WORK/ld2/repo"; mkrepo "$B"; conf "$WORK/ld2" "{\"spec_archive\": {\"repo\": $OPT}}"; BASE=$(git -C "$B" rev-parse HEAD) +git -C "$B" commit -q --allow-empty -m "a (#14)"; git -C "$B" tag idd-14-baseline +lb "$B" 14 +assert_eq "a baseline tag on HEAD itself → falls back to the first commit" "$BASE" "$(printf '%s' "$OUT" | field base)" +assert_eq "… from first-commit" first-commit "$(printf '%s' "$OUT" | field from)" +B="$WORK/ld3/repo"; mkrepo "$B"; conf "$WORK/ld3" "{\"spec_archive\": {\"repo\": $OPT}}" +# A commit carrying a (fake) signature, and log.showSignature pointing at a program that leaves a sentinel +# (the pattern of merge-completeness fixture 40b). Without --no-show-signature git log runs it and prints its text. +mkdir -p "$WORK/fakegpg"; printf '#!/bin/sh\ntouch "%s/ran"\necho "gpg: Signature made by fake"\nexit 1\n' "$WORK/fakegpg" > "$WORK/fakegpg/gpg" +python3 -c 'import os,sys; os.chmod(sys.argv[1], 0o755)' "$WORK/fakegpg/gpg" +git -C "$B" commit -q --allow-empty -m signed +SIGNED=$(git -C "$B" cat-file commit HEAD | awk 'NR == 1 { print; next } !done && /^committer / { print; print "gpgsig -----BEGIN PGP SIGNATURE-----"; print " Zm9v"; print " -----END PGP SIGNATURE-----"; done = 1; next } { print }' \ + | git -C "$B" hash-object -t commit -w --stdin) +git -C "$B" update-ref refs/heads/master "$SIGNED" +git -C "$B" config log.showSignature true; git -C "$B" config gpg.program "$WORK/fakegpg/gpg" +BEFORE=$(git -C "$B" rev-parse HEAD); git -C "$B" commit -q --allow-empty -m "a (#14)" +lb "$B" 14 +assert_eq "log.showSignature on, a signed commit before → still the parent of a (#14)" "$BEFORE" "$(printf '%s' "$OUT" | field base)" +assert_exit "… and gpg.program is not run" 1 "$([ -e "$WORK/fakegpg/ran" ]; echo $?)" +B="$WORK/ld4/repo"; mkrepo "$B"; conf "$WORK/ld4" "{\"spec_archive\": {\"repo\": $OPT}}"; BASE=$(git -C "$B" rev-parse HEAD) +echo x > "$B/HEAD"; git -C "$B" add HEAD; git -C "$B" commit -q -m "a (#14)" +lb "$B" 14 +assert_eq "a file named HEAD at the top level → still ok" "$BASE" "$(printf '%s' "$OUT" | field base)" + +# idd-547-fix3 rows — how a spec_archive entry resolves +mkdir -p "$WORK/d1" "$WORK/d1gd"; conf "$WORK/d1" "{\"spec_archive\": {\"sub\": $OPT}}" +git init -q -b master --separate-git-dir "$WORK/d1gd/meta.git" "$WORK/d1/sub"; git -C "$WORK/d1/sub" commit -q --allow-empty -m base +git -C "$WORK/d1/sub" worktree add -q "$WORK/d1/wt" -b wt 2>/dev/null +ar "$WORK/d1/wt" +assert_eq "a linked worktree of a --separate-git-dir main tree → remote (no key is guessed)" remote "$ST" +assert_eq "… has_remote false (callers stop)" False "$(printf '%s' "$OUT" | field has_remote)" +assert_grep "… saying the main working tree is not recorded" "main working tree" "$(printf '%s' "$OUT" | field reason)" +# its own path is never used as the key: an entry under that name is not read +conf "$WORK/d1" "{\"spec_archive\": {\"wt\": $OPT}}" +ar "$WORK/d1/wt" +assert_eq "… even when an entry names the linked worktree's own path → remote" remote "$ST" +assert_eq "… has_remote false (idd-547-fix4)" False "$(printf '%s' "$OUT" | field has_remote)" +assert_grep "… saying the main working tree is not recorded (idd-547-fix4)" "main working tree" "$(printf '%s' "$OUT" | field reason)" +ar "$WORK/d1/sub" +assert_eq "the --separate-git-dir main working tree itself, no entry for sub → remote" remote "$ST" +mkrepo "$WORK/d2/sub"; mkdir -p "$WORK/d2/.claude/.idd" +printf '{"spec_archive": {"sub": %s}, "spec_archive": {"sub": %s}}\n' "$OPT" "$OPT" > "$WORK/d2/.claude/.idd/local.json" +ar "$WORK/d2/sub" +assert_eq "spec_archive written twice at the top level → error" error "$ST" +assert_grep "… more than one entry" "more than one entry" "$(printf '%s' "$OUT" | field reason)" +mkrepo "$WORK/d3/sub"; mkdir -p "$WORK/d3/.claude/.idd" +printf '{"spec_archive": {"sub": {"ref": "local", "branch": "master", "branch": "master"}}}\n' > "$WORK/d3/.claude/.idd/local.json" +ar "$WORK/d3/sub" +assert_eq "a key written twice inside the entry → error" error "$ST" +assert_grep "… more than one entry (idd-547-fix4)" "more than one entry" "$(printf '%s' "$OUT" | field reason)" +mkrepo "$WORK/d4/sub"; mkdir -p "$WORK/d4/.claude/.idd" +printf '{"spec_archive": {"sub": %s, "\\u0000duplicate-keys": ["sub"]}, "\\u0000duplicate-keys": ["spec_archive"]}\n' "$OPT" > "$WORK/d4/.claude/.idd/local.json" +ar "$WORK/d4/sub" +assert_eq "a key that looks like an internal marker does not imitate a duplicate → local" local "$ST" +mkrepo "$WORK/d5/sub"; conf "$WORK/d5" '{"spec_archive": null}'; git -C "$WORK/d5/sub" remote add origin "$WORK/nowhere.git" +ar "$WORK/d5/sub" +assert_eq "spec_archive null → remote" remote "$ST" +assert_eq "… has_remote true" True "$(printf '%s' "$OUT" | field has_remote)" +mkrepo "$WORK/d6/sub"; conf "$WORK/d6" "{\"spec_archive\": {\"sub\": $OPT}}"; git -C "$WORK/d6/sub" config remote.origin.vcs hg +ar "$WORK/d6/sub" +assert_eq "a remote with only a vcs counts → error" error "$ST" +assert_grep "… naming it" "origin" "$(printf '%s' "$OUT" | field reason)" +ar "$WORK/none/sub" +assert_eq "no configuration, no remote → has_remote false" False "$(printf '%s' "$OUT" | field has_remote)" + +# idd-547-fix3 rows — where the local link check starts +B="$WORK/le1/repo"; mkrepo "$B"; conf "$WORK/le1" "{\"spec_archive\": {\"repo\": $OPT}}"; BASE=$(git -C "$B" rev-parse HEAD) +git -C "$B" commit -q --allow-empty -m "a (#14)"; git -C "$B" config i18n.logOutputEncoding UTF-16 +lb "$B" 14 +assert_eq "i18n.logOutputEncoding UTF-16 → still the commit before a (#14)" "$BASE" "$(printf '%s' "$OUT" | field base)" + +# idd-547-fix3 — a top level whose name ends in a space +mkdir -p "$WORK/sp"; git init -q -b main "$WORK/sp/odd "; git init -q -b main "$WORK/sp/odd" +for r in "$WORK/sp/odd " "$WORK/sp/odd"; do mkdir -p "$r/openspec/specs"; echo x > "$r/openspec/specs/.keep"; git -C "$r" add -A; git -C "$r" commit -q -m base; done +mkdir -p "$WORK/sp/odd /openspec/changes/archive/2026-11-14-idd-9-c1"; echo p > "$WORK/sp/odd /openspec/changes/archive/2026-11-14-idd-9-c1/proposal.md" +git -C "$WORK/sp/odd " add -A; git -C "$WORK/sp/odd " commit -q -m "archive c1" +mkdir -p "$WORK/sp/odd/openspec/changes/idd-9-c1"; echo p > "$WORK/sp/odd/openspec/changes/idd-9-c1/proposal.md"; git -C "$WORK/sp/odd" add -A; git -C "$WORK/sp/odd" commit -q -m "open c1" +printf '%s\n' '' '**Spec**: change idd-9-c1' > "$WORK/sp/ic.md" +OUT=$(python3 "$SC" archived --file "$WORK/sp/ic.md" --cwd "$WORK/sp/odd " --ref HEAD 2>/dev/null) +assert_eq "archived on 'odd ' with a sibling 'odd' → reads 'odd ' (pass)" pass "$(printf '%s' "$OUT" | field status)" +mv "$WORK/sp/odd" "$WORK/sp/away" +OUT=$(python3 "$SC" archived --file "$WORK/sp/ic.md" --cwd "$WORK/sp/odd " --ref HEAD 2>/dev/null) +assert_eq "archived on 'odd ' alone → pass" pass "$(printf '%s' "$OUT" | field status)" +mv "$WORK/sp/away" "$WORK/sp/odd" +SPB=$(git -C "$WORK/sp/odd " rev-parse HEAD); git -C "$WORK/sp/odd " checkout -q -b feat +mkdir -p "$WORK/sp/odd /openspec/specs/cap"; printf '## Purpose\n\nx\n\n## Requirements\n\n### Requirement: Something new\n\nIt SHALL do it.\n' > "$WORK/sp/odd /openspec/specs/cap/spec.md" +git -C "$WORK/sp/odd " add -A; git -C "$WORK/sp/odd " commit -q -m "add a spec" +OUT=$(python3 "$SC" links --base "$SPB" --cwd "$WORK/sp/odd " 2>/dev/null) +assert_eq "links on 'odd ' with a sibling 'odd' → missing (reads 'odd ')" missing "$(printf '%s' "$OUT" | field status)" +# idd-547-fix4: a top level whose name ends in a newline, with a sibling without it +NL="$WORK/sp/nl"$'\n' +git init -q -b main "$NL"; mkdir -p "$NL/openspec/specs"; echo x > "$NL/openspec/specs/.keep"; git -C "$NL" add -A; git -C "$NL" commit -q -m base +mkdir -p "$NL/openspec/changes/archive/2026-11-14-idd-9-c1"; echo p > "$NL/openspec/changes/archive/2026-11-14-idd-9-c1/proposal.md" +git -C "$NL" add -A; git -C "$NL" commit -q -m "archive c1" +git init -q -b main "$WORK/sp/nl"; mkdir -p "$WORK/sp/nl/openspec/changes/idd-9-c1"; echo p > "$WORK/sp/nl/openspec/changes/idd-9-c1/proposal.md" +git -C "$WORK/sp/nl" add -A; git -C "$WORK/sp/nl" commit -q -m "open c1" +OUT=$(python3 "$SC" archived --file "$WORK/sp/ic.md" --cwd "$NL" --ref HEAD 2>/dev/null) +assert_eq "archived on a top level ending in a newline, with a sibling without it → reads itself (pass)" pass "$(printf '%s' "$OUT" | field status)" +# preflight, too (round 4 pre-check): 'pf ' has openspec/, its sibling 'pf' does not; a stub spectra on PATH +mkdir -p "$WORK/stubsp"; printf '#!/bin/sh\nexit 0\n' > "$WORK/stubsp/spectra"; python3 -c 'import os,sys; os.chmod(sys.argv[1], 0o755)' "$WORK/stubsp/spectra" +git init -q -b main "$WORK/sp/pf "; git -C "$WORK/sp/pf " commit -q --allow-empty -m base; mkdir -p "$WORK/sp/pf /openspec/specs" +git init -q -b main "$WORK/sp/pf"; git -C "$WORK/sp/pf" commit -q --allow-empty -m base +OUT=$(PATH="$WORK/stubsp:$PATH" python3 "$SC" preflight --cwd "$WORK/sp/pf " --can-write yes 2>/dev/null) +assert_eq "preflight on 'pf ' with a sibling 'pf' → reads 'pf ' (ready)" ready "$(printf '%s' "$OUT" | field status)" +mv "$WORK/sp/pf" "$WORK/sp/pf-away" +OUT=$(PATH="$WORK/stubsp:$PATH" python3 "$SC" preflight --cwd "$WORK/sp/pf " --can-write yes 2>/dev/null) +assert_eq "preflight on 'pf ' alone → ready" ready "$(printf '%s' "$OUT" | field status)" + +# idd-547-fix4 rows — how a spec_archive entry resolves +for U in " " ""; do + R="$WORK/u$([ -z "$U" ] && echo e || echo s)"; mkrepo "$R/sub"; conf "$R" "{\"spec_archive\": {\"sub\": $OPT}}" + git -C "$R/sub" config remote.origin.url "$U" + ar "$R/sub" + assert_eq "a remote whose url is '$U' counts → error" error "$ST" + assert_grep "… naming it" "origin" "$(printf '%s' "$OUT" | field reason)" +done +# a bare main repository: its linked worktree is keyed by its own path +mkdir -p "$WORK/bare"; git init -q --bare -b master "$WORK/bare/main.git" +git clone -q "$WORK/bare/main.git" "$WORK/bare/seed" 2>/dev/null; git -C "$WORK/bare/seed" commit -q --allow-empty -m base +git -C "$WORK/bare/seed" push -q origin HEAD:master 2>/dev/null +git -C "$WORK/bare/main.git" worktree add -q "$WORK/bare/wt" master 2>/dev/null +conf "$WORK/bare" "{\"spec_archive\": {\"wt\": $OPT}}" +ar "$WORK/bare/wt" +assert_eq "a linked worktree of a bare repository → local" local "$ST" +assert_eq "… keyed by its own path" wt "$(printf '%s' "$OUT" | field key)" +# --separate-git-dir /.git: git reports as the main working tree, and that path is the key +mkdir -p "$WORK/sg/store" "$WORK/sg/iss"; conf "$WORK/sg" "{\"spec_archive\": {\"store\": $OPT}}" +git init -q -b master --separate-git-dir "$WORK/sg/store/.git" "$WORK/sg/iss/sub"; git -C "$WORK/sg/iss/sub" commit -q --allow-empty -m base +git -C "$WORK/sg/iss/sub" worktree add -q "$WORK/sg/wt" -b wt 2>/dev/null +ar "$WORK/sg/wt" +assert_eq "a linked worktree of a --separate-git-dir /.git repository → keyed by " store "$(printf '%s' "$OUT" | field key)" +assert_eq "… local, the same repository's master" "$(git -C "$WORK/sg/iss/sub" rev-parse master)" "$(printf '%s' "$OUT" | field ref)" + +echo "spec-archive-local: the wrapper and idd-all text" +WRAP="$PLUGIN/references/spec-wrapper.md" +STEP5=$(sed -n '/^## 5\. Close-out/,/^## 6\./p' "$WRAP") +assert_grep "spec-wrapper step 5 asks link-base" 'spec-change.py" link-base' "$STEP5" +assert_grep "… with the issue number from NUMBER or N" '${NUMBER:-${N:-}}' "$STEP5" +refute_grep "step 5's JSON readers run isolated: no python3 -c without -I (idd-547-fix2)" "python3 -c 'import json" "$STEP5" +# idd-547-fix2: run the step-5 block as written. In local mode a link-base error stops with its reason and +# does not go on to print the failed-fetch message. +mkdir -p "$WORK/ghok"; printf '#!/bin/sh\ncase "$1 $2" in "repo view") echo main ;; *) exit 1 ;; esac\n' > "$WORK/ghok/gh" +python3 -c 'import os,sys; os.chmod(sys.argv[1], 0o755)' "$WORK/ghok/gh" +printf '%s\n' "$STEP5" | awk '/^```bash$/{b=1; next} b && /^```$/{exit} b' > "$WORK/step5.sh" +S5OUT=$(cd "$WORK/lb3/repo" && env HOME="$WORK" CWD="$WORK/lb3/repo" NUMBER=14 GITHUB_REPO=o/r CLAUDE_PLUGIN_ROOT="$PLUGIN" \ + bash --norc --noprofile "$WORK/step5.sh" 2>&1 &1 /dev/null +git -C "$RM/c" checkout -q -B main; mkdir -p "$RM/c/openspec/specs"; echo x > "$RM/c/openspec/specs/.keep" +git -C "$RM/c" add -A; git -C "$RM/c" commit -q -m base; git -C "$RM/c" push -q origin main 2>/dev/null +git -C "$RM/c" checkout -q -b feat; git -C "$RM/c" commit -q --allow-empty -m "work (#14)" +S5RM=$(cd "$RM/c" && env HOME="$WORK" CWD="$RM/c" NUMBER=14 GITHUB_REPO=o/r CLAUDE_PLUGIN_ROOT="$PLUGIN" \ + PATH="$WORK/ghok:$PATH" bash --norc --noprofile "$WORK/step5.sh" 2>&1 /dev/null)" +git -C "$RM/c" remote set-url origin "$RM/nowhere.git" +S5RF=$(cd "$RM/c" && env HOME="$WORK" CWD="$RM/c" NUMBER=14 GITHUB_REPO=o/r CLAUDE_PLUGIN_ROOT="$PLUGIN" \ + PATH="$WORK/ghok:$PATH" bash --norc --noprofile "$WORK/step5.sh" 2>&1 /dev/null +git -C "$WORK/none/sub/origin" branch -q main master # fetchable: only has_remote keeps the fetch from running +S5NR=$(cd "$WORK/none/sub" && env HOME="$WORK" CWD="$WORK/none/sub" NUMBER=14 GITHUB_REPO=o/r CLAUDE_PLUGIN_ROOT="$PLUGIN" \ + PATH="$WORK/ghok:$PATH" bash --norc --noprofile "$WORK/step5.sh" 2>&1 /dev/null)" +assert_grep "idd-all Step 3b.6 names link-base for the local mode" "link-base" "$(sed -n '/^#### Step 3b.6/,/^---/p' "$PLUGIN/skills/idd-all/SKILL.md")" + +print_summary spec-archive-local diff --git a/plugins/issue-driven-dev/scripts/tests/spec-close-gate/test.sh b/plugins/issue-driven-dev/scripts/tests/spec-close-gate/test.sh index c7f43d15..6aa646fd 100755 --- a/plugins/issue-driven-dev/scripts/tests/spec-close-gate/test.sh +++ b/plugins/issue-driven-dev/scripts/tests/spec-close-gate/test.sh @@ -73,10 +73,10 @@ awk '/^### Step 1.57: /{f=1} f && /^```bash$/{b=1; next} b && /^```$/{exit} b' " step157() { # comments-json fail(0|1) → STATUS, GRC printf '%s' "$1" > "$WORK/comments.json" local out - out=$(cd "$C" && env PATH="$WORK/bin:$PATH" FAKE_COMMENTS="$WORK/comments.json" FAKE_FAIL="$2" FAKE_VIEW_FAIL="${FAKE_VIEW_FAIL:-0}" \ + out=$(cd "$C" && env PATH="$WORK/bin:$PATH" HOME="$WORK" FAKE_COMMENTS="$WORK/comments.json" FAKE_FAIL="$2" FAKE_VIEW_FAIL="${FAKE_VIEW_FAIL:-0}" \ GITHUB_REPO=o/r NUMBER=42 CWD="$C" CLAUDE_PLUGIN_ROOT="$PLUGIN" \ - bash --norc --noprofile -c '. "$1"; printf "\nSTATUS=%s\nGRC=%s\n" "$(printf "%s" "$GATE" | python3 -c "import json,sys; print(json.loads(sys.stdin.read()).get(\"status\"))")" "$GATE_RC"' _ "$BLOCK" 2>/dev/null /dev/null "$L/.claude/.idd/local.json" +git init -q -b master "$LR"; mkdir -p "$LR/openspec/specs"; echo x > "$LR/openspec/specs/.keep" +git -C "$LR" add -A; git -C "$LR" commit -q -m base +step157_in() { # dir comments-json → STATUS, GRC, OUTALL + printf '%s' "$2" > "$WORK/comments.json" + OUTALL=$(cd "$1" && env PATH="$WORK/bin:$PATH" HOME="$WORK" FAKE_COMMENTS="$WORK/comments.json" FAKE_FAIL=0 FAKE_VIEW_FAIL="${VIEW_FAIL_IN:-1}" \ + GITHUB_REPO=o/r NUMBER=42 CWD="$1" CLAUDE_PLUGIN_ROOT="$PLUGIN" \ + bash --norc --noprofile -c '. "$1"; printf "\nSTATUS=%s\nGRC=%s\nNA=%s\n" "$(printf "%s" "$GATE" | python3 -c "import json,sys; print(json.loads(sys.stdin.read()).get(\"status\"))")" "$GATE_RC" "$(printf "%s" "$GATE" | python3 -c "import json,sys; print(\" \".join(json.loads(sys.stdin.read()).get(\"not_archived\") or []))")"' _ "$BLOCK" 2>/dev/null "$LR/openspec/changes/archive/2026-11-14-idd-42-fix-timeout/proposal.md" +git -C "$LR" add -A; git -C "$LR" commit -q -m "chore: archive idd-42-fix-timeout (#42)" +step157_in "$LR" "$IC_OK" +assert_eq "no remote, opted in, the change is archived on master → passes without the remote" pass "$STATUS" +assert_eq "… exit 0" 0 "$GRC" +assert_grep "… and prints the local branch and commit" "local ref master@$(git -C "$LR" rev-parse --short master)" "$OUTALL" +git -C "$LR" remote add origin "$WORK/o.git" +step157_in "$LR" "$IC_OK" +assert_eq "the repository has a remote and is opted into local → stops" error "$STATUS" +assert_eq "… exit 2" 2 "$GRC" +assert_grep "… and prints the reason" "remote" "$OUTALL" +git -C "$LR" remote remove origin +refute_grep "the local-mode line does not print the configuration file's path (idd-547-fix1)" ".claude/.idd/local.json" "$(cd "$LR" && git checkout -q master 2>/dev/null; step157_in "$LR" "$IC_OK"; printf '%s' "$OUTALL")" +# idd-547-fix1: HEAD on the issue's branch, where the change is archived; the configured master does not have it. +LH="$WORK/issue-repo-h"; LHR="$LH/code"; mkdir -p "$LH/.claude/.idd" "$LHR" +printf '%s\n' '{"spec_archive": {"code": {"ref": "local", "branch": "master"}}}' > "$LH/.claude/.idd/local.json" +git init -q -b master "$LHR"; mkdir -p "$LHR/openspec/specs"; echo x > "$LHR/openspec/specs/.keep" +git -C "$LHR" add -A; git -C "$LHR" commit -q -m base; git -C "$LHR" checkout -q -b idd/42-x +mkdir -p "$LHR/openspec/changes/archive/2026-11-14-idd-42-fix-timeout"; echo p > "$LHR/openspec/changes/archive/2026-11-14-idd-42-fix-timeout/proposal.md" +git -C "$LHR" add -A; git -C "$LHR" commit -q -m "chore: archive idd-42-fix-timeout (#42)" +step157_in "$LHR" "$IC_OK" +assert_eq "no remote, opted into master, HEAD on the issue's branch with the archive, master without → stops" refuse "$STATUS" +assert_eq "… naming the change (idd-547-fix4)" idd-42-fix-timeout "$NA" +# idd-547-fix1: no remote and no spec_archive entry → stops, and the reason says why archive-ref answered remote +LN="$WORK/noentry"; mkdir -p "$LN/.claude/.idd"; git init -q -b master "$LN/code" +printf '%s\n' '{"github_repo": "o/r"}' > "$LN/.claude/.idd/local.json" +mkdir -p "$LN/code/openspec/specs"; echo x > "$LN/code/openspec/specs/.keep"; git -C "$LN/code" add -A; git -C "$LN/code" commit -q -m base +step157_in "$LN/code" "$IC_OK" +assert_eq "no remote and no spec_archive entry → stops with error" error "$STATUS" +assert_eq "… exit 2" 2 "$GRC" +GATE_REASON=$(cd "$LN/code" && env PATH="$WORK/bin:$PATH" HOME="$WORK" FAKE_COMMENTS="$WORK/comments.json" FAKE_FAIL=0 FAKE_VIEW_FAIL=0 \ + GITHUB_REPO=o/r NUMBER=42 CWD="$LN/code" CLAUDE_PLUGIN_ROOT="$PLUGIN" \ + bash --norc --noprofile -c '. "$1"; printf "%s" "$GATE"' _ "$BLOCK" 2>/dev/null /dev/null "$LO/code/openspec/specs/.keep"; git -C "$LO/code" add -A; git -C "$LO/code" commit -q -m base +git clone -q --bare "$LO/code" "$LO/code/origin" 2>/dev/null +OC="$WORK/originsrc"; git clone -q "$LO/code/origin" "$OC" 2>/dev/null; git -C "$OC" checkout -q -B main +mkdir -p "$OC/openspec/changes/archive/2026-11-14-idd-42-fix-timeout"; echo p > "$OC/openspec/changes/archive/2026-11-14-idd-42-fix-timeout/proposal.md" +git -C "$OC" add -A; git -C "$OC" commit -q -m archive; git -C "$OC" push -q origin main 2>/dev/null +VIEW_FAIL_IN=0 step157_in "$LO/code" "$IC_OK" +assert_eq "no remote, no entry, a local repository named origin beside it → stops (idd-547-fix3)" error "$STATUS" +assert_eq "… and nothing was fetched" "" "$(git -C "$LO/code" rev-parse -q --verify FETCH_HEAD 2>/dev/null)" +assert_grep "… saying the repository has no remote" "this repository has no remote" "$OUTALL" + echo "spec-close-gate: the skill text" G="$(sed -n '/^### Step 1.57: /,/^### Step 1.6: /p' "$CLOSE")" line_of() { local n; n=$(grep -nF -- "$1" "$CLOSE" | head -1 | cut -d: -f1); printf '%s' "${n:-999999}"; } @@ -121,6 +196,8 @@ assert_true "Step 1.57 sits between Step 1.55 and Step 1.6" \ assert_grep "the gate calls spec-change.py archived" 'spec-change.py" archived' "$G" assert_grep "the gate fetches the default branch from the remote" 'fetch -q origin -- "$DEFAULT_BRANCH"' "$G" assert_grep "… and checks the commit that fetch returned" 'rev-parse -q --verify FETCH_HEAD' "$G" +assert_grep "the gate first asks archive-ref which branch to read (#547)" 'spec-change.py" archive-ref' "$G" +refute_grep "the new inline JSON readers run isolated: no python3 -c without -I in Step 1.57 (idd-547-fix2)" "python3 -c 'import json" "$G" assert_grep "the gate chooses the comment with spec-change.py ic (one selection, shared with verify)" 'spec-change.py" ic --comments' "$G" assert_grep "no Implementation Complete comment → not held to the gate (row 5)" "沒有 Implementation Complete" "$G" assert_grep "refuse or error stops the close" "停下、不 close" "$G" diff --git a/plugins/issue-driven-dev/skills/idd-all/SKILL.md b/plugins/issue-driven-dev/skills/idd-all/SKILL.md index 219ae69f..61344d34 100644 --- a/plugins/issue-driven-dev/skills/idd-all/SKILL.md +++ b/plugins/issue-driven-dev/skills/idd-all/SKILL.md @@ -1023,7 +1023,7 @@ fi #### Step 3b.6: Spec close-out 與 Implementation Complete(#480) -`spectra-apply` 做完之後、Phase 4 之前,照 [`references/spec-wrapper.md`](../../references/spec-wrapper.md) 第 5 步做 Spec close-out,順序固定:`spectra-verify` → `spectra archive -y` → 確認封存落在 `$CWD` → commit `chore: archive (#N)` → `spec-change.py" links --base <這條 branch 與剛 fetch 的預設 branch(FETCH_HEAD)的 merge-base>`(契約第 5 步那段) → `spec-change.py" archived --ref HEAD`。任何一步沒過:unattended 停下回報(`Stopped:`),attended 告訴使用者哪一步、等他決定。 +`spectra-apply` 做完之後、Phase 4 之前,照 [`references/spec-wrapper.md`](../../references/spec-wrapper.md) 第 5 步做 Spec close-out,順序固定:`spectra-verify` → `spectra archive -y` → 確認封存落在 `$CWD` → commit `chore: archive (#N)` → `spec-change.py" links --base <這條 branch 與剛 fetch 的預設 branch(FETCH_HEAD)的 merge-base>`(契約第 5 步那段;程式 repo 沒有 remote、用 `spec_archive` 指到本機 branch 時,起點改由 `spec-change.py link-base` 給,#547) → `spec-change.py" archived --ref HEAD`。任何一步沒過:unattended 停下回報(`Stopped:`),attended 告訴使用者哪一步、等他決定。 通過之後貼 Implementation Complete(經 `gh-egress.sh`),讓 `idd-verify` 與 `idd-close` 讀得到這張 issue 的宣告: diff --git a/plugins/issue-driven-dev/skills/idd-close/SKILL.md b/plugins/issue-driven-dev/skills/idd-close/SKILL.md index 8e1b6bc1..84d65feb 100644 --- a/plugins/issue-driven-dev/skills/idd-close/SKILL.md +++ b/plugins/issue-driven-dev/skills/idd-close/SKILL.md @@ -352,7 +352,7 @@ TaskCreate(name="check_prerequisites", description="gh issue view 確認 OPEN + TaskCreate(name="check_attachments", description="確認 closing comment 即將引用的 attachment 在 .claude/.idd/attachments/issue-NNN/ 仍存在(防止使用者搬檔造成失效引用);manifest 對照 issue 當下 attachment list,新增的不重新 fetch(那是 idd-diagnose 工作)。依 rules/process-attachments.md。") TaskCreate(name="check_open_prs", description="Step 1.5: gh pr list 找引用 #NNN 的 open PR;若有 unmerged PR → refuse close") TaskCreate(name="merge_completeness_gate", description="Step 1.55 (v2.84.0+, #184): resolve issue branch via merged-PR headRefOid (SHA — survives GitHub branch-delete-on-merge) → local idd/-* → VISIBLE skip note; bash ${CLAUDE_PLUGIN_ROOT}/scripts/check-merge-completeness.sh --branch --baseline origin/ (clean at once when the tip has the head's content; else finds the commit where the branch landed, then checks each commit's effects there in place by two-way diffs; the commits it cannot place at a landing commit are checked at the tip by line presence, #399); rc=3 orphans → AskUserQuestion 3-option (close anyway / abort+land / mis-detection), warn-only; rc=4 (could not verify — reason from the helper's stderr) / no-branch print a note (never silent). Never hard-blocks.") -TaskCreate(name="spec_archive_gate", description="Step 1.57 (#480): 最新一則由有寫入權的帳號貼的 Implementation Complete → spec-change.py archived --ref origin/;pass 才往下,refuse / error 停下、不 close;沒有 Implementation Complete 的 issue 不受這一道檢查") +TaskCreate(name="spec_archive_gate", description="Step 1.57 (#480): 最新一則由有寫入權的帳號貼的 Implementation Complete → spec-change.py archived --ref origin/(程式 repo 沒有 remote、設定 spec_archive 指到本機 branch 時改用本機 branch,#547);pass 才往下,refuse / error 停下、不 close;沒有 Implementation Complete 的 issue 不受這一道檢查") TaskCreate(name="semantic_gate_check", description="Step 1.6: 對每個 - [x] bullet 做 keyword extraction → 驗證對應 artifact 真存在/有 commit。Warn-only。") TaskCreate(name="draft_closing_comment", description="起草 closing summary:code issue(bug/feature/refactor/docs)用 Problem / Root Cause / Solution / Verification / Changes 五段式;`type=meeting` 用 decision→action mapping(每個決策 → follow-up action + owner),不用五段式、不跑 ### Verification、不要求 /idd-verify TDD pass(見上方「Meeting close」段 + Step 2 meeting 變體)") TaskCreate(name="review_with_user", description="顯示 closing comment 給使用者確認。一般 close 在使用者已明確下 /idd-close 時可省略;**--retroactive 不得省略**——那條路徑上 helper 已經交出許可權,人是唯一還在判斷的一層") @@ -412,7 +412,15 @@ Exit code: # #366: 引用本 issue 的 open PR 來自共用 helper(唯一的比對實作,契約 references/pr-issue-matching.md)。 # gate 語意不變:任何 open PR 引用本 issue(E1 宣告或 E2 只提及,自家或別人的)都擋 —— # Open PR = 「這個改動還沒進 main」,PR 路徑沒走完就不能結案。 -EW_JSON=$(bash "${CLAUDE_PLUGIN_ROOT}/scripts/check-existing-work.sh" --cwd "${WORKDIR:-$PWD}" "$GITHUB_REPO" "$NUMBER") || { +# #547: 程式 repo 沒有 remote、而且設定 spec_archive 把它指到本機 branch 時(spec-change.py archive-ref 回 local), +# 不 fetch 它、不讀它的 branch;open PR 照樣在 issue 所在的 repo 查。其他回覆(remote、error)照舊。 +EW_SKIP="" +if [ "$(python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" archive-ref --cwd "${WORKDIR:-$PWD}" 2>/dev/null \ + | python3 -I -c 'import json,sys; print(json.loads(sys.stdin.read() or "{}").get("status", ""))' 2>/dev/null)" = local ]; then + EW_SKIP="--skip-branches" + echo "→ Step 1.5: spec_archive.ref=local — this repository has no remote; branch lookup skipped, open PRs checked on $GITHUB_REPO" +fi +EW_JSON=$(bash "${CLAUDE_PLUGIN_ROOT}/scripts/check-existing-work.sh" --cwd "${WORKDIR:-$PWD}" $EW_SKIP "$GITHUB_REPO" "$NUMBER") || { echo "✗ Step 1.5: existing-work lookup produced no result — refusing to close (fail-closed; retry)" >&2; exit 1; } # 查詢 open PR 本身失敗或列表達上限時拒絕結案:close 能 reopen,但結案摘要一貼出就通知了關注者、收不回,「不知道有沒有 open PR」不能當成「沒有」。 # (比 #366 之前更嚴:以前 gh 失敗時 OPEN_PRS 為空,gate 安靜通過。) @@ -533,29 +541,56 @@ fi 這張 issue 宣告的 Spectra change,要在預設 branch 上已經封存,才能結案(capability `spec-on-every-issue`;判定在 `scripts/spec-change.py`,契約見 [`references/spec-wrapper.md`](../../references/spec-wrapper.md))。 1. 留言用 `gh api --paginate` 抓,用 `spec-change.py ic` 選出這張 issue 的 Implementation Complete:最新一則以 `## Implementation Complete` 開頭(或第一行是 ``)、作者的 `author_association` 是 `OWNER`/`MEMBER`/`COLLABORATOR` 的留言(`author_association` 是寫入權的近似:API 不給留言者的權限)。`idd-verify` 用同一個指令,兩邊選到的一定是同一則。沒有 Implementation Complete 的 issue 不受這一道限制(`ic` 印出空的內容,讀成舊制留言)。 -2. 對遠端的預設 branch 跑封存檢查。留言抓不到、或 `ic` 讀不了,一律當成 `error`,**不**當成「沒有 Implementation Complete」(verify 第 1 輪的 HIGH:舊寫法用了一個沒有任何步驟設定的變數,抓不到時讀到空檔就放行): +2. 對遠端的預設 branch 跑封存檢查。程式 repo 沒有任何 remote、而且它上面的 IDD 設定用 `spec_archive` 把它指到本機 branch 時(`spec-change.py archive-ref` 回 `local`),改查那條本機 branch,不 fetch,並印出用的是本機 ref;設定見 [`references/config-protocol.md`](../../references/config-protocol.md) 的 `spec_archive` 一節(#547)。留言抓不到、或 `ic` 讀不了,一律當成 `error`,**不**當成「沒有 Implementation Complete」(verify 第 1 輪的 HIGH:舊寫法用了一個沒有任何步驟設定的變數,抓不到時讀到空檔就放行): ```bash SPEC_JSON=$(mktemp); SPEC_IC=$(mktemp) if ! { gh api "repos/$GITHUB_REPO/issues/$NUMBER/comments" --paginate > "$SPEC_JSON" 2>/dev/null \ && python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" ic --comments "$SPEC_JSON" > "$SPEC_IC"; }; then GATE='{"status": "error", "not_archived": [], "reason": "the issue comments could not be read"}'; GATE_RC=2 -elif [ "$(python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" declared --file "$SPEC_IC" 2>/dev/null | python3 -c 'import json,sys; print(json.loads(sys.stdin.read() or "{}").get("form", ""))' 2>/dev/null)" != change ]; then +elif [ "$(python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" declared --file "$SPEC_IC" 2>/dev/null | python3 -I -c 'import json,sys; print(json.loads(sys.stdin.read() or "{}").get("form", ""))' 2>/dev/null)" != change ]; then # No change declared (no comment, legacy, an exemption, an invalid line): the default branch is not read, # so a failed fetch does not hold an issue this gate does not apply to. archived answers these from the comment. GATE=$(python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" archived --file "$SPEC_IC" --cwd "${CWD:-$PWD}" --ref HEAD); GATE_RC=$? else - # A declared change is checked against the default branch as the remote has it: no name, or a failed fetch, - # is an error, never a pass on a stale local ref (verify round 2). - DEFAULT_BRANCH=$(gh repo view "$GITHUB_REPO" --json defaultBranchRef -q .defaultBranchRef.name 2>/dev/null) || DEFAULT_BRANCH="" - # The commit the fetch returned (FETCH_HEAD), not origin/: a fetch refspec that does not cover the - # default branch leaves the tracking ref where it was (verify round 3). + # #547: which branch to read. A repository without any remote can be opted into its local branch by the + # `spec_archive` field of the IDD configuration above it (archive-ref prints local, with the commit); with no + # entry it prints remote and the default branch is read as the remote has it, as before; an entry that cannot + # be honoured (the repository has a remote, the branch does not exist, ...) is an error, never a fallback. + ARCH=$(python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" archive-ref --cwd "${CWD:-$PWD}" 2>/dev/null) + ARCH_STATUS=$(printf '%s' "$ARCH" | python3 -I -c 'import json,sys; print(json.loads(sys.stdin.read() or "{}").get("status", ""))' 2>/dev/null) SPEC_REF="" - if [ -n "$DEFAULT_BRANCH" ] && git -C "${CWD:-$PWD}" fetch -q origin -- "$DEFAULT_BRANCH" 2>/dev/null; then - SPEC_REF=$(git -C "${CWD:-$PWD}" rev-parse -q --verify FETCH_HEAD 2>/dev/null) + if [ "$ARCH_STATUS" = local ]; then + SPEC_REF=$(printf '%s' "$ARCH" | python3 -I -c 'import json,sys; print(json.loads(sys.stdin.read()).get("ref", ""))' 2>/dev/null) + printf '%s' "$ARCH" | python3 -I -c 'import json,sys; d=json.loads(sys.stdin.read()); print("→ spec archive gate: checking local ref %s@%s (this repository has no remote; spec_archive[%r])" % (d["branch"], d["ref"][:7], d["key"]))' 2>/dev/null + elif [ "$ARCH_STATUS" = remote ]; then + # A declared change is checked against the default branch as the remote has it: no name, or a failed fetch, + # is an error, never a pass on a stale local ref (verify round 2). Only when archive-ref says the repository + # has a remote: without one, git would fetch a local path named origin (#547 round 3), so it stops below. + ARCH_HAS_REMOTE=$(printf '%s' "$ARCH" | python3 -I -c 'import json,sys; print(json.loads(sys.stdin.read() or "{}").get("has_remote") is True)' 2>/dev/null) + if [ "$ARCH_HAS_REMOTE" = True ]; then + DEFAULT_BRANCH=$(gh repo view "$GITHUB_REPO" --json defaultBranchRef -q .defaultBranchRef.name 2>/dev/null) || DEFAULT_BRANCH="" + # The commit the fetch returned (FETCH_HEAD), not origin/: a fetch refspec that does not cover the + # default branch leaves the tracking ref where it was (verify round 3). + if [ -n "$DEFAULT_BRANCH" ] && git -C "${CWD:-$PWD}" fetch -q origin -- "$DEFAULT_BRANCH" 2>/dev/null; then + SPEC_REF=$(git -C "${CWD:-$PWD}" rev-parse -q --verify FETCH_HEAD 2>/dev/null) + fi + fi fi - if [ -z "$SPEC_REF" ]; then - GATE='{"status": "error", "not_archived": [], "reason": "the default branch could not be read (gh repo view or git fetch failed)"}'; GATE_RC=2 + if [ "$ARCH_STATUS" != local ] && [ "$ARCH_STATUS" != remote ]; then + GATE=$(printf '%s' "$ARCH" | python3 -I -c 'import json,sys +try: r = json.loads(sys.stdin.read()).get("reason", "") +except Exception: r = "" +print(json.dumps({"status": "error", "not_archived": [], "reason": "archive-ref: " + (r or "printed nothing readable")}))'); GATE_RC=2 + elif [ -z "$SPEC_REF" ]; then + # #547: say why archive-ref answered remote, so a repository meant for its local branch whose entry was not + # found (or whose configuration could not be read) shows the reason next to the failed fetch. + GATE=$(printf '%s' "$ARCH" | python3 -I -c 'import json,sys +try: d = json.loads(sys.stdin.read()) +except Exception: d = {} +r = d.get("reason", "") if isinstance(d, dict) else "" +why = " (gh repo view or git fetch failed)" if isinstance(d, dict) and d.get("has_remote") is True else ": this repository has no remote, so it was not fetched" +print(json.dumps({"status": "error", "not_archived": [], "reason": "the default branch could not be read" + why + ("; archive-ref: " + r if r else "")}))'); GATE_RC=2 else GATE=$(python3 "${CLAUDE_PLUGIN_ROOT}/scripts/spec-change.py" archived --file "$SPEC_IC" --cwd "${CWD:-$PWD}" --ref "$SPEC_REF"); GATE_RC=$? fi @@ -568,7 +603,7 @@ printf '%s\n' "$GATE" | jq -r '"→ spec archive gate: \(.status) \(.reason)"' |---|---| | `pass`(含 `no behaviour change`、`third-party clone`、舊制留言) | 往下 | | `refuse` | 停下、不 close:列出還沒封存的 change,或 `**Spec**:` 行的問題。常見原因是 PR 還沒合併,或封存 commit 不在合併的範圍裡 | -| `error` | 停下、不 close:留言、預設 branch 或設定讀不到,照原因處理後重跑 | +| `error` | 停下、不 close:留言、預設 branch 或設定讀不到,照原因處理後重跑。程式 repo 沒有任何 remote 而 `archive-ref` 回 `remote`(`has_remote` 為 false)時不 fetch,原因寫「this repository has no remote」:要嘛在 `spec_archive` 加這個 repo 的條目,要嘛加 remote。`archive-ref:` 開頭的原因說的是 `spec-change.py archive-ref` 為什麼這樣回答:設定的問題(例如設了本機模式的 repo 有 remote、branch 不存在、同一個路徑有兩個條目)就改設定或移除那一條;程式 repo 讀不到(不是 git repo)就在正確的目錄重跑 | | 沒有輸出、不是 JSON、或上面沒列的 status | 停下、不 close,回報印出的內容 | 這一道和 Step 1.5 一樣是硬性的:不問人、沒有略過的選項。有人在場時要結案,先把 change 封存進預設 branch,或修正 Implementation Complete 的宣告。