@soleur/opencode 3.409.1 → 3.410.1

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.
@@ -0,0 +1,557 @@
1
+ #!/usr/bin/env bash
2
+ #
3
+ # Parse one retry-budget ruling line and append a fixed ledger section.
4
+ # Identity, then a spent spawn bit, then --not-capped, then empty history,
5
+ # then at most 64 stdin bytes. The parent branches on the verdict line.
6
+
7
+ set -euo pipefail
8
+
9
+ emit() {
10
+ local verb="$1" reason="$2" plain="$3"
11
+ printf 'SOLEUR_RETRY_BUDGET_RULING verb=%s reason=%s\n' "$verb" "$reason"
12
+ if [[ "$plain" == 1 ]]; then
13
+ printf '%s %s\n' "$verb" "$reason"
14
+ fi
15
+ }
16
+
17
+ # 0 when any component from root through path is a symlink. A trailing
18
+ # slash or a final "/." does not hide the link. A path outside root is
19
+ # refused the same way.
20
+ path_has_symlink() {
21
+ local root="$1" path="$2" rel part acc
22
+ root="${root%/}"
23
+ while [[ "$path" == */ ]]; do
24
+ path="${path%/}"
25
+ done
26
+ while [[ "$path" == */. ]]; do
27
+ path="${path%/.}"
28
+ while [[ "$path" == */ ]]; do
29
+ path="${path%/}"
30
+ done
31
+ done
32
+ if [[ -L "$root" ]]; then
33
+ return 0
34
+ fi
35
+ if [[ "$path" == "$root" ]]; then
36
+ return 1
37
+ fi
38
+ if [[ "$path" != "$root/"* ]]; then
39
+ return 0
40
+ fi
41
+ rel="${path#"$root"/}"
42
+ acc="$root"
43
+ while [[ -n "$rel" ]]; do
44
+ part="${rel%%/*}"
45
+ if [[ "$rel" == */* ]]; then
46
+ rel="${rel#*/}"
47
+ else
48
+ rel=""
49
+ fi
50
+ if [[ -z "$part" || "$part" == "." ]]; then
51
+ continue
52
+ fi
53
+ if [[ "$part" == ".." ]]; then
54
+ return 0
55
+ fi
56
+ acc="${acc}/${part}"
57
+ if [[ -L "$acc" ]]; then
58
+ return 0
59
+ fi
60
+ done
61
+ return 1
62
+ }
63
+
64
+ for arg in "$@"; do
65
+ if [[ "$arg" == "--probe" ]]; then
66
+ printf '%s\n' ok
67
+ exit 0
68
+ fi
69
+ done
70
+
71
+ toplevel=""
72
+ branch=""
73
+ git_common=""
74
+ ledger=""
75
+ history=""
76
+ child_session=""
77
+ phase_flag=""
78
+ not_capped=0
79
+
80
+ while [[ $# -gt 0 ]]; do
81
+ case "$1" in
82
+ --not-capped)
83
+ not_capped=1
84
+ shift
85
+ ;;
86
+ --toplevel|--branch|--git-common-dir|--ledger|--history|--child-session|--phase|--evidence)
87
+ if [[ $# -lt 2 ]]; then
88
+ emit fail identity 1
89
+ exit 2
90
+ fi
91
+ key="$1"
92
+ val="$2"
93
+ shift 2
94
+ case "$key" in
95
+ --toplevel) toplevel="$val" ;;
96
+ --branch) branch="$val" ;;
97
+ --git-common-dir) git_common="$val" ;;
98
+ --ledger) ledger="$val" ;;
99
+ --history) history="$val" ;;
100
+ --child-session) child_session="$val" ;;
101
+ --phase) phase_flag="$val" ;;
102
+ --evidence) ;;
103
+ esac
104
+ ;;
105
+ *)
106
+ emit fail identity 1
107
+ exit 2
108
+ ;;
109
+ esac
110
+ done
111
+
112
+ if [[ -z "$toplevel" || -z "$branch" || -z "$git_common" || -z "$ledger" ]]; then
113
+ emit fail identity 1
114
+ exit 2
115
+ fi
116
+
117
+ top="${toplevel%/}"
118
+ parent="${ledger%/*}"
119
+ base="${parent##*/}"
120
+ leaf="${ledger##*/}"
121
+ if [[ -z "$top" || "$parent" == "$ledger" || "$base" != "$branch" || "$leaf" != "session-state.md" || "$ledger" != "$top/"* || ! -d "$parent" ]]; then
122
+ emit fail identity 1
123
+ exit 2
124
+ fi
125
+ case "$ledger" in
126
+ ..|../*|*/..|*/../*)
127
+ emit fail identity 1
128
+ exit 2
129
+ ;;
130
+ esac
131
+ case "$toplevel" in
132
+ ..|../*|*/..|*/../*)
133
+ emit fail identity 1
134
+ exit 2
135
+ ;;
136
+ esac
137
+
138
+ field_rc=0
139
+ python3 - "$branch" "$toplevel" "$git_common" "$ledger" "$child_session" "$phase_flag" <<'PY' || field_rc=$?
140
+ import sys
141
+
142
+ for field in sys.argv[1:]:
143
+ raw = field.encode("utf-8", "surrogateescape")
144
+ if any(byte < 32 or byte == 127 for byte in raw):
145
+ sys.exit(1)
146
+ if "\u2028" in field or "\u2029" in field or "\u0085" in field:
147
+ sys.exit(1)
148
+ if "ruling_grant" in field or "ruling_spawn" in field:
149
+ sys.exit(1)
150
+ if field.splitlines() != ([field] if field else []):
151
+ sys.exit(1)
152
+ sys.exit(0)
153
+ PY
154
+ if [[ "$field_rc" -ne 0 ]]; then
155
+ emit fail identity 1
156
+ exit 2
157
+ fi
158
+
159
+ if [[ -L "$ledger" ]]; then
160
+ emit fail identity 1
161
+ exit 2
162
+ fi
163
+
164
+ real_top=$(realpath -e -- "$top" 2>/dev/null) || {
165
+ emit fail identity 1
166
+ exit 2
167
+ }
168
+ real_parent=$(realpath -e -- "$parent" 2>/dev/null) || {
169
+ emit fail identity 1
170
+ exit 2
171
+ }
172
+ case "$real_parent/" in
173
+ "$real_top"/*) ;;
174
+ *)
175
+ emit fail identity 1
176
+ exit 2
177
+ ;;
178
+ esac
179
+ # An in-tree directory symlink keeps the lexical branch name and still
180
+ # points at another spec. The resolved directory name has to be the branch.
181
+ real_base="${real_parent##*/}"
182
+ if [[ "$real_base" != "$branch" ]]; then
183
+ emit fail identity 1
184
+ exit 2
185
+ fi
186
+ # Basename equality is not enough. Refuse a symlink in any component from
187
+ # the toplevel through the parent, including a same-basename directory link.
188
+ if path_has_symlink "$top" "$parent"; then
189
+ emit fail identity 1
190
+ exit 2
191
+ fi
192
+ if [[ -n "$child_session" ]]; then
193
+ if path_has_symlink "$top" "$child_session"; then
194
+ emit fail identity 1
195
+ exit 2
196
+ fi
197
+ real_child=$(realpath -e -- "$child_session" 2>/dev/null) || {
198
+ emit fail identity 1
199
+ exit 2
200
+ }
201
+ case "$real_child/" in
202
+ "$real_top"/*) ;;
203
+ *)
204
+ emit fail identity 1
205
+ exit 2
206
+ ;;
207
+ esac
208
+ fi
209
+
210
+ line_used() {
211
+ python3 - "$1" <<'PY'
212
+ import sys
213
+ try:
214
+ data = open(sys.argv[1], "rb").read()
215
+ except OSError:
216
+ sys.exit(3)
217
+ sys.exit(0 if b"ruling_spawn: used" in data.splitlines() else 1)
218
+ PY
219
+ }
220
+
221
+ if [[ -f "$ledger" && -s "$ledger" ]]; then
222
+ spent_rc=0
223
+ line_used "$ledger" || spent_rc=$?
224
+ if [[ "$spent_rc" -eq 0 ]]; then
225
+ emit fail spent 0
226
+ exit 2
227
+ fi
228
+ if [[ "$spent_rc" -ne 1 ]]; then
229
+ emit fail write-failed 1
230
+ exit 2
231
+ fi
232
+ fi
233
+
234
+ append_section() {
235
+ local verb="$1" reason="$2" phase="$3" spawn="$4"
236
+ local spawn_line="${5:-}" applied="${6:-}"
237
+ local py_rc=0
238
+ # Exclusive lock on the ledger inode for up to 5s, then append. A replace
239
+ # would drop a concurrent append, including the spawn bit. A short write
240
+ # truncates back to the size from before that write.
241
+ SOLEUR_RULING_LEDGER="$ledger" \
242
+ SOLEUR_RULING_BRANCH="$branch" \
243
+ SOLEUR_RULING_TOPLEVEL="$toplevel" \
244
+ SOLEUR_RULING_GIT_COMMON="$git_common" \
245
+ SOLEUR_RULING_VERB="$verb" \
246
+ SOLEUR_RULING_PHASE="$phase" \
247
+ SOLEUR_RULING_REASON="$reason" \
248
+ SOLEUR_RULING_CHILD="$child_session" \
249
+ SOLEUR_RULING_SPAWN="$spawn" \
250
+ SOLEUR_RULING_SPAWN_LINE="$spawn_line" \
251
+ SOLEUR_RULING_APPLIED="$applied" \
252
+ python3 - <<'PY' || py_rc=$?
253
+ import errno
254
+ import os
255
+ import sys
256
+ import time
257
+
258
+ import fcntl
259
+
260
+ ledger = os.environ["SOLEUR_RULING_LEDGER"]
261
+ phase = os.environ["SOLEUR_RULING_PHASE"]
262
+ spawn = os.environ["SOLEUR_RULING_SPAWN"] == "1"
263
+ spawn_line = os.environ.get("SOLEUR_RULING_SPAWN_LINE", "")
264
+ applied = os.environ.get("SOLEUR_RULING_APPLIED", "")
265
+
266
+ def build(verb, reason, phase_value, with_spawn, line, applied_line):
267
+ phase_line = "phase: " + phase_value if phase_value else "phase:"
268
+ lines = [
269
+ "",
270
+ "## Retry budget ruling",
271
+ "",
272
+ "branch: " + os.environ["SOLEUR_RULING_BRANCH"],
273
+ "toplevel: " + os.environ["SOLEUR_RULING_TOPLEVEL"],
274
+ "git-common-dir: " + os.environ["SOLEUR_RULING_GIT_COMMON"],
275
+ "verb: " + verb,
276
+ phase_line,
277
+ "reason: " + reason,
278
+ "child-session: " + os.environ["SOLEUR_RULING_CHILD"],
279
+ ]
280
+ if line:
281
+ lines.append("spawn-line: " + line)
282
+ if applied_line:
283
+ lines.append("applied: " + applied_line)
284
+ if with_spawn:
285
+ lines.append("ruling_spawn: used")
286
+ lines.append("")
287
+ text = "\n".join(lines)
288
+ if "ruling_grant" in text:
289
+ sys.exit(2)
290
+ return text.encode("utf-8")
291
+
292
+ def write_all(fd, payload):
293
+ view = payload
294
+ while view:
295
+ wrote = os.write(fd, view)
296
+ if wrote <= 0:
297
+ return False
298
+ view = view[wrote:]
299
+ return True
300
+
301
+ def open_ledger(path, top, open_flags):
302
+ # Open the parent with a no-follow directory fd, then the leaf from
303
+ # that fd. O_NOFOLLOW on a full path follows every earlier component.
304
+ def bare(value):
305
+ while value.endswith("/"):
306
+ value = value[:-1]
307
+ while value.endswith("/."):
308
+ value = value[:-2]
309
+ while value.endswith("/"):
310
+ value = value[:-1]
311
+ return value
312
+
313
+ path = bare(path)
314
+ top = bare(top)
315
+ prefix = top + "/"
316
+ if not path.startswith(prefix):
317
+ sys.exit(5)
318
+ parts = [part for part in path[len(prefix):].split("/") if part not in ("", ".")]
319
+ if not parts or any(part == ".." for part in parts):
320
+ sys.exit(5)
321
+ nofollow = os.O_NOFOLLOW if hasattr(os, "O_NOFOLLOW") else 0
322
+ try:
323
+ dirfd = os.open(top, os.O_RDONLY | os.O_DIRECTORY)
324
+ except OSError:
325
+ sys.exit(5)
326
+ try:
327
+ for part in parts[:-1]:
328
+ try:
329
+ nextfd = os.open(
330
+ part, os.O_RDONLY | os.O_DIRECTORY | nofollow, dir_fd=dirfd
331
+ )
332
+ except OSError as exc:
333
+ if exc.errno in (errno.ELOOP, errno.ENOTDIR, errno.ENOENT):
334
+ sys.exit(5)
335
+ sys.exit(2)
336
+ os.close(dirfd)
337
+ dirfd = nextfd
338
+ try:
339
+ return os.open(parts[-1], open_flags, 0o644, dir_fd=dirfd)
340
+ except OSError as exc:
341
+ if exc.errno in (errno.ELOOP, errno.ENOTDIR):
342
+ sys.exit(5)
343
+ sys.exit(2)
344
+ finally:
345
+ os.close(dirfd)
346
+
347
+ flags = os.O_RDWR | os.O_CREAT | os.O_APPEND
348
+ if hasattr(os, "O_NOFOLLOW"):
349
+ flags |= os.O_NOFOLLOW
350
+ fd = open_ledger(ledger, os.environ["SOLEUR_RULING_TOPLEVEL"], flags)
351
+ deadline = time.monotonic() + 5
352
+ while True:
353
+ try:
354
+ fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
355
+ break
356
+ except BlockingIOError:
357
+ if time.monotonic() >= deadline:
358
+ os.close(fd)
359
+ sys.exit(2)
360
+ time.sleep(0.02)
361
+ try:
362
+ os.lseek(fd, 0, os.SEEK_SET)
363
+ chunks = []
364
+ while True:
365
+ block = os.read(fd, 65536)
366
+ if not block:
367
+ break
368
+ chunks.append(block)
369
+ if b"ruling_spawn: used" in b"".join(chunks).splitlines():
370
+ sys.exit(4)
371
+ info = os.fstat(fd)
372
+ if info.st_nlink != 1:
373
+ sys.exit(5)
374
+ payload = build(
375
+ os.environ["SOLEUR_RULING_VERB"],
376
+ os.environ["SOLEUR_RULING_REASON"],
377
+ phase,
378
+ spawn,
379
+ spawn_line,
380
+ applied,
381
+ )
382
+ size_before = info.st_size
383
+ wrote_ok = False
384
+ try:
385
+ wrote_ok = write_all(fd, payload)
386
+ except OSError:
387
+ wrote_ok = False
388
+ if not wrote_ok:
389
+ try:
390
+ os.ftruncate(fd, size_before)
391
+ except OSError:
392
+ sys.exit(2)
393
+ if spawn:
394
+ recovery = build("fail", "write-failed", phase, True, spawn_line, "")
395
+ recovered = False
396
+ try:
397
+ recovered = write_all(fd, recovery)
398
+ except OSError:
399
+ recovered = False
400
+ if not recovered:
401
+ try:
402
+ os.ftruncate(fd, size_before)
403
+ except OSError:
404
+ pass
405
+ sys.exit(2)
406
+ sys.exit(6)
407
+ sys.exit(2)
408
+ finally:
409
+ os.close(fd)
410
+ PY
411
+ return "$py_rc"
412
+ }
413
+
414
+ append_or_spent() {
415
+ local rc=0
416
+ append_section "$@" || rc=$?
417
+ if [[ "$rc" -eq 4 ]]; then
418
+ emit fail spent 0
419
+ exit 2
420
+ fi
421
+ if [[ "$rc" -eq 5 ]]; then
422
+ emit fail identity 1
423
+ exit 2
424
+ fi
425
+ if [[ "$rc" -ne 0 ]]; then
426
+ emit fail write-failed 1
427
+ exit 2
428
+ fi
429
+ }
430
+
431
+ phase_stored_ok() {
432
+ case "$phase_flag" in
433
+ ""|plan|work|review|qa|test-fix-loop) return 0 ;;
434
+ *) return 1 ;;
435
+ esac
436
+ }
437
+
438
+ if [[ "$not_capped" -eq 1 ]]; then
439
+ if ! phase_stored_ok; then
440
+ emit fail identity 1
441
+ exit 2
442
+ fi
443
+ append_or_spent fail not-capped "$phase_flag" 0
444
+ emit fail not-capped 1
445
+ exit 2
446
+ fi
447
+
448
+ hist_nonempty=0
449
+ if [[ -n "$history" && ! -L "$history" && -f "$history" && -s "$history" ]]; then
450
+ real_hist=$(realpath -e -- "$history" 2>/dev/null || true)
451
+ case "$real_hist/" in
452
+ "$real_top"/*) hist_nonempty=1 ;;
453
+ esac
454
+ fi
455
+ ledger_present=0
456
+ if [[ -f "$ledger" && -s "$ledger" ]]; then
457
+ ledger_present=1
458
+ fi
459
+
460
+ if [[ "$hist_nonempty" -eq 0 || "$ledger_present" -eq 0 ]]; then
461
+ if ! phase_stored_ok; then
462
+ emit fail identity 1
463
+ exit 2
464
+ fi
465
+ append_or_spent fail empty-history "$phase_flag" 0
466
+ emit fail empty-history 1
467
+ exit 2
468
+ fi
469
+
470
+ raw="$(mktemp)"
471
+ trap 'rm -f "$raw"' EXIT
472
+ dd_rc=0
473
+ timeout 2 dd if=/dev/stdin of="$raw" bs=1 count=64 status=none || dd_rc=$?
474
+ if [[ "$dd_rc" -ne 0 ]]; then
475
+ if ! phase_stored_ok; then
476
+ emit fail identity 1
477
+ exit 2
478
+ fi
479
+ append_or_spent fail unparseable "$phase_flag" 1
480
+ emit fail unparseable 1
481
+ exit 2
482
+ fi
483
+
484
+ parsed="$(python3 - "$raw" <<'PY'
485
+ import sys
486
+
487
+ data = open(sys.argv[1], "rb").read()
488
+
489
+ def classify(buf):
490
+ if buf == b"":
491
+ return "empty", ""
492
+ for byte in buf:
493
+ if (byte < 32 and byte != 10) or byte == 127:
494
+ return "unparseable", ""
495
+ newlines = buf.count(b"\n")
496
+ if newlines > 1 or (newlines == 1 and not buf.endswith(b"\n")):
497
+ return "unparseable", ""
498
+ line = buf[:-1] if buf.endswith(b"\n") else buf
499
+ if line in (b"continue", b"fail"):
500
+ return line.decode("ascii"), ""
501
+ if line.startswith(b"goto "):
502
+ rest = line[5:]
503
+ allowed = (b"plan", b"work", b"review", b"qa", b"test-fix-loop")
504
+ if rest in allowed:
505
+ return "goto", rest.decode("ascii")
506
+ return "unparseable", ""
507
+
508
+ kind, phase = classify(data)
509
+ sys.stdout.write(kind + "\t" + phase)
510
+ PY
511
+ )"
512
+ kind="${parsed%%$'\t'*}"
513
+ phase_tok="${parsed#*$'\t'}"
514
+
515
+ case "$kind" in
516
+ continue)
517
+ if ! phase_stored_ok; then
518
+ emit fail identity 1
519
+ exit 2
520
+ fi
521
+ append_or_spent fail not-run "$phase_flag" 1 continue not-run
522
+ emit fail not-run 1
523
+ exit 2
524
+ ;;
525
+ goto)
526
+ append_or_spent goto ok "$phase_tok" 1
527
+ printf 'SOLEUR_RETRY_BUDGET_RULING verb=goto reason=ok phase=%s\n' "$phase_tok"
528
+ exit 0
529
+ ;;
530
+ fail)
531
+ if ! phase_stored_ok; then
532
+ emit fail identity 1
533
+ exit 2
534
+ fi
535
+ append_or_spent fail fail "$phase_flag" 1
536
+ emit fail fail 1
537
+ exit 2
538
+ ;;
539
+ empty)
540
+ if ! phase_stored_ok; then
541
+ emit fail identity 1
542
+ exit 2
543
+ fi
544
+ append_or_spent fail empty "$phase_flag" 1
545
+ emit fail empty 1
546
+ exit 2
547
+ ;;
548
+ *)
549
+ if ! phase_stored_ok; then
550
+ emit fail identity 1
551
+ exit 2
552
+ fi
553
+ append_or_spent fail unparseable "$phase_flag" 1
554
+ emit fail unparseable 1
555
+ exit 2
556
+ ;;
557
+ esac