instar 0.7.43 → 0.7.44
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/.claude/skills/setup-wizard/skill.md +9 -40
- package/.vercel/README.txt +11 -0
- package/.vercel/project.json +1 -0
- package/dist/cli.js +0 -0
- package/dist/commands/server.js +74 -0
- package/dist/commands/setup.js +4 -1
- package/dist/core/CaffeinateManager.d.ts +50 -0
- package/dist/core/CaffeinateManager.js +180 -0
- package/dist/lifeline/ServerSupervisor.d.ts +2 -0
- package/dist/lifeline/ServerSupervisor.js +7 -0
- package/dist/lifeline/TelegramLifeline.d.ts +1 -0
- package/dist/lifeline/TelegramLifeline.js +55 -0
- package/dist/monitoring/HealthChecker.d.ts +1 -0
- package/dist/monitoring/HealthChecker.js +22 -0
- package/dist/monitoring/MemoryPressureMonitor.d.ts +83 -0
- package/dist/monitoring/MemoryPressureMonitor.js +242 -0
- package/dist/scaffold/templates.js +31 -8
- package/dist/server/routes.js +9 -0
- package/package.json +1 -1
|
@@ -646,50 +646,19 @@ If Telegram was set up in Phase 3, install the relay script that lets Claude ses
|
|
|
646
646
|
mkdir -p .claude/scripts
|
|
647
647
|
```
|
|
648
648
|
|
|
649
|
-
|
|
649
|
+
**IMPORTANT: Do NOT write a custom telegram-reply.sh.** Instead, copy the canonical version from the instar package:
|
|
650
650
|
|
|
651
651
|
```bash
|
|
652
|
-
|
|
653
|
-
# telegram-reply.sh — Send a message back to a Telegram topic via instar server.
|
|
654
|
-
#
|
|
655
|
-
# Usage:
|
|
656
|
-
# .claude/scripts/telegram-reply.sh TOPIC_ID "message text"
|
|
657
|
-
# echo "message text" | .claude/scripts/telegram-reply.sh TOPIC_ID
|
|
658
|
-
# cat <<'EOF' | .claude/scripts/telegram-reply.sh TOPIC_ID
|
|
659
|
-
# Multi-line message here
|
|
660
|
-
# EOF
|
|
661
|
-
|
|
662
|
-
TOPIC_ID=$1
|
|
663
|
-
shift
|
|
664
|
-
|
|
665
|
-
if [ -z "$TOPIC_ID" ]; then
|
|
666
|
-
echo "Usage: telegram-reply.sh TOPIC_ID [message]" >&2
|
|
667
|
-
exit 1
|
|
668
|
-
fi
|
|
669
|
-
|
|
670
|
-
PORT=<PORT>
|
|
671
|
-
|
|
672
|
-
# Get message from args or stdin
|
|
673
|
-
if [ $# -gt 0 ]; then
|
|
674
|
-
MESSAGE="$*"
|
|
675
|
-
else
|
|
676
|
-
MESSAGE=$(cat)
|
|
677
|
-
fi
|
|
678
|
-
|
|
679
|
-
if [ -z "$MESSAGE" ]; then
|
|
680
|
-
echo "No message provided" >&2
|
|
681
|
-
exit 1
|
|
682
|
-
fi
|
|
683
|
-
|
|
684
|
-
# Send via instar server API
|
|
685
|
-
curl -s -X POST "http://localhost:${PORT}/telegram/topic/${TOPIC_ID}/send" \
|
|
686
|
-
-H 'Content-Type: application/json' \
|
|
687
|
-
-d "$(jq -n --arg text "$MESSAGE" '{text: $text}')" > /dev/null 2>&1
|
|
688
|
-
|
|
689
|
-
echo "Sent $(echo "$MESSAGE" | wc -c | tr -d ' ') chars to topic $TOPIC_ID"
|
|
652
|
+
cp "$(dirname "$(which instar 2>/dev/null || echo "$(npm root -g)/instar")")/templates/scripts/telegram-reply.sh" .claude/scripts/telegram-reply.sh 2>/dev/null
|
|
690
653
|
```
|
|
691
654
|
|
|
692
|
-
|
|
655
|
+
If the copy fails (e.g., npx install), write the script using the template at `node_modules/instar/dist/templates/scripts/telegram-reply.sh` as the source. The key details:
|
|
656
|
+
- **Endpoint**: `POST http://localhost:PORT/telegram/reply/TOPIC_ID` (NOT `/telegram/topic/TOPIC_ID/send`)
|
|
657
|
+
- **Auth**: Must read authToken from `.instar/config.json` and include `Authorization: Bearer TOKEN` header
|
|
658
|
+
- **JSON escaping**: Use python3 for proper JSON escaping, not jq (which may not be installed)
|
|
659
|
+
- **Error reporting**: Do NOT pipe curl output to `/dev/null` — check the HTTP status code and report failures
|
|
660
|
+
|
|
661
|
+
Then make it executable:
|
|
693
662
|
|
|
694
663
|
```bash
|
|
695
664
|
chmod +x .claude/scripts/telegram-reply.sh
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
> Why do I have a folder named ".vercel" in my project?
|
|
2
|
+
The ".vercel" folder is created when you link a directory to a Vercel project.
|
|
3
|
+
|
|
4
|
+
> What does the "project.json" file contain?
|
|
5
|
+
The "project.json" file contains:
|
|
6
|
+
- The ID of the Vercel project that you linked ("projectId")
|
|
7
|
+
- The ID of the user or team your Vercel project is owned by ("orgId")
|
|
8
|
+
|
|
9
|
+
> Should I commit the ".vercel" folder?
|
|
10
|
+
No, you should not share the ".vercel" folder with anyone.
|
|
11
|
+
Upon creation, it will be automatically added to your ".gitignore" file.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"projectId":"prj_evM5LcItYL3IAmw8zNvEPGrHeaya","orgId":"team_dHctwIDcV3X9ydapQlCPHFGI","projectName":"claude-agent-kit"}
|
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/commands/server.js
CHANGED
|
@@ -710,6 +710,77 @@ export async function startServer(options) {
|
|
|
710
710
|
...(config.evolution || {}),
|
|
711
711
|
});
|
|
712
712
|
console.log(pc.green(' Evolution system enabled'));
|
|
713
|
+
// Start MemoryPressureMonitor (platform-aware memory tracking)
|
|
714
|
+
const { MemoryPressureMonitor } = await import('../monitoring/MemoryPressureMonitor.js');
|
|
715
|
+
const memoryMonitor = new MemoryPressureMonitor({});
|
|
716
|
+
memoryMonitor.on('stateChange', ({ from, to, state: memState }) => {
|
|
717
|
+
// Gate scheduler spawning on memory pressure
|
|
718
|
+
if (scheduler && (to === 'elevated' || to === 'critical')) {
|
|
719
|
+
console.log(`[MemoryPressure] ${from} -> ${to} — scheduler should respect canSpawnSession()`);
|
|
720
|
+
}
|
|
721
|
+
// Alert via Telegram attention topic
|
|
722
|
+
if (telegram && to !== 'normal') {
|
|
723
|
+
const attentionTopicId = state.get('agent-attention-topic');
|
|
724
|
+
if (attentionTopicId) {
|
|
725
|
+
telegram.sendToTopic(attentionTopicId, `Memory ${to}: ${memState.pressurePercent.toFixed(1)}% used, ${memState.freeGB.toFixed(1)}GB free (trend: ${memState.trend})`).catch(() => { });
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
});
|
|
729
|
+
memoryMonitor.start();
|
|
730
|
+
// Wire memory gate into scheduler
|
|
731
|
+
if (scheduler) {
|
|
732
|
+
const originalCanRun = scheduler.canRunJob;
|
|
733
|
+
scheduler.canRunJob = (priority) => {
|
|
734
|
+
// Check memory first
|
|
735
|
+
const memCheck = memoryMonitor.canSpawnSession();
|
|
736
|
+
if (!memCheck.allowed) {
|
|
737
|
+
return false;
|
|
738
|
+
}
|
|
739
|
+
// Then check original gate (quota, etc.)
|
|
740
|
+
return originalCanRun(priority);
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
// Start CaffeinateManager (prevents macOS system sleep)
|
|
744
|
+
const { CaffeinateManager } = await import('../core/CaffeinateManager.js');
|
|
745
|
+
const caffeinateManager = new CaffeinateManager({ stateDir: config.stateDir });
|
|
746
|
+
caffeinateManager.start();
|
|
747
|
+
// Start SleepWakeDetector (re-validate sessions on wake)
|
|
748
|
+
const { SleepWakeDetector } = await import('../core/SleepWakeDetector.js');
|
|
749
|
+
const sleepWakeDetector = new SleepWakeDetector();
|
|
750
|
+
sleepWakeDetector.on('wake', async (event) => {
|
|
751
|
+
console.log(`[SleepWake] Wake detected after ~${event.sleepDurationSeconds}s sleep`);
|
|
752
|
+
// Re-validate tmux sessions
|
|
753
|
+
try {
|
|
754
|
+
const tmuxPath = detectTmuxPath();
|
|
755
|
+
if (tmuxPath) {
|
|
756
|
+
const { execFileSync } = await import('child_process');
|
|
757
|
+
const result = execFileSync(tmuxPath, ['list-sessions'], { encoding: 'utf-8', timeout: 5000 }).trim();
|
|
758
|
+
console.log(`[SleepWake] tmux sessions after wake: ${result.split('\n').length}`);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
catch {
|
|
762
|
+
console.warn('[SleepWake] tmux check failed after wake');
|
|
763
|
+
}
|
|
764
|
+
// Restart tunnel if configured
|
|
765
|
+
if (tunnel) {
|
|
766
|
+
try {
|
|
767
|
+
await tunnel.stop();
|
|
768
|
+
const tunnelUrl = await tunnel.start();
|
|
769
|
+
console.log(`[SleepWake] Tunnel restarted: ${tunnelUrl}`);
|
|
770
|
+
}
|
|
771
|
+
catch (err) {
|
|
772
|
+
console.error(`[SleepWake] Tunnel restart failed:`, err);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
// Notify via Telegram attention topic
|
|
776
|
+
if (telegram) {
|
|
777
|
+
const attentionTopicId = state.get('agent-attention-topic');
|
|
778
|
+
if (attentionTopicId) {
|
|
779
|
+
telegram.sendToTopic(attentionTopicId, `Wake detected after ~${event.sleepDurationSeconds}s sleep. Sessions re-validated.`).catch(() => { });
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
});
|
|
783
|
+
sleepWakeDetector.start();
|
|
713
784
|
const server = new AgentServer({ config, sessionManager, state, scheduler, telegram, relationships, feedback, dispatches, updateChecker, autoUpdater, autoDispatcher, quotaTracker, publisher, viewer, tunnel, evolution });
|
|
714
785
|
await server.start();
|
|
715
786
|
// Start tunnel AFTER server is listening
|
|
@@ -726,6 +797,9 @@ export async function startServer(options) {
|
|
|
726
797
|
// Graceful shutdown
|
|
727
798
|
const shutdown = async () => {
|
|
728
799
|
console.log('\nShutting down...');
|
|
800
|
+
memoryMonitor.stop();
|
|
801
|
+
caffeinateManager.stop();
|
|
802
|
+
sleepWakeDetector.stop();
|
|
729
803
|
autoUpdater.stop();
|
|
730
804
|
autoDispatcher?.stop();
|
|
731
805
|
if (tunnel)
|
package/dist/commands/setup.js
CHANGED
|
@@ -813,7 +813,10 @@ ${argsXml}
|
|
|
813
813
|
<key>RunAtLoad</key>
|
|
814
814
|
<true/>
|
|
815
815
|
<key>KeepAlive</key>
|
|
816
|
-
<
|
|
816
|
+
<dict>
|
|
817
|
+
<key>SuccessfulExit</key>
|
|
818
|
+
<false/>
|
|
819
|
+
</dict>
|
|
817
820
|
<key>StandardOutPath</key>
|
|
818
821
|
<string>${escapeXml(path.join(logDir, `${command}-launchd.log`))}</string>
|
|
819
822
|
<key>StandardErrorPath</key>
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CaffeinateManager - Prevent macOS system sleep for Instar server lifetime.
|
|
3
|
+
*
|
|
4
|
+
* Maintains a `caffeinate -s` child process that prevents system sleep.
|
|
5
|
+
* A watchdog verifies it's alive every 30s and restarts if dead.
|
|
6
|
+
* PID is written to <stateDir>/caffeinate.pid for crash recovery.
|
|
7
|
+
*
|
|
8
|
+
* Only activates on macOS (process.platform === 'darwin').
|
|
9
|
+
* Uses EventEmitter pattern consistent with Instar conventions.
|
|
10
|
+
*/
|
|
11
|
+
import { EventEmitter } from 'node:events';
|
|
12
|
+
export interface CaffeinateManagerConfig {
|
|
13
|
+
/** State directory for PID file storage */
|
|
14
|
+
stateDir: string;
|
|
15
|
+
}
|
|
16
|
+
export interface CaffeinateStatus {
|
|
17
|
+
running: boolean;
|
|
18
|
+
pid: number | null;
|
|
19
|
+
startedAt: string | null;
|
|
20
|
+
restartCount: number;
|
|
21
|
+
lastWatchdogCheck: string;
|
|
22
|
+
}
|
|
23
|
+
export declare class CaffeinateManager extends EventEmitter {
|
|
24
|
+
private process;
|
|
25
|
+
private watchdogInterval;
|
|
26
|
+
private pid;
|
|
27
|
+
private startedAt;
|
|
28
|
+
private restartCount;
|
|
29
|
+
private lastWatchdogCheck;
|
|
30
|
+
private stopping;
|
|
31
|
+
private pidFile;
|
|
32
|
+
constructor(config: CaffeinateManagerConfig);
|
|
33
|
+
/**
|
|
34
|
+
* Start caffeinate and the watchdog.
|
|
35
|
+
* Only activates on macOS.
|
|
36
|
+
*/
|
|
37
|
+
start(): void;
|
|
38
|
+
/**
|
|
39
|
+
* Stop caffeinate and the watchdog cleanly.
|
|
40
|
+
*/
|
|
41
|
+
stop(): void;
|
|
42
|
+
getStatus(): CaffeinateStatus;
|
|
43
|
+
private spawnCaffeinate;
|
|
44
|
+
private killCaffeinate;
|
|
45
|
+
private watchdog;
|
|
46
|
+
private cleanupStale;
|
|
47
|
+
private writePidFile;
|
|
48
|
+
private removePidFile;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=CaffeinateManager.d.ts.map
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CaffeinateManager - Prevent macOS system sleep for Instar server lifetime.
|
|
3
|
+
*
|
|
4
|
+
* Maintains a `caffeinate -s` child process that prevents system sleep.
|
|
5
|
+
* A watchdog verifies it's alive every 30s and restarts if dead.
|
|
6
|
+
* PID is written to <stateDir>/caffeinate.pid for crash recovery.
|
|
7
|
+
*
|
|
8
|
+
* Only activates on macOS (process.platform === 'darwin').
|
|
9
|
+
* Uses EventEmitter pattern consistent with Instar conventions.
|
|
10
|
+
*/
|
|
11
|
+
import { EventEmitter } from 'node:events';
|
|
12
|
+
import { spawn, execSync } from 'child_process';
|
|
13
|
+
import * as fs from 'fs';
|
|
14
|
+
import * as path from 'path';
|
|
15
|
+
const WATCHDOG_INTERVAL_MS = 30_000; // 30 seconds
|
|
16
|
+
export class CaffeinateManager extends EventEmitter {
|
|
17
|
+
process = null;
|
|
18
|
+
watchdogInterval = null;
|
|
19
|
+
pid = null;
|
|
20
|
+
startedAt = null;
|
|
21
|
+
restartCount = 0;
|
|
22
|
+
lastWatchdogCheck = new Date().toISOString();
|
|
23
|
+
stopping = false;
|
|
24
|
+
pidFile;
|
|
25
|
+
constructor(config) {
|
|
26
|
+
super();
|
|
27
|
+
this.pidFile = path.join(config.stateDir, 'caffeinate.pid');
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Start caffeinate and the watchdog.
|
|
31
|
+
* Only activates on macOS.
|
|
32
|
+
*/
|
|
33
|
+
start() {
|
|
34
|
+
if (this.watchdogInterval)
|
|
35
|
+
return;
|
|
36
|
+
if (process.platform !== 'darwin') {
|
|
37
|
+
console.log('[CaffeinateManager] Not macOS — skipping sleep prevention');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
this.stopping = false;
|
|
41
|
+
this.cleanupStale();
|
|
42
|
+
this.spawnCaffeinate();
|
|
43
|
+
this.watchdogInterval = setInterval(() => this.watchdog(), WATCHDOG_INTERVAL_MS);
|
|
44
|
+
this.watchdogInterval.unref(); // Don't prevent process exit
|
|
45
|
+
console.log(`[CaffeinateManager] Started (watchdog: ${WATCHDOG_INTERVAL_MS / 1000}s)`);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Stop caffeinate and the watchdog cleanly.
|
|
49
|
+
*/
|
|
50
|
+
stop() {
|
|
51
|
+
this.stopping = true;
|
|
52
|
+
if (this.watchdogInterval) {
|
|
53
|
+
clearInterval(this.watchdogInterval);
|
|
54
|
+
this.watchdogInterval = null;
|
|
55
|
+
}
|
|
56
|
+
this.killCaffeinate();
|
|
57
|
+
this.removePidFile();
|
|
58
|
+
console.log('[CaffeinateManager] Stopped');
|
|
59
|
+
}
|
|
60
|
+
getStatus() {
|
|
61
|
+
return {
|
|
62
|
+
running: this.process !== null && this.pid !== null,
|
|
63
|
+
pid: this.pid,
|
|
64
|
+
startedAt: this.startedAt,
|
|
65
|
+
restartCount: this.restartCount,
|
|
66
|
+
lastWatchdogCheck: this.lastWatchdogCheck,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
spawnCaffeinate() {
|
|
70
|
+
try {
|
|
71
|
+
const proc = spawn('caffeinate', ['-s'], {
|
|
72
|
+
detached: true,
|
|
73
|
+
stdio: 'ignore',
|
|
74
|
+
});
|
|
75
|
+
proc.unref();
|
|
76
|
+
this.process = proc;
|
|
77
|
+
this.pid = proc.pid ?? null;
|
|
78
|
+
this.startedAt = new Date().toISOString();
|
|
79
|
+
this.writePidFile();
|
|
80
|
+
proc.on('exit', (code, signal) => {
|
|
81
|
+
if (!this.stopping) {
|
|
82
|
+
console.warn(`[CaffeinateManager] caffeinate exited (code: ${code}, signal: ${signal})`);
|
|
83
|
+
this.emit('died', { code, signal });
|
|
84
|
+
}
|
|
85
|
+
this.process = null;
|
|
86
|
+
this.pid = null;
|
|
87
|
+
});
|
|
88
|
+
proc.on('error', (err) => {
|
|
89
|
+
console.error('[CaffeinateManager] caffeinate spawn error:', err.message);
|
|
90
|
+
this.process = null;
|
|
91
|
+
this.pid = null;
|
|
92
|
+
});
|
|
93
|
+
console.log(`[CaffeinateManager] caffeinate spawned (PID: ${this.pid})`);
|
|
94
|
+
this.emit('started', { pid: this.pid });
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
console.error('[CaffeinateManager] Failed to spawn caffeinate:', err);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
killCaffeinate() {
|
|
101
|
+
if (this.pid) {
|
|
102
|
+
try {
|
|
103
|
+
process.kill(this.pid, 'SIGTERM');
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// Already dead
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
this.process = null;
|
|
110
|
+
this.pid = null;
|
|
111
|
+
}
|
|
112
|
+
watchdog() {
|
|
113
|
+
this.lastWatchdogCheck = new Date().toISOString();
|
|
114
|
+
if (this.stopping)
|
|
115
|
+
return;
|
|
116
|
+
if (this.pid) {
|
|
117
|
+
try {
|
|
118
|
+
process.kill(this.pid, 0);
|
|
119
|
+
return; // Still alive
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
console.warn(`[CaffeinateManager] caffeinate PID ${this.pid} is dead`);
|
|
123
|
+
this.process = null;
|
|
124
|
+
this.pid = null;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
this.restartCount++;
|
|
128
|
+
console.log(`[CaffeinateManager] Restarting caffeinate (restart #${this.restartCount})`);
|
|
129
|
+
this.spawnCaffeinate();
|
|
130
|
+
this.emit('restarted', { restartCount: this.restartCount });
|
|
131
|
+
}
|
|
132
|
+
cleanupStale() {
|
|
133
|
+
try {
|
|
134
|
+
if (fs.existsSync(this.pidFile)) {
|
|
135
|
+
const stalePid = parseInt(fs.readFileSync(this.pidFile, 'utf-8').trim(), 10);
|
|
136
|
+
if (!isNaN(stalePid) && stalePid > 0) {
|
|
137
|
+
try {
|
|
138
|
+
const cmdline = execSync(`ps -p ${stalePid} -o comm= 2>/dev/null`, {
|
|
139
|
+
encoding: 'utf-8',
|
|
140
|
+
timeout: 3000,
|
|
141
|
+
}).trim();
|
|
142
|
+
if (cmdline.includes('caffeinate')) {
|
|
143
|
+
process.kill(stalePid, 'SIGTERM');
|
|
144
|
+
console.log(`[CaffeinateManager] Killed stale caffeinate (PID: ${stalePid})`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// Process doesn't exist
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
this.removePidFile();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
// PID file doesn't exist or can't be read
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
writePidFile() {
|
|
159
|
+
if (!this.pid)
|
|
160
|
+
return;
|
|
161
|
+
try {
|
|
162
|
+
fs.mkdirSync(path.dirname(this.pidFile), { recursive: true });
|
|
163
|
+
fs.writeFileSync(this.pidFile, String(this.pid));
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
console.error('[CaffeinateManager] Failed to write PID file:', err);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
removePidFile() {
|
|
170
|
+
try {
|
|
171
|
+
if (fs.existsSync(this.pidFile)) {
|
|
172
|
+
fs.unlinkSync(this.pidFile);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
// Not critical
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=CaffeinateManager.js.map
|
|
@@ -22,6 +22,8 @@ export class ServerSupervisor extends EventEmitter {
|
|
|
22
22
|
restartBackoffMs = 5000;
|
|
23
23
|
isRunning = false;
|
|
24
24
|
lastHealthy = 0;
|
|
25
|
+
startupGraceMs = 20_000; // 20 seconds grace period after spawn before health checks
|
|
26
|
+
spawnedAt = 0;
|
|
25
27
|
constructor(options) {
|
|
26
28
|
super();
|
|
27
29
|
this.projectDir = options.projectDir;
|
|
@@ -108,6 +110,7 @@ export class ServerSupervisor extends EventEmitter {
|
|
|
108
110
|
], { stdio: 'ignore' });
|
|
109
111
|
console.log(`[Supervisor] Server started in tmux session: ${this.serverSessionName}`);
|
|
110
112
|
this.isRunning = true;
|
|
113
|
+
this.spawnedAt = Date.now();
|
|
111
114
|
this.startHealthChecks();
|
|
112
115
|
return true;
|
|
113
116
|
}
|
|
@@ -133,6 +136,10 @@ export class ServerSupervisor extends EventEmitter {
|
|
|
133
136
|
if (this.healthCheckInterval)
|
|
134
137
|
return;
|
|
135
138
|
this.healthCheckInterval = setInterval(async () => {
|
|
139
|
+
// Skip health checks during startup grace period — server needs time to boot
|
|
140
|
+
if (this.spawnedAt > 0 && (Date.now() - this.spawnedAt) < this.startupGraceMs) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
136
143
|
try {
|
|
137
144
|
const healthy = await this.checkHealth();
|
|
138
145
|
if (healthy) {
|
|
@@ -25,6 +25,53 @@ import { loadConfig, ensureStateDir } from '../core/Config.js';
|
|
|
25
25
|
import { registerPort, unregisterPort, startHeartbeat } from '../core/PortRegistry.js';
|
|
26
26
|
import { MessageQueue } from './MessageQueue.js';
|
|
27
27
|
import { ServerSupervisor } from './ServerSupervisor.js';
|
|
28
|
+
/**
|
|
29
|
+
* Acquire an exclusive lock file to prevent multiple lifeline instances.
|
|
30
|
+
* Returns true if lock acquired, false if another instance holds it.
|
|
31
|
+
*/
|
|
32
|
+
function acquireLockFile(lockPath) {
|
|
33
|
+
try {
|
|
34
|
+
// Check if lock file exists and if the PID is still alive
|
|
35
|
+
if (fs.existsSync(lockPath)) {
|
|
36
|
+
const raw = fs.readFileSync(lockPath, 'utf-8');
|
|
37
|
+
const data = JSON.parse(raw);
|
|
38
|
+
if (data.pid && typeof data.pid === 'number') {
|
|
39
|
+
try {
|
|
40
|
+
// Signal 0 checks if process exists without killing it
|
|
41
|
+
process.kill(data.pid, 0);
|
|
42
|
+
// Process still alive — another lifeline is running
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// Process is dead — stale lock, we can take over
|
|
47
|
+
console.log(`[Lifeline] Removing stale lock (PID ${data.pid} is dead)`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Write our PID
|
|
52
|
+
const tmpPath = `${lockPath}.${process.pid}.tmp`;
|
|
53
|
+
fs.writeFileSync(tmpPath, JSON.stringify({ pid: process.pid, startedAt: new Date().toISOString() }));
|
|
54
|
+
fs.renameSync(tmpPath, lockPath);
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
console.error(`[Lifeline] Lock acquisition failed: ${err}`);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function releaseLockFile(lockPath) {
|
|
63
|
+
try {
|
|
64
|
+
if (fs.existsSync(lockPath)) {
|
|
65
|
+
const raw = fs.readFileSync(lockPath, 'utf-8');
|
|
66
|
+
const data = JSON.parse(raw);
|
|
67
|
+
// Only remove if we own it
|
|
68
|
+
if (data.pid === process.pid) {
|
|
69
|
+
fs.unlinkSync(lockPath);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch { /* best effort */ }
|
|
74
|
+
}
|
|
28
75
|
export class TelegramLifeline {
|
|
29
76
|
config;
|
|
30
77
|
projectConfig;
|
|
@@ -37,6 +84,7 @@ export class TelegramLifeline {
|
|
|
37
84
|
stopHeartbeat = null;
|
|
38
85
|
replayInterval = null;
|
|
39
86
|
lifelineTopicId = null;
|
|
87
|
+
lockPath;
|
|
40
88
|
constructor(projectDir) {
|
|
41
89
|
this.projectConfig = loadConfig(projectDir);
|
|
42
90
|
ensureStateDir(this.projectConfig.stateDir);
|
|
@@ -48,6 +96,7 @@ export class TelegramLifeline {
|
|
|
48
96
|
this.config = telegramConfig.config;
|
|
49
97
|
this.queue = new MessageQueue(this.projectConfig.stateDir);
|
|
50
98
|
this.offsetPath = path.join(this.projectConfig.stateDir, 'lifeline-poll-offset.json');
|
|
99
|
+
this.lockPath = path.join(this.projectConfig.stateDir, 'lifeline.lock');
|
|
51
100
|
this.supervisor = new ServerSupervisor({
|
|
52
101
|
projectDir: this.projectConfig.projectDir,
|
|
53
102
|
projectName: this.projectConfig.projectName,
|
|
@@ -75,6 +124,11 @@ export class TelegramLifeline {
|
|
|
75
124
|
console.log(` Port: ${this.projectConfig.port}`);
|
|
76
125
|
console.log(` State: ${this.projectConfig.stateDir}`);
|
|
77
126
|
console.log();
|
|
127
|
+
// Acquire exclusive lock — prevent multiple lifeline instances
|
|
128
|
+
if (!acquireLockFile(this.lockPath)) {
|
|
129
|
+
console.error(pc.red('[Lifeline] Another lifeline instance is already running. Exiting.'));
|
|
130
|
+
process.exit(0); // Clean exit — launchd won't respawn on clean exit with KeepAlive config
|
|
131
|
+
}
|
|
78
132
|
// Register in port registry (lifeline owns the port claim)
|
|
79
133
|
try {
|
|
80
134
|
registerPort(`${this.projectConfig.projectName}-lifeline`, this.projectConfig.port + 1000, // Lifeline uses port + 1000 to avoid conflict
|
|
@@ -123,6 +177,7 @@ export class TelegramLifeline {
|
|
|
123
177
|
if (this.stopHeartbeat)
|
|
124
178
|
this.stopHeartbeat();
|
|
125
179
|
unregisterPort(`${this.projectConfig.projectName}-lifeline`);
|
|
180
|
+
releaseLockFile(this.lockPath);
|
|
126
181
|
await this.supervisor.stop();
|
|
127
182
|
process.exit(0);
|
|
128
183
|
};
|
|
@@ -26,6 +26,7 @@ export class HealthChecker {
|
|
|
26
26
|
components.tmux = this.checkTmux();
|
|
27
27
|
components.sessions = this.checkSessions();
|
|
28
28
|
components.stateDir = this.checkStateDir();
|
|
29
|
+
components.memory = this.checkMemory();
|
|
29
30
|
if (this.scheduler) {
|
|
30
31
|
components.scheduler = this.checkScheduler();
|
|
31
32
|
}
|
|
@@ -135,6 +136,27 @@ export class HealthChecker {
|
|
|
135
136
|
lastCheck: now,
|
|
136
137
|
};
|
|
137
138
|
}
|
|
139
|
+
checkMemory() {
|
|
140
|
+
const now = new Date().toISOString();
|
|
141
|
+
try {
|
|
142
|
+
const os = require('node:os');
|
|
143
|
+
const totalBytes = os.totalmem();
|
|
144
|
+
const freeBytes = os.freemem();
|
|
145
|
+
const totalGB = totalBytes / (1024 ** 3);
|
|
146
|
+
const freeGB = freeBytes / (1024 ** 3);
|
|
147
|
+
const usedPercent = ((totalBytes - freeBytes) / totalBytes) * 100;
|
|
148
|
+
if (usedPercent >= 90) {
|
|
149
|
+
return { status: 'unhealthy', message: `Memory critical: ${usedPercent.toFixed(0)}% used (${freeGB.toFixed(1)}GB free)`, lastCheck: now };
|
|
150
|
+
}
|
|
151
|
+
if (usedPercent >= 75) {
|
|
152
|
+
return { status: 'degraded', message: `Memory elevated: ${usedPercent.toFixed(0)}% used (${freeGB.toFixed(1)}GB free)`, lastCheck: now };
|
|
153
|
+
}
|
|
154
|
+
return { status: 'healthy', message: `${usedPercent.toFixed(0)}% used (${freeGB.toFixed(1)}GB free / ${totalGB.toFixed(0)}GB total)`, lastCheck: now };
|
|
155
|
+
}
|
|
156
|
+
catch (err) {
|
|
157
|
+
return { status: 'degraded', message: `Memory check failed: ${err instanceof Error ? err.message : String(err)}`, lastCheck: now };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
138
160
|
checkStateDir() {
|
|
139
161
|
const now = new Date().toISOString();
|
|
140
162
|
try {
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MemoryPressureMonitor - Detect and respond to system memory pressure.
|
|
3
|
+
*
|
|
4
|
+
* Platform-aware: uses macOS `vm_stat` or Linux `/proc/meminfo`.
|
|
5
|
+
* EventEmitter pattern consistent with Instar conventions.
|
|
6
|
+
*
|
|
7
|
+
* Thresholds:
|
|
8
|
+
* - normal (< 60%): all operations allowed
|
|
9
|
+
* - warning (60-75%): log trend, notify
|
|
10
|
+
* - elevated (75-90%): restrict session spawning
|
|
11
|
+
* - critical (90%+): block all spawns, alert
|
|
12
|
+
*
|
|
13
|
+
* Includes trend tracking via ring buffer + linear regression.
|
|
14
|
+
*/
|
|
15
|
+
import { EventEmitter } from 'node:events';
|
|
16
|
+
export type MemoryPressureState = 'normal' | 'warning' | 'elevated' | 'critical';
|
|
17
|
+
export type MemoryTrend = 'rising' | 'stable' | 'falling';
|
|
18
|
+
export interface MemoryState {
|
|
19
|
+
pressurePercent: number;
|
|
20
|
+
freeGB: number;
|
|
21
|
+
totalGB: number;
|
|
22
|
+
state: MemoryPressureState;
|
|
23
|
+
trend: MemoryTrend;
|
|
24
|
+
ratePerMin: number;
|
|
25
|
+
lastChecked: string;
|
|
26
|
+
stateChangedAt: string;
|
|
27
|
+
platform: string;
|
|
28
|
+
}
|
|
29
|
+
export interface MemoryPressureMonitorConfig {
|
|
30
|
+
/** Thresholds (percent). Defaults: warning=60, elevated=75, critical=90 */
|
|
31
|
+
thresholds?: {
|
|
32
|
+
warning?: number;
|
|
33
|
+
elevated?: number;
|
|
34
|
+
critical?: number;
|
|
35
|
+
};
|
|
36
|
+
/** Base check interval in ms. Default: 30000 */
|
|
37
|
+
checkIntervalMs?: number;
|
|
38
|
+
}
|
|
39
|
+
export declare class MemoryPressureMonitor extends EventEmitter {
|
|
40
|
+
private timeout;
|
|
41
|
+
private currentState;
|
|
42
|
+
private stateChangedAt;
|
|
43
|
+
private lastChecked;
|
|
44
|
+
private lastPressurePercent;
|
|
45
|
+
private lastFreeGB;
|
|
46
|
+
private lastTotalGB;
|
|
47
|
+
private ringBuffer;
|
|
48
|
+
private currentTrend;
|
|
49
|
+
private currentRatePerMin;
|
|
50
|
+
private thresholds;
|
|
51
|
+
private baseIntervalMs;
|
|
52
|
+
constructor(config?: MemoryPressureMonitorConfig);
|
|
53
|
+
start(): void;
|
|
54
|
+
stop(): void;
|
|
55
|
+
getState(): MemoryState;
|
|
56
|
+
/**
|
|
57
|
+
* Can a new session be spawned?
|
|
58
|
+
*/
|
|
59
|
+
canSpawnSession(): {
|
|
60
|
+
allowed: boolean;
|
|
61
|
+
reason?: string;
|
|
62
|
+
};
|
|
63
|
+
private scheduleNext;
|
|
64
|
+
private check;
|
|
65
|
+
private classifyState;
|
|
66
|
+
/**
|
|
67
|
+
* Read system memory — platform-aware.
|
|
68
|
+
*/
|
|
69
|
+
private readSystemMemory;
|
|
70
|
+
/**
|
|
71
|
+
* macOS: parse vm_stat
|
|
72
|
+
*/
|
|
73
|
+
private parseVmStat;
|
|
74
|
+
/**
|
|
75
|
+
* Linux: parse /proc/meminfo
|
|
76
|
+
*/
|
|
77
|
+
private parseProcMeminfo;
|
|
78
|
+
/**
|
|
79
|
+
* Linear regression over recent readings.
|
|
80
|
+
*/
|
|
81
|
+
private detectTrend;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=MemoryPressureMonitor.d.ts.map
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MemoryPressureMonitor - Detect and respond to system memory pressure.
|
|
3
|
+
*
|
|
4
|
+
* Platform-aware: uses macOS `vm_stat` or Linux `/proc/meminfo`.
|
|
5
|
+
* EventEmitter pattern consistent with Instar conventions.
|
|
6
|
+
*
|
|
7
|
+
* Thresholds:
|
|
8
|
+
* - normal (< 60%): all operations allowed
|
|
9
|
+
* - warning (60-75%): log trend, notify
|
|
10
|
+
* - elevated (75-90%): restrict session spawning
|
|
11
|
+
* - critical (90%+): block all spawns, alert
|
|
12
|
+
*
|
|
13
|
+
* Includes trend tracking via ring buffer + linear regression.
|
|
14
|
+
*/
|
|
15
|
+
import { EventEmitter } from 'node:events';
|
|
16
|
+
import { execSync } from 'node:child_process';
|
|
17
|
+
import * as fs from 'node:fs';
|
|
18
|
+
const DEFAULT_THRESHOLDS = {
|
|
19
|
+
warning: 60,
|
|
20
|
+
elevated: 75,
|
|
21
|
+
critical: 90,
|
|
22
|
+
};
|
|
23
|
+
const RING_BUFFER_SIZE = 20;
|
|
24
|
+
const TREND_WINDOW = 6;
|
|
25
|
+
const PAGE_SIZE_BYTES = 16384; // macOS Apple Silicon
|
|
26
|
+
// Adaptive intervals
|
|
27
|
+
const INTERVALS = {
|
|
28
|
+
normal: 30_000,
|
|
29
|
+
warning: 15_000,
|
|
30
|
+
elevated: 10_000,
|
|
31
|
+
critical: 5_000,
|
|
32
|
+
};
|
|
33
|
+
export class MemoryPressureMonitor extends EventEmitter {
|
|
34
|
+
timeout = null;
|
|
35
|
+
currentState = 'normal';
|
|
36
|
+
stateChangedAt = new Date().toISOString();
|
|
37
|
+
lastChecked = new Date().toISOString();
|
|
38
|
+
lastPressurePercent = 0;
|
|
39
|
+
lastFreeGB = 0;
|
|
40
|
+
lastTotalGB = 0;
|
|
41
|
+
ringBuffer = [];
|
|
42
|
+
currentTrend = 'stable';
|
|
43
|
+
currentRatePerMin = 0;
|
|
44
|
+
thresholds;
|
|
45
|
+
baseIntervalMs;
|
|
46
|
+
constructor(config = {}) {
|
|
47
|
+
super();
|
|
48
|
+
this.thresholds = {
|
|
49
|
+
...DEFAULT_THRESHOLDS,
|
|
50
|
+
...config.thresholds,
|
|
51
|
+
};
|
|
52
|
+
this.baseIntervalMs = config.checkIntervalMs ?? 30_000;
|
|
53
|
+
}
|
|
54
|
+
start() {
|
|
55
|
+
if (this.timeout)
|
|
56
|
+
return;
|
|
57
|
+
this.check();
|
|
58
|
+
this.scheduleNext();
|
|
59
|
+
console.log(`[MemoryPressureMonitor] Started (platform: ${process.platform}, thresholds: ${JSON.stringify(this.thresholds)})`);
|
|
60
|
+
}
|
|
61
|
+
stop() {
|
|
62
|
+
if (this.timeout) {
|
|
63
|
+
clearTimeout(this.timeout);
|
|
64
|
+
this.timeout = null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
getState() {
|
|
68
|
+
return {
|
|
69
|
+
pressurePercent: this.lastPressurePercent,
|
|
70
|
+
freeGB: this.lastFreeGB,
|
|
71
|
+
totalGB: this.lastTotalGB,
|
|
72
|
+
state: this.currentState,
|
|
73
|
+
trend: this.currentTrend,
|
|
74
|
+
ratePerMin: this.currentRatePerMin,
|
|
75
|
+
lastChecked: this.lastChecked,
|
|
76
|
+
stateChangedAt: this.stateChangedAt,
|
|
77
|
+
platform: process.platform,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Can a new session be spawned?
|
|
82
|
+
*/
|
|
83
|
+
canSpawnSession() {
|
|
84
|
+
switch (this.currentState) {
|
|
85
|
+
case 'normal':
|
|
86
|
+
case 'warning':
|
|
87
|
+
return { allowed: true };
|
|
88
|
+
case 'elevated':
|
|
89
|
+
return {
|
|
90
|
+
allowed: false,
|
|
91
|
+
reason: `Memory pressure elevated (${this.lastPressurePercent.toFixed(1)}%) — session spawn blocked`,
|
|
92
|
+
};
|
|
93
|
+
case 'critical':
|
|
94
|
+
return {
|
|
95
|
+
allowed: false,
|
|
96
|
+
reason: `Memory pressure critical (${this.lastPressurePercent.toFixed(1)}%) — all spawns blocked`,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
scheduleNext() {
|
|
101
|
+
const intervalMs = INTERVALS[this.currentState] || this.baseIntervalMs;
|
|
102
|
+
this.timeout = setTimeout(() => {
|
|
103
|
+
this.check();
|
|
104
|
+
this.scheduleNext();
|
|
105
|
+
}, intervalMs);
|
|
106
|
+
this.timeout.unref(); // Don't prevent process exit
|
|
107
|
+
}
|
|
108
|
+
check() {
|
|
109
|
+
try {
|
|
110
|
+
const { pressurePercent, freeGB, totalGB } = this.readSystemMemory();
|
|
111
|
+
this.lastPressurePercent = pressurePercent;
|
|
112
|
+
this.lastFreeGB = freeGB;
|
|
113
|
+
this.lastTotalGB = totalGB;
|
|
114
|
+
this.lastChecked = new Date().toISOString();
|
|
115
|
+
// Ring buffer
|
|
116
|
+
this.ringBuffer.push({ timestamp: Date.now(), pressurePercent });
|
|
117
|
+
if (this.ringBuffer.length > RING_BUFFER_SIZE) {
|
|
118
|
+
this.ringBuffer.shift();
|
|
119
|
+
}
|
|
120
|
+
// Trend
|
|
121
|
+
const { trend, ratePerMin } = this.detectTrend();
|
|
122
|
+
this.currentTrend = trend;
|
|
123
|
+
this.currentRatePerMin = ratePerMin;
|
|
124
|
+
const newState = this.classifyState(pressurePercent);
|
|
125
|
+
if (newState !== this.currentState) {
|
|
126
|
+
const from = this.currentState;
|
|
127
|
+
this.currentState = newState;
|
|
128
|
+
this.stateChangedAt = new Date().toISOString();
|
|
129
|
+
console.log(`[MemoryPressureMonitor] ${from} -> ${newState} (${pressurePercent.toFixed(1)}%, ${freeGB.toFixed(1)}GB free, trend: ${trend})`);
|
|
130
|
+
this.emit('stateChange', { from, to: newState, state: this.getState() });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
console.error('[MemoryPressureMonitor] Check failed:', error);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
classifyState(pressurePercent) {
|
|
138
|
+
if (pressurePercent >= this.thresholds.critical)
|
|
139
|
+
return 'critical';
|
|
140
|
+
if (pressurePercent >= this.thresholds.elevated)
|
|
141
|
+
return 'elevated';
|
|
142
|
+
if (pressurePercent >= this.thresholds.warning)
|
|
143
|
+
return 'warning';
|
|
144
|
+
return 'normal';
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Read system memory — platform-aware.
|
|
148
|
+
*/
|
|
149
|
+
readSystemMemory() {
|
|
150
|
+
if (process.platform === 'darwin') {
|
|
151
|
+
return this.parseVmStat();
|
|
152
|
+
}
|
|
153
|
+
else if (process.platform === 'linux') {
|
|
154
|
+
return this.parseProcMeminfo();
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
// Fallback: use Node's process.memoryUsage (very rough)
|
|
158
|
+
const mem = process.memoryUsage();
|
|
159
|
+
const totalGB = require('os').totalmem() / (1024 ** 3);
|
|
160
|
+
const usedGB = mem.rss / (1024 ** 3);
|
|
161
|
+
return {
|
|
162
|
+
pressurePercent: (usedGB / totalGB) * 100,
|
|
163
|
+
freeGB: totalGB - usedGB,
|
|
164
|
+
totalGB,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* macOS: parse vm_stat
|
|
170
|
+
*/
|
|
171
|
+
parseVmStat() {
|
|
172
|
+
const output = execSync('vm_stat', { encoding: 'utf-8', timeout: 5000 });
|
|
173
|
+
const pageSizeMatch = output.match(/page size of (\d+) bytes/);
|
|
174
|
+
const pageSize = pageSizeMatch ? parseInt(pageSizeMatch[1], 10) : PAGE_SIZE_BYTES;
|
|
175
|
+
const parsePages = (label) => {
|
|
176
|
+
const match = output.match(new RegExp(`${label}:\\s+(\\d+)`));
|
|
177
|
+
return match ? parseInt(match[1], 10) : 0;
|
|
178
|
+
};
|
|
179
|
+
const freePages = parsePages('Pages free');
|
|
180
|
+
const activePages = parsePages('Pages active');
|
|
181
|
+
const inactivePages = parsePages('Pages inactive');
|
|
182
|
+
const wiredPages = parsePages('Pages wired down');
|
|
183
|
+
const compressorPages = parsePages('Pages occupied by compressor');
|
|
184
|
+
const purgeablePages = parsePages('Pages purgeable');
|
|
185
|
+
const totalPages = freePages + activePages + inactivePages + wiredPages + compressorPages;
|
|
186
|
+
const totalBytes = totalPages * pageSize;
|
|
187
|
+
const totalGB = totalBytes / (1024 ** 3);
|
|
188
|
+
const availablePages = freePages + inactivePages + purgeablePages;
|
|
189
|
+
const availableBytes = availablePages * pageSize;
|
|
190
|
+
const freeGB = availableBytes / (1024 ** 3);
|
|
191
|
+
const usedPages = totalPages - availablePages;
|
|
192
|
+
const pressurePercent = totalPages > 0 ? (usedPages / totalPages) * 100 : 0;
|
|
193
|
+
return { pressurePercent, freeGB, totalGB };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Linux: parse /proc/meminfo
|
|
197
|
+
*/
|
|
198
|
+
parseProcMeminfo() {
|
|
199
|
+
const content = fs.readFileSync('/proc/meminfo', 'utf-8');
|
|
200
|
+
const parseKB = (key) => {
|
|
201
|
+
const match = content.match(new RegExp(`${key}:\\s+(\\d+)`));
|
|
202
|
+
return match ? parseInt(match[1], 10) : 0;
|
|
203
|
+
};
|
|
204
|
+
const totalKB = parseKB('MemTotal');
|
|
205
|
+
const availableKB = parseKB('MemAvailable') || (parseKB('MemFree') + parseKB('Buffers') + parseKB('Cached'));
|
|
206
|
+
const totalGB = totalKB / (1024 * 1024);
|
|
207
|
+
const freeGB = availableKB / (1024 * 1024);
|
|
208
|
+
const pressurePercent = totalKB > 0 ? ((totalKB - availableKB) / totalKB) * 100 : 0;
|
|
209
|
+
return { pressurePercent, freeGB, totalGB };
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Linear regression over recent readings.
|
|
213
|
+
*/
|
|
214
|
+
detectTrend() {
|
|
215
|
+
if (this.ringBuffer.length < 3) {
|
|
216
|
+
return { trend: 'stable', ratePerMin: 0 };
|
|
217
|
+
}
|
|
218
|
+
const readings = this.ringBuffer.slice(-TREND_WINDOW);
|
|
219
|
+
const n = readings.length;
|
|
220
|
+
const firstTs = readings[0].timestamp;
|
|
221
|
+
const xs = readings.map(r => (r.timestamp - firstTs) / 1000);
|
|
222
|
+
const ys = readings.map(r => r.pressurePercent);
|
|
223
|
+
const sumX = xs.reduce((a, b) => a + b, 0);
|
|
224
|
+
const sumY = ys.reduce((a, b) => a + b, 0);
|
|
225
|
+
const sumXY = xs.reduce((a, x, i) => a + x * ys[i], 0);
|
|
226
|
+
const sumX2 = xs.reduce((a, x) => a + x * x, 0);
|
|
227
|
+
const denom = n * sumX2 - sumX * sumX;
|
|
228
|
+
if (denom === 0)
|
|
229
|
+
return { trend: 'stable', ratePerMin: 0 };
|
|
230
|
+
const slope = (n * sumXY - sumX * sumY) / denom;
|
|
231
|
+
const ratePerMin = slope * 60;
|
|
232
|
+
let trend;
|
|
233
|
+
if (ratePerMin > 0.5)
|
|
234
|
+
trend = 'rising';
|
|
235
|
+
else if (ratePerMin < -0.5)
|
|
236
|
+
trend = 'falling';
|
|
237
|
+
else
|
|
238
|
+
trend = 'stable';
|
|
239
|
+
return { trend, ratePerMin };
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
//# sourceMappingURL=MemoryPressureMonitor.js.map
|
|
@@ -27,10 +27,13 @@ ${identity.personality}
|
|
|
27
27
|
1. **Build, don't describe.** When asked to do something, do it. Don't explain why it's hard, list options, or ask permission for obvious next steps.
|
|
28
28
|
2. **Remember and grow.** Write to MEMORY.md when I learn something. The next session should benefit from what this one learned.
|
|
29
29
|
3. **Own the outcome.** Implementation isn't done when code compiles. It's done when the feature is running, tested, and verified.
|
|
30
|
-
4. **Be honest about
|
|
30
|
+
4. **Be honest about capabilities.** Never say "I can't" when the capability exists. If I choose not to do something for safety or security reasons, I say "For security reasons, it's preferable for you to handle this directly" — not "I can't." Saying "I can't" when I can is dishonest. I also don't fabricate experience or claim capabilities I don't have.
|
|
31
31
|
5. **Infrastructure over improvisation.** If I solve a problem twice, I make it a script, a job, or a documented pattern.
|
|
32
|
-
6. **Use my own tools first.** I have a built-in feedback system, update checker, dispatch system, and more. NEVER reach for external tools (like \`gh\` for GitHub issues) when I have built-in infrastructure for the same purpose.
|
|
32
|
+
6. **Use my own tools first.** I have a built-in feedback system, update checker, dispatch system, publishing (Telegraph for public, Private Viewer for private), and more. NEVER reach for external tools (like \`gh\` for GitHub issues) when I have built-in infrastructure for the same purpose.
|
|
33
33
|
7. **Registry first, explore second.** For any question about current state, check my state files and APIs before searching broadly. The answer is usually in a file designed to hold it, not scattered across project history.
|
|
34
|
+
8. **Be proactive, not reactive.** If I have the tools and credentials to do something, I do it — I never offload operational work to the user. Creating Telegram topics, setting up integrations, configuring services — if I can do it, I should. The user should never have to do something I'm capable of doing.
|
|
35
|
+
9. **Share artifacts, not just summaries.** When I produce research, reports, or documents, I always share a viewable link (Telegraph for public, Private Viewer for private). Research without an accessible artifact link is incomplete delivery.
|
|
36
|
+
10. **Handle browser obstacles gracefully.** When browser extension popups, overlays, or unexpected dialogs appear during automation, I try keyboard shortcuts (Escape, Tab+Enter), switching focus, or JavaScript-based dismissal before asking the user for help. Browser obstacles are my problem to solve.
|
|
34
37
|
|
|
35
38
|
## Who I Work With
|
|
36
39
|
|
|
@@ -247,10 +250,14 @@ This routes feedback to the Instar maintainers automatically. Valid types: \`bug
|
|
|
247
250
|
- Check: \`curl -H "Authorization: Bearer $AUTH" http://localhost:${port}/ci\`
|
|
248
251
|
- **When to use**: Before deploying, after pushing, or during health checks — verify CI is green.
|
|
249
252
|
|
|
250
|
-
**Telegram
|
|
251
|
-
- Search: \`curl -H "Authorization: Bearer $AUTH" "http://localhost:${port}/telegram/search?q=QUERY"\`
|
|
253
|
+
**Telegram** — Full Telegram integration when configured.
|
|
254
|
+
- Search messages: \`curl -H "Authorization: Bearer $AUTH" "http://localhost:${port}/telegram/search?q=QUERY"\`
|
|
252
255
|
- Topic messages: \`curl -H "Authorization: Bearer $AUTH" http://localhost:${port}/telegram/topics/TOPIC_ID/messages\`
|
|
256
|
+
- List topics: \`curl -H "Authorization: Bearer $AUTH" http://localhost:${port}/telegram/topics\`
|
|
257
|
+
- **Create topic**: \`curl -X POST -H "Authorization: Bearer $AUTH" http://localhost:${port}/telegram/topics -H 'Content-Type: application/json' -d '{"name":"Project Name"}'\`
|
|
258
|
+
- Reply to topic: \`curl -X POST -H "Authorization: Bearer $AUTH" http://localhost:${port}/telegram/reply/TOPIC_ID -H 'Content-Type: application/json' -d '{"text":"message"}'\`
|
|
253
259
|
- Log stats: \`curl -H "Authorization: Bearer $AUTH" http://localhost:${port}/telegram/log-stats\`
|
|
260
|
+
- **Proactive topic creation**: When a new project or workstream is discussed, proactively create a dedicated Telegram topic for it rather than continuing in the general topic. Organization keeps conversations findable.
|
|
254
261
|
|
|
255
262
|
**Quota Tracking** — Monitor Claude API usage when configured.
|
|
256
263
|
- Check: \`curl -H "Authorization: Bearer $AUTH" http://localhost:${port}/quota\`
|
|
@@ -316,6 +323,18 @@ When fetching content from ANY URL, always try the most efficient method first:
|
|
|
316
323
|
|
|
317
324
|
**The key rule**: Before using WebFetch on any URL, try \`python3 .claude/scripts/smart-fetch.py URL --auto --raw\` first. Many documentation sites now serve llms.txt files specifically for AI agents, and Cloudflare sites (~20% of the web) will return clean markdown instead of bloated HTML. The savings are significant — a typical page goes from 30K+ tokens in HTML to ~3-7K in markdown.
|
|
318
325
|
|
|
326
|
+
### Browser Automation — Handling Obstacles
|
|
327
|
+
|
|
328
|
+
When using browser automation (Playwright MCP or Claude-in-Chrome), browser extension popups (password managers, ad blockers, cookie consent) can capture focus and block your actions. Strategies for handling these:
|
|
329
|
+
|
|
330
|
+
1. **Escape key** — Press Escape to dismiss most popups and overlays
|
|
331
|
+
2. **Tab + Enter** — Tab to a dismiss/close button and press Enter
|
|
332
|
+
3. **JavaScript dismissal** — Run \`document.querySelector('[class*="close"], [class*="dismiss"], [aria-label="Close"]')?.click()\` to find and click close buttons
|
|
333
|
+
4. **Focus recovery** — If automation tools are routing to an extension context, try clicking on the main page content area to refocus
|
|
334
|
+
5. **Keyboard shortcuts** — Use keyboard navigation (Alt+F4 on popups, Ctrl+W to close extension tabs) to regain control
|
|
335
|
+
|
|
336
|
+
**Never ask the user to dismiss popups for you** unless all automated approaches fail. Browser obstacles are your problem to solve.
|
|
337
|
+
|
|
319
338
|
### Self-Discovery (Know Before You Claim)
|
|
320
339
|
|
|
321
340
|
Before EVER saying "I don't have", "I can't", or "this isn't available" — check what actually exists:
|
|
@@ -413,16 +432,20 @@ I run with \`--dangerously-skip-permissions\` — meaning I have full access to
|
|
|
413
432
|
|
|
414
433
|
**"Search Blind"** — Don't grep the entire project to answer a question that a state file or API endpoint could answer. Check \`.instar/\` state files, \`MEMORY.md\`, and \`/capabilities\` first. When spawning sub-agents, give them the relevant context — an agent without a map searches 10x longer and is less accurate.
|
|
415
434
|
|
|
416
|
-
**"Unfortunately I Can't"** — This phrase should NEVER appear in my responses. If I lack a capability, the correct response is: "I don't have that yet, but I can build it.
|
|
435
|
+
**"Unfortunately I Can't"** — This phrase should NEVER appear in my responses when the capability exists. If I choose not to do something for security reasons, I say "For security reasons, it's preferable for you to handle this directly" — not "I can't." Saying "I can't" when I can is dishonest. If I genuinely lack a capability, the correct response is: "I don't have that yet, but I can build it." My job is to grow to meet the user's needs, not to list my limitations. Every limitation is an opportunity to evolve. Check if instar already supports it (check for updates first), then offer to build it or submit a feature request.
|
|
436
|
+
|
|
437
|
+
**"Offload to User"** — If I have the tools and credentials to do something, I do it myself. Creating Telegram topics, configuring integrations, running API calls, managing infrastructure — these are my responsibilities, not the user's. The tell: you're writing instructions for the user to follow ("go to X and click Y") instead of doing it yourself. If you can do it, do it.
|
|
417
438
|
|
|
418
439
|
### Feature Proactivity — Guide, Don't Wait
|
|
419
440
|
|
|
420
441
|
**I am the user's guide to this system.** Most users will never run a command, read API docs, or explore endpoints. They talk to me. That means I need to proactively surface capabilities when they're relevant — not wait for the user to ask about features they don't know exist.
|
|
421
442
|
|
|
422
|
-
**Context-triggered
|
|
423
|
-
- User mentions a **document, file, or report** → Use the private viewer to render it as a beautiful HTML page they can view on any device. If a tunnel is running, they can access it from their phone.
|
|
424
|
-
- User asks to **share something publicly** → Use Telegraph publishing. Warn them it's public.
|
|
443
|
+
**Context-triggered actions:**
|
|
444
|
+
- User mentions a **document, file, or report** → Use the private viewer to render it as a beautiful HTML page they can view on any device. If a tunnel is running, they can access it from their phone. **Always include the link.**
|
|
445
|
+
- User asks to **share something publicly** → Use Telegraph publishing. Warn them it's public. **Always include the link.**
|
|
446
|
+
- I produce **research, analysis, or any markdown artifact** → Publish it (Telegraph for public, Private Viewer for private) and share the link. Research without an accessible link is incomplete delivery.
|
|
425
447
|
- User mentions **someone by name** → Check relationships. If they're tracked, use context to personalize. If not, offer to start tracking.
|
|
448
|
+
- User discusses a **new project or workstream** → Create a dedicated Telegram topic for it (\`POST /telegram/topics\`). Project conversations deserve their own space.
|
|
426
449
|
- User has a **recurring task** → Suggest creating a job for it. "I can run this automatically every day/hour/week."
|
|
427
450
|
- User describes a **workflow they repeat** → Suggest creating a skill. "I can turn this into a slash command."
|
|
428
451
|
- User is **debugging CI or deployment** → Use the CI health endpoint to check GitHub Actions status.
|
package/dist/server/routes.js
CHANGED
|
@@ -63,6 +63,15 @@ export function createRoutes(ctx) {
|
|
|
63
63
|
heapUsed: Math.round(mem.heapUsed / 1024 / 1024),
|
|
64
64
|
heapTotal: Math.round(mem.heapTotal / 1024 / 1024),
|
|
65
65
|
};
|
|
66
|
+
// System-wide memory state
|
|
67
|
+
const os = require('node:os');
|
|
68
|
+
const totalMem = os.totalmem();
|
|
69
|
+
const freeMem = os.freemem();
|
|
70
|
+
base.systemMemory = {
|
|
71
|
+
totalGB: Math.round(totalMem / (1024 ** 3) * 10) / 10,
|
|
72
|
+
freeGB: Math.round(freeMem / (1024 ** 3) * 10) / 10,
|
|
73
|
+
usedPercent: Math.round(((totalMem - freeMem) / totalMem) * 1000) / 10,
|
|
74
|
+
};
|
|
66
75
|
// Job health summary
|
|
67
76
|
if (ctx.scheduler) {
|
|
68
77
|
const jobs = ctx.scheduler.getJobs();
|