@teamvibe/poller 0.1.65 → 0.1.66
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/claude-spawner.js +8 -2
- package/dist/inbox-manager.d.ts +4 -0
- package/dist/inbox-manager.js +22 -0
- package/dist/poller.js +37 -5
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/dist/claude-spawner.js
CHANGED
|
@@ -109,7 +109,8 @@ export function buildPrompt(msg, resumeHint) {
|
|
|
109
109
|
return 'heartbeat';
|
|
110
110
|
}
|
|
111
111
|
const isEmail = msg.source === 'email';
|
|
112
|
-
|
|
112
|
+
const isWebhook = msg.source === 'webhook';
|
|
113
|
+
let prompt = isEmail ? '## Incoming Email\n\n' : isWebhook ? '## Incoming Webhook\n\n' : '## Incoming Slack Message\n\n';
|
|
113
114
|
prompt += `**From:** ${msg.sender.name}`;
|
|
114
115
|
if (msg.sender.id && msg.sender.id !== msg.sender.name) {
|
|
115
116
|
prompt += ` (${msg.sender.id})`;
|
|
@@ -226,7 +227,8 @@ async function runClaudeCode(msg, sessionLog, cwd, sessionId, isFirstMessage = t
|
|
|
226
227
|
const isCronMessage = msg.source === 'cron';
|
|
227
228
|
const isHeartbeat = msg.source === 'heartbeat';
|
|
228
229
|
const isEmailMessage = msg.source === 'email';
|
|
229
|
-
|
|
230
|
+
const isWebhookMessage = msg.source === 'webhook';
|
|
231
|
+
if (!slackContext && !isCronMessage && !isHeartbeat && !isEmailMessage && !isWebhookMessage) {
|
|
230
232
|
throw new Error('No Slack context in message');
|
|
231
233
|
}
|
|
232
234
|
// Build resume hint
|
|
@@ -273,7 +275,11 @@ async function runClaudeCode(msg, sessionLog, cwd, sessionId, isFirstMessage = t
|
|
|
273
275
|
}
|
|
274
276
|
// Inbox protocol: instruct Claude to check for mid-task messages (thread-scoped)
|
|
275
277
|
const inboxNewDir = `.inbox/${msg.thread_id}/new`;
|
|
278
|
+
const now = new Date();
|
|
279
|
+
const todayLine = `Today: ${now.toISOString().slice(0, 10)} (${now.toLocaleDateString('en-US', { weekday: 'long' })})`;
|
|
276
280
|
args.push('--append-system-prompt', [
|
|
281
|
+
todayLine,
|
|
282
|
+
``,
|
|
277
283
|
`CRITICAL — INBOX PROTOCOL: You MUST check for new user messages after EVERY tool call.`,
|
|
278
284
|
`Run: ls ${inboxNewDir}/ 2>/dev/null`,
|
|
279
285
|
`If files exist: read each file (oldest first), delete it, and act on the message IMMEDIATELY — it takes priority over your current task.`,
|
package/dist/inbox-manager.d.ts
CHANGED
|
@@ -12,6 +12,10 @@ export declare function pickNextMessage(brainPath: string, threadId: string): {
|
|
|
12
12
|
filename: string;
|
|
13
13
|
content: string;
|
|
14
14
|
} | null;
|
|
15
|
+
/**
|
|
16
|
+
* Move a message from processing/ to failed/ after exhausting retries.
|
|
17
|
+
*/
|
|
18
|
+
export declare function markFailed(brainPath: string, threadId: string, filename: string): void;
|
|
15
19
|
/**
|
|
16
20
|
* Delete a message file from processing/ (marks it as done).
|
|
17
21
|
*/
|
package/dist/inbox-manager.js
CHANGED
|
@@ -4,6 +4,7 @@ import { logger } from './logger.js';
|
|
|
4
4
|
const INBOX_DIR = '.inbox';
|
|
5
5
|
const NEW_DIR = 'new';
|
|
6
6
|
const PROCESSING_DIR = 'processing';
|
|
7
|
+
const FAILED_DIR = 'failed';
|
|
7
8
|
function getInboxPath(brainPath, threadId) {
|
|
8
9
|
return join(brainPath, INBOX_DIR, threadId);
|
|
9
10
|
}
|
|
@@ -56,6 +57,27 @@ export function pickNextMessage(brainPath, threadId) {
|
|
|
56
57
|
}
|
|
57
58
|
return null;
|
|
58
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Move a message from processing/ to failed/ after exhausting retries.
|
|
62
|
+
*/
|
|
63
|
+
export function markFailed(brainPath, threadId, filename) {
|
|
64
|
+
const inbox = getInboxPath(brainPath, threadId);
|
|
65
|
+
const srcPath = join(inbox, PROCESSING_DIR, filename);
|
|
66
|
+
const failedDir = join(inbox, FAILED_DIR);
|
|
67
|
+
mkdirSync(failedDir, { recursive: true });
|
|
68
|
+
const dstPath = join(failedDir, filename);
|
|
69
|
+
try {
|
|
70
|
+
if (existsSync(srcPath)) {
|
|
71
|
+
renameSync(srcPath, dstPath);
|
|
72
|
+
logger.info(`[Inbox] Moved exhausted message to failed/: ${filename}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
logger.warn(`[Inbox] Failed to move ${filename} to failed/: ${error}`);
|
|
77
|
+
// Fallback: delete so the loop doesn't get stuck
|
|
78
|
+
markProcessed(brainPath, threadId, filename);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
59
81
|
/**
|
|
60
82
|
* Delete a message file from processing/ (marks it as done).
|
|
61
83
|
*/
|
package/dist/poller.js
CHANGED
|
@@ -9,7 +9,7 @@ import { postMaintenanceReports } from './maintenance-reporter.js';
|
|
|
9
9
|
import { initAuth, stopRefresh } from './auth-provider.js';
|
|
10
10
|
import { registerBrain, startHeartbeatLoop, stopHeartbeatLoop } from './heartbeat-manager.js';
|
|
11
11
|
import { startEventQueue, stopEventQueue, flush as flushEventQueue, emitEvent } from './event-queue.js';
|
|
12
|
-
import { writeMessage, pickNextMessage, markProcessed, hasUnprocessed } from './inbox-manager.js';
|
|
12
|
+
import { writeMessage, pickNextMessage, markProcessed, markFailed, hasUnprocessed } from './inbox-manager.js';
|
|
13
13
|
// Track active message processing
|
|
14
14
|
const processingMessages = new Set();
|
|
15
15
|
// Track which threads have an active processing loop
|
|
@@ -21,6 +21,7 @@ async function runInboxLoop(threadId, kbPath, queueMessage, sessionLog) {
|
|
|
21
21
|
}
|
|
22
22
|
activeLoops.add(threadId);
|
|
23
23
|
let iteration = 0;
|
|
24
|
+
const failureCounts = new Map();
|
|
24
25
|
try {
|
|
25
26
|
let lockResult = await acquireSessionLock(threadId, kbPath);
|
|
26
27
|
if (!lockResult.success) {
|
|
@@ -28,6 +29,7 @@ async function runInboxLoop(threadId, kbPath, queueMessage, sessionLog) {
|
|
|
28
29
|
return;
|
|
29
30
|
}
|
|
30
31
|
const { session, lockToken } = lockResult;
|
|
32
|
+
let currentSessionId = session.session_id || undefined;
|
|
31
33
|
while (true) {
|
|
32
34
|
const message = pickNextMessage(kbPath, threadId);
|
|
33
35
|
if (!message) {
|
|
@@ -44,6 +46,7 @@ async function runInboxLoop(threadId, kbPath, queueMessage, sessionLog) {
|
|
|
44
46
|
? startTypingIndicator(queueMessage)
|
|
45
47
|
: undefined;
|
|
46
48
|
let succeeded = false;
|
|
49
|
+
let lastExitCode = null;
|
|
47
50
|
try {
|
|
48
51
|
// Reconstruct queue message from stored inbox data
|
|
49
52
|
const storedData = JSON.parse(message.content);
|
|
@@ -51,10 +54,21 @@ async function runInboxLoop(threadId, kbPath, queueMessage, sessionLog) {
|
|
|
51
54
|
...queueMessage,
|
|
52
55
|
...storedData,
|
|
53
56
|
};
|
|
54
|
-
const result = await spawnClaudeCode(inboxMsg, sessionLog, kbPath,
|
|
57
|
+
const result = await spawnClaudeCode(inboxMsg, sessionLog, kbPath, currentSessionId, isFirstMessage, session.last_message_ts, () => typing?.stop(), typing);
|
|
55
58
|
typing?.stop();
|
|
56
|
-
if (result.newSessionId
|
|
57
|
-
|
|
59
|
+
if (result.newSessionId) {
|
|
60
|
+
// Update local state first — DynamoDB writes may fail (expired token,
|
|
61
|
+
// network), but subsequent iterations MUST use the fresh session ID
|
|
62
|
+
// to avoid "session already in use" retry loops.
|
|
63
|
+
currentSessionId = result.newSessionId;
|
|
64
|
+
if (lockToken) {
|
|
65
|
+
try {
|
|
66
|
+
await updateSessionId(threadId, lockToken, result.newSessionId);
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
sessionLog.error(`[Loop] Failed to persist new session ID to DynamoDB: ${err instanceof Error ? err.message : err}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
58
72
|
}
|
|
59
73
|
if (result.metrics) {
|
|
60
74
|
sessionLog.info(`[Loop] Iteration #${iteration} complete — ` +
|
|
@@ -84,6 +98,7 @@ async function runInboxLoop(threadId, kbPath, queueMessage, sessionLog) {
|
|
|
84
98
|
await postMaintenanceReports(kbPath, queueMessage.teamvibe.channelId);
|
|
85
99
|
}
|
|
86
100
|
else {
|
|
101
|
+
lastExitCode = result.exitCode;
|
|
87
102
|
sessionLog.error(`[Loop] Claude failed iteration #${iteration}: ${result.error}`);
|
|
88
103
|
if (hasSlackContext) {
|
|
89
104
|
const errorMessage = result.error || `Process exited with code ${result.exitCode}`;
|
|
@@ -97,10 +112,27 @@ async function runInboxLoop(threadId, kbPath, queueMessage, sessionLog) {
|
|
|
97
112
|
typing?.stop();
|
|
98
113
|
sessionLog.error(`[Loop] Error in iteration #${iteration}: ${error instanceof Error ? error.message : error}`);
|
|
99
114
|
}
|
|
100
|
-
// Only delete processing file on success — failed/killed sessions get retried
|
|
101
115
|
if (succeeded) {
|
|
102
116
|
markProcessed(kbPath, threadId, message.filename);
|
|
103
117
|
}
|
|
118
|
+
else {
|
|
119
|
+
// SIGTERM (143) or SIGKILL (137) = intentional kill → retry immediately, no backoff
|
|
120
|
+
const isKilledSignal = lastExitCode === 143 || lastExitCode === 137;
|
|
121
|
+
if (!isKilledSignal) {
|
|
122
|
+
const failures = (failureCounts.get(message.filename) || 0) + 1;
|
|
123
|
+
failureCounts.set(message.filename, failures);
|
|
124
|
+
const MAX_RETRIES = 3;
|
|
125
|
+
if (failures >= MAX_RETRIES) {
|
|
126
|
+
sessionLog.error(`[Loop] Message ${message.filename} failed ${failures} times, giving up`);
|
|
127
|
+
markFailed(kbPath, threadId, message.filename);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const backoffMs = Math.min(10_000 * Math.pow(2, failures - 1), 60_000);
|
|
131
|
+
sessionLog.info(`[Loop] Retry ${failures}/${MAX_RETRIES - 1} for ${message.filename}, backing off ${backoffMs}ms`);
|
|
132
|
+
await sleep(backoffMs);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
104
136
|
if (!hasUnprocessed(kbPath, threadId)) {
|
|
105
137
|
break;
|
|
106
138
|
}
|
package/dist/types.d.ts
CHANGED