claude-code-pilot 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +151 -0
- package/bin/install.js +431 -0
- package/docs/agent-guides/architecture.md +107 -0
- package/ecc/agents/architect.md +211 -0
- package/ecc/agents/code-reviewer.md +237 -0
- package/ecc/agents/doc-updater.md +107 -0
- package/ecc/agents/e2e-runner.md +107 -0
- package/ecc/agents/security-reviewer.md +108 -0
- package/ecc/agents/tdd-guide.md +91 -0
- package/ecc/commands/checkpoint.md +74 -0
- package/ecc/commands/evolve.md +178 -0
- package/ecc/commands/learn.md +70 -0
- package/ecc/commands/model-route.md +26 -0
- package/ecc/commands/quality-gate.md +29 -0
- package/ecc/commands/resume-session.md +155 -0
- package/ecc/commands/save-session.md +275 -0
- package/ecc/commands/sessions.md +305 -0
- package/ecc/commands/verify.md +59 -0
- package/ecc/contexts/dev.md +20 -0
- package/ecc/contexts/research.md +26 -0
- package/ecc/contexts/review.md +22 -0
- package/ecc/examples/CLAUDE.md +100 -0
- package/ecc/examples/django-api-CLAUDE.md +308 -0
- package/ecc/examples/go-microservice-CLAUDE.md +267 -0
- package/ecc/examples/rust-api-CLAUDE.md +285 -0
- package/ecc/examples/saas-nextjs-CLAUDE.md +166 -0
- package/ecc/examples/user-CLAUDE.md +109 -0
- package/ecc/rules/common/agents.md +49 -0
- package/ecc/rules/common/coding-style.md +48 -0
- package/ecc/rules/common/development-workflow.md +37 -0
- package/ecc/rules/common/git-workflow.md +24 -0
- package/ecc/rules/common/hooks.md +30 -0
- package/ecc/rules/common/patterns.md +31 -0
- package/ecc/rules/common/performance.md +55 -0
- package/ecc/rules/common/security.md +29 -0
- package/ecc/rules/common/testing.md +29 -0
- package/ecc/rules/golang/coding-style.md +32 -0
- package/ecc/rules/golang/hooks.md +17 -0
- package/ecc/rules/golang/patterns.md +45 -0
- package/ecc/rules/golang/security.md +34 -0
- package/ecc/rules/golang/testing.md +31 -0
- package/ecc/rules/kotlin/coding-style.md +86 -0
- package/ecc/rules/kotlin/patterns.md +146 -0
- package/ecc/rules/kotlin/security.md +82 -0
- package/ecc/rules/kotlin/testing.md +128 -0
- package/ecc/rules/perl/coding-style.md +46 -0
- package/ecc/rules/perl/hooks.md +22 -0
- package/ecc/rules/perl/patterns.md +76 -0
- package/ecc/rules/perl/security.md +69 -0
- package/ecc/rules/perl/testing.md +54 -0
- package/ecc/rules/php/coding-style.md +35 -0
- package/ecc/rules/php/hooks.md +24 -0
- package/ecc/rules/php/patterns.md +32 -0
- package/ecc/rules/php/security.md +33 -0
- package/ecc/rules/php/testing.md +34 -0
- package/ecc/rules/python/coding-style.md +42 -0
- package/ecc/rules/python/hooks.md +19 -0
- package/ecc/rules/python/patterns.md +39 -0
- package/ecc/rules/python/security.md +30 -0
- package/ecc/rules/python/testing.md +38 -0
- package/ecc/rules/swift/coding-style.md +47 -0
- package/ecc/rules/swift/hooks.md +20 -0
- package/ecc/rules/swift/patterns.md +66 -0
- package/ecc/rules/swift/security.md +33 -0
- package/ecc/rules/swift/testing.md +45 -0
- package/ecc/rules/typescript/coding-style.md +199 -0
- package/ecc/rules/typescript/hooks.md +22 -0
- package/ecc/rules/typescript/patterns.md +52 -0
- package/ecc/rules/typescript/security.md +28 -0
- package/ecc/rules/typescript/testing.md +18 -0
- package/ecc/scripts/hooks/check-hook-enabled.js +12 -0
- package/ecc/scripts/hooks/evaluate-session.js +100 -0
- package/ecc/scripts/hooks/pre-compact.js +48 -0
- package/ecc/scripts/hooks/run-with-flags-shell.sh +32 -0
- package/ecc/scripts/hooks/run-with-flags.js +120 -0
- package/ecc/scripts/hooks/session-end-marker.js +15 -0
- package/ecc/scripts/hooks/session-end.js +258 -0
- package/ecc/scripts/hooks/session-start.js +97 -0
- package/ecc/scripts/hooks/suggest-compact.js +80 -0
- package/ecc/scripts/lib/hook-flags.js +74 -0
- package/ecc/scripts/lib/package-manager.d.ts +119 -0
- package/ecc/scripts/lib/package-manager.js +431 -0
- package/ecc/scripts/lib/project-detect.js +428 -0
- package/ecc/scripts/lib/resolve-formatter.js +185 -0
- package/ecc/scripts/lib/session-aliases.d.ts +136 -0
- package/ecc/scripts/lib/session-aliases.js +481 -0
- package/ecc/scripts/lib/session-manager.d.ts +131 -0
- package/ecc/scripts/lib/session-manager.js +444 -0
- package/ecc/scripts/lib/shell-split.js +86 -0
- package/ecc/scripts/lib/utils.d.ts +183 -0
- package/ecc/scripts/lib/utils.js +543 -0
- package/ecc/skills/continuous-learning-v2/SKILL.md +365 -0
- package/ecc/skills/continuous-learning-v2/agents/observer-loop.sh +144 -0
- package/ecc/skills/continuous-learning-v2/agents/observer.md +198 -0
- package/ecc/skills/continuous-learning-v2/agents/start-observer.sh +194 -0
- package/ecc/skills/continuous-learning-v2/config.json +8 -0
- package/ecc/skills/continuous-learning-v2/hooks/observe.sh +246 -0
- package/ecc/skills/continuous-learning-v2/scripts/detect-project.sh +218 -0
- package/ecc/skills/continuous-learning-v2/scripts/instinct-cli.py +1148 -0
- package/ecc/skills/continuous-learning-v2/scripts/test_parse_instinct.py +984 -0
- package/ecc/skills/strategic-compact/SKILL.md +103 -0
- package/ecc/skills/strategic-compact/suggest-compact.sh +54 -0
- package/ecc/skills/verification-loop-SKILL.md +126 -0
- package/gsd/LICENSE +21 -0
- package/gsd/agents/gsd-codebase-mapper.md +772 -0
- package/gsd/agents/gsd-debugger.md +1257 -0
- package/gsd/agents/gsd-executor.md +489 -0
- package/gsd/agents/gsd-integration-checker.md +445 -0
- package/gsd/agents/gsd-nyquist-auditor.md +178 -0
- package/gsd/agents/gsd-phase-researcher.md +555 -0
- package/gsd/agents/gsd-plan-checker.md +708 -0
- package/gsd/agents/gsd-planner.md +1309 -0
- package/gsd/agents/gsd-project-researcher.md +631 -0
- package/gsd/agents/gsd-research-synthesizer.md +249 -0
- package/gsd/agents/gsd-roadmapper.md +652 -0
- package/gsd/agents/gsd-verifier.md +581 -0
- package/gsd/commands-gsd/add-phase.md +43 -0
- package/gsd/commands-gsd/add-tests.md +41 -0
- package/gsd/commands-gsd/add-todo.md +47 -0
- package/gsd/commands-gsd/audit-milestone.md +36 -0
- package/gsd/commands-gsd/check-todos.md +45 -0
- package/gsd/commands-gsd/cleanup.md +18 -0
- package/gsd/commands-gsd/complete-milestone.md +136 -0
- package/gsd/commands-gsd/debug.md +168 -0
- package/gsd/commands-gsd/discuss-phase.md +90 -0
- package/gsd/commands-gsd/execute-phase.md +41 -0
- package/gsd/commands-gsd/health.md +22 -0
- package/gsd/commands-gsd/help.md +22 -0
- package/gsd/commands-gsd/insert-phase.md +32 -0
- package/gsd/commands-gsd/join-discord.md +18 -0
- package/gsd/commands-gsd/list-phase-assumptions.md +46 -0
- package/gsd/commands-gsd/map-codebase.md +71 -0
- package/gsd/commands-gsd/new-milestone.md +44 -0
- package/gsd/commands-gsd/new-project.md +42 -0
- package/gsd/commands-gsd/pause-work.md +38 -0
- package/gsd/commands-gsd/plan-milestone-gaps.md +34 -0
- package/gsd/commands-gsd/plan-phase.md +45 -0
- package/gsd/commands-gsd/progress.md +24 -0
- package/gsd/commands-gsd/quick.md +45 -0
- package/gsd/commands-gsd/reapply-patches.md +123 -0
- package/gsd/commands-gsd/remove-phase.md +31 -0
- package/gsd/commands-gsd/research-phase.md +190 -0
- package/gsd/commands-gsd/resume-work.md +40 -0
- package/gsd/commands-gsd/set-profile.md +34 -0
- package/gsd/commands-gsd/settings.md +36 -0
- package/gsd/commands-gsd/update.md +37 -0
- package/gsd/commands-gsd/validate-phase.md +35 -0
- package/gsd/commands-gsd/verify-work.md +38 -0
- package/gsd/get-shit-done/bin/gsd-tools.cjs +592 -0
- package/gsd/get-shit-done/bin/lib/commands.cjs +548 -0
- package/gsd/get-shit-done/bin/lib/config.cjs +169 -0
- package/gsd/get-shit-done/bin/lib/core.cjs +492 -0
- package/gsd/get-shit-done/bin/lib/frontmatter.cjs +299 -0
- package/gsd/get-shit-done/bin/lib/init.cjs +710 -0
- package/gsd/get-shit-done/bin/lib/milestone.cjs +241 -0
- package/gsd/get-shit-done/bin/lib/phase.cjs +901 -0
- package/gsd/get-shit-done/bin/lib/roadmap.cjs +298 -0
- package/gsd/get-shit-done/bin/lib/state.cjs +721 -0
- package/gsd/get-shit-done/bin/lib/template.cjs +222 -0
- package/gsd/get-shit-done/bin/lib/verify.cjs +820 -0
- package/gsd/get-shit-done/references/checkpoints.md +776 -0
- package/gsd/get-shit-done/references/continuation-format.md +249 -0
- package/gsd/get-shit-done/references/decimal-phase-calculation.md +65 -0
- package/gsd/get-shit-done/references/git-integration.md +248 -0
- package/gsd/get-shit-done/references/git-planning-commit.md +38 -0
- package/gsd/get-shit-done/references/model-profile-resolution.md +34 -0
- package/gsd/get-shit-done/references/model-profiles.md +93 -0
- package/gsd/get-shit-done/references/phase-argument-parsing.md +61 -0
- package/gsd/get-shit-done/references/planning-config.md +200 -0
- package/gsd/get-shit-done/references/questioning.md +162 -0
- package/gsd/get-shit-done/references/tdd.md +263 -0
- package/gsd/get-shit-done/references/ui-brand.md +160 -0
- package/gsd/get-shit-done/references/verification-patterns.md +612 -0
- package/gsd/get-shit-done/templates/DEBUG.md +164 -0
- package/gsd/get-shit-done/templates/UAT.md +247 -0
- package/gsd/get-shit-done/templates/VALIDATION.md +76 -0
- package/gsd/get-shit-done/templates/codebase/architecture.md +255 -0
- package/gsd/get-shit-done/templates/codebase/concerns.md +310 -0
- package/gsd/get-shit-done/templates/codebase/conventions.md +307 -0
- package/gsd/get-shit-done/templates/codebase/integrations.md +280 -0
- package/gsd/get-shit-done/templates/codebase/stack.md +186 -0
- package/gsd/get-shit-done/templates/codebase/structure.md +285 -0
- package/gsd/get-shit-done/templates/codebase/testing.md +480 -0
- package/gsd/get-shit-done/templates/config.json +37 -0
- package/gsd/get-shit-done/templates/context.md +297 -0
- package/gsd/get-shit-done/templates/continue-here.md +78 -0
- package/gsd/get-shit-done/templates/debug-subagent-prompt.md +91 -0
- package/gsd/get-shit-done/templates/discovery.md +146 -0
- package/gsd/get-shit-done/templates/milestone-archive.md +123 -0
- package/gsd/get-shit-done/templates/milestone.md +115 -0
- package/gsd/get-shit-done/templates/phase-prompt.md +569 -0
- package/gsd/get-shit-done/templates/planner-subagent-prompt.md +117 -0
- package/gsd/get-shit-done/templates/project.md +184 -0
- package/gsd/get-shit-done/templates/requirements.md +231 -0
- package/gsd/get-shit-done/templates/research-project/ARCHITECTURE.md +204 -0
- package/gsd/get-shit-done/templates/research-project/FEATURES.md +147 -0
- package/gsd/get-shit-done/templates/research-project/PITFALLS.md +200 -0
- package/gsd/get-shit-done/templates/research-project/STACK.md +120 -0
- package/gsd/get-shit-done/templates/research-project/SUMMARY.md +170 -0
- package/gsd/get-shit-done/templates/research.md +552 -0
- package/gsd/get-shit-done/templates/retrospective.md +54 -0
- package/gsd/get-shit-done/templates/roadmap.md +202 -0
- package/gsd/get-shit-done/templates/state.md +176 -0
- package/gsd/get-shit-done/templates/summary-complex.md +59 -0
- package/gsd/get-shit-done/templates/summary-minimal.md +41 -0
- package/gsd/get-shit-done/templates/summary-standard.md +48 -0
- package/gsd/get-shit-done/templates/summary.md +248 -0
- package/gsd/get-shit-done/templates/user-setup.md +311 -0
- package/gsd/get-shit-done/templates/verification-report.md +322 -0
- package/gsd/get-shit-done/workflows/add-phase.md +112 -0
- package/gsd/get-shit-done/workflows/add-tests.md +351 -0
- package/gsd/get-shit-done/workflows/add-todo.md +158 -0
- package/gsd/get-shit-done/workflows/audit-milestone.md +332 -0
- package/gsd/get-shit-done/workflows/check-todos.md +177 -0
- package/gsd/get-shit-done/workflows/cleanup.md +152 -0
- package/gsd/get-shit-done/workflows/complete-milestone.md +764 -0
- package/gsd/get-shit-done/workflows/diagnose-issues.md +219 -0
- package/gsd/get-shit-done/workflows/discovery-phase.md +289 -0
- package/gsd/get-shit-done/workflows/discuss-phase.md +676 -0
- package/gsd/get-shit-done/workflows/execute-phase.md +459 -0
- package/gsd/get-shit-done/workflows/execute-plan.md +449 -0
- package/gsd/get-shit-done/workflows/health.md +159 -0
- package/gsd/get-shit-done/workflows/help.md +489 -0
- package/gsd/get-shit-done/workflows/insert-phase.md +130 -0
- package/gsd/get-shit-done/workflows/list-phase-assumptions.md +178 -0
- package/gsd/get-shit-done/workflows/map-codebase.md +316 -0
- package/gsd/get-shit-done/workflows/new-milestone.md +384 -0
- package/gsd/get-shit-done/workflows/new-project.md +1111 -0
- package/gsd/get-shit-done/workflows/pause-work.md +122 -0
- package/gsd/get-shit-done/workflows/plan-milestone-gaps.md +274 -0
- package/gsd/get-shit-done/workflows/plan-phase.md +560 -0
- package/gsd/get-shit-done/workflows/progress.md +382 -0
- package/gsd/get-shit-done/workflows/quick.md +601 -0
- package/gsd/get-shit-done/workflows/remove-phase.md +155 -0
- package/gsd/get-shit-done/workflows/research-phase.md +74 -0
- package/gsd/get-shit-done/workflows/resume-project.md +307 -0
- package/gsd/get-shit-done/workflows/set-profile.md +81 -0
- package/gsd/get-shit-done/workflows/settings.md +214 -0
- package/gsd/get-shit-done/workflows/transition.md +544 -0
- package/gsd/get-shit-done/workflows/update.md +240 -0
- package/gsd/get-shit-done/workflows/validate-phase.md +167 -0
- package/gsd/get-shit-done/workflows/verify-phase.md +243 -0
- package/gsd/get-shit-done/workflows/verify-work.md +583 -0
- package/gsd/hooks/gsd-check-update.js +81 -0
- package/gsd/hooks/gsd-context-monitor.js +141 -0
- package/gsd/hooks/gsd-statusline.js +115 -0
- package/kit/CLAUDE.md +43 -0
- package/kit/commands/kit/update.md +46 -0
- package/kit/commands/setup-refresh.md +50 -0
- package/kit/commands/setup.md +579 -0
- package/kit/commands/tool-guide.md +44 -0
- package/kit/hooks/kit-check-update.js +54 -0
- package/kit/mcp.json +10 -0
- package/kit/rules/code-style.md +24 -0
- package/manifest.json +30 -0
- package/package.json +36 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: observer
|
|
3
|
+
description: Background agent that analyzes session observations to detect patterns and create instincts. Uses Haiku for cost-efficiency. v2.1 adds project-scoped instincts.
|
|
4
|
+
model: haiku
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Observer Agent
|
|
8
|
+
|
|
9
|
+
A background agent that analyzes observations from Claude Code sessions to detect patterns and create instincts.
|
|
10
|
+
|
|
11
|
+
## When to Run
|
|
12
|
+
|
|
13
|
+
- After enough observations accumulate (configurable, default 20)
|
|
14
|
+
- On a scheduled interval (configurable, default 5 minutes)
|
|
15
|
+
- When triggered on demand via SIGUSR1 to the observer process
|
|
16
|
+
|
|
17
|
+
## Input
|
|
18
|
+
|
|
19
|
+
Reads observations from the **project-scoped** observations file:
|
|
20
|
+
- Project: `~/.claude/homunculus/projects/<project-hash>/observations.jsonl`
|
|
21
|
+
- Global fallback: `~/.claude/homunculus/observations.jsonl`
|
|
22
|
+
|
|
23
|
+
```jsonl
|
|
24
|
+
{"timestamp":"2025-01-22T10:30:00Z","event":"tool_start","session":"abc123","tool":"Edit","input":"...","project_id":"a1b2c3d4e5f6","project_name":"my-react-app"}
|
|
25
|
+
{"timestamp":"2025-01-22T10:30:01Z","event":"tool_complete","session":"abc123","tool":"Edit","output":"...","project_id":"a1b2c3d4e5f6","project_name":"my-react-app"}
|
|
26
|
+
{"timestamp":"2025-01-22T10:30:05Z","event":"tool_start","session":"abc123","tool":"Bash","input":"npm test","project_id":"a1b2c3d4e5f6","project_name":"my-react-app"}
|
|
27
|
+
{"timestamp":"2025-01-22T10:30:10Z","event":"tool_complete","session":"abc123","tool":"Bash","output":"All tests pass","project_id":"a1b2c3d4e5f6","project_name":"my-react-app"}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Pattern Detection
|
|
31
|
+
|
|
32
|
+
Look for these patterns in observations:
|
|
33
|
+
|
|
34
|
+
### 1. User Corrections
|
|
35
|
+
When a user's follow-up message corrects Claude's previous action:
|
|
36
|
+
- "No, use X instead of Y"
|
|
37
|
+
- "Actually, I meant..."
|
|
38
|
+
- Immediate undo/redo patterns
|
|
39
|
+
|
|
40
|
+
→ Create instinct: "When doing X, prefer Y"
|
|
41
|
+
|
|
42
|
+
### 2. Error Resolutions
|
|
43
|
+
When an error is followed by a fix:
|
|
44
|
+
- Tool output contains error
|
|
45
|
+
- Next few tool calls fix it
|
|
46
|
+
- Same error type resolved similarly multiple times
|
|
47
|
+
|
|
48
|
+
→ Create instinct: "When encountering error X, try Y"
|
|
49
|
+
|
|
50
|
+
### 3. Repeated Workflows
|
|
51
|
+
When the same sequence of tools is used multiple times:
|
|
52
|
+
- Same tool sequence with similar inputs
|
|
53
|
+
- File patterns that change together
|
|
54
|
+
- Time-clustered operations
|
|
55
|
+
|
|
56
|
+
→ Create workflow instinct: "When doing X, follow steps Y, Z, W"
|
|
57
|
+
|
|
58
|
+
### 4. Tool Preferences
|
|
59
|
+
When certain tools are consistently preferred:
|
|
60
|
+
- Always uses Grep before Edit
|
|
61
|
+
- Prefers Read over Bash cat
|
|
62
|
+
- Uses specific Bash commands for certain tasks
|
|
63
|
+
|
|
64
|
+
→ Create instinct: "When needing X, use tool Y"
|
|
65
|
+
|
|
66
|
+
## Output
|
|
67
|
+
|
|
68
|
+
Creates/updates instincts in the **project-scoped** instincts directory:
|
|
69
|
+
- Project: `~/.claude/homunculus/projects/<project-hash>/instincts/personal/`
|
|
70
|
+
- Global: `~/.claude/homunculus/instincts/personal/` (for universal patterns)
|
|
71
|
+
|
|
72
|
+
### Project-Scoped Instinct (default)
|
|
73
|
+
|
|
74
|
+
```yaml
|
|
75
|
+
---
|
|
76
|
+
id: use-react-hooks-pattern
|
|
77
|
+
trigger: "when creating React components"
|
|
78
|
+
confidence: 0.65
|
|
79
|
+
domain: "code-style"
|
|
80
|
+
source: "session-observation"
|
|
81
|
+
scope: project
|
|
82
|
+
project_id: "a1b2c3d4e5f6"
|
|
83
|
+
project_name: "my-react-app"
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
# Use React Hooks Pattern
|
|
87
|
+
|
|
88
|
+
## Action
|
|
89
|
+
Always use functional components with hooks instead of class components.
|
|
90
|
+
|
|
91
|
+
## Evidence
|
|
92
|
+
- Observed 8 times in session abc123
|
|
93
|
+
- Pattern: All new components use useState/useEffect
|
|
94
|
+
- Last observed: 2025-01-22
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Global Instinct (universal patterns)
|
|
98
|
+
|
|
99
|
+
```yaml
|
|
100
|
+
---
|
|
101
|
+
id: always-validate-user-input
|
|
102
|
+
trigger: "when handling user input"
|
|
103
|
+
confidence: 0.75
|
|
104
|
+
domain: "security"
|
|
105
|
+
source: "session-observation"
|
|
106
|
+
scope: global
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
# Always Validate User Input
|
|
110
|
+
|
|
111
|
+
## Action
|
|
112
|
+
Validate and sanitize all user input before processing.
|
|
113
|
+
|
|
114
|
+
## Evidence
|
|
115
|
+
- Observed across 3 different projects
|
|
116
|
+
- Pattern: User consistently adds input validation
|
|
117
|
+
- Last observed: 2025-01-22
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Scope Decision Guide
|
|
121
|
+
|
|
122
|
+
When creating instincts, determine scope based on these heuristics:
|
|
123
|
+
|
|
124
|
+
| Pattern Type | Scope | Examples |
|
|
125
|
+
|-------------|-------|---------|
|
|
126
|
+
| Language/framework conventions | **project** | "Use React hooks", "Follow Django REST patterns" |
|
|
127
|
+
| File structure preferences | **project** | "Tests in `__tests__`/", "Components in src/components/" |
|
|
128
|
+
| Code style | **project** | "Use functional style", "Prefer dataclasses" |
|
|
129
|
+
| Error handling strategies | **project** (usually) | "Use Result type for errors" |
|
|
130
|
+
| Security practices | **global** | "Validate user input", "Sanitize SQL" |
|
|
131
|
+
| General best practices | **global** | "Write tests first", "Always handle errors" |
|
|
132
|
+
| Tool workflow preferences | **global** | "Grep before Edit", "Read before Write" |
|
|
133
|
+
| Git practices | **global** | "Conventional commits", "Small focused commits" |
|
|
134
|
+
|
|
135
|
+
**When in doubt, default to `scope: project`** — it's safer to be project-specific and promote later than to contaminate the global space.
|
|
136
|
+
|
|
137
|
+
## Confidence Calculation
|
|
138
|
+
|
|
139
|
+
Initial confidence based on observation frequency:
|
|
140
|
+
- 1-2 observations: 0.3 (tentative)
|
|
141
|
+
- 3-5 observations: 0.5 (moderate)
|
|
142
|
+
- 6-10 observations: 0.7 (strong)
|
|
143
|
+
- 11+ observations: 0.85 (very strong)
|
|
144
|
+
|
|
145
|
+
Confidence adjusts over time:
|
|
146
|
+
- +0.05 for each confirming observation
|
|
147
|
+
- -0.1 for each contradicting observation
|
|
148
|
+
- -0.02 per week without observation (decay)
|
|
149
|
+
|
|
150
|
+
## Instinct Promotion (Project → Global)
|
|
151
|
+
|
|
152
|
+
An instinct should be promoted from project-scoped to global when:
|
|
153
|
+
1. The **same pattern** (by id or similar trigger) exists in **2+ different projects**
|
|
154
|
+
2. Each instance has confidence **>= 0.8**
|
|
155
|
+
3. The domain is in the global-friendly list (security, general-best-practices, workflow)
|
|
156
|
+
|
|
157
|
+
Promotion is handled by the `instinct-cli.py promote` command or the `/evolve` analysis.
|
|
158
|
+
|
|
159
|
+
## Important Guidelines
|
|
160
|
+
|
|
161
|
+
1. **Be Conservative**: Only create instincts for clear patterns (3+ observations)
|
|
162
|
+
2. **Be Specific**: Narrow triggers are better than broad ones
|
|
163
|
+
3. **Track Evidence**: Always include what observations led to the instinct
|
|
164
|
+
4. **Respect Privacy**: Never include actual code snippets, only patterns
|
|
165
|
+
5. **Merge Similar**: If a new instinct is similar to existing, update rather than duplicate
|
|
166
|
+
6. **Default to Project Scope**: Unless the pattern is clearly universal, make it project-scoped
|
|
167
|
+
7. **Include Project Context**: Always set `project_id` and `project_name` for project-scoped instincts
|
|
168
|
+
|
|
169
|
+
## Example Analysis Session
|
|
170
|
+
|
|
171
|
+
Given observations:
|
|
172
|
+
```jsonl
|
|
173
|
+
{"event":"tool_start","tool":"Grep","input":"pattern: useState","project_id":"a1b2c3","project_name":"my-app"}
|
|
174
|
+
{"event":"tool_complete","tool":"Grep","output":"Found in 3 files","project_id":"a1b2c3","project_name":"my-app"}
|
|
175
|
+
{"event":"tool_start","tool":"Read","input":"src/hooks/useAuth.ts","project_id":"a1b2c3","project_name":"my-app"}
|
|
176
|
+
{"event":"tool_complete","tool":"Read","output":"[file content]","project_id":"a1b2c3","project_name":"my-app"}
|
|
177
|
+
{"event":"tool_start","tool":"Edit","input":"src/hooks/useAuth.ts...","project_id":"a1b2c3","project_name":"my-app"}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Analysis:
|
|
181
|
+
- Detected workflow: Grep → Read → Edit
|
|
182
|
+
- Frequency: Seen 5 times this session
|
|
183
|
+
- **Scope decision**: This is a general workflow pattern (not project-specific) → **global**
|
|
184
|
+
- Create instinct:
|
|
185
|
+
- trigger: "when modifying code"
|
|
186
|
+
- action: "Search with Grep, confirm with Read, then Edit"
|
|
187
|
+
- confidence: 0.6
|
|
188
|
+
- domain: "workflow"
|
|
189
|
+
- scope: "global"
|
|
190
|
+
|
|
191
|
+
## Integration with Skill Creator
|
|
192
|
+
|
|
193
|
+
When instincts are imported from Skill Creator (repo analysis), they have:
|
|
194
|
+
- `source: "repo-analysis"`
|
|
195
|
+
- `source_repo: "https://github.com/..."`
|
|
196
|
+
- `scope: "project"` (since they come from a specific repo)
|
|
197
|
+
|
|
198
|
+
These should be treated as team/project conventions with higher initial confidence (0.7+).
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# Continuous Learning v2 - Observer Agent Launcher
|
|
3
|
+
#
|
|
4
|
+
# Starts the background observer agent that analyzes observations
|
|
5
|
+
# and creates instincts. Uses Haiku model for cost efficiency.
|
|
6
|
+
#
|
|
7
|
+
# v2.1: Project-scoped — detects current project and analyzes
|
|
8
|
+
# project-specific observations into project-scoped instincts.
|
|
9
|
+
#
|
|
10
|
+
# Usage:
|
|
11
|
+
# start-observer.sh # Start observer for current project (or global)
|
|
12
|
+
# start-observer.sh stop # Stop running observer
|
|
13
|
+
# start-observer.sh status # Check if observer is running
|
|
14
|
+
|
|
15
|
+
set -e
|
|
16
|
+
|
|
17
|
+
# NOTE: set -e is disabled inside the background subshell below
|
|
18
|
+
# to prevent claude CLI failures from killing the observer loop.
|
|
19
|
+
|
|
20
|
+
# ─────────────────────────────────────────────
|
|
21
|
+
# Project detection
|
|
22
|
+
# ─────────────────────────────────────────────
|
|
23
|
+
|
|
24
|
+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
25
|
+
SKILL_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
|
26
|
+
OBSERVER_LOOP_SCRIPT="${SCRIPT_DIR}/observer-loop.sh"
|
|
27
|
+
|
|
28
|
+
# Source shared project detection helper
|
|
29
|
+
# This sets: PROJECT_ID, PROJECT_NAME, PROJECT_ROOT, PROJECT_DIR
|
|
30
|
+
source "${SKILL_ROOT}/scripts/detect-project.sh"
|
|
31
|
+
PYTHON_CMD="${CLV2_PYTHON_CMD:-}"
|
|
32
|
+
|
|
33
|
+
# ─────────────────────────────────────────────
|
|
34
|
+
# Configuration
|
|
35
|
+
# ─────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
CONFIG_DIR="${HOME}/.claude/homunculus"
|
|
38
|
+
CONFIG_FILE="${SKILL_ROOT}/config.json"
|
|
39
|
+
# PID file is project-scoped so each project can have its own observer
|
|
40
|
+
PID_FILE="${PROJECT_DIR}/.observer.pid"
|
|
41
|
+
LOG_FILE="${PROJECT_DIR}/observer.log"
|
|
42
|
+
OBSERVATIONS_FILE="${PROJECT_DIR}/observations.jsonl"
|
|
43
|
+
INSTINCTS_DIR="${PROJECT_DIR}/instincts/personal"
|
|
44
|
+
|
|
45
|
+
# Read config values from config.json
|
|
46
|
+
OBSERVER_INTERVAL_MINUTES=5
|
|
47
|
+
MIN_OBSERVATIONS=20
|
|
48
|
+
OBSERVER_ENABLED=false
|
|
49
|
+
if [ -f "$CONFIG_FILE" ]; then
|
|
50
|
+
if [ -z "$PYTHON_CMD" ]; then
|
|
51
|
+
echo "No python interpreter found; using built-in observer defaults." >&2
|
|
52
|
+
else
|
|
53
|
+
_config=$(CLV2_CONFIG="$CONFIG_FILE" "$PYTHON_CMD" -c "
|
|
54
|
+
import json, os
|
|
55
|
+
with open(os.environ['CLV2_CONFIG']) as f:
|
|
56
|
+
cfg = json.load(f)
|
|
57
|
+
obs = cfg.get('observer', {})
|
|
58
|
+
print(obs.get('run_interval_minutes', 5))
|
|
59
|
+
print(obs.get('min_observations_to_analyze', 20))
|
|
60
|
+
print(str(obs.get('enabled', False)).lower())
|
|
61
|
+
" 2>/dev/null || echo "5
|
|
62
|
+
20
|
|
63
|
+
false")
|
|
64
|
+
_interval=$(echo "$_config" | sed -n '1p')
|
|
65
|
+
_min_obs=$(echo "$_config" | sed -n '2p')
|
|
66
|
+
_enabled=$(echo "$_config" | sed -n '3p')
|
|
67
|
+
if [ "$_interval" -gt 0 ] 2>/dev/null; then
|
|
68
|
+
OBSERVER_INTERVAL_MINUTES="$_interval"
|
|
69
|
+
fi
|
|
70
|
+
if [ "$_min_obs" -gt 0 ] 2>/dev/null; then
|
|
71
|
+
MIN_OBSERVATIONS="$_min_obs"
|
|
72
|
+
fi
|
|
73
|
+
if [ "$_enabled" = "true" ]; then
|
|
74
|
+
OBSERVER_ENABLED=true
|
|
75
|
+
fi
|
|
76
|
+
fi
|
|
77
|
+
fi
|
|
78
|
+
OBSERVER_INTERVAL_SECONDS=$((OBSERVER_INTERVAL_MINUTES * 60))
|
|
79
|
+
|
|
80
|
+
echo "Project: ${PROJECT_NAME} (${PROJECT_ID})"
|
|
81
|
+
echo "Storage: ${PROJECT_DIR}"
|
|
82
|
+
|
|
83
|
+
# Windows/Git-Bash detection (Issue #295)
|
|
84
|
+
UNAME_LOWER="$(uname -s 2>/dev/null | tr '[:upper:]' '[:lower:]')"
|
|
85
|
+
IS_WINDOWS=false
|
|
86
|
+
case "$UNAME_LOWER" in
|
|
87
|
+
*mingw*|*msys*|*cygwin*) IS_WINDOWS=true ;;
|
|
88
|
+
esac
|
|
89
|
+
|
|
90
|
+
case "${1:-start}" in
|
|
91
|
+
stop)
|
|
92
|
+
if [ -f "$PID_FILE" ]; then
|
|
93
|
+
pid=$(cat "$PID_FILE")
|
|
94
|
+
if kill -0 "$pid" 2>/dev/null; then
|
|
95
|
+
echo "Stopping observer for ${PROJECT_NAME} (PID: $pid)..."
|
|
96
|
+
kill "$pid"
|
|
97
|
+
rm -f "$PID_FILE"
|
|
98
|
+
echo "Observer stopped."
|
|
99
|
+
else
|
|
100
|
+
echo "Observer not running (stale PID file)."
|
|
101
|
+
rm -f "$PID_FILE"
|
|
102
|
+
fi
|
|
103
|
+
else
|
|
104
|
+
echo "Observer not running."
|
|
105
|
+
fi
|
|
106
|
+
exit 0
|
|
107
|
+
;;
|
|
108
|
+
|
|
109
|
+
status)
|
|
110
|
+
if [ -f "$PID_FILE" ]; then
|
|
111
|
+
pid=$(cat "$PID_FILE")
|
|
112
|
+
if kill -0 "$pid" 2>/dev/null; then
|
|
113
|
+
echo "Observer is running (PID: $pid)"
|
|
114
|
+
echo "Log: $LOG_FILE"
|
|
115
|
+
echo "Observations: $(wc -l < "$OBSERVATIONS_FILE" 2>/dev/null || echo 0) lines"
|
|
116
|
+
# Also show instinct count
|
|
117
|
+
instinct_count=$(find "$INSTINCTS_DIR" -name "*.yaml" 2>/dev/null | wc -l)
|
|
118
|
+
echo "Instincts: $instinct_count"
|
|
119
|
+
exit 0
|
|
120
|
+
else
|
|
121
|
+
echo "Observer not running (stale PID file)"
|
|
122
|
+
rm -f "$PID_FILE"
|
|
123
|
+
exit 1
|
|
124
|
+
fi
|
|
125
|
+
else
|
|
126
|
+
echo "Observer not running"
|
|
127
|
+
exit 1
|
|
128
|
+
fi
|
|
129
|
+
;;
|
|
130
|
+
|
|
131
|
+
start)
|
|
132
|
+
# Check if observer is disabled in config
|
|
133
|
+
if [ "$OBSERVER_ENABLED" != "true" ]; then
|
|
134
|
+
echo "Observer is disabled in config.json (observer.enabled: false)."
|
|
135
|
+
echo "Set observer.enabled to true in config.json to enable."
|
|
136
|
+
exit 1
|
|
137
|
+
fi
|
|
138
|
+
|
|
139
|
+
# Check if already running
|
|
140
|
+
if [ -f "$PID_FILE" ]; then
|
|
141
|
+
pid=$(cat "$PID_FILE")
|
|
142
|
+
if kill -0 "$pid" 2>/dev/null; then
|
|
143
|
+
echo "Observer already running for ${PROJECT_NAME} (PID: $pid)"
|
|
144
|
+
exit 0
|
|
145
|
+
fi
|
|
146
|
+
rm -f "$PID_FILE"
|
|
147
|
+
fi
|
|
148
|
+
|
|
149
|
+
echo "Starting observer agent for ${PROJECT_NAME}..."
|
|
150
|
+
|
|
151
|
+
if [ ! -x "$OBSERVER_LOOP_SCRIPT" ]; then
|
|
152
|
+
echo "Observer loop script not found or not executable: $OBSERVER_LOOP_SCRIPT"
|
|
153
|
+
exit 1
|
|
154
|
+
fi
|
|
155
|
+
|
|
156
|
+
# The observer loop — fully detached with nohup, IO redirected to log.
|
|
157
|
+
# Variables are passed via env; observer-loop.sh handles analysis/retry flow.
|
|
158
|
+
nohup env \
|
|
159
|
+
CONFIG_DIR="$CONFIG_DIR" \
|
|
160
|
+
PID_FILE="$PID_FILE" \
|
|
161
|
+
LOG_FILE="$LOG_FILE" \
|
|
162
|
+
OBSERVATIONS_FILE="$OBSERVATIONS_FILE" \
|
|
163
|
+
INSTINCTS_DIR="$INSTINCTS_DIR" \
|
|
164
|
+
PROJECT_DIR="$PROJECT_DIR" \
|
|
165
|
+
PROJECT_NAME="$PROJECT_NAME" \
|
|
166
|
+
PROJECT_ID="$PROJECT_ID" \
|
|
167
|
+
MIN_OBSERVATIONS="$MIN_OBSERVATIONS" \
|
|
168
|
+
OBSERVER_INTERVAL_SECONDS="$OBSERVER_INTERVAL_SECONDS" \
|
|
169
|
+
CLV2_IS_WINDOWS="$IS_WINDOWS" \
|
|
170
|
+
"$OBSERVER_LOOP_SCRIPT" >> "$LOG_FILE" 2>&1 &
|
|
171
|
+
|
|
172
|
+
# Wait for PID file
|
|
173
|
+
sleep 2
|
|
174
|
+
|
|
175
|
+
if [ -f "$PID_FILE" ]; then
|
|
176
|
+
pid=$(cat "$PID_FILE")
|
|
177
|
+
if kill -0 "$pid" 2>/dev/null; then
|
|
178
|
+
echo "Observer started (PID: $pid)"
|
|
179
|
+
echo "Log: $LOG_FILE"
|
|
180
|
+
else
|
|
181
|
+
echo "Failed to start observer (process died immediately, check $LOG_FILE)"
|
|
182
|
+
exit 1
|
|
183
|
+
fi
|
|
184
|
+
else
|
|
185
|
+
echo "Failed to start observer"
|
|
186
|
+
exit 1
|
|
187
|
+
fi
|
|
188
|
+
;;
|
|
189
|
+
|
|
190
|
+
*)
|
|
191
|
+
echo "Usage: $0 {start|stop|status}"
|
|
192
|
+
exit 1
|
|
193
|
+
;;
|
|
194
|
+
esac
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# Continuous Learning v2 - Observation Hook
|
|
3
|
+
#
|
|
4
|
+
# Captures tool use events for pattern analysis.
|
|
5
|
+
# Claude Code passes hook data via stdin as JSON.
|
|
6
|
+
#
|
|
7
|
+
# v2.1: Project-scoped observations — detects current project context
|
|
8
|
+
# and writes observations to project-specific directory.
|
|
9
|
+
#
|
|
10
|
+
# Registered via plugin hooks/hooks.json (auto-loaded when plugin is enabled).
|
|
11
|
+
# Can also be registered manually in ~/.claude/settings.json.
|
|
12
|
+
|
|
13
|
+
set -e
|
|
14
|
+
|
|
15
|
+
# Hook phase from CLI argument: "pre" (PreToolUse) or "post" (PostToolUse)
|
|
16
|
+
HOOK_PHASE="${1:-post}"
|
|
17
|
+
|
|
18
|
+
# ─────────────────────────────────────────────
|
|
19
|
+
# Read stdin first (before project detection)
|
|
20
|
+
# ─────────────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
# Read JSON from stdin (Claude Code hook format)
|
|
23
|
+
INPUT_JSON=$(cat)
|
|
24
|
+
|
|
25
|
+
# Exit if no input
|
|
26
|
+
if [ -z "$INPUT_JSON" ]; then
|
|
27
|
+
exit 0
|
|
28
|
+
fi
|
|
29
|
+
|
|
30
|
+
resolve_python_cmd() {
|
|
31
|
+
if [ -n "${CLV2_PYTHON_CMD:-}" ] && command -v "$CLV2_PYTHON_CMD" >/dev/null 2>&1; then
|
|
32
|
+
printf '%s\n' "$CLV2_PYTHON_CMD"
|
|
33
|
+
return 0
|
|
34
|
+
fi
|
|
35
|
+
|
|
36
|
+
if command -v python3 >/dev/null 2>&1; then
|
|
37
|
+
printf '%s\n' python3
|
|
38
|
+
return 0
|
|
39
|
+
fi
|
|
40
|
+
|
|
41
|
+
if command -v python >/dev/null 2>&1; then
|
|
42
|
+
printf '%s\n' python
|
|
43
|
+
return 0
|
|
44
|
+
fi
|
|
45
|
+
|
|
46
|
+
return 1
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
PYTHON_CMD="$(resolve_python_cmd 2>/dev/null || true)"
|
|
50
|
+
if [ -z "$PYTHON_CMD" ]; then
|
|
51
|
+
echo "[observe] No python interpreter found, skipping observation" >&2
|
|
52
|
+
exit 0
|
|
53
|
+
fi
|
|
54
|
+
|
|
55
|
+
# ─────────────────────────────────────────────
|
|
56
|
+
# Extract cwd from stdin for project detection
|
|
57
|
+
# ─────────────────────────────────────────────
|
|
58
|
+
|
|
59
|
+
# Extract cwd from the hook JSON to use for project detection.
|
|
60
|
+
# This avoids spawning a separate git subprocess when cwd is available.
|
|
61
|
+
STDIN_CWD=$(echo "$INPUT_JSON" | "$PYTHON_CMD" -c '
|
|
62
|
+
import json, sys
|
|
63
|
+
try:
|
|
64
|
+
data = json.load(sys.stdin)
|
|
65
|
+
cwd = data.get("cwd", "")
|
|
66
|
+
print(cwd)
|
|
67
|
+
except(KeyError, TypeError, ValueError):
|
|
68
|
+
print("")
|
|
69
|
+
' 2>/dev/null || echo "")
|
|
70
|
+
|
|
71
|
+
# If cwd was provided in stdin, use it for project detection
|
|
72
|
+
if [ -n "$STDIN_CWD" ] && [ -d "$STDIN_CWD" ]; then
|
|
73
|
+
export CLAUDE_PROJECT_DIR="$STDIN_CWD"
|
|
74
|
+
fi
|
|
75
|
+
|
|
76
|
+
# ─────────────────────────────────────────────
|
|
77
|
+
# Project detection
|
|
78
|
+
# ─────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
81
|
+
SKILL_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
|
82
|
+
|
|
83
|
+
# Source shared project detection helper
|
|
84
|
+
# This sets: PROJECT_ID, PROJECT_NAME, PROJECT_ROOT, PROJECT_DIR
|
|
85
|
+
source "${SKILL_ROOT}/scripts/detect-project.sh"
|
|
86
|
+
PYTHON_CMD="${CLV2_PYTHON_CMD:-$PYTHON_CMD}"
|
|
87
|
+
|
|
88
|
+
# ─────────────────────────────────────────────
|
|
89
|
+
# Configuration
|
|
90
|
+
# ─────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
CONFIG_DIR="${HOME}/.claude/homunculus"
|
|
93
|
+
OBSERVATIONS_FILE="${PROJECT_DIR}/observations.jsonl"
|
|
94
|
+
MAX_FILE_SIZE_MB=10
|
|
95
|
+
|
|
96
|
+
# Skip if disabled
|
|
97
|
+
if [ -f "$CONFIG_DIR/disabled" ]; then
|
|
98
|
+
exit 0
|
|
99
|
+
fi
|
|
100
|
+
|
|
101
|
+
# Auto-purge observation files older than 30 days (runs once per session)
|
|
102
|
+
PURGE_MARKER="${PROJECT_DIR}/.last-purge"
|
|
103
|
+
if [ ! -f "$PURGE_MARKER" ] || [ "$(find "$PURGE_MARKER" -mtime +1 2>/dev/null)" ]; then
|
|
104
|
+
find "${PROJECT_DIR}" -name "observations-*.jsonl" -mtime +30 -delete 2>/dev/null || true
|
|
105
|
+
touch "$PURGE_MARKER" 2>/dev/null || true
|
|
106
|
+
fi
|
|
107
|
+
|
|
108
|
+
# Parse using Python via stdin pipe (safe for all JSON payloads)
|
|
109
|
+
# Pass HOOK_PHASE via env var since Claude Code does not include hook type in stdin JSON
|
|
110
|
+
PARSED=$(echo "$INPUT_JSON" | HOOK_PHASE="$HOOK_PHASE" "$PYTHON_CMD" -c '
|
|
111
|
+
import json
|
|
112
|
+
import sys
|
|
113
|
+
import os
|
|
114
|
+
|
|
115
|
+
try:
|
|
116
|
+
data = json.load(sys.stdin)
|
|
117
|
+
|
|
118
|
+
# Determine event type from CLI argument passed via env var.
|
|
119
|
+
# Claude Code does NOT include a "hook_type" field in the stdin JSON,
|
|
120
|
+
# so we rely on the shell argument ("pre" or "post") instead.
|
|
121
|
+
hook_phase = os.environ.get("HOOK_PHASE", "post")
|
|
122
|
+
event = "tool_start" if hook_phase == "pre" else "tool_complete"
|
|
123
|
+
|
|
124
|
+
# Extract fields - Claude Code hook format
|
|
125
|
+
tool_name = data.get("tool_name", data.get("tool", "unknown"))
|
|
126
|
+
tool_input = data.get("tool_input", data.get("input", {}))
|
|
127
|
+
tool_output = data.get("tool_response")
|
|
128
|
+
if tool_output is None:
|
|
129
|
+
tool_output = data.get("tool_output", data.get("output", ""))
|
|
130
|
+
session_id = data.get("session_id", "unknown")
|
|
131
|
+
tool_use_id = data.get("tool_use_id", "")
|
|
132
|
+
cwd = data.get("cwd", "")
|
|
133
|
+
|
|
134
|
+
# Truncate large inputs/outputs
|
|
135
|
+
if isinstance(tool_input, dict):
|
|
136
|
+
tool_input_str = json.dumps(tool_input)[:5000]
|
|
137
|
+
else:
|
|
138
|
+
tool_input_str = str(tool_input)[:5000]
|
|
139
|
+
|
|
140
|
+
if isinstance(tool_output, dict):
|
|
141
|
+
tool_response_str = json.dumps(tool_output)[:5000]
|
|
142
|
+
else:
|
|
143
|
+
tool_response_str = str(tool_output)[:5000]
|
|
144
|
+
|
|
145
|
+
print(json.dumps({
|
|
146
|
+
"parsed": True,
|
|
147
|
+
"event": event,
|
|
148
|
+
"tool": tool_name,
|
|
149
|
+
"input": tool_input_str if event == "tool_start" else None,
|
|
150
|
+
"output": tool_response_str if event == "tool_complete" else None,
|
|
151
|
+
"session": session_id,
|
|
152
|
+
"tool_use_id": tool_use_id,
|
|
153
|
+
"cwd": cwd
|
|
154
|
+
}))
|
|
155
|
+
except Exception as e:
|
|
156
|
+
print(json.dumps({"parsed": False, "error": str(e)}))
|
|
157
|
+
')
|
|
158
|
+
|
|
159
|
+
# Check if parsing succeeded
|
|
160
|
+
PARSED_OK=$(echo "$PARSED" | "$PYTHON_CMD" -c "import json,sys; print(json.load(sys.stdin).get('parsed', False))" 2>/dev/null || echo "False")
|
|
161
|
+
|
|
162
|
+
if [ "$PARSED_OK" != "True" ]; then
|
|
163
|
+
# Fallback: log raw input for debugging (scrub secrets before persisting)
|
|
164
|
+
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
|
165
|
+
export TIMESTAMP="$timestamp"
|
|
166
|
+
echo "$INPUT_JSON" | "$PYTHON_CMD" -c '
|
|
167
|
+
import json, sys, os, re
|
|
168
|
+
|
|
169
|
+
_SECRET_RE = re.compile(
|
|
170
|
+
r"(?i)(api[_-]?key|token|secret|password|authorization|credentials?|auth)"
|
|
171
|
+
r"""(["'"'"'\s:=]+)"""
|
|
172
|
+
r"([A-Za-z]+\s+)?"
|
|
173
|
+
r"([A-Za-z0-9_\-/.+=]{8,})"
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
raw = sys.stdin.read()[:2000]
|
|
177
|
+
raw = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", raw)
|
|
178
|
+
print(json.dumps({"timestamp": os.environ["TIMESTAMP"], "event": "parse_error", "raw": raw}))
|
|
179
|
+
' >> "$OBSERVATIONS_FILE"
|
|
180
|
+
exit 0
|
|
181
|
+
fi
|
|
182
|
+
|
|
183
|
+
# Archive if file too large (atomic: rename with unique suffix to avoid race)
|
|
184
|
+
if [ -f "$OBSERVATIONS_FILE" ]; then
|
|
185
|
+
file_size_mb=$(du -m "$OBSERVATIONS_FILE" 2>/dev/null | cut -f1)
|
|
186
|
+
if [ "${file_size_mb:-0}" -ge "$MAX_FILE_SIZE_MB" ]; then
|
|
187
|
+
archive_dir="${PROJECT_DIR}/observations.archive"
|
|
188
|
+
mkdir -p "$archive_dir"
|
|
189
|
+
mv "$OBSERVATIONS_FILE" "$archive_dir/observations-$(date +%Y%m%d-%H%M%S)-$$.jsonl" 2>/dev/null || true
|
|
190
|
+
fi
|
|
191
|
+
fi
|
|
192
|
+
|
|
193
|
+
# Build and write observation (now includes project context)
|
|
194
|
+
# Scrub common secret patterns from tool I/O before persisting
|
|
195
|
+
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
|
196
|
+
|
|
197
|
+
export PROJECT_ID_ENV="$PROJECT_ID"
|
|
198
|
+
export PROJECT_NAME_ENV="$PROJECT_NAME"
|
|
199
|
+
export TIMESTAMP="$timestamp"
|
|
200
|
+
|
|
201
|
+
echo "$PARSED" | "$PYTHON_CMD" -c '
|
|
202
|
+
import json, sys, os, re
|
|
203
|
+
|
|
204
|
+
parsed = json.load(sys.stdin)
|
|
205
|
+
observation = {
|
|
206
|
+
"timestamp": os.environ["TIMESTAMP"],
|
|
207
|
+
"event": parsed["event"],
|
|
208
|
+
"tool": parsed["tool"],
|
|
209
|
+
"session": parsed["session"],
|
|
210
|
+
"project_id": os.environ.get("PROJECT_ID_ENV", "global"),
|
|
211
|
+
"project_name": os.environ.get("PROJECT_NAME_ENV", "global")
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
# Scrub secrets: match common key=value, key: value, and key"value patterns
|
|
215
|
+
# Includes optional auth scheme (e.g., "Bearer", "Basic") before token
|
|
216
|
+
_SECRET_RE = re.compile(
|
|
217
|
+
r"(?i)(api[_-]?key|token|secret|password|authorization|credentials?|auth)"
|
|
218
|
+
r"""(["'"'"'\s:=]+)"""
|
|
219
|
+
r"([A-Za-z]+\s+)?"
|
|
220
|
+
r"([A-Za-z0-9_\-/.+=]{8,})"
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
def scrub(val):
|
|
224
|
+
if val is None:
|
|
225
|
+
return None
|
|
226
|
+
return _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", str(val))
|
|
227
|
+
|
|
228
|
+
if parsed["input"]:
|
|
229
|
+
observation["input"] = scrub(parsed["input"])
|
|
230
|
+
if parsed["output"] is not None:
|
|
231
|
+
observation["output"] = scrub(parsed["output"])
|
|
232
|
+
|
|
233
|
+
print(json.dumps(observation))
|
|
234
|
+
' >> "$OBSERVATIONS_FILE"
|
|
235
|
+
|
|
236
|
+
# Signal observer if running (check both project-scoped and global observer)
|
|
237
|
+
for pid_file in "${PROJECT_DIR}/.observer.pid" "${CONFIG_DIR}/.observer.pid"; do
|
|
238
|
+
if [ -f "$pid_file" ]; then
|
|
239
|
+
observer_pid=$(cat "$pid_file")
|
|
240
|
+
if kill -0 "$observer_pid" 2>/dev/null; then
|
|
241
|
+
kill -USR1 "$observer_pid" 2>/dev/null || true
|
|
242
|
+
fi
|
|
243
|
+
fi
|
|
244
|
+
done
|
|
245
|
+
|
|
246
|
+
exit 0
|