@deftai/directive-content 0.55.1 → 0.56.0

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.
Files changed (220) hide show
  1. package/.githooks/pre-commit +143 -0
  2. package/.githooks/pre-push +121 -0
  3. package/QUICK-START.md +13 -3
  4. package/Taskfile.yml +934 -0
  5. package/UPGRADING.md +82 -11
  6. package/events/README.md +3 -3
  7. package/package.json +5 -4
  8. package/packs/skills/skills-pack-0.1.json +22 -22
  9. package/scripts/_agents_md.py +494 -0
  10. package/scripts/_cache_fetch.py +635 -0
  11. package/scripts/_cache_quota.py +529 -0
  12. package/scripts/_cache_refresh.py +163 -0
  13. package/scripts/_cache_validate.py +209 -0
  14. package/scripts/_content_root.py +42 -0
  15. package/scripts/_doctor_state.py +277 -0
  16. package/scripts/_event_detect.py +305 -0
  17. package/scripts/_events.py +514 -0
  18. package/scripts/_lifecycle_hygiene.py +568 -0
  19. package/scripts/_pathspec.py +91 -0
  20. package/scripts/_policy_show_cli.py +266 -0
  21. package/scripts/_precutover.py +92 -0
  22. package/scripts/_project_context.py +224 -0
  23. package/scripts/_project_definition_io.py +164 -0
  24. package/scripts/_relocate_snapshot.py +209 -0
  25. package/scripts/_relocate_states.py +343 -0
  26. package/scripts/_resolve_preflight_path.py +152 -0
  27. package/scripts/_safe_subprocess.py +167 -0
  28. package/scripts/_session_start_hook.py +205 -0
  29. package/scripts/_sor_gate_diff.py +365 -0
  30. package/scripts/_stdio_utf8.py +59 -0
  31. package/scripts/_triage_bootstrap_gitignore.py +904 -0
  32. package/scripts/_triage_classify_cli.py +122 -0
  33. package/scripts/_triage_queue_cli.py +625 -0
  34. package/scripts/_triage_scope_cli.py +343 -0
  35. package/scripts/_triage_scope_drift_cli.py +121 -0
  36. package/scripts/_triage_scope_ignores.py +286 -0
  37. package/scripts/_triage_scope_milestone.py +432 -0
  38. package/scripts/_triage_scope_mutations.py +337 -0
  39. package/scripts/_triage_scope_renderers.py +207 -0
  40. package/scripts/_triage_smoketest_stages.py +674 -0
  41. package/scripts/_triage_subscribe_cli.py +140 -0
  42. package/scripts/_triage_welcome_cli.py +421 -0
  43. package/scripts/_vbrief_build.py +239 -0
  44. package/scripts/_vbrief_fidelity.py +479 -0
  45. package/scripts/_vbrief_legacy.py +589 -0
  46. package/scripts/_vbrief_reconciliation.py +883 -0
  47. package/scripts/_vbrief_routing.py +277 -0
  48. package/scripts/_vbrief_safety.py +778 -0
  49. package/scripts/_vbrief_sources.py +312 -0
  50. package/scripts/_vbrief_speckit.py +262 -0
  51. package/scripts/_vbrief_story_quality.py +353 -0
  52. package/scripts/_vbrief_validation.py +299 -0
  53. package/scripts/build_dist.py +412 -0
  54. package/scripts/cache.py +1078 -0
  55. package/scripts/cache_scanner.py +745 -0
  56. package/scripts/candidates_log.py +432 -0
  57. package/scripts/capacity_backfill.py +680 -0
  58. package/scripts/capacity_show.py +653 -0
  59. package/scripts/ci_local.py +689 -0
  60. package/scripts/code_structure_validate.py +765 -0
  61. package/scripts/codebase_default_extractor.py +495 -0
  62. package/scripts/codebase_map.py +304 -0
  63. package/scripts/codebase_map_fresh.py +104 -0
  64. package/scripts/codebase_projection_registry.py +94 -0
  65. package/scripts/codebase_provider.py +582 -0
  66. package/scripts/doctor.py +2257 -0
  67. package/scripts/framework_commands.py +505 -0
  68. package/scripts/gh_rest.py +882 -0
  69. package/scripts/github_auth_modes.py +437 -0
  70. package/scripts/github_body.py +292 -0
  71. package/scripts/ip_risk.py +531 -0
  72. package/scripts/issue_emit.py +670 -0
  73. package/scripts/issue_ingest.py +1064 -0
  74. package/scripts/migrate_preflight.py +418 -0
  75. package/scripts/migrate_vbrief.py +2677 -0
  76. package/scripts/monitor_pr.py +401 -0
  77. package/scripts/pack_migrate_lessons.py +336 -0
  78. package/scripts/pack_migrate_patterns.py +254 -0
  79. package/scripts/pack_migrate_rules.py +350 -0
  80. package/scripts/pack_migrate_skills.py +423 -0
  81. package/scripts/pack_migrate_strategies.py +311 -0
  82. package/scripts/pack_migrate_swarm_spec.py +250 -0
  83. package/scripts/pack_render.py +434 -0
  84. package/scripts/packs_slice.py +712 -0
  85. package/scripts/platform_capabilities.py +336 -0
  86. package/scripts/policy.py +2826 -0
  87. package/scripts/policy_set.py +324 -0
  88. package/scripts/pr_check_closing_keywords.py +524 -0
  89. package/scripts/pr_check_protected_issues.py +267 -0
  90. package/scripts/pr_merge_readiness.py +1004 -0
  91. package/scripts/pr_wait_mergeable.py +669 -0
  92. package/scripts/prd_render.py +159 -0
  93. package/scripts/preflight_architecture_sor.py +974 -0
  94. package/scripts/preflight_branch.py +289 -0
  95. package/scripts/preflight_cache.py +974 -0
  96. package/scripts/preflight_gh.py +721 -0
  97. package/scripts/preflight_implementation.py +272 -0
  98. package/scripts/preflight_story_start.py +838 -0
  99. package/scripts/preflight_wip_cap.py +149 -0
  100. package/scripts/probe_session.py +545 -0
  101. package/scripts/project_render.py +293 -0
  102. package/scripts/quarantine_ext.py +237 -0
  103. package/scripts/reconcile_issues.py +1442 -0
  104. package/scripts/refresh-path.ps1 +107 -0
  105. package/scripts/release.py +2030 -0
  106. package/scripts/release_e2e.py +1011 -0
  107. package/scripts/release_publish.py +486 -0
  108. package/scripts/release_rollback.py +980 -0
  109. package/scripts/relocate.py +1034 -0
  110. package/scripts/resolve_changelog_unreleased.py +667 -0
  111. package/scripts/resolve_version.py +490 -0
  112. package/scripts/resume_conditions.py +706 -0
  113. package/scripts/ritual_sentinel.py +609 -0
  114. package/scripts/roadmap_render.py +635 -0
  115. package/scripts/rule_ownership_lint.py +325 -0
  116. package/scripts/scm.py +591 -0
  117. package/scripts/scope_audit_log.py +387 -0
  118. package/scripts/scope_decompose.py +654 -0
  119. package/scripts/scope_demote.py +509 -0
  120. package/scripts/scope_lifecycle.py +1126 -0
  121. package/scripts/scope_undo.py +772 -0
  122. package/scripts/session_start.py +406 -0
  123. package/scripts/setup_ghx.py +339 -0
  124. package/scripts/setup_windows.ps1 +220 -0
  125. package/scripts/slice_audit.py +585 -0
  126. package/scripts/slice_record.py +530 -0
  127. package/scripts/slice_record_existing.py +692 -0
  128. package/scripts/slug_normalize.py +178 -0
  129. package/scripts/spec_render.py +477 -0
  130. package/scripts/spec_validate.py +238 -0
  131. package/scripts/subagent_monitor.py +658 -0
  132. package/scripts/swarm_complete_cohort.py +644 -0
  133. package/scripts/swarm_launch.py +1206 -0
  134. package/scripts/swarm_readiness.py +554 -0
  135. package/scripts/swarm_verify_review_clean.py +438 -0
  136. package/scripts/swarm_worktrees.py +497 -0
  137. package/scripts/toolchain-check.py +52 -0
  138. package/scripts/triage_actions.py +871 -0
  139. package/scripts/triage_bootstrap.py +1153 -0
  140. package/scripts/triage_bulk.py +630 -0
  141. package/scripts/triage_classify.py +932 -0
  142. package/scripts/triage_help.py +1685 -0
  143. package/scripts/triage_queue.py +1944 -0
  144. package/scripts/triage_reconcile.py +581 -0
  145. package/scripts/triage_refresh.py +643 -0
  146. package/scripts/triage_scope.py +999 -0
  147. package/scripts/triage_scope_drift.py +575 -0
  148. package/scripts/triage_smoketest.py +396 -0
  149. package/scripts/triage_subscribe.py +399 -0
  150. package/scripts/triage_summary.py +1011 -0
  151. package/scripts/triage_welcome.py +1178 -0
  152. package/scripts/ts_check_lane.py +86 -0
  153. package/scripts/validate-links.py +64 -0
  154. package/scripts/validate_strategy_output.py +212 -0
  155. package/scripts/vbrief_activate.py +228 -0
  156. package/scripts/vbrief_migrate_conformance.py +368 -0
  157. package/scripts/vbrief_reconcile_graph.py +306 -0
  158. package/scripts/vbrief_reconcile_labels.py +460 -0
  159. package/scripts/vbrief_reconcile_umbrellas.py +741 -0
  160. package/scripts/vbrief_validate.py +1195 -0
  161. package/scripts/verify-stubs.py +61 -0
  162. package/scripts/verify_capacity.py +160 -0
  163. package/scripts/verify_encoding.py +699 -0
  164. package/scripts/verify_hooks_installed.py +206 -0
  165. package/scripts/verify_investigation.py +360 -0
  166. package/scripts/verify_judgment_gates.py +827 -0
  167. package/scripts/verify_no_task_runtime.py +171 -0
  168. package/scripts/verify_scm_boundary.py +509 -0
  169. package/scripts/verify_session_ritual.py +389 -0
  170. package/scripts/verify_tools.py +426 -0
  171. package/scripts/verify_vbrief_conformance.py +478 -0
  172. package/skills/deft-directive-swarm/SKILL.md +7 -26
  173. package/skills/deft-directive-sync/SKILL.md +1 -1
  174. package/tasks/architecture.yml +13 -0
  175. package/tasks/cache.yml +69 -0
  176. package/tasks/capacity.yml +38 -0
  177. package/tasks/change.yml +46 -0
  178. package/tasks/changelog.yml +24 -0
  179. package/tasks/ci.yml +49 -0
  180. package/tasks/codebase.yml +47 -0
  181. package/tasks/commit.yml +30 -0
  182. package/tasks/core.yml +126 -0
  183. package/tasks/deployments.yml +54 -0
  184. package/tasks/framework.yml +74 -0
  185. package/tasks/install.yml +60 -0
  186. package/tasks/issue.yml +50 -0
  187. package/tasks/migrate.yml +73 -0
  188. package/tasks/packs.yml +92 -0
  189. package/tasks/policy.yml +75 -0
  190. package/tasks/pr.yml +89 -0
  191. package/tasks/prd.yml +39 -0
  192. package/tasks/project.yml +27 -0
  193. package/tasks/reconcile.yml +32 -0
  194. package/tasks/relocate.yml +56 -0
  195. package/tasks/roadmap.yml +28 -0
  196. package/tasks/scm.yml +126 -0
  197. package/tasks/scope-undo.yml +36 -0
  198. package/tasks/scope.yml +141 -0
  199. package/tasks/session.yml +19 -0
  200. package/tasks/setup.yml +37 -0
  201. package/tasks/slice.yml +69 -0
  202. package/tasks/spec.yml +41 -0
  203. package/tasks/swarm.yml +85 -0
  204. package/tasks/toolchain.yml +13 -0
  205. package/tasks/triage-actions.yml +94 -0
  206. package/tasks/triage-bootstrap.yml +43 -0
  207. package/tasks/triage-bulk.yml +75 -0
  208. package/tasks/triage-classify.yml +30 -0
  209. package/tasks/triage-queue.yml +50 -0
  210. package/tasks/triage-reconcile.yml +29 -0
  211. package/tasks/triage-scope-drift.yml +29 -0
  212. package/tasks/triage-scope.yml +31 -0
  213. package/tasks/triage-smoketest.yml +33 -0
  214. package/tasks/triage-subscribe.yml +36 -0
  215. package/tasks/triage-summary.yml +29 -0
  216. package/tasks/triage-welcome.yml +32 -0
  217. package/tasks/ts.yml +328 -0
  218. package/tasks/vbrief.yml +206 -0
  219. package/tasks/verify.yml +292 -0
  220. package/templates/agents-entry.md +2 -2
@@ -0,0 +1,530 @@
1
+ """slice_record.py -- writer + reader for ``vbrief/.eval/slices.jsonl`` (#1132 / D13 of #1119).
2
+
3
+ Slicing skills (``deft-directive-gh-slice``, ``deft-directive-gh-arch``,
4
+ the slice phase of ``deft-directive-refinement``) call
5
+ :func:`write_slice` at slice-completion to record a durable cohort entry
6
+ sibling to the gitignored ``vbrief/.eval/candidates.jsonl`` (#845 Story
7
+ 2). Unlike ``candidates.jsonl`` (per-operator, gitignored)
8
+ ``slices.jsonl`` is **tracked in git** (see ``vbrief/.eval/README.md``
9
+ tracking-policy table) because cohort records are team-shared: a fresh
10
+ contributor needs to see prior cohort outputs to detect orphans and
11
+ avoid re-slicing the same scope.
12
+
13
+ Public surface
14
+ --------------
15
+
16
+ * :func:`write_slice` -- atomic, idempotent append. Re-writes with the
17
+ same ``slice_id`` are no-ops (retry-safe). Returns the persisted
18
+ ``slice_id``.
19
+ * :func:`read_all` -- yield every well-formed record. Tolerant of
20
+ malformed lines (logs a warning, skips them).
21
+ * :func:`find_by_slice_id` / :func:`find_by_umbrella` -- targeted reads.
22
+ * :func:`new_slice_id` -- mint a fresh UUID4 for a new cohort.
23
+
24
+ Concurrency
25
+ -----------
26
+
27
+ Mirrors :mod:`candidates_log` (#845 Story 2):
28
+
29
+ * Cross-process safety via a sidecar ``slices.jsonl.lock`` file held
30
+ with ``msvcrt.locking`` on Windows / ``fcntl.flock`` on POSIX.
31
+ * In-process thread safety via a module-level ``threading.Lock``.
32
+
33
+ Validation
34
+ ----------
35
+
36
+ Every dict passed to :func:`write_slice` is validated against the
37
+ constraints in ``vbrief/schemas/slices.schema.json``. The validator is
38
+ hand-rolled so this module has no third-party dependency footprint --
39
+ the schema file remains the canonical reference. Validation errors are
40
+ raised as :class:`SliceRecordError` BEFORE any bytes hit disk.
41
+
42
+ Tracking policy
43
+ ---------------
44
+
45
+ ``vbrief/.eval/slices.jsonl`` is **tracked** in git (see
46
+ ``vbrief/.eval/README.md`` Tracking policy table for the full rationale
47
+ + the ``merge=union`` rebase ergonomic in ``.gitattributes``).
48
+ """
49
+
50
+ from __future__ import annotations
51
+
52
+ import json
53
+ import logging
54
+ import os
55
+ import re
56
+ import sys
57
+ import threading
58
+ import time
59
+ import uuid
60
+ from collections.abc import Iterable, Iterator
61
+ from contextlib import contextmanager, suppress
62
+ from datetime import UTC, datetime
63
+ from pathlib import Path
64
+ from typing import Any
65
+
66
+ LOG = logging.getLogger(__name__)
67
+
68
+ # Canonical default storage location resolved relative to the repo root
69
+ # (mirrors :mod:`candidates_log`).
70
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
71
+
72
+ from _content_root import content_root # noqa: E402
73
+
74
+ REPO_ROOT = Path(__file__).resolve().parent.parent
75
+ # The .eval/ runtime log is a repo-dev sibling that stays at the root vbrief/;
76
+ # only the shipped schemas moved under content/ (#1875 C1 dual-context).
77
+ DEFAULT_LOG_PATH = REPO_ROOT / "vbrief" / ".eval" / "slices.jsonl"
78
+ SCHEMA_PATH = content_root(REPO_ROOT) / "vbrief" / "schemas" / "slices.schema.json"
79
+
80
+ # Frozen enum mirrored from slices.schema.json. Keep in lockstep with the
81
+ # schema file -- bumping the schema's enum requires a follow-up child
82
+ # (additive only per the schema's frozen-interface preamble).
83
+ _VALID_EXPECTED_CLOSE_SIGNALS: frozenset[str] = frozenset(
84
+ {
85
+ "all-children-merged",
86
+ "wave-1-merged",
87
+ "manual",
88
+ }
89
+ )
90
+
91
+ _REQUIRED_FIELDS: tuple[str, ...] = (
92
+ "slice_id",
93
+ "umbrella",
94
+ "umbrella_url",
95
+ "sliced_at",
96
+ "actor",
97
+ "children",
98
+ "expected_close_signal",
99
+ )
100
+ _OPTIONAL_FIELDS: tuple[str, ...] = ("notes",)
101
+ _ALLOWED_FIELDS: frozenset[str] = frozenset(_REQUIRED_FIELDS + _OPTIONAL_FIELDS)
102
+ _CHILD_REQUIRED_FIELDS: tuple[str, ...] = ("n", "url", "wave", "role")
103
+ _CHILD_ALLOWED_FIELDS: frozenset[str] = frozenset(_CHILD_REQUIRED_FIELDS)
104
+
105
+ _UUID_RE = re.compile(
106
+ r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
107
+ )
108
+ # UTC-only on purpose: ``slices.jsonl`` is read by D11's queue ranking +
109
+ # D13's stalled-cohort surface, both of which compare ``sliced_at`` to
110
+ # ``datetime.now(UTC)`` via simple ISO-8601 lexicographic / parsed-utc
111
+ # comparison. A non-UTC offset would silently invert the chronological
112
+ # order (same failure mode as Greptile #876 P1 on candidates_log).
113
+ _ISO8601_RE = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$")
114
+
115
+ _thread_lock = threading.Lock()
116
+
117
+
118
+ class SliceRecordError(ValueError):
119
+ """Raised when a record passed to :func:`write_slice` fails validation."""
120
+
121
+
122
+ def _validate_child(child: Any, index: int) -> None:
123
+ if not isinstance(child, dict):
124
+ raise SliceRecordError(
125
+ f"children[{index}] must be a dict, got {type(child).__name__}"
126
+ )
127
+ missing = [f for f in _CHILD_REQUIRED_FIELDS if f not in child]
128
+ if missing:
129
+ raise SliceRecordError(
130
+ f"children[{index}] missing required field(s): {missing}"
131
+ )
132
+ extras = sorted(set(child.keys()) - _CHILD_ALLOWED_FIELDS)
133
+ if extras:
134
+ raise SliceRecordError(
135
+ f"children[{index}] has unknown field(s): {extras}"
136
+ )
137
+ n = child["n"]
138
+ if not isinstance(n, int) or isinstance(n, bool) or n < 1:
139
+ raise SliceRecordError(
140
+ f"children[{index}].n must be a positive int, got {n!r}"
141
+ )
142
+ url = child["url"]
143
+ if not isinstance(url, str) or not url:
144
+ raise SliceRecordError(
145
+ f"children[{index}].url must be a non-empty string, got {url!r}"
146
+ )
147
+ wave = child["wave"]
148
+ if not isinstance(wave, int) or isinstance(wave, bool) or wave < 1:
149
+ raise SliceRecordError(
150
+ f"children[{index}].wave must be a positive int, got {wave!r}"
151
+ )
152
+ role = child["role"]
153
+ if not isinstance(role, str) or not role:
154
+ raise SliceRecordError(
155
+ f"children[{index}].role must be a non-empty string, got {role!r}"
156
+ )
157
+
158
+
159
+ def _validate_record(record: Any) -> None:
160
+ """Hand-rolled mirror of ``vbrief/schemas/slices.schema.json``.
161
+
162
+ Raises :class:`SliceRecordError` with a human-readable message on the
163
+ first violation encountered. Order-of-checks matches the schema so
164
+ the error message cites the most upstream violation.
165
+ """
166
+ if not isinstance(record, dict):
167
+ raise SliceRecordError(
168
+ f"record must be a dict, got {type(record).__name__}"
169
+ )
170
+
171
+ missing = [f for f in _REQUIRED_FIELDS if f not in record]
172
+ if missing:
173
+ raise SliceRecordError(
174
+ f"record missing required field(s): {missing}"
175
+ )
176
+
177
+ extras = sorted(set(record.keys()) - _ALLOWED_FIELDS)
178
+ if extras:
179
+ raise SliceRecordError(f"record has unknown field(s): {extras}")
180
+
181
+ slice_id = record["slice_id"]
182
+ if not isinstance(slice_id, str) or not _UUID_RE.match(slice_id):
183
+ raise SliceRecordError(
184
+ f"slice_id must be a UUID string, got {slice_id!r}"
185
+ )
186
+
187
+ umbrella = record["umbrella"]
188
+ if (
189
+ not isinstance(umbrella, int)
190
+ or isinstance(umbrella, bool)
191
+ or umbrella < 1
192
+ ):
193
+ raise SliceRecordError(
194
+ f"umbrella must be a positive int, got {umbrella!r}"
195
+ )
196
+
197
+ umbrella_url = record["umbrella_url"]
198
+ if not isinstance(umbrella_url, str) or not umbrella_url:
199
+ raise SliceRecordError(
200
+ f"umbrella_url must be a non-empty string, got {umbrella_url!r}"
201
+ )
202
+
203
+ sliced_at = record["sliced_at"]
204
+ if not isinstance(sliced_at, str) or not _ISO8601_RE.match(sliced_at):
205
+ raise SliceRecordError(
206
+ "sliced_at must be ISO-8601 UTC with Z suffix "
207
+ f"(e.g. 2026-05-13T18:00:00Z), got {sliced_at!r}"
208
+ )
209
+
210
+ actor = record["actor"]
211
+ if not isinstance(actor, str) or not actor:
212
+ raise SliceRecordError(
213
+ f"actor must be a non-empty string, got {actor!r}"
214
+ )
215
+
216
+ children = record["children"]
217
+ if not isinstance(children, list) or not children:
218
+ raise SliceRecordError(
219
+ "children must be a non-empty list of child records"
220
+ )
221
+ for i, child in enumerate(children):
222
+ _validate_child(child, i)
223
+
224
+ expected = record["expected_close_signal"]
225
+ if expected not in _VALID_EXPECTED_CLOSE_SIGNALS:
226
+ raise SliceRecordError(
227
+ f"expected_close_signal must be one of "
228
+ f"{sorted(_VALID_EXPECTED_CLOSE_SIGNALS)}, got {expected!r}"
229
+ )
230
+
231
+ if "notes" in record and not isinstance(record["notes"], str):
232
+ raise SliceRecordError(
233
+ f"notes must be a string, got {type(record['notes']).__name__}"
234
+ )
235
+
236
+
237
+ @contextmanager
238
+ def _append_lock(log_path: Path) -> Iterator[None]:
239
+ """Serialise appenders across threads AND processes.
240
+
241
+ Sibling implementation of :func:`candidates_log._append_lock` --
242
+ sidecar ``<log>.lock`` byte-range exclusive lock.
243
+
244
+ Also exported as :func:`append_lock` for callers (e.g.
245
+ :mod:`slice_record_existing` per #1231) that need to wrap a
246
+ read-decide-write critical section -- specifically the duplicate
247
+ detection + :func:`write_slice_unlocked` pair -- under the SAME
248
+ lock so concurrent invocations cannot both observe "no duplicate"
249
+ before either appends. The lock is NOT reentrant (the underlying
250
+ ``threading.Lock`` + ``msvcrt.locking`` / ``fcntl.flock`` would
251
+ deadlock on re-entry); callers wrapping a critical section MUST
252
+ use :func:`write_slice_unlocked` rather than :func:`write_slice`
253
+ while holding the lock.
254
+ """
255
+ lock_path = log_path.parent / (log_path.name + ".lock")
256
+ lock_path.parent.mkdir(parents=True, exist_ok=True)
257
+ with _thread_lock:
258
+ try:
259
+ with open(lock_path, "a+b") as fh:
260
+ if not lock_path.stat().st_size:
261
+ fh.write(b"\0")
262
+ fh.flush()
263
+ fh.seek(0)
264
+ if sys.platform == "win32":
265
+ import msvcrt
266
+
267
+ acquired = False
268
+ deadline = time.monotonic() + 30.0
269
+ while True:
270
+ try:
271
+ msvcrt.locking(fh.fileno(), msvcrt.LK_NBLCK, 1)
272
+ acquired = True
273
+ break
274
+ except OSError:
275
+ if time.monotonic() > deadline:
276
+ raise
277
+ time.sleep(0.02)
278
+ try:
279
+ yield
280
+ finally:
281
+ if acquired:
282
+ fh.seek(0)
283
+ with suppress(OSError):
284
+ msvcrt.locking(fh.fileno(), msvcrt.LK_UNLCK, 1)
285
+ else:
286
+ import fcntl
287
+
288
+ fcntl.flock(fh.fileno(), fcntl.LOCK_EX)
289
+ try:
290
+ yield
291
+ finally:
292
+ fcntl.flock(fh.fileno(), fcntl.LOCK_UN)
293
+ finally:
294
+ # Remove the sidecar ``<log>.lock`` so a clean append never leaves
295
+ # an untracked lock file behind (#1311 discipline). The handle is
296
+ # closed by the `with open(...)` block above BEFORE this unlink
297
+ # (Windows refuses to delete an open file); held under
298
+ # ``_thread_lock`` so the unlink cannot race an in-process
299
+ # re-acquire. Best-effort across processes.
300
+ with suppress(OSError):
301
+ lock_path.unlink()
302
+
303
+
304
+ def _resolve_path(path: Path | str | None) -> Path:
305
+ return Path(path) if path is not None else DEFAULT_LOG_PATH
306
+
307
+
308
+ def new_slice_id() -> str:
309
+ """Return a fresh UUID4 string for use as a :attr:`slice_id`.
310
+
311
+ Provided so callers (slicing skills + tests) do not have to pull in
312
+ :mod:`uuid` directly and so a future swap to UUID7 (time-ordered) is
313
+ a single-file change.
314
+ """
315
+ return str(uuid.uuid4())
316
+
317
+
318
+ def now_iso() -> str:
319
+ """Return the current UTC time in canonical ISO-8601 form with ``Z`` suffix."""
320
+ return datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
321
+
322
+
323
+ def _existing_slice_ids(log_path: Path) -> set[str]:
324
+ """Return slice_ids already persisted (used for retry-dedup).
325
+
326
+ Tolerant of malformed lines -- mirrors :func:`read_all`'s warn-and-skip.
327
+ """
328
+ if not log_path.exists():
329
+ return set()
330
+ seen: set[str] = set()
331
+ with open(log_path, encoding="utf-8") as fh:
332
+ for lineno, raw in enumerate(fh, start=1):
333
+ stripped = raw.strip()
334
+ if not stripped:
335
+ continue
336
+ try:
337
+ obj = json.loads(stripped)
338
+ except json.JSONDecodeError as exc:
339
+ LOG.warning(
340
+ "slices.jsonl: skipping malformed JSON on line %d: %s",
341
+ lineno,
342
+ exc,
343
+ )
344
+ continue
345
+ if isinstance(obj, dict):
346
+ sid = obj.get("slice_id")
347
+ if isinstance(sid, str):
348
+ seen.add(sid)
349
+ return seen
350
+
351
+
352
+ def write_slice(
353
+ umbrella: int,
354
+ children: Iterable[dict[str, Any]],
355
+ *,
356
+ umbrella_url: str,
357
+ actor: str,
358
+ expected_close_signal: str = "all-children-merged",
359
+ slice_id: str | None = None,
360
+ sliced_at: str | None = None,
361
+ notes: str | None = None,
362
+ path: Path | str | None = None,
363
+ ) -> str:
364
+ """Validate and atomically append a cohort record to ``slices.jsonl``.
365
+
366
+ Args:
367
+ umbrella: Umbrella issue number.
368
+ children: Iterable of child dicts. Each child must carry
369
+ ``{n, url, wave, role}`` per ``vbrief/schemas/slices.schema.json``.
370
+ umbrella_url: Full URL of the umbrella issue.
371
+ actor: Slicing actor identity (e.g. ``"skill:gh-slice"``).
372
+ expected_close_signal: One of ``all-children-merged`` (default),
373
+ ``wave-1-merged``, ``manual``.
374
+ slice_id: Optional explicit slice_id; minted if omitted. Pass an
375
+ existing slice_id to make a retry idempotent.
376
+ sliced_at: Optional ISO-8601 UTC timestamp; current time if omitted.
377
+ notes: Optional free-form rationale.
378
+ path: Optional log file path override (test hook).
379
+
380
+ Returns:
381
+ The persisted ``slice_id`` (the supplied one if provided,
382
+ otherwise the newly-minted UUID). On idempotent no-op (the
383
+ ``slice_id`` is already present in the log), the same id is
384
+ returned without re-writing.
385
+
386
+ Raises:
387
+ SliceRecordError: if any field fails validation. No bytes are
388
+ written to disk in this case.
389
+ """
390
+ resolved_id = slice_id or new_slice_id()
391
+ record: dict[str, Any] = {
392
+ "slice_id": resolved_id,
393
+ "umbrella": umbrella,
394
+ "umbrella_url": umbrella_url,
395
+ "sliced_at": sliced_at or now_iso(),
396
+ "actor": actor,
397
+ "children": [dict(c) for c in children],
398
+ "expected_close_signal": expected_close_signal,
399
+ }
400
+ if notes is not None:
401
+ record["notes"] = notes
402
+
403
+ log_path = _resolve_path(path)
404
+ with _append_lock(log_path):
405
+ return write_slice_unlocked(record=record, path=log_path)
406
+
407
+
408
+ def write_slice_unlocked(
409
+ *,
410
+ record: dict[str, Any],
411
+ path: Path | str | None = None,
412
+ ) -> str:
413
+ """Validate + append ``record`` without acquiring the sidecar lock.
414
+
415
+ Companion to :func:`write_slice` for callers that wrap their own
416
+ read-decide-write critical section under :func:`append_lock`
417
+ directly (see :mod:`slice_record_existing` per #1231 -- the
418
+ duplicate-detection + append pair must run under one lock for
419
+ atomic idempotency).
420
+
421
+ Behaviour mirrors :func:`write_slice`:
422
+
423
+ * Validates ``record`` against the schema; raises
424
+ :class:`SliceRecordError` before any bytes hit disk.
425
+ * Idempotent retry: if ``record['slice_id']`` is already present
426
+ in the log, returns it without re-writing.
427
+ * Otherwise appends one JSONL line, fsync'd.
428
+
429
+ The caller is responsible for holding :func:`append_lock` for the
430
+ same ``path``. Use :func:`write_slice` instead when you do NOT
431
+ need to compose with another read under the same lock.
432
+ """
433
+ _validate_record(record)
434
+ log_path = _resolve_path(path)
435
+ log_path.parent.mkdir(parents=True, exist_ok=True)
436
+ resolved_id = record["slice_id"]
437
+ line = json.dumps(record, sort_keys=True, ensure_ascii=False)
438
+ # Re-check under the lock so a concurrent appender that wrote the
439
+ # same slice_id between the validation pass and the append cannot
440
+ # produce a duplicate.
441
+ existing = _existing_slice_ids(log_path)
442
+ if resolved_id in existing:
443
+ LOG.info(
444
+ "slices.jsonl: slice_id %s already present; write_slice is a no-op",
445
+ resolved_id,
446
+ )
447
+ return resolved_id
448
+ with open(log_path, "a", encoding="utf-8", newline="") as fh:
449
+ fh.write(line + "\n")
450
+ fh.flush()
451
+ os.fsync(fh.fileno())
452
+ return resolved_id
453
+
454
+
455
+ def read_all(*, path: Path | str | None = None) -> list[dict[str, Any]]:
456
+ """Return every well-formed slice record in insertion order.
457
+
458
+ Args:
459
+ path: Optional log path override (test hook).
460
+
461
+ Returns:
462
+ A list of dicts -- never None. An empty list is returned both
463
+ when the file does not exist and when every line is malformed.
464
+ """
465
+ log_path = _resolve_path(path)
466
+ if not log_path.exists():
467
+ return []
468
+ out: list[dict[str, Any]] = []
469
+ with open(log_path, encoding="utf-8") as fh:
470
+ for lineno, raw in enumerate(fh, start=1):
471
+ stripped = raw.strip()
472
+ if not stripped:
473
+ continue
474
+ try:
475
+ obj = json.loads(stripped)
476
+ except json.JSONDecodeError as exc:
477
+ LOG.warning(
478
+ "slices.jsonl: skipping malformed JSON on line %d: %s",
479
+ lineno,
480
+ exc,
481
+ )
482
+ continue
483
+ if not isinstance(obj, dict):
484
+ LOG.warning(
485
+ "slices.jsonl: skipping non-object entry on line %d (got %s)",
486
+ lineno,
487
+ type(obj).__name__,
488
+ )
489
+ continue
490
+ out.append(obj)
491
+ return out
492
+
493
+
494
+ def find_by_slice_id(
495
+ slice_id: str, *, path: Path | str | None = None
496
+ ) -> dict[str, Any] | None:
497
+ """Return the slice record matching ``slice_id`` or ``None``."""
498
+ for record in read_all(path=path):
499
+ if record.get("slice_id") == slice_id:
500
+ return record
501
+ return None
502
+
503
+
504
+ def find_by_umbrella(
505
+ umbrella: int, *, path: Path | str | None = None
506
+ ) -> list[dict[str, Any]]:
507
+ """Return every slice record for ``umbrella`` in insertion order."""
508
+ return [r for r in read_all(path=path) if r.get("umbrella") == umbrella]
509
+
510
+
511
+ # Public alias for the sidecar-file lock. Callers that need to wrap a
512
+ # read-decide-write critical section (`slice_record_existing` per #1231)
513
+ # can import this directly without reaching for the private name; the
514
+ # underscore form is preserved for in-module readability.
515
+ append_lock = _append_lock
516
+
517
+
518
+ __all__ = [
519
+ "DEFAULT_LOG_PATH",
520
+ "SCHEMA_PATH",
521
+ "SliceRecordError",
522
+ "append_lock",
523
+ "find_by_slice_id",
524
+ "find_by_umbrella",
525
+ "new_slice_id",
526
+ "now_iso",
527
+ "read_all",
528
+ "write_slice",
529
+ "write_slice_unlocked",
530
+ ]