agentihooks 2.13.2__tar.gz → 2.13.4__tar.gz

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 (167) hide show
  1. {agentihooks-2.13.2 → agentihooks-2.13.4}/PKG-INFO +1 -1
  2. {agentihooks-2.13.2 → agentihooks-2.13.4}/agentihooks.egg-info/PKG-INFO +1 -1
  3. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/claude_md_sanity.py +81 -2
  4. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/hook_manager.py +23 -0
  5. agentihooks-2.13.4/profiles/package/skills/run-claude-terminal/SKILL.md +73 -0
  6. {agentihooks-2.13.2 → agentihooks-2.13.4}/pyproject.toml +1 -1
  7. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/claude_terminal.py +53 -21
  8. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/install.py +12 -8
  9. agentihooks-2.13.4/tests/test_claude_md_sanity.py +211 -0
  10. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_claude_terminal.py +68 -6
  11. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_install.py +43 -0
  12. agentihooks-2.13.2/profiles/package/skills/run-claude-terminal/SKILL.md +0 -49
  13. agentihooks-2.13.2/tests/test_claude_md_sanity.py +0 -96
  14. {agentihooks-2.13.2 → agentihooks-2.13.4}/LICENSE +0 -0
  15. {agentihooks-2.13.2 → agentihooks-2.13.4}/README.md +0 -0
  16. {agentihooks-2.13.2 → agentihooks-2.13.4}/agentihooks.egg-info/SOURCES.txt +0 -0
  17. {agentihooks-2.13.2 → agentihooks-2.13.4}/agentihooks.egg-info/dependency_links.txt +0 -0
  18. {agentihooks-2.13.2 → agentihooks-2.13.4}/agentihooks.egg-info/entry_points.txt +0 -0
  19. {agentihooks-2.13.2 → agentihooks-2.13.4}/agentihooks.egg-info/requires.txt +0 -0
  20. {agentihooks-2.13.2 → agentihooks-2.13.4}/agentihooks.egg-info/top_level.txt +0 -0
  21. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/__init__.py +0 -0
  22. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/__main__.py +0 -0
  23. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/_async.py +0 -0
  24. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/_brain_http.py +0 -0
  25. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/_redis.py +0 -0
  26. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/common.py +0 -0
  27. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/config.py +0 -0
  28. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/__init__.py +0 -0
  29. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/_strip.py +0 -0
  30. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/amygdala_hook.py +0 -0
  31. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/auto_dev_switch.py +0 -0
  32. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/bash_output_filter.py +0 -0
  33. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/brain_adapter.py +0 -0
  34. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/brain_writer_hook.py +0 -0
  35. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/branch_guard.py +0 -0
  36. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/broadcast.py +0 -0
  37. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/ci_manifesto.py +0 -0
  38. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/codex_context_pin.py +0 -0
  39. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/compact_advisor.py +0 -0
  40. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/controls_toggle.py +0 -0
  41. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/credential_guard.py +0 -0
  42. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/data/abbreviations.json +0 -0
  43. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/dep_banner.py +0 -0
  44. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/enforcement.py +0 -0
  45. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/file_read_cache.py +0 -0
  46. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/kubectl_mutation_guard.py +0 -0
  47. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/preprocessor.py +0 -0
  48. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/prod_lockdown.py +0 -0
  49. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/project_bridge.py +0 -0
  50. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/project_resources.py +0 -0
  51. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/quota_usage.py +0 -0
  52. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/retry_breaker.py +0 -0
  53. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/rules_refresh.py +0 -0
  54. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/thinking_policy.py +0 -0
  55. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/version_guard.py +0 -0
  56. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/context/voice_output.py +0 -0
  57. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/integrations/__init__.py +0 -0
  58. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/integrations/aws.py +0 -0
  59. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/integrations/base.py +0 -0
  60. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/integrations/dynamodb.py +0 -0
  61. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/integrations/lambda_invoke.py +0 -0
  62. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/integrations/mailer.py +0 -0
  63. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/integrations/postgres.py +0 -0
  64. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/integrations/session_state.py +0 -0
  65. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/integrations/storage.py +0 -0
  66. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/mcp/__init__.py +0 -0
  67. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/mcp/__main__.py +0 -0
  68. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/mcp/_registry.py +0 -0
  69. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/mcp/_session.py +0 -0
  70. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/mcp/channels.py +0 -0
  71. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/mcp/enforcement.py +0 -0
  72. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/memory/__init__.py +0 -0
  73. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/memory/__main__.py +0 -0
  74. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/memory/auto_save.py +0 -0
  75. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/memory/server.py +0 -0
  76. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/memory/store.py +0 -0
  77. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/memory/transcript_reader.py +0 -0
  78. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/observability/__init__.py +0 -0
  79. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/observability/agent_log_stream.py +0 -0
  80. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/observability/container_logs.py +0 -0
  81. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/observability/context_audit.py +0 -0
  82. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/observability/event_relay.py +0 -0
  83. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/observability/metrics.py +0 -0
  84. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/observability/otel.py +0 -0
  85. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/observability/token_monitor.py +0 -0
  86. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/observability/transcript.py +0 -0
  87. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/quota.py +0 -0
  88. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/secrets.py +0 -0
  89. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/statusline.py +0 -0
  90. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/targets/__init__.py +0 -0
  91. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/targets/capabilities.py +0 -0
  92. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/targets/emitter.py +0 -0
  93. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/targets/normalizer.py +0 -0
  94. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/targets/notify_shim.py +0 -0
  95. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/telemetry.py +0 -0
  96. {agentihooks-2.13.2 → agentihooks-2.13.4}/hooks/tool_memory.py +0 -0
  97. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/__init__.py +0 -0
  98. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/_base/__init__.py +0 -0
  99. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/_base/config.base.toml +0 -0
  100. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/_base/env.defaults.json +0 -0
  101. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/_base/settings.base.copilot.json +0 -0
  102. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/_base/settings.base.json +0 -0
  103. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/default/CLAUDE.md +0 -0
  104. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/default/__init__.py +0 -0
  105. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/default/profile.yml +0 -0
  106. {agentihooks-2.13.2 → agentihooks-2.13.4}/profiles/package/rules/agentihooks-toolbelt.md +0 -0
  107. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/__init__.py +0 -0
  108. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/claude_linter.py +0 -0
  109. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/claude_quota_balancer.py +0 -0
  110. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/mcp_daemon.py +0 -0
  111. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/mcp_reporter.py +0 -0
  112. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/packaging/systemd/agentihooks-mcp.service.template +0 -0
  113. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/status_checker.py +0 -0
  114. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/targets/__init__.py +0 -0
  115. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/targets/_common.py +0 -0
  116. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/targets/claude_target.py +0 -0
  117. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/targets/codex_target.py +0 -0
  118. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/targets/copilot_target.py +0 -0
  119. {agentihooks-2.13.2 → agentihooks-2.13.4}/scripts/updater.py +0 -0
  120. {agentihooks-2.13.2 → agentihooks-2.13.4}/setup.cfg +0 -0
  121. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_brain_defaults.py +0 -0
  122. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_brain_env_discovery.py +0 -0
  123. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_channels.py +0 -0
  124. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_ci_manifesto.py +0 -0
  125. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_claude_linter.py +0 -0
  126. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_claude_quota_balancer.py +0 -0
  127. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_codex_e2e.py +0 -0
  128. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_codex_target.py +0 -0
  129. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_common.py +0 -0
  130. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_common_extended.py +0 -0
  131. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_config.py +0 -0
  132. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_config_env_precedence.py +0 -0
  133. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_container_logs.py +0 -0
  134. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_copilot_e2e.py +0 -0
  135. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_copilot_target.py +0 -0
  136. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_credential_envelope.py +0 -0
  137. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_credential_guard.py +0 -0
  138. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_enforcement.py +0 -0
  139. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_enforcement_cli.py +0 -0
  140. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_event_relay.py +0 -0
  141. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_hook_manager.py +0 -0
  142. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_hook_targets.py +0 -0
  143. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_import_hygiene.py +0 -0
  144. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_install_validation.py +0 -0
  145. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_integrations_base.py +0 -0
  146. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_kubectl_mutation_guard.py +0 -0
  147. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_mcp_daemon.py +0 -0
  148. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_mcp_registry.py +0 -0
  149. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_mcp_reporter.py +0 -0
  150. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_mcp_session.py +0 -0
  151. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_memory_store.py +0 -0
  152. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_memory_store_extended.py +0 -0
  153. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_metrics_extended.py +0 -0
  154. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_observability.py +0 -0
  155. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_ownership_scope.py +0 -0
  156. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_project_bridge.py +0 -0
  157. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_project_resources.py +0 -0
  158. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_quota.py +0 -0
  159. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_quota_usage.py +0 -0
  160. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_rules_refresh.py +0 -0
  161. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_secrets.py +0 -0
  162. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_status_checker.py +0 -0
  163. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_targets.py +0 -0
  164. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_tool_memory.py +0 -0
  165. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_transcript_reader.py +0 -0
  166. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_transcript_records.py +0 -0
  167. {agentihooks-2.13.2 → agentihooks-2.13.4}/tests/test_updater.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agentihooks
3
- Version: 2.13.2
3
+ Version: 2.13.4
4
4
  Summary: Hook system and MCP tool server for Claude Code agents
5
5
  Author: The Cloud Clockwork
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agentihooks
3
- Version: 2.13.2
3
+ Version: 2.13.4
4
4
  Summary: Hook system and MCP tool server for Claude Code agents
5
5
  Author: The Cloud Clockwork
6
6
  License: MIT
@@ -1,10 +1,89 @@
1
1
  """CLAUDE.md sanity check — block writes/edits that would bloat CLAUDE.md past a line limit."""
2
2
 
3
+ import re
3
4
  from pathlib import Path
4
5
 
5
- from hooks.config import CLAUDE_MD_MAXLINES
6
+ from hooks._redis import SESSION_TTL, get_redis, redis_key
7
+ from hooks.common import log
8
+ from hooks.config import AGENTIHOOKS_HOME, CLAUDE_MD_MAXLINES
6
9
  from hooks.hook_manager import BlockAction
7
10
 
11
+ _LIMIT_TYPE = "claude_md_max_lines"
12
+ _LIMIT_DIR = AGENTIHOOKS_HOME / "claude_md_limits"
13
+ _LIMIT_SIGNAL = re.compile(r"(?<![\w-])claude-md-max-lines=([1-9]\d*)(?![\w-])", re.IGNORECASE)
14
+
15
+
16
+ def _limit_key(session_id: str) -> str:
17
+ return redis_key(_LIMIT_TYPE, session_id)
18
+
19
+
20
+ def _limit_flag(session_id: str) -> Path:
21
+ return _LIMIT_DIR / f"{session_id}.limit"
22
+
23
+
24
+ def parse_max_lines_signal(text: str) -> int | None:
25
+ matches = _LIMIT_SIGNAL.findall(text or "")
26
+ if not matches:
27
+ return None
28
+ try:
29
+ return int(matches[-1])
30
+ except ValueError:
31
+ return None
32
+
33
+
34
+ def set_session_max_lines(session_id: str, max_lines: int) -> int | None:
35
+ if not session_id or max_lines < CLAUDE_MD_MAXLINES:
36
+ return None
37
+ value = str(max_lines)
38
+ r = get_redis()
39
+ if r:
40
+ try:
41
+ r.setex(_limit_key(session_id), SESSION_TTL, value)
42
+ except Exception as e:
43
+ log("claude_md_sanity.set redis failed", {"error": str(e)})
44
+ try:
45
+ flag = _limit_flag(session_id)
46
+ flag.parent.mkdir(parents=True, exist_ok=True)
47
+ flag.write_text(value)
48
+ except Exception as e:
49
+ log("claude_md_sanity.set file failed", {"error": str(e)})
50
+ return max_lines
51
+
52
+
53
+ def get_max_lines(session_id: str) -> int:
54
+ if not session_id:
55
+ return CLAUDE_MD_MAXLINES
56
+ value = None
57
+ r = get_redis()
58
+ if r:
59
+ try:
60
+ value = r.get(_limit_key(session_id))
61
+ except Exception:
62
+ pass
63
+ if value is None:
64
+ try:
65
+ flag = _limit_flag(session_id)
66
+ value = flag.read_text().strip() if flag.is_file() else None
67
+ except Exception:
68
+ pass
69
+ try:
70
+ return max(CLAUDE_MD_MAXLINES, int(value)) if value is not None else CLAUDE_MD_MAXLINES
71
+ except (TypeError, ValueError):
72
+ return CLAUDE_MD_MAXLINES
73
+
74
+
75
+ def clear_session_max_lines(session_id: str) -> None:
76
+ r = get_redis()
77
+ if r:
78
+ try:
79
+ r.delete(_limit_key(session_id))
80
+ except Exception:
81
+ pass
82
+ try:
83
+ _limit_flag(session_id).unlink(missing_ok=True)
84
+ except Exception:
85
+ pass
86
+
8
87
 
9
88
  def check_claude_md_write(payload: dict) -> None:
10
89
  """Block Write/Edit operations that would push a CLAUDE.md file past the max line limit.
@@ -20,7 +99,7 @@ def check_claude_md_write(payload: dict) -> None:
20
99
  if not file_path or Path(file_path).name not in PROTECTED_NAMES:
21
100
  return
22
101
 
23
- max_lines = CLAUDE_MD_MAXLINES
102
+ max_lines = get_max_lines(payload.get("session_id", ""))
24
103
 
25
104
  if tool_name == "Write":
26
105
  content = tool_input.get("content", "")
@@ -628,6 +628,13 @@ def on_session_end(payload: dict) -> None:
628
628
  except Exception:
629
629
  pass
630
630
 
631
+ try:
632
+ from hooks.context.claude_md_sanity import clear_session_max_lines
633
+
634
+ clear_session_max_lines(session_id)
635
+ except Exception:
636
+ pass
637
+
631
638
  # --- Broadcast: deregister session + prune dead peers ---
632
639
  from hooks.config import BROADCAST_ENABLED
633
640
 
@@ -679,6 +686,22 @@ def on_user_prompt_submit(payload: dict) -> None:
679
686
  else:
680
687
  log("Secrets scanning skipped (mode=off)")
681
688
 
689
+ try:
690
+ from hooks.context.claude_md_sanity import parse_max_lines_signal, set_session_max_lines
691
+
692
+ requested_max_lines = parse_max_lines_signal(payload.get("prompt", ""))
693
+ if requested_max_lines is not None:
694
+ active_max_lines = set_session_max_lines(session_id, requested_max_lines)
695
+ if active_max_lines is not None:
696
+ from hooks.common import inject_banner
697
+
698
+ inject_banner(
699
+ "CLAUDE.MD CAP",
700
+ f"CLAUDE.md line cap raised to {active_max_lines} for this session.",
701
+ )
702
+ except Exception as e:
703
+ log("claude_md_sanity signal detection failed", {"error": str(e)})
704
+
682
705
  try:
683
706
  from hooks.common import inject_context as _inject_enforcements
684
707
  from hooks.context.enforcement import get_user_prompt_enforcements
@@ -0,0 +1,73 @@
1
+ ---
2
+ name: run-claude-terminal
3
+ description: >
4
+ Open a smart, quota-routed Claude Code session in a new terminal on WSL,
5
+ macOS, or native Linux. Accept a target directory, session name, opening
6
+ prompt, and arbitrary Claude flags including --resume, --fork-session, and
7
+ --model. Use when the operator says "run claude terminal",
8
+ "run-claude-terminal", "open a routed Claude session", or asks an agent to
9
+ launch Claude in another terminal with a specific model or resumed session.
10
+ argument-hint: "[--dir PATH] [--name NAME] [--prompt-file PATH] [-- <claude flags>]"
11
+ ---
12
+
13
+ # Run Claude Terminal
14
+
15
+ `agentihooks claude-terminal` owns host detection, terminal quoting, OAuth
16
+ account routing, and the start check. Never build a `wt.exe`, `osascript`, or
17
+ terminal command by hand.
18
+
19
+ ## 1. Map the request
20
+
21
+ | Operator says | Launcher argument |
22
+ |---|---|
23
+ | a directory ("in ~/dev/foo", "at /repo") | `--dir <path>` — absolute, `~/…`, or relative to `$RUN_CLAUDE_BASE_DIR` → `~/dev` → `$HOME` |
24
+ | a session or tab name | `--name <name>` — default `s-YYMMDD-HHMMSS` |
25
+ | an opening prompt or task | `--prompt-file <file>` (see below) |
26
+ | a model ("use opus", "on fable") | `-- --model <name>` |
27
+ | resume a session | `-- --resume <session-id>` |
28
+ | fork a session | `-- --resume <session-id> --fork-session` |
29
+ | any other Claude flag | after `--`, unchanged |
30
+
31
+ Rules:
32
+
33
+ - Launcher options (`--dir`, `--name`, `--prompt-file`, `--dry-run`,
34
+ `--start-timeout`) go before `--`. Everything after `--` reaches Claude
35
+ verbatim; put nothing else there.
36
+ - Write every opening prompt to a file with a quoted heredoc and pass
37
+ `--prompt-file`. The quoted heredoc keeps apostrophes, quotes, `$`, and
38
+ newlines literal; an inline `--prompt` breaks on them.
39
+ - Do not guess a session ID. When the operator names a session without an ID,
40
+ ask for it.
41
+
42
+ ## 2. Launch
43
+
44
+ ```bash
45
+ cat > "<scratch-dir>/opening-prompt.md" <<'PROMPT_EOF'
46
+ <opening prompt, verbatim>
47
+ PROMPT_EOF
48
+
49
+ agentihooks claude-terminal \
50
+ --dir "<directory>" \
51
+ --name "<name>" \
52
+ --prompt-file "<scratch-dir>/opening-prompt.md" \
53
+ -- <claude-flags>
54
+ ```
55
+
56
+ Omit any option the request does not supply. Add `--dry-run` before `--` to
57
+ print the resolved launch without opening a terminal.
58
+
59
+ The command selects the healthiest `AH_CC_TOKEN_*` account inside the new
60
+ terminal. Fable models include the separate Fable quota. With no configured or
61
+ eligible account, the terminal launches bare Claude with its existing direct,
62
+ keychain, or provider authentication.
63
+
64
+ ## 3. Complete
65
+
66
+ The command waits until the new terminal has started the launcher, then prints
67
+ `key=value` lines ending in `status=started` and exits zero. That is the
68
+ completion criterion; report `directory`, `name`, `host`, and `claude_args`
69
+ from its output.
70
+
71
+ On a non-zero exit, report stderr verbatim. A start timeout discards the
72
+ launcher, so a late terminal cannot open a second session; relaunch at most
73
+ once, and only after the cause is fixed.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "agentihooks"
3
- version = "2.13.2"
3
+ version = "2.13.4"
4
4
  description = "Hook system and MCP tool server for Claude Code agents"
5
5
  readme = "README.md"
6
6
  license = {text = "MIT"}
@@ -46,6 +46,10 @@ def _runtime_dir(environ: dict[str, str]) -> Path:
46
46
  return root
47
47
 
48
48
 
49
+ def _started_marker(launcher: Path) -> Path:
50
+ return launcher.with_suffix(".started")
51
+
52
+
49
53
  def _write_launcher(
50
54
  directory: Path,
51
55
  name: str,
@@ -81,6 +85,7 @@ def _write_launcher(
81
85
  "#!/usr/bin/env bash\n"
82
86
  "set -u\n"
83
87
  f"cd {shlex.quote(str(directory))} || exit 1\n"
88
+ f": > {shlex.quote(str(_started_marker(launcher)))}\n"
84
89
  f"{command_text}\n"
85
90
  f"rm -f {shlex.join(cleanup)}\n"
86
91
  f"exec {shlex.quote(shell)} -l\n",
@@ -123,24 +128,25 @@ def _launch_command(
123
128
  return "macos", [osascript, "-e", f'tell application "Terminal" to do script "{escaped}"']
124
129
  if system == "Linux" and _is_wsl(environ):
125
130
  wt = shutil.which("wt.exe")
126
- powershell = shutil.which("powershell.exe")
127
- if not wt or not powershell:
128
- raise RuntimeError("WSL interop requires wt.exe and powershell.exe")
131
+ if not wt or not shutil.which("wsl.exe"):
132
+ raise RuntimeError("WSL interop requires wt.exe and wsl.exe on PATH")
133
+ if ";" in str(launcher):
134
+ raise RuntimeError(f"launcher path contains ';', which wt.exe splits on: {launcher}")
129
135
  distro = environ.get("WSL_DISTRO_NAME", "")
130
- wsl_call = ["wsl.exe", *(["-d", distro] if distro else []), "--", "bash", "-lc", str(launcher)]
131
- ps_command = "& { " + " ".join("'" + value.replace("'", "''") + "'" for value in wsl_call) + " }"
136
+ # wt.exe is a Windows process: the program must be a Windows-resolvable name, never a /mnt/c path.
132
137
  return "wsl", [
133
138
  wt,
134
139
  "-w",
135
140
  "0",
136
141
  "new-tab",
137
142
  "--title",
138
- title,
139
- powershell,
140
- "-NoExit",
141
- "-NoProfile",
142
- "-Command",
143
- ps_command,
143
+ title.replace(";", "_"),
144
+ "wsl.exe",
145
+ *(["-d", distro] if distro else []),
146
+ "--",
147
+ "bash",
148
+ "-lc",
149
+ str(launcher),
144
150
  ]
145
151
  if system == "Linux":
146
152
  if not (environ.get("DISPLAY") or environ.get("WAYLAND_DISPLAY")):
@@ -163,6 +169,12 @@ def _parser() -> argparse.ArgumentParser:
163
169
  prompt.add_argument("--prompt", default="", help="Opening Claude prompt")
164
170
  prompt.add_argument("--prompt-file", default="", help="Read the opening prompt from this file")
165
171
  parser.add_argument("--dry-run", action="store_true", help="Print the launch without opening a terminal")
172
+ parser.add_argument(
173
+ "--start-timeout",
174
+ type=float,
175
+ default=30.0,
176
+ help="Seconds to wait for the new terminal to start the launcher before failing",
177
+ )
166
178
  parser.add_argument("claude_args", nargs=argparse.REMAINDER, help="Arguments after -- pass through to Claude")
167
179
  return parser
168
180
 
@@ -181,24 +193,44 @@ def main(argv: list[str] | None = None, environ: dict[str, str] | None = None) -
181
193
  print(f"agentihooks claude-terminal: {exc}", file=sys.stderr)
182
194
  return 2
183
195
 
196
+ report = [
197
+ f"host={host}",
198
+ f"directory={directory}",
199
+ f"name={name}",
200
+ f"claude_args={shlex.join(claude_args)}",
201
+ f"launcher={launcher}",
202
+ f"prompt_file={prompt_file or 'none'}",
203
+ f"command={shlex.join(command)}",
204
+ ]
184
205
  if args.dry_run:
185
- print(f"host={host}")
186
- print(f"directory={directory}")
187
- print(f"name={name}")
188
- print(f"launcher={launcher}")
189
- print(f"prompt_file={prompt_file or 'none'}")
190
- print(f"command={shlex.join(command)}")
206
+ print("\n".join([*report, "status=dry-run"]))
191
207
  return 0
192
208
 
193
- try:
194
- subprocess.Popen(command, env=active_env, start_new_session=True)
195
- except OSError as exc:
209
+ def discard() -> None:
196
210
  launcher.unlink(missing_ok=True)
197
211
  if prompt_file is not None:
198
212
  prompt_file.unlink(missing_ok=True)
213
+
214
+ marker = _started_marker(launcher)
215
+ try:
216
+ subprocess.Popen(command, env=active_env, start_new_session=True)
217
+ except OSError as exc:
218
+ discard()
199
219
  print(f"agentihooks claude-terminal: {exc}", file=sys.stderr)
200
220
  return 2
201
- print(f"Opened routed Claude terminal '{name}' at {directory} ({host})")
221
+ deadline = time.monotonic() + args.start_timeout
222
+ while not marker.exists():
223
+ if time.monotonic() >= deadline:
224
+ discard()
225
+ print(
226
+ f"agentihooks claude-terminal: the new terminal did not start the launcher within "
227
+ f"{args.start_timeout:g}s; launch discarded\ncommand={shlex.join(command)}",
228
+ file=sys.stderr,
229
+ )
230
+ return 2
231
+ time.sleep(0.25)
232
+ marker.unlink(missing_ok=True)
233
+ print("\n".join([*report, "status=started"]))
202
234
  return 0
203
235
 
204
236
 
@@ -2027,7 +2027,7 @@ def cmd_init_unified(args: argparse.Namespace) -> None:
2027
2027
 
2028
2028
  agentihooks init --bundle <path> → link bundle + global install
2029
2029
  agentihooks init → re-run global install (bundle must be linked)
2030
- agentihooks init --force → clean install (wipe state, re-init from scratch)
2030
+ agentihooks init --force → clean reinstall; keeps the bundle link, linked profiles and profile chain
2031
2031
  """
2032
2032
  if getattr(args, "dry_run", False):
2033
2033
  # The flag was accepted by argparse and read nowhere, so `init --dry-run`
@@ -2047,8 +2047,13 @@ def cmd_init_unified(args: argparse.Namespace) -> None:
2047
2047
  )
2048
2048
  sys.exit(2)
2049
2049
 
2050
+ _kept_state: dict = {}
2050
2051
  if getattr(args, "force", False):
2052
+ _kept_state = _load_state()
2051
2053
  _clean_state_dir()
2054
+ _identity = {k: _kept_state[k] for k in ("bundle", "linked_profiles") if _kept_state.get(k)}
2055
+ if _identity:
2056
+ _save_state(_identity)
2052
2057
  bundle_path = getattr(args, "bundle", None)
2053
2058
 
2054
2059
  # Global mode
@@ -2129,11 +2134,10 @@ def cmd_init_unified(args: argparse.Namespace) -> None:
2129
2134
  # default target's chain so `init --target codex` on a claude machine
2130
2135
  # installs the same persona instead of falling to 'default'.
2131
2136
  _prev_global = _global_record(_prev_state, install_target) or _global_record(_prev_state)
2137
+ if _is_force:
2138
+ _prev_global = _global_record(_kept_state, install_target) or _global_record(_kept_state)
2132
2139
  if not profile_name:
2133
- if _is_force:
2134
- # --force = fresh install, ignore stored profile
2135
- profile_name = "default"
2136
- elif _prev_global.get("profile", ""):
2140
+ if _prev_global.get("profile", ""):
2137
2141
  profile_name = _prev_global["profile"]
2138
2142
  print(f"Using profile from previous install: {profile_name}")
2139
2143
  elif sys.stdin.isatty():
@@ -2187,7 +2191,7 @@ def cmd_init_unified(args: argparse.Namespace) -> None:
2187
2191
  f"was built from profile '{_marker_profile}'.\n"
2188
2192
  f"Refusing to overwrite it with 'default'. Re-run with the intended profile:\n"
2189
2193
  f" agentihooks init --profile {_marker_profile}\n"
2190
- f"or force a clean default install with: agentihooks init --force",
2194
+ f"or install the default profile explicitly with: agentihooks init --profile default",
2191
2195
  file=sys.stderr,
2192
2196
  )
2193
2197
  sys.exit(1)
@@ -2196,7 +2200,7 @@ def cmd_init_unified(args: argparse.Namespace) -> None:
2196
2200
  settings_profile = getattr(args, "settings_profile", None)
2197
2201
  if not settings_profile:
2198
2202
  settings_profile = os.environ.get("AGENTIHOOKS_SETTINGS_PROFILE", "")
2199
- if not settings_profile and not _is_force:
2203
+ if not settings_profile:
2200
2204
  settings_profile = _prev_global.get("settings_profile", "")
2201
2205
 
2202
2206
  # Build args for _install_global_inner
@@ -6051,7 +6055,7 @@ def main() -> None:
6051
6055
  "--force",
6052
6056
  action="store_true",
6053
6057
  default=False,
6054
- help="Clean install — wipe ~/.agentihooks/ (except .env) and re-init from scratch",
6058
+ help="Clean reinstall — reset install state and caches; keeps the bundle link, linked profiles and profile chain",
6055
6059
  )
6056
6060
  init_p.add_argument(
6057
6061
  "--settings-profile",
@@ -0,0 +1,211 @@
1
+ """Tests for CLAUDE.md sanity check guardrail."""
2
+
3
+ import json
4
+ import os
5
+ import subprocess
6
+ import sys
7
+ from unittest.mock import patch
8
+
9
+ import pytest
10
+
11
+ from hooks.hook_manager import BlockAction
12
+
13
+
14
+ @pytest.fixture(autouse=True)
15
+ def _disable_redis():
16
+ with patch("hooks._redis.get_redis", return_value=None):
17
+ yield
18
+
19
+
20
+ class TestClaudeMdSanity:
21
+ """Tests for hooks.context.claude_md_sanity.check_claude_md_write."""
22
+
23
+ def test_skip_non_claude_md(self):
24
+ from hooks.context.claude_md_sanity import check_claude_md_write
25
+
26
+ payload = {
27
+ "tool_name": "Write",
28
+ "tool_input": {"file_path": "/tmp/README.md", "content": "x\n" * 999},
29
+ }
30
+ check_claude_md_write(payload) # should not raise
31
+
32
+ def test_write_under_limit(self):
33
+ from hooks.context.claude_md_sanity import check_claude_md_write
34
+
35
+ with patch("hooks.context.claude_md_sanity.CLAUDE_MD_MAXLINES", 400):
36
+ payload = {
37
+ "tool_name": "Write",
38
+ "tool_input": {"file_path": "/tmp/CLAUDE.md", "content": "line\n" * 100},
39
+ }
40
+ check_claude_md_write(payload)
41
+
42
+ def test_write_over_limit_blocked(self):
43
+ from hooks.context.claude_md_sanity import check_claude_md_write
44
+
45
+ with patch("hooks.context.claude_md_sanity.CLAUDE_MD_MAXLINES", 200):
46
+ payload = {
47
+ "tool_name": "Write",
48
+ "tool_input": {"file_path": "/tmp/CLAUDE.md", "content": "line\n" * 500},
49
+ }
50
+ with pytest.raises(BlockAction, match="500 lines"):
51
+ check_claude_md_write(payload)
52
+
53
+ def test_edit_over_limit_blocked(self, tmp_path):
54
+ from hooks.context.claude_md_sanity import check_claude_md_write
55
+
56
+ claude_md = tmp_path / "CLAUDE.md"
57
+ claude_md.write_text("line\n" * 190)
58
+
59
+ with patch("hooks.context.claude_md_sanity.CLAUDE_MD_MAXLINES", 200):
60
+ payload = {
61
+ "tool_name": "Edit",
62
+ "tool_input": {
63
+ "file_path": str(claude_md),
64
+ "old_string": "line\n",
65
+ "new_string": "line\n" * 50,
66
+ },
67
+ }
68
+ with pytest.raises(BlockAction):
69
+ check_claude_md_write(payload)
70
+
71
+ def test_edit_under_limit(self, tmp_path):
72
+ from hooks.context.claude_md_sanity import check_claude_md_write
73
+
74
+ claude_md = tmp_path / "CLAUDE.md"
75
+ claude_md.write_text("old\nline\n" * 10)
76
+
77
+ with patch("hooks.context.claude_md_sanity.CLAUDE_MD_MAXLINES", 400):
78
+ payload = {
79
+ "tool_name": "Edit",
80
+ "tool_input": {
81
+ "file_path": str(claude_md),
82
+ "old_string": "old\n",
83
+ "new_string": "new\n",
84
+ },
85
+ }
86
+ check_claude_md_write(payload)
87
+
88
+ def test_nested_claude_md_path(self):
89
+ from hooks.context.claude_md_sanity import check_claude_md_write
90
+
91
+ with patch("hooks.context.claude_md_sanity.CLAUDE_MD_MAXLINES", 10):
92
+ payload = {
93
+ "tool_name": "Write",
94
+ "tool_input": {
95
+ "file_path": "/home/user/project/.claude/CLAUDE.md",
96
+ "content": "x\n" * 50,
97
+ },
98
+ }
99
+ with pytest.raises(BlockAction):
100
+ check_claude_md_write(payload)
101
+
102
+ def test_message_signal_raises_session_limit(self, tmp_path):
103
+ from hooks.context import claude_md_sanity
104
+
105
+ with (
106
+ patch.object(claude_md_sanity, "_LIMIT_DIR", tmp_path),
107
+ patch.object(claude_md_sanity, "CLAUDE_MD_MAXLINES", 200),
108
+ ):
109
+ requested = claude_md_sanity.parse_max_lines_signal(
110
+ "Update the project claude-md-max-lines=600 and continue"
111
+ )
112
+ assert requested == 600
113
+ assert claude_md_sanity.set_session_max_lines("session-1", requested) == 600
114
+
115
+ payload = {
116
+ "session_id": "session-1",
117
+ "tool_name": "Write",
118
+ "tool_input": {"file_path": "/tmp/CLAUDE.md", "content": "line\n" * 600},
119
+ }
120
+ claude_md_sanity.check_claude_md_write(payload)
121
+
122
+ payload["tool_input"]["content"] += "line\n"
123
+ with pytest.raises(BlockAction, match="cap of 600 lines"):
124
+ claude_md_sanity.check_claude_md_write(payload)
125
+
126
+ def test_signal_only_raises_baseline(self, tmp_path):
127
+ from hooks.context import claude_md_sanity
128
+
129
+ with (
130
+ patch.object(claude_md_sanity, "_LIMIT_DIR", tmp_path),
131
+ patch.object(claude_md_sanity, "CLAUDE_MD_MAXLINES", 200),
132
+ ):
133
+ assert claude_md_sanity.set_session_max_lines("session-1", 100) is None
134
+ assert claude_md_sanity.get_max_lines("session-1") == 200
135
+
136
+ def test_last_message_signal_wins_and_session_end_clears_it(self, tmp_path):
137
+ from hooks.context import claude_md_sanity
138
+
139
+ with (
140
+ patch.object(claude_md_sanity, "_LIMIT_DIR", tmp_path),
141
+ patch.object(claude_md_sanity, "CLAUDE_MD_MAXLINES", 200),
142
+ ):
143
+ requested = claude_md_sanity.parse_max_lines_signal("claude-md-max-lines=400 then claude-md-max-lines=750")
144
+ assert requested == 750
145
+ claude_md_sanity.set_session_max_lines("session-1", requested)
146
+ assert claude_md_sanity.get_max_lines("session-1") == 750
147
+ claude_md_sanity.clear_session_max_lines("session-1")
148
+ assert claude_md_sanity.get_max_lines("session-1") == 200
149
+
150
+ @pytest.mark.parametrize(
151
+ "text",
152
+ [
153
+ "claude-md-max-lines=0",
154
+ "claude-md-max-lines=-1",
155
+ "xclaude-md-max-lines=600",
156
+ "claude-md-max-lines=600x",
157
+ ],
158
+ )
159
+ def test_invalid_message_signals_are_ignored(self, text):
160
+ from hooks.context.claude_md_sanity import parse_max_lines_signal
161
+
162
+ assert parse_max_lines_signal(text) is None
163
+
164
+ def test_message_signal_crosses_hook_processes_and_clears_on_session_end(self, tmp_path):
165
+ env = {
166
+ **os.environ,
167
+ "AGENTIHOOKS_HOME": str(tmp_path),
168
+ "HOME": str(tmp_path),
169
+ "REDIS_URL": "",
170
+ "AGENTIHOOKS_DISABLE_BYPASS_LOOKUP": "1",
171
+ "AGENTIHOOKS_SECRETS_MODE": "off",
172
+ }
173
+
174
+ def run(payload):
175
+ return subprocess.run(
176
+ [sys.executable, "-m", "hooks"],
177
+ input=json.dumps(payload),
178
+ capture_output=True,
179
+ text=True,
180
+ env=env,
181
+ )
182
+
183
+ session_id = "claude-md-cap-integration"
184
+ result = run(
185
+ {
186
+ "hook_event_name": "UserPromptSubmit",
187
+ "session_id": session_id,
188
+ "prompt": "Continue with claude-md-max-lines=600",
189
+ }
190
+ )
191
+ assert result.returncode == 0
192
+ assert "CLAUDE.md line cap raised to 600 for this session." in result.stdout
193
+
194
+ payload = {
195
+ "hook_event_name": "PreToolUse",
196
+ "session_id": session_id,
197
+ "tool_name": "Write",
198
+ "tool_input": {"file_path": "/tmp/CLAUDE.md", "content": "line\n" * 600},
199
+ }
200
+ assert run(payload).returncode == 0
201
+
202
+ payload["tool_input"]["content"] += "line\n"
203
+ blocked = run(payload)
204
+ assert blocked.returncode == 2
205
+ assert "cap of 600 lines" in blocked.stderr
206
+
207
+ assert run({"hook_event_name": "SessionEnd", "session_id": session_id}).returncode == 0
208
+ payload["tool_input"]["content"] = "line\n" * 201
209
+ reset = run(payload)
210
+ assert reset.returncode == 2
211
+ assert "cap of 200 lines" in reset.stderr