prizmkit 1.1.190 → 1.1.192

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 (189) hide show
  1. package/bin/create-prizmkit.js +4 -4
  2. package/bundled/VERSION.json +3 -3
  3. package/bundled/adapters/antigravity/assets/prizmkit-hook.cjs +150 -0
  4. package/bundled/adapters/antigravity/hooks-adapter.js +195 -0
  5. package/bundled/adapters/antigravity/paths.js +14 -0
  6. package/bundled/adapters/antigravity/rules-adapter.js +54 -0
  7. package/bundled/adapters/antigravity/skill-adapter.js +38 -0
  8. package/bundled/adapters/codex/skill-adapter.js +28 -8
  9. package/bundled/adapters/cursor/skill-adapter.js +13 -4
  10. package/bundled/adapters/pi/skill-adapter.js +28 -7
  11. package/bundled/dev-pipeline/.env.example +4 -12
  12. package/bundled/dev-pipeline/README.md +21 -15
  13. package/bundled/dev-pipeline/assets/skill-subagent-integration.md +3 -6
  14. package/bundled/dev-pipeline/prizmkit_runtime/atomic_io.py +48 -0
  15. package/bundled/dev-pipeline/prizmkit_runtime/checkpoint_state.py +66 -23
  16. package/bundled/dev-pipeline/prizmkit_runtime/cli.py +10 -7
  17. package/bundled/dev-pipeline/prizmkit_runtime/commands.py +2 -21
  18. package/bundled/dev-pipeline/prizmkit_runtime/config.py +29 -70
  19. package/bundled/dev-pipeline/prizmkit_runtime/daemon.py +125 -67
  20. package/bundled/dev-pipeline/prizmkit_runtime/gitops.py +89 -442
  21. package/bundled/dev-pipeline/prizmkit_runtime/heartbeat.py +93 -49
  22. package/bundled/dev-pipeline/prizmkit_runtime/interoperability.py +1 -3
  23. package/bundled/dev-pipeline/prizmkit_runtime/paths.py +0 -6
  24. package/bundled/dev-pipeline/prizmkit_runtime/platform_detection.py +49 -18
  25. package/bundled/dev-pipeline/prizmkit_runtime/processes.py +206 -151
  26. package/bundled/dev-pipeline/prizmkit_runtime/reset.py +2 -23
  27. package/bundled/dev-pipeline/prizmkit_runtime/reset_preserve.py +15 -134
  28. package/bundled/dev-pipeline/prizmkit_runtime/results.py +25 -0
  29. package/bundled/dev-pipeline/prizmkit_runtime/runner_bookkeeping.py +68 -83
  30. package/bundled/dev-pipeline/prizmkit_runtime/runner_classification.py +15 -5
  31. package/bundled/dev-pipeline/prizmkit_runtime/runner_models.py +1 -29
  32. package/bundled/dev-pipeline/prizmkit_runtime/runner_prompts.py +0 -2
  33. package/bundled/dev-pipeline/prizmkit_runtime/runner_status.py +7 -2
  34. package/bundled/dev-pipeline/prizmkit_runtime/runners.py +282 -217
  35. package/bundled/dev-pipeline/prizmkit_runtime/runtime_commit.py +124 -122
  36. package/bundled/dev-pipeline/prizmkit_runtime/runtime_helper.py +90 -9
  37. package/bundled/dev-pipeline/prizmkit_runtime/sessions.py +369 -154
  38. package/bundled/dev-pipeline/prizmkit_runtime/status.py +3 -3
  39. package/bundled/dev-pipeline/prizmkit_runtime/stream_progress.py +2780 -0
  40. package/bundled/dev-pipeline/prizmkit_runtime/task_checkout.py +27 -101
  41. package/bundled/dev-pipeline/prizmkit_runtime/test_result.py +58 -4
  42. package/bundled/dev-pipeline/scripts/check-session-status.py +43 -19
  43. package/bundled/dev-pipeline/scripts/continuation.py +6 -4
  44. package/bundled/dev-pipeline/scripts/detect-stuck.py +28 -28
  45. package/bundled/dev-pipeline/scripts/generate-bootstrap-prompt.py +7 -2
  46. package/bundled/dev-pipeline/scripts/generate-bugfix-prompt.py +7 -2
  47. package/bundled/dev-pipeline/scripts/init-bugfix-pipeline.py +10 -7
  48. package/bundled/dev-pipeline/scripts/init-change-artifact.py +8 -2
  49. package/bundled/dev-pipeline/scripts/init-pipeline.py +14 -69
  50. package/bundled/dev-pipeline/scripts/parse-stream-progress.py +31 -2328
  51. package/bundled/dev-pipeline/scripts/patch-completion-notes.py +4 -24
  52. package/bundled/dev-pipeline/scripts/prompt_framework.py +56 -9
  53. package/bundled/dev-pipeline/scripts/status_common.py +591 -0
  54. package/bundled/dev-pipeline/scripts/update-bug-status.py +85 -463
  55. package/bundled/dev-pipeline/scripts/update-checkpoint.py +227 -65
  56. package/bundled/dev-pipeline/scripts/update-feature-status.py +76 -509
  57. package/bundled/dev-pipeline/scripts/utils.py +10 -15
  58. package/bundled/dev-pipeline/templates/bootstrap-tier3.md +4 -4
  59. package/bundled/dev-pipeline/templates/bug-fix-list-schema.json +7 -0
  60. package/bundled/dev-pipeline/templates/bugfix-bootstrap-prompt.md +1 -1
  61. package/bundled/dev-pipeline/templates/sections/artifact-manifest.md +1 -1
  62. package/bundled/dev-pipeline/templates/sections/bugfix-phase-manual-verification.md +1 -1
  63. package/bundled/dev-pipeline/templates/sections/bugfix-phase-review.md +1 -1
  64. package/bundled/dev-pipeline/templates/sections/bugfix-phase-test.md +2 -1
  65. package/bundled/dev-pipeline/templates/sections/checkpoint-system.md +1 -1
  66. package/bundled/dev-pipeline/templates/sections/phase-browser-verification-auto.md +1 -1
  67. package/bundled/dev-pipeline/templates/sections/phase-browser-verification-opencli.md +2 -2
  68. package/bundled/dev-pipeline/templates/sections/phase-browser-verification.md +2 -2
  69. package/bundled/dev-pipeline/templates/sections/phase-prizmkit-test.md +2 -1
  70. package/bundled/dev-pipeline/templates/sections/phase-review-full.md +1 -1
  71. package/bundled/dev-pipeline/templates/sections/phase-specify-plan-full.md +3 -6
  72. package/bundled/dev-pipeline/templates/session-status-schema.json +2 -5
  73. package/bundled/dev-pipeline/tests/_helpers.py +418 -0
  74. package/bundled/dev-pipeline/tests/conftest.py +45 -1
  75. package/bundled/dev-pipeline/tests/live_ai_cli_log_smoke.py +1 -0
  76. package/bundled/dev-pipeline/tests/test_auto_skip.py +4 -69
  77. package/bundled/dev-pipeline/tests/test_checkpoint_state.py +126 -1
  78. package/bundled/dev-pipeline/tests/test_daemon_and_scripts.py +257 -0
  79. package/bundled/dev-pipeline/tests/test_e2e_pipeline_run.py +500 -0
  80. package/bundled/dev-pipeline/tests/test_generate_bootstrap_prompt.py +59 -117
  81. package/bundled/dev-pipeline/tests/test_generate_bugfix_prompt.py +12 -10
  82. package/bundled/dev-pipeline/tests/test_python_runner_parity.py +561 -3094
  83. package/bundled/dev-pipeline/tests/test_recovery_workflow.py +7 -8
  84. package/bundled/dev-pipeline/tests/test_repository_bootstrap.py +0 -1
  85. package/bundled/dev-pipeline/tests/test_reset_modes.py +15 -27
  86. package/bundled/dev-pipeline/tests/test_reset_preserve.py +54 -139
  87. package/bundled/dev-pipeline/tests/test_runner_bookkeeping.py +766 -0
  88. package/bundled/dev-pipeline/tests/test_runner_lifecycle.py +1257 -0
  89. package/bundled/dev-pipeline/tests/test_runtime_helper.py +97 -20
  90. package/bundled/dev-pipeline/tests/test_session_launch.py +1101 -0
  91. package/bundled/dev-pipeline/tests/test_stream_progress.py +585 -0
  92. package/bundled/dev-pipeline/tests/test_unified_cli.py +490 -2006
  93. package/bundled/dev-pipeline/tests/test_utils.py +3 -3
  94. package/bundled/rules/USAGE.md +1 -0
  95. package/bundled/skills/_metadata.json +9 -63
  96. package/bundled/skills/app-planner/SKILL.md +32 -303
  97. package/bundled/skills/app-planner/references/app-design-guide.md +0 -4
  98. package/bundled/skills/app-planner/references/generated-plan-review.md +2 -1
  99. package/bundled/skills/app-planner/references/project-brief-guide.md +14 -0
  100. package/bundled/skills/app-planner/references/project-conventions-discovery.md +4 -6
  101. package/bundled/skills/app-planner/references/project-state-detection.md +2 -3
  102. package/bundled/skills/bug-fix-workflow/SKILL.md +1 -2
  103. package/bundled/skills/bug-fix-workflow/references/bug-diagnosis.md +4 -7
  104. package/bundled/skills/bug-planner/SKILL.md +54 -435
  105. package/bundled/skills/bug-planner/references/generated-plan-review.md +3 -2
  106. package/bundled/skills/bug-planner/references/input-formats.md +32 -0
  107. package/bundled/skills/bugfix-pipeline-launcher/SKILL.md +4 -6
  108. package/bundled/skills/bugfix-pipeline-launcher/references/configuration.md +4 -28
  109. package/bundled/skills/feature-pipeline-launcher/SKILL.md +4 -6
  110. package/bundled/skills/feature-pipeline-launcher/references/configuration.md +4 -28
  111. package/bundled/skills/feature-planner/SKILL.md +60 -439
  112. package/bundled/skills/feature-planner/assets/planning-guide.md +1 -1
  113. package/bundled/skills/feature-planner/references/completeness-review.md +34 -0
  114. package/bundled/skills/feature-planner/references/decomposition-patterns.md +2 -2
  115. package/bundled/skills/feature-planner/references/error-recovery.md +1 -1
  116. package/bundled/skills/feature-planner/references/generated-plan-review.md +3 -2
  117. package/bundled/skills/feature-planner/references/incremental-feature-planning.md +3 -1
  118. package/bundled/skills/feature-workflow/SKILL.md +1 -2
  119. package/bundled/skills/prizmkit/SKILL.md +7 -1
  120. package/bundled/skills/prizmkit-code-review/SKILL.md +14 -6
  121. package/bundled/skills/prizmkit-code-review/references/independent-code-review.md +7 -4
  122. package/bundled/skills/prizmkit-code-review/references/review-report-template.md +16 -0
  123. package/bundled/skills/prizmkit-code-review/scripts/render_review_report.py +58 -20
  124. package/bundled/skills/prizmkit-implement/SKILL.md +1 -1
  125. package/bundled/skills/prizmkit-init/SKILL.md +25 -128
  126. package/bundled/skills/prizmkit-init/assets/project-brief-template.md +3 -3
  127. package/bundled/skills/prizmkit-init/references/config-schema.md +2 -2
  128. package/bundled/skills/prizmkit-plan/SKILL.md +6 -8
  129. package/bundled/skills/prizmkit-plan/references/clarify-guide.md +1 -1
  130. package/bundled/skills/prizmkit-plan/references/independent-plan-review.md +5 -2
  131. package/bundled/skills/prizmkit-plan/references/review-plan-spec-loop.md +6 -5
  132. package/bundled/skills/prizmkit-prizm-docs/SKILL.md +1 -13
  133. package/bundled/skills/prizmkit-prizm-docs/assets/prizm-docs-format.md +1 -1
  134. package/bundled/skills/prizmkit-prizm-docs/references/op-init.md +1 -1
  135. package/bundled/skills/prizmkit-prizm-docs/references/op-update.md +1 -1
  136. package/bundled/skills/prizmkit-retrospective/SKILL.md +6 -4
  137. package/bundled/skills/prizmkit-test/SKILL.md +25 -133
  138. package/bundled/skills/prizmkit-test/references/independent-test-review.md +5 -2
  139. package/bundled/skills/prizmkit-test/references/test-coverage-model.md +6 -0
  140. package/bundled/skills/prizmkit-workflow/SKILL.md +2 -3
  141. package/bundled/skills/prizmkit-workflow/references/workflow-state-protocol.md +4 -3
  142. package/bundled/skills/recovery-workflow/SKILL.md +7 -7
  143. package/bundled/skills/recovery-workflow/evals/evals.json +1 -1
  144. package/bundled/skills/recovery-workflow/references/detection.md +1 -3
  145. package/bundled/skills/recovery-workflow/scripts/detect-recovery-state.py +0 -6
  146. package/bundled/templates/project-memory-template.md +1 -2
  147. package/package.json +1 -1
  148. package/src/ai-cli-launch.js +1 -1
  149. package/src/clean.js +40 -4
  150. package/src/config.js +64 -4
  151. package/src/detect-platform.js +10 -6
  152. package/src/external-skills.js +2 -2
  153. package/src/index.js +1 -0
  154. package/src/manifest.js +1 -1
  155. package/src/platforms.js +6 -2
  156. package/src/prompts.js +11 -3
  157. package/src/scaffold.js +87 -11
  158. package/src/upgrade.js +9 -3
  159. package/bundled/dev-pipeline/SCHEMA_ANALYSIS.md +0 -53
  160. package/bundled/dev-pipeline/scripts/generate-refactor-prompt.py +0 -649
  161. package/bundled/dev-pipeline/scripts/init-refactor-pipeline.py +0 -463
  162. package/bundled/dev-pipeline/scripts/update-refactor-status.py +0 -1512
  163. package/bundled/dev-pipeline/templates/refactor-bootstrap-prompt.md +0 -240
  164. package/bundled/dev-pipeline/templates/refactor-list-schema.json +0 -266
  165. package/bundled/dev-pipeline/templates/sections/refactor-critical-paths.md +0 -15
  166. package/bundled/dev-pipeline/templates/sections/refactor-mission.md +0 -9
  167. package/bundled/dev-pipeline/templates/sections/refactor-phase-commit-report.md +0 -24
  168. package/bundled/dev-pipeline/templates/sections/refactor-phase-implement.md +0 -16
  169. package/bundled/dev-pipeline/templates/sections/refactor-phase-init.md +0 -9
  170. package/bundled/dev-pipeline/templates/sections/refactor-phase-plan.md +0 -22
  171. package/bundled/dev-pipeline/templates/sections/refactor-phase-review.md +0 -19
  172. package/bundled/dev-pipeline/templates/sections/refactor-phase-test.md +0 -24
  173. package/bundled/dev-pipeline/templates/sections/refactor-reminders.md +0 -12
  174. package/bundled/dev-pipeline/templates/sections/refactor-session-context.md +0 -6
  175. package/bundled/dev-pipeline/templates/sections/refactor-task-contract.md +0 -52
  176. package/bundled/dev-pipeline/tests/test_generate_refactor_prompt.py +0 -478
  177. package/bundled/skills/feature-planner/assets/evaluation-guide.md +0 -44
  178. package/bundled/skills/refactor-pipeline-launcher/SKILL.md +0 -219
  179. package/bundled/skills/refactor-pipeline-launcher/references/configuration.md +0 -78
  180. package/bundled/skills/refactor-planner/SKILL.md +0 -467
  181. package/bundled/skills/refactor-planner/assets/planning-guide.md +0 -291
  182. package/bundled/skills/refactor-planner/references/behavior-preservation.md +0 -187
  183. package/bundled/skills/refactor-planner/references/error-recovery.md +0 -45
  184. package/bundled/skills/refactor-planner/references/fast-path.md +0 -59
  185. package/bundled/skills/refactor-planner/references/generated-plan-review.md +0 -107
  186. package/bundled/skills/refactor-planner/references/planning-phases.md +0 -172
  187. package/bundled/skills/refactor-planner/references/refactor-scoping-guide.md +0 -230
  188. package/bundled/skills/refactor-planner/scripts/validate-and-generate-refactor.py +0 -1120
  189. package/bundled/skills/refactor-workflow/SKILL.md +0 -205
@@ -2,7 +2,10 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import ctypes
6
+ import json
5
7
  import os
8
+ import shutil
6
9
  import signal
7
10
  import subprocess
8
11
  import sys
@@ -13,6 +16,69 @@ from contextlib import contextmanager
13
16
  from dataclasses import dataclass, field
14
17
  from pathlib import Path
15
18
 
19
+ from .atomic_io import write_text_atomic
20
+
21
+
22
+ class PidLockError(RuntimeError):
23
+ """Raised when an exclusive PID directory lock is held or cannot be acquired."""
24
+
25
+
26
+ def _lock_owner_pid(lock_path: Path) -> int:
27
+ for _attempt in range(5):
28
+ try:
29
+ data = json.loads((lock_path / "owner.json").read_text(encoding="utf-8"))
30
+ return int(data.get("pid") or 0) if isinstance(data, dict) else 0
31
+ except FileNotFoundError:
32
+ time.sleep(0.01)
33
+ except (OSError, ValueError, json.JSONDecodeError):
34
+ return 0
35
+ return 0
36
+
37
+
38
+ def _remove_stale_pid_lock(lock_path: Path, error_label: str) -> None:
39
+ stale_path = lock_path.with_name(f"{lock_path.name}.stale-{os.getpid()}-{os.urandom(6).hex()}")
40
+ try:
41
+ os.rename(lock_path, stale_path)
42
+ except FileNotFoundError:
43
+ return
44
+ except OSError as exc:
45
+ raise PidLockError(f"Cannot claim stale {error_label} {lock_path}: {exc}") from exc
46
+ shutil.rmtree(stale_path, ignore_errors=True)
47
+
48
+
49
+ @contextmanager
50
+ def exclusive_pid_lock(
51
+ lock_path: Path,
52
+ *,
53
+ busy_message: str | None = None,
54
+ error_label: str = "lock",
55
+ ) -> Iterator[None]:
56
+ """Acquire an exclusive directory lock, reclaiming locks from exited PIDs."""
57
+ lock_path.parent.mkdir(parents=True, exist_ok=True)
58
+ acquired = False
59
+ for _attempt in range(3):
60
+ try:
61
+ lock_path.mkdir()
62
+ acquired = True
63
+ break
64
+ except FileExistsError:
65
+ owner_pid = _lock_owner_pid(lock_path)
66
+ if is_pid_running(owner_pid):
67
+ message = busy_message or f"{error_label} is already held (PID {owner_pid})"
68
+ raise PidLockError(message.format(pid=owner_pid))
69
+ _remove_stale_pid_lock(lock_path, error_label)
70
+ except OSError as exc:
71
+ raise PidLockError(f"Cannot acquire {error_label} {lock_path}: {exc}") from exc
72
+ if not acquired:
73
+ raise PidLockError(f"Cannot acquire {error_label} {lock_path}")
74
+ try:
75
+ (lock_path / "owner.json").write_text(json.dumps({"pid": os.getpid()}) + "\n", encoding="utf-8")
76
+ yield
77
+ finally:
78
+ owner_pid = _lock_owner_pid(lock_path)
79
+ if owner_pid in {0, os.getpid()}:
80
+ shutil.rmtree(lock_path, ignore_errors=True)
81
+
16
82
 
17
83
  @dataclass(frozen=True)
18
84
  class InterruptionDetails:
@@ -98,6 +164,7 @@ class ProcessSpec:
98
164
  pid_file: Path | None = None
99
165
  stdin_text: str | None = None
100
166
  start_new_process_group: bool = True
167
+ detached: bool = False
101
168
 
102
169
 
103
170
  @dataclass(frozen=True)
@@ -153,20 +220,18 @@ def shield_interruption_signals() -> Iterator[None]:
153
220
  signal.signal(signal_number, handler)
154
221
 
155
222
 
156
- def _creationflags_for_platform(start_new_process_group: bool) -> int:
157
- if os.name != "nt" or not start_new_process_group:
223
+ def _creationflags_for_platform(start_new_process_group: bool, *, detached: bool = False) -> int:
224
+ if os.name != "nt":
158
225
  return 0
159
226
  flags = 0
160
- flags |= getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0)
227
+ if start_new_process_group:
228
+ flags |= getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0)
229
+ if detached:
230
+ flags |= getattr(subprocess, "DETACHED_PROCESS", 0)
231
+ flags |= getattr(subprocess, "CREATE_NO_WINDOW", 0)
161
232
  return flags
162
233
 
163
234
 
164
- def _preexec_fn_for_platform(start_new_process_group: bool):
165
- if os.name == "nt" or not start_new_process_group:
166
- return None
167
- return os.setsid
168
-
169
-
170
235
  def _process_env(overrides: Mapping[str, str], unsets: Sequence[str] = ()) -> dict[str, str]:
171
236
  env = dict(os.environ)
172
237
  for key in unsets:
@@ -183,8 +248,9 @@ def launch_process(
183
248
  stdin=subprocess.PIPE,
184
249
  text: bool = True,
185
250
  ) -> ProcessHandle:
186
- """Launch a child process with POSIX group or Windows process flags."""
251
+ """Launch a child process with POSIX session or Windows process flags."""
187
252
  spec.cwd.mkdir(parents=True, exist_ok=True)
253
+ text_kwargs = {"encoding": "utf-8", "errors": "replace"} if text else {}
188
254
  process = subprocess.Popen(
189
255
  [str(part) for part in spec.command],
190
256
  cwd=spec.cwd,
@@ -193,12 +259,12 @@ def launch_process(
193
259
  stdout=stdout,
194
260
  stderr=stderr,
195
261
  text=text,
196
- preexec_fn=_preexec_fn_for_platform(spec.start_new_process_group),
197
- creationflags=_creationflags_for_platform(spec.start_new_process_group),
262
+ start_new_session=(os.name != "nt" and spec.start_new_process_group),
263
+ creationflags=_creationflags_for_platform(spec.start_new_process_group, detached=spec.detached),
264
+ **text_kwargs,
198
265
  )
199
266
  if spec.pid_file is not None:
200
- spec.pid_file.parent.mkdir(parents=True, exist_ok=True)
201
- spec.pid_file.write_text(f"{process.pid}\n", encoding="utf-8")
267
+ write_text_atomic(spec.pid_file, f"{process.pid}\n")
202
268
  return ProcessHandle(pid=process.pid, spec=spec, status="running", popen=process)
203
269
 
204
270
 
@@ -206,96 +272,150 @@ def is_process_running(handle: ProcessHandle) -> bool:
206
272
  """Return true when the process appears alive."""
207
273
  if handle.popen is not None:
208
274
  return handle.popen.poll() is None
209
- if handle.pid is None:
210
- return False
275
+ return is_pid_running(handle.pid)
276
+
277
+
278
+ def _signal_posix_tree(pid: int, sig: int, label: str, details: list[str]) -> None:
211
279
  try:
212
- os.kill(handle.pid, 0)
213
- except OSError:
214
- return False
215
- return True
280
+ os.killpg(pid, sig)
281
+ details.append(f"{label}_process_group")
282
+ except OSError as exc:
283
+ details.append(f"{label}_group_failed={type(exc).__name__}")
284
+ try:
285
+ os.kill(pid, sig)
286
+ details.append(f"{label}_process")
287
+ except OSError as inner:
288
+ details.append(f"{label}_process_failed={type(inner).__name__}")
289
+
290
+
291
+ def _run_windows_taskkill(pid: int, *, force: bool) -> subprocess.CompletedProcess[str]:
292
+ command = ["taskkill", "/PID", str(pid), "/T"]
293
+ if force:
294
+ command.append("/F")
295
+ return subprocess.run(
296
+ command,
297
+ stdout=subprocess.PIPE,
298
+ stderr=subprocess.PIPE,
299
+ text=True,
300
+ encoding="utf-8",
301
+ errors="replace",
302
+ check=False,
303
+ )
216
304
 
217
305
 
218
- def terminate_process_tree(
219
- handle: ProcessHandle,
306
+ def _terminate_tree_impl(
307
+ pid: int,
308
+ *,
220
309
  reason: str,
221
- grace_seconds: float = 10.0,
310
+ grace_seconds: float,
311
+ process: subprocess.Popen[str] | None = None,
222
312
  ) -> ProcessTerminationResult:
223
- """Terminate a process tree without relying on shell runtime logic.
224
-
225
- POSIX uses the process group created at launch. Windows uses taskkill-style
226
- tree termination when available. Both paths are best effort and safe for
227
- tests with short-lived fake processes.
228
- """
229
- pid = handle.pid
230
- if pid is None:
231
- return ProcessTerminationResult(pid=None, reason=reason, terminated=False, forced=False, return_code=None)
232
-
313
+ still_running = (lambda: process.poll() is None) if process is not None else (lambda: is_pid_running(pid))
233
314
  details: list[str] = []
234
315
  forced = False
235
- process = handle.popen
316
+ taskkill_ok = False
317
+ grace = max(0.0, grace_seconds)
236
318
 
237
319
  if os.name == "nt":
238
- completed = subprocess.run(
239
- ["taskkill", "/PID", str(pid), "/T", "/F"],
240
- stdout=subprocess.PIPE,
241
- stderr=subprocess.PIPE,
242
- text=True,
243
- check=False,
244
- )
245
- details.append(f"taskkill_exit={completed.returncode}")
246
- forced = True
320
+ if grace > 0:
321
+ completed = _run_windows_taskkill(pid, force=False)
322
+ details.append(f"taskkill_graceful_exit={completed.returncode}")
323
+ taskkill_ok = completed.returncode == 0
324
+ deadline = time.monotonic() + grace
325
+ while still_running() and time.monotonic() < deadline:
326
+ time.sleep(0.05)
327
+ if still_running():
328
+ forced = True
329
+ completed = _run_windows_taskkill(pid, force=True)
330
+ details.append(f"taskkill_exit={completed.returncode}")
331
+ taskkill_ok = taskkill_ok or (completed.returncode == 0)
332
+ force_deadline = time.monotonic() + min(grace, 2.0)
333
+ while still_running() and time.monotonic() < force_deadline:
334
+ time.sleep(0.05)
335
+ else:
336
+ forced = True
337
+ completed = _run_windows_taskkill(pid, force=True)
338
+ details.append(f"taskkill_exit={completed.returncode}")
339
+ taskkill_ok = completed.returncode == 0
247
340
  else:
248
- try:
249
- os.killpg(pid, signal.SIGTERM)
250
- details.append("sigterm_process_group")
251
- except ProcessLookupError:
252
- pass
253
- except OSError as exc:
254
- details.append(f"sigterm_group_failed={type(exc).__name__}")
255
- try:
256
- os.kill(pid, signal.SIGTERM)
257
- details.append("sigterm_process")
258
- except OSError as inner:
259
- details.append(f"sigterm_process_failed={type(inner).__name__}")
260
-
261
- deadline = time.monotonic() + max(0.0, grace_seconds)
262
- while process is not None and process.poll() is None and time.monotonic() < deadline:
341
+ _signal_posix_tree(pid, signal.SIGTERM, "sigterm", details)
342
+ deadline = time.monotonic() + grace
343
+ while still_running() and time.monotonic() < deadline:
263
344
  time.sleep(0.05)
264
- if process is not None and process.poll() is None:
345
+ if still_running():
265
346
  forced = True
266
- try:
267
- os.killpg(pid, signal.SIGKILL)
268
- details.append("sigkill_process_group")
269
- except ProcessLookupError:
270
- pass
271
- except OSError as exc:
272
- details.append(f"sigkill_group_failed={type(exc).__name__}")
273
- try:
274
- os.kill(pid, signal.SIGKILL)
275
- details.append("sigkill_process")
276
- except OSError as inner:
277
- details.append(f"sigkill_process_failed={type(inner).__name__}")
347
+ _signal_posix_tree(pid, signal.SIGKILL, "sigkill", details)
348
+ if grace > 0:
349
+ force_deadline = time.monotonic() + min(grace, 2.0)
350
+ while still_running() and time.monotonic() < force_deadline:
351
+ time.sleep(0.05)
278
352
 
279
353
  return_code = None
280
354
  if process is not None:
281
355
  try:
282
- return_code = process.wait(timeout=max(0.1, grace_seconds))
356
+ return_code = process.wait(timeout=max(0.1, grace))
283
357
  except subprocess.TimeoutExpired:
284
358
  return_code = process.poll()
359
+
360
+ terminated = (taskkill_ok or (not still_running())) if os.name == "nt" else (not still_running())
285
361
  return ProcessTerminationResult(
286
362
  pid=pid,
287
363
  reason=reason,
288
- terminated=True,
364
+ terminated=terminated,
289
365
  forced=forced,
290
366
  return_code=return_code,
291
367
  details=tuple(details),
292
368
  )
293
369
 
294
370
 
371
+ def terminate_process_tree(
372
+ handle: ProcessHandle,
373
+ reason: str,
374
+ grace_seconds: float = 10.0,
375
+ ) -> ProcessTerminationResult:
376
+ """Terminate a process tree without relying on shell runtime logic.
377
+
378
+ POSIX uses the process group/session created at launch. Windows tries
379
+ graceful taskkill before forced ``taskkill /T /F`` when grace > 0.
380
+ """
381
+ pid = handle.pid
382
+ if pid is None:
383
+ return ProcessTerminationResult(pid=None, reason=reason, terminated=False, forced=False, return_code=None)
384
+ return _terminate_tree_impl(pid, reason=reason, grace_seconds=grace_seconds, process=handle.popen)
385
+
386
+
387
+ def _windows_pid_running(pid: int) -> bool:
388
+ win_dll = getattr(ctypes, "WinDLL", None)
389
+ if win_dll is None:
390
+ try:
391
+ os.kill(pid, 0)
392
+ except PermissionError:
393
+ return True
394
+ except OSError:
395
+ return False
396
+ return True
397
+ kernel32 = win_dll("kernel32", use_last_error=True)
398
+ process_query_limited_information = 0x1000
399
+ still_active = 259
400
+ error_access_denied = 5
401
+ handle = kernel32.OpenProcess(process_query_limited_information, False, int(pid))
402
+ if not handle:
403
+ return ctypes.get_last_error() == error_access_denied
404
+ try:
405
+ exit_code = ctypes.c_ulong()
406
+ if not kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)):
407
+ return False
408
+ return exit_code.value == still_active
409
+ finally:
410
+ kernel32.CloseHandle(handle)
411
+
412
+
295
413
  def is_pid_running(pid: int | None) -> bool:
296
414
  """Return true when a PID appears alive."""
297
415
  if pid is None or pid <= 0:
298
416
  return False
417
+ if os.name == "nt":
418
+ return _windows_pid_running(pid)
299
419
  try:
300
420
  os.kill(pid, 0)
301
421
  except ProcessLookupError:
@@ -304,15 +424,14 @@ def is_pid_running(pid: int | None) -> bool:
304
424
  return True
305
425
  except OSError:
306
426
  return False
307
- if os.name != "nt":
308
- try:
309
- waited_pid, _status = os.waitpid(pid, os.WNOHANG)
310
- except ChildProcessError:
311
- return True
312
- except OSError:
313
- return True
314
- if waited_pid == pid:
315
- return False
427
+ try:
428
+ waited_pid, _status = os.waitpid(pid, os.WNOHANG)
429
+ except ChildProcessError:
430
+ return True
431
+ except OSError:
432
+ return True
433
+ if waited_pid == pid:
434
+ return False
316
435
  return True
317
436
 
318
437
 
@@ -321,83 +440,19 @@ def terminate_pid_tree(
321
440
  reason: str,
322
441
  grace_seconds: float = 10.0,
323
442
  ) -> ProcessTerminationResult:
324
- """Terminate a PID-backed process tree without requiring a live Popen.
325
-
326
- Daemon stop commands reconstruct process state from a PID file, so the
327
- original ``subprocess.Popen`` object is not available. POSIX daemons are
328
- launched in a process group whose id is the daemon PID; Windows uses the
329
- same taskkill-style tree termination strategy as ``terminate_process_tree``.
330
- """
443
+ """Terminate a PID-backed process tree without requiring a live Popen."""
331
444
  if pid is None or pid <= 0:
332
445
  return ProcessTerminationResult(pid=pid, reason=reason, terminated=False, forced=False, return_code=None)
333
-
334
- details: list[str] = []
335
- forced = False
336
446
  if not is_pid_running(pid):
337
- return ProcessTerminationResult(pid=pid, reason=reason, terminated=False, forced=False, return_code=None, details=("not_running",))
338
-
339
- if os.name == "nt":
340
- completed = subprocess.run(
341
- ["taskkill", "/PID", str(pid), "/T", "/F"],
342
- stdout=subprocess.PIPE,
343
- stderr=subprocess.PIPE,
344
- text=True,
345
- check=False,
346
- )
347
- details.append(f"taskkill_exit={completed.returncode}")
348
447
  return ProcessTerminationResult(
349
448
  pid=pid,
350
449
  reason=reason,
351
- terminated=completed.returncode == 0,
352
- forced=True,
450
+ terminated=False,
451
+ forced=False,
353
452
  return_code=None,
354
- details=tuple(details),
453
+ details=("not_running",),
355
454
  )
356
-
357
- try:
358
- os.killpg(pid, signal.SIGTERM)
359
- details.append("sigterm_process_group")
360
- except ProcessLookupError:
361
- pass
362
- except OSError as exc:
363
- details.append(f"sigterm_group_failed={type(exc).__name__}")
364
- try:
365
- os.kill(pid, signal.SIGTERM)
366
- details.append("sigterm_process")
367
- except OSError as inner:
368
- details.append(f"sigterm_process_failed={type(inner).__name__}")
369
-
370
- deadline = time.monotonic() + max(0.0, grace_seconds)
371
- while is_pid_running(pid) and time.monotonic() < deadline:
372
- time.sleep(0.05)
373
-
374
- if is_pid_running(pid):
375
- forced = True
376
- try:
377
- os.killpg(pid, signal.SIGKILL)
378
- details.append("sigkill_process_group")
379
- except ProcessLookupError:
380
- pass
381
- except OSError as exc:
382
- details.append(f"sigkill_group_failed={type(exc).__name__}")
383
- try:
384
- os.kill(pid, signal.SIGKILL)
385
- details.append("sigkill_process")
386
- except OSError as inner:
387
- details.append(f"sigkill_process_failed={type(inner).__name__}")
388
-
389
- deadline = time.monotonic() + max(0.0, min(grace_seconds, 2.0))
390
- while is_pid_running(pid) and time.monotonic() < deadline:
391
- time.sleep(0.05)
392
-
393
- return ProcessTerminationResult(
394
- pid=pid,
395
- reason=reason,
396
- terminated=not is_pid_running(pid),
397
- forced=forced,
398
- return_code=None,
399
- details=tuple(details),
400
- )
455
+ return _terminate_tree_impl(pid, reason=reason, grace_seconds=grace_seconds)
401
456
 
402
457
 
403
458
 
@@ -10,19 +10,14 @@ from collections.abc import Sequence
10
10
  from dataclasses import dataclass
11
11
  from pathlib import Path
12
12
 
13
- from .commands import CommandResult # type: ignore # runtime boundary import
14
13
  from .gitops import (
15
- BranchContext,
16
- WorktreePolicy,
17
14
  default_branch,
18
- guarded_worktree_remove,
19
15
  list_reset_branches,
20
16
  reset_cleanup_branch,
21
- worktree_runtime_context,
22
17
  )
18
+ from .results import CommandResult
23
19
  from .runner_models import RunnerFamily, family_for
24
20
  from .task_checkout import (
25
- CHECKOUT_MODE_WORKTREE,
26
21
  TaskCheckout,
27
22
  load_task_checkout,
28
23
  mark_task_checkout_reset_locked,
@@ -60,7 +55,6 @@ class ResetItem:
60
55
  FAMILY_LABELS = {
61
56
  "feature": "feature",
62
57
  "bugfix": "bug",
63
- "refactor": "refactor",
64
58
  }
65
59
 
66
60
  FILTER_FLAGS = {
@@ -73,21 +67,18 @@ FILTER_FLAGS = {
73
67
  ID_FIELD_BY_KIND = {
74
68
  "feature": "id",
75
69
  "bugfix": "id",
76
- "refactor": "id",
77
70
  }
78
71
 
79
72
 
80
73
  TITLE_FIELD_BY_KIND = {
81
74
  "feature": "title",
82
75
  "bugfix": "title",
83
- "refactor": "title",
84
76
  }
85
77
 
86
78
 
87
79
  BRANCH_PREFIX_BY_KIND = {
88
80
  "feature": "dev",
89
81
  "bugfix": "bugfix",
90
- "refactor": "refactor",
91
82
  }
92
83
 
93
84
  STALLED_FILTER_STATUSES = {
@@ -100,7 +91,7 @@ STALLED_FILTER_STATUSES = {
100
91
 
101
92
 
102
93
  def run_reset_command(kind: str, legacy_args: tuple[str, ...], paths) -> CommandResult:
103
- """Run a manual reset command for feature, bugfix, or refactor."""
94
+ """Run a manual reset command for feature or bugfix."""
104
95
  family = family_for(kind, paths)
105
96
  try:
106
97
  options = _parse_reset_args(family, legacy_args)
@@ -377,18 +368,6 @@ def _reset_one(family: RunnerFamily, item: ResetItem, options: ResetOptions, pro
377
368
  if checkout is not None and (checkout.is_active or checkout.is_completed):
378
369
  if checkout.active_dev_branch not in branches:
379
370
  branches.insert(0, checkout.active_dev_branch)
380
- if checkout.checkout_mode == CHECKOUT_MODE_WORKTREE:
381
- runtime = worktree_runtime_context(
382
- BranchContext(checkout.base_branch, checkout.active_dev_branch, project_root),
383
- WorktreePolicy(True, True, True, Path(checkout.worktree_path).parent),
384
- )
385
- if runtime.worktree_path != Path(checkout.worktree_path).resolve():
386
- lines.append(f"ERROR: Reset {item.item_id} worktree identity resolves to an unexpected path")
387
- return False
388
- removal = guarded_worktree_remove(runtime, delete_branch=False)
389
- if not removal.ok:
390
- lines.append(f"ERROR: Reset {item.item_id} could not remove linked worktree: {removal.reason}")
391
- return False
392
371
 
393
372
  for branch in branches:
394
373
  cleanup = reset_cleanup_branch(project_root, branch, return_branch=return_branch, delete=options.clean)