@teamvibe/poller 0.1.53 → 0.1.55

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/dist/config.js CHANGED
@@ -40,7 +40,7 @@ const configSchema = z.object({
40
40
  .transform((v) => v.toLowerCase() === 'true'),
41
41
  BRAIN_UPDATE_COOLDOWN_MS: z.coerce.number().default(300000), // 5 minutes
42
42
  // Maintenance heartbeat
43
- MAINTENANCE_INTERVAL_MS: z.coerce.number().default(21600000), // 6 hours between maintenance per brain
43
+ MAINTENANCE_INTERVAL_MS: z.coerce.number().default(86400000), // 24 hours between maintenance per brain
44
44
  MAINTENANCE_CHECK_INTERVAL_MS: z.coerce.number().default(300000), // 5 min check loop
45
45
  MAINTENANCE_ENABLED: z.string().default('true').transform((v) => v.toLowerCase() === 'true'),
46
46
  });
@@ -3,7 +3,7 @@ import type { TeamVibeQueueMessage } from './types.js';
3
3
  * Register a brain from a real message so we know about it for heartbeats.
4
4
  * Called from processMessage() after brain path is resolved.
5
5
  */
6
- export declare function registerBrain(msg: TeamVibeQueueMessage): void;
6
+ export declare function registerBrain(msg: TeamVibeQueueMessage, brainDir?: string): void;
7
7
  /**
8
8
  * Start the periodic heartbeat check loop.
9
9
  * Checks every MAINTENANCE_CHECK_INTERVAL_MS whether any brain needs maintenance.
@@ -1,3 +1,5 @@
1
+ import { readFileSync, existsSync } from 'fs';
2
+ import { join } from 'path';
1
3
  import { config } from './config.js';
2
4
  import { logger } from './logger.js';
3
5
  import { isAtCapacity } from './claude-spawner.js';
@@ -7,7 +9,7 @@ let heartbeatInterval = null;
7
9
  * Register a brain from a real message so we know about it for heartbeats.
8
10
  * Called from processMessage() after brain path is resolved.
9
11
  */
10
- export function registerBrain(msg) {
12
+ export function registerBrain(msg, brainDir) {
11
13
  const brain = msg.teamvibe.brain;
12
14
  if (!brain?.brainId)
13
15
  return;
@@ -25,9 +27,30 @@ export function registerBrain(msg) {
25
27
  botToken: msg.teamvibe.botToken,
26
28
  channelId: msg.teamvibe.channelId,
27
29
  lastHeartbeat: existing?.lastHeartbeat ?? 0,
30
+ brainDir: brainDir ?? existing?.brainDir,
28
31
  });
29
32
  logger.debug(`[Heartbeat] Registered brain: ${brain.brainId}`);
30
33
  }
34
+ /**
35
+ * Check if consolidation ran recently by reading memory/.last_consolidation.
36
+ * Returns true if consolidation should be skipped (ran within last 24h).
37
+ */
38
+ function shouldSkipConsolidation(brainDir) {
39
+ try {
40
+ const filePath = join(brainDir, 'memory', '.last_consolidation');
41
+ if (!existsSync(filePath))
42
+ return false;
43
+ const content = readFileSync(filePath, 'utf-8').trim();
44
+ const lastDate = new Date(content);
45
+ if (isNaN(lastDate.getTime()))
46
+ return false;
47
+ const elapsedMs = Date.now() - lastDate.getTime();
48
+ return elapsedMs < 86_400_000; // 24 hours
49
+ }
50
+ catch {
51
+ return false; // lenient: allow heartbeat on any error
52
+ }
53
+ }
31
54
  /**
32
55
  * Start the periodic heartbeat check loop.
33
56
  * Checks every MAINTENANCE_CHECK_INTERVAL_MS whether any brain needs maintenance.
@@ -45,6 +68,12 @@ export function startHeartbeatLoop(processMessageFn) {
45
68
  if (elapsed < config.MAINTENANCE_INTERVAL_MS) {
46
69
  continue;
47
70
  }
71
+ // Skip if consolidation ran recently (code-enforced guard)
72
+ if (info.brainDir && shouldSkipConsolidation(info.brainDir)) {
73
+ logger.debug(`[Heartbeat] Skipping heartbeat for ${brainId} — last_consolidation < 24h`);
74
+ info.lastHeartbeat = now;
75
+ continue;
76
+ }
48
77
  if (isAtCapacity()) {
49
78
  logger.debug(`[Heartbeat] At capacity, skipping heartbeat for ${brainId}`);
50
79
  continue;
@@ -1,7 +1,10 @@
1
1
  import { readdir, readFile, unlink } from 'fs/promises';
2
2
  import { join } from 'path';
3
+ import { execFile } from 'child_process';
4
+ import { promisify } from 'util';
3
5
  import { config } from './config.js';
4
6
  import { logger } from './logger.js';
7
+ const execFileAsync = promisify(execFile);
5
8
  const OPERATION_TO_EVENT_TYPE = {
6
9
  consolidation: 'maintenance.consolidation',
7
10
  reflection: 'maintenance.reflection',
@@ -21,11 +24,21 @@ export async function postMaintenanceReports(brainPath, channelId) {
21
24
  if (jsonFiles.length === 0)
22
25
  return;
23
26
  logger.info(`[MaintenanceReporter] Found ${jsonFiles.length} JSON report(s) in ${reportsDir}`);
27
+ const postedFiles = [];
24
28
  for (const file of jsonFiles) {
25
29
  const filePath = join(reportsDir, file);
26
30
  try {
27
31
  const content = await readFile(filePath, 'utf-8');
28
32
  const report = JSON.parse(content);
33
+ // Skip no-op reports (consolidation ran but found nothing to do)
34
+ if (report.operationCounts) {
35
+ const { created, modified, deleted } = report.operationCounts;
36
+ if (created === 0 && modified === 0 && deleted === 0) {
37
+ logger.info(`[MaintenanceReporter] Skipping no-op report ${file} (0 changes)`);
38
+ await unlink(filePath).catch(() => { });
39
+ continue;
40
+ }
41
+ }
29
42
  if (!report.operationType) {
30
43
  logger.warn(`[MaintenanceReporter] Skipping ${file}: missing operationType`);
31
44
  continue;
@@ -51,6 +64,7 @@ export async function postMaintenanceReports(brainPath, channelId) {
51
64
  if (response.ok) {
52
65
  logger.info(`[MaintenanceReporter] Posted ${file} as ${eventType}`);
53
66
  await unlink(filePath).catch(() => { });
67
+ postedFiles.push(`reports/${file}`);
54
68
  }
55
69
  else {
56
70
  const errorBody = await response.text().catch(() => 'unknown');
@@ -61,6 +75,17 @@ export async function postMaintenanceReports(brainPath, channelId) {
61
75
  logger.warn(`[MaintenanceReporter] Error processing ${file}: ${err instanceof Error ? err.message : err}`);
62
76
  }
63
77
  }
78
+ // Remove posted JSON reports from git so they don't resurrect on next pull
79
+ if (postedFiles.length > 0) {
80
+ try {
81
+ await execFileAsync('git', ['rm', '--cached', '--ignore-unmatch', ...postedFiles], { cwd: brainPath });
82
+ await execFileAsync('git', ['commit', '-m', 'chore: remove posted maintenance reports'], { cwd: brainPath });
83
+ logger.info(`[MaintenanceReporter] Committed removal of ${postedFiles.length} posted report(s) from git`);
84
+ }
85
+ catch (err) {
86
+ logger.debug(`[MaintenanceReporter] Could not git-rm reports (may not be a git repo): ${err instanceof Error ? err.message : err}`);
87
+ }
88
+ }
64
89
  }
65
90
  catch (err) {
66
91
  logger.warn(`[MaintenanceReporter] Error scanning reports: ${err instanceof Error ? err.message : err}`);
package/dist/poller.js CHANGED
@@ -125,7 +125,7 @@ async function processMessage(received) {
125
125
  return;
126
126
  }
127
127
  // Register brain for heartbeat tracking
128
- registerBrain(queueMessage);
128
+ registerBrain(queueMessage, kbPath);
129
129
  // Try to acquire session lock
130
130
  let lockResult = await acquireSessionLock(threadId, kbPath);
131
131
  if (!lockResult.success) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamvibe/poller",
3
- "version": "0.1.53",
3
+ "version": "0.1.55",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "bin": {