@geravant/sinain 1.0.1

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 (53) hide show
  1. package/README.md +183 -0
  2. package/index.ts +2096 -0
  3. package/install.js +155 -0
  4. package/openclaw.plugin.json +59 -0
  5. package/package.json +21 -0
  6. package/sinain-memory/common.py +403 -0
  7. package/sinain-memory/demo_knowledge_transfer.sh +85 -0
  8. package/sinain-memory/embedder.py +268 -0
  9. package/sinain-memory/eval/__init__.py +0 -0
  10. package/sinain-memory/eval/assertions.py +288 -0
  11. package/sinain-memory/eval/judges/__init__.py +0 -0
  12. package/sinain-memory/eval/judges/base_judge.py +61 -0
  13. package/sinain-memory/eval/judges/curation_judge.py +46 -0
  14. package/sinain-memory/eval/judges/insight_judge.py +48 -0
  15. package/sinain-memory/eval/judges/mining_judge.py +42 -0
  16. package/sinain-memory/eval/judges/signal_judge.py +45 -0
  17. package/sinain-memory/eval/schemas.py +247 -0
  18. package/sinain-memory/eval_delta.py +109 -0
  19. package/sinain-memory/eval_reporter.py +642 -0
  20. package/sinain-memory/feedback_analyzer.py +221 -0
  21. package/sinain-memory/git_backup.sh +19 -0
  22. package/sinain-memory/insight_synthesizer.py +181 -0
  23. package/sinain-memory/memory/2026-03-01.md +11 -0
  24. package/sinain-memory/memory/playbook-archive/sinain-playbook-2026-03-01-1418.md +15 -0
  25. package/sinain-memory/memory/playbook-logs/2026-03-01.jsonl +1 -0
  26. package/sinain-memory/memory/sinain-playbook.md +21 -0
  27. package/sinain-memory/memory-config.json +39 -0
  28. package/sinain-memory/memory_miner.py +183 -0
  29. package/sinain-memory/module_manager.py +695 -0
  30. package/sinain-memory/playbook_curator.py +225 -0
  31. package/sinain-memory/requirements.txt +3 -0
  32. package/sinain-memory/signal_analyzer.py +141 -0
  33. package/sinain-memory/test_local.py +402 -0
  34. package/sinain-memory/tests/__init__.py +0 -0
  35. package/sinain-memory/tests/conftest.py +189 -0
  36. package/sinain-memory/tests/test_curator_helpers.py +94 -0
  37. package/sinain-memory/tests/test_embedder.py +210 -0
  38. package/sinain-memory/tests/test_extract_json.py +124 -0
  39. package/sinain-memory/tests/test_feedback_computation.py +121 -0
  40. package/sinain-memory/tests/test_miner_helpers.py +71 -0
  41. package/sinain-memory/tests/test_module_management.py +458 -0
  42. package/sinain-memory/tests/test_parsers.py +96 -0
  43. package/sinain-memory/tests/test_tick_evaluator.py +430 -0
  44. package/sinain-memory/tests/test_triple_extractor.py +255 -0
  45. package/sinain-memory/tests/test_triple_ingest.py +191 -0
  46. package/sinain-memory/tests/test_triple_migrate.py +138 -0
  47. package/sinain-memory/tests/test_triplestore.py +248 -0
  48. package/sinain-memory/tick_evaluator.py +392 -0
  49. package/sinain-memory/triple_extractor.py +402 -0
  50. package/sinain-memory/triple_ingest.py +290 -0
  51. package/sinain-memory/triple_migrate.py +275 -0
  52. package/sinain-memory/triple_query.py +184 -0
  53. package/sinain-memory/triplestore.py +498 -0
package/README.md ADDED
@@ -0,0 +1,183 @@
1
+ # sinain-hud OpenClaw Plugin
2
+
3
+ Plugin for the [anthillnet fork of OpenClaw](https://github.com/anthillnet/openclaw) that manages the sinain-hud agent lifecycle on the server.
4
+
5
+ ## What It Does
6
+
7
+ Five lifecycle hooks, one tool, four commands, and a background service:
8
+
9
+ ### Hooks
10
+
11
+ | Hook | Purpose |
12
+ |---|---|
13
+ | `session_start` | Initializes per-session tool usage and compliance tracking |
14
+ | `before_agent_start` | Syncs HEARTBEAT.md, SKILL.md, sinain-memory/ (recursively, including eval/), and modules/ from `sinain-sources/` to workspace; generates effective playbook; creates memory directories |
15
+ | `tool_result_persist` | Strips `<private>` tags from tool results; tracks `sinain_heartbeat_tick` calls for compliance validation |
16
+ | `agent_end` | Writes structured session summary; validates heartbeat compliance (warns on skip, escalates after 3 consecutive skips) |
17
+ | `session_end` | Cleans up orphaned session state |
18
+
19
+ ### Tool
20
+
21
+ | Tool | Purpose |
22
+ |---|---|
23
+ | `sinain_heartbeat_tick` | Executes all heartbeat mechanical work (git backup, signal analysis, insight synthesis, log writing). Returns structured JSON with results, recommended actions, and Telegram output. |
24
+
25
+ The heartbeat tool accepts `{ sessionSummary: string, idle: boolean }` and runs:
26
+ 1. `bash sinain-memory/git_backup.sh` (30s timeout)
27
+ 2. `uv run python3 sinain-memory/signal_analyzer.py` (60s timeout)
28
+ 3. `uv run python3 sinain-memory/insight_synthesizer.py` (60s timeout)
29
+ 4. Writes log entry to `memory/playbook-logs/YYYY-MM-DD.jsonl`
30
+
31
+ ### Commands
32
+
33
+ | Command | Purpose |
34
+ |---|---|
35
+ | `/sinain_status` | Shows persistent session data from `sessions.json` (update time, tokens, compactions, transcript size) and resilience metrics |
36
+ | `/sinain_modules` | Shows active knowledge module stack, suspended and disabled modules |
37
+ | `/sinain_eval` | Shows latest evaluation report and recent tick evaluation metrics |
38
+ | `/sinain_eval_level` | Sets evaluation level: `mechanical`, `sampled`, or `full` |
39
+
40
+ ### Service
41
+
42
+ **Curation pipeline** — runs every 30 minutes in the background:
43
+ 1. Feedback analysis (`feedback_analyzer.py`) → extracts `curateDirective` + effectiveness metrics
44
+ 2. Memory mining (`memory_miner.py`) → reads unread daily memory files
45
+ 3. Playbook curation (`playbook_curator.py`) → archives, applies changes
46
+ 4. Effectiveness footer update → writes metrics into playbook
47
+ 5. Effective playbook regeneration → merges base playbook + active module patterns
48
+ 6. Tick evaluation (`tick_evaluator.py`) → runs mechanical + sampled judges (120s timeout)
49
+ 7. Daily eval report (`eval_reporter.py`) → generates report once per day after 03:00 UTC
50
+
51
+ ## Configuration
52
+
53
+ Configured in `openclaw.json` under `plugins.entries.sinain-hud`:
54
+
55
+ ```json
56
+ {
57
+ "plugins": {
58
+ "entries": {
59
+ "sinain-hud": {
60
+ "enabled": true,
61
+ "config": {
62
+ "heartbeatPath": "/home/node/.openclaw/sinain-sources/HEARTBEAT.md",
63
+ "skillPath": "/home/node/.openclaw/sinain-sources/SKILL.md",
64
+ "memoryPath": "/home/node/.openclaw/sinain-sources/sinain-memory",
65
+ "modulesPath": "/home/node/.openclaw/sinain-sources/modules",
66
+ "sessionKey": "agent:main:sinain"
67
+ }
68
+ }
69
+ }
70
+ }
71
+ }
72
+ ```
73
+
74
+ | Field | Type | Description |
75
+ |---|---|---|
76
+ | `heartbeatPath` | string | Path to HEARTBEAT.md source (resolved relative to state dir) |
77
+ | `skillPath` | string | Path to SKILL.md source |
78
+ | `memoryPath` | string | Path to sinain-memory/ scripts directory |
79
+ | `modulesPath` | string | Path to modules/ directory for knowledge module system |
80
+ | `sessionKey` | string | Session key for the sinain agent |
81
+
82
+ ## File Auto-Deploy
83
+
84
+ The `before_agent_start` hook copies files from the persistent source directory to the agent workspace:
85
+
86
+ ```
87
+ /mnt/openclaw-state/sinain-sources/ → /home/node/.openclaw/workspace/
88
+ HEARTBEAT.md HEARTBEAT.md
89
+ SKILL.md SKILL.md
90
+ sinain-memory/ sinain-memory/
91
+ *.json, *.sh, *.txt (always overwritten)
92
+ *.py (deploy-once — skip if exists)
93
+ modules/ modules/
94
+ manifest.json (always overwritten)
95
+ module-registry.json (deploy-once)
96
+ */patterns.md (deploy-once)
97
+ sinain-memory/eval/ sinain-memory/eval/ (recursive)
98
+ *.py (deploy-once)
99
+ *.json, *.jsonl (always overwritten)
100
+ ```
101
+
102
+ Only writes if content has actually changed (avoids unnecessary git diffs).
103
+
104
+ Also ensures these directories exist:
105
+ - `memory/`, `memory/playbook-archive/`, `memory/playbook-logs/`
106
+ - `memory/eval-logs/`, `memory/eval-reports/`
107
+
108
+ The `git_backup.sh` script is automatically made executable (chmod 755) after sync.
109
+
110
+ After syncing modules, the plugin generates `memory/sinain-playbook-effective.md` — a merged view of active module patterns (sorted by priority) plus the base playbook.
111
+
112
+ ## Heartbeat Compliance Validation
113
+
114
+ The plugin enforces that the agent actually calls `sinain_heartbeat_tick` during heartbeat runs:
115
+
116
+ 1. `tool_result_persist` sets `heartbeatToolCalled = true` when `sinain_heartbeat_tick` is invoked
117
+ 2. `agent_end` checks if the run was a heartbeat (`messageProvider === "heartbeat"`)
118
+ 3. If tool wasn't called: logs warning, increments `consecutiveHeartbeatSkips` counter
119
+ 4. After 3 consecutive skips: logs ESCALATION warning
120
+ 5. A successful tool call resets the counter to 0
121
+
122
+ ## Privacy Tag Stripping
123
+
124
+ The `tool_result_persist` hook intercepts tool results before they're saved to session history. Any `<private>...</private>` blocks are removed from:
125
+ - String content (simple tool results)
126
+ - Text blocks in array content (structured tool results)
127
+
128
+ This is the server-side complement to sense_client's client-side `apply_privacy()` filter.
129
+
130
+ ## Session Summaries
131
+
132
+ On `agent_end`, the plugin appends a JSON line to `memory/session-summaries.jsonl`:
133
+
134
+ ```json
135
+ {
136
+ "ts": "2026-02-18T12:00:00.000Z",
137
+ "sessionKey": "agent:main:sinain",
138
+ "agentId": "...",
139
+ "durationMs": 45000,
140
+ "success": true,
141
+ "error": null,
142
+ "toolCallCount": 12,
143
+ "toolBreakdown": { "sessions_history": 3, "sinain_heartbeat_tick": 1, "Write": 5 },
144
+ "messageCount": 8
145
+ }
146
+ ```
147
+
148
+ ## Context Overflow Watchdog
149
+
150
+ Automatically recovers from runaway context growth that causes repeated agent failures.
151
+
152
+ - **Detection:** Tracks consecutive errors matching `/overloaded|context.*too.*long|token.*limit/i` on `cfg.sessionKey`
153
+ - **Trigger:** 5 consecutive errors + transcript ≥ 1 MB
154
+ - **Action:** Archives transcript via `copyFileSync`, truncates to empty, resets `contextTokens` in `sessions.json`
155
+ - **Resets:** Counter clears on any successful session completion and on `gateway_start`
156
+
157
+ The 1 MB minimum guard prevents resets from transient API outages when the transcript is small.
158
+
159
+ ## Deployment
160
+
161
+ **IMPORTANT:** Use `docker-compose.openclaw.yml` — the default compose file uses unset env vars and will fail.
162
+
163
+ ```bash
164
+ # Upload plugin files to the server
165
+ scp -i ~/.ssh/<your-key> \
166
+ sinain-hud-plugin/index.ts sinain-hud-plugin/openclaw.plugin.json \
167
+ root@<your-server-ip>:/mnt/openclaw-state/extensions/sinain-hud/
168
+
169
+ # Restart gateway to load updated plugin
170
+ ssh -i ~/.ssh/<your-key> root@<your-server-ip> \
171
+ 'cd /opt/openclaw && docker compose -f docker-compose.openclaw.yml restart'
172
+
173
+ # Verify plugin loaded
174
+ ssh -i ~/.ssh/<your-key> root@<your-server-ip> \
175
+ 'cd /opt/openclaw && docker compose -f docker-compose.openclaw.yml logs --tail=30 openclaw-gateway 2>&1 | grep sinain'
176
+ ```
177
+
178
+ ## Files
179
+
180
+ | File | Purpose |
181
+ |---|---|
182
+ | `index.ts` | Plugin implementation (hooks, tool, commands, curation service) |
183
+ | `openclaw.plugin.json` | Plugin manifest (metadata, config schema, UI hints) |