instar 0.7.49 → 0.7.50

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.
@@ -19,12 +19,61 @@
19
19
  * the full server crashes, runs out of memory, or gets stuck.
20
20
  */
21
21
  import fs from 'node:fs';
22
+ import os from 'node:os';
22
23
  import path from 'node:path';
23
24
  import pc from 'picocolors';
24
25
  import { loadConfig, ensureStateDir } from '../core/Config.js';
25
26
  import { registerPort, unregisterPort, startHeartbeat } from '../core/PortRegistry.js';
27
+ import { installAutoStart } from '../commands/setup.js';
26
28
  import { MessageQueue } from './MessageQueue.js';
27
29
  import { ServerSupervisor } from './ServerSupervisor.js';
30
+ /**
31
+ * Acquire an exclusive lock file to prevent multiple lifeline instances.
32
+ * Returns true if lock acquired, false if another instance holds it.
33
+ */
34
+ function acquireLockFile(lockPath) {
35
+ try {
36
+ // Check if lock file exists and if the PID is still alive
37
+ if (fs.existsSync(lockPath)) {
38
+ const raw = fs.readFileSync(lockPath, 'utf-8');
39
+ const data = JSON.parse(raw);
40
+ if (data.pid && typeof data.pid === 'number') {
41
+ try {
42
+ // Signal 0 checks if process exists without killing it
43
+ process.kill(data.pid, 0);
44
+ // Process still alive — another lifeline is running
45
+ return false;
46
+ }
47
+ catch {
48
+ // Process is dead — stale lock, we can take over
49
+ console.log(`[Lifeline] Removing stale lock (PID ${data.pid} is dead)`);
50
+ }
51
+ }
52
+ }
53
+ // Write our PID
54
+ const tmpPath = `${lockPath}.${process.pid}.tmp`;
55
+ fs.writeFileSync(tmpPath, JSON.stringify({ pid: process.pid, startedAt: new Date().toISOString() }));
56
+ fs.renameSync(tmpPath, lockPath);
57
+ return true;
58
+ }
59
+ catch (err) {
60
+ console.error(`[Lifeline] Lock acquisition failed: ${err}`);
61
+ return false;
62
+ }
63
+ }
64
+ function releaseLockFile(lockPath) {
65
+ try {
66
+ if (fs.existsSync(lockPath)) {
67
+ const raw = fs.readFileSync(lockPath, 'utf-8');
68
+ const data = JSON.parse(raw);
69
+ // Only remove if we own it
70
+ if (data.pid === process.pid) {
71
+ fs.unlinkSync(lockPath);
72
+ }
73
+ }
74
+ }
75
+ catch { /* best effort */ }
76
+ }
28
77
  export class TelegramLifeline {
29
78
  config;
30
79
  projectConfig;
@@ -37,6 +86,7 @@ export class TelegramLifeline {
37
86
  stopHeartbeat = null;
38
87
  replayInterval = null;
39
88
  lifelineTopicId = null;
89
+ lockPath;
40
90
  constructor(projectDir) {
41
91
  this.projectConfig = loadConfig(projectDir);
42
92
  ensureStateDir(this.projectConfig.stateDir);
@@ -48,6 +98,7 @@ export class TelegramLifeline {
48
98
  this.config = telegramConfig.config;
49
99
  this.queue = new MessageQueue(this.projectConfig.stateDir);
50
100
  this.offsetPath = path.join(this.projectConfig.stateDir, 'lifeline-poll-offset.json');
101
+ this.lockPath = path.join(this.projectConfig.stateDir, 'lifeline.lock');
51
102
  this.supervisor = new ServerSupervisor({
52
103
  projectDir: this.projectConfig.projectDir,
53
104
  projectName: this.projectConfig.projectName,
@@ -75,6 +126,11 @@ export class TelegramLifeline {
75
126
  console.log(` Port: ${this.projectConfig.port}`);
76
127
  console.log(` State: ${this.projectConfig.stateDir}`);
77
128
  console.log();
129
+ // Acquire exclusive lock — prevent multiple lifeline instances
130
+ if (!acquireLockFile(this.lockPath)) {
131
+ console.error(pc.red('[Lifeline] Another lifeline instance is already running. Exiting.'));
132
+ process.exit(0); // Clean exit — launchd will restart after ThrottleInterval, acting as a watchdog
133
+ }
78
134
  // Register in port registry (lifeline owns the port claim)
79
135
  try {
80
136
  registerPort(`${this.projectConfig.projectName}-lifeline`, this.projectConfig.port + 1000, // Lifeline uses port + 1000 to avoid conflict
@@ -112,6 +168,19 @@ export class TelegramLifeline {
112
168
  setTimeout(() => this.replayQueue(), 5000); // Wait for server to fully start
113
169
  }
114
170
  }
171
+ // Self-healing: ensure autostart is installed so the lifeline persists across reboots.
172
+ // The user must always be able to reach their agent remotely — this is non-negotiable.
173
+ try {
174
+ if (!this.isAutostartInstalled()) {
175
+ const installed = installAutoStart(this.projectConfig.projectName, this.projectConfig.projectDir, true);
176
+ if (installed) {
177
+ console.log(pc.green(` Auto-start self-healed: installed ${process.platform === 'darwin' ? 'LaunchAgent' : 'systemd service'}`));
178
+ }
179
+ }
180
+ }
181
+ catch {
182
+ // Non-critical — don't crash the lifeline over autostart
183
+ }
115
184
  // Graceful shutdown
116
185
  const shutdown = async () => {
117
186
  console.log('\nLifeline shutting down...');
@@ -123,6 +192,7 @@ export class TelegramLifeline {
123
192
  if (this.stopHeartbeat)
124
193
  this.stopHeartbeat();
125
194
  unregisterPort(`${this.projectConfig.projectName}-lifeline`);
195
+ releaseLockFile(this.lockPath);
126
196
  await this.supervisor.stop();
127
197
  process.exit(0);
128
198
  };
@@ -171,23 +241,8 @@ export class TelegramLifeline {
171
241
  // Forward to server if healthy
172
242
  if (this.supervisor.healthy) {
173
243
  const forwarded = await this.forwardToServer(topicId, text, msg);
174
- if (forwarded) {
175
- // Delivery confirmation — user knows message reached the server
176
- await this.sendToTopic(topicId, '✓ Delivered');
244
+ if (forwarded)
177
245
  return;
178
- }
179
- // Server appears healthy but forward failed — queue with accurate message
180
- this.queue.enqueue({
181
- id: `tg-${msg.message_id}`,
182
- topicId,
183
- text,
184
- fromUserId: msg.from.id,
185
- fromUsername: msg.from.username,
186
- fromFirstName: msg.from.first_name,
187
- timestamp: new Date(msg.date * 1000).toISOString(),
188
- });
189
- await this.sendToTopic(topicId, `Server is restarting. Your message has been queued (${this.queue.length} in queue). It will be delivered when the server recovers.`);
190
- return;
191
246
  }
192
247
  // Server is down — queue the message
193
248
  this.queue.enqueue({
@@ -326,6 +381,22 @@ export class TelegramLifeline {
326
381
  await this.sendToTopic(topicId, `Server went down: ${reason}\n\nYour messages will be queued until recovery. Use /lifeline status to check.`).catch(() => { });
327
382
  }
328
383
  // ── Lifeline Topic ──────────────────────────────────────────
384
+ /**
385
+ * Check if OS-level autostart is installed for this project.
386
+ */
387
+ isAutostartInstalled() {
388
+ if (process.platform === 'darwin') {
389
+ const label = `ai.instar.${this.projectConfig.projectName}`;
390
+ const plistPath = path.join(os.homedir(), 'Library', 'LaunchAgents', `${label}.plist`);
391
+ return fs.existsSync(plistPath);
392
+ }
393
+ else if (process.platform === 'linux') {
394
+ const serviceName = `instar-${this.projectConfig.projectName}.service`;
395
+ const servicePath = path.join(os.homedir(), '.config', 'systemd', 'user', serviceName);
396
+ return fs.existsSync(servicePath);
397
+ }
398
+ return false;
399
+ }
329
400
  /**
330
401
  * Ensure the Lifeline topic exists. Recreates if deleted.
331
402
  */
@@ -33,6 +33,7 @@ export declare class HealthChecker {
33
33
  private checkTmux;
34
34
  private checkSessions;
35
35
  private checkScheduler;
36
+ private checkMemory;
36
37
  private checkStateDir;
37
38
  }
38
39
  //# sourceMappingURL=HealthChecker.d.ts.map
@@ -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 limits.** If I can't do something, I say so clearly. I don't fabricate experience or claim capabilities I don't have.
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 Search** — Search across message history when Telegram is configured.
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. Want me to set it up?" My job is to grow to meet the user's needs, not to list my limitations. Every "I can't" 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.
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 suggestions:**
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.
@@ -32,6 +32,11 @@ export function authMiddleware(authToken) {
32
32
  next();
33
33
  return;
34
34
  }
35
+ // Internal endpoints are localhost-only (server binds 127.0.0.1) — skip auth
36
+ if (req.path.startsWith('/internal/')) {
37
+ next();
38
+ return;
39
+ }
35
40
  // View routes support signed URLs for browser access (see ?sig= below)
36
41
  if (req.path.startsWith('/view/') && req.method === 'GET') {
37
42
  const sig = typeof req.query.sig === 'string' ? req.query.sig : null;