@rpamis/comet 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,496 +1,497 @@
1
- #!/bin/bash
2
- # Comet State — unified interface for .comet.yaml state management
3
- # Usage: comet-state.sh <subcommand> <change-name> [args...]
4
- #
5
- # Subcommands:
6
- # init <change-name> <workflow> — Initialize .comet.yaml with workflow defaults
7
- # get <change-name> <field> — Read a field value from .comet.yaml
8
- # set <change-name> <field> <val> — Update a field value
9
- # check <phase> <change-name> — Verify entry requirements for a phase
10
- # scale <change-name> — Assess and set verification mode based on metrics
11
- #
12
- # Workflows: full, hotfix, tweak
13
- # Phases for check: open, design, build, verify, archive
14
-
15
- set -euo pipefail
16
-
17
- # --- Color output helpers ---
18
-
19
- red() { echo -e "\033[31m$1\033[0m" >&2; }
20
- green() { echo -e "\033[32m$1\033[0m" >&2; }
21
- yellow() { echo -e "\033[33m$1\033[0m" >&2; }
22
-
23
- # --- Script location ---
24
-
25
- SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
26
-
27
- # --- Input validation ---
28
-
29
- validate_change_name() {
30
- local name="$1"
31
- # Reject empty names
32
- if [ -z "$name" ]; then
33
- red "ERROR: Change name cannot be empty" >&2
34
- exit 1
35
- fi
36
- # Only allow alphanumeric, hyphens, and underscores
37
- if [[ ! "$name" =~ ^[a-zA-Z0-9_-]+$ ]]; then
38
- red "ERROR: Invalid change name: '$name'" >&2
39
- red "Valid characters: a-z, A-Z, 0-9, -, _" >&2
40
- exit 1
41
- fi
42
- # Reject path traversal attempts
43
- if [[ "$name" =~ \.\. ]]; then
44
- red "ERROR: Change name cannot contain '..' (path traversal not allowed)" >&2
45
- exit 1
46
- fi
47
- }
48
-
49
- validate_enum() {
50
- local value="$1"
51
- shift
52
- local valid_values=("$@")
53
-
54
- for valid in "${valid_values[@]}"; do
55
- if [ "$value" = "$valid" ]; then
56
- return 0
57
- fi
58
- done
59
-
60
- red "ERROR: Invalid value: '$value'" >&2
61
- red "Valid values: ${valid_values[*]}" >&2
62
- exit 1
63
- }
64
-
65
- # --- Helper functions ---
66
-
67
- yaml_field() {
68
- local field="$1"
69
- local yaml_file="$2"
70
- if [ -f "$yaml_file" ]; then
71
- grep "^${field}:" "$yaml_file" | sed "s/^${field}: *//" | tr -d '"' | tr -d "'"
72
- fi
73
- }
74
-
75
- file_nonempty() {
76
- [ -f "$1" ] && [ -s "$1" ]
77
- }
78
-
79
- # --- Subcommands ---
80
-
81
- cmd_init() {
82
- local change_name="$1"
83
- local workflow="$2"
84
-
85
- validate_change_name "$change_name"
86
- validate_enum "$workflow" "full" "hotfix" "tweak"
87
-
88
- local change_dir="openspec/changes/$change_name"
89
- local yaml_file="$change_dir/.comet.yaml"
90
-
91
- # Check if .comet.yaml already exists
92
- if [ -f "$yaml_file" ]; then
93
- red "ERROR: .comet.yaml already exists at $yaml_file"
94
- exit 1
95
- fi
96
-
97
- # Create change directory if it doesn't exist
98
- mkdir -p "$change_dir"
99
-
100
- # Set workflow-appropriate defaults
101
- local phase build_mode isolation verify_mode
102
-
103
- case "$workflow" in
104
- full)
105
- phase="design"
106
- build_mode="null"
107
- isolation="null"
108
- verify_mode="null"
109
- ;;
110
- hotfix|tweak)
111
- phase="build"
112
- build_mode="direct"
113
- isolation="branch"
114
- verify_mode="light"
115
- ;;
116
- esac
117
-
118
- # Write .comet.yaml
119
- cat > "$yaml_file" <<EOF
120
- workflow: $workflow
121
- phase: $phase
122
- build_mode: $build_mode
123
- isolation: $isolation
124
- verify_mode: $verify_mode
125
- design_doc: null
126
- plan: null
127
- verify_result: pending
128
- verified_at: null
129
- archived: false
130
- EOF
131
-
132
- green "Initialized: $yaml_file (workflow=$workflow)"
133
- }
134
-
135
- cmd_get() {
136
- local change_name="$1"
137
- local field="$2"
138
-
139
- validate_change_name "$change_name"
140
-
141
- local change_dir="openspec/changes/$change_name"
142
- local yaml_file="$change_dir/.comet.yaml"
143
-
144
- # Check if .comet.yaml exists
145
- if [ ! -f "$yaml_file" ]; then
146
- red "ERROR: .comet.yaml not found at $yaml_file"
147
- exit 1
148
- fi
149
-
150
- # Read and output the field value
151
- local value
152
- value=$(yaml_field "$field" "$yaml_file")
153
- echo "${value:-}"
154
- }
155
-
156
- cmd_set() {
157
- local change_name="$1"
158
- local field="$2"
159
- local value="$3"
160
-
161
- validate_change_name "$change_name"
162
-
163
- local change_dir="openspec/changes/$change_name"
164
- local yaml_file="$change_dir/.comet.yaml"
165
-
166
- # Check if .comet.yaml exists
167
- if [ ! -f "$yaml_file" ]; then
168
- red "ERROR: .comet.yaml not found at $yaml_file"
169
- exit 1
170
- fi
171
-
172
- # Validate field name
173
- case "$field" in
174
- workflow|phase|build_mode|isolation|verify_mode|verify_result|archived|design_doc|plan|verified_at)
175
- # Valid field
176
- ;;
177
- *)
178
- red "ERROR: Unknown field: '$field'" >&2
179
- red "Valid fields: workflow, phase, design_doc, plan, build_mode, isolation, verify_mode, verify_result, verified_at, archived" >&2
180
- exit 1
181
- ;;
182
- esac
183
-
184
- # Validate enum values
185
- case "$field" in
186
- workflow)
187
- validate_enum "$value" "full" "hotfix" "tweak"
188
- ;;
189
- phase)
190
- validate_enum "$value" "design" "build" "verify" "archive"
191
- ;;
192
- build_mode)
193
- validate_enum "$value" "subagent-driven-development" "executing-plans" "direct"
194
- ;;
195
- isolation)
196
- validate_enum "$value" "branch" "worktree"
197
- ;;
198
- verify_mode)
199
- validate_enum "$value" "light" "full"
200
- ;;
201
- verify_result)
202
- validate_enum "$value" "pending" "pass" "fail"
203
- ;;
204
- archived)
205
- validate_enum "$value" "true" "false"
206
- ;;
207
- design_doc|plan|verified_at)
208
- # No validation for path fields and date fields
209
- ;;
210
- esac
211
-
212
- # Write or update the field
213
- if grep -q "^${field}:" "$yaml_file"; then
214
- # Field exists, replace it
215
- sed -i "s/^${field}:.*/${field}: ${value}/" "$yaml_file"
216
- else
217
- # Field doesn't exist, append it
218
- echo "${field}: ${value}" >> "$yaml_file"
219
- fi
220
-
221
- green "[SET] ${field}=${value}"
222
- }
223
-
224
- # --- Check helpers for entry verification ---
225
-
226
- CHECK_BLOCK=0
227
-
228
- check_pass() {
229
- local msg="$1"
230
- echo " $(green "[PASS]") $msg"
231
- }
232
-
233
- check_fail() {
234
- local msg="$1"
235
- echo " $(red "[FAIL]") $msg"
236
- CHECK_BLOCK=1
237
- }
238
-
239
- check_nonempty() {
240
- local desc="$1"
241
- local path="$1"
242
- if file_nonempty "$path"; then
243
- check_pass "$desc non-empty"
244
- else
245
- check_fail "$desc missing or empty"
246
- fi
247
- }
248
-
249
- check_yaml_is() {
250
- local field="$1"
251
- local expected="$2"
252
- local change_name="$3"
253
- local actual
254
- actual=$(cmd_get "$change_name" "$field")
255
- if [ "$actual" = "$expected" ]; then
256
- check_pass "${field}=${actual} (expected: ${expected})"
257
- else
258
- check_fail "${field}=${actual} (expected: ${expected})"
259
- fi
260
- }
261
-
262
- check_yaml_empty() {
263
- local field="$1"
264
- local change_name="$2"
265
- local value
266
- value=$(cmd_get "$change_name" "$field")
267
- if [ -z "$value" ] || [ "$value" = "null" ]; then
268
- check_pass "${field} is empty/null"
269
- else
270
- check_fail "${field}=${value} (expected: empty/null)"
271
- fi
272
- }
273
-
274
- check_file_not_exists() {
275
- local desc="$1"
276
- local path="$2"
277
- if [ ! -f "$path" ]; then
278
- check_pass "$desc does not exist"
279
- else
280
- check_fail "$desc exists (should not exist)"
281
- fi
282
- }
283
-
284
- cmd_check() {
285
- local phase="$1"
286
- local change_name="$2"
287
-
288
- validate_change_name "$change_name"
289
- validate_enum "$phase" "open" "design" "build" "verify" "archive"
290
-
291
- local change_dir="openspec/changes/$change_name"
292
- local yaml_file="$change_dir/.comet.yaml"
293
- local proposal_file="$change_dir/proposal.md"
294
- local design_file="$change_dir/design.md"
295
- local tasks_file="$change_dir/tasks.md"
296
-
297
- echo "=== Entry Check: comet-${phase} ==="
298
-
299
- # For non-open phases, .comet.yaml must exist
300
- if [ "$phase" != "open" ]; then
301
- if [ ! -f "$yaml_file" ]; then
302
- red "ERROR: .comet.yaml not found at $yaml_file"
303
- exit 1
304
- fi
305
- fi
306
-
307
- # Phase-specific checks
308
- case "$phase" in
309
- open)
310
- check_file_not_exists ".comet.yaml" "$yaml_file"
311
- check_nonempty "proposal.md" "$proposal_file"
312
- check_nonempty "design.md" "$design_file"
313
- check_nonempty "tasks.md" "$tasks_file"
314
- ;;
315
- design)
316
- check_pass ".comet.yaml exists"
317
- check_yaml_is "phase" "design" "$change_name"
318
- check_yaml_is "workflow" "full" "$change_name"
319
- check_yaml_empty "design_doc" "$change_name"
320
- check_nonempty "proposal.md" "$proposal_file"
321
- check_nonempty "design.md" "$design_file"
322
- check_nonempty "tasks.md" "$tasks_file"
323
- ;;
324
- build)
325
- check_pass ".comet.yaml exists"
326
- check_yaml_is "phase" "build" "$change_name"
327
- # Check design_doc is non-null and file exists
328
- local design_doc
329
- design_doc=$(cmd_get "$change_name" "design_doc")
330
- if [ -n "$design_doc" ] && [ "$design_doc" != "null" ] && [ -f "$change_dir/$design_doc" ]; then
331
- check_pass "design_doc=${design_doc} (file exists)"
332
- else
333
- check_fail "design_doc=${design_doc} (expected: non-null and file exists)"
334
- fi
335
- check_nonempty "proposal.md" "$proposal_file"
336
- check_nonempty "tasks.md" "$tasks_file"
337
- ;;
338
- verify)
339
- check_pass ".comet.yaml exists"
340
- check_yaml_is "phase" "verify" "$change_name"
341
- # Check verify_result is pending or null
342
- local verify_result
343
- verify_result=$(cmd_get "$change_name" "verify_result")
344
- if [ "$verify_result" = "pending" ] || [ -z "$verify_result" ] || [ "$verify_result" = "null" ]; then
345
- check_pass "verify_result=${verify_result} (expected: pending or null)"
346
- else
347
- check_fail "verify_result=${verify_result} (expected: pending or null)"
348
- fi
349
- ;;
350
- archive)
351
- check_pass ".comet.yaml exists"
352
- check_yaml_is "phase" "archive" "$change_name"
353
- check_yaml_is "verify_result" "pass" "$change_name"
354
- # Check archived is NOT true
355
- local archived
356
- archived=$(cmd_get "$change_name" "archived")
357
- if [ "$archived" != "true" ]; then
358
- check_pass "archived=${archived} (expected: not true)"
359
- else
360
- check_fail "archived=${archived} (expected: not true)"
361
- fi
362
- ;;
363
- *)
364
- red "ERROR: Unknown phase for check: $phase"
365
- exit 1
366
- ;;
367
- esac
368
-
369
- echo ""
370
- if [ "$CHECK_BLOCK" -eq 1 ]; then
371
- red "BLOCKED fix failing checks before proceeding"
372
- exit 1
373
- else
374
- green "ALL CHECKS PASSED — ready to proceed"
375
- exit 0
376
- fi
377
- }
378
-
379
- cmd_scale() {
380
- local change_name="$1"
381
-
382
- validate_change_name "$change_name"
383
-
384
- local change_dir="openspec/changes/$change_name"
385
- local yaml_file="$change_dir/.comet.yaml"
386
-
387
- # Verify .comet.yaml exists
388
- if [ ! -f "$yaml_file" ]; then
389
- red "ERROR: .comet.yaml not found at $yaml_file"
390
- exit 1
391
- fi
392
-
393
- # Read metrics
394
- # 1. Task count: count lines matching `- [` in tasks.md
395
- local tasks_file="$change_dir/tasks.md"
396
- local task_count=0
397
- if [ -f "$tasks_file" ]; then
398
- task_count=$(grep -c '^\- \[' "$tasks_file" 2>/dev/null || echo "0")
399
- fi
400
-
401
- # 2. Delta spec count: count files named spec.md under specs/*/spec.md
402
- local delta_spec_count=0
403
- if [ -d "$change_dir/specs" ]; then
404
- delta_spec_count=$(find "$change_dir/specs" -name "spec.md" -type f 2>/dev/null | wc -l | tr -d ' ')
405
- fi
406
-
407
- # 3. Changed files: from git diff --stat HEAD
408
- local changed_files=0
409
- if git rev-parse --git-dir > /dev/null 2>&1; then
410
- # Extract the number before "file" in the last line
411
- local stat_output
412
- stat_output=$(git diff --stat HEAD 2>/dev/null | tail -1)
413
- if [[ "$stat_output" =~ ([0-9]+)\ file ]]; then
414
- changed_files="${BASH_REMATCH[1]}"
415
- fi
416
- fi
417
-
418
- # Decision rules
419
- local result="light"
420
- if [ "$task_count" -gt 3 ] || [ "$delta_spec_count" -gt 1 ] || [ "$changed_files" -gt 5 ]; then
421
- result="full"
422
- fi
423
-
424
- # Output assessment to stderr
425
- echo "=== Scale Assessment: $change_name ===" >&2
426
- echo " Tasks: $task_count (threshold: 3)" >&2
427
- echo " Delta specs: $delta_spec_count capabilities (threshold: 1)" >&2
428
- echo " Changed files: $changed_files (threshold: 5)" >&2
429
- echo " Result: $result" >&2
430
-
431
- # Update verify_mode in .comet.yaml
432
- sed -i "s/^verify_mode:.*/verify_mode: $result/" "$yaml_file"
433
-
434
- green "[SCALE] verify_mode=$result"
435
- }
436
-
437
- # --- Main ---
438
-
439
- SUBCOMMAND="${1:-}"
440
- shift || true
441
-
442
- case "$SUBCOMMAND" in
443
- init)
444
- if [ $# -lt 2 ]; then
445
- red "Usage: comet-state.sh init <change-name> <workflow>" >&2
446
- red "Workflows: full, hotfix, tweak" >&2
447
- exit 1
448
- fi
449
- cmd_init "$@"
450
- ;;
451
- get)
452
- if [ $# -lt 2 ]; then
453
- red "Usage: comet-state.sh get <change-name> <field>" >&2
454
- exit 1
455
- fi
456
- cmd_get "$@"
457
- ;;
458
- set)
459
- if [ $# -lt 3 ]; then
460
- red "Usage: comet-state.sh set <change-name> <field> <value>" >&2
461
- exit 1
462
- fi
463
- cmd_set "$@"
464
- ;;
465
- check)
466
- if [ $# -lt 2 ]; then
467
- red "Usage: comet-state.sh check <phase> <change-name>" >&2
468
- red "Phases: open, design, build, verify, archive" >&2
469
- exit 1
470
- fi
471
- cmd_check "$@"
472
- ;;
473
- scale)
474
- if [ $# -lt 1 ]; then
475
- red "Usage: comet-state.sh scale <change-name>" >&2
476
- exit 1
477
- fi
478
- cmd_scale "$@"
479
- ;;
480
- *)
481
- red "Unknown subcommand: $SUBCOMMAND" >&2
482
- echo "" >&2
483
- echo "Usage: comet-state.sh <subcommand> <change-name> [args...]" >&2
484
- echo "" >&2
485
- echo "Subcommands:" >&2
486
- echo " init <change-name> <workflow> — Initialize .comet.yaml with workflow defaults" >&2
487
- echo " get <change-name> <field> Read a field value from .comet.yaml" >&2
488
- echo " set <change-name> <field> <val> Update a field value in .comet.yaml" >&2
489
- echo " check <phase> <change-name> Verify entry requirements for a phase" >&2
490
- echo " scale <change-name> Assess and set verification mode based on metrics" >&2
491
- echo "" >&2
492
- echo "Workflows: full, hotfix, tweak" >&2
493
- echo "Phases for check: open, design, build, verify, archive" >&2
494
- exit 1
495
- ;;
496
- esac
1
+ #!/bin/bash
2
+ # Comet State — unified interface for .comet.yaml state management
3
+ # Usage: comet-state.sh <subcommand> <change-name> [args...]
4
+ #
5
+ # Subcommands:
6
+ # init <change-name> <workflow> — Initialize .comet.yaml with workflow defaults
7
+ # get <change-name> <field> — Read a field value from .comet.yaml
8
+ # set <change-name> <field> <val> — Update a field value
9
+ # check <phase> <change-name> — Verify entry requirements for a phase
10
+ # scale <change-name> — Assess and set verification mode based on metrics
11
+ #
12
+ # Workflows: full, hotfix, tweak
13
+ # Phases for check: open, design, build, verify, archive
14
+
15
+ set -euo pipefail
16
+
17
+ # --- Color output helpers ---
18
+
19
+ red() { echo -e "\033[31m$1\033[0m" >&2; }
20
+ green() { echo -e "\033[32m$1\033[0m" >&2; }
21
+ yellow() { echo -e "\033[33m$1\033[0m" >&2; }
22
+
23
+ # --- Script location ---
24
+
25
+ # shellcheck disable=SC2034
26
+ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
27
+
28
+ # --- Input validation ---
29
+
30
+ validate_change_name() {
31
+ local name="$1"
32
+ # Reject empty names
33
+ if [ -z "$name" ]; then
34
+ red "ERROR: Change name cannot be empty" >&2
35
+ exit 1
36
+ fi
37
+ # Only allow alphanumeric, hyphens, and underscores
38
+ if [[ ! "$name" =~ ^[a-zA-Z0-9_-]+$ ]]; then
39
+ red "ERROR: Invalid change name: '$name'" >&2
40
+ red "Valid characters: a-z, A-Z, 0-9, -, _" >&2
41
+ exit 1
42
+ fi
43
+ # Reject path traversal attempts
44
+ if [[ "$name" =~ \.\. ]]; then
45
+ red "ERROR: Change name cannot contain '..' (path traversal not allowed)" >&2
46
+ exit 1
47
+ fi
48
+ }
49
+
50
+ validate_enum() {
51
+ local value="$1"
52
+ shift
53
+ local valid_values=("$@")
54
+
55
+ for valid in "${valid_values[@]}"; do
56
+ if [ "$value" = "$valid" ]; then
57
+ return 0
58
+ fi
59
+ done
60
+
61
+ red "ERROR: Invalid value: '$value'" >&2
62
+ red "Valid values: ${valid_values[*]}" >&2
63
+ exit 1
64
+ }
65
+
66
+ # --- Helper functions ---
67
+
68
+ yaml_field() {
69
+ local field="$1"
70
+ local yaml_file="$2"
71
+ if [ -f "$yaml_file" ]; then
72
+ grep "^${field}:" "$yaml_file" | sed "s/^${field}: *//" | tr -d '"' | tr -d "'"
73
+ fi
74
+ }
75
+
76
+ file_nonempty() {
77
+ [ -f "$1" ] && [ -s "$1" ]
78
+ }
79
+
80
+ # --- Subcommands ---
81
+
82
+ cmd_init() {
83
+ local change_name="$1"
84
+ local workflow="$2"
85
+
86
+ validate_change_name "$change_name"
87
+ validate_enum "$workflow" "full" "hotfix" "tweak"
88
+
89
+ local change_dir="openspec/changes/$change_name"
90
+ local yaml_file="$change_dir/.comet.yaml"
91
+
92
+ # Check if .comet.yaml already exists
93
+ if [ -f "$yaml_file" ]; then
94
+ red "ERROR: .comet.yaml already exists at $yaml_file"
95
+ exit 1
96
+ fi
97
+
98
+ # Create change directory if it doesn't exist
99
+ mkdir -p "$change_dir"
100
+
101
+ # Set workflow-appropriate defaults
102
+ local phase build_mode isolation verify_mode
103
+
104
+ case "$workflow" in
105
+ full)
106
+ phase="design"
107
+ build_mode="null"
108
+ isolation="null"
109
+ verify_mode="null"
110
+ ;;
111
+ hotfix|tweak)
112
+ phase="build"
113
+ build_mode="direct"
114
+ isolation="branch"
115
+ verify_mode="light"
116
+ ;;
117
+ esac
118
+
119
+ # Write .comet.yaml
120
+ cat > "$yaml_file" <<EOF
121
+ workflow: $workflow
122
+ phase: $phase
123
+ build_mode: $build_mode
124
+ isolation: $isolation
125
+ verify_mode: $verify_mode
126
+ design_doc: null
127
+ plan: null
128
+ verify_result: pending
129
+ verified_at: null
130
+ archived: false
131
+ EOF
132
+
133
+ green "Initialized: $yaml_file (workflow=$workflow)"
134
+ }
135
+
136
+ cmd_get() {
137
+ local change_name="$1"
138
+ local field="$2"
139
+
140
+ validate_change_name "$change_name"
141
+
142
+ local change_dir="openspec/changes/$change_name"
143
+ local yaml_file="$change_dir/.comet.yaml"
144
+
145
+ # Check if .comet.yaml exists
146
+ if [ ! -f "$yaml_file" ]; then
147
+ red "ERROR: .comet.yaml not found at $yaml_file"
148
+ exit 1
149
+ fi
150
+
151
+ # Read and output the field value
152
+ local value
153
+ value=$(yaml_field "$field" "$yaml_file")
154
+ echo "${value:-}"
155
+ }
156
+
157
+ cmd_set() {
158
+ local change_name="$1"
159
+ local field="$2"
160
+ local value="$3"
161
+
162
+ validate_change_name "$change_name"
163
+
164
+ local change_dir="openspec/changes/$change_name"
165
+ local yaml_file="$change_dir/.comet.yaml"
166
+
167
+ # Check if .comet.yaml exists
168
+ if [ ! -f "$yaml_file" ]; then
169
+ red "ERROR: .comet.yaml not found at $yaml_file"
170
+ exit 1
171
+ fi
172
+
173
+ # Validate field name
174
+ case "$field" in
175
+ workflow|phase|build_mode|isolation|verify_mode|verify_result|archived|design_doc|plan|verified_at)
176
+ # Valid field
177
+ ;;
178
+ *)
179
+ red "ERROR: Unknown field: '$field'" >&2
180
+ red "Valid fields: workflow, phase, design_doc, plan, build_mode, isolation, verify_mode, verify_result, verified_at, archived" >&2
181
+ exit 1
182
+ ;;
183
+ esac
184
+
185
+ # Validate enum values
186
+ case "$field" in
187
+ workflow)
188
+ validate_enum "$value" "full" "hotfix" "tweak"
189
+ ;;
190
+ phase)
191
+ validate_enum "$value" "design" "build" "verify" "archive"
192
+ ;;
193
+ build_mode)
194
+ validate_enum "$value" "subagent-driven-development" "executing-plans" "direct"
195
+ ;;
196
+ isolation)
197
+ validate_enum "$value" "branch" "worktree"
198
+ ;;
199
+ verify_mode)
200
+ validate_enum "$value" "light" "full"
201
+ ;;
202
+ verify_result)
203
+ validate_enum "$value" "pending" "pass" "fail"
204
+ ;;
205
+ archived)
206
+ validate_enum "$value" "true" "false"
207
+ ;;
208
+ design_doc|plan|verified_at)
209
+ # No validation for path fields and date fields
210
+ ;;
211
+ esac
212
+
213
+ # Write or update the field
214
+ if grep -q "^${field}:" "$yaml_file"; then
215
+ # Field exists, replace it (use | delimiter to avoid / conflicts in paths)
216
+ sed -i "s|^${field}:.*|${field}: ${value}|" "$yaml_file"
217
+ else
218
+ # Field doesn't exist, append it
219
+ echo "${field}: ${value}" >> "$yaml_file"
220
+ fi
221
+
222
+ green "[SET] ${field}=${value}"
223
+ }
224
+
225
+ # --- Check helpers for entry verification ---
226
+
227
+ CHECK_BLOCK=0
228
+
229
+ check_pass() {
230
+ local msg="$1"
231
+ echo " $(green "[PASS]") $msg"
232
+ }
233
+
234
+ check_fail() {
235
+ local msg="$1"
236
+ echo " $(red "[FAIL]") $msg"
237
+ CHECK_BLOCK=1
238
+ }
239
+
240
+ check_nonempty() {
241
+ local desc="$1"
242
+ local path="$2"
243
+ if file_nonempty "$path"; then
244
+ check_pass "$desc non-empty"
245
+ else
246
+ check_fail "$desc missing or empty"
247
+ fi
248
+ }
249
+
250
+ check_yaml_is() {
251
+ local field="$1"
252
+ local expected="$2"
253
+ local change_name="$3"
254
+ local actual
255
+ actual=$(cmd_get "$change_name" "$field")
256
+ if [ "$actual" = "$expected" ]; then
257
+ check_pass "${field}=${actual} (expected: ${expected})"
258
+ else
259
+ check_fail "${field}=${actual} (expected: ${expected})"
260
+ fi
261
+ }
262
+
263
+ check_yaml_empty() {
264
+ local field="$1"
265
+ local change_name="$2"
266
+ local value
267
+ value=$(cmd_get "$change_name" "$field")
268
+ if [ -z "$value" ] || [ "$value" = "null" ]; then
269
+ check_pass "${field} is empty/null"
270
+ else
271
+ check_fail "${field}=${value} (expected: empty/null)"
272
+ fi
273
+ }
274
+
275
+ check_file_not_exists() {
276
+ local desc="$1"
277
+ local path="$2"
278
+ if [ ! -f "$path" ]; then
279
+ check_pass "$desc does not exist"
280
+ else
281
+ check_fail "$desc exists (should not exist)"
282
+ fi
283
+ }
284
+
285
+ cmd_check() {
286
+ local phase="$1"
287
+ local change_name="$2"
288
+
289
+ validate_change_name "$change_name"
290
+ validate_enum "$phase" "open" "design" "build" "verify" "archive"
291
+
292
+ local change_dir="openspec/changes/$change_name"
293
+ local yaml_file="$change_dir/.comet.yaml"
294
+ local proposal_file="$change_dir/proposal.md"
295
+ local design_file="$change_dir/design.md"
296
+ local tasks_file="$change_dir/tasks.md"
297
+
298
+ echo "=== Entry Check: comet-${phase} ==="
299
+
300
+ # For non-open phases, .comet.yaml must exist
301
+ if [ "$phase" != "open" ]; then
302
+ if [ ! -f "$yaml_file" ]; then
303
+ red "ERROR: .comet.yaml not found at $yaml_file"
304
+ exit 1
305
+ fi
306
+ fi
307
+
308
+ # Phase-specific checks
309
+ case "$phase" in
310
+ open)
311
+ check_file_not_exists ".comet.yaml" "$yaml_file"
312
+ check_nonempty "proposal.md" "$proposal_file"
313
+ check_nonempty "design.md" "$design_file"
314
+ check_nonempty "tasks.md" "$tasks_file"
315
+ ;;
316
+ design)
317
+ check_pass ".comet.yaml exists"
318
+ check_yaml_is "phase" "design" "$change_name"
319
+ check_yaml_is "workflow" "full" "$change_name"
320
+ check_yaml_empty "design_doc" "$change_name"
321
+ check_nonempty "proposal.md" "$proposal_file"
322
+ check_nonempty "design.md" "$design_file"
323
+ check_nonempty "tasks.md" "$tasks_file"
324
+ ;;
325
+ build)
326
+ check_pass ".comet.yaml exists"
327
+ check_yaml_is "phase" "build" "$change_name"
328
+ # Check design_doc is non-null and file exists
329
+ local design_doc
330
+ design_doc=$(cmd_get "$change_name" "design_doc")
331
+ if [ -n "$design_doc" ] && [ "$design_doc" != "null" ] && [ -f "$change_dir/$design_doc" ]; then
332
+ check_pass "design_doc=${design_doc} (file exists)"
333
+ else
334
+ check_fail "design_doc=${design_doc} (expected: non-null and file exists)"
335
+ fi
336
+ check_nonempty "proposal.md" "$proposal_file"
337
+ check_nonempty "tasks.md" "$tasks_file"
338
+ ;;
339
+ verify)
340
+ check_pass ".comet.yaml exists"
341
+ check_yaml_is "phase" "verify" "$change_name"
342
+ # Check verify_result is pending or null
343
+ local verify_result
344
+ verify_result=$(cmd_get "$change_name" "verify_result")
345
+ if [ "$verify_result" = "pending" ] || [ -z "$verify_result" ] || [ "$verify_result" = "null" ]; then
346
+ check_pass "verify_result=${verify_result} (expected: pending or null)"
347
+ else
348
+ check_fail "verify_result=${verify_result} (expected: pending or null)"
349
+ fi
350
+ ;;
351
+ archive)
352
+ check_pass ".comet.yaml exists"
353
+ check_yaml_is "phase" "archive" "$change_name"
354
+ check_yaml_is "verify_result" "pass" "$change_name"
355
+ # Check archived is NOT true
356
+ local archived
357
+ archived=$(cmd_get "$change_name" "archived")
358
+ if [ "$archived" != "true" ]; then
359
+ check_pass "archived=${archived} (expected: not true)"
360
+ else
361
+ check_fail "archived=${archived} (expected: not true)"
362
+ fi
363
+ ;;
364
+ *)
365
+ red "ERROR: Unknown phase for check: $phase"
366
+ exit 1
367
+ ;;
368
+ esac
369
+
370
+ echo ""
371
+ if [ "$CHECK_BLOCK" -eq 1 ]; then
372
+ red "BLOCKED — fix failing checks before proceeding"
373
+ exit 1
374
+ else
375
+ green "ALL CHECKS PASSED — ready to proceed"
376
+ exit 0
377
+ fi
378
+ }
379
+
380
+ cmd_scale() {
381
+ local change_name="$1"
382
+
383
+ validate_change_name "$change_name"
384
+
385
+ local change_dir="openspec/changes/$change_name"
386
+ local yaml_file="$change_dir/.comet.yaml"
387
+
388
+ # Verify .comet.yaml exists
389
+ if [ ! -f "$yaml_file" ]; then
390
+ red "ERROR: .comet.yaml not found at $yaml_file"
391
+ exit 1
392
+ fi
393
+
394
+ # Read metrics
395
+ # 1. Task count: count lines matching `- [` in tasks.md
396
+ local tasks_file="$change_dir/tasks.md"
397
+ local task_count=0
398
+ if [ -f "$tasks_file" ]; then
399
+ task_count=$(grep -c '^\- \[' "$tasks_file" 2>/dev/null || echo "0")
400
+ fi
401
+
402
+ # 2. Delta spec count: count files named spec.md under specs/*/spec.md
403
+ local delta_spec_count=0
404
+ if [ -d "$change_dir/specs" ]; then
405
+ delta_spec_count=$(find "$change_dir/specs" -name "spec.md" -type f 2>/dev/null | wc -l | tr -d ' ')
406
+ fi
407
+
408
+ # 3. Changed files: from git diff --stat HEAD
409
+ local changed_files=0
410
+ if git rev-parse --git-dir > /dev/null 2>&1; then
411
+ # Extract the number before "file" in the last line
412
+ local stat_output
413
+ stat_output=$(git diff --stat HEAD 2>/dev/null | tail -1)
414
+ if [[ "$stat_output" =~ ([0-9]+)\ file ]]; then
415
+ changed_files="${BASH_REMATCH[1]}"
416
+ fi
417
+ fi
418
+
419
+ # Decision rules
420
+ local result="light"
421
+ if [ "$task_count" -gt 3 ] || [ "$delta_spec_count" -gt 1 ] || [ "$changed_files" -gt 5 ]; then
422
+ result="full"
423
+ fi
424
+
425
+ # Output assessment to stderr
426
+ echo "=== Scale Assessment: $change_name ===" >&2
427
+ echo " Tasks: $task_count (threshold: 3)" >&2
428
+ echo " Delta specs: $delta_spec_count capabilities (threshold: 1)" >&2
429
+ echo " Changed files: $changed_files (threshold: 5)" >&2
430
+ echo " → Result: $result" >&2
431
+
432
+ # Update verify_mode in .comet.yaml
433
+ sed -i "s/^verify_mode:.*/verify_mode: $result/" "$yaml_file"
434
+
435
+ green "[SCALE] verify_mode=$result"
436
+ }
437
+
438
+ # --- Main ---
439
+
440
+ SUBCOMMAND="${1:-}"
441
+ shift || true
442
+
443
+ case "$SUBCOMMAND" in
444
+ init)
445
+ if [ $# -lt 2 ]; then
446
+ red "Usage: comet-state.sh init <change-name> <workflow>" >&2
447
+ red "Workflows: full, hotfix, tweak" >&2
448
+ exit 1
449
+ fi
450
+ cmd_init "$@"
451
+ ;;
452
+ get)
453
+ if [ $# -lt 2 ]; then
454
+ red "Usage: comet-state.sh get <change-name> <field>" >&2
455
+ exit 1
456
+ fi
457
+ cmd_get "$@"
458
+ ;;
459
+ set)
460
+ if [ $# -lt 3 ]; then
461
+ red "Usage: comet-state.sh set <change-name> <field> <value>" >&2
462
+ exit 1
463
+ fi
464
+ cmd_set "$@"
465
+ ;;
466
+ check)
467
+ if [ $# -lt 2 ]; then
468
+ red "Usage: comet-state.sh check <phase> <change-name>" >&2
469
+ red "Phases: open, design, build, verify, archive" >&2
470
+ exit 1
471
+ fi
472
+ cmd_check "$@"
473
+ ;;
474
+ scale)
475
+ if [ $# -lt 1 ]; then
476
+ red "Usage: comet-state.sh scale <change-name>" >&2
477
+ exit 1
478
+ fi
479
+ cmd_scale "$@"
480
+ ;;
481
+ *)
482
+ red "Unknown subcommand: $SUBCOMMAND" >&2
483
+ echo "" >&2
484
+ echo "Usage: comet-state.sh <subcommand> <change-name> [args...]" >&2
485
+ echo "" >&2
486
+ echo "Subcommands:" >&2
487
+ echo " init <change-name> <workflow> Initialize .comet.yaml with workflow defaults" >&2
488
+ echo " get <change-name> <field> Read a field value from .comet.yaml" >&2
489
+ echo " set <change-name> <field> <val> Update a field value in .comet.yaml" >&2
490
+ echo " check <phase> <change-name> Verify entry requirements for a phase" >&2
491
+ echo " scale <change-name> — Assess and set verification mode based on metrics" >&2
492
+ echo "" >&2
493
+ echo "Workflows: full, hotfix, tweak" >&2
494
+ echo "Phases for check: open, design, build, verify, archive" >&2
495
+ exit 1
496
+ ;;
497
+ esac