-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge_hooks_impl.py
More file actions
1057 lines (836 loc) · 37.5 KB
/
forge_hooks_impl.py
File metadata and controls
1057 lines (836 loc) · 37.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Nova Forge hook implementations — Python ports of V11's 12 shell hooks.
Each function follows the HookSystem signature::
def hook_fn(tool_name: str, args: dict, result: str | None) -> HookResult
Pre-tool hooks return HookResult(blocked=True) to block an operation.
Post-tool hooks are informational (blocked is advisory only).
Registration is handled by :func:`wire_all_hooks`.
Usage::
from forge_hooks import HookSystem, HookEvent
from forge_hooks_impl import wire_all_hooks
hs = HookSystem()
wire_all_hooks(hs, project_root=Path("."))
"""
from __future__ import annotations
import fnmatch
import json
import logging
import os
import re
import shutil
import subprocess
from datetime import datetime, timezone, timedelta
from dataclasses import dataclass, field
from pathlib import Path
from typing import Optional
from filelock import FileLock
from config import (
ForgeProject,
detect_project,
is_metadata_file,
FORGE_DIR_NAME,
NON_PROJECTS,
)
from forge_hooks import HookResult, HookEvent, HookSystem
from forge_guards import RiskClassifier, RiskLevel, AutonomyManager, SyntaxVerifier
logger = logging.getLogger(__name__)
# ── Tool name normalization ──────────────────────────────────────────────────
# ForgeAgent uses lowercase names (write_file, edit_file, bash, read_file, ...)
# V11 hooks use capitalized names (Write, Edit, Bash, Read, ...).
# Normalize ForgeAgent names to V11 names so hooks match correctly.
_TOOL_NAME_MAP: dict[str, str] = {
"write_file": "Write",
"append_file": "Write",
"edit_file": "Edit",
"read_file": "Read",
"bash": "Bash",
"glob_files": "Glob",
"grep": "Grep",
}
def _normalize_tool_name(tool_name: str) -> str:
"""Map ForgeAgent tool names to V11 hook names."""
return _TOOL_NAME_MAP.get(tool_name, tool_name)
# ── Shared state for hooks within a session ──────────────────────────────────
@dataclass
class _HookState:
"""Mutable state shared across hooks during a session."""
active_project: Optional[str] = None
project_root: Optional[Path] = None
session_writes: int = 0
files_modified: set = field(default_factory=set)
risk_classifier: RiskClassifier = field(default_factory=RiskClassifier)
syntax_verifier: Optional[SyntaxVerifier] = None
autonomy_manager: Optional[AutonomyManager] = None
task_state: Optional[dict] = None
_task_state_loaded: bool = False
# ── 1. detect-project (PreToolUse, any tool) ────────────────────────────────
def _detect_project_hook(state: _HookState) -> callable:
"""Create detect-project hook bound to shared state."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
# Extract file path from common tool input patterns
file_path = (
args.get("file_path")
or args.get("path")
or args.get("command", "") # Bash commands may reference files
)
if not file_path or not isinstance(file_path, str):
return HookResult()
# Skip metadata files
if is_metadata_file(file_path):
return HookResult()
project = detect_project(file_path)
if project and project != state.active_project:
state.active_project = project
logger.debug("detect-project: active project → %s", project)
return HookResult()
return hook
# ── 2. guard-write-gates (PreToolUse, Write/Edit) ───────────────────────────
def _guard_write_gates_hook(state: _HookState) -> callable:
"""Enforce task-state requirement before writes."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
if tool_name not in ("Write", "Edit"):
return HookResult()
file_path = args.get("file_path", "")
# Detect project from file path
project = detect_project(file_path) if file_path else state.active_project
if project:
state.active_project = project
if not state.active_project:
return HookResult()
# Part A: File ownership check (if formation registry exists)
ownership_result = _check_file_ownership(state, file_path, args)
if ownership_result.blocked:
return ownership_result
# Part B: Task state enforcement
# Only block writes when there are pending tasks waiting to be started
# (i.e., a build should be running). Allow writes when:
# - All tasks are complete (post-build chat editing)
# - Only failed tasks remain (user manually fixing)
# - No tasks exist yet
task_state = _load_task_state(state)
if task_state:
total = task_state.get("total", 0)
pending = task_state.get("pending", 0)
in_progress = task_state.get("in_progress", 0)
if total >= 2 and in_progress == 0 and pending > 0:
return HookResult(
blocked=True,
reason=(
f"No tasks in_progress for project '{state.active_project}'. "
"Start a task with TaskUpdate(status='in_progress') before writing."
),
)
# Part C: Track writes for plan-mode advisory
state.session_writes += 1
state.files_modified.add(file_path)
if len(state.files_modified) >= 6 and state.session_writes % 10 == 0:
logger.info(
"guard-write-gates advisory: %d files modified in session — "
"consider entering plan mode for large changes",
len(state.files_modified),
)
return HookResult()
return hook
# ── 3. guard-enforcement (PreToolUse, Write/Edit/Bash) ──────────────────────
def _guard_enforcement_hook(state: _HookState) -> callable:
"""Block high-risk operations and enforce tool policies."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
if tool_name not in ("Write", "Edit", "Bash"):
return HookResult()
command = args.get("command", "")
file_path = args.get("file_path", "")
# Part A: High-risk blocking (Bash only)
if tool_name == "Bash" and command:
risk = state.risk_classifier.classify("Bash", command=command)
if risk == RiskLevel.HIGH:
# Check autonomy — A4 may auto-approve some high-risk
if state.autonomy_manager:
auto_result = state.autonomy_manager.check(
tool_name="Bash", risk_level=risk,
file_path="", command=command,
)
if auto_result.allowed:
return HookResult()
return HookResult(
blocked=True,
reason=f"High-risk command blocked: {command[:100]}",
)
# Part B: Tool policy cascade (formation teams)
policy_result = _check_tool_policy(state, tool_name)
if policy_result.blocked:
return policy_result
return HookResult()
return hook
# ── 4. guard-effort (PreToolUse, agent launches — advisory) ─────────────────
# Effort keyword patterns
_EFFORT_KEYWORDS = {
"max": re.compile(
r"\b(?:design|architect|security|threat|novel|complex|scalab)", re.I
),
"high": re.compile(
r"\b(?:implement|fix|refactor|add|review|build|write|create|modify|debug)\b", re.I
),
"medium": re.compile(
r"\b(?:test|coordinate|validate|optimize|integrate)\b", re.I
),
"low": re.compile(
r"\b(?:format|lint|scan|check|list|count|find|read)\b", re.I
),
}
# Complexity × scope → effort matrix
_EFFORT_MATRIX = {
("novel", "large"): "max",
("novel", "medium"): "high",
("novel", "small"): "high",
("complex", "large"): "max",
("complex", "medium"): "high",
("complex", "small"): "medium",
("medium", "large"): "high",
("medium", "medium"): "medium",
("medium", "small"): "low",
("routine", "large"): "high",
("routine", "medium"): "medium",
("routine", "small"): "low",
}
def _guard_effort_hook(state: _HookState) -> callable:
"""Advisory effort-level guidance for agent tasks."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
# Only advise on agent/task launches
prompt = args.get("prompt", args.get("description", ""))
if not prompt:
return HookResult()
# Keyword analysis
keyword_effort = "medium" # default
for level in ("max", "high", "medium", "low"):
if _EFFORT_KEYWORDS[level].search(prompt):
keyword_effort = level
break
# Metadata-based routing
metadata = args.get("metadata", {})
complexity = metadata.get("complexity", "")
scope = metadata.get("scope", "")
matrix_effort = _EFFORT_MATRIX.get((complexity, scope))
recommended = matrix_effort or keyword_effort
# Contradiction detection
risk = metadata.get("risk", "")
if complexity == "routine" and risk in ("medium", "high"):
logger.info(
"guard-effort advisory: routine task has %s risk — verify classification",
risk,
)
if complexity == "novel" and risk == "low":
logger.info(
"guard-effort advisory: novel task has low risk — verify classification"
)
logger.debug(
"guard-effort: recommended effort=%s (keyword=%s, matrix=%s)",
recommended, keyword_effort, matrix_effort,
)
return HookResult()
return hook
# ── 5. enforce-test-coverage (PreToolUse, Bash deploy commands) ─────────────
_DEPLOY_PATTERNS = re.compile(
r"docker[\s-]+compose\s+up|"
r"kubectl\s+apply|"
r"./deploy\.sh|"
r"forge\s+deploy|"
r"docker\s+build\s+.*-t|"
r"npm\s+run\s+deploy",
re.IGNORECASE,
)
# Test framework detection
_TEST_FRAMEWORKS = {
"pytest": {
"markers": ["pytest.ini", "pyproject.toml", "setup.cfg"],
"command": "python3 -m pytest --tb=no -q 2>&1 | tail -1",
},
"jest": {
"markers": ["jest.config.js", "jest.config.ts"],
"command": "npx jest --ci --coverage 2>&1 | tail -5",
},
"go": {
"markers": ["go.mod"],
"command": "go test -cover ./... 2>&1 | tail -1",
},
}
def _enforce_test_coverage_hook(state: _HookState) -> callable:
"""Block deploys if test coverage is below threshold."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
if tool_name != "Bash":
return HookResult()
command = args.get("command", "")
if not _DEPLOY_PATTERNS.search(command):
return HookResult()
# Detect test framework
project_root = state.project_root or Path.cwd()
framework = _detect_test_framework(project_root)
if not framework:
logger.debug("enforce-test-coverage: no test framework detected — skipping")
return HookResult()
# Run test coverage check
threshold = int(os.environ.get("TEST_COVERAGE_THRESHOLD", "80"))
try:
result_proc = subprocess.run(
["bash", "-c", _TEST_FRAMEWORKS[framework]["command"]],
cwd=str(project_root),
capture_output=True, text=True, timeout=120,
)
output = result_proc.stdout + result_proc.stderr
# Extract coverage percentage
coverage_match = re.search(r"(\d+)%", output)
if coverage_match:
coverage = int(coverage_match.group(1))
if coverage < threshold:
return HookResult(
blocked=True,
reason=(
f"Test coverage {coverage}% is below threshold {threshold}%. "
f"Write more tests before deploying."
),
)
logger.debug("enforce-test-coverage: %d%% >= %d%% — OK", coverage, threshold)
except (subprocess.TimeoutExpired, FileNotFoundError) as exc:
logger.warning("enforce-test-coverage: test runner failed — %s", exc)
return HookResult()
return hook
def _detect_test_framework(project_root: Path) -> Optional[str]:
"""Auto-detect test framework from config files."""
for name, cfg in _TEST_FRAMEWORKS.items():
for marker in cfg["markers"]:
if (project_root / marker).exists():
return name
return None
# ── 6. verify-syntax (PostToolUse, Write/Edit) ──────────────────────────────
# Extension → syntax check command template ({file} is replaced)
_SYNTAX_CHECKERS: dict[str, list[str]] = {
".py": ["python3", "-m", "py_compile", "{file}"],
".js": ["node", "--check", "{file}"],
".jsx": ["node", "--check", "{file}"],
".json": ["python3", "-c", "import json; json.load(open('{file}'))"],
".yml": ["python3", "-c", "import yaml; yaml.safe_load(open('{file}'))"],
".yaml": ["python3", "-c", "import yaml; yaml.safe_load(open('{file}'))"],
".sh": ["bash", "-n", "{file}"],
".bash": ["bash", "-n", "{file}"],
}
def _verify_syntax_hook(state: _HookState) -> callable:
"""Advisory syntax check after file writes."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
if tool_name not in ("Write", "Edit"):
return HookResult()
file_path = args.get("file_path", "")
if not file_path or not Path(file_path).exists():
return HookResult()
if is_metadata_file(file_path):
return HookResult()
ext = Path(file_path).suffix.lower()
checker_template = _SYNTAX_CHECKERS.get(ext)
if not checker_template:
return HookResult()
checker = [arg.replace("{file}", str(file_path)) for arg in checker_template]
try:
proc = subprocess.run(
checker, capture_output=True, text=True, timeout=10,
)
if proc.returncode != 0:
error_msg = (proc.stderr or proc.stdout).strip()[:200]
logger.warning(
"verify-syntax: %s has syntax error: %s",
Path(file_path).name, error_msg,
)
except (subprocess.TimeoutExpired, FileNotFoundError):
pass # Tool not available — skip silently
return HookResult()
return hook
# ── 7. track-autonomy (PostToolUse, Write/Edit/Bash) ────────────────────────
def _track_autonomy_hook(state: _HookState) -> callable:
"""Update autonomy trust score and append audit trail."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
if tool_name not in ("Write", "Edit", "Bash"):
return HookResult()
command = args.get("command", "")
file_path = args.get("file_path", "")
# Skip low-risk operations
risk = state.risk_classifier.classify(tool_name, command=command, file_path=file_path)
if risk == RiskLevel.LOW:
return HookResult()
if not state.active_project or not state.project_root:
return HookResult()
# Determine success/failure from result — only count severe failures,
# not normal tool errors like "old_string not found" or "File not found"
is_error = False
if result and isinstance(result, str):
is_error = bool(re.search(
r"(?:SANDBOX VIOLATION|Traceback|BLOCKED)", result
))
# Update autonomy manager
if state.autonomy_manager:
try:
state.autonomy_manager.track(
tool_name=tool_name,
risk_level=risk,
outcome="error" if is_error else "success",
)
except Exception as exc:
logger.debug("autonomy track failed: %s", exc)
# Append audit entry
_append_audit_entry(state, tool_name, args, risk, is_error)
return HookResult()
return hook
def _append_audit_entry(
state: _HookState, tool_name: str, args: dict,
risk: RiskLevel, is_error: bool,
) -> None:
"""Append JSONL audit entry to .forge/audit/audit.jsonl."""
if not state.project_root:
return
audit_dir = state.project_root / FORGE_DIR_NAME / "audit"
audit_dir.mkdir(parents=True, exist_ok=True)
audit_file = audit_dir / "audit.jsonl"
entry = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"project": state.active_project or "",
"tool": tool_name,
"file": args.get("file_path", args.get("command", ""))[:200],
"risk": str(risk),
"outcome": "error" if is_error else "success",
"autonomy_level": (
state.autonomy_manager._state.get("level", 0)
if state.autonomy_manager else 0
),
}
lock = FileLock(str(audit_file) + ".lock", timeout=2)
try:
with lock:
with open(audit_file, "a") as f:
f.write(json.dumps(entry) + "\n")
except Exception as exc:
logger.warning("track-autonomy: failed to write audit entry — %s", exc)
# ── 8. sync-tasks (PostToolUse, TaskCreate/TaskUpdate/TaskList) ─────────────
def _sync_tasks_hook(state: _HookState) -> callable:
"""Sync task state to .forge/state/ on task operations."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
# Only act on task-related tools
if tool_name not in ("TaskCreate", "TaskUpdate", "TaskList", "TaskGet"):
return HookResult()
if not state.active_project or not state.project_root:
# Try to detect from metadata
metadata = args.get("metadata", {})
if isinstance(metadata, dict):
project = metadata.get("project", "")
if project:
state.active_project = project
if not state.active_project:
return HookResult()
# Parse task info from result
task_state = _load_task_state(state)
if task_state is None:
task_state = {
"project": state.active_project,
"total": 0, "completed": 0, "pending": 0,
"in_progress": 0, "blocked": 0,
"last_updated": datetime.now(timezone.utc).isoformat(),
}
# Update counts based on tool type
if tool_name == "TaskCreate":
task_state["total"] = task_state.get("total", 0) + 1
task_state["pending"] = task_state.get("pending", 0) + 1
elif tool_name == "TaskUpdate":
new_status = args.get("status", "")
if new_status == "in_progress":
task_state["pending"] = max(0, task_state.get("pending", 0) - 1)
task_state["in_progress"] = task_state.get("in_progress", 0) + 1
elif new_status == "completed":
task_state["in_progress"] = max(0, task_state.get("in_progress", 0) - 1)
task_state["completed"] = task_state.get("completed", 0) + 1
elif new_status == "blocked":
task_state["in_progress"] = max(0, task_state.get("in_progress", 0) - 1)
task_state["blocked"] = task_state.get("blocked", 0) + 1
task_state["last_updated"] = datetime.now(timezone.utc).isoformat()
# Save task state
_save_task_state(state, task_state)
state.task_state = task_state
state._task_state_loaded = True
return HookResult()
return hook
# ── 9. guard-teammate-timeout (PostToolUse, TaskList) ────────────────────────
# Configurable thresholds (minutes)
_STALL_WARNING = int(os.environ.get("STALL_WARNING_MINUTES", "15"))
_STALL_TIMEOUT = int(os.environ.get("STALL_TIMEOUT_MINUTES", "30"))
_STALL_CRITICAL = int(os.environ.get("STALL_CRITICAL_MINUTES", "45"))
def _guard_teammate_timeout_hook(state: _HookState) -> callable:
"""Detect stalled teammates via task state timestamps."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
if tool_name != "TaskList":
return HookResult()
task_state = _load_task_state(state)
if not task_state:
return HookResult()
in_progress = task_state.get("in_progress", 0)
if in_progress == 0:
return HookResult()
# Check staleness
last_updated = task_state.get("last_updated", "")
if not last_updated:
return HookResult()
try:
last_dt = datetime.fromisoformat(last_updated)
if last_dt.tzinfo is None:
last_dt = last_dt.replace(tzinfo=timezone.utc)
elapsed = (datetime.now(timezone.utc) - last_dt).total_seconds() / 60
except (ValueError, TypeError):
return HookResult()
if elapsed >= _STALL_CRITICAL:
logger.warning(
"guard-teammate-timeout CRITICAL: %d task(s) in_progress, "
"no update for %.0f minutes — consider restarting stalled agents",
in_progress, elapsed,
)
elif elapsed >= _STALL_TIMEOUT:
logger.warning(
"guard-teammate-timeout ALERT: %d task(s) in_progress, "
"no update for %.0f minutes",
in_progress, elapsed,
)
elif elapsed >= _STALL_WARNING:
logger.info(
"guard-teammate-timeout WARNING: %d task(s) in_progress, "
"no update for %.0f minutes",
in_progress, elapsed,
)
return HookResult()
return hook
# ── 10. track-agents (PostToolUse, agent launches) ──────────────────────────
_AGENT_CATEGORIES = {
"database": re.compile(r"\b(?:database|schema|migration|sql)\b", re.I),
"backend": re.compile(r"\b(?:backend|api|endpoint|server)\b", re.I),
"frontend": re.compile(r"\b(?:frontend|react|component|ui)\b", re.I),
"testing": re.compile(r"\b(?:test|coverage|unit|integration)\b", re.I),
"deployment": re.compile(r"\b(?:deploy|production|docker)\b", re.I),
"security": re.compile(r"\b(?:security|auth|owasp)\b", re.I),
"debugging": re.compile(r"\b(?:debug|error|fix)\b", re.I),
}
def _track_agents_hook(state: _HookState) -> callable:
"""Track agent usage metrics and detect escalation patterns."""
_consecutive_failures: list[int] = [0] # mutable counter in closure
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
description = args.get("description", "")
prompt = args.get("prompt", "")
subagent_type = args.get("subagent_type", "")
if not description and not prompt and not subagent_type:
return HookResult()
# Determine success
success = "unknown"
if result and isinstance(result, str):
if re.search(r"(?:error|failed|exception|traceback)", result, re.I):
success = "false"
_consecutive_failures[0] += 1
elif re.search(r"(?:success|complete|done|finished)", result, re.I):
success = "true"
_consecutive_failures[0] = 0
else:
_consecutive_failures[0] = 0
# Detect category
text = f"{description} {prompt}"
category = "general"
for cat, pattern in _AGENT_CATEGORIES.items():
if pattern.search(text):
category = cat
break
# Log metric
entry = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"agent": subagent_type or "unknown",
"description": description[:200],
"success": success,
"category": category,
"project": state.active_project or "",
}
if state.project_root:
metrics_file = state.project_root / FORGE_DIR_NAME / "audit" / "agent-usage.jsonl"
metrics_file.parent.mkdir(parents=True, exist_ok=True)
try:
with open(metrics_file, "a") as f:
f.write(json.dumps(entry) + "\n")
except OSError:
pass
# Escalation advisory after consecutive failures
if _consecutive_failures[0] >= 2:
logger.warning(
"track-agents ESCALATION: %d consecutive agent failures — "
"try higher effort level or different model",
_consecutive_failures[0],
)
return HookResult()
return hook
# ── 11. fix-team-model (Pre+Post, TeamCreate/Agent — advisory) ──────────────
def _fix_team_model_hook(state: _HookState) -> callable:
"""Advisory check for team model configuration issues."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
# This hook is primarily relevant in Claude Code's Agent Teams
# In Nova Forge, model selection is explicit per agent launch
# We keep it as an advisory: warn if formation registry agent
# doesn't match the spawned subagent_type
subagent_type = args.get("subagent_type", "")
if not subagent_type:
return HookResult()
if not state.project_root:
return HookResult()
formation_file = state.project_root / FORGE_DIR_NAME / "state" / "formation-registry.json"
if not formation_file.exists():
return HookResult()
try:
registry = json.loads(formation_file.read_text())
teammates = registry.get("teammates", {})
for role, info in teammates.items():
expected_agent = info.get("agent", "")
if expected_agent and expected_agent != subagent_type:
# Check if this spawn is for a role that expects a different agent
logger.info(
"fix-team-model advisory: role '%s' expects agent '%s' "
"but spawning '%s'",
role, expected_agent, subagent_type,
)
except (json.JSONDecodeError, OSError):
pass
return HookResult()
return hook
# ── 12. session-end (Stop) ──────────────────────────────────────────────────
def _session_end_hook(state: _HookState) -> callable:
"""Save session summary on stop."""
def hook(tool_name: str, args: dict, result: str | None) -> HookResult:
if not state.active_project or not state.project_root:
return HookResult()
task_state = _load_task_state(state)
if not task_state:
return HookResult()
# Update last_session_end timestamp
task_state["last_session_end"] = datetime.now(timezone.utc).isoformat()
_save_task_state(state, task_state)
# Append session log
session_log = state.project_root / FORGE_DIR_NAME / "audit" / "session-log.jsonl"
session_log.parent.mkdir(parents=True, exist_ok=True)
entry = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"project": state.active_project,
"total": task_state.get("total", 0),
"completed": task_state.get("completed", 0),
"pending": task_state.get("pending", 0),
"in_progress": task_state.get("in_progress", 0),
"session_writes": state.session_writes,
"files_modified": len(state.files_modified),
}
try:
with open(session_log, "a") as f:
f.write(json.dumps(entry) + "\n")
except OSError as exc:
logger.warning("session-end: failed to write session log — %s", exc)
logger.info(
"session-end: project=%s completed=%d/%d writes=%d",
state.active_project,
task_state.get("completed", 0),
task_state.get("total", 0),
state.session_writes,
)
return HookResult()
return hook
# ── Helper functions ─────────────────────────────────────────────────────────
def _load_task_state(state: _HookState) -> Optional[dict]:
"""Load task state from .forge/state/task-state.json."""
if state._task_state_loaded and state.task_state is not None:
return state.task_state
if not state.project_root:
return None
state_file = state.project_root / FORGE_DIR_NAME / "state" / "task-state.json"
if not state_file.exists():
return None
try:
lock = FileLock(str(state_file) + ".lock", timeout=2)
with lock:
data = json.loads(state_file.read_text())
state.task_state = data
state._task_state_loaded = True
return data
except (json.JSONDecodeError, OSError, Exception) as exc:
logger.warning("Failed to load task state: %s", exc)
return None
def _save_task_state(state: _HookState, task_state: dict) -> None:
"""Save task state to .forge/state/task-state.json with file locking."""
if not state.project_root:
return
state_dir = state.project_root / FORGE_DIR_NAME / "state"
state_dir.mkdir(parents=True, exist_ok=True)
state_file = state_dir / "task-state.json"
lock = FileLock(str(state_file) + ".lock", timeout=2)
try:
with lock:
state_file.write_text(json.dumps(task_state, indent=2) + "\n")
except Exception as exc:
logger.warning("Failed to save task state: %s", exc)
def _check_file_ownership(state: _HookState, file_path: str, args: dict) -> HookResult:
"""Check file ownership against formation registry."""
if not state.project_root or not file_path:
return HookResult()
registry_file = state.project_root / FORGE_DIR_NAME / "state" / "formation-registry.json"
if not registry_file.exists():
return HookResult()
try:
registry = json.loads(registry_file.read_text())
except (json.JSONDecodeError, OSError):
return HookResult()
agent_id = args.get("agent_id", os.environ.get("FORGE_AGENT_ID", ""))
teammates = registry.get("teammates", {})
for role, info in teammates.items():
if info.get("agent_id") == agent_id:
# This is our agent — no conflict
continue
ownership = info.get("ownership", {})
owned_dirs = ownership.get("directories", [])
owned_files = ownership.get("files", [])
owned_patterns = ownership.get("patterns", [])
# Check if file is owned by another teammate
is_owned = False
for d in owned_dirs:
if file_path.startswith(d) or str(Path(file_path)).startswith(str(Path(d))):
is_owned = True
break
if not is_owned:
for f in owned_files:
if file_path == f or Path(file_path).name == Path(f).name:
is_owned = True
break
if not is_owned:
for p in owned_patterns:
if fnmatch.fnmatch(file_path, p) or fnmatch.fnmatch(Path(file_path).name, p):
is_owned = True
break
if is_owned and info.get("agent_id") and info["agent_id"] != agent_id:
return HookResult(
blocked=True,
reason=(
f"File '{Path(file_path).name}' is owned by teammate "
f"'{role}' (agent: {info.get('agent', 'unknown')}). "
f"Do not modify files outside your ownership."
),
)
return HookResult()
def _check_tool_policy(state: _HookState, tool_name: str) -> HookResult:
"""Check tool against formation tool policy cascade."""
if not state.project_root:
return HookResult()
registry_file = state.project_root / FORGE_DIR_NAME / "state" / "formation-registry.json"
if not registry_file.exists():
return HookResult()
agent_id = os.environ.get("FORGE_AGENT_ID", "")
if not agent_id:
return HookResult()
try:
registry = json.loads(registry_file.read_text())
except (json.JSONDecodeError, OSError):
return HookResult()
# Find our role
teammates = registry.get("teammates", {})
our_role = None
for role, info in teammates.items():
if info.get("agent_id") == agent_id:
our_role = role
break
if not our_role:
return HookResult() # Not in formation — no policy
# Resolve effective profile
# Layer 1: formation defaults
policies = registry.get("tool_policies", {})
defaults = policies.get("defaults", {})
profile = defaults.get("profile", "full")
# Layer 2: per-role override
per_role = policies.get("per_role", {})
if our_role in per_role:
profile = per_role[our_role].get("profile", profile)
# Layer 3: teammate level
teammate_info = teammates.get(our_role, {})
teammate_policies = teammate_info.get("tool_policies", {})
if teammate_policies:
profile = teammate_policies.get("profile", profile)
# Expand profile to allowed tools
allowed = _expand_tool_profile(profile)
# Check deny lists (deny-wins)
deny_list: set[str] = set()
for layer in [defaults, per_role.get(our_role, {}), teammate_policies]:
deny_list.update(layer.get("deny", []))
if tool_name in deny_list:
return HookResult(
blocked=True,
reason=f"Tool '{tool_name}' denied by policy for role '{our_role}'",
)
if allowed and tool_name not in allowed:
return HookResult(
blocked=True,
reason=f"Tool '{tool_name}' not in allowed set for profile '{profile}' (role: {our_role})",
)
return HookResult()
# Tool profile expansion (matches V11 common.sh)
_TOOL_PROFILES: dict[str, set[str]] = {
"full": {
"Read", "Write", "Edit", "Grep", "Glob", "Bash",
"TaskCreate", "TaskUpdate", "TaskList", "TaskGet",
"WebSearch", "WebFetch", "Agent",
},
"coding": {
"Read", "Write", "Edit", "Grep", "Glob", "Bash",
"TaskCreate", "TaskUpdate", "TaskList", "TaskGet",
},
"testing": {
"Read", "Grep", "Glob", "Bash",
"TaskCreate", "TaskUpdate", "TaskList", "TaskGet",
},
"readonly": {
"Read", "Grep", "Glob",
"TaskCreate", "TaskUpdate", "TaskList", "TaskGet",
},
"minimal": {
"TaskCreate", "TaskUpdate", "TaskList", "TaskGet",
},
}