Skill Tracer
Skills › gstack › gstack › tasks-section.ts

tasks-section.ts

173 lines · scripts/resolvers/tasks-section.ts at 3f79d81

1/**
2 * Resolvers for the Implementation Tasks emission (#1454).
3 *
4 * {{TASKS_SECTION_EMIT:<phase>}} — per-skill task emission + JSONL write
5 * {{TASKS_SECTION_AGGREGATE}} — autoplan aggregation across all phases
6 *
7 * JSONL artifact fields: phase, run_id, branch, commit, id, priority,
8 * component, files, effort_human, effort_cc, title, source_finding
9 * (consumed by /autoplan's aggregator).
10 */
12import type { TemplateContext, ResolverFn } from './types';
14const VALID_PHASES = new Set(['ceo-review', 'design-review', 'eng-review', 'devex-review']);
16export const generateTasksSectionEmit: ResolverFn = (_ctx: TemplateContext, args?: string[]) => {
17 const phase = args?.[0];
18 if (!phase || !VALID_PHASES.has(phase)) {
19 throw new Error(`TASKS_SECTION_EMIT requires one of ${[...VALID_PHASES].join(', ')} — got ${phase}`);
20 }
21 const ceo = _ctx.skillName === 'plan-ceo-review';
22 const conditionalWrites = ceo || _ctx.skillName === 'plan-eng-review';
23 const storagePolicy = ceo ? 'Step 0 storage policy' : 'Review record and write policy';
25 return `## Implementation Tasks
27${ceo ? `Turn findings into tasks within the approved review depth. Implementation-ready
28tasks describe the build. Strategy-only tasks name the next research, design or
29verification action and its owner; they do not choose implementation contracts.
30List known files only. For unknown files, write "to be determined" and use an
31empty JSONL files array. Each task needs a concrete verification step.` : `Before closing this review, synthesize the findings above into a flat list of
32build-actionable tasks. Each task derives from a specific finding — no padding.`}
33${conditionalWrites ? `Always emit the markdown section. Write its JSONL artifact for \`/autoplan\` only when the ${storagePolicy} permits it; otherwise label the complete task output not persisted and do not claim an aggregation artifact exists.` : 'Emit the markdown section AND write a JSONL artifact that `/autoplan` can\naggregate across phases.'}
35### Markdown section (always emit)
37\`\`\`markdown
38## Implementation Tasks
39Synthesized from this review's findings. Each task derives from a specific
40finding above. Run with Claude Code or Codex; checkbox as you ship.
42- [ ] **T1 (P1, human: ~2h / CC: ~15min)** — <component> — <imperative title>
43 - Surfaced by: <section name> — <specific finding text or line reference>
44 - Files: <paths to touch>
45 - Verify: <test command or manual check>
46- [ ] **T2 (P2, human: ~30min / CC: ~5min)** — ...
47\`\`\`
49Rules:
50- P1 blocks ship; P2 should land same branch; P3 is a follow-up TODO.
51- If a finding produced no actionable task, do not invent one.
52- If a section had zero findings, emit \`_No new tasks from <section>._\`
53- Show human-team and CC+gstack effort estimates. Default task-type ratios (human ÷ CC time): scaffolding ~100x, tests ~50x, features ~30x, bug fix with regression ~20x, architecture ~5x, research ~3x. Adjust to the actual work and state the assumption.
55### JSONL artifact (${conditionalWrites ? 'write when permitted, including zero tasks' : 'always write, even if zero tasks'})
57\`/autoplan\` reads this file to aggregate across phases. Build each line with
58\`jq -nc\` so titles and source findings containing quotes, newlines, or
59backslashes serialize cleanly — never use hand-rolled \`echo\` / \`printf\`.
61\`\`\`bash
62GSTACK_STATE_ROOT=$(~/.claude/skills/gstack/bin/gstack-paths --get GSTACK_STATE_ROOT); : "\${GSTACK_STATE_ROOT:?gstack-paths failed; reinstall with ./setup or /gstack-upgrade}"
63SLUG=$(~/.claude/skills/gstack/bin/gstack-slug --get SLUG 2>/dev/null)
64TASKS_DIR="$GSTACK_STATE_ROOT/projects/\${SLUG:-unknown}"
65mkdir -p "$TASKS_DIR"
66TASKS_FILE="$TASKS_DIR/tasks-${phase}-$(date +%Y%m%d-%H%M%S).jsonl"
67COMMIT=$(git rev-parse HEAD 2>/dev/null || echo unknown)
68# The task record keeps the raw git branch; artifact filenames keep gstack-slug's sanitized BRANCH.
69GIT_BRANCH=$(git branch --show-current 2>/dev/null || echo unknown)
70RUN_ID="$(date -u +%Y%m%dT%H%M%SZ)-$$"
72# Repeat ONE jq invocation per task identified during this review.
73# Substitute the placeholders inline with shell variables you set per task:
74# TASK_ID (T1, T2, ...), PRIORITY (P1/P2/P3), COMPONENT, TITLE,
75# SOURCE_FINDING, EFFORT_HUMAN, EFFORT_CC, FILES_JSON (a JSON array literal
76# like '["browse/src/sanitize.ts","browse/src/server.ts"]').
77jq -nc \\
78 --arg phase '${phase}' \\
79 --arg run_id "$RUN_ID" \\
80 --arg branch "$GIT_BRANCH" \\
81 --arg commit "$COMMIT" \\
82 --arg id "$TASK_ID" \\
83 --arg priority "$PRIORITY" \\
84 --arg component "$COMPONENT" \\
85 --arg effort_human "$EFFORT_HUMAN" \\
86 --arg effort_cc "$EFFORT_CC" \\
87 --arg title "$TITLE" \\
88 --arg source_finding "$SOURCE_FINDING" \\
89 --argjson files "$FILES_JSON" \\
90 '{phase:$phase, run_id:$run_id, branch:$branch, commit:$commit, id:$id, priority:$priority, component:$component, files:$files, effort_human:$effort_human, effort_cc:$effort_cc, title:$title, source_finding:$source_finding}' \\
91 >> "$TASKS_FILE"
92\`\`\`
94If \`jq\` is not installed, fall back to skipping the JSONL write and warn
95the user to install jq for autoplan aggregation. Never hand-roll JSONL.
97${conditionalWrites ? 'When writes are permitted and zero tasks were identified, touch the JSONL file' : 'If zero tasks were identified in this review, still touch the JSONL file'}
98(\`: > "$TASKS_FILE"\`) so the aggregator sees that the phase produced output
99this run (an empty file means "ran, no findings" — distinct from "didn't run").
100`;
101};
103export const generateTasksSectionAggregate: ResolverFn = (_ctx: TemplateContext) => {
104 return `## Implementation Tasks aggregator
106Before rendering the Final Approval Gate output block below, aggregate the
107per-phase task lists each review skill wrote.
109\`\`\`bash
110GSTACK_STATE_ROOT=$(~/.claude/skills/gstack/bin/gstack-paths --get GSTACK_STATE_ROOT); : "\${GSTACK_STATE_ROOT:?gstack-paths failed; reinstall with ./setup or /gstack-upgrade}"
111BRANCH=$(~/.claude/skills/gstack/bin/gstack-slug --get BRANCH 2>/dev/null)
112SLUG=$(~/.claude/skills/gstack/bin/gstack-slug --get SLUG 2>/dev/null)
113TASKS_DIR="$GSTACK_STATE_ROOT/projects/\${SLUG:-unknown}"
114BRANCH=$(git branch --show-current 2>/dev/null || echo unknown)
115# Commit window: last 5 commits on this branch. Drops stale standalone reviews.
116COMMITS_RECENT=$(git log --format=%H -n 5 2>/dev/null | tr '\\n' '|' | sed 's/|$//')
118AGGREGATED_TASKS=""
119if command -v jq >/dev/null 2>&1; then
120 # All 4 phases, scoped to this branch + commit window; latest run_id per
121 # phase; exact dedupe by (component, sorted(files), title); P1 > P2 > P3, then phase order.
122 ALL_JSONL=$(mktemp -t autoplan-tasks.XXXXXXXX)
123 for phase in ceo-review design-review eng-review devex-review; do
124 # find, not a glob: zsh nomatch errors when a phase wrote no JSONL. Sorted for stable order.
125 while IFS= read -r f; do
126 [ -f "$f" ] || continue
127 # .commit is bound BEFORE the pipe: a pipe rebinds jq's context, so a bare
128 # .commit after it indexes the array, every line errors, and the aggregate is empty.
129 jq -c --arg branch "$BRANCH" --arg commits "$COMMITS_RECENT" \\
130 '.commit as $c | select(.branch == $branch and ($commits | split("|") | index($c) != null))' \\
131 "$f" 2>/dev/null >> "$ALL_JSONL" || true
132 done < <(find "$TASKS_DIR" -maxdepth 1 -name "tasks-$phase-*.jsonl" 2>/dev/null | sort)
133 # Latest run_id per phase
134 if [ -s "$ALL_JSONL" ]; then
135 jq -sc --arg phase "$phase" \\
136 '[.[] | select(.phase == $phase)] | (max_by(.run_id) // null) as $latest_run | if $latest_run then map(select(.run_id == $latest_run.run_id)) else [] end | .[]' \\
137 "$ALL_JSONL" > "$ALL_JSONL.phase" 2>/dev/null || true
138 jq -c --arg phase "$phase" 'select(.phase != $phase)' "$ALL_JSONL" > "$ALL_JSONL.other" 2>/dev/null || true
139 cat "$ALL_JSONL.other" "$ALL_JSONL.phase" > "$ALL_JSONL"
140 rm -f "$ALL_JSONL.phase" "$ALL_JSONL.other"
141 fi
142 done
144 # Exact-match dedupe; non-matches stay separate (the renderer marks possible duplicates).
145 AGGREGATED_TASKS=$(jq -s \\
146 'group_by([.component, (.files | sort), .title])
147 | map(
148 # Highest priority per group; tie-break by phase order
149 sort_by({P1:0,P2:1,P3:2}[.priority] // 99, {"ceo-review":0,"design-review":1,"eng-review":2,"devex-review":3}[.phase] // 99) | .[0]
150 )
151 | sort_by({P1:0,P2:1,P3:2}[.priority] // 99, {"ceo-review":0,"design-review":1,"eng-review":2,"devex-review":3}[.phase] // 99)
152 | if length == 0 then "_No actionable tasks emitted from any phase._" else
153 map("- [ ] **\\(.id) (\\(.priority), human: \\(.effort_human) / CC: \\(.effort_cc)) — \\(.component)** — \\(.title)\\n - Surfaced by: \\(.phase) — \\(.source_finding)\\n - Files: \\(.files | join(", "))") | join("\\n")
154 end' "$ALL_JSONL" 2>/dev/null | sed 's/^"//;s/"$//;s/\\\\n/\\n/g')
155 rm -f "$ALL_JSONL"
156else
157 AGGREGATED_TASKS="_jq not installed — install jq to aggregate per-phase task lists. Skipping._"
158fi
159\`\`\`
161In the Final Approval Gate output template below, render the aggregated markdown
162in the \`### Implementation Tasks (aggregated across phases)\` section: substitute
163\`$AGGREGATED_TASKS\` (the bash variable above) at runtime before printing the
164message; it is not a gen-skill-docs placeholder.
166If \`$AGGREGATED_TASKS\` is empty (no JSONL files found — none of the review
167skills ran in this session), render:
169\`_No per-phase task lists found in $TASKS_DIR for branch $BRANCH. Each review
170skill writes its own; if you ran one of them but no list appears here, check
171that jq is installed and the tasks-<phase>-*.jsonl files exist._\`
172`;
173};