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
@@ -0,0 +1,1101 @@
1
+ """Focused tests for AI session launching, streaming, and heartbeat handling."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import sys
7
+ import time
8
+
9
+ import pytest
10
+
11
+ from _helpers import PIPELINE_ROOT
12
+
13
+ sys.path.insert(0, str(PIPELINE_ROOT))
14
+
15
+
16
+ def test_session_reader_does_not_retain_or_reread_raw_history():
17
+ import inspect
18
+ import prizmkit_runtime.sessions as sessions
19
+
20
+ reader_source = inspect.getsource(sessions._reader_thread)
21
+ run_source = inspect.getsource(sessions.AISessionLauncher.run)
22
+
23
+ assert "lines.append" not in reader_source
24
+ assert "output_queue" not in reader_source
25
+ assert "session_log.read_text" not in run_source
26
+ assert "splitlines()" not in run_source
27
+ assert "maxsize=1" in run_source
28
+
29
+
30
+ def test_session_reader_contains_parser_failures_and_continues_draining(tmp_path, monkeypatch):
31
+ import io
32
+ import queue
33
+ import prizmkit_runtime.sessions as sessions
34
+
35
+ class FailingTracker:
36
+ def __init__(self, _session_log):
37
+ pass
38
+
39
+ def process_event(self, _event):
40
+ raise ValueError("unsupported shape")
41
+
42
+ def to_dict(self):
43
+ return {"event_type_counts": {}, "errors": []}
44
+
45
+ class FailingNormalizer:
46
+ def process_event(self, _event):
47
+ raise ValueError("unsupported shape")
48
+
49
+ def process_text(self, text):
50
+ return f"[OUTPUT] {text.strip()}\n"
51
+
52
+ def finish(self):
53
+ return "[SESSION_END] status=stream_closed\n"
54
+
55
+ monkeypatch.setattr(sessions, "_load_stream_parser_classes", lambda: (FailingTracker, FailingNormalizer))
56
+ session_log = tmp_path / "session.log"
57
+ backup_log = tmp_path / "backup.log"
58
+ session_log.write_text("", encoding="utf-8")
59
+ backup_log.write_text("", encoding="utf-8")
60
+ result_queue = queue.Queue(maxsize=1)
61
+ pipe = io.StringIO('{"type":"result","subtype":"success","result":"done"}\nplain\n')
62
+
63
+ sessions._reader_thread(pipe, session_log, backup_log, result_queue)
64
+
65
+ text = session_log.read_text(encoding="utf-8")
66
+ assert "[ERROR] progress parser rejected event=result: ValueError" in text
67
+ assert "[ERROR] semantic log adapter rejected event=result: ValueError" in text
68
+ assert "[OUTPUT] done" in text
69
+ assert "[OUTPUT] plain" in text
70
+ assert "[SESSION_END] status=stream_closed" in text
71
+ assert backup_log.read_text(encoding="utf-8") == text
72
+ assert result_queue.get_nowait()["event_type_counts"] == {}
73
+
74
+
75
+ def test_session_persists_normalized_stream_without_raw_history(tmp_path, monkeypatch):
76
+ import prizmkit_runtime.sessions as sessions
77
+
78
+ fake_cli = tmp_path / "pi_like_cli.py"
79
+ fake_cli.write_text(
80
+ "#!/usr/bin/env python3\n"
81
+ "import json, sys\n"
82
+ "_prompt = sys.stdin.read()\n"
83
+ "events = [\n"
84
+ " {'type':'session','id':'s1'},\n"
85
+ " {'type':'agent_start'},\n"
86
+ " {'type':'turn_start'},\n"
87
+ " {'type':'message_update','assistantMessageEvent':{'type':'thinking_start','contentIndex':0}},\n"
88
+ " {'type':'message_update','assistantMessageEvent':{'type':'thinking_delta','contentIndex':0,'delta':'Think'}},\n"
89
+ " {'type':'message_update','assistantMessageEvent':{'type':'thinking_end','contentIndex':0,'content':'Think'}},\n"
90
+ " {'type':'tool_execution_start','toolCallId':'c1','toolName':'bash','args':{'command':'printf ABC'}},\n"
91
+ " {'type':'tool_execution_update','toolCallId':'c1','toolName':'bash','partialResult':{'content':[{'type':'text','text':'A'}]}},\n"
92
+ " {'type':'tool_execution_update','toolCallId':'c1','toolName':'bash','partialResult':{'content':[{'type':'text','text':'ABC'}]}},\n"
93
+ " {'type':'tool_execution_end','toolCallId':'c1','toolName':'bash','result':{'content':[{'type':'text','text':'ABC'}]},'isError':False},\n"
94
+ " {'type':'message_update','assistantMessageEvent':{'type':'text_start','contentIndex':1}},\n"
95
+ " {'type':'message_update','assistantMessageEvent':{'type':'text_delta','contentIndex':1,'delta':'Done'}},\n"
96
+ " {'type':'message_update','assistantMessageEvent':{'type':'text_end','contentIndex':1,'content':'Done'}},\n"
97
+ " {'type':'message_end','message':{'role':'assistant','content':[{'type':'text','text':'Done'}],'stopReason':'stop'}},\n"
98
+ " {'type':'agent_end','willRetry':False,'messages':[{'role':'assistant','content':[{'type':'text','text':'Done'}],'stopReason':'stop'}]},\n"
99
+ "]\n"
100
+ "for event in events: print(json.dumps(event), flush=True)\n",
101
+ encoding="utf-8",
102
+ )
103
+ fake_cli.chmod(0o755)
104
+ prompt = tmp_path / "prompt.md"
105
+ prompt.write_text("hello", encoding="utf-8")
106
+ session_log = tmp_path / "state" / "sessions" / "sess-normalized" / "logs" / "session.log"
107
+ backup_log = tmp_path / "home" / ".prizmkit" / "session-backups" / "sess-normalized.log"
108
+ progress = session_log.with_name("progress.json")
109
+ captured_backup = {}
110
+ original_recover = sessions.recover_session_log
111
+
112
+ def capture_recovery(primary, backup, min_size=1024):
113
+ captured_backup["text"] = backup.read_text(encoding="utf-8")
114
+ return original_recover(primary, backup, min_size)
115
+
116
+ monkeypatch.setattr(sessions, "recover_session_log", capture_recovery)
117
+ result = sessions.AISessionLauncher(
118
+ sessions.AISessionConfig(
119
+ cli_command=str(fake_cli),
120
+ platform="pi",
121
+ model=None,
122
+ prompt_path=prompt,
123
+ cwd=tmp_path,
124
+ log_path=session_log,
125
+ backup_log_path=backup_log,
126
+ progress_path=progress,
127
+ )
128
+ ).run()
129
+
130
+ text = session_log.read_text(encoding="utf-8")
131
+ assert result.exit_code == 0
132
+ assert result.progress_summary.result_subtype == "success"
133
+ assert "[THINK] Think" in text
134
+ assert "[ACTION] bash" in text
135
+ assert "[OBSERVATION] ABC" in text
136
+ assert "[FINAL] Done" in text
137
+ assert "[SESSION_END] status=success" in text
138
+ assert text.count("ABC") == 2 # one action argument and one observation
139
+ assert '"type": "message_update"' not in text
140
+ assert '"type":"message_update"' not in text
141
+ assert captured_backup["text"] == text
142
+ assert not backup_log.exists()
143
+
144
+
145
+ def test_pi_session_diagnostic_tool_output_does_not_trigger_fatal_termination(tmp_path):
146
+ import prizmkit_runtime.sessions as sessions
147
+
148
+ fake_cli = tmp_path / "pi_diagnostic_cli.py"
149
+ fake_cli.write_text(
150
+ "#!/usr/bin/env python3\n"
151
+ "import json, sys, time\n"
152
+ "_prompt = sys.stdin.read()\n"
153
+ "source = 'continuation_reason = \\\"context_overflow\\\"\\nerror = _error_text(result)'\n"
154
+ "events = [\n"
155
+ " {'type':'session','id':'s1'},\n"
156
+ " {'type':'agent_start'},\n"
157
+ " {'type':'turn_start'},\n"
158
+ " {'type':'tool_execution_start','toolCallId':'c1','toolName':'read','args':{'path':'runtime.py'}},\n"
159
+ " {'type':'tool_execution_end','toolCallId':'c1','toolName':'read','result':{'content':[{'type':'text','text':source}]},'isError':False},\n"
160
+ " {'type':'message_end','message':{'role':'toolResult','toolCallId':'c1','content':[{'type':'text','text':source}],'stopReason':'toolUse'}},\n"
161
+ "]\n"
162
+ "for event in events: print(json.dumps(event), flush=True)\n"
163
+ "time.sleep(1.5)\n"
164
+ "message = {'role':'assistant','content':[{'type':'text','text':'Done'}],'stopReason':'stop'}\n"
165
+ "print(json.dumps({'type':'message_end','message':message}), flush=True)\n"
166
+ "print(json.dumps({'type':'agent_end','willRetry':False,'messages':[message]}), flush=True)\n",
167
+ encoding="utf-8",
168
+ )
169
+ fake_cli.chmod(0o755)
170
+ prompt = tmp_path / "prompt.md"
171
+ prompt.write_text("hello", encoding="utf-8")
172
+ session_log = tmp_path / "state" / "sessions" / "sess-diagnostic" / "logs" / "session.log"
173
+ progress = session_log.with_name("progress.json")
174
+
175
+ result = sessions.AISessionLauncher(
176
+ sessions.AISessionConfig(
177
+ cli_command=str(fake_cli),
178
+ platform="pi",
179
+ model=None,
180
+ prompt_path=prompt,
181
+ cwd=tmp_path,
182
+ log_path=session_log,
183
+ progress_path=progress,
184
+ )
185
+ ).run()
186
+
187
+ progress_data = json.loads(progress.read_text(encoding="utf-8"))
188
+ assert result.exit_code == 0
189
+ assert result.fatal_error_code == ""
190
+ assert result.progress_summary.result_subtype == "success"
191
+ assert progress_data["fatal_error_code"] == ""
192
+ assert not session_log.with_name("fatal-error.json").exists()
193
+ assert not session_log.with_name("stale-kill.json").exists()
194
+
195
+
196
+ def test_custom_session_launch_writes_existing_logs_and_progress(tmp_path):
197
+ from prizmkit_runtime.launch_profiles import HeadlessLaunchProfile
198
+ from prizmkit_runtime.sessions import AISessionConfig, AISessionLauncher
199
+
200
+ fake_cli = tmp_path / "custom_cli.py"
201
+ fake_cli.write_text(
202
+ "#!/usr/bin/env python3\n"
203
+ "import json, sys\n"
204
+ "assert sys.argv[1] == '-i'\n"
205
+ "assert sys.argv[2] == 'hello custom'\n"
206
+ "assert sys.argv[3:] == ['--verbose', '--path', 'two words']\n"
207
+ "print(json.dumps({'type':'result','subtype':'success','usage':{'tokens':1},'result':'ok'}), flush=True)\n",
208
+ encoding="utf-8",
209
+ )
210
+ fake_cli.chmod(0o755)
211
+ prompt = tmp_path / "prompt.md"
212
+ prompt.write_text("hello custom", encoding="utf-8")
213
+ session_log = tmp_path / "state" / "sessions" / "sess-custom" / "logs" / "session.log"
214
+ progress = session_log.with_name("progress.json")
215
+
216
+ result = AISessionLauncher(
217
+ AISessionConfig(
218
+ cli_command=str(fake_cli),
219
+ platform="custom",
220
+ model=None,
221
+ prompt_path=prompt,
222
+ cwd=tmp_path,
223
+ log_path=session_log,
224
+ progress_path=progress,
225
+ launch_profile=HeadlessLaunchProfile(
226
+ profile="custom",
227
+ prompt_arg="-i",
228
+ extra_args=("--verbose", "--path", "two words"),
229
+ ),
230
+ )
231
+ ).run()
232
+
233
+ assert result.exit_code == 0
234
+ assert result.command.argv[1:4] == ("-i", "hello custom", "--verbose")
235
+ assert session_log.exists()
236
+ assert progress.exists()
237
+ progress_data = json.loads(progress.read_text(encoding="utf-8"))
238
+ assert progress_data["event_type_counts"]["result"] == 1
239
+ assert progress_data["has_usage"] is True
240
+ assert progress_data["subagent_statistics"]["source"] == "session_log"
241
+ assert progress_data["subagent_statistics"]["spawned"] == 0
242
+ assert progress_data["subagent_statistics"]["providers"] == []
243
+ assert progress_data["child_activity_source"] == "filesystem_liveness_only"
244
+
245
+
246
+ @pytest.mark.parametrize("platform", ["claude", "codex", "codebuddy"])
247
+ def test_builtin_provider_launches_write_session_artifacts(tmp_path, platform):
248
+ from prizmkit_runtime.sessions import AISessionConfig, AISessionLauncher
249
+
250
+ fake_cli = tmp_path / f"{platform}_cli.py"
251
+ fake_cli.write_text(
252
+ "#!/usr/bin/env python3\n"
253
+ "import json, sys\n"
254
+ "_stdin = sys.stdin.read()\n"
255
+ "print(json.dumps({'type':'result','subtype':'success','usage':{'tokens':1},'result':'ok'}), flush=True)\n",
256
+ encoding="utf-8",
257
+ )
258
+ fake_cli.chmod(0o755)
259
+ prompt = tmp_path / f"{platform}-prompt.md"
260
+ prompt.write_text(f"hello {platform}", encoding="utf-8")
261
+ session_log = tmp_path / "state" / "sessions" / f"sess-{platform}" / "logs" / "session.log"
262
+ progress = session_log.with_name("progress.json")
263
+ heartbeat = session_log.with_name("heartbeat.json")
264
+
265
+ result = AISessionLauncher(
266
+ AISessionConfig(
267
+ cli_command=str(fake_cli),
268
+ platform=platform,
269
+ model=None,
270
+ prompt_path=prompt,
271
+ cwd=tmp_path,
272
+ log_path=session_log,
273
+ progress_path=progress,
274
+ heartbeat_path=heartbeat,
275
+ use_stream_json=True,
276
+ )
277
+ ).run()
278
+
279
+ progress_data = json.loads(progress.read_text(encoding="utf-8"))
280
+ assert result.exit_code == 0
281
+ assert session_log.exists()
282
+ assert heartbeat.exists()
283
+ assert progress_data["event_type_counts"]["result"] == 1
284
+ assert progress_data["has_usage"] is True
285
+
286
+
287
+ def test_session_live_output_prints_paths_and_verbose_cli_output(tmp_path, capsys):
288
+ from prizmkit_runtime.sessions import AISessionConfig, AISessionLauncher
289
+
290
+ fake_cli = tmp_path / "live_cli.py"
291
+ fake_cli.write_text(
292
+ "#!/usr/bin/env python3\n"
293
+ "print('live-line', flush=True)\n",
294
+ encoding="utf-8",
295
+ )
296
+ fake_cli.chmod(0o755)
297
+ prompt = tmp_path / "prompt.md"
298
+ prompt.write_text("hello", encoding="utf-8")
299
+ session_log = tmp_path / "state" / "sessions" / "sess-live" / "logs" / "session.log"
300
+ progress = session_log.with_name("progress.json")
301
+ heartbeat = session_log.with_name("heartbeat.json")
302
+
303
+ result = AISessionLauncher(
304
+ AISessionConfig(
305
+ cli_command=str(fake_cli),
306
+ platform="codebuddy",
307
+ model=None,
308
+ prompt_path=prompt,
309
+ cwd=tmp_path,
310
+ log_path=session_log,
311
+ progress_path=progress,
312
+ heartbeat_path=heartbeat,
313
+ verbose=True,
314
+ live_output=True,
315
+ )
316
+ ).run()
317
+
318
+ captured = capsys.readouterr()
319
+ assert result.exit_code == 0
320
+ assert "live-line" in captured.out
321
+ assert "[prizmkit] AI session started" in captured.err
322
+ assert f"[prizmkit] Prompt: {prompt}" in captured.err
323
+ assert f"[prizmkit] Session log: {session_log}" in captured.err
324
+ assert f"[prizmkit] Progress: {progress}" in captured.err
325
+ assert f"[prizmkit] Heartbeat: {heartbeat}" in captured.err
326
+ assert "[prizmkit] AI session finished" in captured.err
327
+
328
+
329
+ def test_compact_progress_line_hides_stale_until_no_progress(tmp_path):
330
+ from prizmkit_runtime.heartbeat import HeartbeatState
331
+ from prizmkit_runtime.sessions import _compact_progress_line
332
+
333
+ session_log = tmp_path / "session.log"
334
+ progress = tmp_path / "progress.json"
335
+ checkpoint = tmp_path / "workflow-checkpoint.json"
336
+ progress.write_text(
337
+ json.dumps(
338
+ {
339
+ "child_total_bytes": 2 * 1024 * 1024,
340
+ "child_session_files": [{"path": "a"}, {"path": "b"}],
341
+ "subagent_statistics": {
342
+ "spawned": 5,
343
+ "active": 2,
344
+ "states": [{"status": "running", "count": 2}, {"status": "completed", "count": 3}],
345
+ },
346
+ "current_phase": "stream-derived-phase",
347
+ "current_tool": "Read",
348
+ "message_count": 465,
349
+ "total_tool_calls": 412,
350
+ }
351
+ ),
352
+ encoding="utf-8",
353
+ )
354
+ checkpoint.write_text(
355
+ json.dumps({"steps": [
356
+ {"id": "S01", "skill": "plan", "name": "Plan", "status": "completed"},
357
+ {"id": "S02", "skill": "retro", "name": "Retrospective", "status": "pending", "depends_on": "S01"},
358
+ ]}),
359
+ encoding="utf-8",
360
+ )
361
+ running = HeartbeatState(last_seen_epoch=1.0, stale=False, details="progress", stale_seconds=0, log_size=4 * 1024 * 1024)
362
+ brief_pause = HeartbeatState(last_seen_epoch=1.0, stale=False, details="no progress", stale_seconds=125, log_size=4 * 1024 * 1024)
363
+ paused = HeartbeatState(last_seen_epoch=1.0, stale=True, details="no progress", stale_seconds=15 * 60, log_size=4 * 1024 * 1024)
364
+ resumed = HeartbeatState(last_seen_epoch=3.0, stale=False, details="progress", stale_seconds=0, log_size=5 * 1024 * 1024)
365
+
366
+ running_line = _compact_progress_line(0, session_log, progress, running, checkpoint_path=checkpoint, stale_threshold_seconds=15 * 60)
367
+ brief_pause_line = _compact_progress_line(0, session_log, progress, brief_pause, checkpoint_path=checkpoint, stale_threshold_seconds=15 * 60)
368
+ paused_line = _compact_progress_line(0, session_log, progress, paused, checkpoint_path=checkpoint, stale_threshold_seconds=15 * 60)
369
+ resumed_line = _compact_progress_line(0, session_log, progress, resumed, checkpoint_path=checkpoint, stale_threshold_seconds=15 * 60)
370
+
371
+ assert "▶ [HEARTBEAT]" in running_line
372
+ assert "log: 4MB" in running_line
373
+ assert "subagents: spawned=5 active=2 states:running=2,completed=3" in running_line
374
+ assert "child logs: 2MB/2 files" in running_line
375
+ assert "child: 2MB/5" not in running_line
376
+ assert "phase: Retrospective" in running_line
377
+ assert "stream-derived-phase" not in running_line
378
+ assert "tool: Read" in running_line
379
+ assert "msgs: 465" in running_line
380
+ assert "412 tool calls" in running_line
381
+ assert "stale:" not in running_line
382
+ assert "▶ [HEARTBEAT]" in brief_pause_line
383
+ assert "stale:" not in brief_pause_line
384
+ assert "⏸ [HEARTBEAT]" in paused_line
385
+ assert "stale: 15m/15m" in paused_line
386
+ assert "▶ [HEARTBEAT]" in resumed_line
387
+ assert "log: 5MB" in resumed_line
388
+ assert "stale:" not in resumed_line
389
+
390
+ def test_compact_progress_line_omits_phase_without_valid_checkpoint(tmp_path):
391
+ from prizmkit_runtime.heartbeat import HeartbeatState
392
+ from prizmkit_runtime.sessions import _compact_progress_line
393
+
394
+ session_log = tmp_path / "session.log"
395
+ progress = tmp_path / "progress.json"
396
+ malformed = tmp_path / "bad-checkpoint.json"
397
+ invalid = tmp_path / "invalid-checkpoint.json"
398
+ progress.write_text(
399
+ json.dumps({"current_phase": "legacy-plan", "current_tool": "Edit"}),
400
+ encoding="utf-8",
401
+ )
402
+ malformed.write_text("{bad", encoding="utf-8")
403
+ invalid.write_text(json.dumps({"steps": [{"id": "S01", "skill": "x", "name": "X", "status": "done"}]}), encoding="utf-8")
404
+ state = HeartbeatState(last_seen_epoch=1.0, stale=False, details="progress", stale_seconds=0, log_size=12)
405
+
406
+ missing_line = _compact_progress_line(0, session_log, progress, state, checkpoint_path=tmp_path / "missing.json")
407
+ malformed_line = _compact_progress_line(0, session_log, progress, state, checkpoint_path=malformed)
408
+ invalid_line = _compact_progress_line(0, session_log, progress, state, checkpoint_path=invalid)
409
+
410
+ for line in (missing_line, malformed_line, invalid_line):
411
+ assert "phase:" not in line
412
+ assert "legacy-plan" not in line
413
+ assert "tool: Edit" in line
414
+
415
+
416
+ def test_heartbeat_observation_stale_accumulates_by_elapsed_time_and_resets_on_progress(tmp_path, monkeypatch):
417
+ import prizmkit_runtime.heartbeat as heartbeat
418
+ from prizmkit_runtime.heartbeat import HeartbeatConfig, observe_heartbeat_state
419
+
420
+ session_log = tmp_path / "session.log"
421
+ progress = tmp_path / "progress.json"
422
+ session_log.write_text("initial\n", encoding="utf-8")
423
+ progress.write_text('{"current_phase":"plan","current_tool":null,"message_count":1,"total_tool_calls":1}\n', encoding="utf-8")
424
+ config = HeartbeatConfig(
425
+ heartbeat_file=tmp_path / "heartbeat.json",
426
+ session_log=session_log,
427
+ progress_file=progress,
428
+ interval_seconds=60,
429
+ stale_after_seconds=15 * 60,
430
+ )
431
+ ticks = iter([1000.0, 1025.0, 1090.0, 1095.0])
432
+ monkeypatch.setattr(heartbeat.time, "time", lambda: next(ticks))
433
+
434
+ first = observe_heartbeat_state(config)
435
+ stale = observe_heartbeat_state(config, first)
436
+ older = observe_heartbeat_state(config, stale)
437
+ progress.write_text('{"current_phase":"plan","current_tool":"Edit","message_count":1,"total_tool_calls":2,"child_total_bytes":10}\n', encoding="utf-8")
438
+ reset = observe_heartbeat_state(config, older)
439
+
440
+ assert first.details == "progress"
441
+ assert first.stale_seconds == 0
442
+ assert first.last_seen_epoch == 1000.0
443
+ assert stale.details == "no progress"
444
+ assert stale.stale_seconds == 25.0
445
+ assert older.stale_seconds == 90.0
446
+ assert reset.details == "progress"
447
+ assert reset.stale_seconds == 0
448
+ assert reset.last_seen_epoch == 1095.0
449
+
450
+
451
+ def test_heartbeat_observation_active_tool_resets_stale_until_tool_clears(tmp_path, monkeypatch):
452
+ import prizmkit_runtime.heartbeat as heartbeat
453
+ from prizmkit_runtime.heartbeat import HeartbeatConfig, observe_heartbeat_state
454
+
455
+ progress = tmp_path / "progress.json"
456
+ progress.write_text(
457
+ '{"current_tool":"bash","message_count":1,"total_tool_calls":1}\n',
458
+ encoding="utf-8",
459
+ )
460
+ config = HeartbeatConfig(
461
+ heartbeat_file=tmp_path / "heartbeat.json",
462
+ progress_file=progress,
463
+ stale_after_seconds=60,
464
+ )
465
+ ticks = iter([1000.0, 1100.0, 1200.0, 1205.0, 1230.0])
466
+ monkeypatch.setattr(heartbeat.time, "time", lambda: next(ticks))
467
+
468
+ first = observe_heartbeat_state(config)
469
+ active = observe_heartbeat_state(config, first)
470
+ still_active = observe_heartbeat_state(config, active)
471
+ progress.write_text(
472
+ '{"current_tool":null,"message_count":1,"total_tool_calls":1}\n',
473
+ encoding="utf-8",
474
+ )
475
+ tool_finished = observe_heartbeat_state(config, still_active)
476
+ idle = observe_heartbeat_state(config, tool_finished)
477
+
478
+ assert active.details == "progress"
479
+ assert active.stale_seconds == 0
480
+ assert active.last_seen_epoch == 1100.0
481
+ assert still_active.details == "progress"
482
+ assert still_active.stale_seconds == 0
483
+ assert still_active.last_seen_epoch == 1200.0
484
+ assert tool_finished.details == "progress"
485
+ assert tool_finished.stale_seconds == 0
486
+ assert tool_finished.last_seen_epoch == 1205.0
487
+ assert idle.details == "no progress"
488
+ assert idle.stale_seconds == 25.0
489
+
490
+
491
+ @pytest.mark.parametrize("current_tool", [None, "", " ", 0, ["bash"]])
492
+ def test_heartbeat_observation_rejects_inactive_or_malformed_tool_values(
493
+ tmp_path,
494
+ monkeypatch,
495
+ current_tool,
496
+ ):
497
+ import prizmkit_runtime.heartbeat as heartbeat
498
+ from prizmkit_runtime.heartbeat import HeartbeatConfig, observe_heartbeat_state
499
+
500
+ progress = tmp_path / "progress.json"
501
+ progress.write_text(json.dumps({"current_tool": current_tool}), encoding="utf-8")
502
+ config = HeartbeatConfig(
503
+ heartbeat_file=tmp_path / "heartbeat.json",
504
+ progress_file=progress,
505
+ stale_after_seconds=60,
506
+ )
507
+ ticks = iter([1000.0, 1070.0])
508
+ monkeypatch.setattr(heartbeat.time, "time", lambda: next(ticks))
509
+
510
+ first = observe_heartbeat_state(config)
511
+ stale = observe_heartbeat_state(config, first)
512
+
513
+ assert stale.details == "no progress"
514
+ assert stale.stale is True
515
+ assert stale.stale_seconds == 70.0
516
+
517
+
518
+ def test_heartbeat_state_tracks_progress_json_child_activity(tmp_path, monkeypatch):
519
+ import prizmkit_runtime.heartbeat as heartbeat
520
+ from prizmkit_runtime.heartbeat import HeartbeatConfig, observe_heartbeat_state
521
+
522
+ progress = tmp_path / "progress.json"
523
+ progress.write_text(
524
+ json.dumps({"child_activity_signature": "child:1", "child_total_bytes": 10}),
525
+ encoding="utf-8",
526
+ )
527
+ config = HeartbeatConfig(
528
+ heartbeat_file=tmp_path / "heartbeat.json",
529
+ session_log=tmp_path / "missing-session.log",
530
+ progress_file=progress,
531
+ stale_after_seconds=60,
532
+ )
533
+ ticks = iter([10.0, 20.0, 25.0])
534
+ monkeypatch.setattr(heartbeat.time, "time", lambda: next(ticks))
535
+
536
+ first = observe_heartbeat_state(config)
537
+ quiet = observe_heartbeat_state(config, first)
538
+ progress.write_text(
539
+ json.dumps({"child_activity_signature": "child:2", "child_total_bytes": 20}),
540
+ encoding="utf-8",
541
+ )
542
+ child_progress = observe_heartbeat_state(config, quiet)
543
+
544
+ assert quiet.details == "no progress"
545
+ assert quiet.stale_seconds == 10.0
546
+ assert child_progress.details == "progress"
547
+ assert child_progress.stale_seconds == 0
548
+ assert child_progress.child_activity_signature == "child:2"
549
+
550
+
551
+ def test_heartbeat_monitor_stale_kill_uses_elapsed_no_progress_threshold(tmp_path):
552
+ from prizmkit_runtime.sessions import AISessionConfig, AISessionLauncher
553
+
554
+ fake_cli = tmp_path / "quiet_cli.py"
555
+ fake_cli.write_text(
556
+ "#!/usr/bin/env python3\n"
557
+ "import time\n"
558
+ "time.sleep(5)\n",
559
+ encoding="utf-8",
560
+ )
561
+ fake_cli.chmod(0o755)
562
+ prompt = tmp_path / "prompt.md"
563
+ prompt.write_text("hello", encoding="utf-8")
564
+ session_log = tmp_path / "state" / "sessions" / "sess-stale" / "logs" / "session.log"
565
+ progress = session_log.with_name("progress.json")
566
+ heartbeat = session_log.with_name("heartbeat.json")
567
+
568
+ result = AISessionLauncher(
569
+ AISessionConfig(
570
+ cli_command=str(fake_cli),
571
+ platform="codebuddy",
572
+ model=None,
573
+ prompt_path=prompt,
574
+ cwd=tmp_path,
575
+ log_path=session_log,
576
+ progress_path=progress,
577
+ heartbeat_path=heartbeat,
578
+ stale_kill_threshold_seconds=0.3,
579
+ heartbeat_stale_threshold_seconds=0.2,
580
+ stale_kill_grace_seconds=0.1,
581
+ )
582
+ ).run()
583
+
584
+ marker = session_log.parent / "stale-kill.json"
585
+ marker_data = json.loads(marker.read_text(encoding="utf-8"))
586
+ assert result.termination_reason == "stale_session"
587
+ assert result.stale_marker == marker
588
+ assert marker_data["reason"] == "stale_session"
589
+ assert marker_data["threshold"] == pytest.approx(0.3)
590
+ assert marker_data["stale_seconds"] >= 0
591
+
592
+
593
+ def test_heartbeat_monitor_active_tool_defers_stale_kill_until_tool_clears(tmp_path):
594
+ from prizmkit_runtime.heartbeat import HeartbeatConfig, HeartbeatMonitor
595
+ from prizmkit_runtime.processes import ProcessSpec, is_process_running, launch_process
596
+
597
+ sleeper = tmp_path / "active_tool_sleep.py"
598
+ sleeper.write_text("import time\ntime.sleep(5)\n", encoding="utf-8")
599
+ progress = tmp_path / "progress.json"
600
+ progress.write_text('{"current_tool":"bash"}\n', encoding="utf-8")
601
+ handle = launch_process(ProcessSpec(command=(sys.executable, str(sleeper)), cwd=tmp_path))
602
+ monitor = HeartbeatMonitor(
603
+ handle,
604
+ HeartbeatConfig(
605
+ heartbeat_file=tmp_path / "heartbeat.json",
606
+ session_log=tmp_path / "session.log",
607
+ progress_file=progress,
608
+ marker_dir=tmp_path,
609
+ interval_seconds=0.05,
610
+ stale_after_seconds=0.1,
611
+ kill_after_seconds=0.2,
612
+ grace_seconds=0.1,
613
+ ),
614
+ )
615
+ monitor.start()
616
+
617
+ time.sleep(0.5)
618
+ assert is_process_running(handle)
619
+ assert not (tmp_path / "stale-kill.json").exists()
620
+
621
+ progress.write_text('{"current_tool":null}\n', encoding="utf-8")
622
+ deadline = time.time() + 3
623
+ while time.time() < deadline and not (tmp_path / "stale-kill.json").exists():
624
+ time.sleep(0.05)
625
+ result = monitor.stop()
626
+
627
+ marker = json.loads((tmp_path / "stale-kill.json").read_text(encoding="utf-8"))
628
+ assert result.killed is True
629
+ assert result.reason == "stale_session"
630
+ assert marker["reason"] == "stale_session"
631
+
632
+
633
+ def test_pi_session_active_tool_stream_survives_stale_threshold(tmp_path, monkeypatch):
634
+ import prizmkit_runtime.sessions as sessions
635
+ from prizmkit_runtime.sessions import AISessionConfig, AISessionLauncher
636
+
637
+ parser_classes = sessions._load_stream_parser_classes()
638
+ monkeypatch.setattr(sessions, "_load_stream_parser_classes", lambda: parser_classes)
639
+
640
+ fake_cli = tmp_path / "pi_active_tool_cli.py"
641
+ fake_cli.write_text(
642
+ "#!/usr/bin/env python3\n"
643
+ "import json, sys, time\n"
644
+ "_prompt = sys.stdin.read(5)\n"
645
+ "for event in [\n"
646
+ " {'type':'session','id':'active-tool-session'},\n"
647
+ " {'type':'agent_start'},\n"
648
+ " {'type':'turn_start'},\n"
649
+ " {'type':'tool_execution_start','toolCallId':'call-1','toolName':'long-running-tool','args':{}},\n"
650
+ "]:\n"
651
+ " print(json.dumps(event), flush=True)\n"
652
+ "time.sleep(3.0)\n"
653
+ "for event in [\n"
654
+ " {'type':'tool_execution_end','toolCallId':'call-1','toolName':'long-running-tool','result':{'content':[]},'isError':False},\n"
655
+ " {'type':'message_end','message':{'role':'assistant','content':[{'type':'text','text':'done'}],'stopReason':'stop'}},\n"
656
+ " {'type':'agent_end','willRetry':False,'messages':[{'role':'assistant','content':[{'type':'text','text':'done'}],'stopReason':'stop'}]},\n"
657
+ "]:\n"
658
+ " print(json.dumps(event), flush=True)\n",
659
+ encoding="utf-8",
660
+ )
661
+ fake_cli.chmod(0o755)
662
+ prompt = tmp_path / "prompt.md"
663
+ prompt.write_text("hello", encoding="utf-8")
664
+ session_log = tmp_path / "state" / "sessions" / "active-tool" / "logs" / "session.log"
665
+ progress = session_log.with_name("progress.json")
666
+
667
+ result = AISessionLauncher(
668
+ AISessionConfig(
669
+ cli_command=str(fake_cli),
670
+ platform="pi",
671
+ model=None,
672
+ prompt_path=prompt,
673
+ cwd=tmp_path,
674
+ log_path=session_log,
675
+ progress_path=progress,
676
+ heartbeat_path=session_log.with_name("heartbeat.json"),
677
+ stale_kill_threshold_seconds=1.5,
678
+ heartbeat_stale_threshold_seconds=1.0,
679
+ stale_kill_grace_seconds=0.1,
680
+ live_progress_interval_seconds=0.05,
681
+ )
682
+ ).run()
683
+
684
+ final_progress = json.loads(progress.read_text(encoding="utf-8"))
685
+ assert result.exit_code == 0
686
+ assert result.termination_reason is None
687
+ assert final_progress["current_tool"] is None
688
+ assert not (session_log.parent / "stale-kill.json").exists()
689
+
690
+
691
+ def test_stale_marker_preserves_subsecond_threshold(tmp_path):
692
+ from prizmkit_runtime.heartbeat import write_stale_marker
693
+
694
+ marker = write_stale_marker(
695
+ tmp_path,
696
+ reason="stale_session",
697
+ stale_seconds=0.4,
698
+ threshold=0.3,
699
+ )
700
+ data = json.loads(marker.read_text(encoding="utf-8"))
701
+
702
+ assert data["stale_seconds"] == pytest.approx(0.4)
703
+ assert data["threshold"] == pytest.approx(0.3)
704
+
705
+
706
+ def test_idle_progress_refresh_updates_child_activity_without_parent_output(tmp_path, monkeypatch):
707
+ import prizmkit_runtime.sessions as sessions
708
+
709
+ progress = tmp_path / "progress.json"
710
+ tracker_lock = sessions.threading.Lock()
711
+ stop_event = sessions.threading.Event()
712
+
713
+ class FakeTracker:
714
+ def __init__(self):
715
+ self.calls = 0
716
+
717
+ def to_dict(self):
718
+ self.calls += 1
719
+ return {
720
+ "message_count": 1,
721
+ "event_type_counts": {},
722
+ "current_tool": "Task",
723
+ "current_phase": "implement",
724
+ "total_tool_calls": 1,
725
+ "subagent_statistics": {"active": 1, "spawned": 1},
726
+ "child_activity_signature": f"child:{self.calls}",
727
+ "child_total_bytes": self.calls,
728
+ "fatal_error_code": "",
729
+ "terminal_result_text": "",
730
+ "errors": [],
731
+ }
732
+
733
+ thread, refresh = sessions._start_idle_progress_refresh(
734
+ progress,
735
+ FakeTracker(),
736
+ tracker_lock,
737
+ stop_event,
738
+ interval_seconds=0.1,
739
+ )
740
+ try:
741
+ refresh(True)
742
+ first = json.loads(progress.read_text(encoding="utf-8"))
743
+ time.sleep(0.25)
744
+ second = json.loads(progress.read_text(encoding="utf-8"))
745
+ finally:
746
+ stop_event.set()
747
+ thread.join(timeout=1)
748
+
749
+ assert first["child_activity_signature"].startswith("child:")
750
+ assert second["child_activity_signature"] != first["child_activity_signature"]
751
+ assert second["child_total_bytes"] > first["child_total_bytes"]
752
+
753
+ def test_compact_live_heartbeat_stays_out_of_primary_and_backup_logs(tmp_path, capsys, monkeypatch):
754
+ import prizmkit_runtime.sessions as sessions
755
+
756
+ fake_cli = tmp_path / "slow_cli.py"
757
+ fake_cli.write_text(
758
+ "#!/usr/bin/env python3\n"
759
+ "import time\n"
760
+ "print('logged-payload', flush=True)\n"
761
+ "time.sleep(0.45)\n",
762
+ encoding="utf-8",
763
+ )
764
+ fake_cli.chmod(0o755)
765
+ prompt = tmp_path / "prompt.md"
766
+ prompt.write_text("hello", encoding="utf-8")
767
+ session_log = tmp_path / "state" / "sessions" / "sess-heartbeat" / "logs" / "session.log"
768
+ backup_log = tmp_path / "home" / ".prizmkit" / "session-backups" / "sess-heartbeat.log"
769
+ progress = session_log.with_name("progress.json")
770
+ backup_text = {}
771
+ original_recover = sessions.recover_session_log
772
+
773
+ def spy_recover(primary, backup, min_size=1024):
774
+ if backup.is_file():
775
+ backup_text["content"] = backup.read_text(encoding="utf-8")
776
+ return original_recover(primary, backup, min_size)
777
+
778
+ monkeypatch.setattr(sessions, "recover_session_log", spy_recover)
779
+ result = sessions.AISessionLauncher(
780
+ sessions.AISessionConfig(
781
+ cli_command=str(fake_cli),
782
+ platform="codebuddy",
783
+ model=None,
784
+ prompt_path=prompt,
785
+ cwd=tmp_path,
786
+ log_path=session_log,
787
+ backup_log_path=backup_log,
788
+ progress_path=progress,
789
+ live_output=True,
790
+ live_progress_interval_seconds=0.1,
791
+ stale_kill_threshold_seconds=15 * 60,
792
+ )
793
+ ).run()
794
+
795
+ captured = capsys.readouterr()
796
+ primary_text = session_log.read_text(encoding="utf-8")
797
+ assert result.exit_code == 0
798
+ assert "[HEARTBEAT]" in captured.err
799
+ assert "logged-payload" in primary_text
800
+ assert "[HEARTBEAT]" not in primary_text
801
+ assert "stale:" not in primary_text
802
+ assert "[HEARTBEAT]" not in backup_text.get("content", "")
803
+ assert "stale:" not in backup_text.get("content", "")
804
+
805
+
806
+ def test_session_summary_reports_normalized_subagents_and_child_liveness(tmp_path, capsys):
807
+ import prizmkit_runtime.runners as runners
808
+ from prizmkit_runtime.runner_models import SessionPaths
809
+
810
+ logs = tmp_path / "sessions" / "s1" / "logs"
811
+ logs.mkdir(parents=True)
812
+ session_log = logs / "session.log"
813
+ progress = logs / "progress.json"
814
+ session_log.write_text("one\ntwo\n", encoding="utf-8")
815
+ progress.write_text(
816
+ json.dumps({
817
+ "subagent_statistics": {
818
+ "spawned": 2,
819
+ "active": 1,
820
+ "states": [{"status": "running", "count": 1}, {"status": "completed", "count": 1}],
821
+ "providers": ["claude"],
822
+ "source": "session_log",
823
+ },
824
+ "child_total_bytes": 2048,
825
+ "child_session_files": [{"path": "child-a"}],
826
+ }),
827
+ encoding="utf-8",
828
+ )
829
+ paths = SessionPaths(
830
+ session_dir=logs.parent,
831
+ prompt_path=logs.parent / "bootstrap-prompt.md",
832
+ logs_dir=logs,
833
+ session_log=session_log,
834
+ progress_json=progress,
835
+ heartbeat_json=logs / "heartbeat.json",
836
+ session_status_json=logs / "session-status.json",
837
+ )
838
+ result = type("Result", (), {"exit_code": 0})()
839
+ classification = type("Classification", (), {"session_status": "success"})()
840
+
841
+ runners._emit_session_summary(result, paths, classification)
842
+
843
+ captured = capsys.readouterr()
844
+ assert "Subagents: spawned=2, active=1, states: running=1, completed=1 (claude; source=session.log)" in captured.err
845
+ assert "Child log activity: 2KB across 1 files (heartbeat liveness only)" in captured.err
846
+ assert "Subagent calls detected" not in captured.err
847
+
848
+ def test_ai_interruption_diagnostics_are_detailed_console_only(tmp_path, capsys):
849
+ import signal
850
+
851
+ from prizmkit_runtime.heartbeat import HeartbeatState
852
+ from prizmkit_runtime.processes import (
853
+ ProcessHandle,
854
+ ProcessSpec,
855
+ ProcessTerminationResult,
856
+ RuntimeInterruption,
857
+ )
858
+ from prizmkit_runtime.sessions import (
859
+ AISessionCommand,
860
+ _emit_ai_interruption_received,
861
+ _emit_ai_interruption_termination,
862
+ )
863
+
864
+ session_log = tmp_path / "sessions" / "sess-interrupt" / "logs" / "session.log"
865
+ session_log.parent.mkdir(parents=True)
866
+ session_log.write_text("session activity\n", encoding="utf-8")
867
+ progress = session_log.with_name("progress.json")
868
+ progress.write_text(
869
+ json.dumps(
870
+ {
871
+ "current_phase": "implement",
872
+ "current_tool": "bash",
873
+ "current_tool_input_summary": "secret-command-must-not-be-logged",
874
+ "message_count": 455,
875
+ "total_tool_calls": 453,
876
+ "subagent_statistics": {"active": 0, "spawned": 0},
877
+ }
878
+ ),
879
+ encoding="utf-8",
880
+ )
881
+ handle = ProcessHandle(
882
+ pid=999_999,
883
+ spec=ProcessSpec(command=("fake-ai",), cwd=tmp_path),
884
+ status="running",
885
+ popen=None,
886
+ )
887
+ command = AISessionCommand(
888
+ argv=("fake-ai",),
889
+ stdin_text=None,
890
+ provider="pi",
891
+ prompt_mode="stdin",
892
+ )
893
+ heartbeat = HeartbeatState(
894
+ last_seen_epoch=time.time() - 1,
895
+ stale=False,
896
+ details="no progress",
897
+ stale_seconds=1.0,
898
+ log_size=2048,
899
+ )
900
+
901
+ _emit_ai_interruption_received(
902
+ RuntimeInterruption(signal.SIGTERM),
903
+ command=command,
904
+ handle=handle,
905
+ started_epoch=time.time() - 120,
906
+ session_log=session_log,
907
+ progress_path=progress,
908
+ checkpoint_path=None,
909
+ heartbeat_state=heartbeat,
910
+ )
911
+ _emit_ai_interruption_termination(
912
+ ProcessTerminationResult(
913
+ pid=handle.pid,
914
+ reason="interrupted",
915
+ terminated=True,
916
+ forced=False,
917
+ return_code=-15,
918
+ details=("sigterm_process_group",),
919
+ )
920
+ )
921
+
922
+ captured = capsys.readouterr()
923
+ assert "received trigger=platform_signal; platform_signal=SIGTERM(15)" in captured.err
924
+ assert "runner_pid=" in captured.err
925
+ assert "parent_pid=" in captured.err
926
+ assert "ai_pid=999999" in captured.err
927
+ assert "provider=pi" in captured.err
928
+ assert "phase=implement; tool=bash; messages=455; tool_calls=453" in captured.err
929
+ assert "heartbeat details=no progress; stale=False; age=1s" in captured.err
930
+ assert "process-tree termination completed" in captured.err
931
+ assert "forced=False; return_code=-15; details=sigterm_process_group" in captured.err
932
+ assert "secret-command-must-not-be-logged" not in captured.err
933
+ assert not session_log.with_name("session-status.json").exists()
934
+
935
+
936
+ def test_ai_session_interrupt_diagnostics_are_wired_before_existing_cleanup(monkeypatch, tmp_path, capsys):
937
+ import io
938
+
939
+ from prizmkit_runtime import sessions
940
+ from prizmkit_runtime.processes import ProcessHandle, ProcessTerminationResult
941
+ from prizmkit_runtime.sessions import AISessionConfig, AISessionLauncher
942
+
943
+ prompt = tmp_path / "prompt.md"
944
+ prompt.write_text("hello", encoding="utf-8")
945
+ session_log = tmp_path / "sessions" / "sess-wired" / "logs" / "session.log"
946
+ termination_calls = []
947
+
948
+ class InterruptingProcess:
949
+ def __init__(self):
950
+ self.stdout = io.StringIO("")
951
+ self.stdin = io.StringIO()
952
+ self.returncode = -2
953
+
954
+ def wait(self, timeout=None):
955
+ raise KeyboardInterrupt
956
+
957
+ def poll(self):
958
+ return self.returncode
959
+
960
+ process = InterruptingProcess()
961
+
962
+ def fake_launch(spec, **kwargs):
963
+ return ProcessHandle(pid=777_777, spec=spec, status="running", popen=process)
964
+
965
+ def fake_terminate(handle, reason, grace_seconds=10):
966
+ termination_calls.append((handle.pid, reason, grace_seconds))
967
+ return ProcessTerminationResult(
968
+ pid=handle.pid,
969
+ reason=reason,
970
+ terminated=True,
971
+ forced=False,
972
+ return_code=process.returncode,
973
+ details=("sigterm_process_group",),
974
+ )
975
+
976
+ monkeypatch.setattr(sessions, "launch_process", fake_launch)
977
+ monkeypatch.setattr(sessions, "terminate_process_tree", fake_terminate)
978
+
979
+ with pytest.raises(KeyboardInterrupt):
980
+ AISessionLauncher(
981
+ AISessionConfig(
982
+ cli_command="fake-ai",
983
+ platform="pi",
984
+ model=None,
985
+ prompt_path=prompt,
986
+ cwd=tmp_path,
987
+ log_path=session_log,
988
+ stale_kill_grace_seconds=4,
989
+ )
990
+ ).run()
991
+
992
+ captured = capsys.readouterr()
993
+ assert termination_calls == [(777_777, "interrupted", 4)]
994
+ assert "received trigger=keyboard_interrupt; platform_signal=SIGINT(2) (inferred)" in captured.err
995
+ assert "ai_pid=777777" in captured.err
996
+ assert "classification and retry accounting have not run for this AI session" in captured.err
997
+ assert "process-tree termination completed pid=777777" in captured.err
998
+ assert not session_log.with_name("session-status.json").exists()
999
+
1000
+
1001
+ def test_session_live_output_does_not_forward_cli_output_without_verbose(tmp_path, capsys):
1002
+ from prizmkit_runtime.sessions import AISessionConfig, AISessionLauncher
1003
+
1004
+ fake_cli = tmp_path / "quiet_cli.py"
1005
+ fake_cli.write_text(
1006
+ "#!/usr/bin/env python3\n"
1007
+ "print('hidden-line', flush=True)\n",
1008
+ encoding="utf-8",
1009
+ )
1010
+ fake_cli.chmod(0o755)
1011
+ prompt = tmp_path / "prompt.md"
1012
+ prompt.write_text("hello", encoding="utf-8")
1013
+ session_log = tmp_path / "state" / "sessions" / "sess-quiet" / "logs" / "session.log"
1014
+
1015
+ result = AISessionLauncher(
1016
+ AISessionConfig(
1017
+ cli_command=str(fake_cli),
1018
+ platform="codebuddy",
1019
+ model=None,
1020
+ prompt_path=prompt,
1021
+ cwd=tmp_path,
1022
+ log_path=session_log,
1023
+ live_output=True,
1024
+ )
1025
+ ).run()
1026
+
1027
+ captured = capsys.readouterr()
1028
+ assert result.exit_code == 0
1029
+ assert "hidden-line" not in captured.out
1030
+ assert "hidden-line" in session_log.read_text(encoding="utf-8")
1031
+ assert "[prizmkit] AI session started" in captured.err
1032
+ assert "[prizmkit] AI session finished" in captured.err
1033
+
1034
+
1035
+ def test_session_final_progress_merge_preserves_tracker_fatal_error(tmp_path):
1036
+ from prizmkit_runtime.sessions import AISessionConfig, AISessionLauncher
1037
+
1038
+ fake_cli = tmp_path / "context_cli.py"
1039
+ fake_cli.write_text(
1040
+ "#!/usr/bin/env python3\n"
1041
+ "import json\n"
1042
+ "print(json.dumps({'type':'result','is_error':True,'result':'API Error: Your input exceeds the context window'}), flush=True)\n",
1043
+ encoding="utf-8",
1044
+ )
1045
+ fake_cli.chmod(0o755)
1046
+ prompt = tmp_path / "prompt.md"
1047
+ prompt.write_text("hello", encoding="utf-8")
1048
+ session_log = tmp_path / "state" / "sessions" / "sess-context" / "logs" / "session.log"
1049
+ progress = session_log.with_name("progress.json")
1050
+
1051
+ result = AISessionLauncher(
1052
+ AISessionConfig(
1053
+ cli_command=str(fake_cli),
1054
+ platform="codebuddy",
1055
+ model=None,
1056
+ prompt_path=prompt,
1057
+ cwd=tmp_path,
1058
+ log_path=session_log,
1059
+ progress_path=progress,
1060
+ )
1061
+ ).run()
1062
+
1063
+ progress_data = json.loads(progress.read_text(encoding="utf-8"))
1064
+ assert result.progress_summary.fatal_error_code == "context_overflow"
1065
+ assert result.fatal_error_code == "context_overflow"
1066
+ assert progress_data["fatal_error_code"] == "context_overflow"
1067
+
1068
+
1069
+ def test_session_unsets_claudecode_and_merges_stderr_order(tmp_path, monkeypatch):
1070
+ from prizmkit_runtime.sessions import AISessionConfig, AISessionLauncher
1071
+
1072
+ fake_cli = tmp_path / "env_cli.py"
1073
+ fake_cli.write_text(
1074
+ "#!/usr/bin/env python3\n"
1075
+ "import os, sys\n"
1076
+ "print('stdout-before', flush=True)\n"
1077
+ "print('claudecode=' + os.environ.get('CLAUDECODE', '<unset>'), flush=True)\n"
1078
+ "print('stderr-after', file=sys.stderr, flush=True)\n",
1079
+ encoding="utf-8",
1080
+ )
1081
+ fake_cli.chmod(0o755)
1082
+ monkeypatch.setenv("CLAUDECODE", "1")
1083
+ prompt = tmp_path / "prompt.md"
1084
+ prompt.write_text("hello", encoding="utf-8")
1085
+ session_log = tmp_path / "state" / "sessions" / "sess-env" / "logs" / "session.log"
1086
+
1087
+ result = AISessionLauncher(
1088
+ AISessionConfig(
1089
+ cli_command=str(fake_cli),
1090
+ platform="codebuddy",
1091
+ model=None,
1092
+ prompt_path=prompt,
1093
+ cwd=tmp_path,
1094
+ log_path=session_log,
1095
+ )
1096
+ ).run()
1097
+
1098
+ text = session_log.read_text(encoding="utf-8")
1099
+ assert result.exit_code == 0
1100
+ assert "claudecode=<unset>" in text
1101
+ assert text.index("stdout-before") < text.index("stderr-after")