feat(apply): explore deterministic task completion sync from agent prompt files to tasks.md
Problem
When agents complete tasks during `/opsx:apply`, task completions are not synced back to `tasks.md`. The coder prompt says "mark task complete" but provides no mechanism — agents either forget or don't know to edit `tasks.md` and change `- [ ]` to `- [x]`.
Result: `openspec list --json` always shows `completedTasks: 0` even after a full apply run.
Reproduction Steps
- Run `/opsx:apply` on a change with tasks (e.g. `202603040827-markdown-prompt-sources`)
- Agents implement all tasks and report completion via SendMessage
- Run `openspec list --json`
- Observe: `"completedTasks": 0, "totalTasks": 35` — no tasks marked done
Root Cause
The `generate-agent-prompts.sh` script generates coder prompts with this rule:
- Do NOT commit — mark task complete + SendMessage to team-lead
"Mark task complete" is undefined — no instruction to edit `tasks.md`. Agents complete work but never update the checkbox state.
Additionally, even if agents do edit `tasks.md`, the agent-prompt files (`coder-group-N.md`) are isolated — there's no mechanism to reconcile multiple agents' completions back to a single source of truth.
Expected Behavior
After a full `/opsx:apply` run, `tasks.md` checkboxes reflect actual completion state. `openspec list --json` shows accurate `completedTasks` count.
Related
- `/WesleyMFrederick/cc-workflows/blob/main/openspec/schemas/tdd-teams/scripts/generate-agent-prompts.sh` — where coder prompts are generated (line ~166)
- `/WesleyMFrederick/cc-workflows/blob/main/openspec/schemas/tdd-teams/templates/apply-instruction.md` — apply orchestration instruction
Note
The core challenge is deterministic sync: multiple agents work in parallel on different task groups. A viable approach may be to have each coder agent edit `tasks.md` directly after each task (sequential within a group), with the team-lead receiving a completion signal to verify the edit landed. Alternatively, the team-lead could be responsible for marking tasks done after receiving each agent's completion message.
Explore options:
- Agent edits tasks.md directly — simple, but requires agents to locate the exact line
- Team-lead marks done on completion signal — centralized, deterministic, team-lead controls state
- Post-run reconciliation script — team-lead collects all completed task IDs and batch-updates tasks.md at the end
Acceptance Criteria
- [ ] Approach selected and documented (one of the three above, or a new option)
- [ ] `tasks.md` checkboxes accurately reflect completion after an apply run
- [ ] `openspec list --json` shows correct `completedTasks` count post-apply
- [ ] Works correctly when agents run in parallel (no race conditions / overwrites)
- [ ] `generate-agent-prompts.sh` or `apply-instruction.md` updated with explicit task-marking instructions
Definition of Done
- [ ] Approach documented or spike completed
- [ ] Implementation in place (script, prompt, or orchestration change)
- [ ] Verified with a real apply run showing accurate task counts
- [ ] Committed with conventional commit
A fourth option worth considering: a first-class CLI writer (openspec task complete)
The three options here are all at the prompt/orchestration layer. I'd like to add a fourth that complements them: give the CLI a deterministic write path for task state, rather than relying on agents to hand-edit tasks.md.
Motivation. Today the CLI only reads task state — countTasksFromContent in src/utils/task-progress.ts tallies - [ ]/- [x], and status/list/instructions consume the counts. There's no writer, so every agent (and the tdd-teams prompts referenced here) re-implements checkbox flipping, almost always via perl -i/sed -i. That has three recurring problems:
- Correctness — a loose regex flips the wrong checkbox or several at once, with no verification that exactly one task matched.
- Token cost — the safe alternative (an editor's exact-match edit) requires loading the whole
tasks.mdinto the model's context; expensive for long task lists. This is also the thrust of #572. - Duplication — the checkbox format lives in N prompts instead of one parser.
Proposed shape (mirrors the existing "explicit --change required" convention from #415):
openspec task complete --change <id> --task <addr>
openspec task reopen --change <id> --task <addr>
# --json for structured { task, completed, total } output
Strictly byte-preserving: only the [ ]↔[x] character changes, no reformatting/reordering — so it stays faithful to the markdown-first model (the .md remains source of truth; this is just git add for the checkbox).
The one real design question — addressing. The current parser has no task identity; tasks are counted, never addressed. So a writer needs an addressing scheme, and that's the decision I'd want maintainer input on before building:
- Positional index (Nth task line) — simple, matches authoring order, but fragile if tasks are reordered between read and write.
- Hierarchical number (
1.2) — stable and human-meaningful, but requires a numbering convention that isn't currently enforced intasks.md. - Text match — what
perldoes today; ambiguous, not recommended as the primary handle.
This keeps the parallel-sync concerns in this issue intact (each agent still serializes its own toggles; the team-lead can reconcile via --json), while removing the fragile hand-editing.
Two questions before I'd open a PR:
- Is a CLI writer for task state something you'd want upstream, or do you prefer to keep task mutation purely at the prompt/orchestration layer?
- If yes — which addressing scheme do you favor (positional index vs. hierarchical ID)?
Happy to implement it (parser extension + command + tests is fairly contained) once there's a direction.