@bolloon/bolloon-agent 0.1.1 → 0.1.3
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/bin/bolloon-cli.cjs +165 -0
- package/bin/bolloon-daemon.sh +207 -0
- package/bin/bolloon.cmd +11 -0
- package/dist/agents/constraint-layer.js +10 -15
- package/dist/agents/pi-sdk.js +433 -106
- package/dist/agents/protocol.js +82 -1
- package/dist/agents/subagent-manager.js +2 -2
- package/dist/agents/workflow-engine.js +15 -20
- package/dist/agents/workflow-pivot-loop.js +541 -0
- package/dist/bollharness/src/index.js +5 -0
- package/dist/bollharness/src/scripts/checks/check_adr_plan_numbering.js +6 -0
- package/dist/bollharness/src/scripts/checks/check_api_types.js +45 -0
- package/dist/bollharness/src/scripts/checks/check_artifact_link.js +146 -0
- package/dist/bollharness/src/scripts/checks/check_bridge_deps.js +6 -0
- package/dist/bollharness/src/scripts/checks/check_bugfix_binding.js +6 -0
- package/dist/bollharness/src/scripts/checks/check_bugfix_binding_ci.js +6 -0
- package/dist/bollharness/src/scripts/checks/check_doc_file_references.js +6 -0
- package/dist/bollharness/src/scripts/checks/check_doc_freshness.js +135 -0
- package/dist/bollharness/src/scripts/checks/check_doc_links.js +31 -0
- package/dist/bollharness/src/scripts/checks/check_file_existence_claims.js +6 -0
- package/dist/bollharness/src/scripts/checks/check_fragment_integrity.js +34 -0
- package/dist/bollharness/src/scripts/checks/check_hook_installed.js +63 -0
- package/dist/bollharness/src/scripts/checks/check_issue_closure.js +41 -0
- package/dist/bollharness/src/scripts/checks/check_mcp_parity.js +6 -0
- package/dist/bollharness/src/scripts/checks/check_security.js +48 -0
- package/dist/bollharness/src/scripts/checks/check_skill_parity.js +6 -0
- package/dist/bollharness/src/scripts/checks/check_versions.js +6 -0
- package/dist/bollharness/src/scripts/checks/finding.js +13 -0
- package/dist/bollharness/src/scripts/checks/next_decision_number.js +20 -0
- package/dist/bollharness/src/scripts/checks/regenerate_magic_docs.js +6 -0
- package/dist/bollharness/src/scripts/ci/detect_rebaseline_triggers.js +8 -0
- package/dist/bollharness/src/scripts/ci/scan_subprocess_cfg.js +8 -0
- package/dist/bollharness/src/scripts/ci/scan_verify_artifacts.js +8 -0
- package/dist/bollharness/src/scripts/ci/scan_yaml_schema.js +8 -0
- package/dist/bollharness/src/scripts/context_router.js +67 -0
- package/dist/bollharness/src/scripts/deploy-guard.js +157 -0
- package/dist/bollharness/src/scripts/guard-feedback.js +192 -0
- package/dist/bollharness/src/scripts/guard_router.js +158 -0
- package/dist/bollharness/src/scripts/hooks/_hook_output.js +6 -0
- package/dist/bollharness/src/scripts/hooks/auto-python3.js +6 -0
- package/dist/bollharness/src/scripts/hooks/deploy-progress-on-session-end.js +6 -0
- package/dist/bollharness/src/scripts/hooks/failure-analyzer.js +6 -0
- package/dist/bollharness/src/scripts/hooks/gate-judgment-inject.js +92 -0
- package/dist/bollharness/src/scripts/hooks/gate-transition-judgment.js +63 -0
- package/dist/bollharness/src/scripts/hooks/inbox-ack.js +6 -0
- package/dist/bollharness/src/scripts/hooks/inbox-inject-on-start.js +6 -0
- package/dist/bollharness/src/scripts/hooks/inbox-validate.js +6 -0
- package/dist/bollharness/src/scripts/hooks/inbox-write-ledger.js +6 -0
- package/dist/bollharness/src/scripts/hooks/initializer-agent.js +6 -0
- package/dist/bollharness/src/scripts/hooks/loop-detection.js +73 -0
- package/dist/bollharness/src/scripts/hooks/owner-guard.js +6 -0
- package/dist/bollharness/src/scripts/hooks/precompact.js +6 -0
- package/dist/bollharness/src/scripts/hooks/review-agent-gatekeeper.js +6 -0
- package/dist/bollharness/src/scripts/hooks/risk-tracker.js +108 -0
- package/dist/bollharness/src/scripts/hooks/sanitize-on-read.js +6 -0
- package/dist/bollharness/src/scripts/hooks/session-reflection.js +7 -0
- package/dist/bollharness/src/scripts/hooks/session-start-magic-docs.js +7 -0
- package/dist/bollharness/src/scripts/hooks/session-start-reset-risk.js +7 -0
- package/dist/bollharness/src/scripts/hooks/session-start-toolkit-reminder.js +7 -0
- package/dist/bollharness/src/scripts/hooks/stop-evaluator.js +157 -0
- package/dist/bollharness/src/scripts/hooks/tool-call-counter.js +6 -0
- package/dist/bollharness/src/scripts/hooks/trace-analyzer.js +10 -0
- package/dist/bollharness/src/scripts/install/install-trust-token.js +7 -0
- package/dist/bollharness/src/scripts/install/multi_project_registry.js +9 -0
- package/dist/bollharness/src/scripts/install/phase2_auto.js +21 -0
- package/dist/bollharness/src/scripts/install/pre_commit_installer.js +6 -0
- package/dist/bollharness/src/scripts/install/tier_selector.js +7 -0
- package/dist/bollharness/src/scripts/install/transcript_miner.js +7 -0
- package/dist/bollharness/src/scripts/lib/claim_patterns.js +10 -0
- package/dist/bollharness/src/scripts/lib/sanitize_patterns.js +12 -0
- package/dist/bollharness/src/scripts/sanitize.js +6 -0
- package/dist/bollharness-integration/channel-judgment-engine.js +530 -0
- package/dist/bollharness-integration/context-chain-router.js +383 -0
- package/dist/bollharness-integration/context-router-judgment.js +13 -21
- package/dist/bollharness-integration/context-router.js +22 -64
- package/dist/bollharness-integration/gate-state-machine.js +14 -19
- package/dist/bollharness-integration/gate-transition-hooks.js +16 -61
- package/dist/bollharness-integration/guard-checker.js +21 -68
- package/dist/bollharness-integration/index.js +14 -124
- package/dist/bollharness-integration/integration.js +13 -20
- package/dist/bollharness-integration/llm-judgment-engine.js +569 -0
- package/dist/bollharness-integration/skill-adapter.js +18 -64
- package/dist/cli-entry.js +261 -0
- package/dist/constraint-runtime/src/commands.js +17 -7
- package/dist/constraint-runtime/src/constraint/budget.js +1 -6
- package/dist/constraint-runtime/src/constraint/permission.js +1 -6
- package/dist/constraint-runtime/src/models.js +1 -3
- package/dist/constraint-runtime/src/tools.js +17 -7
- package/dist/constraints/index.js +1 -7
- package/dist/documents/reader.js +8 -49
- package/dist/heartbeat/DaemonManager.js +242 -0
- package/dist/heartbeat/HealthMonitor.js +285 -0
- package/dist/heartbeat/StartupVerifier.js +205 -0
- package/dist/heartbeat/Watchdog.js +168 -0
- package/dist/heartbeat/index.js +84 -0
- package/dist/heartbeat/types.js +5 -0
- package/dist/index.js +381 -28
- package/dist/llm/config-store.js +31 -57
- package/dist/llm/llm-judgment-client.js +389 -0
- package/dist/llm/pi-ai.js +9 -52
- package/dist/network/agent-network.js +46 -90
- package/dist/network/hybrid-messenger.js +125 -0
- package/dist/network/iroh-bootstrap.js +38 -0
- package/dist/network/iroh-discovery.js +145 -0
- package/dist/network/iroh-integration.js +9 -16
- package/dist/network/iroh-transport.js +10 -48
- package/dist/network/p2p.js +23 -62
- package/dist/network/storage/adapters/json-adapter.js +4 -42
- package/dist/network/storage/index.js +147 -0
- package/dist/network/storage/types.js +14 -0
- package/dist/pi-ecosystem/index.js +233 -0
- package/dist/pi-ecosystem-colony/index.js +29 -90
- package/dist/pi-ecosystem-goals/index.js +20 -74
- package/dist/pi-ecosystem-judgment/decision.js +29 -47
- package/dist/pi-ecosystem-judgment/distillation.js +16 -29
- package/dist/pi-ecosystem-judgment/human-value-store.js +13 -60
- package/dist/pi-ecosystem-judgment/index.js +21 -74
- package/dist/pi-ecosystem-judgment/value-injection.js +26 -72
- package/dist/pi-ecosystem-mcp/index.js +24 -78
- package/dist/pi-ecosystem-subagents/index.js +20 -69
- package/dist/social/ant-colony/AdaptiveHeartbeat.js +3 -8
- package/dist/social/ant-colony/PheromoneEngine.js +11 -49
- package/dist/social/ant-colony/index.js +6 -0
- package/dist/social/ant-colony/types.js +4 -8
- package/dist/social/channels/ChannelManager.js +8 -46
- package/dist/social/channels/DiapChannelBridge.js +9 -47
- package/dist/social/channels/InterestMatcher.js +2 -7
- package/dist/social/channels/channel-agent-session.js +309 -0
- package/dist/social/channels/channel-heartbeat-agent.js +494 -0
- package/dist/social/channels/diap-doc-parser.js +204 -0
- package/dist/social/channels/harness-workflow-integrator.js +446 -0
- package/dist/social/channels/index.js +9 -0
- package/dist/social/channels/types.js +3 -7
- package/dist/social/global-shared-context.js +6 -47
- package/dist/social/heartbeat.js +29 -72
- package/dist/social/persona/enhanced-persona.js +299 -0
- package/dist/web/client.js +302 -136
- package/dist/web/components/p2p/index.js +159 -9
- package/dist/web/components/p2p/p2p-connection.js +136 -0
- package/dist/web/components/p2p/p2p-manager.js +24 -0
- package/dist/web/components/p2p/p2p-store-memory.js +1 -1
- package/dist/web/components/p2p/types.js +7 -0
- package/dist/web/index.html +5 -0
- package/dist/web/style.css +118 -0
- package/package.json +12 -6
- package/scripts/build-cli.js +206 -0
- package/scripts/postinstall.js +153 -0
- package/src/agents/pi-sdk.ts +347 -28
- package/src/agents/protocol.ts +95 -1
- package/src/agents/workflow-pivot-loop.ts +674 -0
- package/src/bollharness/CLAUDE.md +73 -0
- package/src/bollharness/README.md +143 -0
- package/src/bollharness/README.zh-CN.md +131 -0
- package/src/bollharness/reference/boll-reference/scripts/hooks/stop-evaluator.md +57 -0
- package/src/bollharness/scripts/context-fragments/artifact-linkage.md +14 -0
- package/src/bollharness/scripts/context-fragments/auth-consumers.md +17 -0
- package/src/bollharness/scripts/context-fragments/bridge-constitution.md +13 -0
- package/src/bollharness/scripts/context-fragments/catalyst-distributed.md +18 -0
- package/src/bollharness/scripts/context-fragments/closure-checklist.md +13 -0
- package/src/bollharness/scripts/context-fragments/contract-consumers.md +15 -0
- package/src/bollharness/scripts/context-fragments/db-shared-structures.md +15 -0
- package/src/bollharness/scripts/context-fragments/fixed-three-layers.md +19 -0
- package/src/bollharness/scripts/context-fragments/general-dev-principles.md +11 -0
- package/src/bollharness/scripts/context-fragments/issue-first.md +8 -0
- package/src/bollharness/scripts/context-fragments/mcp-parity.md +16 -0
- package/src/bollharness/scripts/context-fragments/pi-agent-operations.md +108 -0
- package/src/bollharness/scripts/context-fragments/protocol-consumers.md +15 -0
- package/src/bollharness/scripts/context-fragments/run-events-consumers.md +15 -0
- package/src/bollharness/scripts/context-fragments/scene-fidelity.md +13 -0
- package/src/bollharness/scripts/context-fragments/truth-source-hierarchy.md +15 -0
- package/src/bollharness/scripts/context-fragments/two-language.md +15 -0
- package/src/bollharness/scripts/context-fragments/version-sources.md +14 -0
- package/src/bollharness/scripts/hooks/stop-evaluator.md +83 -0
- package/src/bollharness/templates/scaffold/CLAUDE.md +89 -0
- package/src/cli-entry.ts +304 -0
- package/src/heartbeat/DaemonManager.ts +283 -0
- package/src/heartbeat/HealthMonitor.ts +316 -0
- package/src/heartbeat/StartupVerifier.ts +223 -0
- package/src/heartbeat/Watchdog.ts +198 -0
- package/src/heartbeat/index.ts +108 -0
- package/src/heartbeat/types.ts +82 -0
- package/src/llm/config-store.ts +23 -5
- package/src/network/iroh-transport.ts +3 -3
- package/src/web/client.js +302 -136
- package/src/web/components/p2p/P2PModal.tsx +91 -3
- package/src/web/components/p2p/index.ts +171 -9
- package/src/web/components/p2p/p2p-connection.ts +153 -1
- package/src/web/components/p2p/p2p-manager.ts +39 -1
- package/src/web/components/p2p/p2p-store-memory.ts +1 -1
- package/src/web/components/p2p/p2p-tools.ts +315 -0
- package/src/web/components/p2p/types.ts +58 -0
- package/src/web/design.md +99 -0
- package/src/web/index.html +5 -0
- package/src/web/server.ts +353 -36
- package/src/web/style.css +118 -0
- package/tsconfig.cli.json +16 -0
- package/tsconfig.electron.json +1 -1
- package/tsconfig.json +1 -2
- package/dist/web/server.js +0 -1647
- package/dist/web/server.js.map +0 -1
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StartupVerifier - 启动自检验证器
|
|
3
|
+
* 在服务启动时验证所有关键组件
|
|
4
|
+
*/
|
|
5
|
+
export class StartupVerifier {
|
|
6
|
+
checks = new Map();
|
|
7
|
+
/**
|
|
8
|
+
* 添加检查项
|
|
9
|
+
*/
|
|
10
|
+
addCheck(name) {
|
|
11
|
+
this.checks.set(name, { name, status: 'pending' });
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 标记检查开始
|
|
15
|
+
*/
|
|
16
|
+
startCheck(name) {
|
|
17
|
+
const check = this.checks.get(name);
|
|
18
|
+
if (check) {
|
|
19
|
+
check.status = 'running';
|
|
20
|
+
check.startTime = Date.now();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 标记检查完成
|
|
25
|
+
*/
|
|
26
|
+
completeCheck(name, passed, message) {
|
|
27
|
+
const check = this.checks.get(name);
|
|
28
|
+
if (check) {
|
|
29
|
+
check.status = passed ? 'passed' : 'failed';
|
|
30
|
+
check.message = message;
|
|
31
|
+
check.duration_ms = Date.now() - (check.startTime || Date.now());
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 运行所有自检
|
|
36
|
+
*/
|
|
37
|
+
async verify() {
|
|
38
|
+
const startTime = Date.now();
|
|
39
|
+
const critical_failures = [];
|
|
40
|
+
// 1. 检查 Node.js 版本
|
|
41
|
+
this.addCheck('node_version');
|
|
42
|
+
this.startCheck('node_version');
|
|
43
|
+
const nodeVersion = process.version;
|
|
44
|
+
const majorVersion = parseInt(nodeVersion.replace('v', '').split('.')[0], 10);
|
|
45
|
+
if (majorVersion >= 18) {
|
|
46
|
+
this.completeCheck('node_version', true, `Node.js ${nodeVersion}`);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
this.completeCheck('node_version', false, `Node.js ${nodeVersion} too old, need >= 18`);
|
|
50
|
+
critical_failures.push('Node.js version too old');
|
|
51
|
+
}
|
|
52
|
+
// 2. 检查必要目录
|
|
53
|
+
this.addCheck('required_directories');
|
|
54
|
+
this.startCheck('required_directories');
|
|
55
|
+
try {
|
|
56
|
+
const fs = await import('fs/promises');
|
|
57
|
+
const paths = [
|
|
58
|
+
process.env.HOME ? `${process.env.HOME}/.bolloon` : '/tmp/.bolloon',
|
|
59
|
+
'/tmp'
|
|
60
|
+
];
|
|
61
|
+
for (const p of paths) {
|
|
62
|
+
await fs.access(p);
|
|
63
|
+
}
|
|
64
|
+
this.completeCheck('required_directories', true, 'All required directories accessible');
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
this.completeCheck('required_directories', false, err.message);
|
|
68
|
+
critical_failures.push('Cannot access required directories');
|
|
69
|
+
}
|
|
70
|
+
// 3. 检查 LLM 配置
|
|
71
|
+
this.addCheck('llm_config');
|
|
72
|
+
this.startCheck('llm_config');
|
|
73
|
+
const hasLlmKey = !!(process.env.OPENAI_API_KEY ||
|
|
74
|
+
process.env.ANTHROPIC_API_KEY ||
|
|
75
|
+
process.env.MINIMAX_API_KEY ||
|
|
76
|
+
process.env.OPENROUTER_API_KEY ||
|
|
77
|
+
process.env.GEMINI_API_KEY ||
|
|
78
|
+
process.env.OLLAMA_BASE_URL);
|
|
79
|
+
if (hasLlmKey) {
|
|
80
|
+
this.completeCheck('llm_config', true, 'LLM API key found');
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
this.completeCheck('llm_config', false, 'No LLM API key configured (optional)');
|
|
84
|
+
// LLM 配置不是致命的,只是警告
|
|
85
|
+
}
|
|
86
|
+
// 4. 检查 P2P 依赖
|
|
87
|
+
this.addCheck('p2p_modules');
|
|
88
|
+
this.startCheck('p2p_modules');
|
|
89
|
+
try {
|
|
90
|
+
await import('@diap/sdk');
|
|
91
|
+
this.completeCheck('p2p_modules', true, '@diap/sdk available');
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
this.completeCheck('p2p_modules', false, `@diap/sdk not available: ${err.message}`);
|
|
95
|
+
critical_failures.push('@diap/sdk module not found');
|
|
96
|
+
}
|
|
97
|
+
// 5. 检查 IPFS 连接
|
|
98
|
+
this.addCheck('ipfs_connection');
|
|
99
|
+
this.startCheck('ipfs_connection');
|
|
100
|
+
try {
|
|
101
|
+
const ipfsRes = await fetch('http://127.0.0.1:5001/api/v0/id', {
|
|
102
|
+
method: 'POST',
|
|
103
|
+
signal: AbortSignal.timeout(3000)
|
|
104
|
+
});
|
|
105
|
+
if (ipfsRes.ok) {
|
|
106
|
+
this.completeCheck('ipfs_connection', true, 'IPFS local node accessible');
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
this.completeCheck('ipfs_connection', false, 'IPFS returned error');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
this.completeCheck('ipfs_connection', false, 'IPFS not accessible (optional)');
|
|
114
|
+
}
|
|
115
|
+
// 6. 检查内存
|
|
116
|
+
this.addCheck('memory');
|
|
117
|
+
this.startCheck('memory');
|
|
118
|
+
const usage = process.memoryUsage();
|
|
119
|
+
const heapPercent = (usage.heapUsed / usage.heapTotal) * 100;
|
|
120
|
+
if (heapPercent < 90) {
|
|
121
|
+
this.completeCheck('memory', true, `Heap usage ${heapPercent.toFixed(1)}%`);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
this.completeCheck('memory', false, `Heap usage critical: ${heapPercent.toFixed(1)}%`);
|
|
125
|
+
critical_failures.push('Memory usage too high at startup');
|
|
126
|
+
}
|
|
127
|
+
// 7. 检查网络
|
|
128
|
+
this.addCheck('network');
|
|
129
|
+
this.startCheck('network');
|
|
130
|
+
try {
|
|
131
|
+
const res = await fetch('https://www.google.com/generate_204', {
|
|
132
|
+
signal: AbortSignal.timeout(5000)
|
|
133
|
+
});
|
|
134
|
+
this.completeCheck('network', true, 'Internet connection available');
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
this.completeCheck('network', false, 'No internet connection (optional)');
|
|
138
|
+
}
|
|
139
|
+
const totalDuration = Date.now() - startTime;
|
|
140
|
+
return {
|
|
141
|
+
success: critical_failures.length === 0,
|
|
142
|
+
checks: Array.from(this.checks.values()),
|
|
143
|
+
total_duration_ms: totalDuration,
|
|
144
|
+
critical_failures
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 打印检查报告
|
|
149
|
+
*/
|
|
150
|
+
printReport(report) {
|
|
151
|
+
console.log('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
152
|
+
console.log('🚀 Bolloon 启动自检报告');
|
|
153
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
154
|
+
for (const check of report.checks) {
|
|
155
|
+
const icon = check.status === 'passed' ? '✅' :
|
|
156
|
+
check.status === 'failed' ? '❌' :
|
|
157
|
+
check.status === 'running' ? '⏳' : '⭕';
|
|
158
|
+
const duration = check.duration_ms ? ` (${check.duration_ms}ms)` : '';
|
|
159
|
+
console.log(` ${icon} ${check.name}${duration}`);
|
|
160
|
+
if (check.message) {
|
|
161
|
+
console.log(` ${check.message}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
165
|
+
console.log(`总计耗时: ${report.total_duration_ms}ms`);
|
|
166
|
+
if (report.success) {
|
|
167
|
+
console.log('✅ 所有关键检查通过!');
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
console.log('❌ 关键检查失败:');
|
|
171
|
+
for (const failure of report.critical_failures) {
|
|
172
|
+
console.log(` · ${failure}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 快速验证(不打印)
|
|
179
|
+
*/
|
|
180
|
+
async quickCheck() {
|
|
181
|
+
const report = await this.verify();
|
|
182
|
+
return report.success;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 获取检查状态
|
|
186
|
+
*/
|
|
187
|
+
getChecks() {
|
|
188
|
+
return Array.from(this.checks.values());
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// 导出便捷函数
|
|
192
|
+
let startupVerifierInstance = null;
|
|
193
|
+
export async function runStartupVerification() {
|
|
194
|
+
const verifier = new StartupVerifier();
|
|
195
|
+
startupVerifierInstance = verifier;
|
|
196
|
+
const report = await verifier.verify();
|
|
197
|
+
verifier.printReport(report);
|
|
198
|
+
return report;
|
|
199
|
+
}
|
|
200
|
+
export function getStartupVerifier() {
|
|
201
|
+
if (!startupVerifierInstance) {
|
|
202
|
+
startupVerifierInstance = new StartupVerifier();
|
|
203
|
+
}
|
|
204
|
+
return startupVerifierInstance;
|
|
205
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Watchdog - 看门狗机制
|
|
3
|
+
* 检测进程卡死、无响应等情况
|
|
4
|
+
*/
|
|
5
|
+
export class Watchdog {
|
|
6
|
+
config;
|
|
7
|
+
state;
|
|
8
|
+
checkIntervalId = null;
|
|
9
|
+
restartStrategies = new Map();
|
|
10
|
+
restartCount = 0;
|
|
11
|
+
callbacks;
|
|
12
|
+
constructor(config = {}, callbacks = {}) {
|
|
13
|
+
this.config = {
|
|
14
|
+
silentThresholdMs: config.silentThresholdMs || 300000, // 5 分钟无日志
|
|
15
|
+
maxConsecutiveFailures: config.maxConsecutiveFailures || 3,
|
|
16
|
+
checkIntervalMs: config.checkIntervalMs || 30000 // 30 秒检查一次
|
|
17
|
+
};
|
|
18
|
+
this.state = {
|
|
19
|
+
lastActivityTime: Date.now(),
|
|
20
|
+
consecutiveFailures: 0,
|
|
21
|
+
isTriggered: false
|
|
22
|
+
};
|
|
23
|
+
this.callbacks = callbacks;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 注册重启策略
|
|
27
|
+
*/
|
|
28
|
+
registerRestartStrategy(level, action) {
|
|
29
|
+
this.restartStrategies.set(level, action);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 记录活动(调用后更新 lastActivityTime)
|
|
33
|
+
*/
|
|
34
|
+
recordActivity(component) {
|
|
35
|
+
this.state.lastActivityTime = Date.now();
|
|
36
|
+
if (component) {
|
|
37
|
+
this.callbacks.onLog?.(`[Watchdog] Activity from ${component}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 记录日志(同时触发活动检测)
|
|
42
|
+
*/
|
|
43
|
+
log(message) {
|
|
44
|
+
this.recordActivity();
|
|
45
|
+
console.log(`[Watchdog] ${message}`);
|
|
46
|
+
this.callbacks.onLog?.(message);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 开始看门狗监控
|
|
50
|
+
*/
|
|
51
|
+
start() {
|
|
52
|
+
if (this.checkIntervalId)
|
|
53
|
+
return;
|
|
54
|
+
console.log(`[Watchdog] Started - silent threshold: ${this.config.silentThresholdMs}ms, check interval: ${this.config.checkIntervalMs}ms`);
|
|
55
|
+
this.checkIntervalId = setInterval(() => {
|
|
56
|
+
this.check();
|
|
57
|
+
}, this.config.checkIntervalMs);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 停止看门狗
|
|
61
|
+
*/
|
|
62
|
+
stop() {
|
|
63
|
+
if (this.checkIntervalId) {
|
|
64
|
+
clearInterval(this.checkIntervalId);
|
|
65
|
+
this.checkIntervalId = null;
|
|
66
|
+
}
|
|
67
|
+
console.log('[Watchdog] Stopped');
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 报告健康检查结果
|
|
71
|
+
*/
|
|
72
|
+
reportHealthCheck(success, details) {
|
|
73
|
+
if (success) {
|
|
74
|
+
this.state.consecutiveFailures = 0;
|
|
75
|
+
this.recordActivity('health_check');
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.state.consecutiveFailures++;
|
|
79
|
+
console.log(`[Watchdog] Health check failed (${this.state.consecutiveFailures}/${this.config.maxConsecutiveFailures}): ${details || 'unknown'}`);
|
|
80
|
+
this.callbacks.onHealthCheckFailed?.(this.state.consecutiveFailures);
|
|
81
|
+
if (this.state.consecutiveFailures >= this.config.maxConsecutiveFailures) {
|
|
82
|
+
this.triggerRestart(1, `Health check failed ${this.state.consecutiveFailures} times`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 获取当前状态
|
|
88
|
+
*/
|
|
89
|
+
getState() {
|
|
90
|
+
return {
|
|
91
|
+
...this.state,
|
|
92
|
+
uptime_ms: Date.now() - this.state.lastActivityTime,
|
|
93
|
+
restartCount: this.restartCount
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 执行检查
|
|
98
|
+
*/
|
|
99
|
+
check() {
|
|
100
|
+
const now = Date.now();
|
|
101
|
+
const silentTime = now - this.state.lastActivityTime;
|
|
102
|
+
// 检查是否沉默太久
|
|
103
|
+
if (silentTime > this.config.silentThresholdMs) {
|
|
104
|
+
console.warn(`[Watchdog] No activity for ${Math.round(silentTime / 1000)}s`);
|
|
105
|
+
this.triggerRestart(1, `No activity for ${Math.round(silentTime / 1000)}s`);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
// 检查内存使用
|
|
109
|
+
const usage = process.memoryUsage();
|
|
110
|
+
const heapUsedPercent = (usage.heapUsed / usage.heapTotal) * 100;
|
|
111
|
+
if (heapUsedPercent > 90) {
|
|
112
|
+
console.warn(`[Watchdog] Memory usage critical: ${heapUsedPercent.toFixed(1)}%`);
|
|
113
|
+
this.triggerRestart(1, `Memory usage ${heapUsedPercent.toFixed(1)}%`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 触发重启
|
|
118
|
+
*/
|
|
119
|
+
triggerRestart(level, reason) {
|
|
120
|
+
if (this.state.isTriggered) {
|
|
121
|
+
console.log(`[Watchdog] Already triggered, ignoring restart request: ${reason}`);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
this.state.isTriggered = true;
|
|
125
|
+
this.state.triggerReason = reason;
|
|
126
|
+
this.restartCount++;
|
|
127
|
+
console.error(`[Watchdog] 🔴 TRIGGERED: ${reason} (restart #${this.restartCount})`);
|
|
128
|
+
const strategy = this.restartStrategies.get(level);
|
|
129
|
+
if (strategy) {
|
|
130
|
+
try {
|
|
131
|
+
this.callbacks.onRestart?.(level, reason);
|
|
132
|
+
const result = strategy();
|
|
133
|
+
if (result instanceof Promise) {
|
|
134
|
+
result.catch(err => {
|
|
135
|
+
console.error('[Watchdog] Restart action failed:', err);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
console.error('[Watchdog] Failed to execute restart strategy:', err);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
console.warn(`[Watchdog] No restart strategy registered for level ${level}`);
|
|
145
|
+
this.callbacks.onRestart?.(level, reason);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 重置触发状态(允许再次触发)
|
|
150
|
+
*/
|
|
151
|
+
reset() {
|
|
152
|
+
this.state.isTriggered = false;
|
|
153
|
+
this.state.triggerReason = undefined;
|
|
154
|
+
this.state.lastActivityTime = Date.now();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// 全局实例
|
|
158
|
+
let watchdogInstance = null;
|
|
159
|
+
export function getWatchdog() {
|
|
160
|
+
if (!watchdogInstance) {
|
|
161
|
+
watchdogInstance = new Watchdog();
|
|
162
|
+
}
|
|
163
|
+
return watchdogInstance;
|
|
164
|
+
}
|
|
165
|
+
export function createWatchdog(config, callbacks) {
|
|
166
|
+
watchdogInstance = new Watchdog(config, callbacks);
|
|
167
|
+
return watchdogInstance;
|
|
168
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bolloon 24h Heartbeat Module
|
|
3
|
+
* 24 小时稳定运行心跳闭环
|
|
4
|
+
*/
|
|
5
|
+
export * from './types.js';
|
|
6
|
+
export * from './HealthMonitor.js';
|
|
7
|
+
export * from './Watchdog.js';
|
|
8
|
+
export * from './DaemonManager.js';
|
|
9
|
+
export * from './StartupVerifier.js';
|
|
10
|
+
import { createHealthMonitor, getHealthMonitor } from './HealthMonitor.js';
|
|
11
|
+
import { createWatchdog, getWatchdog } from './Watchdog.js';
|
|
12
|
+
import { createDaemonManager, getDaemonManager } from './DaemonManager.js';
|
|
13
|
+
import { runStartupVerification, getStartupVerifier } from './StartupVerifier.js';
|
|
14
|
+
/**
|
|
15
|
+
* 初始化完整的 24h 心跳系统
|
|
16
|
+
*/
|
|
17
|
+
export async function initialize24hHeartbeat() {
|
|
18
|
+
// 1. 启动自检
|
|
19
|
+
const startupReport = await runStartupVerification();
|
|
20
|
+
if (!startupReport.success) {
|
|
21
|
+
console.warn('[24h] Startup verification failed, continuing anyway...');
|
|
22
|
+
}
|
|
23
|
+
// 2. 初始化健康监控
|
|
24
|
+
const healthMonitor = createHealthMonitor({
|
|
25
|
+
onStatusChange: (status) => {
|
|
26
|
+
if (status.status !== 'healthy') {
|
|
27
|
+
console.warn(`[24h] Health status: ${status.status}`);
|
|
28
|
+
for (const rec of status.recommendations || []) {
|
|
29
|
+
console.warn(`[24h] → ${rec}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
// 3. 初始化看门狗
|
|
35
|
+
const watchdog = createWatchdog({ silentThresholdMs: 300000, maxConsecutiveFailures: 3, checkIntervalMs: 30000 }, {
|
|
36
|
+
onLog: (msg) => console.log(`[24h-Watchdog] ${msg}`),
|
|
37
|
+
onHealthCheckFailed: (failures) => console.warn(`[24h] Health check failures: ${failures}`),
|
|
38
|
+
onRestart: (level, reason) => {
|
|
39
|
+
console.error(`[24h] Restart triggered (level ${level}): ${reason}`);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
// 4. 连接健康监控和看门狗
|
|
43
|
+
healthMonitor.startPeriodicCheck(60000, (status) => {
|
|
44
|
+
const hasError = Object.values(status.checks).some(c => c.status === 'error');
|
|
45
|
+
watchdog.reportHealthCheck(!hasError, status.status);
|
|
46
|
+
});
|
|
47
|
+
// 5. 初始化守护进程管理器
|
|
48
|
+
const daemonManager = createDaemonManager({
|
|
49
|
+
maxRestarts: 5,
|
|
50
|
+
restartDelayMs: 5000
|
|
51
|
+
});
|
|
52
|
+
// 6. 注册看门狗重启策略
|
|
53
|
+
watchdog.registerRestartStrategy(1, () => {
|
|
54
|
+
console.log('[24h] Level 1 restart: Graceful restart...');
|
|
55
|
+
// 可以添加进程重启逻辑
|
|
56
|
+
});
|
|
57
|
+
watchdog.registerRestartStrategy(2, () => {
|
|
58
|
+
console.log('[24h] Level 2 restart: Process restart...');
|
|
59
|
+
// 可以添加进程重启逻辑
|
|
60
|
+
});
|
|
61
|
+
watchdog.registerRestartStrategy(3, () => {
|
|
62
|
+
console.log('[24h] Level 3 restart: System notification...');
|
|
63
|
+
// 可以发送通知
|
|
64
|
+
});
|
|
65
|
+
// 7. 启动看门狗
|
|
66
|
+
watchdog.start();
|
|
67
|
+
console.log('[24h] 24h heartbeat system initialized');
|
|
68
|
+
console.log(`[24h] Health checks: every 60s`);
|
|
69
|
+
console.log(`[24h] Watchdog checks: every 30s`);
|
|
70
|
+
console.log(`[24h] Max restarts: 5`);
|
|
71
|
+
return { healthMonitor, watchdog, daemonManager, startupVerifier: getStartupVerifier() };
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* 获取健康状态 JSON
|
|
75
|
+
*/
|
|
76
|
+
export async function getHealthStatus() {
|
|
77
|
+
const healthMonitor = getHealthMonitor();
|
|
78
|
+
return healthMonitor.check();
|
|
79
|
+
}
|
|
80
|
+
// Re-export singleton getters for convenience
|
|
81
|
+
export { getHealthMonitor, createHealthMonitor };
|
|
82
|
+
export { getWatchdog, createWatchdog };
|
|
83
|
+
export { getDaemonManager, createDaemonManager };
|
|
84
|
+
export { getStartupVerifier };
|