diff --git a/.gitignore b/.gitignore index 7b812e8..adda256 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,7 @@ TaskFlowDocs/**/sessions.md # and re-include the routing record itself. TaskFlowDocs/repository-docs/* !TaskFlowDocs/repository-docs/index.md + +# Remote Todo recovery records are clone-local and may identify private repositories. +TaskFlowDocs/.remote-todo-operations/ +TaskFlowDocs/.remote-todo-updates/ diff --git a/README.md b/README.md index ed776e8..12e3d1b 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,29 @@ flowchart TD TaskFlow guides the Agent to consider useful capabilities at each phase; it does not select a particular tool. Plan Review happens before approval and is separate from the review of the implemented result. Hooks assist where the host supports them, and their coverage differs by host; see [runtime.md](skills/taskflow/references/runtime.md) for the event map and limitations. +## Optional remote Issue Todos + +A project defaults to local `TaskFlowDocs/todo.md`. To use Issues as the Todo record, configure one or more GitHub or GitLab repositories (including self-hosted instances) and select remote mode. Run these commands from the project repository in Bash; replace the example plugin path with your installed TaskFlow location: + +```bash +TASKFLOW_PLUGIN_ROOT="/path/to/installed/TaskFlow" # replace with the installed plugin path +bash "$TASKFLOW_PLUGIN_ROOT/hooks/remote-todo" connect work github github.com owner/repo +bash "$TASKFLOW_PLUGIN_ROOT/hooks/remote-todo" connect lab gitlab git.example.org group/project +bash "$TASKFLOW_PLUGIN_ROOT/hooks/remote-todo" mode-set remote +bash "$TASKFLOW_PLUGIN_ROOT/hooks/remote-todo" list +``` + +The host's `gh` or `glab` must already be installed and authenticated for each host; credentials are not written to the project. Remote access happens only when requested. `list` retains local items and identifies incomplete sources. In remote mode, an explicit `bash "$TASKFLOW_PLUGIN_ROOT/hooks/task" intake --local` still creates one local item. + +Use `remote-todo create`, `edit`, `label`, `stage`, and `promote` to work with a selected Issue. `migrate-to-remote ` keeps the original local entry; `migrate-to-local --detach` copies content locally and disconnects an existing association without changing the Issue. `snapshot ` saves optional historical metadata: source order, Issue numbers, states and update times. Titles, bodies and labels are omitted; the metadata may still reveal activity and become stale. Source numbers refer to configuration order at export time, so retain that order separately if it may change. + +To reconnect an unpromoted local copy to its original Issue, use `reattach-todo --remote-authoritative` after reconciling differences. The local copy remains on disk and the remote Issue becomes authoritative; this does not update the Issue. + +For a detached active task, `reattach-task ` reconnects the original Issue only when its stage matches the local task and it is not linked to another task. Reconcile any differences first; earlier local copies and detach records are retained. + +Migration checks a stable Todo marker across all Issues in the selected repository before creation. Do not migrate the same Todo simultaneously from multiple worktrees or machines: the check and creation are not atomic. Editing or removing the marker outside TaskFlow can defeat later checks. + +A remote write can succeed even when its response is lost. Inspect the operation ID before retrying; `resolve`, `resolve-absent --checked`, and `ack-update --checked` are explicit recovery operations. GitHub and GitLab cannot generally guarantee atomic conditional Issue edits, so concurrent changes can still race. Local recovery files must be Git-excluded; run `bash "$TASKFLOW_PLUGIN_ROOT/hooks/install-git-hygiene"` if prompted. See [runtime details](skills/taskflow/references/runtime.md#remote-todo-backend). ## Repository documents `TaskFlowDocs/repository-docs/index.md` is the authoritative routing and check record for repository rules, repository guidance, and scoped personal rules. Repository-owned documents stay authoritative at their conventional source locations. @@ -121,7 +144,7 @@ For fork, remote, or pull-request work, TaskFlow records the configured remotes, TaskFlow never writes a task document in the base working tree: each task runs on its own short-lived branch and its own working tree, created before the first task document rather than before the first edit. `CONTRIBUTING.md` states the branch rule; the Skill states the worktree rule. -Because every task appends to the one `TaskFlowDocs/todo.md`, TaskFlow ships a Git merge driver for it, installed per clone by `hooks/session-start`. Two branches that each added an entry, or that changed different entries, merge without a human; the same entry changed differently on both sides is still left as a real conflict. Todo IDs are derived from the goal rather than from a counter, so two branches cut from the same base cannot allocate the same ID. Merging a pull request in a hosting web UI runs server-side and does not use the driver, so that path falls back to an ordinary content conflict. +Because local tasks and promoted remote tasks may share `TaskFlowDocs/todo.md`, TaskFlow ships a Git merge driver for it, installed per clone by `hooks/session-start`. Two branches that each added an entry, or that changed different entries, merge without a human; the same entry changed differently on both sides is still left as a real conflict. Todo IDs are derived from the goal rather than from a counter, so two branches cut from the same base cannot allocate the same ID. Merging a pull request in a hosting web UI runs server-side and does not use the driver, so that path falls back to an ordinary content conflict. Before creating or updating a pull request, TaskFlow reads the applicable `.github/pull_request_template.md`, satisfies every required item, records the field mapping and verification in `plan.md`, and blocks PR mutation when a required item is missing or ambiguous. @@ -131,7 +154,7 @@ Any user correction or addition to an approved task is classified before documen ## Todo intake -TaskFlow applies automatically only to repository development requests: features, fixes, refactors, tests, and configuration/build/CI changes. A release is the exception — it runs `RELEASE.md` directly on the base checkout, with no Todo item, task directory, branch, or PRD/Plan, and its record is the `CHANGELOG.md` section and the GitHub Release body. Read-only explanation, translation, status, research, review, and diagnosis do not create Todo or task documents; a later implementation request starts the workflow then. Explicitly invoking `$taskflow` opts planning or research into the workflow. For applicable requests, `TaskFlowDocs/todo.md` is the mandatory first record. An item moves from `inbox` to `clarified`, then is promoted into a task directory with `prd.md`, optional `spec.md`, and `plan.md`; it enters `in_progress` after approval and closes as `done` or `cancelled`. +TaskFlow applies automatically only to repository development requests: features, fixes, refactors, tests, and configuration/build/CI changes. A release is the exception — it runs `RELEASE.md` directly on the base checkout, with no Todo item, task directory, branch, or PRD/Plan, and its record is the `CHANGELOG.md` section and the GitHub Release body. Read-only explanation, translation, status, research, review, and diagnosis do not create Todo or task documents; a later implementation request starts the workflow then. Explicitly invoking `$taskflow` opts planning or research into the workflow. For applicable requests, record the request first in the selected Todo backend: local `TaskFlowDocs/todo.md` by default, or an explicitly configured remote Issue repository. An item moves from `inbox` to `clarified`, then is promoted into a task directory with `prd.md`, optional `spec.md`, and `plan.md`; it enters `in_progress` after approval and closes as `done` or `cancelled`. After verification, TaskFlow moves the whole task directory to `TaskFlowDocs/achieved//`, updates the Todo item's task path and status to `done`, and verifies the active path is absent. If later work belongs to that achieved deliverable, it retrieves the directory to the active root, records the Todo source and reopen reason, creates a new Task version, and returns to approval before changing implementation. Achieved tasks are read-only; their `old/vN/` history is read only when the current documents or a version summary require it. @@ -426,7 +449,7 @@ Once installed, tell your Agent: Use $taskflow to plan, execute, verify, and archive this task. ``` -Then: add ideas to `TaskFlowDocs/todo.md`; promote clarified items into `prd.md`, `spec.md` (when needed), and `plan.md`; review the task documents, record approval, then implement one planned step at a time using its checklist. +Then: add ideas to the selected local Todo or remote Issue repository; promote clarified items into `prd.md`, `spec.md` (when needed), and `plan.md`; review the task documents, record approval, then implement one planned step at a time using its checklist. > [!TIP] > A small, obvious one-file change can still be a direct change with minimal verification. TaskFlow does not create documents merely to satisfy a process. diff --git a/README.zh-CN.md b/README.zh-CN.md index 033f499..974b616 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -76,6 +76,29 @@ flowchart TD TaskFlow 引导 Agent 在每个阶段考虑有帮助的能力,但不替它选择具体工具。计划审核发生在用户批准之前,与实施完成后的成果评审分开。Hook 仅在宿主支持时辅助执行,各宿主覆盖范围不同;事件映射和限制见 [runtime.md](skills/taskflow/references/runtime.md)。 +## 可选的远端 Issue 待办 + +项目默认使用本地 `TaskFlowDocs/todo.md`。如需把 Issue 用作待办记录,可配置一个或多个 GitHub、GitLab 仓库(包括自托管实例),并选择远程模式。请在项目仓库的 Bash 中运行,并把示例路径改为实际安装的 TaskFlow 目录: + +```bash +TASKFLOW_PLUGIN_ROOT="/path/to/installed/TaskFlow" # replace with the installed plugin path +bash "$TASKFLOW_PLUGIN_ROOT/hooks/remote-todo" connect work github github.com owner/repo +bash "$TASKFLOW_PLUGIN_ROOT/hooks/remote-todo" connect lab gitlab git.example.org group/project +bash "$TASKFLOW_PLUGIN_ROOT/hooks/remote-todo" mode-set remote +bash "$TASKFLOW_PLUGIN_ROOT/hooks/remote-todo" list +``` + +所选宿主须已安装并登录对应的 `gh` 或 `glab`;项目配置不保存凭据。只有显式操作才会访问远端。`list` 会保留本地条目并提示不完整的来源;在远程默认模式下,仍可用 `bash "$TASKFLOW_PLUGIN_ROOT/hooks/task" intake --local` 显式创建单条本地待办。 + +使用 `remote-todo create`、`edit`、`label`、`stage` 和 `promote` 操作选定 Issue。`migrate-to-remote ` 保留原本地条目;`migrate-to-local --detach` 将内容复制到本地并解除已有关系,不修改远端 Issue。`snapshot ` 可保存历史元数据:来源顺序、Issue 编号、状态和更新时间;不包含标题、正文或标签。元数据仍可能透露活动情况,也可能过期。来源序号对应导出时的配置顺序;如顺序可能变动,请另行保留当时的顺序。 + +未提升的本地副本可在核对差异后使用 `reattach-todo --remote-authoritative` 重新关联原 Issue。本地副本保留,远端 Issue 成为主记录,此操作不修改远端。 + +已解除关联的活动任务可以用 `reattach-task ` 重新连接原 Issue;仅当远端阶段与本地一致且 Issue 未关联其他任务时才会连接。请先核对并解决差异;原本地副本和解除关联记录会保留。 + +迁移前会检查目标仓库全部 Issue 中的稳定 Todo 标记。不要在多个工作区或机器上同时迁移同一条 Todo:检查与创建并非原子操作。在 TaskFlow 外编辑或删除标记会削弱后续去重。 + +远端写入超时不代表一定失败,重试前须核对操作 ID;`resolve`、`resolve-absent --checked` 和 `ack-update --checked` 用于显式恢复。GitHub 和 GitLab 通常不能保证 Issue 的原子条件更新,并发修改仍可能发生竞态。本地恢复记录必须被 Git 排除,收到提示时运行 `bash "$TASKFLOW_PLUGIN_ROOT/hooks/install-git-hygiene"`。具体命令、隐私和平台限制见[运行时说明](skills/taskflow/references/runtime.md#remote-todo-backend)。 ## 仓库文档 `TaskFlowDocs/repository-docs/index.md` 是仓库规则、仓库指引和有范围个人规则的权威路由与检查记录;具体规则内容仍以惯用位置上的源文档为准。 @@ -88,7 +111,7 @@ TaskFlow 引导 Agent 在每个阶段考虑有帮助的能力,但不替它选 TaskFlow 从不在 base 工作区里写任务文档:每个任务使用独立短生命周期分支与独立工作区,且在工作区的建立是在**第一份任务文档之前**,而不是第一次改动之前。分支规则写在 `CONTRIBUTING.md`,worktree 规则写在 Skill 中。 -由于所有任务都追加同一个 `TaskFlowDocs/todo.md`,TaskFlow 为它内置了 Git 合并驱动,由 `hooks/session-start` 按克隆安装。两个分支各自新增条目、或修改不同条目时无需人工介入;同一条目两侧改法不同仍会保留为真实冲突。Todo ID 由目标内容派生而非递增计数,因此同一基点切出的两个分支不会分到同一个 ID。在托管平台网页端合并 PR 时由服务端执行,不会使用该驱动,这条路径仍退化为普通内容冲突。 +由于本地任务和已提升的远端任务可能共用 `TaskFlowDocs/todo.md`,TaskFlow 为它内置了 Git 合并驱动,由 `hooks/session-start` 按克隆安装。两个分支各自新增条目、或修改不同条目时无需人工介入;同一条目两侧改法不同仍会保留为真实冲突。Todo ID 由目标内容派生而非递增计数,因此同一基点切出的两个分支不会分到同一个 ID。在托管平台网页端合并 PR 时由服务端执行,不会使用该驱动,这条路径仍退化为普通内容冲突。 创建或更新 Pull Request 前,TaskFlow 必须读取适用的 `.github/pull_request_template.md`,完成每个必填项,在 `plan.md` 中记录字段映射和验证结果;必填项缺失或有歧义时禁止修改 PR。 @@ -98,7 +121,7 @@ TaskFlow 从不在 base 工作区里写任务文档:每个任务使用独立 ## Todo 收件箱 -TaskFlow 只自动用于仓库开发需求:功能、Bug 修复、重构、测试、配置/构建/CI 变更。**发布是例外**——它直接在 base 检出上执行 `RELEASE.md`,不创建 Todo 条目、任务目录、分支,也不写 PRD/Plan;它的记录是 `CHANGELOG.md` 段落与 GitHub Release 正文。只读解释、翻译、状态查询、研究、审查和诊断不会创建 Todo 或任务文档;若之后要求实施,再从该实施请求开始进入流程。用户显式调用 `$taskflow` 时,规划或研究也会进入流程。对于适用请求,`TaskFlowDocs/todo.md` 是强制首条记录;条目按 `inbox → clarified → promoted → in_progress → done/cancelled` 演进,提升后创建 `prd.md`、按需创建 `spec.md` 和 `plan.md`,批准后才进入实施。 +TaskFlow 只自动用于仓库开发需求:功能、Bug 修复、重构、测试、配置/构建/CI 变更。**发布是例外**——它直接在 base 检出上执行 `RELEASE.md`,不创建 Todo 条目、任务目录、分支,也不写 PRD/Plan;它的记录是 `CHANGELOG.md` 段落与 GitHub Release 正文。只读解释、翻译、状态查询、研究、审查和诊断不会创建 Todo 或任务文档;若之后要求实施,再从该实施请求开始进入流程。用户显式调用 `$taskflow` 时,规划或研究也会进入流程。对于适用请求,应先记录到所选 Todo 存储:默认是本地 `TaskFlowDocs/todo.md`,也可显式配置远端 Issue 仓库;条目按 `inbox → clarified → promoted → in_progress → done/cancelled` 演进,提升后创建 `prd.md`、按需创建 `spec.md` 和 `plan.md`,批准后才进入实施。 验证通过后,TaskFlow 将整个任务目录移入 `TaskFlowDocs/achieved//`,同步 Todo 的任务路径和 `done` 状态,并确认活动路径已不存在。已归档任务只读;其 `old/vN/` 历史仅在当前文档或版本摘要需要时才读取。之后若新工作属于该已完成交付物,先将目录取回活动根目录,记录 Todo 来源与重新打开原因,创建新的 Task version,并在修改实现前重新经过批准门禁。 @@ -376,7 +399,7 @@ dsh plugin --profile web add <仓库根目录> 使用 $taskflow 规划、执行、验证并归档这个任务。 ``` -然后把想法记录到 `TaskFlowDocs/todo.md`;澄清后的条目再提升为 `prd.md`、按需的 `spec.md` 和 `plan.md`;审阅任务文档并记录批准,再按每个步骤的 checklist 实施和记录验证结果。 +然后把想法记录到所选本地 Todo 或远端 Issue 仓库;澄清后的条目再提升为 `prd.md`、按需的 `spec.md` 和 `plan.md`;审阅任务文档并记录批准,再按每个步骤的 checklist 实施和记录验证结果。 > [!TIP] > 边界清晰的简单单文件修改可以直接完成并做最小验证,不必为了流程创建空文档。 diff --git a/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/plan.md b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/plan.md new file mode 100644 index 0000000..443b391 --- /dev/null +++ b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/plan.md @@ -0,0 +1,156 @@ +# Plan — 远程 Issues 待办 +> Task version: v1 +> Status: in_progress + +## Reference Pointers +- `TaskFlowDocs/repository-docs/index.md`、`LICENSE`:文档路由和所有阶段规则。 +- `README.md`、`README.zh-CN.md`:现有本地工作流和双语用户说明。 +- `CONTRIBUTING.md`、`CODE_STYLE.md`:任务分支、批准、CI 和 shell 规范。 +- `skills/taskflow/SKILL.md`、`skills/taskflow/references/artifacts.md`、`skills/taskflow/references/runtime.md`:插件流程、产物和宿主行为。 +- `hooks/task`、`hooks/archive`、`hooks/reopen`、`hooks/summarize-state`、`hooks/todo-check`:当前本地入口与状态消费者。 +- https://docs.github.com/en/rest/issues/issues — GitHub Issue 列表可能包含 PR,更新所需权限。 +- https://docs.gitlab.com/api/issues/ — GitLab Issue 字段与状态事件;实施前核验实际使用的接口。 + +## Related Tasks +- `TaskFlowDocs/achieved/2026-09-07-todo-intake-and-promotion/`:本地 Todo 的已有设计;当时明确排除远程跟踪器,本任务有独立交付与验收。 + +## Skills / Tools Used +- [PRD] `agent-skills:spec-driven-development` — purpose: 梳理需求;outcome: succeeded;incorporated: 按用户选择明确验收。 +- [Spec] `agent-skills:api-and-interface-design` — purpose: 界定接口;outcome: succeeded;incorporated: 平台身份与失败语义。 +- [Plan] `agent-skills:planning-and-task-breakdown` — purpose: 拆解步骤;outcome: succeeded;incorporated: 按依赖编排验证切片。 +- [Test] `agent-skills:test-driven-development` — purpose: 建立失败先行与端到端断言;outcome: succeeded;incorporated: 覆盖两平台、失败恢复、冲突和完整生命周期。 +- get-api-docs 的 chub 查询仅找到 SDK 内容;以 GitHub/GitLab 官方文档为 API 来源,实施前核验选定接口。 + +## 前置条件和设计选择 +- 改动归属插件行为:`skills/taskflow/` 与 `hooks/`,会影响使用远程模式的安装者;默认本地模式保持现有路径。 +- v1 提供项目默认模式和多仓库配置,每条记录携带本地或远端归属;只在操作时访问网络,绝不增加后台轮询。 +- 使用宿主已有认证能力;评估 `gh`、`glab` 的实例选择、输出和权限,不自动安装、不复制凭据。GitLab CLI 此环境当前未发现,缺失时要给出可执行提示;具体可用性须测试。 +- 远端写入与本地文件不是原子操作:准备恢复意图,超时或结果不明不盲目重试,核对远端稳定标识后继续;对无法确认的情况停止并保留可恢复状态。 +- GitHub 普通写请求不支持通用条件更新,用户已接受写前后核对仍有竞态;只修改指定字段并告知,不能宣称完全防止并发覆盖。GitLab 写条件支持尚未证实,同样不得假定存在。 +- 每个 Task version 有代码变更时至少一个 Git 提交,合适时增加回退点;不提交其他任务文件。 + +## Plan Review +- Verdict: sound +- Coverage: PRD 验收 1(本地默认/配置)→ Step 2;2(汇总/分页)→ Step 2;3(创建/编辑/标签)→ Step 3;4(生命周期)→ Step 4;5(故障、恢复和已接受的竞态)→ Steps 1、3、4;6(迁移)→ Step 3;7(归属/去重)→ Steps 2–3;8(快照)→ Step 2;9(平台及回归测试)→ Steps 2–5。 +- Verifiability: Step 1 比对官方文档和记录;Step 2 测试完整、部分失败与只读快照;Step 3 的服务端状态断言覆盖创建、编辑、迁移及失败;Step 4 检查远端标签和本地目录的最终状态;Step 5 逐条复核 PRD 并运行相应宿主测试。 +- Layer ownership: 远端适配器负责协议和结果;本地任务层负责关联及恢复;展示层负责选择、告知及过期状态。测试断言远端实际结果和本地状态;修改已有测试仅在接口契约确有变化时记录,不以改中间值掩盖错误。 + +## Approval +- Status: approved +- Approved by: user +- Approved at: 2026-11-16 14:51 +08:00 +- Approved version: v1 +- Approved scope: PRD / Spec / Plan + +## Steps + +### Step 1 — 确认平台能力和恢复契约 +- Goal: 核验两平台身份、分页、写入、认证和自托管接口,确定错误与恢复协议;如文档不足则标明未验证,不虚称已测。 +- Dependencies: 计划获准且官方文档可读。 +- Files: `spec.md`、`plan.md`、`reference/platform-evidence.md`。 +- Implementation checklist: + - [x] 按官方文档和可控 CLI 夹具核验接口、权限及条件更新,记录不能保证的并发窗口;未运行真实 GitLab、自托管或远端写入,用户知悉限制后批准验收。 + - [x] 定义身份格式、阶段标签、明确结果不明时的恢复与重复创建防护。 +- Acceptance: 已确认的官方接口约束、已知限制和恢复步骤可审核;无资料证实的行为标注未验证,接口实际行为由后续测试验证。 +- Verification: 引用已核查的官方文档并逐项审阅身份、阶段标签、已知竞态和操作恢复协议;接口夹具在后续实现步骤验证实际请求。 +- Rollback: 仅撤回本步骤未使用的设计记录。 +- Status: done + +### Step 2 — 本地模式与远端读取切片 +- Goal: 配置项目默认方式与多个实例,按记录归属汇总远端及保留本地记录。 +- Dependencies: Step 1。 +- Files: 新增 `hooks/remote-todo`(显式远端操作)、`hooks/test-remote-todo`;按需调整 `hooks/json-field`、`hooks/smoke-test`、`skills/taskflow/SKILL.md`。 +- Implementation checklist: + - [x] 实现显式模式选择、多仓库配置及分页读取,区分身份并排除 GitHub PR。 + - [x] 实现部分失败提示和用户选择的本地只读快照,不写入凭据。 +- Acceptance: 本地默认行为保持;两个平台的多仓库列表、错误和快照均可验证。 +- Verification: 可控接口测试、Windows 与 Unix 入口测试。 +- Rollback: 回退该切片提交,保留用户配置和已有待办。 +- Status: done + +### Step 3 — 创建、编辑与迁移切片 +- Goal: 支持用户指定目标的创建、修改、标签操作及双向按需迁移。 +- Dependencies: Steps 1–2。 +- Files: `hooks/remote-todo`、`hooks/test-remote-todo`、`hooks/task`、`skills/taskflow/references/runtime.md`。 +- Implementation checklist: + - [x] 验证目标与输入,执行新建、编辑及保留无关标签的更新。 + - [x] 执行选择式迁移、关联去重及结果不明的核对恢复;保留原始记录。 +- Acceptance: 同号、重复操作、部分成功、权限不足和并发更改都有明确结果,不静默删除或覆盖。 +- Verification: 对两平台使用模拟服务器注入超时、丢响应、冲突和部分写入;校验服务端实际状态。 +- Rollback: 回退代码提交;已创建远端 Issue 保留,由用户决定后续处理。 +- Status: done + +### Step 4 — 任务生命周期切片 +- Goal: 把远端主记录接入提升、阶段变化、完成、取消、归档和恢复。 +- Dependencies: Steps 1–3。 +- Files: `hooks/task`、`hooks/archive`、`hooks/reopen`、`hooks/summarize-state`、`hooks/test-remote-todo`。 +- Implementation checklist: + - [x] 以稳定关联路由本地和远端条目,阶段标签变更保留其他标签。 + - [x] 覆盖完成、取消、重开和跨系统中断,远端失败保留本地恢复信息。 +- Acceptance: 可从远端 Issue 完整走通任务生命周期;错误时不丢失任务目录或错误报告完成。 +- Verification: 两平台端到端夹具及本地模式回归。 +- Rollback: 回退该切片;保留任务目录、Issue 和关联记录供恢复。 +- Status: done + +### Step 5 — 集成与验收 +- Goal: 更新全部宿主说明并验证完整功能。 +- Dependencies: Steps 1–4。 +- Files: `skills/taskflow/SKILL.md`、`skills/taskflow/references/runtime.md`、`README.md`、`README.zh-CN.md`、`.github/workflows/hooks.yml`。 +- Implementation checklist: + - [x] 更新双语文档和主机入口,说明权限、快照隐私、恢复和并发限制。 + - [ ] 覆盖 PRD 每项验收,审阅安全边界及改动,记录真实服务未验证之处。 + - [x] 运行必要的本地检查和 CI 相关测试,提交代码回退点。 +- Acceptance: 文档与实现一致,两平台和本地模式回归通过,未验证内容准确披露。 +- Verification: 针对性测试、`git diff --check`、skill 校验及适用的跨平台 CI。 +- Rollback: 按切片提交回退代码;不自动删除用户数据或远端 Issue。 +- Status: in_progress + +## Checkpoints +- Steps 1–2 后检查身份、平台差异和读取端到端结果。 +- Steps 3–4 后检查失败恢复、去重和完整生命周期。 + +## Verification / Review +- 已比较活动任务、已有 Todo 和历史成果;无覆盖本任务的活动任务。 +- 仓库无 `.codegraph/`;现有本地 Todo 调用路径直接写在任务和归档脚本中。 +- 工作区 `.worktrees/capability-outcomes`,分支 `feature/remote-issue-todos`,v1 基线为分支创建时 HEAD。 +- 已实现配置校验、模拟远程读取、可选快照、Issue 创建及核对恢复;对应聚焦脚本红绿测试通过。真实 GitHub 公共仓库只读列表通过:1 条 Issue、141 个 Pull Request 被过滤;GitLab 和远端写入尚未真实验证。 +- 已提交 `0e98b7a`、`0025359`、`f4a6189`、`e78badf`、`c854332` 作为阶段回退点;这些提交未包含另一条 Todo。 +- 同仓库创建互斥与已知未决记录阻断已通过模拟并发测试;异常进程遗留互斥目录的用户恢复和真实平台行为仍未验证。 +- 已实现远端标题/正文编辑、标签与阶段变更、任务提升、双向复制/显式解除关联及混合列表去重;主要写入路径仍以模拟接口验证,完整验收尚未完成。 +- 远端恢复目录需要 Git 本地排除,未排除或包含已追踪文件时拒绝写入;测试覆盖固定探针绕过与强制追踪。 +- GitHub 缺失阶段标签的创建、任务状态、归档/恢复、迁移与解除关联的定向夹具通过;真实 GitHub 写入、自托管实例和真实 GitLab 尚未验证。 +- 2026-11-16 Windows 完整测试首次发现 `complete --root` 调用 `archive` 丢失根目录;已修复并在重跑中通过生命周期段。Windows 全量测试最终通过;Bash 全量在 Git Bash 上运行到独立复制 Bash 的原有测试时因缺少 Windows DLL 中止(该套件不在 Windows CI 运行)。Windows 运行时部分脚本使用系统临时目录,未遵守项目内临时目录边界;没有删除项目外文件,后续测试显式设置项目内临时目录。 +- `quick_validate.py skills/taskflow` 使用现有 torch Python 环境通过;`git diff --check` 通过。 +- 已解除关联的任务可在远端与本地阶段一致且未被其他任务占用时显式重新连接,保留历史;再次解除关联时保留旧副本并重新获取。GitHub/GitLab 夹具验证了冲突及恢复同步。未提升的远端本地副本可以明确选择远端为准并重新关联原 Issue;保留本地副本,禁止误用迁移在原仓库创建重复 Issue。用户选择稳定标记和迁移前全量检查;两平台重复标记及读取失败夹具通过。检查与创建非原子操作,同一条 Todo 不得在多台机器或 worktree 中同时迁移;用户手动移除标记也会削弱检查。 +- 2026-11-16:用户同意项目级默认、条目级归属、显式迁移和可选只读快照;用户已审阅需求方向;随后明确同意实施计划。 + +- Windows 完整回归 `windows-full-05` 通过,包括开放标准输入测试;之后对只读关联记录语义、孤立记录、跨仓库迁移与历史去重的调整分别运行 Windows 启动器定向测试通过,未重跑整个套件。 +- Linux/macOS 完整回归未在本机运行:Git Bash 执行 Bash 全量测试时,仓库原有的隔离 Bash 夹具缺少 Windows DLL 而中止;不可把它当作 Linux/macOS 通过。真实 GitHub 写入、自托管服务和真实 GitLab 仍未验证。 +- 用户在知悉真实 GitLab、自托管和远端写入未实测的情况下批准本次验收;这些真实平台验证保留为后续工作。测试夹具留在任务参考目录,另一条独立 Todo 保留在未提交工作区。 + +- PR #143 的首轮 Hooks CI:Ubuntu 在 hooks 清单检查失败,原因是两份清单遗漏新增脚本;已在 `hooks/README.md` 和 `runtime.md` 列出实际脚本,并修正网络命令说明。与 CI 相同的清单检查和 `git diff --check` 在项目内通过;修复后的完整 CI 待重跑。 + +- PR #143 第二轮 macOS 回归在中文标签 URL 编码失败;怀疑 Bash 3.2 的字节符号扩展。编码时显式限制为无符号 8 位,Windows Git Bash 的中文标签回归通过;macOS 结果以重跑 CI 为准。 + +- PR #143 的 Hooks CI 在代码提交 `4e1b09fb59b95e984b116552e7ad9e507db308a9` 全部通过,包括 Ubuntu、macOS、Windows、评估、发布和 Todo 合并检查:[CI run 43848406813](https://github.com/hkwuks/TaskFlow/actions/runs/43848406813)。此前两次失败的清单及 macOS 中文标签问题均已修复。真实 GitLab、自托管及远端写入仍未实测。 + +- 用户于 2026-11-16 批准本任务验收;此批准不表示已运行真实 GitLab、自托管或远端写入测试。PRD 逐项审阅:配置/本地默认对应配置与 intake 夹具;多源分页和 PR 过滤对应列表夹具及 GitHub 只读验证;创建、编辑、标签、阶段和恢复对应两平台 CLI 夹具;迁移、去重、快照和任务生命周期对应各自回归及三平台 Hooks CI。真实平台验证边界列为后续工作,不伪称通过。 + +- 归档前复核发现 `snapshot` 原样写入远端 Issue 内容,若内容含凭据则违反 PRD 的快照凭据要求。先撤回未提交的归档;用户批准验收基于此前披露的限制,不应误认为已接受该新发现。需确定快照隐私方案并修复后再完成验收。 + +## PR Template Mapping +- Summary:GitHub / GitLab Issues 作为可选 Todo 主记录,覆盖多仓库、状态标签、迁移和恢复。 +- TaskFlow traceability:本任务 `TaskFlowDocs/2026-11-16-remote-issue-todos/`;范围为远端适配器、任务生命周期、双语文档及回归测试;目标 `hkwuks/TaskFlow:main`。 +- Verification:`git diff --check origin/main...HEAD` 和 skill 校验通过;Windows 完整测试通过后有额外代码提交;PR Hooks CI 已在代码提交 `4e1b09f` 通过 Ubuntu、macOS、Windows。Git Bash 本机测试不单独构成 Linux/macOS 验收。 +- Review boundaries:不提交恢复数据、测试夹具或其他 Todo;不含凭据;真实 GitLab、自托管与远程写入未验证,远程创建及并发编辑存在已披露竞态。 +- PR 状态:用户已在知悉真实服务测试限制后批准验收;归档前新发现快照凭据风险,修复前保持草稿。 + +## Change Log +- 2026-11-16:记录用户决定、起草规格,并基于任务拆解技能编写计划。 +- 2026-11-16:核验官方接口,发现 GitHub 常规写请求不能依赖通用条件更新;用户接受仅更新指定字段、写前后核对并提示剩余竞态。 +- 2026-11-16:用户明确同意实施计划;手工记录批准,因为 `hooks/task` 每次运行会删除其临时目录,而当前用户规则禁止未经许可删除文件。 +- 2026-11-16:实现并提交配置、模拟读取、快照、创建恢复、编辑、标签、阶段同步和 Git 私有记录保护;全部是阶段成果,不代表完整功能已完成。 + +## Follow-ups +- 实际服务测试资源尚未确定;继续验证真实 GitLab、自托管及远端写入,不把模拟结果说成真实宿主验证。 +- 2026-11-16:用户选择跨工作区稳定标记与迁移前核对,明确不得并发迁移同一条;GitHub/GitLab 重复标记、读取失败、正文编辑保留标记及 Windows 启动器定向测试通过。 diff --git a/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/prd.md b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/prd.md new file mode 100644 index 0000000..f6b62a8 --- /dev/null +++ b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/prd.md @@ -0,0 +1,47 @@ +# 远程 Issues 待办 +> Task version: v1 +> Status: in_progress + +## 目标 +让项目可选择使用 GitHub 或 GitLab Issues 作为待办主记录,并保留 TaskFlow 任务的本地执行关联。 + +## Task Kind +- Kind: capability + +## 已确认事实和用户决定 +- 每个项目显式选择本地或远程模式;默认保持本地模式。 +- 首版支持 GitHub 和 GitLab 官方云端与自托管实例;一个项目可连接多个 Issue 仓库。 +- 查看时汇总已连接仓库的待办;创建时由用户选择目标。Issue 是远程模式的待办主记录,本地只保留任务执行所需关联。 +- 支持创建、编辑 Issue,更新状态和标签。操作时读取远端,以远端为准,不做后台轮询。 +- 用户接受 GitHub 等不提供写入条件保护时的剩余竞态:仅更新指定字段、写前后核对并明确提示,不能保证发现或阻止所有并发覆盖。 +- TaskFlow 阶段用标签表示;完成或取消时关闭 Issue,保留其他标签。 +- 项目仓库配置默认模式,单条 Todo 保留实际存储归属;切换默认模式不自动迁移现有条目。 +- 用户可选择保留本地只读快照,标明时间及可能过期;本地关联始终保留,快照不能离线修改后自动覆盖远端。 +- 本地迁移到远端时保留原条目并关联去重;远端切回本地时由用户选择复制并解除关联,不擅自关闭或删除 Issue。 +- 远端不可用或权限不足时,远端更新操作明确失败,保留本地数据,恢复后由用户重试。 +- 旧本地 Todo 原文件保留;用户按需选择条目迁移,不自动删除。连接信息保存在项目中,复用宿主已有登录,凭据不写入仓库。 + +## 需求与验收标准 +- 本地模式保持现有工作流;明确选择远程模式后,可配置多个不同平台及自托管实例的仓库,凭据不进入项目文件、任务文档或日志。 +- 汇总列表可区分不同实例、仓库和 Issue;排除 GitHub Pull Request,分页结果完整,部分仓库失败时显式报告不完整。 +- 用户可选目标创建 Issue、编辑内容及标签、查看最新状态;不覆盖其他用户的无关标签或未修改字段。 +- Issue 可进入 TaskFlow 任务生命周期,关联足以定位远端 Issue 和本地任务;TaskFlow 阶段标签与关闭、恢复行为可追溯。 +- 本地与远端之间的失败、超时和结果不明均明确报告并可恢复;不确定结果不得盲目重试创建。并发修改尽可能识别并报告,但不声称能捕获读写窗口内的所有覆盖。 +- 可选择已有本地 Todo 迁移到指定仓库;保留原文件并防止重复创建;已有关联和历史不丢失。 +- 默认模式切换后,可识别各条现有待办的归属;已关联或迁移的本地和远程条目在汇总中去重。 +- 可选快照只读、可选择关闭并明确过期状态;快照中不得记录凭据。 +- 对 GitHub 与 GitLab 的各项操作、鉴权失败、分页、网络故障、并发修改及生命周期和迁移进行可重复验证;说明真实服务测试覆盖范围。 + +## External Facts +- Depends on external facts: yes +- Source: https://docs.github.com/en/rest/issues/issues +- What it settled: GitHub Issues 接口可能返回 Pull Request,需按 pull_request 字段排除;写操作需要相应权限。 +- Source: https://docs.gitlab.com/api/issues/ +- What it settled: GitLab 提供 Issue 创建、更新、标签及 state_event 状态操作;具体请求及权限需以选用的接口和实例版本核验。 + +## 范围之外 +- 后台轮询、自动离线队列和删除 Issue。 + +## 风险与待定事项 +- 本地文件与远端 API 无法组成单一数据库事务;需要定义部分成功和结果不明时的可恢复步骤。 +- 已知限制:平台不提供原子条件更新时,读后写窗口仍可能覆盖并发改动;远端删除或转移须明确报告并保留关联,不自动认定完成。 \ No newline at end of file diff --git a/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/spec.md b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/spec.md new file mode 100644 index 0000000..f5080c6 --- /dev/null +++ b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/spec.md @@ -0,0 +1,74 @@ +# Spec — 远程 Issues 待办 +> Task version: v1 +> Status: in_progress + +## 目标与成功标准 +实现 PRD 所列 GitHub、GitLab 多仓库 Issue 待办及 TaskFlow 生命周期;远端是待办主记录,本地保存执行关联和用户选择的只读快照。 + +## 架构边界与职责 +- 用户操作层选择模式、仓库、Issue 和明确请求的操作;不在后台触发同步。 +- 远程访问层负责各平台实例鉴权、分页、Issue 标识、读写与错误分类;不得输出或持久化凭据。 +- TaskFlow 生命周期层管理本地任务及其关联,不把远端内容复制成第二份待办主记录。 +- 存储和恢复边界需明确项目共享连接配置、本地关联以及跨本地/远程部分成功后的重试。 + +## 受影响范围 +- `skills/taskflow/` 与 `hooks/`:插件用户的入口、生命周期和行为,而非仅修改本仓库治理规则。 +- `TaskFlowDocs/todo.md` 的读取、提升、归档、恢复、冲突及检查路径;两份 README、运行时文档和相关测试。 +- `hooks/task`、`hooks/archive`、`hooks/reopen`、`hooks/summarize-state`、`hooks/todo-check` 等依赖的具体调整,待设计核查后确定。 + +## 接口、数据流与契约 +- 项目配置记录默认模式及多个平台、实例和仓库标识;每条记录保留实际归属,绝不包含访问令牌。 +- 切换默认模式只改变新建条目的默认位置;已有条目继续按原位置处理,用户可选择条目逐条迁移。 +- 用户可选择保留带抓取时间的本地只读快照;离线时标为可能过期,不允许把快照当作可编辑主记录。 +- 唯一身份至少包含平台、实例、仓库和 Issue 标识;不能把不同仓库的相同编号视为同一项。 +- 查询汇总保留来源,并区分完整结果与部分失败;GitHub 列表排除 Pull Request。 +- 创建由用户指定目标;编辑基于新读取的远端状态,仅修改用户指定字段;处理并发修改,保留非 TaskFlow 标签。 +- 阶段以专用标签表达;完成或取消关闭 Issue。具体标签命名、状态映射、冲突检测与恢复协议待设计。 +- 旧本地条目只在用户选择时迁移,保留原文件并关联去重。远端迁回本地时用户明确选择复制及解除关联,远端 Issue 不自动关闭或删除。 +- 本地任务与远端 Issue 分属两套存储;先持久化恢复意图和操作标识,再发起远端变更,保存已确认结果后更新本地关联。结果不明时停止自动重试并引导查询核对;不能凭网络失败断定远端未执行。 +- 更新时先重新读取远端;当平台支持安全的条件更新时使用条件更新,否则明确并发窗口,检测到冲突或不能确认结果时停止并要求核对,不以先读后写宣称完全无竞态。 + +## 不变量、错误和恢复 +- 默认本地模式;远程更新失败不得伪报成功或丢失本地数据。 +- 远端成功但响应丢失、本地写入失败、读取权限不足、远端变更和重复重试均须有明确处理和验证,不得假定本地和远端可原子提交。 +- 不记录凭据;Issue 内容和自托管地址按用户授权范围处理。 + +## 测试约束 +- 两平台各覆盖列表分页、创建、更新、标签、状态、权限和失败恢复;覆盖多实例及同号 Issue、迁移重复执行、已有本地模式、归档与恢复。 +- 使用可控测试服务或隔离夹具验证网络故障和并发;真实托管服务验证须明确测试资源和授权,不能以模拟替代已完成的真实测试声明。 + +## 开放设计问题 +- 在平台约束下完成阶段标签和恢复协议的细化;无条件更新时按用户接受的竞态限制实施。 +- 认证工具选择及未安装或不支持实例时的提示;主机支持范围与依赖约束。 +- 远端 Issue 被第三方关闭、删除或转移后的细节和平台能力差异。 +## 已核实的接口约束与待审方案 +- 使用现有宿主的 `gh api` 和 `glab api` 作为候选适配器;按配置的主机明确寻址,使用标准输入传递请求体,不读取或保存用户令牌。缺少 CLI、对应主机登录或权限时明确报告。 +- GitHub 的 Issue 列表含 Pull Request,必须过滤;GitLab 使用 URL 编码项目路径及项目内 IID。多仓库结果包含完整来源,某个来源失败时必须明确标示不完整。 +- GitLab 的 `add_labels` 在标签不存在时可能创建项目标签,建立阶段标签时必须把这种副作用和所需权限纳入设计;用户未请求的其他标签不得用整表替换方式清除。 +- GitHub 普通写入不支持通用条件请求;预读取和事后核对不能保证阻止两个客户端竞态。用户已接受这一限制,操作提示须明确说明;GitLab 的条件更新能力尚未得到证实。 +- 对创建结果不明的操作,持久记录非敏感操作标识及目标,停止自动重试;只有可靠核对后才关联已有 Issue 或由用户决定下一步,不能简单以标题匹配判定唯一。 +- 缓存和恢复记录可能包含私有项目信息;默认不生成内容快照,用户显式启用时说明保存位置、访问边界、更新时间和不完整状态,凭据不入文件。 +## 操作约定 +- 显式操作命令使用项目本地配置与结构化输入;不信任远程字段、转义序列或可执行文本,不执行从 Issue 或配置中读取的命令。GitHub 与 GitLab 适配器只接受校验过的平台、主机和仓库参数。 +- 本地模式继续使用现有任务接口;远程模式对已提升任务保存可稳定定位 Issue 的关联。模式切换只影响新建默认值,查询汇总本地遗留项和已连接远端并按关联去重。 +- 操作恢复记录保存在不含凭据的项目工作区私有位置;意图与每次远端写入结果分别记录。创建结果未知时不自动重复写入,显式查询和用户确认后恢复;查询不完整时保持未知。 +- 每一阶段映射为单一 TaskFlow 专用标签;以增删指定标签的方式更新,保留其他标签。缺失标签的创建及权限要求在操作前提示,更新失败时不假报完成。 +- 用户选择生成的只读快照默认不生成;当网络失败时仅作为有时间标记的历史信息展示,且不能作为写入依据。快照及本地恢复材料可能包含私有 Issue 信息,写入之前说明位置和共享风险。 +- 对于远端不存在、无权限或已转移,保留本地任务和关联并报告无法确认;不存在与无权限不可由单次 404 安全区分。 +- 手工验证基线:`git diff --check`;核心行为由 `bash hooks/smoke-test`、新增聚焦测试和 `powershell -File hooks/smoke-test-windows.ps1` 的相应范围验证;CI 验证 Ubuntu、macOS、Windows。 +- 测试文件与入口按现有 `hooks/test-*` 和 `hooks/smoke-test` 约定;插件 hooks 的 Bash 3.2 与无语言运行时约束见 `skills/taskflow/references/runtime.md`。 +## 持久数据和恢复协议 +- 项目配置使用 `TaskFlowDocs/remote-todos.conf`:首行声明 `local` 或 `remote` 默认模式,其余行声明每个连接的唯一别名、平台、HTTPS 主机和仓库路径;禁止换行、控制字符、URL 用户信息及查询参数,拒绝重复别名。模式缺省时视为本地;连接变更不迁移条目。 +- Issue 的稳定身份由平台、规范化主机、平台项目标识和 Issue 编号组成;展示别名不作为唯一身份。GitLab 的项目路径按 API 规定 URL 编码,GitHub 仓库按 owner/repo 校验。 +- 只为已提升或显式迁移的条目在本地保留关联;远端未提升 Issue 不生成本地 Todo 正文。关联中保留远端身份和任务路径,不把远端内容设为本地权威。历史本地条目原文保留,通过关联去重。 +- 远端创建前使用操作唯一 ID 持久化意图;在新建 Issue 描述附加可识别的 TaskFlow 操作标记。确认创建成功后持久化平台身份;超时等结果不明时列出全部可能目标并按标记核对,查询不完整、没有匹配或多个匹配时停止并让用户核对,不凭标题或空结果自动重复创建。 +- 生命周期标签固定为 `taskflow:inbox`、`taskflow:clarified`、`taskflow:promoted`、`taskflow:planning`、`taskflow:ready`、`taskflow:in_progress`、`taskflow:checking`、`taskflow:blocked`、`taskflow:done`、`taskflow:cancelled`,更新时增删 TaskFlow 阶段标签,不替换其他标签。完成与取消均关闭 Issue,重新打开任务时重新打开 Issue。缺失标签创建行为和权限必须明确告知;GitHub 与 GitLab 分别使用其原生标签接口。 +- 可选只读快照和恢复意图不能写入诊断日志,不包含凭据。快照开启及位置由用户选择;默认关闭。故障导致关联更新未完成时不标记整体成功,保留已知远端身份以便恢复。 +- `gh` / `glab` 作为用户显式选择远程模式的外部工具;本地模式和已有 hooks 不新增语言运行时依赖。复用或扩展仓库 JSON 处理能力时必须覆盖嵌套数组、Unicode、特殊字符及恶意输入。 +## 跨工作区迁移防重 +- 用户选择稳定 Todo 标记:创建前分页检查目标仓库所有 Issue,出现匹配或检查不完整时停止;不同工作区和机器不得同时迁移同一条 Todo,因为检查与创建不是原子操作。 +- TaskFlow 正文编辑保留稳定标记;用户或其他客户端删除标记会削弱后续检查。标记匹配只提示人工核对,不自动合并记录。 + +## 切换回原 Issue +- 已解除关联的活动任务仅在原 Issue 未被其他任务关联、远端阶段与本地任务一致时重新连接;否则保留现状并先人工协调。此前解除关联记录和本地快照保留,再次切回本地时重新获取快照。 +- 未提升的远端本地副本可显式选择远端为准并重新关联原 Issue;此操作只读取远端并保留原本地内容。普通迁移不会将该副本再次创建到原仓库;切换到其他仓库仍可按正常迁移处理。 diff --git a/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/version.md b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/version.md new file mode 100644 index 0000000..6aba4aa --- /dev/null +++ b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/old/v1/version.md @@ -0,0 +1,18 @@ +# Archived Version v1 — Plan — 远程 Issues 待办 +> Task: 2026-11-16-remote-issue-todos +> Version: v1 +> Status: superseded +> Archived: 2026-11-16 18:50 +0800 +> Superseded by: v2 +> Archive mode: file + +## Change Summary +v2 将可选快照限定为来源序号、Issue 编号、状态和更新时间,排除所有远端自由文本,以满足凭据隐私要求;其他 v1 需求保留。 + +## Archived Materials +- prd.md +- spec.md +- plan.md + +## Restore +Read the archived v1 core docs from a new temporary restore root. Do not overwrite the current task root. diff --git a/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/plan.md b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/plan.md new file mode 100644 index 0000000..68524c5 --- /dev/null +++ b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/plan.md @@ -0,0 +1,186 @@ +# Plan — 远程 Issues 待办 +> Task version: v2 +> Status: completed + +## Reference Pointers +- `TaskFlowDocs/repository-docs/index.md`、`LICENSE`:文档路由和所有阶段规则。 +- `README.md`、`README.zh-CN.md`:现有本地工作流和双语用户说明。 +- `CONTRIBUTING.md`、`CODE_STYLE.md`:任务分支、批准、CI 和 shell 规范。 +- `skills/taskflow/SKILL.md`、`skills/taskflow/references/artifacts.md`、`skills/taskflow/references/runtime.md`:插件流程、产物和宿主行为。 +- `hooks/task`、`hooks/archive`、`hooks/reopen`、`hooks/summarize-state`、`hooks/todo-check`:当前本地入口与状态消费者。 +- https://docs.github.com/en/rest/issues/issues — GitHub Issue 列表可能包含 PR,更新所需权限。 +- https://docs.gitlab.com/api/issues/ — GitLab Issue 字段与状态事件;实施前核验实际使用的接口。 + +## Related Tasks +- `TaskFlowDocs/achieved/2026-09-07-todo-intake-and-promotion/`:本地 Todo 的已有设计;当时明确排除远程跟踪器,本任务有独立交付与验收。 + +## Skills / Tools Used +- [PRD] `agent-skills:spec-driven-development` — purpose: 梳理需求;outcome: succeeded;incorporated: 按用户选择明确验收。 +- [Spec] `agent-skills:api-and-interface-design` — purpose: 界定接口;outcome: succeeded;incorporated: 平台身份与失败语义。 +- [Plan] `agent-skills:planning-and-task-breakdown` — purpose: 拆解步骤;outcome: succeeded;incorporated: 按依赖编排验证切片。 +- [Security] `agent-skills:security-and-hardening` — purpose: 限制快照信任边界;outcome: succeeded;incorporated: 允许列表和拒绝不完整数据。 +- [Test] `agent-skills:test-driven-development` — purpose: 建立失败先行与端到端断言;outcome: succeeded;incorporated: 覆盖两平台、失败恢复、冲突和完整生命周期。 +- get-api-docs 的 chub 查询仅找到 SDK 内容;以 GitHub/GitLab 官方文档为 API 来源,实施前核验选定接口。 + +- [PRD] Declared `agent-skills:spec-driven-development` — v2 字段范围与验收。 +- [Spec] Declared `agent-skills:api-and-interface-design` — 快照输出和失败契约。 +- [Plan] Declared `agent-skills:planning-and-task-breakdown` — v2 测试、实现和回退步骤。 + +- [Test] Declared `agent-skills:test-driven-development` — 先验证快照泄漏,再验证字段允许列表。 + +## 前置条件和设计选择 +- 改动归属插件行为:`skills/taskflow/` 与 `hooks/`,会影响使用远程模式的安装者;默认本地模式保持现有路径。 +- v1 提供项目默认模式和多仓库配置,每条记录携带本地或远端归属;只在操作时访问网络,绝不增加后台轮询。 +- 使用宿主已有认证能力;评估 `gh`、`glab` 的实例选择、输出和权限,不自动安装、不复制凭据。GitLab CLI 此环境当前未发现,缺失时要给出可执行提示;具体可用性须测试。 +- 远端写入与本地文件不是原子操作:准备恢复意图,超时或结果不明不盲目重试,核对远端稳定标识后继续;对无法确认的情况停止并保留可恢复状态。 +- GitHub 普通写请求不支持通用条件更新,用户已接受写前后核对仍有竞态;只修改指定字段并告知,不能宣称完全防止并发覆盖。GitLab 写条件支持尚未证实,同样不得假定存在。 +- 每个 Task version 有代码变更时至少一个 Git 提交,合适时增加回退点;不提交其他任务文件。 + +## Plan Review +- Verdict: sound +- v2 审阅:新增 Step 6 逐项覆盖快照字段、泄漏、格式校验和失败不落盘;其余 v1 验收及范围保留。测试观察最终快照内容而非仅检查流程标记。 +- Coverage: PRD 验收 1(本地默认/配置)→ Step 2;2(汇总/分页)→ Step 2;3(创建/编辑/标签)→ Step 3;4(生命周期)→ Step 4;5(故障、恢复和已接受的竞态)→ Steps 1、3、4;6(迁移)→ Step 3;7(归属/去重)→ Steps 2–3;8(快照)→ Steps 2、6;9(平台及回归测试)→ Steps 2–5。 +- Verifiability: Step 1 比对官方文档和记录;Step 2 测试完整、部分失败与只读快照;Step 6 以含伪凭据的真实输出检查允许字段及失败不落盘;Step 3 的可控 CLI 夹具覆盖创建、编辑、迁移及失败,真实服务测试边界单列;Step 4 检查远端标签和本地目录的最终状态;Step 5 逐条复核 PRD 并运行相应宿主测试。 +- Layer ownership: 远端适配器负责协议和结果;本地任务层负责关联及恢复;展示层负责选择、告知及过期状态。测试断言远端实际结果和本地状态;修改已有测试仅在接口契约确有变化时记录,不以改中间值掩盖错误。 + +## Approval +- Status: approved +- Approved by: user +- Approved at: 2026-11-16 18:53 +0800 +- Approved version: v2 +- Approved scope: PRD / Spec / Plan + +## Steps + +### Step 1 — 确认平台能力和恢复契约 +- Goal: 核验两平台身份、分页、写入、认证和自托管接口,确定错误与恢复协议;如文档不足则标明未验证,不虚称已测。 +- Dependencies: 计划获准且官方文档可读。 +- Files: `spec.md`、`plan.md`、`reference/platform-evidence.md`。 +- Implementation checklist: + - [x] 按官方文档和可控 CLI 夹具核验接口、权限及条件更新,记录不能保证的并发窗口;未运行真实 GitLab、自托管或远端写入,用户知悉限制后批准验收。 + - [x] 定义身份格式、阶段标签、明确结果不明时的恢复与重复创建防护。 +- Acceptance: 已确认的官方接口约束、已知限制和恢复步骤可审核;无资料证实的行为标注未验证,接口实际行为由后续测试验证。 +- Verification: 引用已核查的官方文档并逐项审阅身份、阶段标签、已知竞态和操作恢复协议;接口夹具在后续实现步骤验证实际请求。 +- Rollback: 仅撤回本步骤未使用的设计记录。 +- Status: done + +### Step 2 — 本地模式与远端读取切片 +- Goal: 配置项目默认方式与多个实例,按记录归属汇总远端及保留本地记录。 +- Dependencies: Step 1。 +- Files: 新增 `hooks/remote-todo`(显式远端操作)、`hooks/test-remote-todo`;按需调整 `hooks/json-field`、`hooks/smoke-test`、`skills/taskflow/SKILL.md`。 +- Implementation checklist: + - [x] 实现显式模式选择、多仓库配置及分页读取,区分身份并排除 GitHub PR。 + - [x] 实现部分失败提示和用户选择的本地只读快照,不写入凭据。 +- Acceptance: 本地默认行为保持;两个平台的多仓库列表、错误和快照均可验证。 +- Verification: 可控接口测试、Windows 与 Unix 入口测试。 +- Rollback: 回退该切片提交,保留用户配置和已有待办。 +- Status: done + +### Step 3 — 创建、编辑与迁移切片 +- Goal: 支持用户指定目标的创建、修改、标签操作及双向按需迁移。 +- Dependencies: Steps 1–2。 +- Files: `hooks/remote-todo`、`hooks/test-remote-todo`、`hooks/task`、`skills/taskflow/references/runtime.md`。 +- Implementation checklist: + - [x] 验证目标与输入,执行新建、编辑及保留无关标签的更新。 + - [x] 执行选择式迁移、关联去重及结果不明的核对恢复;保留原始记录。 +- Acceptance: 同号、重复操作、部分成功、权限不足和并发更改都有明确结果,不静默删除或覆盖。 +- Verification: 对两平台使用可控 CLI 夹具注入超时、丢响应、冲突和部分写入;核对夹具记录与本地状态,不把夹具结果称为真实服务状态。 +- Rollback: 回退代码提交;已创建远端 Issue 保留,由用户决定后续处理。 +- Status: done + +### Step 4 — 任务生命周期切片 +- Goal: 把远端主记录接入提升、阶段变化、完成、取消、归档和恢复。 +- Dependencies: Steps 1–3。 +- Files: `hooks/task`、`hooks/archive`、`hooks/reopen`、`hooks/summarize-state`、`hooks/test-remote-todo`。 +- Implementation checklist: + - [x] 以稳定关联路由本地和远端条目,阶段标签变更保留其他标签。 + - [x] 覆盖完成、取消、重开和跨系统中断,远端失败保留本地恢复信息。 +- Acceptance: 可从远端 Issue 完整走通任务生命周期;错误时不丢失任务目录或错误报告完成。 +- Verification: 两平台端到端夹具及本地模式回归。 +- Rollback: 回退该切片;保留任务目录、Issue 和关联记录供恢复。 +- Status: done + +### Step 5 — 集成与验收 +- Goal: 更新全部宿主说明并验证完整功能。 +- Dependencies: Steps 1–4。 +- Files: `skills/taskflow/SKILL.md`、`skills/taskflow/references/runtime.md`、`README.md`、`README.zh-CN.md`、`.github/workflows/hooks.yml`。 +- Implementation checklist: + - [x] 更新双语文档和主机入口,说明权限、快照隐私、恢复和并发限制。 + - [x] 覆盖 PRD 每项验收,审阅安全边界及改动,记录真实服务未验证之处。 + - [x] 运行必要的本地检查和 CI 相关测试,提交代码回退点。 +- Acceptance: 文档与实现一致,两平台和本地模式回归通过,未验证内容准确披露。 +- Verification: 针对性测试、`git diff --check`、skill 校验及适用的跨平台 CI。 +- Rollback: 按切片提交回退代码;不自动删除用户数据或远端 Issue。 +- Status: done + +### Step 6 — 限定快照内容并验证隐私 +- Goal: 实施用户选择的仅编号、状态和更新时间的快照;保留所有其他 v1 功能和批准的边界。 +- Dependencies: 用户于 2026-11-16 选择该方案;v1 代码和文档已保留。 +- Files: `hooks/remote-todo`、`hooks/test-remote-todo-snapshot`、双语 README、`skills/taskflow/references/runtime.md`。 +- Implementation checklist: + - [x] 先以伪凭据、标题、正文和标签构造失败测试,覆盖两平台和无效字段。 + - [x] 仅输出校验过的来源序号、编号、状态和更新时间;失败不创建快照。 + - [x] 更新双语说明,运行定向和跨平台测试并提交 v2 Git 回退点。 +- Acceptance: 快照不包含远端自由文本或原始 JSON;多源可按来源顺序识别;无效状态、时间或来源失败时不落盘;原有列表、迁移和写入行为不变。 +- Verification: 先观察原实现泄漏测试失败,再验证两平台、伪凭据、无效字段、部分失败、已有文件保护及 PR CI。 +- Rollback: 回退 v2 代码提交;不删除或覆盖用户现有快照。 +- Status: done + +## Checkpoints +- Steps 1–2 后检查身份、平台差异和读取端到端结果。 +- Steps 3–4 后检查失败恢复、去重和完整生命周期。 + +## Verification / Review + +- 2026-11-16 Step 5: PRD acceptance reviewed; privacy fix and platform limitations recorded + +- 2026-11-16 Step 6: v2 snapshot privacy regression and three-platform Hooks CI passed +- 已比较活动任务、已有 Todo 和历史成果;无覆盖本任务的活动任务。 +- 仓库无 `.codegraph/`;现有本地 Todo 调用路径直接写在任务和归档脚本中。 +- 工作区 `.worktrees/capability-outcomes`,分支 `feature/remote-issue-todos`,v1 基线为分支创建时 HEAD。 +- 已实现配置校验、模拟远程读取、可选快照、Issue 创建及核对恢复;对应聚焦脚本红绿测试通过。真实 GitHub 公共仓库只读列表通过:1 条 Issue、141 个 Pull Request 被过滤;GitLab 和远端写入尚未真实验证。 +- 已提交 `0e98b7a`、`0025359`、`f4a6189`、`e78badf`、`c854332` 作为阶段回退点;这些提交未包含另一条 Todo。 +- 同仓库创建互斥与已知未决记录阻断已通过模拟并发测试;异常进程遗留互斥目录的用户恢复和真实平台行为仍未验证。 +- 已实现远端标题/正文编辑、标签与阶段变更、任务提升、双向复制/显式解除关联及混合列表去重;主要写入路径仍以模拟接口验证,完整验收尚未完成。 +- 远端恢复目录需要 Git 本地排除,未排除或包含已追踪文件时拒绝写入;测试覆盖固定探针绕过与强制追踪。 +- GitHub 缺失阶段标签的创建、任务状态、归档/恢复、迁移与解除关联的定向夹具通过;真实 GitHub 写入、自托管实例和真实 GitLab 尚未验证。 +- 2026-11-16 Windows 完整测试首次发现 `complete --root` 调用 `archive` 丢失根目录;已修复并在重跑中通过生命周期段。Windows 全量测试最终通过;Bash 全量在 Git Bash 上运行到独立复制 Bash 的原有测试时因缺少 Windows DLL 中止(该套件不在 Windows CI 运行)。Windows 运行时部分脚本使用系统临时目录,未遵守项目内临时目录边界;没有删除项目外文件,后续测试显式设置项目内临时目录。 +- `quick_validate.py skills/taskflow` 使用现有 torch Python 环境通过;`git diff --check` 通过。 +- 已解除关联的任务可在远端与本地阶段一致且未被其他任务占用时显式重新连接,保留历史;再次解除关联时保留旧副本并重新获取。GitHub/GitLab 夹具验证了冲突及恢复同步。未提升的远端本地副本可以明确选择远端为准并重新关联原 Issue;保留本地副本,禁止误用迁移在原仓库创建重复 Issue。用户选择稳定标记和迁移前全量检查;两平台重复标记及读取失败夹具通过。检查与创建非原子操作,同一条 Todo 不得在多台机器或 worktree 中同时迁移;用户手动移除标记也会削弱检查。 +- 2026-11-16:用户同意项目级默认、条目级归属、显式迁移和可选只读快照;用户已审阅需求方向;随后明确同意实施计划。 + +- Windows 完整回归 `windows-full-05` 通过,包括开放标准输入测试;之后对只读关联记录语义、孤立记录、跨仓库迁移与历史去重的调整分别运行 Windows 启动器定向测试通过,未重跑整个套件。 +- Linux/macOS 完整回归未在本机运行:Git Bash 执行 Bash 全量测试时,仓库原有的隔离 Bash 夹具缺少 Windows DLL 而中止;不可把它当作 Linux/macOS 通过。真实 GitHub 写入、自托管服务和真实 GitLab 仍未验证。 +- 用户在知悉真实 GitLab、自托管和远端写入未实测的情况下批准本次验收;这些真实平台验证保留为后续工作。测试夹具留在任务参考目录,另一条独立 Todo 保留在未提交工作区。 + +- PR #143 的首轮 Hooks CI:Ubuntu 在 hooks 清单检查失败,原因是两份清单遗漏新增脚本;已在 `hooks/README.md` 和 `runtime.md` 列出实际脚本,并修正网络命令说明。与 CI 相同的清单检查和 `git diff --check` 在项目内通过;修复后的完整 CI 待重跑。 + +- PR #143 第二轮 macOS 回归在中文标签 URL 编码失败;怀疑 Bash 3.2 的字节符号扩展。编码时显式限制为无符号 8 位,Windows Git Bash 的中文标签回归通过;macOS 结果以重跑 CI 为准。 + +- PR #143 的 Hooks CI 在代码提交 `4e1b09fb59b95e984b116552e7ad9e507db308a9` 全部通过,包括 Ubuntu、macOS、Windows、评估、发布和 Todo 合并检查:[CI run 43848406813](https://github.com/hkwuks/TaskFlow/actions/runs/43848406813)。此前两次失败的清单及 macOS 中文标签问题均已修复。真实 GitLab、自托管及远端写入仍未实测。 + +- 用户于 2026-11-16 批准本任务验收;此批准不表示已运行真实 GitLab、自托管或远端写入测试。PRD 逐项审阅:配置/本地默认对应配置与 intake 夹具;多源分页和 PR 过滤对应列表夹具及 GitHub 只读验证;创建、编辑、标签、阶段和恢复对应两平台 CLI 夹具;迁移、去重、快照和任务生命周期对应各自回归及三平台 Hooks CI。真实平台验证边界列为后续工作,不伪称通过。 + +- 归档前复核发现 `snapshot` 原样写入远端 Issue 内容,若内容含凭据则违反 PRD 的快照凭据要求。先撤回未提交的归档;用户批准验收基于此前披露的限制,不应误认为已接受该新发现。用户随后选择仅保存编号、状态及更新时间;v2 已实施并通过回归。 + +- v2 快照回归先在旧实现复现伪凭据和自由文本泄漏,再以 GitHub/GitLab 夹具验证允许列表、来源顺序、状态归一、时间格式、目标已存在和来源不完整;Windows 启动器定向测试通过。v2 的跨平台 CI 尚待运行。 + +- v2 代码提交 `d633a3ea17d52cb0200466e67c93befda94dafb7` 的 [Hooks CI](https://github.com/hkwuks/TaskFlow/actions/runs/43853794108) 在 Ubuntu、macOS、Windows 全部通过;快照泄漏测试先复现失败,修复后严格比较完整输出并覆盖无效数据、部分失败和两平台。用户选择仅保存编号、状态及更新时间,保留来源顺序编号,不保存远端自由文本。 + +## PR Template Mapping +- Summary:GitHub / GitLab Issues 作为可选 Todo 主记录,覆盖多仓库、状态标签、迁移和恢复。 +- TaskFlow traceability:本任务 `TaskFlowDocs/2026-11-16-remote-issue-todos/`;范围为远端适配器、任务生命周期、双语文档及回归测试;目标 `hkwuks/TaskFlow:main`。 +- Verification:`git diff --check origin/main...HEAD` 和 skill 校验通过;Windows 完整测试通过后有额外代码提交;PR Hooks CI 已在代码提交 `4e1b09f` 通过 Ubuntu、macOS、Windows。Git Bash 本机测试不单独构成 Linux/macOS 验收。 +- Review boundaries:不提交恢复数据、测试夹具或其他 Todo;不含凭据;真实 GitLab、自托管与远程写入未验证,远程创建及并发编辑存在已披露竞态。 +- PR 状态:用户已批准任务并选择 v2 快照隐私方案;三平台 CI 通过后归档并转为 Ready,合并仍需用户指示。 + +## Change Log +- 2026-11-16:用户选择快照只保留编号、状态和更新时间;v2 明确字段允许列表并保留 v1 其他要求。 +- 2026-11-16:记录用户决定、起草规格,并基于任务拆解技能编写计划。 +- 2026-11-16:核验官方接口,发现 GitHub 常规写请求不能依赖通用条件更新;用户接受仅更新指定字段、写前后核对并提示剩余竞态。 +- 2026-11-16:用户明确同意实施计划;手工记录批准,因为 `hooks/task` 每次运行会删除其临时目录,而当前用户规则禁止未经许可删除文件。 +- 2026-11-16:实现并提交配置、模拟读取、快照、创建恢复、编辑、标签、阶段同步和 Git 私有记录保护;全部是阶段成果,不代表完整功能已完成。 + +## Follow-ups +- 实际服务测试资源尚未确定;继续验证真实 GitLab、自托管及远端写入,不把模拟结果说成真实宿主验证。 +- 2026-11-16:用户选择跨工作区稳定标记与迁移前核对,明确不得并发迁移同一条;GitHub/GitLab 重复标记、读取失败、正文编辑保留标记及 Windows 启动器定向测试通过。 diff --git a/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/prd.md b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/prd.md new file mode 100644 index 0000000..a848ea6 --- /dev/null +++ b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/prd.md @@ -0,0 +1,47 @@ +# 远程 Issues 待办 +> Task version: v2 +> Status: completed + +## 目标 +让项目可选择使用 GitHub 或 GitLab Issues 作为待办主记录,并保留 TaskFlow 任务的本地执行关联。 + +## Task Kind +- Kind: capability + +## 已确认事实和用户决定 +- 每个项目显式选择本地或远程模式;默认保持本地模式。 +- 首版支持 GitHub 和 GitLab 官方云端与自托管实例;一个项目可连接多个 Issue 仓库。 +- 查看时汇总已连接仓库的待办;创建时由用户选择目标。Issue 是远程模式的待办主记录,本地只保留任务执行所需关联。 +- 支持创建、编辑 Issue,更新状态和标签。操作时读取远端,以远端为准,不做后台轮询。 +- 用户接受 GitHub 等不提供写入条件保护时的剩余竞态:仅更新指定字段、写前后核对并明确提示,不能保证发现或阻止所有并发覆盖。 +- TaskFlow 阶段用标签表示;完成或取消时关闭 Issue,保留其他标签。 +- 项目仓库配置默认模式,单条 Todo 保留实际存储归属;切换默认模式不自动迁移现有条目。 +- 用户可选择保留本地只读快照,标明时间及可能过期;本地关联始终保留,快照不能离线修改后自动覆盖远端。 +- 本地迁移到远端时保留原条目并关联去重;远端切回本地时由用户选择复制并解除关联,不擅自关闭或删除 Issue。 +- 远端不可用或权限不足时,远端更新操作明确失败,保留本地数据,恢复后由用户重试。 +- 旧本地 Todo 原文件保留;用户按需选择条目迁移,不自动删除。连接信息保存在项目中,复用宿主已有登录,凭据不写入仓库。 + +## 需求与验收标准 +- 本地模式保持现有工作流;明确选择远程模式后,可配置多个不同平台及自托管实例的仓库,凭据不进入项目文件、任务文档或日志。 +- 汇总列表可区分不同实例、仓库和 Issue;排除 GitHub Pull Request,分页结果完整,部分仓库失败时显式报告不完整。 +- 用户可选目标创建 Issue、编辑内容及标签、查看最新状态;不覆盖其他用户的无关标签或未修改字段。 +- Issue 可进入 TaskFlow 任务生命周期,关联足以定位远端 Issue 和本地任务;TaskFlow 阶段标签与关闭、恢复行为可追溯。 +- 本地与远端之间的失败、超时和结果不明均明确报告并可恢复;不确定结果不得盲目重试创建。并发修改尽可能识别并报告,但不声称能捕获读写窗口内的所有覆盖。 +- 可选择已有本地 Todo 迁移到指定仓库;保留原文件并防止重复创建;已有关联和历史不丢失。 +- 默认模式切换后,可识别各条现有待办的归属;已关联或迁移的本地和远程条目在汇总中去重。 +- 可选快照只读、可选择关闭并明确过期状态;只保存经过校验的 Issue 编号、状态和更新时间,以及不含仓库名称的来源顺序编号;不保存标题、正文、标签、凭据或其他远端字段。 +- 对 GitHub 与 GitLab 的各项操作、鉴权失败、分页、网络故障、并发修改及生命周期和迁移进行可重复验证;说明真实服务测试覆盖范围。 + +## External Facts +- Depends on external facts: yes +- Source: https://docs.github.com/en/rest/issues/issues +- What it settled: GitHub Issues 接口可能返回 Pull Request,需按 pull_request 字段排除;写操作需要相应权限。 +- Source: https://docs.gitlab.com/api/issues/ +- What it settled: GitLab 提供 Issue 创建、更新、标签及 state_event 状态操作;具体请求及权限需以选用的接口和实例版本核验。 + +## 范围之外 +- 后台轮询、自动离线队列和删除 Issue。 + +## 风险与待定事项 +- 本地文件与远端 API 无法组成单一数据库事务;需要定义部分成功和结果不明时的可恢复步骤。 +- 已知限制:平台不提供原子条件更新时,读后写窗口仍可能覆盖并发改动;远端删除或转移须明确报告并保留关联,不自动认定完成。 \ No newline at end of file diff --git a/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/reference/platform-evidence.md b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/reference/platform-evidence.md new file mode 100644 index 0000000..1741059 --- /dev/null +++ b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/reference/platform-evidence.md @@ -0,0 +1,16 @@ +# 平台接口证据 + +- GitHub REST Issues:`https://docs.github.com/en/rest/issues/issues`。Issue 端点返回项可能含 Pull Request,应检查 `pull_request`;按仓库和 issue number 定位,写操作需相应权限。 +- GitHub REST 最佳实践:`https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api`。除具体端点另有说明外,POST、PUT、PATCH、DELETE 的条件请求不受支持。不能把读 ETag 后写入当作原子条件更新;重读只能减少窗口。 +- GitHub CLI:`https://cli.github.com/manual/gh_api`。`gh api` 支持 `--hostname`、`--paginate`、`--slurp` 和从 stdin 输入请求体。需对自托管实例及实际安装版本验证。 +- GitLab Issues:`https://docs.gitlab.com/api/issues/`。项目地址是项目 ID 或 URL 编码路径,issue 使用项目内 IID;更新支持 `add_labels`、`remove_labels` 和 `state_event`。`add_labels` 中不存在的标签可能被自动创建,须纳入权限与副作用设计;私有项目无权访问也可能返回 404。 +- GitLab CLI:`https://docs.gitlab.com/cli/api/`。`glab api` 支持 `--hostname`、`--paginate`、从 stdin 输入 JSON;其文档声明 429 错误需按响应提示重试,不能将所有失败混为未写入。 +- 本机可发现 `gh` 2.100.0,未发现 `glab`;不据此推断用户其他主机的安装或授权状态。 + +## 未验证 +- GitLab 指定 Issue 写接口的条件更新能力尚无可确认的官方依据;不能宣称支持原子并发保护。 +- 两平台真实私有、自托管实例及故障注入尚未运行;本证据只来自文档和本机只读命令。 +## 真实 GitHub 只读验证 +- 2026-11-16:使用项目内隔离配置执行实际 `remote-todo list-remote`,退出码 0,返回 1 条 Issue。单独对公开 API 按类型计数为 1 条 Issue、141 条 Pull Request,过滤结果与 Issue 数一致;未记录 Issue 内容。没有执行真实远端写入。 + +- 复核 GitLab CLI 官方文档:`--paginate --output ndjson` 每个对象一行,`--field description=@-` 从标准输入读取内容;本机无 `glab`,仍未进行真实客户端验证。 diff --git a/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/spec.md b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/spec.md new file mode 100644 index 0000000..9a2e3a2 --- /dev/null +++ b/TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/spec.md @@ -0,0 +1,78 @@ +# Spec — 远程 Issues 待办 +> Task version: v2 +> Status: completed + +## 目标与成功标准 +实现 PRD 所列 GitHub、GitLab 多仓库 Issue 待办及 TaskFlow 生命周期;远端是待办主记录,本地保存执行关联和用户选择的只读快照。 + +## 架构边界与职责 +- 用户操作层选择模式、仓库、Issue 和明确请求的操作;不在后台触发同步。 +- 远程访问层负责各平台实例鉴权、分页、Issue 标识、读写与错误分类;不得输出或持久化凭据。 +- TaskFlow 生命周期层管理本地任务及其关联,不把远端内容复制成第二份待办主记录。 +- 存储和恢复边界需明确项目共享连接配置、本地关联以及跨本地/远程部分成功后的重试。 + +## 受影响范围 +- `skills/taskflow/` 与 `hooks/`:插件用户的入口、生命周期和行为,而非仅修改本仓库治理规则。 +- `TaskFlowDocs/todo.md` 的读取、提升、归档、恢复、冲突及检查路径;两份 README、运行时文档和相关测试。 +- `hooks/task`、`hooks/archive`、`hooks/reopen`、`hooks/summarize-state`、`hooks/todo-check` 等依赖的具体调整,待设计核查后确定。 + +## 接口、数据流与契约 +- 项目配置记录默认模式及多个平台、实例和仓库标识;每条记录保留实际归属,绝不包含访问令牌。 +- 切换默认模式只改变新建条目的默认位置;已有条目继续按原位置处理,用户可选择条目逐条迁移。 +- 用户可选择保留带抓取时间的本地只读快照;离线时标为可能过期,不允许把快照当作可编辑主记录。 +- 唯一身份至少包含平台、实例、仓库和 Issue 标识;不能把不同仓库的相同编号视为同一项。 +- 查询汇总保留来源,并区分完整结果与部分失败;GitHub 列表排除 Pull Request。 +- 创建由用户指定目标;编辑基于新读取的远端状态,仅修改用户指定字段;处理并发修改,保留非 TaskFlow 标签。 +- 阶段以专用标签表达;完成或取消关闭 Issue。具体标签命名、状态映射、冲突检测与恢复协议待设计。 +- 旧本地条目只在用户选择时迁移,保留原文件并关联去重。远端迁回本地时用户明确选择复制及解除关联,远端 Issue 不自动关闭或删除。 +- 本地任务与远端 Issue 分属两套存储;先持久化恢复意图和操作标识,再发起远端变更,保存已确认结果后更新本地关联。结果不明时停止自动重试并引导查询核对;不能凭网络失败断定远端未执行。 +- 更新时先重新读取远端;当平台支持安全的条件更新时使用条件更新,否则明确并发窗口,检测到冲突或不能确认结果时停止并要求核对,不以先读后写宣称完全无竞态。 + +## 不变量、错误和恢复 +- 默认本地模式;远程更新失败不得伪报成功或丢失本地数据。 +- 远端成功但响应丢失、本地写入失败、读取权限不足、远端变更和重复重试均须有明确处理和验证,不得假定本地和远端可原子提交。 +- 不记录凭据;Issue 内容和自托管地址按用户授权范围处理。 + +## 测试约束 +- 两平台各覆盖列表分页、创建、更新、标签、状态、权限和失败恢复;覆盖多实例及同号 Issue、迁移重复执行、已有本地模式、归档与恢复。 +- 使用可控测试服务或隔离夹具验证网络故障和并发;真实托管服务验证须明确测试资源和授权,不能以模拟替代已完成的真实测试声明。 + +## 开放设计问题 +- 在平台约束下完成阶段标签和恢复协议的细化;无条件更新时按用户接受的竞态限制实施。 +- 认证工具选择及未安装或不支持实例时的提示;主机支持范围与依赖约束。 +- 远端 Issue 被第三方关闭、删除或转移后的细节和平台能力差异。 +## 已核实的接口约束与待审方案 +- 使用现有宿主的 `gh api` 和 `glab api` 作为候选适配器;按配置的主机明确寻址,使用标准输入传递请求体,不读取或保存用户令牌。缺少 CLI、对应主机登录或权限时明确报告。 +- GitHub 的 Issue 列表含 Pull Request,必须过滤;GitLab 使用 URL 编码项目路径及项目内 IID。多仓库结果包含完整来源,某个来源失败时必须明确标示不完整。 +- GitLab 的 `add_labels` 在标签不存在时可能创建项目标签,建立阶段标签时必须把这种副作用和所需权限纳入设计;用户未请求的其他标签不得用整表替换方式清除。 +- GitHub 普通写入不支持通用条件请求;预读取和事后核对不能保证阻止两个客户端竞态。用户已接受这一限制,操作提示须明确说明;GitLab 的条件更新能力尚未得到证实。 +- 对创建结果不明的操作,持久记录非敏感操作标识及目标,停止自动重试;只有可靠核对后才关联已有 Issue 或由用户决定下一步,不能简单以标题匹配判定唯一。 +- 缓存和恢复记录可能包含私有项目信息;默认不生成内容快照,用户显式启用时说明保存位置、访问边界、更新时间和不完整状态,凭据不入文件。 +## 操作约定 +- 显式操作命令使用项目本地配置与结构化输入;不信任远程字段、转义序列或可执行文本,不执行从 Issue 或配置中读取的命令。GitHub 与 GitLab 适配器只接受校验过的平台、主机和仓库参数。 +- 本地模式继续使用现有任务接口;远程模式对已提升任务保存可稳定定位 Issue 的关联。模式切换只影响新建默认值,查询汇总本地遗留项和已连接远端并按关联去重。 +- 操作恢复记录保存在不含凭据的项目工作区私有位置;意图与每次远端写入结果分别记录。创建结果未知时不自动重复写入,显式查询和用户确认后恢复;查询不完整时保持未知。 +- 每一阶段映射为单一 TaskFlow 专用标签;以增删指定标签的方式更新,保留其他标签。缺失标签的创建及权限要求在操作前提示,更新失败时不假报完成。 +- 用户选择生成的只读快照默认不生成;当网络失败时仅作为有时间标记的历史信息展示,且不能作为写入依据。快照及本地恢复材料可能包含私有 Issue 信息,写入之前说明位置和共享风险。 +- 对于远端不存在、无权限或已转移,保留本地任务和关联并报告无法确认;不存在与无权限不可由单次 404 安全区分。 +- 手工验证基线:`git diff --check`;核心行为由 `bash hooks/smoke-test`、新增聚焦测试和 `powershell -File hooks/smoke-test-windows.ps1` 的相应范围验证;CI 验证 Ubuntu、macOS、Windows。 +- 测试文件与入口按现有 `hooks/test-*` 和 `hooks/smoke-test` 约定;插件 hooks 的 Bash 3.2 与无语言运行时约束见 `skills/taskflow/references/runtime.md`。 +## 持久数据和恢复协议 +- 项目配置使用 `TaskFlowDocs/remote-todos.conf`:首行声明 `local` 或 `remote` 默认模式,其余行声明每个连接的唯一别名、平台、HTTPS 主机和仓库路径;禁止换行、控制字符、URL 用户信息及查询参数,拒绝重复别名。模式缺省时视为本地;连接变更不迁移条目。 +- Issue 的稳定身份由平台、规范化主机、平台项目标识和 Issue 编号组成;展示别名不作为唯一身份。GitLab 的项目路径按 API 规定 URL 编码,GitHub 仓库按 owner/repo 校验。 +- 只为已提升或显式迁移的条目在本地保留关联;远端未提升 Issue 不生成本地 Todo 正文。关联中保留远端身份和任务路径,不把远端内容设为本地权威。历史本地条目原文保留,通过关联去重。 +- 远端创建前使用操作唯一 ID 持久化意图;在新建 Issue 描述附加可识别的 TaskFlow 操作标记。确认创建成功后持久化平台身份;超时等结果不明时列出全部可能目标并按标记核对,查询不完整、没有匹配或多个匹配时停止并让用户核对,不凭标题或空结果自动重复创建。 +- 生命周期标签固定为 `taskflow:inbox`、`taskflow:clarified`、`taskflow:promoted`、`taskflow:planning`、`taskflow:ready`、`taskflow:in_progress`、`taskflow:checking`、`taskflow:blocked`、`taskflow:done`、`taskflow:cancelled`,更新时增删 TaskFlow 阶段标签,不替换其他标签。完成与取消均关闭 Issue,重新打开任务时重新打开 Issue。缺失标签创建行为和权限必须明确告知;GitHub 与 GitLab 分别使用其原生标签接口。 +- 可选只读快照和恢复意图不能写入诊断日志,不包含凭据。快照开启及位置由用户选择;默认关闭。故障导致关联更新未完成时不标记整体成功,保留已知远端身份以便恢复。 +- `gh` / `glab` 作为用户显式选择远程模式的外部工具;本地模式和已有 hooks 不新增语言运行时依赖。复用或扩展仓库 JSON 处理能力时必须覆盖嵌套数组、Unicode、特殊字符及恶意输入。 +## 跨工作区迁移防重 +- 用户选择稳定 Todo 标记:创建前分页检查目标仓库所有 Issue,出现匹配或检查不完整时停止;不同工作区和机器不得同时迁移同一条 Todo,因为检查与创建不是原子操作。 +- TaskFlow 正文编辑保留稳定标记;用户或其他客户端删除标记会削弱后续检查。标记匹配只提示人工核对,不自动合并记录。 + +## 切换回原 Issue +- 已解除关联的活动任务仅在原 Issue 未被其他任务关联、远端阶段与本地任务一致时重新连接;否则保留现状并先人工协调。此前解除关联记录和本地快照保留,再次切回本地时重新获取快照。 +- 未提升的远端本地副本可显式选择远端为准并重新关联原 Issue;此操作只读取远端并保留原本地内容。普通迁移不会将该副本再次创建到原仓库;切换到其他仓库仍可按正常迁移处理。 + +## v2 快照隐私契约 +- 快照按配置来源顺序分组,仅输出来源序号、正整数 Issue 编号、限定状态及严格格式校验的更新时间。禁止输出标题、正文、标签、仓库标识及其他远端字段;不输出原始 JSON。 +- 对 GitHub 和 GitLab 分别识别编号字段,状态归一为 open / closed;无法完整读取、解析或校验任一条记录时,不创建快照。来源序号从 1 开始,与配置中的来源顺序对应;快照保持历史只读语义。 diff --git a/TaskFlowDocs/todo.md b/TaskFlowDocs/todo.md index cd502e4..f9015ee 100644 --- a/TaskFlowDocs/todo.md +++ b/TaskFlowDocs/todo.md @@ -35,6 +35,14 @@ has an issue number or URL. +## 支持使用远程仓库 Issues 作为用户的待办列表 + +- ID: TF-20261116-7e2c41 +- Status: done +- Goal: 支持将远程仓库的 Issues 作为用户的 Todo 列表记录使用。 +- Task: `TaskFlowDocs/achieved/2026-11-16-remote-issue-todos/` +- Next action: None — completed and archived. + ## 排查任务版本 Git 回退点检查的子模块忽略规则及子目录 root - ID: TF-20261112-f593c5 diff --git a/hooks/README.md b/hooks/README.md index b02abef..641984c 100644 --- a/hooks/README.md +++ b/hooks/README.md @@ -47,6 +47,40 @@ taskflow/hooks/ ├── test-host-parity # synthetic host event and capability declaration fixture ├── smoke-test-windows.ps1 # Windows PowerShell/cmd lifecycle regression ├── test-version-baseline # real Git lifecycle regression fixture +├── remote-todo # explicit remote Issue Todo commands +├── remote-todo-local-list # list and deduplicate retained local Todos +├── remote-todo-migrate # migrate one local Todo to an Issue +├── remote-todo-promote # promote an Issue into a TaskFlow task +├── remote-todo-reattach # reconnect a detached task after checking phases +├── remote-todo-reattach-todo # reconnect an unpromoted local Issue copy +├── remote-todo-to-local # copy an Issue locally and optionally detach +├── test-json-field-arrays # JSON string array membership regression +├── test-json-field-numbers # JSON number parsing regression +├── test-remote-todo # remote Todo regression +├── test-remote-todo-archive # remote Todo archive regression +├── test-remote-todo-concurrency # remote Todo concurrency regression +├── test-remote-todo-configure # remote Todo configure regression +├── test-remote-todo-create # remote Todo create regression +├── test-remote-todo-detach # remote Todo detach regression +├── test-remote-todo-detach-archived # remote Todo detach archived regression +├── test-remote-todo-detach-task # remote Todo detach task regression +├── test-remote-todo-edit # remote Todo edit regression +├── test-remote-todo-intake # remote Todo intake regression +├── test-remote-todo-label # remote Todo label regression +├── test-remote-todo-link # remote Todo link regression +├── test-remote-todo-list # remote Todo list regression +├── test-remote-todo-migrate # remote Todo migrate regression +├── test-remote-todo-migrate-dedup # remote Todo migrate dedup regression +├── test-remote-todo-migrate-stage # remote Todo migrate stage regression +├── test-remote-todo-mixed-list # remote Todo mixed list regression +├── test-remote-todo-private # remote Todo private regression +├── test-remote-todo-promote # remote Todo promote regression +├── test-remote-todo-recover # remote Todo recover regression +├── test-remote-todo-reminders # remote Todo reminders regression +├── test-remote-todo-snapshot # remote Todo snapshot regression +├── test-remote-todo-stage # remote Todo stage regression +├── test-remote-todo-task-state # remote Todo task state regression +├── test-remote-todo-to-local # remote Todo to local regression └── smoke-test # assert-style smoke tests ``` @@ -320,3 +354,7 @@ Hosts without hooks run the base flow unchanged; hooks are optional. The `base-write-gate` checks observable file-edit targets before `capability-gate` consumes evidence. It is not a filesystem sandbox: shell and other unobserved paths can still write, and release edits may be refused. See `skills/taskflow/references/runtime.md` for host coverage. Codex and Claude Code also check the base checkout asynchronously after shell calls. They report existing uncommitted changes outside Todo without claiming which command or person caused them. Claude Code delivers asynchronous output to the agent on a later turn rather than showing it directly to the user. This is a Git status reminder, not a before/after comparison; ignored files and changes that disappear before the check may be missed. + +## Optional remote Todo backend + +`remote-todo` is an explicit command, not an event-bound network hook. Run it from the user repository and invoke it using the installed plugin's `hooks/remote-todo` path. `mode-set` and `connect` configure the project; `list` combines local items with configured GitHub/GitLab sources. See `skills/taskflow/references/runtime.md#remote-todo-backend` for the operations, authentication, private recovery records, and non-atomic failure semantics. The local mode remains the default. \ No newline at end of file diff --git a/hooks/archive b/hooks/archive index f1e58b4..e443512 100755 --- a/hooks/archive +++ b/hooks/archive @@ -51,6 +51,10 @@ if ! grep -q "\`TaskFlowDocs/$task/\`" "$todo"; then echo "Todo entry for $task missing; archive not started"; exit 1 fi +if [ -e "$docs/$task/remote-issue" ] || [ -L "$docs/$task/remote-issue" ]; then + bash "$HERE/remote-todo" stage-task "$task" done --root "$root" || { echo 'Remote stage update failed; local archive was not started.' >&2; exit 1; } + echo 'If a remote stage changed and local archive fails, inspect both sides before retrying.' >&2 +fi # Undo whatever step 2–4 already did, so a failure never leaves active and # achieved both present, a half-rewritten Todo beside a moved directory, or a # Todo record appended to a Plan whose entry is about to come back. diff --git a/hooks/install-git-hygiene b/hooks/install-git-hygiene index e5473fa..a640cd6 100755 --- a/hooks/install-git-hygiene +++ b/hooks/install-git-hygiene @@ -6,7 +6,7 @@ # * `merge.taskflow-todo.driver` in the repository's own config, so parallel # task branches merge `TaskFlowDocs/todo.md` by entry # * a `TaskFlowDocs/todo.md merge=taskflow-todo` line in `.git/info/attributes` -# * two ignore rules in `.git/info/exclude`, for the files that belong to one +# * local-only ignore rules in `.git/info/exclude`, for the files that belong to one # clone rather than to the repository # # Nothing is written outside the repository, and nothing tracked is edited: a @@ -53,12 +53,13 @@ if [ ! -f "$info" ] || ! grep -qxF 'TaskFlowDocs/todo.md merge=taskflow-todo' "$ printf 'TaskFlowDocs/todo.md merge=taskflow-todo\n' >> "$info" 2>/dev/null || exit 0 fi -# Two TaskFlow files belong to the clone, not to the repository: +# These TaskFlow paths belong to the clone, not to the repository: # * `repository-docs/personal.md` is a personal rule, which is never team policy # * `sessions.md` carries a session id, a working directory, timestamps and # `Session availability: local-only`, so it is a resume index for the machine # that wrote it -# Both are documented as never committed, and until now nothing made that true +# * remote Todo operation and update records contain local recovery state +# These are documented as never committed, and until now nothing made that true # for a repository that is not this one: a `.gitignore` line lives in a tracked # file, so it would change every clone's view — including the clones that really # do want to commit them. `info/exclude` is the per-clone place for a per-clone @@ -76,7 +77,9 @@ fi # trying to recognise them as the same rule would be a guess, and the direction # it fails is the file getting committed again. LOCAL_ONLY='TaskFlowDocs/repository-docs/personal.md -TaskFlowDocs/**/sessions.md' +TaskFlowDocs/**/sessions.md +TaskFlowDocs/.remote-todo-operations/ +TaskFlowDocs/.remote-todo-updates/' exclude="$(git -C "$root" rev-parse --git-path info/exclude 2>/dev/null)" || exit 0 [ -n "$exclude" ] || exit 0 case "$exclude" in /*|[A-Za-z]:[/\\]*) ;; *) exclude="$root/$exclude" ;; esac diff --git a/hooks/json-field b/hooks/json-field index c79a0ae..de504a3 100644 --- a/hooks/json-field +++ b/hooks/json-field @@ -1,10 +1,10 @@ #!/usr/bin/env bash # json-field — print selected fields from a JSON object on stdin, one per line. # -# Usage: json-field [...] spec: `key` or `key.sub` (one level) +# Usage: json-field [--numbers] [...] spec: `key` or `key.sub` (one level) # -# Prints one line per spec, in the order given; a missing field, a null, or a -# non-string scalar prints an empty line so callers can read by position. +# Prints one line per spec; missing fields and non-string scalars print empty. +# --numbers also returns JSON numbers without changing the default behavior. # Exit 0 on success, 2 on invalid JSON. # # The parser is the one `session-start` uses, extended to descend one level. @@ -20,10 +20,17 @@ set -euo pipefail LC_ALL=C export LC_ALL +numbers=0 +array_mode=0 +if [ "${1:-}" = --numbers ]; then numbers=1; shift; fi +if [ "${1:-}" = --array-has ]; then + [ "$#" -eq 3 ] || { echo 'usage: json-field --array-has ' >&2; exit 2; } + array_mode=1; TASKFLOW_ARRAY_EXPECTED="$3"; export TASKFLOW_ARRAY_EXPECTED; set -- "$2" +fi KEYS="${*:-}" [ -n "$KEYS" ] || exit 0 -awk -v KEYS="$KEYS" ' +awk -v KEYS="$KEYS" -v NUMBERS="$numbers" -v ARRAY_MODE="$array_mode" ' function die(msg) { printf "TaskFlow json-field error: invalid event JSON: %s\n", msg > "/dev/stderr" exit 2 @@ -85,18 +92,20 @@ awk -v KEYS="$KEYS" ' die("bad escape") } } - # A string is returned unescaped; null, a number, true and false all consume - # their token and return empty, so a caller never sees a raw JSON token. - function scalar( c) { + # Return strings and opt-in numeric tokens; null and booleans remain empty. + function scalar( c, start, token) { ws() c = peek() if (c == "\"") return str() if (substr(S, P, 4) == "null") { P += 4; return "" } + start = P while (P <= L) { c = peek() if (c == "," || c == "}" || c == "]" || c == " " || c == "\t" || c == "\n" || c == "\r") break P++ } + token = substr(S, start, P - start) + if (NUMBERS && token ~ /^-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?$/) return token return "" } function skipval( c, depth) { @@ -120,6 +129,23 @@ awk -v KEYS="$KEYS" ' P++ } } + function array_has( item, found) { + if (peek() != "[") die("expected a string array") + array_seen = 1 + P++; ws() + if (peek() == "]") { P++; return "no" } + while (1) { + ws() + if (peek() != "\"") die("expected an array string") + item = str() + if (item == ENVIRON["TASKFLOW_ARRAY_EXPECTED"]) found = 1 + ws() + if (peek() == ",") { P++; continue } + if (peek() == "]") { P++; break } + die("expected an array comma or closing bracket") + } + return found ? "yes" : "no" + } # Descend into the object whose opening brace is next, capturing `parent.sub` # specs. A null or a non-object value leaves every nested spec empty. function nested_obj(parent, k, sp) { @@ -177,7 +203,7 @@ awk -v KEYS="$KEYS" ' if (peek() != ":") die("expected a colon") P++ ws() - if (k in want) out[want[k]] = scalar() + if (k in want) out[want[k]] = ARRAY_MODE ? array_has() : scalar() else if (k in nested) nested_obj(k) else skipval() ws() @@ -188,6 +214,7 @@ awk -v KEYS="$KEYS" ' } ws() if (P <= L) die("unexpected trailing content") + if (ARRAY_MODE && !array_seen) die("missing string array") for (i = 1; i <= NS; i++) print (i in out) ? out[i] : "" } ' diff --git a/hooks/prompt-reminder b/hooks/prompt-reminder index e4e4a2e..1b54a6e 100644 --- a/hooks/prompt-reminder +++ b/hooks/prompt-reminder @@ -129,6 +129,12 @@ If this instruction asks you to change this repository or deliver a development bash hooks/task intake "" git worktree add .worktrees/ -b / Read-only work — explanation, translation, status, research, review, diagnosis — needs neither. If the work is already recorded elsewhere, say where and continue.' + case "$summary" in + *'- Todo backend: remote'*) + ctx='This repository uses remote Issues as its Todo backend. For development work, select a configured remote repository and record the request there before task selection; retain existing local records and create a task worktree before planning. Remote access is explicit, not automatic.' ;; + *'- Todo backend: invalid'*) + ctx='This repository has an invalid remote Todo configuration. Repair it before selecting an intake location; do not silently switch to a different backend.' ;; + esac ctx="$ctx For development tasks: $capability_guidance" printf '{\n "hookSpecificOutput": {\n "hookEventName": "UserPromptSubmit",\n "additionalContext": "%s"\n }\n}\n' "$(escape_for_json "$ctx")" diff --git a/hooks/remote-todo b/hooks/remote-todo new file mode 100644 index 0000000..4a7db98 --- /dev/null +++ b/hooks/remote-todo @@ -0,0 +1,675 @@ +#!/usr/bin/env bash +# Explicit remote Todo operations; a missing configuration keeps local mode. +set -euo pipefail +usage() { echo 'usage: remote-todo mode|validate|list [--root ] | mode-set local|remote [--root ] | connect github|gitlab [--root ] | snapshot [--root ] | create <body-file> [--root <path>] | migrate-to-remote <todo-id> <alias> [--root <path>] | promote <alias> <number> <task-id> small|large --kind <kind> [--root <path>] | migrate-to-local <alias> <number> [--detach] [--root <path>] | edit <alias> <number> title|body <value> [--root <path>] | stage <alias> <number> <stage> [--root <path>] | stage-task <task-id> <stage> [--root <path>] | reattach-task <task-id> [--root <path>] | reattach-todo <todo-id> <alias> --remote-authoritative [--root <path>] | label <alias> <number> add|remove <label> [--root <path>] | link <alias> <number> <task-id> [--root <path>] | resolve <operation-id> <number> [--root <path>] | resolve-absent <operation-id> --checked [--root <path>] | ack-update <operation-id> --checked [--root <path>]' >&2; exit 2; } +[ "$#" -ge 1 ] || usage +command="$1"; shift +target= +if [ "$command" = mode-set ]; then [ "$#" -ge 1 ] || usage; requested_mode="$1"; shift; fi +if [ "$command" = connect ]; then [ "$#" -ge 4 ] || usage; new_alias="$1"; new_platform="$2"; new_host="$3"; new_project="$4"; shift 4; fi +if [ "$command" = snapshot ]; then [ "$#" -ge 1 ] || usage; target="$1"; shift; fi +if [ "$command" = ack-update ]; then + [ "$#" -ge 2 ] && [ "$2" = --checked ] || usage + resolve_id="$1"; shift 2 +fi +if [ "$command" = resolve-absent ]; then + [ "$#" -ge 2 ] && [ "$2" = --checked ] || usage + resolve_id="$1"; shift 2 +fi +if [ "$command" = resolve ]; then + [ "$#" -ge 2 ] || usage + resolve_id="$1"; resolve_number="$2"; shift 2 +fi +if [ "$command" = link ]; then + [ "$#" -ge 3 ] || usage + selected="$1"; issue_number="$2"; task_id="$3"; shift 3 +fi +if [ "$command" = label ]; then + [ "$#" -ge 4 ] || usage + selected="$1"; issue_number="$2"; action="$3"; label="$4"; shift 4 +fi +if [ "$command" = reattach-task ]; then [ "$#" -ge 1 ] || usage; task_id="$1"; shift; fi +if [ "$command" = reattach-todo ]; then [ "$#" -ge 3 ] && [ "$3" = --remote-authoritative ] || usage; local_id="$1"; selected="$2"; shift 3; fi +if [ "$command" = stage-task ]; then + [ "$#" -ge 2 ] || usage + task_id="$1"; stage="$2"; shift 2 +fi +if [ "$command" = stage ]; then + [ "$#" -ge 3 ] || usage + selected="$1"; issue_number="$2"; stage="$3"; shift 3 +fi +if [ "$command" = edit ]; then + [ "$#" -ge 4 ] || usage + selected="$1"; issue_number="$2"; field="$3"; value="$4"; shift 4 +fi +if [ "$command" = promote ]; then + [ "$#" -ge 6 ] && [ "$5" = --kind ] || usage + selected="$1"; issue_number="$2"; task_id="$3"; task_size="$4"; task_kind="$6"; shift 6 +fi +if [ "$command" = migrate-to-local ]; then + [ "$#" -ge 2 ] || usage + selected="$1"; issue_number="$2"; shift 2 + detach=0; if [ "${1:-}" = --detach ]; then detach=1; shift; fi +fi +if [ "$command" = migrate-to-remote ]; then + [ "$#" -ge 2 ] || usage + local_id="$1"; selected="$2"; shift 2 +fi +if [ "$command" = create ]; then + [ "$#" -ge 3 ] || usage + selected="$1"; title="$2"; body="$3"; shift 3 + supplied_id= + if [ "${1:-}" = --operation-id ]; then [ "$#" -ge 2 ] || usage; supplied_id="$2"; shift 2; fi +fi +root=. +if [ "$#" -gt 0 ]; then + [ "$#" -eq 2 ] && [ "$1" = --root ] || usage + root="$2" +fi +case "$command" in mode|mode-set|connect|validate|list|list-remote|snapshot|create|migrate-to-remote|migrate-to-local|promote|edit|stage|stage-task|reattach-task|reattach-todo|label|link|resolve|resolve-absent|ack-update) ;; *) usage ;; esac +if [ "$command" = snapshot ]; then + [ -n "$target" ] && [ ! -e "$target" ] && [ ! -L "$target" ] || { echo 'Snapshot target must be a new file.' >&2; exit 2; } + if ! content="$(bash "$0" list-remote --root "$root")"; then echo 'Snapshot not created: remote listing incomplete.' >&2; exit 2; fi + HERE="$(cd "$(dirname "$0")" && pwd)" + source_count=0; platform=''; safe='' + time_pattern='^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]([.][0-9]{1,9})?(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])$' + while IFS= read -r row; do + [ -n "$row" ] || continue + case "$row" in + 'SOURCE '*) + read -r header alias platform host project <<EOF +$row +EOF + case "$platform" in github|gitlab) ;; *) echo 'Snapshot not created: invalid source.' >&2; exit 2 ;; esac + source_count=$((source_count + 1)); safe="${safe}Source $source_count (configuration order) +" ;; + *) + case "$platform" in github) number_key=number ;; gitlab) number_key=iid ;; *) echo 'Snapshot not created: missing source.' >&2; exit 2 ;; esac + number="$(printf '%s' "$row" | bash "$HERE/json-field" --numbers "$number_key")" || { echo 'Snapshot not created: invalid Issue.' >&2; exit 2; } + state="$(printf '%s' "$row" | bash "$HERE/json-field" state)" || { echo 'Snapshot not created: invalid Issue.' >&2; exit 2; } + updated="$(printf '%s' "$row" | bash "$HERE/json-field" updated_at)" || { echo 'Snapshot not created: invalid Issue.' >&2; exit 2; } + [[ "$number" =~ ^[1-9][0-9]*$ ]] || { echo 'Snapshot not created: invalid Issue number.' >&2; exit 2; } + case "$state" in open|opened) state=open ;; closed) ;; *) echo 'Snapshot not created: invalid Issue state.' >&2; exit 2 ;; esac + [[ "$updated" =~ $time_pattern ]] || { echo 'Snapshot not created: invalid update time.' >&2; exit 2; } + safe="${safe}${number} ${state} ${updated} +" ;; + esac + done <<EOF +$content +EOF + [ "$source_count" -gt 0 ] || { echo 'Snapshot not created: no source.' >&2; exit 2; } + umask 077 + (set -o noclobber; printf 'Snapshot UTC: %s (read-only historical metadata; may be stale)\n%s' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$safe" > "$target") || { echo 'Cannot create snapshot.' >&2; exit 2; } + exit 0 +fi +validate_config() { + awk -F '\t' -v action="$2" ' + function invalid() { bad = 1 } + NR == 1 { + if (NF != 1 || ($1 != "local" && $1 != "remote")) invalid() + mode = $1 + next + } + { + if (NF != 4 || $1 !~ /^[A-Za-z0-9][A-Za-z0-9_-]*$/ || ($2 != "github" && $2 != "gitlab")) invalid() + if ($3 !~ /^[A-Za-z0-9][A-Za-z0-9.-]*(:[0-9]+)?$/ || $3 ~ /\.\./) invalid() + if ($4 !~ /^[A-Za-z0-9_.-]+(\/[A-Za-z0-9_.-]+)+$/ || $4 ~ /(^|\/)\.\.?($|\/)/) invalid() + if ($2 == "github" && split($4, parts, "/") != 2) invalid() + if (seen[$1]++) invalid() + } + END { + if (NR == 0 || bad) { print "Invalid remote Todo configuration." > "/dev/stderr"; exit 2 } + if (action == "mode") print mode + else if (action == "validate") print "OK " mode + } +' "$1" +} +config="$root/TaskFlowDocs/remote-todos.conf" +if [ "$command" = mode-set ] || [ "$command" = connect ]; then + if [ "$command" = mode-set ]; then case "$requested_mode" in local|remote) ;; *) echo 'Invalid Todo mode.' >&2; exit 2 ;; esac + else + for part in "$new_alias" "$new_platform" "$new_host" "$new_project"; do + [[ "$part" != *[[:cntrl:]]* ]] || { echo 'Invalid remote Todo configuration.' >&2; exit 2; } + done + printf 'local\n%s\t%s\t%s\t%s\n' "$new_alias" "$new_platform" "$new_host" "$new_project" | validate_config /dev/stdin validate >/dev/null || exit 2 + fi + mkdir -p "$root/TaskFlowDocs" + lock="$root/TaskFlowDocs/.remote-todo-config-lock" + mkdir "$lock" 2>/dev/null || { echo 'Todo configuration is busy or needs inspection.' >&2; exit 2; } + trap 'rmdir "$lock" 2>/dev/null || true' EXIT + [ ! -L "$config" ] || { echo 'Invalid remote Todo configuration.' >&2; exit 2; } + if [ -e "$config" ]; then [ -f "$config" ] && validate_config "$config" validate >/dev/null || exit 2; fi + if [ "$command" = connect ] && [ -f "$config" ]; then + if awk -F '\t' -v name="$new_alias" 'NR > 1 && $1 == name { found = 1 } END { if (!found) exit 1 }' "$config"; then echo 'Source alias already exists.' >&2; exit 2; fi + fi + tmp="$(mktemp "$root/TaskFlowDocs/.remote-todo-config.XXXXXXXX")" || { echo 'Cannot stage Todo configuration.' >&2; exit 2; } + if [ -f "$config" ]; then + if [ "$command" = mode-set ]; then awk -v mode="$requested_mode" 'NR == 1 { print mode; next } { print }' "$config" > "$tmp" + else cat "$config" > "$tmp"; fi + else + if [ "$command" = mode-set ]; then printf '%s\n' "$requested_mode" > "$tmp"; else printf 'local\n' > "$tmp"; fi + fi + if [ "$command" = connect ]; then printf '%s\t%s\t%s\t%s\n' "$new_alias" "$new_platform" "$new_host" "$new_project" >> "$tmp"; fi + validate_config "$tmp" validate >/dev/null || { echo 'Staged configuration invalid; original retained.' >&2; exit 2; } + mv "$tmp" "$config" || { echo 'Cannot publish configuration; original retained where possible.' >&2; exit 2; } + echo 'Remote Todo configuration updated.' + exit 0 +fi +if [ "$command" = stage-task ]; then + HERE="$(cd "$(dirname "$0")" && pwd)" + bash "$HERE/task-id" "$task_id" || exit 2 + active="$root/TaskFlowDocs/$task_id/remote-issue" + achieved="$root/TaskFlowDocs/achieved/$task_id/remote-issue" + [ ! -e "$active" ] || [ ! -e "$achieved" ] || { echo 'Ambiguous task association.' >&2; exit 2; } + ref="$active" + [ -e "$ref" ] || ref="$achieved" + [ -f "$ref" ] && [ ! -L "$ref" ] || { echo 'Remote task association not found.' >&2; exit 2; } + awk -F '\t' 'NR == 1 { valid = NF == 4 && ($1 == "github" || $1 == "gitlab") && $4 ~ /^[1-9][0-9]*$/ } END { if (NR != 1 || !valid) exit 1 }' "$ref" || { echo 'Invalid remote task association.' >&2; exit 2; } + tab="$(printf '\t')" + IFS="$tab" read -r platform host project issue_number < "$ref" + detached="$root/TaskFlowDocs/remote-todo-links/task-$task_id.detached" + if [ -e "$detached" ] || [ -L "$detached" ]; then + [ -f "$detached" ] && [ ! -L "$detached" ] || { echo 'Invalid task detach record.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$issue_number" ' + NR == 1 { valid = NF == 5 && $1 == "detached" && $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == n } + END { if (NR != 1 || !valid) exit 1 } + ' "$detached" || { echo 'Task detach record does not match the association.' >&2; exit 2; } + echo 'Task association is detached; no remote update.' + exit 0 + fi + [ -f "$config" ] && [ ! -L "$config" ] && validate_config "$config" validate >/dev/null || { echo 'Associated remote source is not configured.' >&2; exit 2; } + selected="$(awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" 'NR > 1 && $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) { print $1; found = 1; exit } END { if (!found) exit 1 }' "$config")" || { echo 'Associated remote source is not configured.' >&2; exit 2; } + exec bash "$0" stage "$selected" "$issue_number" "$stage" --root "$root" +fi +if [ ! -e "$config" ] && [ ! -L "$config" ]; then + case "$command" in mode) echo local ;; validate) echo 'OK local' ;; list) HERE="$(cd "$(dirname "$0")" && pwd)"; exec bash "$HERE/remote-todo-local-list" "$root" </dev/null ;; list-remote|create|migrate-to-remote|migrate-to-local|promote|edit|stage|stage-task|reattach-task|reattach-todo|label|link|resolve|resolve-absent|ack-update) echo 'No remote Todo repositories configured.' >&2; exit 2 ;; esac + exit 0 +fi +[ -f "$config" ] && [ ! -L "$config" ] || { echo 'Invalid remote Todo configuration.' >&2; exit 2; } +# The file has a mode line followed by tab-separated alias, platform, host and path. +# Never print rejected data: it might contain accidentally pasted credentials. +result="$(validate_config "$config" "$command")" +if [ "$command" = mode ] || [ "$command" = validate ]; then + printf '%s\n' "$result" + exit 0 +fi +if [ "$command" = reattach-todo ]; then + HERE="$(cd "$(dirname "$0")" && pwd)" + exec bash "$HERE/remote-todo-reattach-todo" "$local_id" "$selected" "$root" +fi +if [ "$command" = reattach-task ]; then + HERE="$(cd "$(dirname "$0")" && pwd)" + exec bash "$HERE/remote-todo-reattach" "$task_id" "$root" +fi +if [ "$command" = promote ]; then + HERE="$(cd "$(dirname "$0")" && pwd)" + exec bash "$HERE/remote-todo-promote" "$selected" "$issue_number" "$task_id" "$task_size" "$task_kind" "$root" +fi +if [ "$command" = migrate-to-local ]; then + HERE="$(cd "$(dirname "$0")" && pwd)" + exec bash "$HERE/remote-todo-to-local" "$selected" "$issue_number" "$root" "$detach" +fi +if [ "$command" = migrate-to-remote ]; then + HERE="$(cd "$(dirname "$0")" && pwd)" + exec bash "$HERE/remote-todo-migrate" "$local_id" "$selected" "$root" +fi +private_store() { + relative="$1" + if git -C "$root" rev-parse --is-inside-work-tree >/dev/null 2>&1; then + git -C "$root" check-ignore --no-index -q -- "$relative/$id" 2>/dev/null || { echo 'Private recovery storage is not Git-excluded; run hooks/install-git-hygiene before remote writes.' >&2; return 2; } + tracked="$(git -C "$root" ls-files -- "$relative")" || { echo 'Cannot inspect private recovery storage.' >&2; return 2; } + [ -z "$tracked" ] || { echo 'Recovery storage contains tracked files; inspect them before remote writes.' >&2; return 2; } + fi +} +if [ "$command" = link ]; then + [[ "$selected" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] && [[ "$issue_number" =~ ^[1-9][0-9]*$ ]] || { echo 'Invalid source or Issue number.' >&2; exit 2; } + HERE="$(cd "$(dirname "$0")" && pwd)" + bash "$HERE/task-id" "$task_id" || exit 2 + task_dir="$root/TaskFlowDocs/$task_id" + [ -d "$task_dir" ] && [ ! -L "$task_dir" ] && [ -f "$task_dir/prd.md" ] && [ -f "$task_dir/plan.md" ] || { echo 'Active task documents not found.' >&2; exit 2; } + target="$task_dir/remote-issue" + [ ! -e "$target" ] && [ ! -L "$target" ] || { echo 'Task already has a remote association.' >&2; exit 2; } + connection="$(awk -F '\t' -v name="$selected" 'NR > 1 && $1 == name { print; found = 1 } END { if (!found) exit 2 }' "$config")" || { echo 'Unknown source alias.' >&2; exit 2; } + tab="$(printf '\t')" + IFS="$tab" read -r alias platform host project <<EOF +$connection +EOF + if [ "$platform" = github ]; then + response="$(gh api "repos/$project/issues/$issue_number" --hostname "$host" --jq 'select(.pull_request == null)' 2>/dev/null)" || { echo 'Cannot read remote Issue.' >&2; exit 2; } + number_key=number + else + encoded="${project//\//%2F}" + response="$(glab api "projects/$encoded/issues/$issue_number" --hostname "$host" 2>/dev/null)" || { echo 'Cannot read remote Issue.' >&2; exit 2; } + number_key=iid + fi + actual="$(printf '%s' "$response" | bash "$HERE/json-field" --numbers "$number_key")" || { echo 'Cannot parse remote Issue.' >&2; exit 2; } + [ "$actual" = "$issue_number" ] || { echo 'Remote Issue identity mismatch.' >&2; exit 2; } + lock="$root/TaskFlowDocs/.remote-todo-link-lock" + mkdir "$lock" 2>/dev/null || { echo 'A remote association is already in progress or needs inspection.' >&2; exit 2; } + trap 'rmdir "$lock" 2>/dev/null || true' EXIT + for existing in "$root"/TaskFlowDocs/*/remote-issue "$root"/TaskFlowDocs/achieved/*/remote-issue; do + [ -e "$existing" ] || [ -L "$existing" ] || continue + [ -f "$existing" ] && [ ! -L "$existing" ] || { echo 'Invalid existing association.' >&2; exit 2; } + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$issue_number" ' + NR == 1 { same = NF == 4 && $1 == p && tolower($2) == tolower(h) && (p == "github" ? tolower($3) == tolower(r) : $3 == r) && $4 == n } + END { if (same) exit 0; exit 1 } + ' "$existing"; then + existing_task="${existing%/remote-issue}"; existing_task="${existing_task##*/}" + bash "$HERE/task-id" "$existing_task" >/dev/null 2>&1 || { echo 'Invalid existing task association.' >&2; exit 2; } + detached="$root/TaskFlowDocs/remote-todo-links/task-$existing_task.detached" + if [ -e "$detached" ] || [ -L "$detached" ]; then + [ -f "$detached" ] && [ ! -L "$detached" ] || { echo 'Invalid task detach record.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$issue_number" ' + NR == 1 { valid = NF == 5 && $1 == "detached" && $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == n } + END { if (NR != 1 || !valid) exit 1 } + ' "$detached" || { echo 'Task detach record does not match the association.' >&2; exit 2; } + continue + fi + echo 'Remote Issue already has a TaskFlow association.' >&2; exit 2 + fi + done + (set -o noclobber; printf '%s\t%s\t%s\t%s\n' "$platform" "$host" "$project" "$issue_number" > "$target") || { echo 'Cannot create association.' >&2; exit 2; } + printf 'Linked %s #%s to %s.\n' "$selected" "$issue_number" "$task_id" + exit 0 +fi +if [ "$command" = label ]; then + [[ "$selected" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] && [[ "$issue_number" =~ ^[1-9][0-9]*$ ]] || { echo 'Invalid source or Issue number.' >&2; exit 2; } + case "$action" in add|remove) ;; *) echo 'Invalid label operation.' >&2; exit 2 ;; esac + [ -n "$label" ] && [[ "$label" != *[[:cntrl:]]* ]] || { echo 'Invalid label.' >&2; exit 2; } + case "$label" in taskflow:*) echo 'TaskFlow stage labels are managed by the stage command.' >&2; exit 2 ;; esac + connection="$(awk -F '\t' -v name="$selected" 'NR > 1 && $1 == name { print; found = 1 } END { if (!found) exit 2 }' "$config")" || { echo 'Unknown source alias.' >&2; exit 2; } + tab="$(printf '\t')" + IFS="$tab" read -r alias platform host project <<EOF +$connection +EOF + if [ "$platform" = github ]; then endpoint="repos/$project/issues/$issue_number"; number_key=number + else + case "$label" in *,*) echo 'GitLab label names containing commas cannot be changed safely through this interface.' >&2; exit 2 ;; esac + encoded="${project//\//%2F}"; endpoint="projects/$encoded/issues/$issue_number"; number_key=iid + fi + HERE="$(cd "$(dirname "$0")" && pwd)" + label_get() { + if [ "$platform" = github ]; then gh api "$endpoint" --hostname "$host" --jq 'select(.pull_request == null) | .labels |= map(.name)' 2>/dev/null + else glab api "$endpoint" --hostname "$host" 2>/dev/null; fi + } + before="$(label_get)" || { echo 'Cannot read remote Issue; label not changed.' >&2; exit 2; } + actual="$(printf '%s' "$before" | bash "$HERE/json-field" --numbers "$number_key")" || { echo 'Cannot parse remote Issue.' >&2; exit 2; } + [ "$actual" = "$issue_number" ] || { echo 'Remote Issue identity mismatch.' >&2; exit 2; } + has="$(printf '%s' "$before" | bash "$HERE/json-field" --array-has labels "$label")" || { echo 'Cannot parse Issue labels.' >&2; exit 2; } + if { [ "$action" = add ] && [ "$has" = yes ]; } || { [ "$action" = remove ] && [ "$has" = no ]; }; then echo 'Label already has the requested state.'; exit 0; fi + id="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" + [[ "$id" =~ ^[0-9a-f]{32}$ ]] || { echo 'Cannot allocate operation ID.' >&2; exit 2; } + private_store TaskFlowDocs/.remote-todo-updates || exit 2 + store="$root/TaskFlowDocs/.remote-todo-updates" + umask 077 + [ ! -L "$store" ] || { echo 'Invalid operation directory.' >&2; exit 2; } + mkdir -p "$store" + [ -d "$store" ] && [ ! -L "$store" ] || { echo 'Invalid operation directory.' >&2; exit 2; } + normalized_host="$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" + normalized_project="$project" + if [ "$platform" = github ]; then normalized_project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')"; fi + lock_id="$(printf '%s\n%s\n%s\n%s\nlabel\n' "$platform" "$normalized_host" "$normalized_project" "$issue_number" | cksum | awk '{ print $1 "-" $2 }')" + lock="$store/.edit-$lock_id" + mkdir "$lock" 2>/dev/null || { echo 'A label update is already in progress or needs inspection.' >&2; exit 2; } + trap 'rmdir "$lock" 2>/dev/null || true' EXIT + for existing in "$store"/*; do + [ -e "$existing" ] || [ -L "$existing" ] || continue + [ -f "$existing" ] && [ ! -L "$existing" ] || { echo 'Invalid operation record.' >&2; exit 2; } + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$issue_number" ' + NR == 1 { valid = NF == 7 && $1 == "pending"; same = $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == n && $6 == "label" } + NR == 2 && ($1 == "confirmed" || $1 == "reviewed") { resolved = 1 } + END { if (!valid || (same && !resolved)) exit 0; exit 1 } + ' "$existing"; then echo 'Unresolved label update exists; inspect it before retrying.' >&2; exit 2; fi + done + record="$store/$id" + (set -o noclobber; printf 'pending\t%s\t%s\t%s\t%s\tlabel\t%s\n' "$platform" "$host" "$project" "$issue_number" "$id" > "$record") || { echo 'Cannot save operation intent.' >&2; exit 2; } + echo 'Updating one label; GitLab may create a missing label. Concurrent edits may race.' >&2 + if [ "$platform" = github ] && [ "$action" = add ]; then + gh api "$endpoint/labels" --hostname "$host" --method POST --raw-field "labels[]=$label" >/dev/null 2>&1 || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + elif [ "$platform" = github ]; then + encoded_label='' + LC_ALL=C + for ((i=0; i<${#label}; i++)); do + c="${label:i:1}" + case "$c" in + [A-Za-z0-9._~-]) encoded_label="$encoded_label$c" ;; + *) printf -v byte '%d' "'$c"; printf -v hex '%%%02X' "$((byte & 255))"; encoded_label="$encoded_label$hex" ;; + esac + done + gh api "$endpoint/labels/$encoded_label" --hostname "$host" --method DELETE >/dev/null 2>&1 || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + else + glab api "$endpoint" --hostname "$host" --method PUT --raw-field "${action}_labels=$label" >/dev/null 2>&1 || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + fi + after="$(label_get)" || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + actual="$(printf '%s' "$after" | bash "$HERE/json-field" --numbers "$number_key")" || { echo "Result unknown for operation $id." >&2; exit 2; } + [ "$actual" = "$issue_number" ] || { echo "Result unknown for operation $id: identity mismatch." >&2; exit 2; } + has="$(printf '%s' "$after" | bash "$HERE/json-field" --array-has labels "$label")" || { echo "Result unknown for operation $id: invalid labels." >&2; exit 2; } + if { [ "$action" = add ] && [ "$has" != yes ]; } || { [ "$action" = remove ] && [ "$has" != no ]; }; then echo "Result unknown for operation $id: label state mismatch." >&2; exit 2; fi + printf 'confirmed\n' >> "$record" || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + printf 'Updated label on %s #%s (operation %s).\n' "$selected" "$issue_number" "$id" + exit 0 +fi +if [ "$command" = stage ]; then + [[ "$selected" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] && [[ "$issue_number" =~ ^[1-9][0-9]*$ ]] || { echo 'Invalid source or Issue number.' >&2; exit 2; } + case "$stage" in inbox|clarified|promoted|planning|ready|in_progress|checking|blocked|done|cancelled) ;; *) echo 'Invalid TaskFlow stage.' >&2; exit 2 ;; esac + connection="$(awk -F '\t' -v name="$selected" 'NR > 1 && $1 == name { print; found = 1 } END { if (!found) exit 2 }' "$config")" || { echo 'Unknown source alias.' >&2; exit 2; } + tab="$(printf '\t')" + IFS="$tab" read -r alias platform host project <<EOF +$connection +EOF + if [ "$platform" = github ]; then endpoint="repos/$project/issues/$issue_number"; number_key=number + else encoded="${project//\//%2F}"; endpoint="projects/$encoded/issues/$issue_number"; number_key=iid; fi + HERE="$(cd "$(dirname "$0")" && pwd)" + stage_get() { + if [ "$platform" = github ]; then gh api "$endpoint" --hostname "$host" --jq 'select(.pull_request == null) | .labels |= map(.name)' 2>/dev/null + else glab api "$endpoint" --hostname "$host" 2>/dev/null; fi + } + before="$(stage_get)" || { echo 'Cannot read remote Issue; stage not changed.' >&2; exit 2; } + actual="$(printf '%s' "$before" | bash "$HERE/json-field" --numbers "$number_key")" || { echo 'Cannot parse remote Issue.' >&2; exit 2; } + [ "$actual" = "$issue_number" ] || { echo 'Remote Issue identity mismatch.' >&2; exit 2; } + present='' + for item in inbox clarified promoted planning ready in_progress checking blocked done cancelled; do + has="$(printf '%s' "$before" | bash "$HERE/json-field" --array-has labels "taskflow:$item")" || { echo 'Cannot parse Issue labels.' >&2; exit 2; } + if [ "$has" = yes ] && [ "$item" != "$stage" ]; then present="$present $item"; fi + done + desired="taskflow:$stage" + desired_state=open; event=reopen + case "$stage" in done|cancelled) desired_state=closed; event=close ;; esac + id="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" + [[ "$id" =~ ^[0-9a-f]{32}$ ]] || { echo 'Cannot allocate operation ID.' >&2; exit 2; } + private_store TaskFlowDocs/.remote-todo-updates || exit 2 + store="$root/TaskFlowDocs/.remote-todo-updates" + umask 077 + [ ! -L "$store" ] || { echo 'Invalid operation directory.' >&2; exit 2; } + mkdir -p "$store" + [ -d "$store" ] && [ ! -L "$store" ] || { echo 'Invalid operation directory.' >&2; exit 2; } + normalized_host="$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" + normalized_project="$project" + if [ "$platform" = github ]; then normalized_project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')"; fi + lock_id="$(printf '%s\n%s\n%s\n%s\nstage\n' "$platform" "$normalized_host" "$normalized_project" "$issue_number" | cksum | awk '{ print $1 "-" $2 }')" + lock="$store/.edit-$lock_id" + mkdir "$lock" 2>/dev/null || { echo 'A stage update is already in progress or needs inspection.' >&2; exit 2; } + trap 'rmdir "$lock" 2>/dev/null || true' EXIT + for existing in "$store"/*; do + [ -e "$existing" ] || [ -L "$existing" ] || continue + [ -f "$existing" ] && [ ! -L "$existing" ] || { echo 'Invalid operation record.' >&2; exit 2; } + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$issue_number" ' + NR == 1 { valid = NF == 7 && $1 == "pending"; same = $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == n && $6 == "stage" } + NR == 2 && ($1 == "confirmed" || $1 == "reviewed") { resolved = 1 } + END { if (!valid || (same && !resolved)) exit 0; exit 1 } + ' "$existing"; then echo 'Unresolved stage update exists; inspect it before retrying.' >&2; exit 2; fi + done + record="$store/$id" + (set -o noclobber; printf 'pending\t%s\t%s\t%s\t%s\tstage\t%s\n' "$platform" "$host" "$project" "$issue_number" "$id" > "$record") || { echo 'Cannot save operation intent.' >&2; exit 2; } + echo 'Updating TaskFlow labels and Issue state; existing non-TaskFlow labels are retained. Missing labels may need creation or permissions; concurrent edits may race.' >&2 + if [ "$platform" = github ]; then + has="$(printf '%s' "$before" | bash "$HERE/json-field" --array-has labels "$desired")" || { echo "Result unknown for operation $id: invalid labels." >&2; exit 2; } + if [ "$has" != yes ]; then + label_endpoint="repos/$project/labels" + label_get() { gh api "$label_endpoint?per_page=100" --hostname "$host" --paginate --jq ".[] | select(.name == \"$desired\") | .name" 2>/dev/null; } + available="$(label_get)" || { echo "Cannot read repository labels; operation $id needs inspection." >&2; exit 2; } + if [ "$available" != "$desired" ]; then + echo "Creating repository label $desired if missing." >&2 + if ! gh api "$label_endpoint" --hostname "$host" --method POST --raw-field "name=$desired" --raw-field 'color=ededed' >/dev/null 2>&1; then + available="$(label_get)" || available='' + [ "$available" = "$desired" ] || { echo "Result unknown for operation $id: label creation failed; inspect before retrying." >&2; exit 2; } + fi + fi + fi + for item in $present; do + gh api "$endpoint/labels/taskflow%3A$item" --hostname "$host" --method DELETE >/dev/null 2>&1 || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + done + gh api "$endpoint/labels" --hostname "$host" --method POST --raw-field "labels[]=$desired" >/dev/null 2>&1 || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + gh api "$endpoint" --hostname "$host" --method PATCH --raw-field "state=$desired_state" >/dev/null 2>&1 || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + else + remove='' + for item in $present; do if [ -n "$remove" ]; then remove="$remove,"; fi; remove="${remove}taskflow:$item"; done + glab api "$endpoint" --hostname "$host" --method PUT --raw-field "add_labels=$desired" --raw-field "remove_labels=$remove" --raw-field "state_event=$event" >/dev/null 2>&1 || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + fi + after="$(stage_get)" || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + actual="$(printf '%s' "$after" | bash "$HERE/json-field" --numbers "$number_key")" || { echo "Result unknown for operation $id." >&2; exit 2; } + [ "$actual" = "$issue_number" ] || { echo "Result unknown for operation $id: identity mismatch." >&2; exit 2; } + actual="$(printf '%s' "$after" | bash "$HERE/json-field" state)" || { echo "Result unknown for operation $id." >&2; exit 2; } + if [ "$platform" = gitlab ] && [ "$desired_state" = open ]; then desired_state=opened; fi + [ "$actual" = "$desired_state" ] || { echo "Result unknown for operation $id: state mismatch." >&2; exit 2; } + for item in inbox clarified promoted planning ready in_progress checking blocked done cancelled; do + has="$(printf '%s' "$after" | bash "$HERE/json-field" --array-has labels "taskflow:$item")" || { echo "Result unknown for operation $id: invalid labels." >&2; exit 2; } + if { [ "$item" = "$stage" ] && [ "$has" != yes ]; } || { [ "$item" != "$stage" ] && [ "$has" = yes ]; }; then echo "Result unknown for operation $id: stage mismatch." >&2; exit 2; fi + done + printf 'confirmed\n' >> "$record" || { echo "Result unknown for operation $id; inspect the Issue before retrying." >&2; exit 2; } + printf 'Updated stage for %s #%s (operation %s).\n' "$selected" "$issue_number" "$id" + exit 0 +fi +if [ "$command" = ack-update ]; then + [[ "$resolve_id" =~ ^[0-9a-f]{32}$ ]] || { echo 'Invalid operation ID.' >&2; exit 2; } + record="$root/TaskFlowDocs/.remote-todo-updates/$resolve_id" + [ -f "$record" ] && [ ! -L "$record" ] && [ ! -L "$root/TaskFlowDocs/.remote-todo-updates" ] || { echo 'Operation record not found.' >&2; exit 2; } + if ! awk -F '\t' -v id="$resolve_id" 'NR == 1 { valid = NF == 7 && $1 == "pending" && $7 == id } NR > 1 { resolved = 1 } END { if (!valid || resolved) exit 1 }' "$record"; then echo 'Invalid or resolved operation.' >&2; exit 2; fi + printf 'reviewed\n' >> "$record" || { echo 'Cannot save the decision.' >&2; exit 2; } + echo 'Recorded user review; no remote Issue was changed.' + exit 0 +fi +if [ "$command" = edit ]; then + [[ "$selected" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] && [[ "$issue_number" =~ ^[1-9][0-9]*$ ]] || { echo 'Invalid source or Issue number.' >&2; exit 2; } + case "$field" in title) [ -n "$value" ] || { echo 'Title must not be empty.' >&2; exit 2; } ;; body) [ -f "$value" ] && [ -r "$value" ] || { echo 'Body must be a readable file.' >&2; exit 2; } ;; *) echo 'Only title and body can be edited.' >&2; exit 2 ;; esac + connection="$(awk -F '\t' -v name="$selected" 'NR > 1 && $1 == name { print; found = 1 } END { if (!found) exit 2 }' "$config")" || { echo 'Unknown source alias.' >&2; exit 2; } + tab="$(printf '\t')" + IFS="$tab" read -r alias platform host project <<EOF +$connection +EOF + if [ "$platform" = github ]; then endpoint="repos/$project/issues/$issue_number"; cli=gh; method=PATCH; number_key=number; body_key=body + else encoded="${project//\//%2F}"; endpoint="projects/$encoded/issues/$issue_number"; cli=glab; method=PUT; number_key=iid; body_key=description; fi + HERE="$(cd "$(dirname "$0")" && pwd)" + get_issue() { + if [ "$platform" = github ]; then gh api "$endpoint" --hostname "$host" --jq 'select(.pull_request == null)' 2>/dev/null + else glab api "$endpoint" --hostname "$host" 2>/dev/null; fi + } + before="$(get_issue)" || { echo 'Cannot read remote Issue; edit not started.' >&2; exit 2; } + actual="$(printf '%s' "$before" | bash "$HERE/json-field" --numbers "$number_key")" || { echo 'Cannot parse remote Issue; edit not started.' >&2; exit 2; } + [ "$actual" = "$issue_number" ] || { echo 'Remote Issue identity mismatch; edit not started.' >&2; exit 2; } + retained_markers='' + if [ "$field" = title ]; then expected="$value"; api_field=title + else + expected="$(cat "$value")" || { echo 'Cannot read body file.' >&2; exit 2; }; api_field="$body_key" + original_body="$(printf '%s' "$before" | bash "$HERE/json-field" "$body_key")" || { echo 'Cannot parse remote body; edit not started.' >&2; exit 2; } + markers="$(printf '%s\n' "$original_body" | grep -oE '<!-- taskflow-migration:(TF|T)-[0-9]{8}-[a-z0-9]+ -->' || true)" + if [ -n "$markers" ]; then + while IFS= read -r marker; do + case "$expected" in *"$marker"*) ;; *) retained_markers="${retained_markers}${retained_markers:+ +}$marker" ;; esac + done <<EOF +$markers +EOF + fi + if [ -n "$retained_markers" ]; then expected="$({ cat "$value"; printf '\n\n%s\n' "$retained_markers"; })"; fi + fi + id="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')" + [[ "$id" =~ ^[0-9a-f]{32}$ ]] || { echo 'Cannot allocate operation ID.' >&2; exit 2; } + private_store TaskFlowDocs/.remote-todo-updates || exit 2 + store="$root/TaskFlowDocs/.remote-todo-updates" + umask 077 + [ ! -L "$store" ] || { echo 'Invalid operation directory.' >&2; exit 2; } + mkdir -p "$store" + [ -d "$store" ] && [ ! -L "$store" ] || { echo 'Invalid operation directory.' >&2; exit 2; } + normalized_host="$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" + normalized_project="$project" + if [ "$platform" = github ]; then normalized_project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')"; fi + lock_id="$(printf '%s\n%s\n%s\n%s\n%s\n' "$platform" "$normalized_host" "$normalized_project" "$issue_number" "$field" | cksum | awk '{ print $1 "-" $2 }')" + lock="$store/.edit-$lock_id" + mkdir "$lock" 2>/dev/null || { echo 'An edit for this field is already in progress or needs inspection.' >&2; exit 2; } + trap 'rmdir "$lock" 2>/dev/null || true' EXIT + for existing in "$store"/*; do + [ -e "$existing" ] || [ -L "$existing" ] || continue + [ -f "$existing" ] && [ ! -L "$existing" ] || { echo 'Invalid operation record.' >&2; exit 2; } + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$issue_number" -v f="$field" ' + NR == 1 { valid = NF == 7 && $1 == "pending"; same = $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == n && $6 == f } + NR == 2 && ($1 == "confirmed" || $1 == "reviewed") { resolved = 1 } + END { if (!valid || (same && !resolved)) exit 0; exit 1 } + ' "$existing"; then echo 'Unresolved update exists; inspect it before editing this field.' >&2; exit 2; fi + done + record="$store/$id" + (set -o noclobber; printf 'pending\t%s\t%s\t%s\t%s\t%s\t%s\n' "$platform" "$host" "$project" "$issue_number" "$field" "$id" > "$record") || { echo 'Cannot save operation intent.' >&2; exit 2; } + echo 'Editing only the requested field; concurrent edits can still be overwritten.' >&2 + if [ "$field" = title ]; then + if response="$("$cli" api "$endpoint" --hostname "$host" --method "$method" --raw-field "title=$value" 2>/dev/null)"; then : + else echo "Result unknown for operation $id; inspect the remote Issue before retrying." >&2; exit 2; fi + else + if response="$({ cat "$value"; if [ -n "$retained_markers" ]; then printf '\n\n%s\n' "$retained_markers"; fi; } | "$cli" api "$endpoint" --hostname "$host" --method "$method" --field "$api_field=@-" 2>/dev/null)"; then : + else echo "Result unknown for operation $id; inspect the remote Issue before retrying." >&2; exit 2; fi + fi + after="$(get_issue)" || { echo "Result unknown for operation $id; inspect the remote Issue before retrying." >&2; exit 2; } + actual="$(printf '%s' "$after" | bash "$HERE/json-field" --numbers "$number_key")" || { echo "Result unknown for operation $id." >&2; exit 2; } + [ "$actual" = "$issue_number" ] || { echo "Result unknown for operation $id: Issue identity changed." >&2; exit 2; } + actual="$(printf '%s' "$after" | bash "$HERE/json-field" "$api_field")" || { echo "Result unknown for operation $id." >&2; exit 2; } + [ "$actual" = "$expected" ] || { echo "Result unknown for operation $id: requested field differs after write." >&2; exit 2; } + printf 'confirmed\n' >> "$record" || { echo "Result unknown for operation $id; inspect the remote Issue before retrying." >&2; exit 2; } + printf 'Updated %s #%s (operation %s).\n' "$selected" "$issue_number" "$id" + exit 0 +fi +if [ "$command" = resolve-absent ]; then + [[ "$resolve_id" =~ ^[0-9a-f]{32}$ ]] || { echo 'Invalid operation ID.' >&2; exit 2; } + record="$root/TaskFlowDocs/.remote-todo-operations/$resolve_id" + [ -f "$record" ] && [ ! -L "$record" ] && [ ! -L "$root/TaskFlowDocs/.remote-todo-operations" ] || { echo 'Operation record not found.' >&2; exit 2; } + tab="$(printf '\t')" + IFS="$tab" read -r state platform host project saved_id < "$record" + [ "$state" = pending ] && [ "$saved_id" = "$resolve_id" ] || { echo 'Invalid operation record.' >&2; exit 2; } + if awk -F '\t' 'NR > 1 && ($1 == "confirmed" || $1 == "absent") { found = 1 } END { if (!found) exit 1 }' "$record"; then + echo 'Operation already resolved.' >&2; exit 2 + fi + printf 'absent\tuser-checked\n' >> "$record" || { echo 'Cannot save the decision.' >&2; exit 2; } + echo 'Recorded user-confirmed absence; no remote Issue was changed.' + exit 0 +fi +if [ "$command" = resolve ]; then + [[ "$resolve_id" =~ ^[0-9a-f]{32}$ ]] && [[ "$resolve_number" =~ ^[1-9][0-9]*$ ]] || { echo 'Invalid operation ID or Issue number.' >&2; exit 2; } + record="$root/TaskFlowDocs/.remote-todo-operations/$resolve_id" + [ -f "$record" ] && [ ! -L "$record" ] && [ ! -L "$root/TaskFlowDocs/.remote-todo-operations" ] || { echo 'Operation record not found.' >&2; exit 2; } + tab="$(printf '\t')" + IFS="$tab" read -r state platform host project saved_id < "$record" + [ "$state" = pending ] && [ "$saved_id" = "$resolve_id" ] || { echo 'Invalid operation record.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" 'NR > 1 && $2 == p && $3 == h && $4 == r { found = 1 } END { if (!found) exit 1 }' "$config" || { echo 'Operation source is not configured.' >&2; exit 2; } + if awk -F '\t' 'NR > 1 && ($1 == "confirmed" || $1 == "absent") { found = 1 } END { if (!found) exit 1 }' "$record"; then + echo 'Operation already confirmed.' >&2; exit 2 + fi + if [ "$platform" = github ]; then + response="$(gh api "repos/$project/issues/$resolve_number" --hostname "$host" --jq 'select(.pull_request == null)' 2>/dev/null)" || { echo 'Cannot verify remote Issue.' >&2; exit 2; } + number_key=number; body_key=body + else + encoded="${project//\//%2F}" + response="$(glab api "projects/$encoded/issues/$resolve_number" --hostname "$host" 2>/dev/null)" || { echo 'Cannot verify remote Issue.' >&2; exit 2; } + number_key=iid; body_key=description + fi + HERE="$(cd "$(dirname "$0")" && pwd)" + actual="$(printf '%s' "$response" | bash "$HERE/json-field" --numbers "$number_key")" || { echo 'Cannot parse remote Issue.' >&2; exit 2; } + body_value="$(printf '%s' "$response" | bash "$HERE/json-field" "$body_key")" || { echo 'Cannot parse remote Issue.' >&2; exit 2; } + [ "$actual" = "$resolve_number" ] || { echo 'Remote Issue identity mismatch.' >&2; exit 2; } + case "$body_value" in *"<!-- taskflow-operation:$resolve_id -->"*) ;; *) echo 'Remote Issue operation marker mismatch.' >&2; exit 2 ;; esac + printf 'confirmed\t%s\n' "$resolve_number" >> "$record" || { echo 'Cannot confirm operation.' >&2; exit 2; } + printf 'Operation %s confirmed as Issue #%s.\n' "$resolve_id" "$resolve_number" + exit 0 +fi +if [ "$command" = create ]; then + [[ "$selected" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] || { echo 'Invalid source alias.' >&2; exit 2; } + [ -n "$title" ] && { [ "$body" = - ] || { [ -f "$body" ] && [ -r "$body" ]; }; } || { echo 'A title and readable body file are required.' >&2; exit 2; } + connection="$(awk -F '\t' -v name="$selected" 'NR > 1 && $1 == name { print; found = 1 } END { if (!found) exit 2 }' "$config")" || { echo 'Unknown source alias.' >&2; exit 2; } + tab="$(printf '\t')" + IFS="$tab" read -r alias platform host project <<EOF +$connection +EOF + id="$supplied_id" + if [ -z "$id" ]; then id="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"; fi + [[ "$id" =~ ^[0-9a-f]{32}$ ]] || { echo 'Cannot allocate operation ID.' >&2; exit 2; } + private_store TaskFlowDocs/.remote-todo-operations || exit 2 + store="$root/TaskFlowDocs/.remote-todo-operations" + umask 077 + [ ! -L "$store" ] || { echo 'Invalid operation directory.' >&2; exit 2; } + mkdir -p "$store" + [ -d "$store" ] && [ ! -L "$store" ] || { echo 'Invalid operation directory.' >&2; exit 2; } + normalized_host="$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" + normalized_project="$project" + if [ "$platform" = github ]; then normalized_project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')"; fi + lock_id="$(printf '%s\n%s\n%s\n' "$platform" "$normalized_host" "$normalized_project" | cksum | awk '{ print $1 "-" $2 }')" + lock="$store/.create-$lock_id" + # Only the empty directory created by this process is removed; crash locks are retained. + mkdir "$lock" 2>/dev/null || { echo 'A creation for this source is already in progress or needs inspection.' >&2; exit 2; } + trap 'rmdir "$lock" 2>/dev/null || true' EXIT + for existing in "$store"/*; do + [ -e "$existing" ] || [ -L "$existing" ] || continue + [ -f "$existing" ] && [ ! -L "$existing" ] || { echo 'Invalid operation record.' >&2; exit 2; } + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" ' + NR == 1 { valid = NF == 5 && $1 == "pending"; same = $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) } + NR == 2 && NF == 2 && (($1 == "confirmed" && $2 ~ /^[1-9][0-9]*$/) || ($1 == "absent" && $2 == "user-checked")) { confirmed = 1 } + END { if (!valid || (same && !confirmed)) exit 0; exit 1 } + ' "$existing"; then + echo 'Unresolved creation operation exists; inspect and resolve it before creating another Issue in this source.' >&2 + exit 2 + fi + done + record="$store/$id" + (set -o noclobber; printf 'pending\t%s\t%s\t%s\t%s\n' "$platform" "$host" "$project" "$id" > "$record") || { echo 'Cannot save operation intent.' >&2; exit 2; } + echo 'Creating remote Issue; concurrent changes may not be detected.' >&2 + if [ "$platform" = github ]; then + if response="$({ cat "$body"; printf '\n\n<!-- taskflow-operation:%s -->\n' "$id"; } | gh api "repos/$project/issues" --hostname "$host" --method POST --raw-field "title=$title" --field body=@- 2>/dev/null)"; then : + else echo "Result unknown for operation $id; inspect the remote Issue before retrying." >&2; exit 2; fi + key=number + else + encoded="${project//\//%2F}" + if response="$({ cat "$body"; printf '\n\n<!-- taskflow-operation:%s -->\n' "$id"; } | glab api "projects/$encoded/issues" --hostname "$host" --method POST --raw-field "title=$title" --field description=@- 2>/dev/null)"; then : + else echo "Result unknown for operation $id; inspect the remote Issue before retrying." >&2; exit 2; fi + key=iid + fi + HERE="$(cd "$(dirname "$0")" && pwd)" + number="$(printf '%s' "$response" | bash "$HERE/json-field" --numbers "$key")" || { echo "Result unknown for operation $id; inspect the remote Issue before retrying." >&2; exit 2; } + [[ "$number" =~ ^[1-9][0-9]*$ ]] || { echo "Result unknown for operation $id; inspect the remote Issue before retrying." >&2; exit 2; } + printf 'confirmed\t%s\n' "$number" >> "$record" || { echo "Result unknown for operation $id; inspect the remote Issue before retrying." >&2; exit 2; } + printf 'Created %s #%s (operation %s).\n' "$selected" "$number" "$id" + exit 0 +fi +connections="$(awk 'NR > 1 { print }' "$config")" +failed=0 +seen='' +HERE="$(cd "$(dirname "$0")" && pwd)" +if [ -n "$connections" ]; then + tab="$(printf '\t')" + while IFS="$tab" read -r alias platform host project; do + printf 'SOURCE %s %s %s %s\n' "$alias" "$platform" "$host" "$project" + if [ "$platform" = github ]; then + if output="$(gh api "repos/$project/issues?state=all&per_page=100" --hostname "$host" --paginate --jq '.[] | select(.pull_request == null) | @json' 2>/dev/null)"; then number_key=number + else printf 'Incomplete: could not read source %s.\n' "$alias" >&2; failed=1; continue; fi + else + encoded="${project//\//%2F}" + if output="$(glab api "projects/$encoded/issues?scope=all&state=all&per_page=100" --hostname "$host" --paginate --output ndjson 2>/dev/null)"; then number_key=iid + else printf 'Incomplete: could not read source %s.\n' "$alias" >&2; failed=1; continue; fi + fi + source_keys=''; new_output=''; valid=1 + if [ -n "$output" ]; then + normalized_host="$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" + normalized_project="$project" + if [ "$platform" = github ]; then normalized_project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')"; fi + while IFS= read -r item; do + number="$(printf '%s' "$item" | bash "$HERE/json-field" --numbers "$number_key" 2>/dev/null)" || { valid=0; break; } + if ! [[ "$number" =~ ^[1-9][0-9]*$ ]]; then valid=0; break; fi + key="$platform|$normalized_host|$normalized_project|$number" + case " +$seen$source_keys +" in *" +$key +"*) continue ;; esac + source_keys="$source_keys +$key" + if [ -n "$new_output" ]; then new_output="$new_output +$item"; else new_output="$item"; fi + done <<EOF +$output +EOF + fi + if [ "$valid" -ne 1 ]; then printf 'Incomplete: invalid response from source %s.\n' "$alias" >&2; failed=1; continue; fi + [ -z "$new_output" ] || printf '%s\n' "$new_output" + seen="$seen$source_keys" + done <<EOF +$connections +EOF +fi +if [ "$command" = list ]; then printf '%s\n' "$seen" | bash "$HERE/remote-todo-local-list" "$root"; fi +if [ "$command" = list-remote ] && [ -z "$connections" ]; then echo 'No remote Todo repositories configured.' >&2; exit 2; fi +[ "$failed" -eq 0 ] || exit 2 \ No newline at end of file diff --git a/hooks/remote-todo-local-list b/hooks/remote-todo-local-list new file mode 100644 index 0000000..201a548 --- /dev/null +++ b/hooks/remote-todo-local-list @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +# List local Todo entries not represented by a successfully read remote Issue. +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: remote-todo-local-list <root>' >&2; exit 2; } +root="$1" +todo="$root/TaskFlowDocs/todo.md" +[ -f "$todo" ] && [ ! -L "$todo" ] || exit 0 +HERE="$(cd "$(dirname "$0")" && pwd)" +seen="$(cat)" +tab="$(printf '\t')" +contains() { + case " +$seen +" in *" +$1 +"*) return 0 ;; *) return 1 ;; esac +} +key_for() { + local p="$1" h="$2" r="$3" n="$4" + case "$p" in github|gitlab) ;; *) return 1 ;; esac + [[ "$h" =~ ^[A-Za-z0-9][A-Za-z0-9.:-]*$ ]] && [[ "$r" =~ ^[A-Za-z0-9_./-]+$ ]] && [[ "$n" =~ ^[1-9][0-9]*$ ]] || return 1 + h="$(printf '%s' "$h" | tr '[:upper:]' '[:lower:]')" + if [ "$p" = github ]; then r="$(printf '%s' "$r" | tr '[:upper:]' '[:lower:]')"; fi + printf '%s|%s|%s|%s' "$p" "$h" "$r" "$n" +} +awk ' + function flush() { + if (id == "") return + if (status == "") status = "unknown" + if (goal == "") goal = "(no goal)" + gsub(/[[:cntrl:]]/, "", id); gsub(/[[:cntrl:]]/, "", status); gsub(/[[:cntrl:]]/, "", goal); gsub(/[[:cntrl:]]/, "", task) + print id "\t" status "\t" goal "\t" task + } + /^## / { flush(); id = status = goal = task = ""; next } + /^- ID: / { id = substr($0, 7); next } + /^- Status: / { status = substr($0, 11); next } + /^- Goal: / { goal = substr($0, 9); next } + /^- Task: / { task = substr($0, 9); next } + END { flush() } +' "$todo" | while IFS="$tab" read -r id status goal task; do + [[ "$id" =~ ^(TF|T)-[0-9]{8}-[a-z0-9]+$ ]] || continue + key='' + mapping="$root/TaskFlowDocs/remote-todo-links/$id" + if [ -f "$mapping" ] && [ ! -L "$mapping" ] && [ -f "$mapping.detached" ] && [ ! -L "$mapping.detached" ]; then + IFS="$tab" read -r detached copy_id < "$mapping.detached" + if [ "$detached" = detached ] && [[ "$copy_id" =~ ^(TF|T)-[0-9]{8}-[a-z0-9]+$ ]] && [ "$copy_id" != "$id" ]; then + header="$(awk -F '\t' 'NR == 1 && NF == 5 && $1 == "pending" { p = $3; h = $4; r = $5 } $1 == "confirmed" && NF == 2 && $2 ~ /^[1-9][0-9]*$/ { n = $2 } END { if (p != "" && n != "") print p "\t" h "\t" r "\t" n }' "$mapping")" + if [ -n "$header" ]; then + IFS="$tab" read -r p h r n <<EOF +$header +EOF + source_key="$(key_for "$p" "$h" "$r" "$n")" || source_key='' + if [ -n "$source_key" ]; then + h="$(printf '%s' "$h" | tr '[:upper:]' '[:lower:]')" + if [ "$p" = github ]; then r="$(printf '%s' "$r" | tr '[:upper:]' '[:lower:]')"; url="https://$h/$r/issues/$n" + else url="https://$h/$r/-/issues/$n"; fi + if TASKFLOW_COPY_ID="$copy_id" TASKFLOW_COPY_URL="$url" awk ' + function flush() { if (inside && found_id) { matches++; if (found_url && found_end) complete++ } } + /^## / { flush(); inside = 1; found_id = found_url = found_end = 0 } + inside && $0 == "- ID: " ENVIRON["TASKFLOW_COPY_ID"] { found_id = 1 } + inside && $0 == "- External: " ENVIRON["TASKFLOW_COPY_URL"] { found_url = 1 } + inside && $0 == " <!-- taskflow-copy-complete:" ENVIRON["TASKFLOW_COPY_ID"] " -->" { found_end = 1 } + END { flush(); if (matches == 1 && complete == 1) exit 0; exit 1 } + ' "$todo"; then continue; fi + fi + fi + fi + fi + if [ -f "$mapping" ] && [ ! -L "$mapping" ] && [ ! -e "$mapping.detached" ]; then + header="$(awk -F '\t' 'NR == 1 && NF == 5 && $1 == "pending" { p = $3; h = $4; r = $5 } $1 == "confirmed" && NF == 2 && $2 ~ /^[1-9][0-9]*$/ { n = $2 } ($1 == "stage-synced" || $1 == "remote-authoritative") && NF == 2 { synced = 1 } END { if (p != "" && n != "" && synced) print p "\t" h "\t" r "\t" n }' "$mapping")" + if [ -n "$header" ]; then + IFS="$tab" read -r p h r n <<EOF +$header +EOF + key="$(key_for "$p" "$h" "$r" "$n")" || key='' + fi + fi + case "$task" in + \`TaskFlowDocs/*/\`) + task_id="${task#\`TaskFlowDocs/}"; task_id="${task_id%/\`}" + if [ -z "$key" ] && bash "$HERE/task-id" "$task_id" >/dev/null 2>&1; then + ref="$root/TaskFlowDocs/$task_id/remote-issue" + if [ -f "$ref" ] && [ ! -L "$ref" ] && [ ! -e "$root/TaskFlowDocs/remote-todo-links/task-$task_id.detached" ] && [ ! -L "$root/TaskFlowDocs/remote-todo-links/task-$task_id.detached" ]; then + IFS="$tab" read -r p h r n < "$ref" + key="$(key_for "$p" "$h" "$r" "$n")" || key='' + fi + fi ;; + esac + if [ -n "$key" ] && contains "$key"; then continue; fi + printf 'LOCAL %s [%s] %s\n' "$id" "$status" "$goal" +done \ No newline at end of file diff --git a/hooks/remote-todo-migrate b/hooks/remote-todo-migrate new file mode 100644 index 0000000..8a1466e --- /dev/null +++ b/hooks/remote-todo-migrate @@ -0,0 +1,145 @@ +#!/usr/bin/env bash +# Migrate one local Todo without changing or removing the original entry. +set -euo pipefail +[ "$#" -eq 3 ] || { echo 'usage: remote-todo-migrate <todo-id> <alias> <root>' >&2; exit 2; } +local_id="$1"; selected="$2"; root="$3" +[[ "$local_id" =~ ^(TF|T)-[0-9]{8}-[a-z0-9]+$ ]] && [[ "$selected" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] || { echo 'Invalid Todo ID or source alias.' >&2; exit 2; } +HERE="$(cd "$(dirname "$0")" && pwd)" +todo="$root/TaskFlowDocs/todo.md" +config="$root/TaskFlowDocs/remote-todos.conf" +[ -f "$todo" ] && [ ! -L "$todo" ] || { echo 'Local Todo file not found.' >&2; exit 2; } +entry="$(awk -v wanted="$local_id" ' + function finish() { if (matched) { count++; selected = block } } + /^## / { finish(); block = $0 "\n"; matched = 0; next } + { block = block $0 "\n"; if ($0 == "- ID: " wanted) matched = 1 } + END { finish(); if (count != 1) exit 2; printf "%s", selected } +' "$todo")" || { echo 'Todo ID missing or ambiguous.' >&2; exit 2; } +title="$(printf '%s\n' "$entry" | awk '/^- Goal: / { count++; value = substr($0, 9) } END { if (count != 1 || value == "") exit 2; print value }')" || { echo 'Todo goal missing or ambiguous.' >&2; exit 2; } +[[ "$title" != *[[:cntrl:]]* ]] || { echo 'Todo goal contains control characters.' >&2; exit 2; } +local_status="$(printf '%s\n' "$entry" | awk '/^- Status: / { count++; value = substr($0, 11) } END { if (count != 1) exit 2; print value }')" || { echo 'Todo status missing or ambiguous.' >&2; exit 2; } +task_id="$(printf '%s\n' "$entry" | sed -n 's/^- Task: `TaskFlowDocs\/\([^`/]*\)\/`$/\1/p')" +stage="$local_status" +if [ -n "$task_id" ]; then + bash "$HERE/task-id" "$task_id" || exit 2 + [ -f "$root/TaskFlowDocs/$task_id/prd.md" ] || { echo 'Active task PRD not found.' >&2; exit 2; } + stage="$(awk '/^> Status: / { count++; value = substr($0, 11) } END { if (count != 1) exit 2; print value }' "$root/TaskFlowDocs/$task_id/prd.md")" || { echo 'Task phase missing or ambiguous.' >&2; exit 2; } + [ "$stage" != completed ] || stage=done +fi +case "$stage" in inbox|clarified|promoted|planning|ready|in_progress|checking|blocked|done|cancelled) ;; *) echo 'Unsupported local Todo or task status.' >&2; exit 2 ;; esac +connection="$(awk -F '\t' -v name="$selected" 'NR > 1 && $1 == name { print; found = 1 } END { if (!found) exit 2 }' "$config")" || { echo 'Unknown source alias.' >&2; exit 2; } +tab="$(printf '\t')" +IFS="$tab" read -r alias platform host project <<EOF +$connection +EOF +external="$(printf '%s\n' "$entry" | sed -n 's/^- External: //p')" +normalized_host="$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')"; normalized_project="$project" +if [ "$platform" = github ]; then normalized_project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')"; prefix="https://$normalized_host/$normalized_project/issues/" +else prefix="https://$normalized_host/$project/-/issues/"; fi +case "$external" in + "$prefix"*) + source_number="${external#"$prefix"}" + if [[ "$source_number" =~ ^[1-9][0-9]*$ ]] && printf '%s\n' "$entry" | grep -Fx -- " <!-- taskflow-copy-complete:$local_id -->" >/dev/null; then + echo 'This Todo is a remote copy; use reattach-todo with --remote-authoritative after reconciling it instead of creating a duplicate.' >&2; exit 2 + fi ;; +esac +links="$root/TaskFlowDocs/remote-todo-links" +[ ! -L "$links" ] || { echo 'Invalid Todo links directory.' >&2; exit 2; } +mkdir -p "$links" +[ -d "$links" ] && [ ! -L "$links" ] || { echo 'Invalid Todo links directory.' >&2; exit 2; } +lock="$links/.lock-$local_id" +mkdir "$lock" 2>/dev/null || { echo 'Todo migration is already in progress or needs inspection.' >&2; exit 2; } +trap 'rmdir "$lock" 2>/dev/null || true' EXIT +link="$links/$local_id" +[ ! -L "$link" ] || { echo 'Invalid Todo migration link.' >&2; exit 2; } +if [ -e "$link.detached" ] || [ -L "$link.detached" ]; then + [ -f "$link" ] && [ ! -L "$link" ] && [ -f "$link.detached" ] && [ ! -L "$link.detached" ] && [ "$(cat "$link.detached")" = "$(printf 'detached\t%s' "$local_id")" ] || { echo 'This Todo was detached from its previous Issue; inspect both records before migrating it again.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" 'NR == 1 { valid = NF == 5 && $1 == "pending"; same = $3 == p && tolower($4) == tolower(h) && (p == "github" ? tolower($5) == tolower(r) : $5 == r) } $1 == "confirmed" && $2 ~ /^[1-9][0-9]*$/ { confirmed = 1 } END { if (!valid || !confirmed || same) exit 1 }' "$link" || { echo 'Previous association is invalid or belongs to the selected source; inspect before migrating.' >&2; exit 2; } + history_id="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"; [[ "$history_id" =~ ^[0-9a-f]{32}$ ]] || exit 2 + [ ! -e "$link.history-$history_id" ] && [ ! -e "$link.detached.history-$history_id" ] || exit 2 + mv -n "$link" "$link.history-$history_id" || exit 2 + [ ! -e "$link" ] && [ -f "$link.history-$history_id" ] || { echo 'Association history collision.' >&2; exit 2; } + if ! mv -n "$link.detached" "$link.detached.history-$history_id" || [ -e "$link.detached" ]; then + mv -n "$link.history-$history_id" "$link" || true + echo 'Cannot preserve previous detach record; inspect associations.' >&2; exit 2 + fi +fi +new_id() { od -An -N16 -tx1 /dev/urandom | tr -d ' \n'; } +if [ ! -e "$link" ]; then + id="$(new_id)" + [[ "$id" =~ ^[0-9a-f]{32}$ ]] || { echo 'Cannot allocate operation ID.' >&2; exit 2; } + (set -o noclobber; printf 'pending\t%s\t%s\t%s\t%s\n' "$id" "$platform" "$host" "$project" > "$link") || { echo 'Cannot save migration intent.' >&2; exit 2; } +else + [ -f "$link" ] || { echo 'Invalid Todo migration link.' >&2; exit 2; } + if ! awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" ' + NR == 1 { valid = NF == 5 && $1 == "pending" && $3 == p && tolower($4) == tolower(h) && (p == "github" ? tolower($5) == tolower(r) : $5 == r) } + END { if (!valid) exit 1 } + ' "$link"; then echo 'Todo is linked to another source or the link is invalid.' >&2; exit 2; fi +fi +confirmed="$(awk -F '\t' '$1 == "confirmed" && NF == 2 && $2 ~ /^[1-9][0-9]*$/ { number = $2 } END { print number }' "$link")" +id="$(awk -F '\t' 'NR == 1 { id = $2 } $1 == "retry" && NF == 2 { id = $2 } END { print id }' "$link")" +[[ "$id" =~ ^[0-9a-f]{32}$ ]] || { echo 'Invalid migration operation ID.' >&2; exit 2; } +if [ -z "$confirmed" ]; then + record="$root/TaskFlowDocs/.remote-todo-operations/$id" + if [ -e "$record" ] || [ -L "$record" ]; then + [ -f "$record" ] && [ ! -L "$record" ] || { echo 'Invalid creation operation.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v id="$id" ' + NR == 1 { valid = NF == 5 && $1 == "pending" && $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == id } + NR == 2 { valid = valid && NF == 2 && (($1 == "confirmed" && $2 ~ /^[1-9][0-9]*$/) || ($1 == "absent" && $2 == "user-checked")) } + NR > 2 { valid = 0 } + END { if (!valid) exit 1 } + ' "$record" || { echo 'Migration operation identity is invalid.' >&2; exit 2; } + status="$(awk -F '\t' 'NR == 2 && $1 == "confirmed" && $2 ~ /^[1-9][0-9]*$/ { print "confirmed " $2 } NR == 2 && $1 == "absent" && $2 == "user-checked" { print "absent" }' "$record")" + case "$status" in + 'confirmed '*) confirmed="${status#confirmed }" ;; + absent) + id="$(new_id)"; [[ "$id" =~ ^[0-9a-f]{32}$ ]] || { echo 'Cannot allocate operation ID.' >&2; exit 2; } + printf 'retry\t%s\n' "$id" >> "$link" || { echo 'Cannot save retry intent.' >&2; exit 2; } ;; + *) echo "Migration operation $id is unresolved; verify or resolve it before retrying." >&2; exit 2 ;; + esac + fi + if [ -z "$confirmed" ]; then + marker="<!-- taskflow-migration:$local_id -->" + echo 'Checking the whole remote Issue source for this migration marker; do not migrate this Todo simultaneously from another worktree or machine.' >&2 + if [ "$platform" = github ]; then + candidates="$(gh api "repos/$project/issues?state=all&per_page=100" --hostname "$host" --paginate --jq '.[] | select(.pull_request == null) | @json' 2>/dev/null)" || { echo 'Cannot check all remote Issues; migration not started.' >&2; exit 2; } + number_key=number; body_key=body + else + encoded="${project//\//%2F}" + candidates="$(glab api "projects/$encoded/issues?scope=all&state=all&per_page=100" --hostname "$host" --paginate --output ndjson 2>/dev/null)" || { echo 'Cannot check all remote Issues; migration not started.' >&2; exit 2; } + number_key=iid; body_key=description + fi + if [ -n "$candidates" ]; then + while IFS= read -r candidate; do + number="$(printf '%s' "$candidate" | bash "$HERE/json-field" --numbers "$number_key")" || { echo 'Invalid remote Issue list; migration not started.' >&2; exit 2; } + [[ "$number" =~ ^[1-9][0-9]*$ ]] || { echo 'Invalid remote Issue identity; migration not started.' >&2; exit 2; } + remote_body="$(printf '%s' "$candidate" | bash "$HERE/json-field" "$body_key")" || { echo 'Invalid remote Issue body; migration not started.' >&2; exit 2; } + case "$remote_body" in *"$marker"*) echo "An Issue with this migration marker already exists (#$number); inspect it before proceeding." >&2; exit 2 ;; esac + done <<EOF +$candidates +EOF + fi + echo 'Migrating the selected Todo content to the selected remote repository; concurrent migrations can still race.' >&2 + if ! { printf '%s\n\n%s\n' "$entry" "$marker"; } | bash "$HERE/remote-todo" create "$selected" "$title" - --operation-id "$id" --root "$root"; then + echo "Migration operation $id needs inspection before retrying." >&2; exit 2 + fi + record="$root/TaskFlowDocs/.remote-todo-operations/$id" + confirmed="$(awk -F '\t' 'NR == 2 && $1 == "confirmed" && $2 ~ /^[1-9][0-9]*$/ { print $2 }' "$record")" + [[ "$confirmed" =~ ^[1-9][0-9]*$ ]] || { echo 'Migration result cannot be confirmed.' >&2; exit 2; } + fi + printf 'confirmed\t%s\n' "$confirmed" >> "$link" || { echo 'Cannot save migration link; retry after inspecting the operation.' >&2; exit 2; } +fi +if ! awk -F '\t' '$1 == "stage-synced" && NF == 2 { found = 1 } END { if (!found) exit 1 }' "$link"; then + bash "$HERE/remote-todo" stage "$selected" "$confirmed" "$stage" --root "$root" || { echo 'Migration created the Issue but its initial stage needs recovery.' >&2; exit 2; } + printf 'stage-synced\t%s\n' "$stage" >> "$link" || { echo 'Cannot save migration stage; inspect both sides before retrying.' >&2; exit 2; } +fi +if [ -n "$task_id" ]; then + bash "$HERE/task-id" "$task_id" || exit 2 + ref="$root/TaskFlowDocs/$task_id/remote-issue" + if [ -e "$ref" ] || [ -L "$ref" ]; then + [ -f "$ref" ] && [ ! -L "$ref" ] || { echo 'Invalid task association.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$confirmed" 'NR == 1 { ok = NF == 4 && $1 == p && tolower($2) == tolower(h) && (p == "github" ? tolower($3) == tolower(r) : $3 == r) && $4 == n } END { if (NR != 1 || !ok) exit 1 }' "$ref" || { echo 'Task has a different remote association.' >&2; exit 2; } + else + bash "$HERE/remote-todo" link "$selected" "$confirmed" "$task_id" --root "$root" || { echo 'Migration succeeded; task association needs recovery.' >&2; exit 2; } + fi +fi +printf 'Todo %s linked to %s #%s. Original retained.\n' "$local_id" "$selected" "$confirmed" \ No newline at end of file diff --git a/hooks/remote-todo-promote b/hooks/remote-todo-promote new file mode 100644 index 0000000..0999bfe --- /dev/null +++ b/hooks/remote-todo-promote @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# Promote an explicitly selected remote Issue through TaskFlow's existing task scaffold. +set -euo pipefail +[ "$#" -eq 6 ] || { echo 'usage: remote-todo-promote <alias> <number> <task-id> <small|large> <kind> <root>' >&2; exit 2; } +selected="$1"; number="$2"; task="$3"; size="$4"; kind="$5"; root="$6" +[[ "$selected" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] && [[ "$number" =~ ^[1-9][0-9]*$ ]] || { echo 'Invalid source or Issue number.' >&2; exit 2; } +case "$size" in small|large) ;; *) echo 'Invalid task size.' >&2; exit 2 ;; esac +case "$kind" in defect|capability|internal) ;; *) echo 'Invalid task kind.' >&2; exit 2 ;; esac +HERE="$(cd "$(dirname "$0")" && pwd)" +bash "$HERE/task-id" "$task" || exit 2 +git_dir="$(git -C "$root" rev-parse --absolute-git-dir 2>/dev/null)" || { echo 'A task worktree is required.' >&2; exit 2; } +[ -f "$git_dir/commondir" ] || { echo 'A task worktree is required.' >&2; exit 2; } +config="$root/TaskFlowDocs/remote-todos.conf" +connection="$(awk -F '\t' -v name="$selected" 'NR > 1 && $1 == name { print; found = 1 } END { if (!found) exit 2 }' "$config")" || { echo 'Unknown source alias.' >&2; exit 2; } +tab="$(printf '\t')" +IFS="$tab" read -r alias platform host project <<EOF +$connection +EOF +if [ "$platform" = github ]; then + response="$(gh api "repos/$project/issues/$number" --hostname "$host" --jq 'select(.pull_request == null)' 2>/dev/null)" || { echo 'Cannot read remote Issue.' >&2; exit 2; } + number_key=number +else + encoded="${project//\//%2F}" + response="$(glab api "projects/$encoded/issues/$number" --hostname "$host" 2>/dev/null)" || { echo 'Cannot read remote Issue.' >&2; exit 2; } + number_key=iid +fi +actual="$(printf '%s' "$response" | bash "$HERE/json-field" --numbers "$number_key")" || { echo 'Cannot parse remote Issue.' >&2; exit 2; } +[ "$actual" = "$number" ] || { echo 'Remote Issue identity mismatch.' >&2; exit 2; } +title="$(printf '%s' "$response" | bash "$HERE/json-field" title)" || { echo 'Cannot parse remote title.' >&2; exit 2; } +created="$(printf '%s' "$response" | bash "$HERE/json-field" created_at)" || { echo 'Cannot parse remote creation time.' >&2; exit 2; } +[[ "${created:0:10}" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] || { echo 'Invalid remote creation time.' >&2; exit 2; } +title="$(printf '%s' "$title" | LC_ALL=C tr '\r\n\t' ' ' | LC_ALL=C tr -d '\000-\010\013\014\016-\037\177')" +[ -n "$title" ] || { echo 'Remote title is empty.' >&2; exit 2; } +normalized_host="$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" +normalized_project="$project" +if [ "$platform" = github ]; then normalized_project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')"; url="https://$normalized_host/$normalized_project/issues/$number" +else url="https://$normalized_host/$normalized_project/-/issues/$number"; fi +digest="$(printf 'remote-reference\n%s\n%s\n%s\n%s\n' "$platform" "$normalized_host" "$normalized_project" "$number" | cksum | awk '{ printf "%06x", $1 % 16777216 }')" +day="${created:0:10}"; day="${day//-/}" +id="TF-$day-$digest" +docs="$root/TaskFlowDocs" +todo="$docs/todo.md" +[ ! -L "$todo" ] || { echo 'Invalid local Todo file.' >&2; exit 2; } +mkdir -p "$docs" +lock="$docs/.remote-todo-promote-lock" +mkdir "$lock" 2>/dev/null || { echo 'Remote promotion is busy or needs inspection.' >&2; exit 2; } +trap 'rmdir "$lock" 2>/dev/null || true' EXIT +if [ ! -e "$docs/$task" ]; then bash "$HERE/version-baseline" init "$root" "$task" v1 || { echo 'Task baseline could not be recorded.' >&2; exit 2; }; fi +if [ ! -e "$todo" ]; then (set -o noclobber; printf '# Todo\n' > "$todo") || { echo 'Cannot create local Todo.' >&2; exit 2; }; fi +[ -f "$todo" ] && [ ! -L "$todo" ] || { echo 'Invalid local Todo file.' >&2; exit 2; } +existing="$(TASKFLOW_REMOTE_ID="$id" TASKFLOW_REMOTE_URL="$url" awk ' + function flush() { if (inside && found_id) { matches++; if (found_url && found_end) target = task } } + /^## / { flush(); inside = 1; found_id = found_url = found_end = 0; task = "" } + inside && $0 == "- ID: " ENVIRON["TASKFLOW_REMOTE_ID"] { found_id = 1 } + inside && $0 == "- External: " ENVIRON["TASKFLOW_REMOTE_URL"] { found_url = 1 } + inside && $0 == " <!-- taskflow-remote-ref:" ENVIRON["TASKFLOW_REMOTE_ID"] " -->" { found_end = 1 } + inside && /^- Task: / { task = substr($0, 9) } + END { flush(); if (matches == 0) print "missing"; else if (matches == 1 && target != "") print target; else print "invalid" } +' "$todo")" +case "$existing" in + missing) + [ ! -e "$docs/$task" ] || { echo 'Task directory already exists without an association.' >&2; exit 2; } + today="$(date +%Y-%m-%d)" + printf '\n## Remote Issue %s\n\n- ID: %s\n- Status: clarified\n- Priority: normal\n- Owner: Unknown\n- Source: remote reference\n- Kind: %s\n- Added: %s\n- Updated: %s\n- Goal: %s\n- External: %s\n- Task: Not promoted.\n- Next action: Prepare task documents.\n <!-- taskflow-remote-ref:%s -->\n' "$number" "$id" "$kind" "$today" "$today" "$title" "$url" "$id" >> "$todo" || { echo 'Local association may be incomplete; inspect before retrying.' >&2; exit 2; } + existing='Not promoted.' ;; + invalid) echo 'Local association is incomplete or collides with another Todo.' >&2; exit 2 ;; +esac +if [ "$existing" = 'Not promoted.' ]; then + [ ! -e "$docs/$task" ] || { echo 'Task directory exists but the Todo was not promoted; inspect before retrying.' >&2; exit 2; } + bash "$HERE/task" promote "$id" "$task" "$size" --kind "$kind" --root "$root" || { echo 'Remote association retained; task promotion needs inspection.' >&2; exit 2; } +elif [ "$existing" != "\`TaskFlowDocs/$task/\`" ]; then + echo 'This Issue already points to another local task.' >&2; exit 2 +fi +detached="$docs/remote-todo-links/task-$task.detached" +if [ -e "$detached" ] || [ -L "$detached" ]; then + echo 'This task was detached from its Issue; inspect both records before reconnecting.' >&2; exit 2 +fi +ref="$docs/$task/remote-issue" +if [ -e "$ref" ] || [ -L "$ref" ]; then + [ -f "$ref" ] && [ ! -L "$ref" ] || { echo 'Invalid task association.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { ok = NF == 4 && $1 == p && tolower($2) == tolower(h) && (p == "github" ? tolower($3) == tolower(r) : $3 == r) && $4 == n } END { if (NR != 1 || !ok) exit 1 }' "$ref" || { echo 'Task has a different remote association.' >&2; exit 2; } +else + bash "$HERE/remote-todo" link "$selected" "$number" "$task" --root "$root" || { echo 'Task created; association needs recovery.' >&2; exit 2; } +fi +phase="$(awk '/^> Status: / { count++; value = substr($0, 11) } END { if (count != 1) exit 2; print value }' "$docs/$task/prd.md")" || { echo 'Task phase missing or ambiguous.' >&2; exit 2; } +[ "$phase" != completed ] || phase=done +case "$phase" in planning|ready|in_progress|checking|blocked|done) ;; *) echo 'Unsupported task phase.' >&2; exit 2 ;; esac +bash "$HERE/remote-todo" stage-task "$task" "$phase" --root "$root" || { echo 'Task associated; remote stage needs recovery.' >&2; exit 2; } +printf 'Remote Issue %s #%s promoted as task %s.\n' "$selected" "$number" "$task" \ No newline at end of file diff --git a/hooks/remote-todo-reattach b/hooks/remote-todo-reattach new file mode 100644 index 0000000..e0a0178 --- /dev/null +++ b/hooks/remote-todo-reattach @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +# Reattach only when the existing remote and local task phases already agree. +set -euo pipefail +[ "$#" -eq 2 ] || exit 2 +task="$1"; root="$2"; HERE="$(cd "$(dirname "$0")" && pwd)" +bash "$HERE/task-id" "$task" || exit 2 +docs="$root/TaskFlowDocs"; links="$docs/remote-todo-links"; ref="$docs/$task/remote-issue"; marker="$links/task-$task.detached" +[ -f "$ref" ] && [ ! -L "$ref" ] && [ -f "$marker" ] && [ ! -L "$marker" ] && [ ! -L "$links" ] || { echo 'Detached task association not found.' >&2; exit 2; } +[ -f "$docs/$task/prd.md" ] && [ -f "$docs/$task/plan.md" ] || { echo 'Active task documents not found.' >&2; exit 2; } +awk -F '\t' 'NR == 1 { valid = NF == 4 && ($1 == "github" || $1 == "gitlab") && $4 ~ /^[1-9][0-9]*$/ } END { if (NR != 1 || !valid) exit 1 }' "$ref" || { echo 'Invalid task association.' >&2; exit 2; } +tab="$(printf '\t')"; IFS="$tab" read -r platform host project number < "$ref" +expected="$(printf 'detached\t%s\t%s\t%s\t%s' "$platform" "$host" "$project" "$number")" +[ "$(cat "$marker")" = "$expected" ] || { echo 'Task detach record does not match the association.' >&2; exit 2; } +phase="$(awk '/^> Status: / { count++; value = substr($0, 11) } END { if (count != 1) exit 2; print value }' "$docs/$task/prd.md")" || { echo 'Invalid local task phase.' >&2; exit 2; } +case "$phase" in planning|ready|in_progress|checking|blocked) ;; *) echo 'Unsupported active task phase.' >&2; exit 2 ;; esac +config="$docs/remote-todos.conf" +selected="$(awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" 'NR > 1 && $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) { print $1; found = 1; exit } END { if (!found) exit 1 }' "$config")" || { echo 'Associated remote source is not configured.' >&2; exit 2; } +link_lock="$docs/.remote-todo-link-lock"; task_lock="$links/.lock-task-$task" +mkdir "$link_lock" 2>/dev/null || { echo 'Remote association is busy or needs inspection.' >&2; exit 2; } +mkdir "$task_lock" 2>/dev/null || { rmdir "$link_lock" 2>/dev/null || true; echo 'Task association is busy or needs inspection.' >&2; exit 2; } +trap 'rmdir "$task_lock" 2>/dev/null || true; rmdir "$link_lock" 2>/dev/null || true' EXIT +[ -f "$marker" ] && [ ! -L "$marker" ] && [ "$(cat "$marker")" = "$expected" ] || { echo 'Task detach record changed; inspect before retrying.' >&2; exit 2; } +for other in "$docs"/*/remote-issue "$docs"/achieved/*/remote-issue; do + [ -e "$other" ] || [ -L "$other" ] || continue + [ "$other" != "$ref" ] || continue + [ -f "$other" ] && [ ! -L "$other" ] || { echo 'Invalid task association.' >&2; exit 2; } + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { same = NF == 4 && $1 == p && tolower($2) == tolower(h) && (p == "github" ? tolower($3) == tolower(r) : $3 == r) && $4 == n } END { if (!same) exit 1 }' "$other"; then + other_task="${other%/remote-issue}"; other_task="${other_task##*/}"; other_marker="$links/task-$other_task.detached" + [ -f "$other_marker" ] && [ ! -L "$other_marker" ] || { echo 'Remote Issue is already associated with another task.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { valid = NF == 5 && $1 == "detached" && $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == n } END { if (NR != 1 || !valid) exit 1 }' "$other_marker" || { echo 'Invalid task detach record.' >&2; exit 2; } + fi +done +if [ "$platform" = github ]; then + response="$(gh api "repos/$project/issues/$number" --hostname "$host" --jq 'select(.pull_request == null) | .labels |= map(.name)' 2>/dev/null)" || { echo 'Cannot read remote Issue; task remains detached.' >&2; exit 2; }; number_key=number +else + encoded="${project//\//%2F}"; response="$(glab api "projects/$encoded/issues/$number" --hostname "$host" 2>/dev/null)" || { echo 'Cannot read remote Issue; task remains detached.' >&2; exit 2; }; number_key=iid +fi +actual="$(printf '%s' "$response" | bash "$HERE/json-field" --numbers "$number_key")" || exit 2 +state="$(printf '%s' "$response" | bash "$HERE/json-field" state)" || exit 2 +[ "$actual" = "$number" ] && { [ "$state" = open ] || [ "$state" = opened ]; } || { echo 'Remote Issue identity or state differs; reconcile before reattaching.' >&2; exit 2; } +for item in inbox clarified promoted planning ready in_progress checking blocked done cancelled; do + has="$(printf '%s' "$response" | bash "$HERE/json-field" --array-has labels "taskflow:$item")" || exit 2 + if { [ "$item" = "$phase" ] && [ "$has" != yes ]; } || { [ "$item" != "$phase" ] && [ "$has" = yes ]; }; then echo 'Remote and local task phases differ; reconcile before reattaching.' >&2; exit 2; fi +done +local_marker='' +for file in "$links"/*.detached; do + [ -e "$file" ] || [ -L "$file" ] || continue + [ "$file" != "$marker" ] || continue + [ -f "$file" ] && [ ! -L "$file" ] || { echo 'Invalid detach record.' >&2; exit 2; } + if [ "$(cat "$file")" = "$(printf 'detached-task\t%s' "$task")" ]; then [ -z "$local_marker" ] || { echo 'Multiple Todo detach records need inspection.' >&2; exit 2; }; local_marker="$file"; fi +done +if [ -n "$local_marker" ]; then + local_id="${local_marker##*/}"; local_id="${local_id%.detached}" + [[ "$local_id" =~ ^(TF|T)-[0-9]{8}-[a-z0-9]+$ ]] || { echo 'Invalid Todo detach record name.' >&2; exit 2; } + local_lock="$links/.lock-$local_id" + mkdir "$local_lock" 2>/dev/null || { echo 'Todo association is busy or needs inspection.' >&2; exit 2; } + trap 'rmdir "$local_lock" 2>/dev/null || true; rmdir "$task_lock" 2>/dev/null || true; rmdir "$link_lock" 2>/dev/null || true' EXIT + [ -f "$local_marker" ] && [ ! -L "$local_marker" ] && [ "$(cat "$local_marker")" = "$(printf 'detached-task\t%s' "$task")" ] || { echo 'Todo detach record changed; inspect before retrying.' >&2; exit 2; } +fi +id="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"; [[ "$id" =~ ^[0-9a-f]{32}$ ]] || exit 2 +history="$marker.history-$id"; [ ! -e "$history" ] && [ ! -L "$history" ] || exit 2 +if [ -n "$local_marker" ]; then + local_history="$local_marker.history-$id"; [ ! -e "$local_history" ] && [ ! -L "$local_history" ] || exit 2 + mv -n "$local_marker" "$local_history" || { echo 'Cannot preserve Todo detach history.' >&2; exit 2; } + [ ! -e "$local_marker" ] && [ -f "$local_history" ] || { echo 'Todo detach history collision; inspect before retrying.' >&2; exit 2; } +fi +if ! mv -n "$marker" "$history" || [ -e "$marker" ] || [ ! -f "$history" ]; then + if [ -n "$local_marker" ]; then mv -n "$local_history" "$local_marker" || true; fi + echo 'Cannot preserve task detach history; inspect local associations.' >&2; exit 2 +fi +printf 'Reattached task %s to %s #%s; local and remote phases agree.\n' "$task" "$selected" "$number" diff --git a/hooks/remote-todo-reattach-todo b/hooks/remote-todo-reattach-todo new file mode 100644 index 0000000..44eceab --- /dev/null +++ b/hooks/remote-todo-reattach-todo @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +# Explicitly make the original remote Issue authoritative for a local copy. +set -euo pipefail +[ "$#" -eq 3 ] || exit 2 +id="$1"; selected="$2"; root="$3"; HERE="$(cd "$(dirname "$0")" && pwd)" +[[ "$id" =~ ^(TF|T)-[0-9]{8}-[a-z0-9]+$ ]] && [[ "$selected" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] || exit 2 +docs="$root/TaskFlowDocs"; todo="$docs/todo.md"; config="$docs/remote-todos.conf"; links="$docs/remote-todo-links" +[ -f "$todo" ] && [ ! -L "$todo" ] || { echo 'Local Todo file not found.' >&2; exit 2; } +entry="$(awk -v wanted="$id" 'function finish() { if (matched) { count++; selected = block } } /^## / { finish(); block = $0 "\n"; matched = 0; next } { block = block $0 "\n"; if ($0 == "- ID: " wanted) matched = 1 } END { finish(); if (count != 1) exit 2; printf "%s", selected }' "$todo")" || { echo 'Todo ID missing or ambiguous.' >&2; exit 2; } +printf '%s\n' "$entry" | grep -Fx -- " <!-- taskflow-copy-complete:$id -->" >/dev/null || { echo 'Todo is not a complete remote copy.' >&2; exit 2; } +printf '%s\n' "$entry" | grep -Fx -- '- Task: Not promoted.' >/dev/null || { echo 'Use reattach-task for an active task.' >&2; exit 2; } +external="$(printf '%s\n' "$entry" | awk '/^- External: / { count++; value = substr($0, 13) } END { if (count != 1) exit 2; print value }')" || { echo 'Remote origin missing or ambiguous.' >&2; exit 2; } +connection="$(awk -F '\t' -v name="$selected" 'NR > 1 && $1 == name { print; found = 1 } END { if (!found) exit 2 }' "$config")" || { echo 'Unknown source alias.' >&2; exit 2; } +tab="$(printf '\t')"; IFS="$tab" read -r alias platform host project <<EOF +$connection +EOF +normalized_host="$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')"; normalized_project="$project" +if [ "$platform" = github ]; then normalized_project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')"; prefix="https://$normalized_host/$normalized_project/issues/" +else prefix="https://$normalized_host/$project/-/issues/"; fi +case "$external" in "$prefix"*) number="${external#"$prefix"}" ;; *) echo 'Local copy belongs to a different source.' >&2; exit 2 ;; esac +[[ "$number" =~ ^[1-9][0-9]*$ ]] || { echo 'Invalid remote Issue identity.' >&2; exit 2; } +[ ! -L "$links" ] || exit 2; mkdir -p "$links"; [ -d "$links" ] && [ ! -L "$links" ] || exit 2 +link_lock="$docs/.remote-todo-link-lock"; local_lock="$links/.lock-$id" +mkdir "$link_lock" 2>/dev/null || { echo 'Remote association is busy.' >&2; exit 2; } +mkdir "$local_lock" 2>/dev/null || { rmdir "$link_lock" 2>/dev/null || true; echo 'Todo association is busy.' >&2; exit 2; } +trap 'rmdir "$local_lock" 2>/dev/null || true; rmdir "$link_lock" 2>/dev/null || true' EXIT +mapping="$links/$id"; marker="$mapping.detached" +[ ! -L "$mapping" ] && [ ! -L "$marker" ] || { echo 'Invalid Todo association.' >&2; exit 2; } +for other in "$links"/*; do + [ -e "$other" ] || [ -L "$other" ] || continue + other_id="${other##*/}" + [[ "$other_id" =~ ^(TF|T)-[0-9]{8}-[a-z0-9]+$ ]] || continue + [ "$other" != "$mapping" ] || continue + [ -f "$other" ] && [ ! -L "$other" ] || { echo 'Invalid Todo association.' >&2; exit 2; } + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { same = NF == 5 && $1 == "pending" && $3 == p && tolower($4) == tolower(h) && (p == "github" ? tolower($5) == tolower(r) : $5 == r) } $1 == "confirmed" && $2 == n { confirmed = 1 } END { if (!(same && confirmed)) exit 1 }' "$other"; then + [ -f "$other.detached" ] && [ ! -L "$other.detached" ] || { echo 'Remote Issue already has a local Todo association.' >&2; exit 2; } + awk -F '\t' 'NR == 1 { valid = NF == 2 && (($1 == "detached" && $2 ~ /^(TF|T)-[0-9]{8}-[a-z0-9]+$/) || ($1 == "detached-task" && $2 ~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}-[a-z0-9-]+$/)) } END { if (NR != 1 || !valid) exit 1 }' "$other.detached" || { echo 'Invalid Todo detach record.' >&2; exit 2; } + fi +done +for ref in "$docs"/*/remote-issue "$docs"/achieved/*/remote-issue; do + [ -e "$ref" ] || [ -L "$ref" ] || continue + [ -f "$ref" ] && [ ! -L "$ref" ] || exit 2 + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { same = NF == 4 && $1 == p && tolower($2) == tolower(h) && (p == "github" ? tolower($3) == tolower(r) : $3 == r) && $4 == n } END { if (!same) exit 1 }' "$ref"; then + task="${ref%/remote-issue}"; task="${task##*/}"; detached="$links/task-$task.detached" + [ -f "$detached" ] && [ ! -L "$detached" ] || { echo 'Remote Issue already has a task association.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { valid = NF == 5 && $1 == "detached" && $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == n } END { if (NR != 1 || !valid) exit 1 }' "$detached" || { echo 'Invalid task detach record.' >&2; exit 2; } + fi +done +if [ "$platform" = github ]; then + response="$(gh api "repos/$project/issues/$number" --hostname "$host" --jq 'select(.pull_request == null)' 2>/dev/null)" || { echo 'Cannot read remote Issue; local copy unchanged.' >&2; exit 2; }; key=number +else + encoded="${project//\//%2F}"; response="$(glab api "projects/$encoded/issues/$number" --hostname "$host" 2>/dev/null)" || { echo 'Cannot read remote Issue; local copy unchanged.' >&2; exit 2; }; key=iid +fi +actual="$(printf '%s' "$response" | bash "$HERE/json-field" --numbers "$key")" || exit 2 +state="$(printf '%s' "$response" | bash "$HERE/json-field" state)" || exit 2 +[ "$actual" = "$number" ] && { [ "$state" = open ] || [ "$state" = opened ] || [ "$state" = closed ]; } || { echo 'Remote Issue identity or state invalid.' >&2; exit 2; } +op="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"; [[ "$op" =~ ^[0-9a-f]{32}$ ]] || exit 2 +if [ ! -e "$mapping" ]; then + [ ! -e "$marker" ] || { echo 'Orphaned detach record; inspect before reattaching.' >&2; exit 2; } + (set -o noclobber; printf 'pending\t%s\t%s\t%s\t%s\nconfirmed\t%s\nremote-authoritative\tconfirmed\n' "$op" "$platform" "$host" "$project" "$number" > "$mapping") || { echo 'Cannot save association; inspect before retrying.' >&2; exit 2; } +else + [ -f "$mapping" ] && awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { valid = NF == 5 && $1 == "pending" && $3 == p && tolower($4) == tolower(h) && (p == "github" ? tolower($5) == tolower(r) : $5 == r) } $1 == "confirmed" && $2 == n { found = 1 } ($1 == "stage-synced" || $1 == "remote-authoritative") && NF == 2 { synced = 1 } END { if (!(valid && found && synced)) exit 1 }' "$mapping" || { echo 'Existing Todo association differs.' >&2; exit 2; } + if [ -e "$marker" ]; then + [ -f "$marker" ] && [ "$(cat "$marker")" = "$(printf 'detached\t%s' "$id")" ] || { echo 'Invalid detach record.' >&2; exit 2; } + history="$marker.history-$op"; [ ! -e "$history" ] && [ ! -L "$history" ] || exit 2 + mv -n "$marker" "$history" || exit 2 + [ ! -e "$marker" ] && [ -f "$history" ] || { echo 'Detach history collision.' >&2; exit 2; } + fi +fi +printf 'Reattached Todo %s to %s #%s; remote Issue is authoritative and the local copy was retained.\n' "$id" "$selected" "$number" diff --git a/hooks/remote-todo-to-local b/hooks/remote-todo-to-local new file mode 100644 index 0000000..e173198 --- /dev/null +++ b/hooks/remote-todo-to-local @@ -0,0 +1,179 @@ +#!/usr/bin/env bash +# Copy one remote Issue into local Todo without closing or deleting it. +set -euo pipefail +[ "$#" -eq 4 ] || { echo 'usage: remote-todo-to-local <alias> <number> <root> <detach>' >&2; exit 2; } +selected="$1"; number="$2"; root="$3"; detach="$4" +[[ "$selected" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] && [[ "$number" =~ ^[1-9][0-9]*$ ]] || { echo 'Invalid source or Issue number.' >&2; exit 2; } +HERE="$(cd "$(dirname "$0")" && pwd)" +config="$root/TaskFlowDocs/remote-todos.conf" +connection="$(awk -F '\t' -v name="$selected" 'NR > 1 && $1 == name { print; found = 1 } END { if (!found) exit 2 }' "$config")" || { echo 'Unknown source alias.' >&2; exit 2; } +tab="$(printf '\t')" +IFS="$tab" read -r alias platform host project <<EOF +$connection +EOF +if [ "$platform" = github ]; then + response="$(gh api "repos/$project/issues/$number" --hostname "$host" --jq 'select(.pull_request == null) | .labels |= map(.name)' 2>/dev/null)" || { echo 'Cannot read remote Issue.' >&2; exit 2; } + number_key=number; body_key=body + url="https://$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')/$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')/issues/$number" +else + encoded="${project//\//%2F}" + response="$(glab api "projects/$encoded/issues/$number" --hostname "$host" 2>/dev/null)" || { echo 'Cannot read remote Issue.' >&2; exit 2; } + number_key=iid; body_key=description; url="https://$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')/$project/-/issues/$number" +fi +actual="$(printf '%s' "$response" | bash "$HERE/json-field" --numbers "$number_key")" || { echo 'Cannot parse remote Issue.' >&2; exit 2; } +[ "$actual" = "$number" ] || { echo 'Remote Issue identity mismatch.' >&2; exit 2; } +title="$(printf '%s' "$response" | bash "$HERE/json-field" title)" || { echo 'Cannot parse remote title.' >&2; exit 2; } +body="$(printf '%s' "$response" | bash "$HERE/json-field" "$body_key")" || { echo 'Cannot parse remote description.' >&2; exit 2; } +created="$(printf '%s' "$response" | bash "$HERE/json-field" created_at)" || { echo 'Cannot parse remote creation time.' >&2; exit 2; } +state="$(printf '%s' "$response" | bash "$HERE/json-field" state)" || { echo 'Cannot parse remote state.' >&2; exit 2; } +case "$state" in open|opened|closed) ;; *) echo 'Invalid remote state.' >&2; exit 2 ;; esac +[[ "${created:0:10}" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] || { echo 'Invalid remote creation time.' >&2; exit 2; } +title="$(printf '%s' "$title" | LC_ALL=C tr '\r\n\t' ' ' | LC_ALL=C tr -d '\000-\010\013\014\016-\037\177')" +[ -n "$title" ] || { echo 'Remote title is empty.' >&2; exit 2; } +body="$(printf '%s' "$body" | LC_ALL=C tr -d '\000-\010\013\014\016-\037\177')" +stage=''; count=0 +for item in inbox clarified promoted planning ready in_progress checking blocked done cancelled; do + has="$(printf '%s' "$response" | bash "$HERE/json-field" --array-has labels "taskflow:$item")" || { echo 'Cannot parse remote labels.' >&2; exit 2; } + if [ "$has" = yes ]; then stage="$item"; count=$((count + 1)); fi +done +[ "$count" -le 1 ] || { echo 'Remote Issue has conflicting TaskFlow stages.' >&2; exit 2; } +if [ -z "$stage" ]; then if [ "$state" = closed ]; then stage=done; else stage=inbox; fi; fi +case "$stage" in planning|ready) local_status=promoted ;; checking|blocked) local_status=in_progress ;; *) local_status="$stage" ;; esac +case "$stage:$state" in done:closed|cancelled:closed|inbox:open|inbox:opened|clarified:open|clarified:opened|promoted:open|promoted:opened|planning:open|planning:opened|ready:open|ready:opened|in_progress:open|in_progress:opened|checking:open|checking:opened|blocked:open|blocked:opened) ;; *) echo 'Remote state and TaskFlow stage conflict.' >&2; exit 2 ;; esac +# Do not detach an active association implicitly; that transition needs its own recovery path. +origin_task=; detached_task=; detached_count=0 +for ref in "$root"/TaskFlowDocs/*/remote-issue "$root"/TaskFlowDocs/achieved/*/remote-issue; do + [ -e "$ref" ] || [ -L "$ref" ] || continue + [ -f "$ref" ] && [ ! -L "$ref" ] || { echo 'Invalid existing task association.' >&2; exit 2; } + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { same = NF == 4 && $1 == p && tolower($2) == tolower(h) && (p == "github" ? tolower($3) == tolower(r) : $3 == r) && $4 == n } END { if (same) exit 0; exit 1 }' "$ref"; then + [ "$detach" = 1 ] || { echo 'This Issue is associated with a task; use --detach to copy and disconnect it.' >&2; exit 2; } + candidate="${ref%/remote-issue}"; candidate="${candidate##*/}" + bash "$HERE/task-id" "$candidate" || exit 2 + detached="$root/TaskFlowDocs/remote-todo-links/task-$candidate.detached" + if [ -e "$detached" ] || [ -L "$detached" ]; then + [ -f "$detached" ] && [ ! -L "$detached" ] || { echo 'Invalid task detach record.' >&2; exit 2; } + awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { valid = NF == 5 && $1 == "detached" && $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == n } END { if (NR != 1 || !valid) exit 1 }' "$detached" || { echo 'Invalid task detach record.' >&2; exit 2; } + detached_task="$candidate"; detached_count=$((detached_count + 1)); continue + fi + [ -z "$origin_task" ] || { echo 'Multiple task associations need inspection.' >&2; exit 2; } + origin_task="$candidate" + fi +done +if [ -z "$origin_task" ] && [ "$detached_count" -eq 1 ]; then origin_task="$detached_task"; fi +[ -n "$origin_task" ] || [ "$detached_count" -le 1 ] || { echo 'Multiple detached task associations need inspection.' >&2; exit 2; } +origin_link= +for link in "$root"/TaskFlowDocs/remote-todo-links/*; do + [ -e "$link" ] || [ -L "$link" ] || continue + link_id="${link##*/}" + [[ "$link_id" =~ ^(TF|T)-[0-9]{8}-[a-z0-9]+$ ]] || continue + [ -f "$link" ] && [ ! -L "$link" ] || { echo 'Invalid existing Todo association.' >&2; exit 2; } + if [ -e "$link.detached" ] || [ -L "$link.detached" ]; then + [ -f "$link.detached" ] && [ ! -L "$link.detached" ] || { echo 'Invalid Todo detach record.' >&2; exit 2; } + [ -n "$origin_task" ] && [ "$(cat "$link.detached")" = "$(printf 'detached-task\t%s' "$origin_task")" ] || continue + fi + if awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { same = NF == 5 && $1 == "pending" && $3 == p && tolower($4) == tolower(h) && (p == "github" ? tolower($5) == tolower(r) : $5 == r) } $1 == "confirmed" && $2 == n { confirmed = 1 } END { if (same && confirmed) exit 0; exit 1 }' "$link"; then + [ "$detach" = 1 ] || { echo 'This Issue is associated with a local Todo; use --detach to copy and disconnect it.' >&2; exit 2; } + [ -z "$origin_link" ] || { echo 'Multiple local Todo associations need inspection.' >&2; exit 2; } + origin_link="$link" + fi +done +if [ -n "$origin_task" ]; then + links="$root/TaskFlowDocs/remote-todo-links" + [ ! -L "$links" ] || { echo 'Invalid Todo links directory.' >&2; exit 2; } + mkdir -p "$links" + [ -d "$links" ] && [ ! -L "$links" ] || { echo 'Invalid Todo links directory.' >&2; exit 2; } + lock="$links/.lock-task-$origin_task" + mkdir "$lock" 2>/dev/null || { echo 'Task detachment is busy or needs inspection.' >&2; exit 2; } + trap 'rmdir "$lock" 2>/dev/null || true' EXIT + copy="$links/task-$origin_task-copy.md" + marker="$links/task-$origin_task.detached" + [ ! -L "$copy" ] && [ ! -L "$marker" ] || { echo 'Invalid task copy or detach record.' >&2; exit 2; } + for prior in "$links/task-$origin_task.detached.history-"*; do + [ -e "$prior" ] || [ -L "$prior" ] || continue + if [ -e "$copy" ] && [ ! -e "$marker" ]; then + [ -f "$copy" ] && [ ! -L "$copy" ] || { echo 'Invalid previous task copy.' >&2; exit 2; } + copy_id="$(od -An -N16 -tx1 /dev/urandom | tr -d ' \n')"; [[ "$copy_id" =~ ^[0-9a-f]{32}$ ]] || exit 2 + [ ! -e "$copy.history-$copy_id" ] && [ ! -L "$copy.history-$copy_id" ] || exit 2 + mv -n "$copy" "$copy.history-$copy_id" || { echo 'Cannot preserve previous task copy.' >&2; exit 2; } + [ ! -e "$copy" ] && [ -f "$copy.history-$copy_id" ] || { echo 'Task copy history collision; inspect before retrying.' >&2; exit 2; } + fi + break + done + if [ -e "$copy" ]; then + [ -f "$copy" ] && grep -Fx -- "- Source: $url" "$copy" >/dev/null && grep -Fx -- " <!-- taskflow-copy-complete:task-$origin_task -->" "$copy" >/dev/null || { echo 'Existing task copy is incomplete or belongs to another source.' >&2; exit 2; } + else + echo 'Copying the remote Issue into a local task reference; the remote Issue remains unchanged.' >&2 + (set -o noclobber; { + printf '# Remote Issue copy\n\n- Source: %s\n- Retrieved: %s\n- Title: %s\n\n## Untrusted remote description\n\n' "$url" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$title" + printf '%s\n' "$body" | sed 's/^/ /' || exit 2 + printf ' <!-- taskflow-copy-complete:task-%s -->\n' "$origin_task" + } > "$copy") || { echo 'Task copy may be incomplete; inspect it before retrying.' >&2; exit 2; } + fi + expected="$(printf 'detached\t%s\t%s\t%s\t%s' "$platform" "$host" "$project" "$number")" + if [ -e "$marker" ]; then + [ -f "$marker" ] && awk -F '\t' -v p="$platform" -v h="$host" -v r="$project" -v n="$number" 'NR == 1 { valid = NF == 5 && $1 == "detached" && $2 == p && tolower($3) == tolower(h) && (p == "github" ? tolower($4) == tolower(r) : $4 == r) && $5 == n } END { if (NR != 1 || !valid) exit 1 }' "$marker" || { echo 'Conflicting task detach record.' >&2; exit 2; } + else (set -o noclobber; printf '%s\n' "$expected" > "$marker") || { echo 'Task copy saved; detachment needs recovery.' >&2; exit 2; }; fi + if [ -n "$origin_link" ]; then + local_marker="$origin_link.detached" + [ ! -L "$local_marker" ] || { echo 'Invalid Todo detach record.' >&2; exit 2; } + expected_local="$(printf 'detached-task\t%s' "$origin_task")" + if [ -e "$local_marker" ]; then [ -f "$local_marker" ] && [ "$(cat "$local_marker")" = "$expected_local" ] || { echo 'Conflicting Todo detach record.' >&2; exit 2; } + else (set -o noclobber; printf '%s\n' "$expected_local" > "$local_marker") || { echo 'Task detached; local Todo association needs recovery.' >&2; exit 2; }; fi + fi + printf 'Copied remote Issue and detached task %s; remote Issue unchanged.\n' "$origin_task" + exit 0 +fi +normalized_host="$(printf '%s' "$host" | tr '[:upper:]' '[:lower:]')" +normalized_project="$project" +if [ "$platform" = github ]; then normalized_project="$(printf '%s' "$project" | tr '[:upper:]' '[:lower:]')"; fi +if [ "$platform" = github ]; then url="https://$normalized_host/$normalized_project/issues/$number" +else url="https://$normalized_host/$normalized_project/-/issues/$number"; fi +digest="$(printf '%s\n%s\n%s\n%s\n' "$platform" "$normalized_host" "$normalized_project" "$number" | cksum | awk '{ printf "%06x", $1 % 16777216 }')" +day="${created:0:10}"; day="${day//-/}" +id="TF-$day-$digest" +todo="$root/TaskFlowDocs/todo.md" +[ ! -L "$todo" ] || { echo 'Invalid local Todo file.' >&2; exit 2; } +mkdir -p "$root/TaskFlowDocs" +lock="$root/TaskFlowDocs/.remote-todo-local-lock" +mkdir "$lock" 2>/dev/null || { echo 'Local Todo copy is busy or needs inspection.' >&2; exit 2; } +trap 'rmdir "$lock" 2>/dev/null || true' EXIT +already=0 +if [ -f "$todo" ]; then + if awk -v id="$id" 'index($0, "- ID: " id) == 1 && $0 == "- ID: " id { found = 1 } END { if (!found) exit 1 }' "$todo"; then + copy_state="$(TASKFLOW_COPY_ID="$id" TASKFLOW_COPY_URL="$url" awk ' + function flush() { if (inside && found_id) { matches++; if (found_url && found_end) complete++; else if (found_url) incomplete++ } } + /^## / { flush(); inside = 1; found_id = found_url = found_end = 0 } + inside && $0 == "- ID: " ENVIRON["TASKFLOW_COPY_ID"] { found_id = 1 } + inside && $0 == "- External: " ENVIRON["TASKFLOW_COPY_URL"] { found_url = 1 } + inside && $0 == " <!-- taskflow-copy-complete:" ENVIRON["TASKFLOW_COPY_ID"] " -->" { found_end = 1 } + END { flush(); if (matches == 1 && complete == 1) print "complete"; else if (matches == 1 && incomplete == 1) print "incomplete"; else print "collision" } + ' "$todo")" + case "$copy_state" in + complete) echo 'Local copy already exists; remote Issue unchanged.'; already=1 ;; + incomplete) echo 'Local copy is incomplete; inspect the Todo before retrying.' >&2; exit 2 ;; + *) echo 'Local Todo ID collision; copy not started.' >&2; exit 2 ;; + esac + fi +else + [ ! -e "$todo" ] || { echo 'Invalid local Todo file.' >&2; exit 2; } + (set -o noclobber; printf '# Todo\n' > "$todo") || { echo 'Cannot create local Todo.' >&2; exit 2; } +fi +if [ "$already" -eq 0 ]; then +today="$(date +%Y-%m-%d)" +echo 'Copying remote Issue content into the local Todo; the remote Issue will remain unchanged.' >&2 +{ + printf '\n## Remote Issue copy\n\n- ID: %s\n- Status: %s\n- Priority: normal\n- Owner: Unknown\n- Source: remote copy (not synchronized)\n- Kind: capability\n- Added: %s\n- Updated: %s\n- Goal: %s\n- External: %s\n- Task: Not promoted.\n- Next action: Clarify and promote when ready.\n- Notes: Remote description copied below; this local entry is independent.\n' "$id" "$local_status" "$today" "$today" "$title" "$url" + printf '%s\n' "$body" | sed 's/^/ /' || exit 2 + printf ' <!-- taskflow-copy-complete:%s -->\n' "$id" +} >> "$todo" || { echo 'Local copy may be incomplete; inspect the Todo before retrying.' >&2; exit 2; } +fi +if [ -n "$origin_link" ]; then + marker="$origin_link.detached" + [ ! -L "$marker" ] || { echo 'Invalid detach record.' >&2; exit 2; } + if [ -e "$marker" ]; then + [ -f "$marker" ] && [ "$(cat "$marker")" = "$(printf 'detached\t%s' "$id")" ] || { echo 'Conflicting detach record.' >&2; exit 2; } + else + (set -o noclobber; printf 'detached\t%s\n' "$id" > "$marker") || { echo 'Local copy saved; detachment needs recovery.' >&2; exit 2; } + fi +fi +printf 'Copied remote Issue to local Todo %s; remote Issue unchanged.\n' "$id" \ No newline at end of file diff --git a/hooks/reopen b/hooks/reopen index 32a1dc1..8718373 100755 --- a/hooks/reopen +++ b/hooks/reopen @@ -61,6 +61,10 @@ entry_block() { # entry_block <needle> # so leaving the directory where it is beats moving it and reporting the rest. entry_block "\`$was\`" >/dev/null || { echo "Todo entry for $task missing; reopen not started"; exit 1; } +if [ -e "$docs/achieved/$task/remote-issue" ] || [ -L "$docs/achieved/$task/remote-issue" ]; then + bash "$HERE/remote-todo" stage-task "$task" promoted --root "$root" || { echo 'Remote stage update failed; local reopen was not started.' >&2; exit 1; } + echo 'If a remote stage changed and local reopen fails, inspect both sides before retrying.' >&2 +fi plan="$docs/achieved/$task/plan.md" todo_tmp="$todo.tmp" plan_tmp="$plan.tmp" diff --git a/hooks/smoke-test b/hooks/smoke-test index 63a52ff..15343c7 100755 --- a/hooks/smoke-test +++ b/hooks/smoke-test @@ -31,6 +31,11 @@ fi bash "$HERE/test-version-baseline" "$tmp/version-baseline-fixture" bash "$HERE/test-diagnostic-log" "$tmp/diagnostic-fixture" +bash "$HERE/test-json-field-numbers" +bash "$HERE/test-json-field-arrays" +for remote_case in todo todo-configure todo-list todo-mixed-list todo-snapshot todo-create todo-recover todo-concurrency todo-edit todo-stage todo-label todo-migrate todo-migrate-dedup todo-migrate-stage todo-to-local todo-detach todo-detach-task todo-detach-archived todo-promote todo-link todo-task-state todo-intake todo-reminders todo-archive todo-private; do + bash "$HERE/test-remote-$remote_case" "$tmp/remote-$remote_case" +done echo "== completion-gate decides the one shared answer ==" # `archive`, `task complete`, `task state in_progress` and `task progress` all diff --git a/hooks/smoke-test-windows.ps1 b/hooks/smoke-test-windows.ps1 index a4be0bd..5318d72 100755 --- a/hooks/smoke-test-windows.ps1 +++ b/hooks/smoke-test-windows.ps1 @@ -342,6 +342,14 @@ try { if ($LASTEXITCODE -ne 0) { throw 'Diagnostic log test failed' } & "$here\run-hook.cmd" test-host-parity $parityRoot if ($LASTEXITCODE -ne 0) { throw 'host parity fixture failed' } + foreach ($jsonTest in @('test-json-field-numbers', 'test-json-field-arrays')) { + & "$here\run-hook.cmd" $jsonTest + if ($LASTEXITCODE -ne 0) { throw "$jsonTest failed" } + } + foreach ($remoteTest in @('todo', 'todo-configure', 'todo-list', 'todo-mixed-list', 'todo-snapshot', 'todo-create', 'todo-recover', 'todo-concurrency', 'todo-edit', 'todo-stage', 'todo-label', 'todo-migrate', 'todo-migrate-dedup', 'todo-migrate-stage', 'todo-to-local', 'todo-detach', 'todo-detach-task', 'todo-detach-archived', 'todo-promote', 'todo-link', 'todo-task-state', 'todo-intake', 'todo-reminders', 'todo-archive', 'todo-private')) { + & "$here\run-hook.cmd" "test-remote-$remoteTest" (Join-Path $Root "remote-$remoteTest") + if ($LASTEXITCODE -ne 0) { throw "test-remote-$remoteTest failed" } + } } # `TASKFLOW_STDIN_SELFCHECK` stops the recursion: the child is this same # script and must not spawn a third. diff --git a/hooks/summarize-state b/hooks/summarize-state index bd187b6..d11d928 100755 --- a/hooks/summarize-state +++ b/hooks/summarize-state @@ -82,6 +82,7 @@ done if [ "${#active[@]}" -eq 0 ]; then recorded=0 [ -f "$todo" ] && recorded=1 + [ ! -e "$docs/remote-todos.conf" ] || recorded=1 if [ "$recorded" -eq 0 ]; then for dir in "$docs"/*/; do name="${dir%/}" @@ -91,20 +92,28 @@ if [ "${#active[@]}" -eq 0 ]; then break done fi - if [ "$recorded" -eq 1 ]; then - echo "# TaskFlow (no active task)" - echo "- Applicable work enters TaskFlow: a request to modify this repository or deliver a development artifact — a feature, fix, refactor, test, or configuration/build/CI change — is recorded first in TaskFlowDocs/todo.md." - else - echo "# TaskFlow (not started in this repository)" - echo "- Applicable work enters TaskFlow: a request to modify this repository or deliver a development artifact — a feature, fix, refactor, test, or configuration/build/CI change — is recorded first in TaskFlowDocs/todo.md." + if [ "$recorded" -eq 1 ]; then echo "# TaskFlow (no active task)" + else echo "# TaskFlow (not started in this repository)"; fi + backend=local + if [ -e "$docs/remote-todos.conf" ] || [ -L "$docs/remote-todos.conf" ]; then + backend="$(bash "$SCRIPT_DIR/remote-todo" mode --root "$repo_root" 2>/dev/null)" || backend=invalid fi + case "$backend" in + remote) + echo '- Todo backend: remote' + echo '- Applicable work enters TaskFlow: record the request in the selected configured remote Issue repository before task selection; retained local entries remain available.' ;; + invalid) + echo '- Todo backend: invalid' + echo '- Applicable work enters TaskFlow: repair the remote Todo configuration before choosing an intake location; do not silently switch backends.' ;; + *) echo "- Applicable work enters TaskFlow: a request to modify this repository or deliver a development artifact — a feature, fix, refactor, test, or configuration/build/CI change — is recorded first in TaskFlowDocs/todo.md." ;; + esac echo "- Read-only work (explanation, translation, status, research, review, diagnosis) does not, and an explicit \$taskflow request opts it in." fi # The Todo inbox is only worth printing when there is no active task to report, # or on request: the active-task list is the more useful default. if [ -f "$todo" ] && { [ "$verbose" = "1" ] || [ "${#active[@]}" -eq 0 ]; }; then - echo "# TaskFlow inbox (not done)" + if [ "${backend:-local}" = remote ]; then echo "# Retained local TaskFlow inbox (remote not fetched)"; else echo "# TaskFlow inbox (not done)"; fi awk ' function ltrim(s) { sub(/^[[:space:]]+/, "", s); return s } function rtrim(s) { sub(/[[:space:]]+$/, "", s); return s } diff --git a/hooks/task b/hooks/task index 725d9e7..17bc7c6 100755 --- a/hooks/task +++ b/hooks/task @@ -866,7 +866,12 @@ case "$command" in intake) split_root "$@" split_kind "${rest[@]+"${rest[@]}"}" - [ "${#rest[@]}" -ge 1 ] && [ "${#rest[@]}" -le 2 ] || fail 'usage: task intake <goal> [source] [--kind <defect|capability|internal>] [--root <path>]' + intake_local=0; intake_args=() + for arg in "${rest[@]+"${rest[@]}"}"; do + if [ "$arg" = --local ]; then intake_local=1; else intake_args+=("$arg"); fi + done + rest=("${intake_args[@]+"${intake_args[@]}"}") + [ "${#rest[@]}" -ge 1 ] && [ "${#rest[@]}" -le 2 ] || fail 'usage: task intake <goal> [source] [--local] [--kind <defect|capability|internal>] [--root <path>]' # Only `promote` makes this binding, so `intake` may leave it out. The # default is the majority case rather than a judgement about this request: # an inbox item may never be planned, and demanding a classification at the @@ -875,6 +880,10 @@ case "$command" in goal="${rest[0]}" if [ "${#rest[@]}" -eq 2 ]; then source="${rest[1]}"; else source="direct user request"; fi [ -n "$goal" ] || fail "goal must not be empty" + if [ "$intake_local" -eq 0 ] && { [ -e "$root/TaskFlowDocs/remote-todos.conf" ] || [ -L "$root/TaskFlowDocs/remote-todos.conf" ]; }; then + backend="$(bash "$SCRIPT_DIR/remote-todo" mode --root "$root")" || fail 'Invalid Todo backend configuration; intake was not started.' + [ "$backend" != remote ] || fail 'Remote Todo mode is selected; choose a configured source and create the Issue with remote-todo instead.' + fi # Who wrote the entry is the host that ran the command, and only the host can # answer that. The detection is the one `hooks/session-record` already uses, so # the Todo entry and the sessions index name the same agent. An unrecognized @@ -1234,6 +1243,10 @@ case "$command" in planning|ready) todo_status=promoted ;; *) todo_status=in_progress ;; esac + if [ -e "$base/remote-issue" ] || [ -L "$base/remote-issue" ]; then + bash "$SCRIPT_DIR/remote-todo" stage-task "$task" "$new_state" --root "$root" || fail 'Remote stage update failed; local state was not changed.' + echo 'If a remote stage changed and a later local write fails, inspect both sides before retrying.' >&2 + fi { printf 'Status\t%s\n' "$todo_status" printf 'Updated\t%s\n' "$today" @@ -2178,7 +2191,7 @@ STAGE_LINES run_awk_to "$plan" "$plan" TASKFLOW_CMD=docstatus TASKFLOW_VALUE=completed run_awk_to "$prd" "$prd" TASKFLOW_CMD=docstatus TASKFLOW_VALUE=completed archive_out="" - if ! archive_out="$(cd "$root" && "$bash_cmd" "$SCRIPT_DIR/archive" "$task" 2>&1)"; then + if ! archive_out="$(cd "$root" && "$bash_cmd" "$SCRIPT_DIR/archive" "$task" "$root" 2>&1)"; then # Best-effort rollback: each docstatus independently; report archive last. run_awk_to "$plan" "$plan" TASKFLOW_CMD=docstatus TASKFLOW_VALUE="$plan_prev" || true run_awk_to "$prd" "$prd" TASKFLOW_CMD=docstatus TASKFLOW_VALUE="$prd_prev" || true diff --git a/hooks/test-json-field-arrays b/hooks/test-json-field-arrays new file mode 100644 index 0000000..0156cc4 --- /dev/null +++ b/hooks/test-json-field-arrays @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -euo pipefail +HERE="$(cd "$(dirname "$0")" && pwd)" +reader="$HERE/json-field" +[ "$(printf '%s' '{"labels":["taskflow:inbox","user"]}' | bash "$reader" --array-has labels 'taskflow:inbox')" = yes ] || { echo 'FAIL missing label'; exit 1; } +[ "$(printf '%s' '{"labels":["user\ntaskflow:inbox"]}' | bash "$reader" --array-has labels 'taskflow:inbox')" = no ] || { echo 'FAIL newline spoof'; exit 1; } +[ "$(printf '%s' '{"body":"labels taskflow:inbox","labels":[]}' | bash "$reader" --array-has labels 'taskflow:inbox')" = no ] || { echo 'FAIL body spoof'; exit 1; } +[ "$(printf '%s' '{"labels":["测试"]}' | bash "$reader" --array-has labels '测试')" = yes ] || { echo 'FAIL unicode'; exit 1; } +if printf '%s' '{"labels":{"bad":"taskflow:inbox"}}' | bash "$reader" --array-has labels 'taskflow:inbox' >/dev/null 2>&1; then echo 'FAIL non-array accepted'; exit 1; fi +echo 'PASS json string array membership' \ No newline at end of file diff --git a/hooks/test-json-field-numbers b/hooks/test-json-field-numbers new file mode 100644 index 0000000..fc9f174 --- /dev/null +++ b/hooks/test-json-field-numbers @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -euo pipefail +HERE="$(cd "$(dirname "$0")" && pwd)" +reader="$HERE/json-field" +[ "$(printf '%s' '{"iid":17,"body":"number:99"}' | bash "$reader" iid)" = '' ] || { echo 'FAIL default changed'; exit 1; } +[ "$(printf '%s' '{"iid":17,"body":"number:99"}' | bash "$reader" --numbers iid)" = 17 ] || { echo 'FAIL number not read'; exit 1; } +[ "$(printf '%s' '{"iid":null,"body":"\"iid\":99"}' | bash "$reader" --numbers iid)" = '' ] || { echo 'FAIL string spoof'; exit 1; } +[ "$(printf '%s' '{"iid":true}' | bash "$reader" --numbers iid)" = '' ] || { echo 'FAIL boolean read as number'; exit 1; } +echo 'PASS optional number parsing' \ No newline at end of file diff --git a/hooks/test-remote-todo b/hooks/test-remote-todo new file mode 100644 index 0000000..2851bbb --- /dev/null +++ b/hooks/test-remote-todo @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# The caller supplies a new fixture directory; no existing files are removed. +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +remote="$HERE/remote-todo" +mode() { bash "$remote" mode --root "$fixture"; } +validate() { bash "$remote" validate --root "$fixture"; } +reject() { if "$@" >/dev/null 2>&1; then echo 'FAIL expected refusal' >&2; exit 1; fi; } +[ "$(mode)" = local ] || { echo 'FAIL missing config must default to local' >&2; exit 1; } +printf 'remote\nwork\tgithub\tgithub.com\towner/repo\nlab\tgitlab\tgit.example.org\tgroup/sub/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +[ "$(mode)" = remote ] || { echo 'FAIL remote mode' >&2; exit 1; } +validate >/dev/null +printf 'remote\nwork\tgithub\tgithub.com\towner/repo\nwork\tgitlab\tgitlab.com\tgroup/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +reject validate +printf 'remote\nwork\tgithub\tuser:password@github.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +reject validate +printf 'remote\nwork\tgithub\tgithub.com\towner/repo?token=secret\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +reject validate +printf 'remote\nwork\tgithub\tgithub.com\towner/repo\ninvalid\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +reject validate +echo 'PASS remote todo config' \ No newline at end of file diff --git a/hooks/test-remote-todo-archive b/hooks/test-remote-todo-archive new file mode 100644 index 0000000..56cdbce --- /dev/null +++ b/hooks/test-remote-todo-archive @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-archive <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/2026-11-16-demo" "$fixture/bin" "$fixture/tmp" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +git -C "$fixture" init -q +git -C "$fixture" config user.name 'Fixture Test' +git -C "$fixture" config user.email 'fixture@example.invalid' +printf 'bin/\ntmp/\nout\nreopen-out\nargs\nlabel\nstate\nTaskFlowDocs/.remote-todo-updates/\n' > "$fixture/.gitignore" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +git -C "$fixture" add .gitignore TaskFlowDocs/remote-todos.conf +git -C "$fixture" commit -qm baseline +printf 'github\tgithub.com\towner/repo\t17\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/remote-issue" +printf '# PRD\n> Task version: v1\n> Status: completed\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/prd.md" +cat > "$fixture/TaskFlowDocs/2026-11-16-demo/plan.md" <<'EOF' +# Plan +> Task version: v1 +> Status: completed + +## Approval +- Status: approved +- Approved by: user +- Approved at: 2026-11-16 12:00 +08:00 +- Approved version: v1 +- Approved scope: PRD / Spec / Plan + +## Change Log +EOF +printf '## Test\n\n- ID: TF-20261116-abcdef\n- Status: in_progress\n- Updated: 2026-11-16\n- Goal: demo\n- Task: `TaskFlowDocs/2026-11-16-demo/`\n- Next action: verify\n' > "$fixture/TaskFlowDocs/todo.md" +bash "$HERE/version-baseline" init "$fixture" 2026-11-16-demo v1 +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +printf '%s\n' "$@" >> "$TEST_FIXTURE/args" +case " $* " in + *' --method DELETE '*) : ;; + *' --method POST '*) case " $* " in *' labels[]=taskflow:done '*) printf done > "$TEST_FIXTURE/label" ;; *' labels[]=taskflow:promoted '*) printf promoted > "$TEST_FIXTURE/label" ;; esac ;; + *' --method PATCH '*) case " $* " in *' state=closed '*) printf closed > "$TEST_FIXTURE/state" ;; *) printf open > "$TEST_FIXTURE/state" ;; esac ;; + *) label=in_progress; state=open; [ ! -f "$TEST_FIXTURE/label" ] || label="$(cat "$TEST_FIXTURE/label")"; [ ! -f "$TEST_FIXTURE/state" ] || state="$(cat "$TEST_FIXTURE/state")"; printf '{"number":17,"state":"%s","labels":["taskflow:%s","user-label"]}\n' "$state" "$label" ;; +esac +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TMPDIR="$fixture/tmp" bash "$HERE/archive" 2026-11-16-demo "$fixture" > "$fixture/out" +[ "$(cat "$fixture/state")" = closed ] || { echo 'FAIL archive did not close Issue'; exit 1; } +[ "$(cat "$fixture/label")" = done ] || { echo 'FAIL archive did not set done'; exit 1; } +[ -f "$fixture/TaskFlowDocs/achieved/2026-11-16-demo/remote-issue" ] || { echo 'FAIL missing archived association'; exit 1; } +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TMPDIR="$fixture/tmp" bash "$HERE/reopen" 2026-11-16-demo "$fixture" > "$fixture/reopen-out" +[ "$(cat "$fixture/state")" = open ] || { echo 'FAIL reopen did not open Issue'; exit 1; } +[ "$(cat "$fixture/label")" = promoted ] || { echo 'FAIL reopen did not set promoted'; exit 1; } +[ -f "$fixture/TaskFlowDocs/2026-11-16-demo/remote-issue" ] || { echo 'FAIL missing active association'; exit 1; } +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +exit 1 +EOF +chmod +x "$fixture/bin/gh" +before="$(cksum < "$fixture/TaskFlowDocs/todo.md")" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TMPDIR="$fixture/tmp" bash "$HERE/archive" 2026-11-16-demo "$fixture" > "$fixture/out" 2>&1; then echo 'FAIL archive succeeded after remote failure'; exit 1; fi +[ -d "$fixture/TaskFlowDocs/2026-11-16-demo" ] || { echo 'FAIL archive moved task despite remote failure'; exit 1; } +[ "$(cksum < "$fixture/TaskFlowDocs/todo.md")" = "$before" ] || { echo 'FAIL Todo changed after remote failure'; exit 1; } +echo 'PASS remote todo archive and reopen' \ No newline at end of file diff --git a/hooks/test-remote-todo-concurrency b/hooks/test-remote-todo-concurrency new file mode 100644 index 0000000..9e19c83 --- /dev/null +++ b/hooks/test-remote-todo-concurrency @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-concurrency <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +printf '.remote-todo-operations/\n.remote-todo-updates/\n' > "$fixture/TaskFlowDocs/.gitignore" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\na\tgithub\tgithub.com\towner/repo\nb\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf 'content\n' > "$fixture/body" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf 'call\n' >> "$TEST_FIXTURE/calls" +cat >/dev/null +sleep 2 +printf '%s\n' '{"number":17}' +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" create a 'Example' "$fixture/body" --root "$fixture" > "$fixture/out1" 2>&1 & +first=$! +for attempt in 1 2 3 4 5 6 7 8 9 10; do [ ! -f "$fixture/calls" ] || break; sleep 1; done +[ -f "$fixture/calls" ] || { echo 'FAIL first create did not reach server'; wait "$first" || true; exit 1; } +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" create b 'Example' "$fixture/body" --root "$fixture" > "$fixture/out2" 2>&1; then + echo 'FAIL concurrent create accepted'; wait "$first" || true; exit 1 +fi +wait "$first" +[ "$(wc -l < "$fixture/calls" | tr -d ' ')" = 1 ] || { echo 'FAIL duplicate request'; exit 1; } +echo 'PASS remote todo concurrent create' \ No newline at end of file diff --git a/hooks/test-remote-todo-configure b/hooks/test-remote-todo-configure new file mode 100644 index 0000000..3288a34 --- /dev/null +++ b/hooks/test-remote-todo-configure @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-configure <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +cmd="$HERE/remote-todo" +bash "$cmd" connect main github github.com owner/repo --root "$fixture" >/dev/null +[ "$(bash "$cmd" mode --root "$fixture")" = local ] || { echo 'FAIL default mode changed'; exit 1; } +bash "$cmd" connect lab gitlab git.example.org group/sub/repo --root "$fixture" >/dev/null +bash "$cmd" mode-set remote --root "$fixture" >/dev/null +[ "$(bash "$cmd" mode --root "$fixture")" = remote ] || { echo 'FAIL mode change'; exit 1; } +grep -q 'owner/repo' "$fixture/TaskFlowDocs/remote-todos.conf" || { echo 'FAIL lost GitHub source'; exit 1; } +grep -q 'group/sub/repo' "$fixture/TaskFlowDocs/remote-todos.conf" || { echo 'FAIL lost GitLab source'; exit 1; } +before="$(cksum < "$fixture/TaskFlowDocs/remote-todos.conf")" +if bash "$cmd" connect main github github.com other/repo --root "$fixture" >/dev/null 2>&1; then echo 'FAIL duplicate accepted'; exit 1; fi +[ "$(cksum < "$fixture/TaskFlowDocs/remote-todos.conf")" = "$before" ] || { echo 'FAIL modified config on duplicate'; exit 1; } +if bash "$cmd" connect evil github 'user:secret@github.com' owner/repo --root "$fixture" >/dev/null 2>&1; then echo 'FAIL credentials accepted'; exit 1; fi +[ "$(cksum < "$fixture/TaskFlowDocs/remote-todos.conf")" = "$before" ] || { echo 'FAIL modified config on invalid input'; exit 1; } +malicious="$(printf 'owner/repo\ninjected\tgithub\tgithub.com\tother/repo')" +if bash "$cmd" connect evil github github.com "$malicious" --root "$fixture" >/dev/null 2>&1; then echo 'FAIL newline injected extra source'; exit 1; fi +[ "$(cksum < "$fixture/TaskFlowDocs/remote-todos.conf")" = "$before" ] || { echo 'FAIL config changed after newline injection'; exit 1; } +echo 'PASS remote todo configure' \ No newline at end of file diff --git a/hooks/test-remote-todo-create b/hooks/test-remote-todo-create new file mode 100644 index 0000000..5ffa690 --- /dev/null +++ b/hooks/test-remote-todo-create @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-create <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +printf '.remote-todo-operations/\n.remote-todo-updates/\n' > "$fixture/TaskFlowDocs/.gitignore" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\nothercase\tgithub\tGITHUB.COM\tOWNER/REPO\nlab\tgitlab\tgit.example.org\tgroup/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf 'Text with \\ and "quotes"\n' > "$fixture/body" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +cat > "$TEST_FIXTURE/sent" +printf '%s\n' '{"number":17,"title":"result"}' +EOF +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +cat > "$TEST_FIXTURE/sent-gitlab" +printf '%s\n' '{"iid":19,"title":"result"}' +EOF +chmod +x "$fixture/bin/gh" "$fixture/bin/glab" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" create main 'Example title' "$fixture/body" --root "$fixture" > "$fixture/out" +grep -q '17' "$fixture/out" || { echo 'FAIL missing created number'; exit 1; } +grep -q 'Text with' "$fixture/sent" || { echo 'FAIL missing content'; exit 1; } +grep -q 'taskflow-operation:' "$fixture/sent" || { echo 'FAIL missing operation marker'; exit 1; } +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" create lab 'Example title' "$fixture/body" --root "$fixture" > "$fixture/out-lab" +grep -q '19' "$fixture/out-lab" || { echo 'FAIL missing GitLab number'; exit 1; } +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +cat > /dev/null +exit 1 +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" create main 'Maybe created' "$fixture/body" --root "$fixture" > "$fixture/out-failed" 2>&1; then + echo 'FAIL uncertain response reported success'; exit 1 +fi +grep -qi 'unknown' "$fixture/out-failed" || { echo 'FAIL unknown result not reported'; exit 1; } +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" create othercase 'Maybe created' "$fixture/body" --root "$fixture" > "$fixture/out-repeat" 2>&1; then + echo 'FAIL unresolved operation allowed retry'; exit 1 +fi +grep -qi 'unresolved' "$fixture/out-repeat" || { echo 'FAIL unresolved operation not reported'; exit 1; } +if grep -R 'Maybe created' "$fixture/TaskFlowDocs/.remote-todo-operations" >/dev/null 2>&1; then echo 'FAIL operation record contains issue title'; exit 1; fi +echo 'PASS remote todo create' \ No newline at end of file diff --git a/hooks/test-remote-todo-detach b/hooks/test-remote-todo-detach new file mode 100644 index 0000000..c20987e --- /dev/null +++ b/hooks/test-remote-todo-detach @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-detach <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/remote-todo-links" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf '## Existing\n- ID: TF-20261116-abcdef\n- Status: inbox\n- Goal: Old local title\n- Task: Not promoted.\n' > "$fixture/TaskFlowDocs/todo.md" +printf 'pending\t0123456789abcdef0123456789abcdef\tgithub\tgithub.com\towner/repo\nconfirmed\t17\nstage-synced\tinbox\n' > "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef" +printf 'pending\t0123456789abcdef0123456789abcdef\tgithub\tgithub.com\towner/repo\nconfirmed\t17\nstage-synced\tinbox\n' > "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-deadbe.history-0123456789abcdef0123456789abcdef" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case " $* " in *' --method '*) echo 'unexpected remote mutation' >&2; exit 1 ;; esac +printf '%s\n' '{"number":17,"title":"Updated remote title","body":"Remote body","state":"open","created_at":"2026-01-02T00:00:00Z","labels":["taskflow:inbox"]}' +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --root "$fixture" > "$fixture/refused" 2>&1; then echo 'FAIL implicit detach'; exit 1; fi +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --detach --root "$fixture" > "$fixture/out" +grep -q 'Old local title' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL original lost'; exit 1; } +grep -q 'Updated remote title' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL remote copy missing'; exit 1; } +[ -f "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.detached" ] || { echo 'FAIL missing detach record'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" list --root "$fixture" > "$fixture/list" +if grep -q 'Old local title' "$fixture/list"; then echo 'FAIL duplicate previous local entry'; exit 1; fi +grep -q 'Updated remote title' "$fixture/list" || { echo 'FAIL new local entry hidden'; exit 1; } +second="$fixture/retry-case" +mkdir -p "$second/TaskFlowDocs/remote-todo-links" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$second/TaskFlowDocs/remote-todos.conf" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --root "$second" > "$fixture/copied-first" +printf '\n## Original\n- ID: TF-20261116-fedcba\n- Status: inbox\n- Goal: Old local title\n- Task: Not promoted.\n' >> "$second/TaskFlowDocs/todo.md" +printf 'pending\t0123456789abcdef0123456789abcdef\tgithub\tgithub.com\towner/repo\nconfirmed\t17\nstage-synced\tinbox\n' > "$second/TaskFlowDocs/remote-todo-links/TF-20261116-fedcba" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --detach --root "$second" > "$fixture/resumed" +[ -f "$second/TaskFlowDocs/remote-todo-links/TF-20261116-fedcba.detached" ] || { echo 'FAIL did not finish interrupted detach'; exit 1; } +echo 'PASS remote todo detach' \ No newline at end of file diff --git a/hooks/test-remote-todo-detach-archived b/hooks/test-remote-todo-detach-archived new file mode 100644 index 0000000..70f9cd4 --- /dev/null +++ b/hooks/test-remote-todo-detach-archived @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-detach-archived <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/achieved/2026-11-16-demo" "$fixture/bin" "$fixture/tmp" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf 'github\tgithub.com\towner/repo\t17\n' > "$fixture/TaskFlowDocs/achieved/2026-11-16-demo/remote-issue" +printf '# PRD\n> Task version: v1\n> Status: completed\n' > "$fixture/TaskFlowDocs/achieved/2026-11-16-demo/prd.md" +printf '# Plan\n> Task version: v1\n> Status: completed\n\n## Change Log\n' > "$fixture/TaskFlowDocs/achieved/2026-11-16-demo/plan.md" +printf '## Task\n- ID: TF-20261116-abcdef\n- Status: done\n- Goal: Original goal\n- Task: `TaskFlowDocs/achieved/2026-11-16-demo/`\n- Next action: None\n' > "$fixture/TaskFlowDocs/todo.md" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +printf '%s\n' '{"number":17,"title":"Latest","body":"Archived copy","state":"closed","created_at":"2026-01-02T00:00:00Z","labels":["taskflow:done"]}' +EOF +chmod +x "$fixture/bin/gh" +before="$(cksum < "$fixture/TaskFlowDocs/achieved/2026-11-16-demo/plan.md")" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --detach --root "$fixture" > "$fixture/out" +[ "$(cksum < "$fixture/TaskFlowDocs/achieved/2026-11-16-demo/plan.md")" = "$before" ] || { echo 'FAIL archived task was changed'; exit 1; } +grep -q 'Archived copy' "$fixture/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo-copy.md" || { echo 'FAIL missing reference copy'; exit 1; } +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +exit 1 +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TMPDIR="$fixture/tmp" bash "$HERE/reopen" 2026-11-16-demo "$fixture" > "$fixture/reopen" || { echo 'FAIL detached task reopen required remote'; exit 1; } +[ -f "$fixture/TaskFlowDocs/2026-11-16-demo/remote-issue" ] || { echo 'FAIL task not reopened'; exit 1; } +echo 'PASS remote todo archived detach' \ No newline at end of file diff --git a/hooks/test-remote-todo-detach-task b/hooks/test-remote-todo-detach-task new file mode 100644 index 0000000..984cceb --- /dev/null +++ b/hooks/test-remote-todo-detach-task @@ -0,0 +1,104 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-detach-task <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/2026-11-16-demo" "$fixture/TaskFlowDocs/remote-todo-links" "$fixture/bin" "$fixture/tmp" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf 'github\tgithub.com\towner/repo\t17\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/remote-issue" +printf 'pending\t0123456789abcdef0123456789abcdef\tgithub\tgithub.com\towner/repo\nconfirmed\t17\nstage-synced\tplanning\n' > "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef" +printf '# PRD\n> Task version: v1\n> Status: planning\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/prd.md" +printf '# Plan\n> Task version: v1\n> Status: planning\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/plan.md" +printf '## Task\n- ID: TF-20261116-abcdef\n- Status: promoted\n- Updated: 2026-11-16\n- Goal: Original goal\n- Task: `TaskFlowDocs/2026-11-16-demo/`\n' > "$fixture/TaskFlowDocs/todo.md" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +case " $* " in *' --method '*) echo 'unexpected remote mutation' >&2; exit 1 ;; esac +printf '%s\n' '{"number":17,"title":"Latest remote title","body":"Latest remote description","state":"open","created_at":"2026-01-02T00:00:00Z","labels":["taskflow:planning"]}' +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --root "$fixture" >/dev/null 2>&1; then echo 'FAIL implicit task detach'; exit 1; fi +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --detach --root "$fixture" > "$fixture/out" +grep -q 'Original goal' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL original task entry lost'; exit 1; } +grep -q 'Latest remote description' "$fixture/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo-copy.md" || { echo 'FAIL missing task copy'; exit 1; } +printf 'other\tgithub\tGITHUB.COM\tOWNER/REPO\n' >> "$fixture/TaskFlowDocs/remote-todos.conf" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local other 17 --detach --root "$fixture" >/dev/null || { echo 'FAIL case variant did not match detached Issue'; exit 1; } +[ -f "$fixture/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo.detached" ] || { echo 'FAIL missing task detach'; exit 1; } +grep -q "$(printf 'detached-task\t2026-11-16-demo')" "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.detached" || { echo 'FAIL missing Todo detach'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" list --root "$fixture" > "$fixture/list" +grep -q 'Original goal' "$fixture/list" || { echo 'FAIL detached local task hidden'; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/2026-11-16-other" +printf '# PRD\n' > "$fixture/TaskFlowDocs/2026-11-16-other/prd.md" +printf '# Plan\n' > "$fixture/TaskFlowDocs/2026-11-16-other/plan.md" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" link main 17 2026-11-16-other --root "$fixture" > "$fixture/relink" || { echo 'FAIL detached issue could not link to another task'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --detach --root "$fixture" > "$fixture/detach-other" || { echo 'FAIL cannot detach active task after previous detachment'; exit 1; } +[ -f "$fixture/TaskFlowDocs/remote-todo-links/task-2026-11-16-other.detached" ] || { echo 'FAIL wrong task detached'; exit 1; } +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +exit 1 +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TMPDIR="$fixture/tmp" bash "$HERE/task" state 2026-11-16-demo planning --root "$fixture" > "$fixture/state-out" || { echo 'FAIL detached task still required remote'; exit 1; } +grep -q '^> Status: planning' "$fixture/TaskFlowDocs/2026-11-16-demo/prd.md" || { echo 'FAIL local task state'; exit 1; } +second="$fixture/local-only" +mkdir -p "$second/TaskFlowDocs/2026-11-16-demo" "$second/TaskFlowDocs/remote-todo-links" "$second/tmp" +printf 'github\tgithub.com\towner/repo\t17\n' > "$second/TaskFlowDocs/2026-11-16-demo/remote-issue" +printf 'detached\tgithub\tgithub.com\towner/repo\t17\n' > "$second/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo.detached" +printf '# PRD\n> Task version: v1\n> Status: planning\n' > "$second/TaskFlowDocs/2026-11-16-demo/prd.md" +printf '# Plan\n> Task version: v1\n> Status: planning\n' > "$second/TaskFlowDocs/2026-11-16-demo/plan.md" +printf '## Task\n- ID: TF-20261116-abcdef\n- Status: promoted\n- Updated: 2026-11-16\n- Goal: Local\n- Task: `TaskFlowDocs/2026-11-16-demo/`\n' > "$second/TaskFlowDocs/todo.md" +PATH="$fixture/bin:$PATH" TMPDIR="$second/tmp" bash "$HERE/task" state 2026-11-16-demo planning --root "$second" >/dev/null || { echo 'FAIL detached task needs remote config'; exit 1; } +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$second/TaskFlowDocs/remote-todos.conf" +printf 'pending\t0123456789abcdef0123456789abcdef\tgithub\tgithub.com\towner/repo\nconfirmed\t17\nstage-synced\tplanning\n' > "$second/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef" +printf 'detached-task\t2026-11-16-demo\n' > "$second/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.detached" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case " $* " in *' --method '*) echo 'unexpected remote mutation' >&2; exit 1 ;; esac +printf '{"number":17,"title":"Remote","body":"new body","created_at":"2026-01-02T00:00:00Z","state":"open","labels":["taskflow:%s"]}\n' "${REMOTE_PHASE:-planning}" +EOF +chmod +x "$fixture/bin/gh" +mkdir -p "$fixture/TaskFlowDocs/2026-11-16-third" +printf 'github\tgithub.com\towner/repo\t17\n' > "$fixture/TaskFlowDocs/2026-11-16-third/remote-issue" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-task 2026-11-16-demo --root "$fixture" > "$fixture/reattach-conflict" 2>&1; then echo 'FAIL reattached an Issue already linked elsewhere'; exit 1; fi +grep -qi 'another task' "$fixture/reattach-conflict" || { echo 'FAIL missing association conflict'; exit 1; } +if PATH="$fixture/bin:$PATH" REMOTE_PHASE=ready bash "$HERE/remote-todo" reattach-task 2026-11-16-demo --root "$second" > "$fixture/reattach-mismatch" 2>&1; then echo 'FAIL reattached differing phases'; exit 1; fi +[ -f "$second/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo.detached" ] || { echo 'FAIL phase mismatch lost detachment'; exit 1; } +mkdir "$second/TaskFlowDocs/remote-todo-links/.lock-TF-20261116-abcdef" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-task 2026-11-16-demo --root "$second" > "$fixture/reattach-busy" 2>&1; then echo 'FAIL reattached while Todo locked'; exit 1; fi +[ -d "$second/TaskFlowDocs/remote-todo-links/.lock-TF-20261116-abcdef" ] || { echo 'FAIL removed another operation lock'; exit 1; } +rmdir "$second/TaskFlowDocs/remote-todo-links/.lock-TF-20261116-abcdef" +printf '# Old copy\n- Source: https://github.com/owner/repo/issues/17\n old body\n <!-- taskflow-copy-complete:task-2026-11-16-demo -->\n' > "$second/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo-copy.md" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-task 2026-11-16-demo --root "$second" > "$fixture/reattach" || { echo 'FAIL could not reattach task'; exit 1; } +[ ! -e "$second/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo.detached" ] || { echo 'FAIL task remains detached'; exit 1; } +compgen -G "$second/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo.detached.history-*" >/dev/null || { echo 'FAIL detach history lost'; exit 1; } +[ ! -e "$second/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.detached" ] || { echo 'FAIL Todo mapping remains detached'; exit 1; } +compgen -G "$second/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.detached.history-*" >/dev/null || { echo 'FAIL Todo detach history lost'; exit 1; } +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" stage-task 2026-11-16-demo planning --root "$second" > "$fixture/resync" 2>&1; then echo 'FAIL reattached task did not attempt remote synchronization'; exit 1; fi +grep -qi 'unknown' "$fixture/resync" || { echo 'FAIL remote update attempt not reported'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --detach --root "$second" >/dev/null || { echo 'FAIL repeat detach'; exit 1; } +grep -q 'new body' "$second/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo-copy.md" || { echo 'FAIL repeat detach did not refresh copy'; exit 1; } +[ -f "$second/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.detached" ] || { echo 'FAIL repeat detach did not detach Todo mapping'; exit 1; } +compgen -G "$second/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo-copy.md.history-*" >/dev/null || { echo 'FAIL previous copy not retained'; exit 1; } +lab="$fixture/lab" +mkdir -p "$lab/TaskFlowDocs/2026-11-16-demo" "$lab/TaskFlowDocs/remote-todo-links" +printf 'remote\nlab\tgitlab\tgit.example.org\tgroup/repo\n' > "$lab/TaskFlowDocs/remote-todos.conf" +printf 'gitlab\tgit.example.org\tgroup/repo\t19\n' > "$lab/TaskFlowDocs/2026-11-16-demo/remote-issue" +printf 'detached\tgitlab\tgit.example.org\tgroup/repo\t19\n' > "$lab/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo.detached" +printf '# PRD\n> Status: planning\n' > "$lab/TaskFlowDocs/2026-11-16-demo/prd.md" +printf '# Plan\n> Status: planning\n' > "$lab/TaskFlowDocs/2026-11-16-demo/plan.md" +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +case "$2" in *group%2Frepo/issues/19) ;; *) exit 1 ;; esac +printf '%s\n' '{"iid":19,"state":"opened","labels":["taskflow:planning"]}' +EOF +chmod +x "$fixture/bin/glab" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-task 2026-11-16-demo --root "$lab" > "$fixture/reattach-lab" || { echo 'FAIL GitLab reattachment'; exit 1; } +[ ! -e "$lab/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo.detached" ] || { echo 'FAIL GitLab task remains detached'; exit 1; } +echo 'PASS remote todo task detach' \ No newline at end of file diff --git a/hooks/test-remote-todo-edit b/hooks/test-remote-todo-edit new file mode 100644 index 0000000..d7aa7c4 --- /dev/null +++ b/hooks/test-remote-todo-edit @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-edit <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +printf '.remote-todo-operations/\n.remote-todo-updates/\n' > "$fixture/TaskFlowDocs/.gitignore" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\nlab\tgitlab\tgit.example.org\tgroup/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$@" >> "$TEST_FIXTURE/args" +case " $* " in + *' --method PATCH '*) printf '%s\n' updated > "$TEST_FIXTURE/written" ;; +esac +if [ -f "$TEST_FIXTURE/written" ]; then printf '%s\n' '{"number":17,"title":"New title"}'; else printf '%s\n' '{"number":17,"title":"Old title"}'; fi +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" edit main 17 title 'New title' --root "$fixture" > "$fixture/out" +grep -q '17' "$fixture/out" || { echo 'FAIL missing identity'; exit 1; } +grep -q 'title=New title' "$fixture/args" || { echo 'FAIL missing requested field'; exit 1; } +if grep -q '^labels=' "$fixture/args"; then echo 'FAIL unrelated labels sent'; exit 1; fi +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case " $* " in *' --method PATCH '*) exit 1 ;; esac +printf '%s\n' '{"number":17,"title":"Old title"}' +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" edit main 17 title 'Other title' --root "$fixture" > "$fixture/failed" 2>&1; then echo 'FAIL write failure reported success'; exit 1; fi +grep -qi 'unknown' "$fixture/failed" || { echo 'FAIL missing unknown result'; exit 1; } +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" edit main 17 title 'Other title' --root "$fixture" > "$fixture/retry" 2>&1; then echo 'FAIL unresolved update allowed'; exit 1; fi +grep -qi 'unresolved' "$fixture/retry" || { echo 'FAIL unresolved update not reported'; exit 1; } +id="$(sed -n 's/.*operation \([0-9a-f]*\);.*/\1/p' "$fixture/failed" | head -1)" +[ -n "$id" ] || { echo 'FAIL missing operation ID'; exit 1; } +if bash "$HERE/remote-todo" ack-update "$id" --root "$fixture" >/dev/null 2>&1; then echo 'FAIL missing explicit confirmation'; exit 1; fi +bash "$HERE/remote-todo" ack-update "$id" --checked --root "$fixture" >/dev/null +grep -q 'reviewed' "$fixture/TaskFlowDocs/.remote-todo-updates/$id" || { echo 'FAIL update not acknowledged'; exit 1; } +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$@" >> "$TEST_FIXTURE/glab-args" +case " $* " in + *' --method PUT '*) cat > "$TEST_FIXTURE/glab-body"; printf '%s\n' updated > "$TEST_FIXTURE/glab-written" ;; +esac +if [ -f "$TEST_FIXTURE/glab-written" ]; then printf '%s\n' '{"iid":19,"description":"New body"}'; else printf '%s\n' '{"iid":19,"description":"Old body"}'; fi +EOF +chmod +x "$fixture/bin/glab" +printf 'New body' > "$fixture/new-body" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" edit lab 19 body "$fixture/new-body" --root "$fixture" > "$fixture/out-lab" +grep -q 'group%2Frepo' "$fixture/glab-args" || { echo 'FAIL GitLab path encoding'; exit 1; } +[ "$(cat "$fixture/glab-body")" = 'New body' ] || { echo 'FAIL GitLab body'; exit 1; } +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case " $* " in *' --method PATCH '*) cat > "$TEST_FIXTURE/github-body"; printf yes > "$TEST_FIXTURE/github-body-written" ;; esac +if [ -f "$TEST_FIXTURE/github-body-written" ]; then printf '%s\n' '{"number":20,"body":"New body\n\n<!-- taskflow-migration:TF-20261116-aabbcc -->\n"}'; else printf '%s\n' '{"number":20,"body":"Old body\n\n<!-- taskflow-migration:TF-20261116-aabbcc -->"}'; fi +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" edit main 20 body "$fixture/new-body" --root "$fixture" > "$fixture/out-migration" +grep -Fq '<!-- taskflow-migration:TF-20261116-aabbcc -->' "$fixture/github-body" || { echo 'FAIL body edit lost migration identity'; exit 1; } +echo 'PASS remote todo edit' \ No newline at end of file diff --git a/hooks/test-remote-todo-intake b/hooks/test-remote-todo-intake new file mode 100644 index 0000000..5eeae5e --- /dev/null +++ b/hooks/test-remote-todo-intake @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-intake <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/tmp" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +if TMPDIR="$fixture/tmp" bash "$HERE/task" intake 'New work' --root "$fixture" > "$fixture/remote" 2>&1; then echo 'FAIL remote mode silently created local entry'; exit 1; fi +[ ! -e "$fixture/TaskFlowDocs/todo.md" ] || { echo 'FAIL local Todo created in remote mode'; exit 1; } +grep -qi remote "$fixture/remote" || { echo 'FAIL remote guidance missing'; exit 1; } +TMPDIR="$fixture/tmp" bash "$HERE/task" intake 'Explicit local work' --local --root "$fixture" > "$fixture/explicit-local" +grep -q 'Explicit local work' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL explicit local override'; exit 1; } +printf 'broken\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +if TMPDIR="$fixture/tmp" bash "$HERE/task" intake 'New work' --root "$fixture" > "$fixture/invalid" 2>&1; then echo 'FAIL invalid config silently defaulted'; exit 1; fi +if grep -q 'New work' "$fixture/TaskFlowDocs/todo.md"; then echo 'FAIL local Todo created on invalid config'; exit 1; fi +printf 'local\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +TMPDIR="$fixture/tmp" bash "$HERE/task" intake 'New work' --root "$fixture" > "$fixture/local" +grep -q 'New work' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL local mode intake'; exit 1; } +echo 'PASS remote todo intake' \ No newline at end of file diff --git a/hooks/test-remote-todo-label b/hooks/test-remote-todo-label new file mode 100644 index 0000000..52f296a --- /dev/null +++ b/hooks/test-remote-todo-label @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-label <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +printf '.remote-todo-operations/\n.remote-todo-updates/\n' > "$fixture/TaskFlowDocs/.gitignore" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\nlab\tgitlab\tgit.example.org\tgroup/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$@" >> "$TEST_FIXTURE/gh-args" +case " $* " in + *' --method DELETE '*) printf yes > "$TEST_FIXTURE/gh-done" ;; + *) if [ -f "$TEST_FIXTURE/gh-done" ]; then printf '%s\n' '{"number":17,"labels":["unrelated"]}'; else printf '%s\n' '{"number":17,"labels":["needs review","unrelated"]}'; fi ;; +esac +EOF +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$@" >> "$TEST_FIXTURE/glab-args" +case " $* " in + *' --method PUT '*) printf yes > "$TEST_FIXTURE/glab-done" ;; + *) if [ -f "$TEST_FIXTURE/glab-done" ]; then printf '%s\n' '{"iid":19,"labels":["new label","unrelated"]}'; else printf '%s\n' '{"iid":19,"labels":["unrelated"]}'; fi ;; +esac +EOF +chmod +x "$fixture/bin/gh" "$fixture/bin/glab" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" label main 17 remove 'needs review' --root "$fixture" > "$fixture/gh-out" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" label lab 19 add 'new label' --root "$fixture" > "$fixture/glab-out" +grep -q 'labels/needs%20review' "$fixture/gh-args" || { echo 'FAIL label path encoding'; exit 1; } +grep -q 'add_labels=new label' "$fixture/glab-args" || { echo 'FAIL GitLab add label'; exit 1; } +if grep -q 'unrelated' "$fixture/gh-args" || grep -q 'unrelated' "$fixture/glab-args"; then echo 'FAIL unrelated label changed'; exit 1; fi +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" label lab 19 add 'one,two' --root "$fixture" >/dev/null 2>&1; then echo 'FAIL ambiguous GitLab label'; exit 1; fi +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" label main 17 add 'taskflow:done' --root "$fixture" >/dev/null 2>&1; then echo 'FAIL reserved stage label'; exit 1; fi +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$@" >> "$TEST_FIXTURE/utf-args" +case " $* " in + *' --method DELETE '*) printf yes > "$TEST_FIXTURE/utf-done" ;; + *) if [ -f "$TEST_FIXTURE/utf-done" ]; then printf '%s\n' '{"number":17,"labels":["unrelated"]}'; else printf '%s\n' '{"number":17,"labels":["测试","unrelated"]}'; fi ;; +esac +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" label main 17 remove '测试' --root "$fixture" > "$fixture/utf-out" +grep -q 'labels/%E6%B5%8B%E8%AF%95' "$fixture/utf-args" || { echo 'FAIL UTF-8 label encoding'; exit 1; } +echo 'PASS remote todo label' \ No newline at end of file diff --git a/hooks/test-remote-todo-link b/hooks/test-remote-todo-link new file mode 100644 index 0000000..bc019b1 --- /dev/null +++ b/hooks/test-remote-todo-link @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-link <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/2026-11-16-demo" "$fixture/TaskFlowDocs/2026-11-16-other" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf '# PRD\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/prd.md" +printf '# Plan\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/plan.md" +printf '# PRD\n' > "$fixture/TaskFlowDocs/2026-11-16-other/prd.md" +printf '# Plan\n' > "$fixture/TaskFlowDocs/2026-11-16-other/plan.md" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' '{"number":17,"title":"Example"}' +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" link main 17 2026-11-16-demo --root "$fixture" +grep -q "$(printf 'github\tgithub.com\towner/repo\t17')" "$fixture/TaskFlowDocs/2026-11-16-demo/remote-issue" || { echo 'FAIL wrong association'; exit 1; } +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" link main 17 2026-11-16-demo --root "$fixture" >/dev/null 2>&1; then echo 'FAIL overwrote association'; exit 1; fi +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" link main 17 2026-11-16-other --root "$fixture" >/dev/null 2>&1; then echo 'FAIL duplicate association accepted'; exit 1; fi +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" link main 19 2026-11-16-other --root "$fixture" >/dev/null 2>&1; then echo 'FAIL identity mismatch accepted'; exit 1; fi +[ ! -e "$fixture/TaskFlowDocs/2026-11-16-other/remote-issue" ] || { echo 'FAIL wrote bad association'; exit 1; } +echo 'PASS remote todo link' \ No newline at end of file diff --git a/hooks/test-remote-todo-list b/hooks/test-remote-todo-list new file mode 100644 index 0000000..3ffcf23 --- /dev/null +++ b/hooks/test-remote-todo-list @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-list <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\ngh-main\tgithub\tgithub.com\towner/repo\ngl-main\tgitlab\tgit.example.org\tgroup/sub/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$@" > "$TEST_ARGS/gh.args" +printf '%s\n' '{"number":7,"title":"github item"}' +EOF +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$@" > "$TEST_ARGS/glab.args" +printf '%s\n' '{"iid":7,"title":"gitlab item"}' +EOF +chmod +x "$fixture/bin/gh" "$fixture/bin/glab" +PATH="$fixture/bin:$PATH" TEST_ARGS="$fixture" bash "$HERE/remote-todo" list --root "$fixture" > "$fixture/out" +grep -q 'github item' "$fixture/out" || { echo 'FAIL missing GitHub issue'; exit 1; } +grep -q 'gitlab item' "$fixture/out" || { echo 'FAIL missing GitLab issue'; exit 1; } +grep -q 'gh-main' "$fixture/out" || { echo 'FAIL missing GitHub source'; exit 1; } +grep -q 'gl-main' "$fixture/out" || { echo 'FAIL missing GitLab source'; exit 1; } +grep -q 'group%2Fsub%2Frepo' "$fixture/glab.args" || { echo 'FAIL GitLab path encoding'; exit 1; } +grep -q 'pull_request' "$fixture/gh.args" || { echo 'FAIL GitHub PR filter'; exit 1; } +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +exit 1 +EOF +chmod +x "$fixture/bin/glab" +if PATH="$fixture/bin:$PATH" TEST_ARGS="$fixture" bash "$HERE/remote-todo" list --root "$fixture" > "$fixture/partial" 2>&1; then + echo 'FAIL partial failure was reported as complete'; exit 1 +fi +grep -q 'gl-main' "$fixture/partial" || { echo 'FAIL missing failed source'; exit 1; } +echo 'PASS remote todo list' \ No newline at end of file diff --git a/hooks/test-remote-todo-migrate b/hooks/test-remote-todo-migrate new file mode 100644 index 0000000..43b14c1 --- /dev/null +++ b/hooks/test-remote-todo-migrate @@ -0,0 +1,104 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-migrate <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf '.remote-todo-operations/\n.remote-todo-updates/\n' > "$fixture/TaskFlowDocs/.gitignore" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +cat > "$fixture/TaskFlowDocs/todo.md" <<'EOF' +## First + +- ID: TF-20261116-abcdef +- Status: inbox +- Goal: Move this task +- Task: Not promoted. +- Next action: clarify + +## Second + +- ID: TF-20261116-111111 +- Status: inbox +- Goal: Keep this private +- Task: Not promoted. +EOF +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'issues?'*) exit 0 ;; + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +endpoint="$2" +case "$endpoint" in + repos/owner/repo/issues) printf 'call\n' >> "$TEST_FIXTURE/calls"; cat > "$TEST_FIXTURE/sent"; printf '%s\n' '{"number":17,"title":"Move this task"}' ;; + */labels) number="${endpoint%/labels}"; number="${number##*/}"; printf yes > "$TEST_FIXTURE/stage-$number" ;; + */labels/*) : ;; + *) number="${endpoint##*/}"; case " $* " in *' --method PATCH '*) : ;; *) if [ -f "$TEST_FIXTURE/stage-$number" ]; then printf '{"number":%s,"state":"open","labels":["taskflow:inbox"]}\n' "$number"; else printf '{"number":%s,"state":"open","labels":[]}\n' "$number"; fi ;; esac ;; +esac +EOF +chmod +x "$fixture/bin/gh" +before="$(cksum < "$fixture/TaskFlowDocs/todo.md")" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-abcdef main --root "$fixture" > "$fixture/out" +[ "$(cksum < "$fixture/TaskFlowDocs/todo.md")" = "$before" ] || { echo 'FAIL original Todo changed'; exit 1; } +grep -q 'Move this task' "$fixture/sent" || { echo 'FAIL missing selected item'; exit 1; } +grep -Fq '<!-- taskflow-migration:TF-20261116-abcdef -->' "$fixture/sent" || { echo 'FAIL missing stable migration marker'; exit 1; } +if grep -q 'Keep this private' "$fixture/sent"; then echo 'FAIL sent another item'; exit 1; fi +grep -q '17' "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef" || { echo 'FAIL missing durable link'; exit 1; } +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-abcdef main --root "$fixture" > "$fixture/retry" +[ "$(wc -l < "$fixture/calls" | tr -d ' ')" = 1 ] || { echo 'FAIL duplicate migration'; exit 1; } +printf 'detached\tTF-20260102-123456\n' > "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.detached" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-abcdef main --root "$fixture" > "$fixture/detached-retry" 2>&1; then echo 'FAIL detached migration falsely reported linked'; exit 1; fi +grep -qi 'detached' "$fixture/detached-retry" || { echo 'FAIL missing detached guidance'; exit 1; } +id=0123456789abcdef0123456789abcdef +printf 'pending\t%s\tgithub\tgithub.com\towner/repo\n' "$id" > "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-111111" +printf 'pending\tgithub\tgithub.com\tother/repo\t%s\nconfirmed\t22\n' "$id" > "$fixture/TaskFlowDocs/.remote-todo-operations/$id" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-111111 main --root "$fixture" > "$fixture/mismatch" 2>&1; then echo 'FAIL accepted mismatched operation source'; exit 1; fi +printf 'pending\tgithub\tgithub.com\towner/repo\t%s\nconfirmed\t22\n' "$id" > "$fixture/TaskFlowDocs/.remote-todo-operations/$id" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-111111 main --root "$fixture" > "$fixture/recovered" +grep -q '22' "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-111111" || { echo 'FAIL recovery link'; exit 1; } +[ "$(wc -l < "$fixture/calls" | tr -d ' ')" = 1 ] || { echo 'FAIL recovery created another Issue'; exit 1; } +cat >> "$fixture/TaskFlowDocs/todo.md" <<'EOF' + +## Third + +- ID: TF-20261116-222222 +- Status: inbox +- Goal: Uncertain create +- Task: Not promoted. +EOF +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'issues?'*) exit 0 ;; + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +printf 'uncertain\n' >> "$TEST_FIXTURE/uncertain-calls" +cat >/dev/null +exit 1 +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-222222 main --root "$fixture" > "$fixture/uncertain" 2>&1; then echo 'FAIL uncertain migration succeeded'; exit 1; fi +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-222222 main --root "$fixture" > "$fixture/uncertain-retry" 2>&1; then echo 'FAIL uncertain migration retried'; exit 1; fi +[ "$(wc -l < "$fixture/uncertain-calls" | tr -d ' ')" = 1 ] || { echo 'FAIL repeated uncertain create'; exit 1; } +id="$(awk -F '\t' 'NR == 1 { print $2 }' "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-222222")" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'issues?'*) exit 0 ;; + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +endpoint="$2" +case "$endpoint" in + */labels) printf yes > "$TEST_FIXTURE/stage-31" ;; + */labels/*) : ;; + *) case " $* " in *' --method PATCH '*) : ;; *) if [ -f "$TEST_FIXTURE/stage-31" ]; then printf '{"number":31,"state":"open","labels":["taskflow:inbox"],"body":"<!-- taskflow-operation:%s -->"}\n' "${TEST_OPERATION:-}"; else printf '{"number":31,"state":"open","labels":[],"body":"<!-- taskflow-operation:%s -->"}\n' "${TEST_OPERATION:-}"; fi ;; esac ;; +esac +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TEST_OPERATION="$id" bash "$HERE/remote-todo" resolve "$id" 31 --root "$fixture" > "$fixture/resolved" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-222222 main --root "$fixture" > "$fixture/finished" +grep -q '31' "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-222222" || { echo 'FAIL uncertain recovery did not link'; exit 1; } +[ "$(wc -l < "$fixture/uncertain-calls" | tr -d ' ')" = 1 ] || { echo 'FAIL recovery created a duplicate'; exit 1; } +echo 'PASS remote todo migrate' \ No newline at end of file diff --git a/hooks/test-remote-todo-migrate-dedup b/hooks/test-remote-todo-migrate-dedup new file mode 100644 index 0000000..1a7dd3c --- /dev/null +++ b/hooks/test-remote-todo-migrate-dedup @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || exit 1 +fixture="$1" +[ ! -e "$fixture" ] || exit 1 +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\ngh\tgithub\tgithub.com\towner/repo\ngl\tgitlab\tgitlab.com\tgroup/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf '.remote-todo-operations/\n.remote-todo-updates/\n' > "$fixture/TaskFlowDocs/.gitignore" +printf '## Test\n\n- ID: TF-20261116-aabbcc\n- Status: inbox\n- Goal: Test cross worktree\n- Task: Not promoted.\n' > "$fixture/TaskFlowDocs/todo.md" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "$2" in + *'issues?'*) [ "${FAIL_READ:-0}" = 0 ] || exit 1; printf '%s\n' '{"number":23,"body":"<!-- taskflow-migration:TF-20261116-aabbcc -->"}' ;; + *) printf 'write\n' >> "$TEST_FIXTURE/writes"; exit 1 ;; +esac +EOF +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +case "$2" in + *'issues?'*) [ "${FAIL_READ:-0}" = 0 ] || exit 1; printf '%s\n' '{"iid":24,"description":"<!-- taskflow-migration:TF-20261116-aabbcc -->"}' ;; + *) printf 'write\n' >> "$TEST_FIXTURE/writes"; exit 1 ;; +esac +EOF +chmod +x "$fixture/bin/gh" "$fixture/bin/glab" +for source in gh gl; do + mkdir -p "$fixture/$source/TaskFlowDocs" + cp "$fixture/TaskFlowDocs/remote-todos.conf" "$fixture/TaskFlowDocs/todo.md" "$fixture/TaskFlowDocs/.gitignore" "$fixture/$source/TaskFlowDocs/" + if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-aabbcc "$source" --root "$fixture/$source" > "$fixture/$source.out" 2>&1; then echo 'FAIL duplicate migration succeeded'; exit 1; fi + grep -qi 'already.*migration\|migration.*already' "$fixture/$source.out" || { echo 'FAIL existing marker not reported'; exit 1; } +done +mkdir -p "$fixture/failure/TaskFlowDocs" +cp "$fixture/TaskFlowDocs/remote-todos.conf" "$fixture/TaskFlowDocs/todo.md" "$fixture/TaskFlowDocs/.gitignore" "$fixture/failure/TaskFlowDocs/" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" FAIL_READ=1 bash "$HERE/remote-todo" migrate-to-remote TF-20261116-aabbcc gh --root "$fixture/failure" > "$fixture/failure.out" 2>&1; then echo 'FAIL incomplete remote check succeeded'; exit 1; fi +grep -qi 'cannot check' "$fixture/failure.out" || { echo 'FAIL incomplete remote check not reported'; exit 1; } +mkdir -p "$fixture/failure-gl/TaskFlowDocs" +cp "$fixture/TaskFlowDocs/remote-todos.conf" "$fixture/TaskFlowDocs/todo.md" "$fixture/TaskFlowDocs/.gitignore" "$fixture/failure-gl/TaskFlowDocs/" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" FAIL_READ=1 bash "$HERE/remote-todo" migrate-to-remote TF-20261116-aabbcc gl --root "$fixture/failure-gl" > "$fixture/failure-gl.out" 2>&1; then echo 'FAIL incomplete GitLab check succeeded'; exit 1; fi +grep -qi 'cannot check' "$fixture/failure-gl.out" || { echo 'FAIL incomplete GitLab check not reported'; exit 1; } +[ ! -e "$fixture/writes" ] || { echo 'FAIL remote write despite duplicate'; exit 1; } +echo 'PASS cross-worktree migration check' diff --git a/hooks/test-remote-todo-migrate-stage b/hooks/test-remote-todo-migrate-stage new file mode 100644 index 0000000..869e81e --- /dev/null +++ b/hooks/test-remote-todo-migrate-stage @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-migrate-stage <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf '.remote-todo-operations/\n.remote-todo-updates/\n' > "$fixture/TaskFlowDocs/.gitignore" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf '## Completed\n\n- ID: TF-20261116-abcdef\n- Status: done\n- Goal: Old completed work\n- External: https://github.com/owner/old/issues/42\n- Task: Not promoted.\n <!-- taskflow-copy-complete:TF-20261116-abcdef -->\n' > "$fixture/TaskFlowDocs/todo.md" +mkdir -p "$fixture/TaskFlowDocs/remote-todo-links" +printf 'pending\t0123456789abcdef0123456789abcdef\tgithub\tgithub.com\towner/old\nconfirmed\t42\nremote-authoritative\tconfirmed\n' > "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef" +printf 'detached\tTF-20261116-abcdef\n' > "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.detached" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'issues?'*) exit 0 ;; + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +case "${2:-}" in *'issues?'*) exit 0 ;; esac +case " $* " in + *' repos/owner/repo/issues '--*|*' repos/owner/repo/issues --hostname '* ) cat >/dev/null; printf '%s\n' '{"number":17}' ;; + *' --method POST '*) printf done > "$TEST_FIXTURE/label" ;; + *' --method PATCH '*) printf closed > "$TEST_FIXTURE/state" ;; + *' --method DELETE '*) : ;; + *) if [ -f "$TEST_FIXTURE/label" ]; then printf '%s\n' '{"number":17,"state":"closed","labels":["taskflow:done"]}'; else printf '%s\n' '{"number":17,"state":"open","labels":[]}'; fi ;; +esac +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" migrate-to-remote TF-20261116-abcdef main --root "$fixture" > "$fixture/out" +[ -f "$fixture/label" ] && [ "$(cat "$fixture/label")" = done ] || { echo 'FAIL did not apply migrated status label'; exit 1; } +[ -f "$fixture/state" ] && [ "$(cat "$fixture/state")" = closed ] || { echo 'FAIL did not close completed Issue'; exit 1; } +compgen -G "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.history-*" >/dev/null || { echo 'FAIL previous association history lost'; exit 1; } +compgen -G "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef.detached.history-*" >/dev/null || { echo 'FAIL previous detach history lost'; exit 1; } +grep -Fq "$(printf 'confirmed\t17')" "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-abcdef" || { echo 'FAIL new source not associated'; exit 1; } +echo 'PASS remote todo migration stage' \ No newline at end of file diff --git a/hooks/test-remote-todo-mixed-list b/hooks/test-remote-todo-mixed-list new file mode 100644 index 0000000..798691b --- /dev/null +++ b/hooks/test-remote-todo-mixed-list @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-mixed-list <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/remote-todo-links" "$fixture/TaskFlowDocs/2026-11-16-linked" "$fixture/bin" "$fixture/local/TaskFlowDocs" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\nsecond\tgithub\tGITHUB.COM\tOWNER/REPO\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +cat > "$fixture/TaskFlowDocs/todo.md" <<'EOF' +## Local +- ID: TF-20261116-111111 +- Status: inbox +- Goal: Local only +- Task: Not promoted. + +## Migrated +- ID: TF-20261116-222222 +- Status: inbox +- Goal: Already migrated +- Task: Not promoted. + +## Linked task +- ID: TF-20261116-444444 +- Status: in_progress +- Goal: Already linked +- Task: `TaskFlowDocs/2026-11-16-linked/` +EOF +printf 'github\tgithub.com\towner/repo\t17\n' > "$fixture/TaskFlowDocs/2026-11-16-linked/remote-issue" +printf 'pending\t0123456789abcdef0123456789abcdef\tgithub\tgithub.com\towner/repo\nconfirmed\t17\nstage-synced\tinbox\n' > "$fixture/TaskFlowDocs/remote-todo-links/TF-20261116-222222" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' '{"number":17,"title":"Remote item"}' +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" list --root "$fixture" > "$fixture/out" +grep -q 'Local only' "$fixture/out" || { echo 'FAIL missing local item'; exit 1; } +grep -q 'Remote item' "$fixture/out" || { echo 'FAIL missing remote item'; exit 1; } +[ "$(grep -c 'Remote item' "$fixture/out")" = 1 ] || { echo 'FAIL duplicated remote issue across aliases'; exit 1; } +if grep -q 'Already migrated' "$fixture/out"; then echo 'FAIL duplicated linked local item'; exit 1; fi +if grep -q 'Already linked' "$fixture/out"; then echo 'FAIL duplicated linked task'; exit 1; fi +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +exit 1 +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" list --root "$fixture" > "$fixture/partial" 2>&1; then echo 'FAIL missing source was treated as complete'; exit 1; fi +grep -q 'Already migrated' "$fixture/partial" || { echo 'FAIL lost local item when source unavailable'; exit 1; } +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' '{"number":"invalid","title":"spoof"}' +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" list --root "$fixture" > "$fixture/invalid" 2>&1; then echo 'FAIL invalid identity treated as complete'; exit 1; fi +grep -q 'Already migrated' "$fixture/invalid" || { echo 'FAIL invalid response hid local item'; exit 1; } +printf '## Local\n- ID: TF-20261116-333333\n- Status: inbox\n- Goal: Offline item\n' > "$fixture/local/TaskFlowDocs/todo.md" +bash "$HERE/remote-todo" list --root "$fixture/local" > "$fixture/offline" +grep -q 'Offline item' "$fixture/offline" || { echo 'FAIL local-only list'; exit 1; } +echo 'PASS remote todo mixed list' \ No newline at end of file diff --git a/hooks/test-remote-todo-private b/hooks/test-remote-todo-private new file mode 100644 index 0000000..90b8791 --- /dev/null +++ b/hooks/test-remote-todo-private @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-private <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +git -C "$fixture" init -q +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf 'body' > "$fixture/body" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +cat >/dev/null +printf '%s\n' '{"number":17}' +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" create main 'title' "$fixture/body" --root "$fixture" > "$fixture/refused" 2>&1; then echo 'FAIL wrote without private ignore'; exit 1; fi +[ ! -e "$fixture/TaskFlowDocs/.remote-todo-operations" ] || { echo 'FAIL created nonprivate store'; exit 1; } +printf '.remote-todo-operations/.taskflow-probe\n' > "$fixture/TaskFlowDocs/.gitignore" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" create main 'title' "$fixture/body" --root "$fixture" > "$fixture/probe-refused" 2>&1; then echo 'FAIL checked only a fixed probe'; exit 1; fi +[ ! -e "$fixture/TaskFlowDocs/.remote-todo-operations" ] || { echo 'FAIL created partially ignored store'; exit 1; } +TASKFLOW_REPO_ROOT="$fixture" bash "$HERE/install-git-hygiene" +git -C "$fixture" check-ignore -q TaskFlowDocs/.remote-todo-operations/test || { echo 'FAIL operations not excluded'; exit 1; } +git -C "$fixture" check-ignore -q TaskFlowDocs/.remote-todo-updates/test || { echo 'FAIL updates not excluded'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" create main 'title' "$fixture/body" --root "$fixture" > "$fixture/out" +git -C "$fixture" status --porcelain --untracked-files=all > "$fixture/status" +if grep -q 'remote-todo-operations' "$fixture/status"; then echo 'FAIL private record appears in status'; exit 1; fi +printf 'tracked fixture\n' > "$fixture/TaskFlowDocs/.remote-todo-updates-tracked" +mkdir -p "$fixture/TaskFlowDocs/.remote-todo-operations" +printf 'tracked fixture\n' > "$fixture/TaskFlowDocs/.remote-todo-operations/tracked" +git -C "$fixture" add -f TaskFlowDocs/.remote-todo-operations/tracked +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" create main 'another' "$fixture/body" --root "$fixture" > "$fixture/tracked-refused" 2>&1; then echo 'FAIL tracked recovery storage accepted'; exit 1; fi +grep -qi tracked "$fixture/tracked-refused" || { echo 'FAIL tracked storage not reported'; exit 1; } +echo 'PASS remote todo private storage' \ No newline at end of file diff --git a/hooks/test-remote-todo-promote b/hooks/test-remote-todo-promote new file mode 100644 index 0000000..7888158 --- /dev/null +++ b/hooks/test-remote-todo-promote @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-promote <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/base/TaskFlowDocs" "$fixture/bin" "$fixture/tmp" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +git -C "$fixture/base" init -q +git -C "$fixture/base" config user.name 'Fixture Test' +git -C "$fixture/base" config user.email 'fixture@example.invalid' +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/base/TaskFlowDocs/remote-todos.conf" +printf 'TaskFlowDocs/.remote-todo-operations/\nTaskFlowDocs/.remote-todo-updates/\n' > "$fixture/base/.gitignore" +git -C "$fixture/base" add .gitignore TaskFlowDocs/remote-todos.conf +git -C "$fixture/base" commit -qm baseline +git -C "$fixture/base" worktree add -q -b feature/demo "$fixture/work" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +case " $* " in + *' --method DELETE '*) : ;; + *' --method POST '*) printf yes > "$TEST_FIXTURE/label" ;; + *' --method PATCH '*) : ;; + *) if [ -f "$TEST_FIXTURE/label" ]; then printf '%s\n' '{"number":17,"title":"Remote task","body":"body","state":"open","created_at":"2026-01-02T00:00:00Z","labels":["taskflow:planning"]}'; else printf '%s\n' '{"number":17,"title":"Remote task","body":"body","state":"open","created_at":"2026-01-02T00:00:00Z","labels":[]}'; fi ;; +esac +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TMPDIR="$fixture/tmp" bash "$HERE/remote-todo" promote main 17 2026-11-16-demo small --kind capability --root "$fixture/work" > "$fixture/out" +[ -f "$fixture/work/TaskFlowDocs/2026-11-16-demo/prd.md" ] || { echo 'FAIL missing task PRD'; exit 1; } +[ -f "$fixture/work/TaskFlowDocs/2026-11-16-demo/remote-issue" ] || { echo 'FAIL missing association'; exit 1; } +grep -q 'Remote task' "$fixture/work/TaskFlowDocs/2026-11-16-demo/prd.md" || { echo 'FAIL task goal'; exit 1; } +[ -f "$fixture/label" ] || { echo 'FAIL remote stage not synchronized'; exit 1; } +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TMPDIR="$fixture/tmp" bash "$HERE/remote-todo" promote main 17 2026-11-16-demo small --kind capability --root "$fixture/work" > "$fixture/retry" +[ "$(grep -c '^## Remote Issue ' "$fixture/work/TaskFlowDocs/todo.md")" = 1 ] || { echo 'FAIL duplicate local association'; exit 1; } +mkdir -p "$fixture/work/TaskFlowDocs/remote-todo-links" +printf 'detached\tgithub\tgithub.com\towner/repo\t17\n' > "$fixture/work/TaskFlowDocs/remote-todo-links/task-2026-11-16-demo.detached" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TMPDIR="$fixture/tmp" bash "$HERE/remote-todo" promote main 17 2026-11-16-demo small --kind capability --root "$fixture/work" > "$fixture/detached" 2>&1; then echo 'FAIL detached task reported as reconnected'; exit 1; fi +grep -qi 'detached' "$fixture/detached" || { echo 'FAIL detached task guidance'; exit 1; } +echo 'PASS remote todo promote' \ No newline at end of file diff --git a/hooks/test-remote-todo-recover b/hooks/test-remote-todo-recover new file mode 100644 index 0000000..2622bb1 --- /dev/null +++ b/hooks/test-remote-todo-recover @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-recover <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/.remote-todo-operations" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +id=0123456789abcdef0123456789abcdef +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf 'pending\tgithub\tgithub.com\towner/repo\t%s\n' "$id" > "$fixture/TaskFlowDocs/.remote-todo-operations/$id" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' '{"number":17,"body":"<!-- taskflow-operation:0123456789abcdef0123456789abcdef -->"}' +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" resolve "$id" 17 --root "$fixture" > "$fixture/out" +grep -q 'confirmed' "$fixture/out" || { echo 'FAIL no confirmation'; exit 1; } +grep -q "$(printf 'confirmed\t17')" "$fixture/TaskFlowDocs/.remote-todo-operations/$id" || { echo 'FAIL missing durable confirmation'; exit 1; } +id2=ffffffffffffffffffffffffffffffff +printf 'pending\tgithub\tgithub.com\towner/repo\t%s\n' "$id2" > "$fixture/TaskFlowDocs/.remote-todo-operations/$id2" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" resolve "$id2" 17 --root "$fixture" > /dev/null 2>&1; then echo 'FAIL unrelated issue accepted'; exit 1; fi +grep -q 'confirmed' "$fixture/TaskFlowDocs/.remote-todo-operations/$id2" && { echo 'FAIL unrelated issue recorded'; exit 1; } +if bash "$HERE/remote-todo" resolve-absent "$id2" --root "$fixture" >/dev/null 2>&1; then echo 'FAIL absence accepted without explicit confirmation'; exit 1; fi +bash "$HERE/remote-todo" resolve-absent "$id2" --checked --root "$fixture" > "$fixture/absent" +grep -q 'absent' "$fixture/TaskFlowDocs/.remote-todo-operations/$id2" || { echo 'FAIL missing absent decision'; exit 1; } +echo 'PASS remote todo recover' \ No newline at end of file diff --git a/hooks/test-remote-todo-reminders b/hooks/test-remote-todo-reminders new file mode 100644 index 0000000..b416205 --- /dev/null +++ b/hooks/test-remote-todo-reminders @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-reminders <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +git -C "$fixture" init -q +printf '# Todo\n' > "$fixture/TaskFlowDocs/todo.md" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +printf called > "$TEST_FIXTURE/network-called" +exit 1 +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TASKFLOW_REPO_ROOT="$fixture" bash "$HERE/summarize-state" > "$fixture/summary" +grep -q 'Todo backend: remote' "$fixture/summary" || { echo 'FAIL remote backend missing'; exit 1; } +if grep -q 'recorded first in TaskFlowDocs/todo.md' "$fixture/summary"; then echo 'FAIL remote mode forced local intake'; exit 1; fi +printf '{"hook_event_name":"UserPromptSubmit","cwd":"%s"}\n' "$fixture" | PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/prompt-reminder" > "$fixture/reminder" +grep -q 'remote' "$fixture/reminder" || { echo 'FAIL remote prompt guidance missing'; exit 1; } +if grep -q 'hooks/task intake' "$fixture/reminder"; then echo 'FAIL remote prompt forced local intake'; exit 1; fi +[ ! -e "$fixture/network-called" ] || { echo 'FAIL reminder accessed remote'; exit 1; } +printf 'local\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +TASKFLOW_REPO_ROOT="$fixture" bash "$HERE/summarize-state" > "$fixture/local" +grep -q 'recorded first in TaskFlowDocs/todo.md' "$fixture/local" || { echo 'FAIL local intake guidance changed'; exit 1; } +printf '{"hook_event_name":"UserPromptSubmit","cwd":"%s"}\n' "$fixture" | bash "$HERE/prompt-reminder" > "$fixture/local-reminder" +grep -q 'hooks/task intake' "$fixture/local-reminder" || { echo 'FAIL local prompt guidance changed'; exit 1; } +printf 'broken\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +TASKFLOW_REPO_ROOT="$fixture" bash "$HERE/summarize-state" > "$fixture/invalid" +grep -q 'Todo backend: invalid' "$fixture/invalid" || { echo 'FAIL invalid config silently defaulted'; exit 1; } +echo 'PASS remote todo reminders' \ No newline at end of file diff --git a/hooks/test-remote-todo-snapshot b/hooks/test-remote-todo-snapshot new file mode 100644 index 0000000..e33a771 --- /dev/null +++ b/hooks/test-remote-todo-snapshot @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-snapshot <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\ntest\tgithub\tgithub.com\towner/repo\nlab\tgitlab\tgit.example.org\tgroup/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf '## Local\n- ID: TF-20261116-aaaaaa\n- Status: inbox\n- Goal: Local secret\n' > "$fixture/TaskFlowDocs/todo.md" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${TEST_CASE:-good}" in + bad-state) printf '%s\n' '{"number":1,"state":"secret-state","updated_at":"2026-11-16T12:30:45Z"}' ;; + bad-time) printf '%s\n' '{"number":1,"state":"open","updated_at":"secret-time"}' ;; + *) printf '%s%s%s\n' '{"number":1,"state":"open","updated_at":"2026-11-16T12:30:45Z","title":"SECRET_TITLE","body":"SECRET_BODY ghp_' 'FAKECREDENTIAL1234567890' '","labels":["SECRET_LABEL"],"token":"SECRET_TOKEN"}'; printf '%s\n' '{"number":2,"state":"closed","updated_at":"2026-11-16T12:32:45+00:00"}' ;; +esac +EOF +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +[ "${TEST_CASE:-good}" != partial ] || exit 1 +printf '%s\n' '{"iid":19,"state":"opened","updated_at":"2026-11-16T13:31:46.123Z","title":"SECRET_GITLAB_TITLE","description":"SECRET_GITLAB_BODY","labels":["SECRET_GITLAB_LABEL"]}' +EOF +chmod +x "$fixture/bin/gh" "$fixture/bin/glab" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" snapshot "$fixture/snapshot" --root "$fixture" +grep -q '2026-11-16T12:30:45Z' "$fixture/snapshot" || { echo 'FAIL missing GitHub update time'; exit 1; } +grep -q '2026-11-16T13:31:46.123Z' "$fixture/snapshot" || { echo 'FAIL missing GitLab update time'; exit 1; } +grep -q 'UTC' "$fixture/snapshot" || { echo 'FAIL missing timestamp'; exit 1; } +grep -Fxq 'Source 1 (configuration order)' "$fixture/snapshot" && grep -Fxq 'Source 2 (configuration order)' "$fixture/snapshot" || { echo 'FAIL missing source order'; exit 1; } +grep -Eq '^2[[:space:]]+closed[[:space:]]+2026-11-16T12:32:45[+]00:00$' "$fixture/snapshot" || { echo 'FAIL closed Issue metadata'; exit 1; } +if grep -Eqi 'SECRET_|ghp_|Local secret|owner/repo|group/repo|"(title|body|labels|token)"' "$fixture/snapshot"; then echo 'FAIL snapshot exposed free text or credentials'; exit 1; fi +grep -Eq '^1[[:space:]]+open[[:space:]]+2026-11-16T12:30:45Z$' "$fixture/snapshot" || { echo 'FAIL GitHub snapshot fields'; exit 1; } +grep -Eq '^19[[:space:]]+open[[:space:]]+2026-11-16T13:31:46.123Z$' "$fixture/snapshot" || { echo 'FAIL GitLab snapshot fields'; exit 1; } +expected="$(printf 'Source 1 (configuration order)\n1\topen\t2026-11-16T12:30:45Z\n2\tclosed\t2026-11-16T12:32:45+00:00\nSource 2 (configuration order)\n19\topen\t2026-11-16T13:31:46.123Z\n')" +actual="$(tail -n +2 "$fixture/snapshot")" +[ "$actual" = "$expected" ] || { echo 'FAIL snapshot contains unexpected fields or values'; exit 1; } +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" snapshot "$fixture/snapshot" --root "$fixture"; then echo 'FAIL overwrote existing file'; exit 1; fi +for scenario in bad-state bad-time partial; do + if PATH="$fixture/bin:$PATH" TEST_CASE="$scenario" bash "$HERE/remote-todo" snapshot "$fixture/$scenario" --root "$fixture" > "$fixture/$scenario.out" 2>&1; then echo "FAIL $scenario snapshot accepted"; exit 1; fi + [ ! -e "$fixture/$scenario" ] || { echo "FAIL $scenario snapshot created"; exit 1; } +done +echo 'PASS remote todo snapshot privacy' diff --git a/hooks/test-remote-todo-stage b/hooks/test-remote-todo-stage new file mode 100644 index 0000000..3d8e469 --- /dev/null +++ b/hooks/test-remote-todo-stage @@ -0,0 +1,69 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-stage <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +printf '.remote-todo-operations/\n.remote-todo-updates/\n' > "$fixture/TaskFlowDocs/.gitignore" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\nlab\tgitlab\tgit.example.org\tgroup/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +printf '%s\n' "$@" >> "$TEST_FIXTURE/gh-args" +case " $* " in + *' --method DELETE '*) : ;; + *' --method POST '*) : ;; + *' --method PATCH '*) printf yes > "$TEST_FIXTURE/gh-done" ;; + *) if [ -f "$TEST_FIXTURE/gh-done" ]; then printf '%s\n' '{"number":17,"state":"closed","labels":["taskflow:done","user-label"]}'; else printf '%s\n' '{"number":17,"state":"open","labels":["taskflow:inbox","user-label"]}'; fi ;; +esac +EOF +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$@" >> "$TEST_FIXTURE/glab-args" +case " $* " in + *' --method PUT '*) printf yes > "$TEST_FIXTURE/glab-done" ;; + *) if [ -f "$TEST_FIXTURE/glab-done" ]; then printf '%s\n' '{"iid":19,"state":"closed","labels":["taskflow:done","user-label"]}'; else printf '%s\n' '{"iid":19,"state":"opened","labels":["taskflow:inbox","user-label"]}'; fi ;; +esac +EOF +chmod +x "$fixture/bin/gh" "$fixture/bin/glab" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" stage main 17 done --root "$fixture" > "$fixture/gh-out" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" stage lab 19 done --root "$fixture" > "$fixture/glab-out" +grep -q 'labels/taskflow%3Ainbox' "$fixture/gh-args" || { echo 'FAIL GitHub did not remove previous stage'; exit 1; } +grep -q 'taskflow:done' "$fixture/gh-args" || { echo 'FAIL GitHub missing new stage'; exit 1; } +grep -q 'add_labels=taskflow:done' "$fixture/glab-args" || { echo 'FAIL GitLab missing new stage'; exit 1; } +grep -q 'state_event=close' "$fixture/glab-args" || { echo 'FAIL GitLab not closed'; exit 1; } +if grep -q 'user-label' "$fixture/gh-args" || grep -q 'user-label' "$fixture/glab-args"; then echo 'FAIL unrelated label sent for removal'; exit 1; fi +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +case " $* " in + *' --method POST '*) exit 1 ;; + *' --method DELETE '*|*' --method PATCH '*) : ;; + *) printf '%s\n' '{"number":17,"state":"closed","labels":["taskflow:done","user-label"]}' ;; +esac +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" stage main 17 inbox --root "$fixture" > "$fixture/partial" 2>&1; then echo 'FAIL partial stage write reported success'; exit 1; fi +grep -qi 'unknown' "$fixture/partial" || { echo 'FAIL partial write not marked unknown'; exit 1; } +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" stage main 17 inbox --root "$fixture" > "$fixture/retry" 2>&1; then echo 'FAIL unresolved stage retry accepted'; exit 1; fi +grep -qi 'unresolved' "$fixture/retry" || { echo 'FAIL unresolved stage not reported'; exit 1; } +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') [ ! -f "$TEST_FIXTURE/repo-label" ] || printf 'taskflow:ready\n' ;; + repos/owner/repo/labels) printf yes > "$TEST_FIXTURE/repo-label" ;; + */issues/18/labels) [ -f "$TEST_FIXTURE/repo-label" ] || exit 1; printf yes > "$TEST_FIXTURE/issue-label" ;; + */issues/18) case " $* " in *' --method PATCH '*) : ;; *) if [ -f "$TEST_FIXTURE/issue-label" ]; then printf '%s\n' '{"number":18,"state":"open","labels":["taskflow:ready"]}'; else printf '%s\n' '{"number":18,"state":"open","labels":[]}'; fi ;; esac ;; + *) exit 1 ;; +esac +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" bash "$HERE/remote-todo" stage main 18 ready --root "$fixture" > "$fixture/create-label-out" +[ -f "$fixture/repo-label" ] && [ -f "$fixture/issue-label" ] || { echo 'FAIL missing repository label was not created and applied'; exit 1; } +echo 'PASS remote todo stage' \ No newline at end of file diff --git a/hooks/test-remote-todo-task-state b/hooks/test-remote-todo-task-state new file mode 100644 index 0000000..101ac17 --- /dev/null +++ b/hooks/test-remote-todo-task-state @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-task-state <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/2026-11-16-demo" "$fixture/bin" "$fixture/tmp" +fixture="$(cd "$fixture" && pwd -P)" +printf '.remote-todo-operations/\n.remote-todo-updates/\n' > "$fixture/TaskFlowDocs/.gitignore" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf 'github\tgithub.com\towner/repo\t17\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/remote-issue" +printf '# PRD\n> Task version: v1\n> Status: planning\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/prd.md" +printf '# Plan\n> Task version: v1\n> Status: planning\n' > "$fixture/TaskFlowDocs/2026-11-16-demo/plan.md" +printf '## Test\n\n- ID: TF-20261116-abcdef\n- Status: promoted\n- Updated: 2026-11-16\n- Goal: demo\n- Task: `TaskFlowDocs/2026-11-16-demo/`\n' > "$fixture/TaskFlowDocs/todo.md" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +printf '%s\n' "$@" >> "$TEST_FIXTURE/args" +case " $* " in + *' --method DELETE '*) : ;; + *' --method POST '*) : ;; + *' --method PATCH '*) printf yes > "$TEST_FIXTURE/done" ;; + *) if [ -f "$TEST_FIXTURE/done" ]; then printf '%s\n' '{"number":17,"state":"open","labels":["taskflow:planning","user-label"]}'; else printf '%s\n' '{"number":17,"state":"open","labels":["taskflow:inbox","user-label"]}'; fi ;; +esac +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TMPDIR="$fixture/tmp" bash "$HERE/task" state 2026-11-16-demo planning --root "$fixture" > "$fixture/out" +grep -q 'taskflow:planning' "$fixture/args" || { echo 'FAIL remote stage was not updated'; exit 1; } +grep -q '^> Status: planning' "$fixture/TaskFlowDocs/2026-11-16-demo/prd.md" || { echo 'FAIL local state'; exit 1; } +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case "${2:-}" in + *'/labels?per_page=100') printf '%s\n' "$*" | sed -n 's/.*\.name == "\([^"]*\)".*/\1/p'; exit 0 ;; +esac +exit 1 +EOF +chmod +x "$fixture/bin/gh" +if PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TMPDIR="$fixture/tmp" bash "$HERE/task" state 2026-11-16-demo ready --root "$fixture" > "$fixture/failure" 2>&1; then echo 'FAIL remote failure reported success'; exit 1; fi +grep -q '^> Status: planning' "$fixture/TaskFlowDocs/2026-11-16-demo/prd.md" || { echo 'FAIL local state changed after remote failure'; exit 1; } +mkdir -p "$fixture/TaskFlowDocs/2026-11-16-local" +printf '# PRD\n> Task version: v1\n> Status: planning\n' > "$fixture/TaskFlowDocs/2026-11-16-local/prd.md" +printf '# Plan\n> Task version: v1\n> Status: planning\n' > "$fixture/TaskFlowDocs/2026-11-16-local/plan.md" +printf '\n## Local\n\n- ID: TF-20261116-111111\n- Status: promoted\n- Updated: 2026-11-16\n- Goal: local\n- Task: `TaskFlowDocs/2026-11-16-local/`\n' >> "$fixture/TaskFlowDocs/todo.md" +PATH="$fixture/bin:$PATH" TEST_FIXTURE="$fixture" TMPDIR="$fixture/tmp" bash "$HERE/task" state 2026-11-16-local planning --root "$fixture" >/dev/null || { echo 'FAIL local task required remote access'; exit 1; } +echo 'PASS remote todo task state' \ No newline at end of file diff --git a/hooks/test-remote-todo-to-local b/hooks/test-remote-todo-to-local new file mode 100644 index 0000000..92a050f --- /dev/null +++ b/hooks/test-remote-todo-to-local @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +set -euo pipefail +[ "$#" -eq 1 ] || { echo 'usage: test-remote-todo-to-local <new-fixture-directory>' >&2; exit 1; } +fixture="$1" +[ ! -e "$fixture" ] || { echo 'fixture already exists' >&2; exit 1; } +mkdir -p "$fixture/TaskFlowDocs" "$fixture/bin" +fixture="$(cd "$fixture" && pwd -P)" +HERE="$(cd "$(dirname "$0")" && pwd)" +printf 'remote\nmain\tgithub\tgithub.com\towner/repo\nlab\tgitlab\tgit.example.org\tgroup/sub/repo\n' > "$fixture/TaskFlowDocs/remote-todos.conf" +printf '## Existing\n- ID: TF-20261116-abcdef\n- Status: inbox\n- Goal: Original item\n- Task: Not promoted.\n' > "$fixture/TaskFlowDocs/todo.md" +cat > "$fixture/bin/gh" <<'EOF' +#!/usr/bin/env bash +case " $* " in *' --method '*) echo 'unexpected remote write' >&2; exit 1 ;; esac +[ "${FAIL_READ:-0}" = 0 ] || exit 1 +printf '%s\n' '{"number":17,"title":"Remote task","body":"First line\n## fake heading\n- ID: TF-00000000-aaaaaa","state":"open","created_at":"2026-01-02T00:00:00Z","labels":[]}' +EOF +chmod +x "$fixture/bin/gh" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --root "$fixture" > "$fixture/out" +grep -q 'Original item' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL lost original Todo'; exit 1; } +grep -q 'Remote task' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL did not copy remote title'; exit 1; } +grep -q '^ ## fake heading' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL untrusted heading not contained'; exit 1; } +if grep -q '^- ID: TF-00000000-aaaaaa$' "$fixture/TaskFlowDocs/todo.md"; then echo 'FAIL untrusted ID escaped containment'; exit 1; fi +before="$(cksum < "$fixture/TaskFlowDocs/todo.md")" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --root "$fixture" > "$fixture/retry" +[ "$(cksum < "$fixture/TaskFlowDocs/todo.md")" = "$before" ] || { echo 'FAIL duplicate local copy'; exit 1; } +copy_id="$(sed -n 's/^Copied remote Issue to local Todo \([^;]*\);.*/\1/p' "$fixture/out")" +[ -n "$copy_id" ] || { echo 'FAIL missing copied Todo ID'; exit 1; } +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-remote "$copy_id" main --root "$fixture" > "$fixture/copy-migrate" 2>&1; then echo 'FAIL duplicated an existing Issue'; exit 1; fi +grep -qi 'remote copy' "$fixture/copy-migrate" || { echo 'FAIL missing duplicate prevention guidance'; exit 1; } +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-todo "$copy_id" main --root "$fixture" >/dev/null 2>&1; then echo 'FAIL accepted reattachment without explicit remote authority'; exit 1; fi +orphan="$fixture/orphan" +mkdir -p "$orphan/TaskFlowDocs/remote-todo-links" +cp "$fixture/TaskFlowDocs/todo.md" "$fixture/TaskFlowDocs/remote-todos.conf" "$orphan/TaskFlowDocs/" +printf 'detached\t%s\n' "$copy_id" > "$orphan/TaskFlowDocs/remote-todo-links/$copy_id.detached" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-todo "$copy_id" main --remote-authoritative --root "$orphan" > "$fixture/reattach-orphan" 2>&1; then echo 'FAIL accepted orphaned detach record'; exit 1; fi +[ ! -e "$orphan/TaskFlowDocs/remote-todo-links/$copy_id" ] || { echo 'FAIL orphaned record caused new mapping'; exit 1; } +if PATH="$fixture/bin:$PATH" FAIL_READ=1 bash "$HERE/remote-todo" reattach-todo "$copy_id" main --remote-authoritative --root "$fixture" > "$fixture/reattach-offline" 2>&1; then echo 'FAIL reattached while remote unavailable'; exit 1; fi +[ ! -e "$fixture/TaskFlowDocs/remote-todo-links/$copy_id" ] || { echo 'FAIL offline reattachment changed local association'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-todo "$copy_id" main --remote-authoritative --root "$fixture" > "$fixture/reattach" || { echo 'FAIL could not reattach local copy'; exit 1; } +[ -f "$fixture/TaskFlowDocs/remote-todo-links/$copy_id" ] || { echo 'FAIL missing Todo association'; exit 1; } +grep -Fqx "$(printf 'remote-authoritative\tconfirmed')" "$fixture/TaskFlowDocs/remote-todo-links/$copy_id" || { echo 'FAIL association misstates remote operation'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --detach --root "$fixture" > "$fixture/redetach" || { echo 'FAIL could not detach same local copy'; exit 1; } +[ -f "$fixture/TaskFlowDocs/remote-todo-links/$copy_id.detached" ] || { echo 'FAIL missing Todo detach'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-todo "$copy_id" main --remote-authoritative --root "$fixture" > "$fixture/reattach-again" || { echo 'FAIL repeated reattachment'; exit 1; } +partial="$fixture/partial" +mkdir -p "$partial/TaskFlowDocs" +cp "$fixture/TaskFlowDocs/remote-todos.conf" "$partial/TaskFlowDocs/remote-todos.conf" +awk '/^## Remote Issue copy/ { copy = 1 } copy && !/taskflow-copy-complete/ { print }' "$fixture/TaskFlowDocs/todo.md" > "$partial/TaskFlowDocs/todo.md" +printf '\n## Another\n- ID: TF-20261116-bbbbbb\n- Status: inbox\n- Goal: Later item\n' >> "$partial/TaskFlowDocs/todo.md" +if PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local main 17 --root "$partial" > "$partial/out" 2>&1; then echo 'FAIL incomplete copy reported complete'; exit 1; fi +grep -qi 'incomplete' "$partial/out" || { echo 'FAIL incomplete copy not reported'; exit 1; } +cat > "$fixture/bin/glab" <<'EOF' +#!/usr/bin/env bash +case " $* " in *' --method '*) echo 'unexpected remote write' >&2; exit 1 ;; esac +printf '%s\n' '{"iid":19,"title":"GitLab task","description":"Remote details","state":"closed","created_at":"2026-02-03T00:00:00Z","labels":["taskflow:cancelled"]}' +EOF +chmod +x "$fixture/bin/glab" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local lab 19 --root "$fixture" > "$fixture/gitlab-out" +grep -q 'GitLab task' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL missing GitLab task'; exit 1; } +grep -q 'https://git.example.org/group/sub/repo/-/issues/19' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL GitLab origin'; exit 1; } +grep -q '^- Status: cancelled$' "$fixture/TaskFlowDocs/todo.md" || { echo 'FAIL GitLab status'; exit 1; } +lab_id="$(sed -n 's/^Copied remote Issue to local Todo \([^;]*\);.*/\1/p' "$fixture/gitlab-out")" +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-todo "$lab_id" lab --remote-authoritative --root "$fixture" > "$fixture/lab-reattach" || { echo 'FAIL GitLab copy reattachment'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" migrate-to-local lab 19 --detach --root "$fixture" > "$fixture/lab-redetach" || { echo 'FAIL GitLab repeated detach'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" reattach-todo "$lab_id" lab --remote-authoritative --root "$fixture" > "$fixture/lab-reattach-again" || { echo 'FAIL GitLab repeated reattachment'; exit 1; } +PATH="$fixture/bin:$PATH" bash "$HERE/remote-todo" list --root "$fixture" > "$fixture/reattach-list" +if grep -Fq "LOCAL $copy_id " "$fixture/reattach-list" || grep -Fq "LOCAL $lab_id " "$fixture/reattach-list"; then echo 'FAIL reattached copies duplicated in list'; exit 1; fi +echo 'PASS remote todo to local' \ No newline at end of file diff --git a/skills/taskflow/SKILL.md b/skills/taskflow/SKILL.md index b8b8a81..b75a449 100644 --- a/skills/taskflow/SKILL.md +++ b/skills/taskflow/SKILL.md @@ -31,7 +31,7 @@ TaskFlowDocs/YYYY-MM-DD-short-slug/ `TaskFlowDocs/repository-docs/index.md` is the authoritative routing and check record for repository documents, repository rules, and personal rules. Maintain or read it first, route by phase, then load the listed source documents; those source documents remain authoritative for policy content. Record selected paths and incorporated conclusions in the active task Plan. If the task needs a rule that the repository does not provide, use the repository-document rules below before implementation. -`TaskFlowDocs/todo.md` is the mandatory intake record for every request that passes the applicability gate, including qualifying direct development requests, GitHub Issues, other development imports, and explicit `$taskflow` requests. Create or update its Todo item before task matching, clarification, promotion, planning, implementation, or import-specific processing. It is not a second source of task facts: after promotion, the linked `TaskFlowDocs/<task-id>/` directory is authoritative for requirements, design, plan, and verification. +The project Todo backend is selected in `TaskFlowDocs/remote-todos.conf`: absent configuration defaults to local `TaskFlowDocs/todo.md`; explicit remote mode uses configured GitHub or GitLab Issues as the Todo records. Record each qualifying request in the selected backend before task selection. A promoted remote Issue gains a minimal local task reference; the Issue remains the Todo record, while `TaskFlowDocs/<task-id>/` remains authoritative for task requirements, design, plan and verification. A user may explicitly choose local intake for one item with `hooks/task intake ... --local`. Required artifacts: @@ -45,7 +45,7 @@ Conditional artifacts: - `sessions.md` — session/resume index. Optional as a file; whether an entry must be written is a condition, stated under **Sessions and collaboration**. - `old/vN/` — only for a superseded logical version. -Never create a second task fact source such as a root `SPEC.md`, `TaskFlowDocs/plan.md`, or an automatic review file for the same task. `TaskFlowDocs/todo.md` is the sole repository Todo inbox; it contains triage metadata only and never duplicates promoted task facts. +Never create a second task fact source such as a root `SPEC.md`, `TaskFlowDocs/plan.md`, or an automatic review file for the same task. In local mode `TaskFlowDocs/todo.md` is the local Todo inbox; remote mode retains any existing local entries and uses Issues as the selected remote records. Local task references contain only the metadata needed for the task and do not replace the remote Issue. Task documents are read by the user and parsed by hooks, so they carry two languages at once. Write the prose, and any section heading the boundary does not name, in the user's working language; keep the lines hooks match — the section and entry headings they match by name, the `## Approval` fields, `> Task version:`, `> Status:`, `- Status:` lines, Todo field names, and `- [ ]` / `- [x]` markers — in English exactly as the hooks expect. The rule binds the document, not its drafter: a capability invoked for a phase supplies a shape to fill, and prose folded in from it is written in the user's working language like any other. `references/artifacts.md` states the boundary as an explicit name list, and says why a heading outside it is free to be translated, why a scaffold arrives in English whether `hooks/task promote` or a capability's template wrote it, and why that exemption stops at the headings. Existing documents, including everything under `TaskFlowDocs/achieved/`, are not rewritten for language. @@ -53,9 +53,22 @@ Task documents are read by the user and parsed by hooks, so they carry two langu After the request passes the applicability gate, first record or update its Todo item. Then inspect the repository, `TaskFlowDocs/repository-docs/index.md`, applicable project rules, active and achieved TaskFlowDocs tasks, tests, configuration, Git state, and uncommitted changes. Separate findings into confirmed facts, user decisions, technical unknowns, and explicit exclusions. +## Remote Todo operations + +Paths beginning `hooks/` in this section are relative to the installed TaskFlow plugin root, derived from this `SKILL.md` location, not from the user repository. Run them with the user repository as the working directory (or pass `--root`). + +Remote mode is opt-in per project. Set a default with `bash hooks/remote-todo mode-set remote`, add sources with `bash hooks/remote-todo connect <alias> github|gitlab <host> <owner/repo-or-group/project>`, and inspect the mode with `bash hooks/remote-todo mode`. Host is a hostname, not a credential-bearing URL. A project may connect multiple sources, including self-managed hosts. `gh` or `glab` must already be installed and authenticated for the selected host; never put credentials in the project config. Run `bash hooks/install-git-hygiene` explicitly if a remote write says its local recovery store is not Git-excluded. + +`bash hooks/remote-todo list` reads configured remote Issues explicitly and includes retained local entries; it never runs from an automatic hook. A source error makes the result incomplete, and a local entry is hidden only when its linked remote Issue was actually read. `snapshot <new-file>` is optional and stores only source order, validated Issue numbers, states and update times; it excludes titles, bodies and labels, but the metadata may still be sensitive or stale. Do not treat it as a current source for edits. + +For remote intake, choose the target with the user, then use `create <alias> <title> <body-file>`; `-` reads the body from stdin. Use `edit <alias> <number> title|body <value>` (body value is a readable file), `label <alias> <number> add|remove <label>`, and `stage <alias> <number> <stage>` for explicit operations. Stage labels are reserved for TaskFlow. Missing TaskFlow stage labels are created as needed; repository label creation requires permission. Concurrent remote changes can still race between a read and a write. Re-read and report discrepancies; do not promise atomic conflict prevention. + +`promote <alias> <number> <task-id> small|large --kind <kind>` creates a local task scaffold and minimal association, then synchronizes its stage. Linked task state, archive and reopen synchronize their stage; if the remote call fails, local transition is not started. Remote success followed by local failure is not an atomic transaction: inspect both sides. `migrate-to-remote <todo-id> <alias>` copies one local entry and retains its original; `migrate-to-local <alias> <number> [--detach]` copies remotely stored content locally without changing the remote Issue. Detaching an associated task keeps its approved task contract, saves a local reference copy and stops later remote updates. `reattach-todo <todo-id> <alias> --remote-authoritative` reconnects an unpromoted local copy to its original Issue after the user chooses the remote as authoritative; retain local content and do not write the Issue. `reattach-task <task-id>` reconnects only after checking the remote Issue is unclaimed and its stage matches the local task; reconcile differences explicitly first. + +A failed or timed-out write may already have succeeded remotely. Keep its operation ID; do not blindly retry. `resolve <operation-id> <number>` verifies a creation marker, `resolve-absent <operation-id> --checked` records the user's explicit verification that no Issue was created, and `ack-update <operation-id> --checked` records the user's review of an uncertain update. Recovery records are local-only and must not contain credentials. Stale lock directories after a crash require inspection; never remove user data to clear them. See `references/runtime.md` for commands, privacy limits and platform caveats. ### Todo-first intake -Every request within TaskFlow's applicability boundary enters `TaskFlowDocs/todo.md` before task selection. Record its ID, source, goal, status, next action, update date, and external identifier/link when one exists. Deduplicate imported items by source plus external identifier; preserve distinct requirements even when imported as a batch. A batch may carry common source metadata, but it is never itself a task, PRD, or requirements source. +Every request within TaskFlow's applicability boundary enters the selected Todo backend before task selection. For a local item record its ID, source, goal, status, next action, update date, and external identifier/link when one exists. For a remote item select the configured repository and create or use its Issue; preserve its platform, host, project and Issue number. Deduplicate imported items by source plus external identifier; preserve distinct requirements even when imported as a batch. A batch may carry common source metadata, but it is never itself a task, PRD, or requirements source. Prefer `hooks/task intake`, `promote`, `unpromote`, `state`, `progress`, `unaided`, and `complete` for their bounded mechanical edits. Use Agent edits for semantic content and cases those commands do not cover. These commands are explicit Agent actions, not automatically bound hooks. When a Todo ID is known, use `hooks/task get <todo-id>` to read that entry instead of loading the entire Todo file; retrieve every relevant entry when checking overlaps, and read the whole file when a repository-wide review or structural check needs it. @@ -153,7 +166,7 @@ Two things that version-control the honest version of this: a dirty base checkou A worktree isolates the work; it does not decide where the commits land. `CONTRIBUTING.md` states that separately, and it is not one question but two independent ones — where a task's own documents go, and where the bookkeeping that belongs to no task goes. Read it there, including the checkable facts that decide it, rather than from a summary here. -Use `TaskFlowDocs/todo.md` as the single intake for every idea, request, and imported requirement, whether or not it is ready for planning. Maintain each item with an ID, status, priority, owner, source, external identifier/link when available, one-sentence goal, task link, next action, and update date. Move it through `inbox → clarified → promoted → in_progress → done/cancelled`. +Use the selected Todo backend for every idea, request, and imported requirement, whether or not it is ready for planning. Local mode uses `TaskFlowDocs/todo.md`; remote mode uses the selected configured Issue repository. Maintain each item with an ID, status, priority, owner, source, external identifier/link when available, one-sentence goal, task link, next action, and update date. Move it through `inbox → clarified → promoted → in_progress → done/cancelled`. For batch imports, create or update one Todo item per source requirement before triage; deduplicate only source-identical requirements, retain source identity, and never promote a whole batch as one task. Keep an item in `inbox` while its intent is unknown. Move it to `clarified` only after goal, scope, acceptance, dependencies, size, and applicable repository documents are explicit. On promotion, create `prd.md`, decide whether `spec.md` is required, create `plan.md`, and link the task path back in the Todo item. Do not duplicate requirements or design in the inbox. Enter `in_progress` only after Plan approval; mark `done` only after task acceptance and verification, or `cancelled` with a reason. @@ -258,7 +271,7 @@ For a large task, `spec.md` records architecture boundaries, responsibilities, d ### 4. Plan — execution contract -Create or update `plan.md`. Route generic planning outputs such as `TaskFlowDocs/plan.md` into this task's `plan.md`; retain unpromoted Todo intake in `TaskFlowDocs/todo.md`. Each Step should include: +Create or update `plan.md`. Route generic planning outputs such as `TaskFlowDocs/plan.md` into this task's `plan.md`; retain unpromoted Todo intake in its selected local or remote backend. Each Step should include: - goal; - dependencies; @@ -314,7 +327,7 @@ After the user has approved, write those five fields with `task approve <task-id Before the first edit, confirm the task's working tree is still the one you are in — isolation was established in Phase 1, before the first document, and this phase only carries it forward. If it was skipped, do it now before editing, following the move procedure under **Isolate before the first document**: `git worktree add .worktrees/<slug> -b <type>/<slug> <base>`, then carry the uncommitted work across rather than re-doing it. Read the applicable repository guidance first — `CONTRIBUTING.md` names the branch prefixes and the base-branch rule; follow it when the repository states one, and otherwise branch as `<type>/<description>` off the intended base. Record the branch and worktree in `plan.md` when they are not obvious from the task directory. -Every task appends to the one `TaskFlowDocs/todo.md`, so merging parallel branches touches that file even when the branches share nothing else. TaskFlow installs a per-entry Git merge driver for it (see [runtime.md](references/runtime.md)); merge the branches locally so it runs, and expect an ordinary content conflict when a pull request is merged in a hosting web UI. +Local tasks and promoted remote tasks retain references in `TaskFlowDocs/todo.md`, so merging parallel branches may touch that file even when the branches share nothing else. TaskFlow installs a per-entry Git merge driver for it (see [runtime.md](references/runtime.md)); merge the branches locally so it runs, and expect an ordinary content conflict when a pull request is merged in a hosting web UI. When a merge or rebase you run hits a conflict, stop and put both sides in front of the user, who decides what to keep; do not silently choose one. Record that decision in the task `plan.md` before continuing. An automatic merge needs no action. A conflict on a pull request merged in a hosting web UI is outside this rule — that path is resolved on the platform, not by the Agent. diff --git a/skills/taskflow/references/artifacts.md b/skills/taskflow/references/artifacts.md index 3bb8fd3..6242b34 100644 --- a/skills/taskflow/references/artifacts.md +++ b/skills/taskflow/references/artifacts.md @@ -6,7 +6,7 @@ Use this reference when creating or reviewing task documents. The task directory If a task needs a rule the repository does not supply, first classify it as shared governance or a personal habit. **The plugin ships a default for the two shared documents a new repository never has**: `hooks/templates/CONTRIBUTING.md` and `hooks/templates/CODE_STYLE.md`, reachable through `hooks/repo-rules show <name>`, which prints one and writes nothing. `hooks/repo-rules create <name>` installs it and refuses when the file exists; `hooks/repo-rules check` names any line where an existing rule would disagree with this workflow, hedged as *possible* because the match is by keyword. Show, then ask, then write — in that order, never merged. For non-trivial development, derive missing `CONTRIBUTING.md` and `CODE_STYLE.md` from repository evidence, ask at most three dependency-ordered questions per turn for undecidable policy, and require explicit approval before either becomes binding. Draft `ROADMAP.md` only from confirmed product direction and other repository documents only when task-dependent. Put only stricter or orthogonal personal habits as sections of `repository-docs/personal.md`; personal rules never replace repository-owned policy and are never committed — the shared alternative is a repository-owned document. That exclusion is written per clone by `hooks/install-git-hygiene` into `.git/info/exclude`, not into a tracked `.gitignore`, and it hides only an untracked copy. Stop for any conflict. -`TaskFlowDocs/todo.md` is the mandatory first record only after a request passes the Skill's applicability gate: a repository development request or an explicit `$taskflow` invocation. Read-only explanation, translation, status, research, review, and diagnosis do not create Todo or task documents. Each qualifying batch import creates or updates one Todo item per source requirement, retaining source plus external identifier/link when available; the batch itself is metadata only. Todo retains triage metadata and a task link after promotion; PRD, Spec, Plan, and verification remain authoritative in that task directory. +The selected Todo backend is the mandatory first record only after a request passes the Skill's applicability gate: a repository development request or an explicit `$taskflow` invocation. Local mode uses `TaskFlowDocs/todo.md`; remote mode uses the selected configured GitHub or GitLab Issue repository and keeps a minimal local task reference only after promotion. Read-only explanation, translation, status, research, review, and diagnosis do not create Todo or task documents. Each qualifying batch import creates or updates one Todo item per source requirement, retaining source plus external identifier/link when available; the batch itself is metadata only. Todo retains triage metadata and a task link after promotion; PRD, Spec, Plan, and verification remain authoritative in that task directory. A **live** entry carries every field `hooks/task intake` writes. A **closed** entry — one whose status is `done`, rewritten by `hooks/archive` at the moment of archiving — keeps only `- ID:`, `- Status:`, `- Goal:`, `- Task:` and `- Next action:`. The rest are intake facts nothing reads once the work is finished, and the free-form prose under them is carried verbatim into the achieved `plan.md` under `## Todo Record` before it is removed, because that prose is the analysis behind the task and is not duplicated by the Plan. `Goal` stays for two reasons: the heading is a byte-truncated copy of it, and `hooks/task`'s duplicate-goal check reads the field on every entry regardless of status. `Updated` is the one field a later `hooks/reopen` has to insert, since a closed entry no longer carries it. @@ -204,7 +204,7 @@ The approval a Plan's `## Approval` block normally records comes with the proced | requirements / PRD | `prd.md` | | design / specification | `spec.md` when required | | plan / task list / checkpoint / review | `plan.md` | -| applicable development request / explicit `$taskflow` request | `TaskFlowDocs/todo.md` before task selection | +| applicable development request / explicit `$taskflow` request | selected local Todo or remote Issue before task selection | | research / evidence | `reference/` | | resume index | `sessions.md` | diff --git a/skills/taskflow/references/runtime.md b/skills/taskflow/references/runtime.md index 5bb5a91..50081f5 100644 --- a/skills/taskflow/references/runtime.md +++ b/skills/taskflow/references/runtime.md @@ -310,7 +310,7 @@ Anything else is refused with exit `2` — needs-user-input, per `CODE_STYLE.md` | Command | Action | Verify after | | --- | --- | --- | -| `hooks/task intake <goal> [source] [--kind <defect\|capability\|internal>] [--root <path>]` | Add one deduplicated Todo entry and allocate its ID. | Prints the Todo ID; duplicate goals fail before mutation. `--kind` defaults to `capability` here and is written to the entry as `- Kind:`; `promote` requires it explicitly, because that is where it starts deciding anything. In the base working tree it additionally notes that `todo.md` is now uncommitted there (exit code unchanged). | +| `hooks/task intake <goal> [source] [--local] [--kind <defect\|capability\|internal>] [--root <path>]` | Add one deduplicated Todo entry and allocate its ID. | Prints the Todo ID; duplicate goals fail before mutation. `--kind` defaults to `capability` here and is written to the entry as `- Kind:`; `promote` requires it explicitly, because that is where it starts deciding anything. In the base working tree it additionally notes that `todo.md` is now uncommitted there (exit code unchanged). | | `hooks/task promote <todo-id> <task-id> <small\|large> --kind <defect\|capability\|internal> [--root <path>]` | Create minimal PRD/Plan and optional Spec scaffolds, then link the Todo. | Existing destinations and already-promoted items fail before mutation. `--kind` is required here and optional at intake: it is written into the PRD's `## Task Kind`, and `approve` reads it to decide whether this task's `[PRD]` line must name something that investigates a cause. Refuses with `STATUS: blocked` (exit `3`) outside a task worktree — the first task documents are written in Phase 1, so they would otherwise land in the base tree on whatever branch is checked out. The message prints the `git worktree add` command with everything but `<type>` filled in. | | `hooks/task state <task-id> <state> [--root <path>]` | Update PRD/Plan state and Todo triage state. | `in_progress` requires approval for the current Task version. | | `hooks/task progress <task-id> <step> <status> [verification] [--root <path>]` | Update one Plan Step and optionally append a verification line. | `done` rejects unchecked checklist items. | @@ -325,14 +325,14 @@ Anything else is refused with exit `2` — needs-user-input, per `CODE_STYLE.md` ## Parallel branches and the Todo merge driver -`TaskFlowDocs/todo.md` is one file every task appends to, so two task branches merge-conflict there even when they touched nothing in common. TaskFlow ships a Git merge driver for it, so the two ordinary cases resolve without a human: +Local Todo entries and promoted remote task references share `TaskFlowDocs/todo.md`, so two task branches can merge-conflict there even when they touched nothing in common. TaskFlow ships a Git merge driver for it, so the two ordinary cases resolve without a human: - two branches each added an entry — both entries survive; - two branches changed different entries — both changes survive. Two branches changing *the same* entry differently is still a real conflict and is left for review with ordinary conflict markers. -The wiring is per-clone and untracked: `merge.taskflow-todo.driver` in the repository's own Git config, a `TaskFlowDocs/todo.md merge=taskflow-todo` line in `.git/info/attributes`, and two exclude rules in `.git/info/exclude` (`TaskFlowDocs/repository-docs/personal.md` and `TaskFlowDocs/**/sessions.md`). `hooks/install-git-hygiene` writes all three, `hooks/session-start` calls it on every session start, and it is idempotent, `--local`-only, silent on success, and best-effort. Nothing tracked is edited, so a repository that has not adopted TaskFlow is unaffected; `hooks/merge-todo` is the driver itself. +The wiring is per-clone and untracked: `merge.taskflow-todo.driver` in the repository's own Git config, a `TaskFlowDocs/todo.md merge=taskflow-todo` line in `.git/info/attributes`, and local-only exclude rules in `.git/info/exclude` (`TaskFlowDocs/repository-docs/personal.md`, `TaskFlowDocs/**/sessions.md`, and the remote Todo recovery directories). `hooks/install-git-hygiene` writes all three, `hooks/session-start` calls it on every session start, and it is idempotent, `--local`-only, silent on success, and best-effort. Nothing tracked is edited, so a repository that has not adopted TaskFlow is unaffected; `hooks/merge-todo` is the driver itself. The two exclude rules are there because those files belong to one clone rather than to the repository — a personal rule is not team policy, and `sessions.md` carries a session id, a working directory and `Session availability: local-only`. They are written per clone for exactly that reason: a `.gitignore` line would be a tracked file, which changes the view of every clone including the ones that want to commit the same paths. Two limits follow. Git applies no ignore rule to a **tracked** path, so a copy that was forced in once keeps appearing in `git status` — deliberately, because a rule that hid real changes to a tracked file would be worse than the stale file; clearing those copies is a decision about tracked history, not a configuration change. And the installation is done by SessionStart, so a host that does not run the hooks leaves the repository unconfigured until someone runs `bash hooks/install-git-hygiene` there. @@ -410,6 +410,40 @@ repo-root/ ├── test-host-parity # synthetic host event and declaration fixture ├── smoke-test-windows.ps1 # PowerShell/cmd full lifecycle regression ├── test-version-baseline # real Git lifecycle regression fixture + ├── remote-todo # explicit remote Issue Todo commands + ├── remote-todo-local-list # list and deduplicate retained local Todos + ├── remote-todo-migrate # migrate one local Todo to an Issue + ├── remote-todo-promote # promote an Issue into a TaskFlow task + ├── remote-todo-reattach # reconnect a detached task after checking phases + ├── remote-todo-reattach-todo # reconnect an unpromoted local Issue copy + ├── remote-todo-to-local # copy an Issue locally and optionally detach + ├── test-json-field-arrays # JSON string array membership regression + ├── test-json-field-numbers # JSON number parsing regression + ├── test-remote-todo # remote Todo regression + ├── test-remote-todo-archive # remote Todo archive regression + ├── test-remote-todo-concurrency # remote Todo concurrency regression + ├── test-remote-todo-configure # remote Todo configure regression + ├── test-remote-todo-create # remote Todo create regression + ├── test-remote-todo-detach # remote Todo detach regression + ├── test-remote-todo-detach-archived # remote Todo detach archived regression + ├── test-remote-todo-detach-task # remote Todo detach task regression + ├── test-remote-todo-edit # remote Todo edit regression + ├── test-remote-todo-intake # remote Todo intake regression + ├── test-remote-todo-label # remote Todo label regression + ├── test-remote-todo-link # remote Todo link regression + ├── test-remote-todo-list # remote Todo list regression + ├── test-remote-todo-migrate # remote Todo migrate regression + ├── test-remote-todo-migrate-dedup # remote Todo migrate dedup regression + ├── test-remote-todo-migrate-stage # remote Todo migrate stage regression + ├── test-remote-todo-mixed-list # remote Todo mixed list regression + ├── test-remote-todo-private # remote Todo private regression + ├── test-remote-todo-promote # remote Todo promote regression + ├── test-remote-todo-recover # remote Todo recover regression + ├── test-remote-todo-reminders # remote Todo reminders regression + ├── test-remote-todo-snapshot # remote Todo snapshot regression + ├── test-remote-todo-stage # remote Todo stage regression + ├── test-remote-todo-task-state # remote Todo task state regression + ├── test-remote-todo-to-local # remote Todo to local regression └── smoke-test # assert-style smoke tests ``` @@ -419,10 +453,20 @@ The tree is the inventory, and `hooks/smoke-test` asserts it against the directo Not every script in the tree is a lifecycle command, and the two are documented in different places. `hooks/task` and the four it drives (`task-id`, `completion-gate`, `archive`, `version`, `reopen`) are the lifecycle, and their rows are above. `hooks/release-version` and `hooks/repository-check` belong to the release procedure instead — `release-version` writes the mechanical version literals across the eight files that carry them, and `repository-check` performs a read-only governance and fork-readiness check that also names every worktree whose branch is already merged. Both are documented in `RELEASE.md`, which is where their ordering and preconditions live; they are listed here so that a reader routed to the hooks layout knows they exist, not so that a second copy of the procedure lives here. -**`hooks/remote-topology` is the one script here that uses the network, and the only one that is deliberately not wired to any event.** It answers what `git remote -v` cannot: which repository each remote names, whether this clone can push to it, and which base a pull request should target. `CONTRIBUTING.md` requires the receiving repository be established rather than inferred from a remote named `origin` or `upstream`, and remote names carry no role — a fork's `origin` may be the author's own fork or the upstream. Every fact it prints is read from `GET /repos/{owner}/{repo}`: `fork`, `parent.full_name`, `source.full_name`, `default_branch`, and `permissions.push`. The base is not inferred — a fork targets the repository it was forked from and a non-fork targets itself, both read from `parent`. +**`hooks/remote-topology` is a read-only network command and is not wired to an event.** The explicit `remote-todo` commands also use the network when invoked. It answers what `git remote -v` cannot: which repository each remote names, whether this clone can push to it, and which base a pull request should target. `CONTRIBUTING.md` requires the receiving repository be established rather than inferred from a remote named `origin` or `upstream`, and remote names carry no role — a fork's `origin` may be the author's own fork or the upstream. Every fact it prints is read from `GET /repos/{owner}/{repo}`: `fork`, `parent.full_name`, `source.full_name`, `default_branch`, and `permissions.push`. The base is not inferred — a fork targets the repository it was forked from and a non-fork targets itself, both read from `parent`. Three boundaries keep it from changing what the rest of the runtime is. It is **never** added to any `hooks-*.json`: a network round trip in `SessionStart` would make every session wait on GitHub, and the smoke suite asserts the absence rather than trusting it. It writes no file and issues no request other than `GET /repos/*`. And it **always exits 0** — `repository-check`'s `2` and `3` are verdicts about the repository, and "`gh` is not installed" is not one. The four ways the lookup can fail are reported apart because each calls for a different action: `gh not found` (install it), `gh not logged in` (authenticate it), `cannot reach the GitHub API` (wait for the network), `API rate limit` (wait for the window to reset). They cannot be told apart by exit code — an unauthenticated `gh` and an unreachable network both exit `1`, measured 2026-11-02 — so the authentication question is asked first and the response body distinguishes the rest. The timeout budget is for the whole run rather than each request, so the command is bounded at ten seconds however many remotes a clone has; without `timeout` or `gtimeout` on `PATH` the script says so instead of leaving the absence silent. `hooks.json` is named without a host suffix because Claude Code auto-loads that exact filename at the plugin root. Every other host points at its file some other way — a manifest `hooks` entry for Codex and CodeBuddy, `dsh/index.js` for dsh, a copy to `.trae/hooks.json` for Trae — so only Claude Code depends on the name. + +## Remote Todo backend + +`hooks/remote-todo mode-set local|remote` selects the project default; `connect <alias> github|gitlab <host> <project>` adds a source without changing the mode. `mode` and `validate` are local, and `list` explicitly reads remote sources alongside retained local entries. GitHub's issue endpoint can include pull requests; those are filtered. Source failures make the result incomplete. A user's explicit `hooks/task intake ... --local` can still make one local entry in remote mode. + +`promote`, `stage`, `edit`, `label`, `create`, `migrate-to-remote`, and `migrate-to-local` require explicit invocation. A remote task keeps `remote-issue` as an execution association; `task state`, `archive`, and `reopen` synchronize its phase before the local transition. The two sides cannot be committed atomically. Existing remote labels outside `taskflow:` are preserved by targeted additions/removals. Missing TaskFlow stage labels are created as needed; repository label creation requires permission. `migrate-to-local ... --detach` keeps the original local record and remote Issue; for a task it retains the approved task documents and saves an untrusted local reference copy. Local detached tasks no longer contact the remote source. `reattach-todo <todo-id> <alias> --remote-authoritative` reconnects an unpromoted remote copy without writing the Issue; retain the local content. `reattach-task <task-id>` reads the original Issue, refuses if the phase differs or another task is linked, and preserves detach history without changing remote content. A later detachment retains the earlier local copy and fetches a fresh one. Migration checks all Issues for a stable Todo marker before creation; never migrate the same Todo concurrently from multiple worktrees or machines. Editing or removing the marker outside TaskFlow can defeat later checks. + +`gh` and `glab` use the host's existing authentication for the selected hostname; missing tools or permissions fail visibly. No credential is saved in project config or recovery records. Before a remote write, the local recovery directories must be excluded from Git and must contain no tracked files; `hooks/install-git-hygiene` installs the per-clone exclusions. Explicit snapshots contain only source order and validated Issue numbers, states and update times; titles, bodies and labels are excluded. Metadata can still reveal activity; source numbers refer to configuration order at export time. The user selects a new path, and the snapshot is historical, not an editing source. No automatic network polling occurs. + +A write timeout can mean success or failure. Inspect the recorded operation before retrying: use `resolve <id> <number>` to verify a creation marker, `resolve-absent <id> --checked` only after confirming no Issue was created, and `ack-update <id> --checked` only after reviewing an uncertain update. A stale lock may require user inspection. Local locks do not coordinate independent worktrees or machines. Platforms without conditional writes leave an accepted concurrent-update window; a read-before/write/read-after sequence cannot prove no overwrite occurred.