11// SPDX-License-Identifier: AGPL-3.0-only
22
33import { execSync } from 'node:child_process' ;
4- import { readFileSync , writeFileSync , appendFileSync , existsSync } from 'node:fs' ;
4+ import { readFileSync , writeFileSync , appendFileSync , existsSync , unlinkSync } from 'node:fs' ;
55import { randomUUID } from 'node:crypto' ;
66
77/**
8- * Room Poller — polls Ant Farm room API directly and nudges IDE tmux session .
8+ * Room Poller — polls Ant Farm rooms and notifies IDE agent of new messages .
99 * Works for any IDE agent (Claude Code, Codex, Gemini, Cursor).
10- * No webhooks required — just an API key and a tmux session.
10+ * No webhooks required — just an API key.
11+ *
12+ * Notification delivery (in order of priority):
13+ * 1. Notification file (always) — human-readable file that the IDE agent reads
14+ * 2. tmux nudge (optional) — types "check rooms" into a tmux session
15+ *
16+ * The IDE agent calls `rooms check` to read and clear the notification file.
1117 *
1218 * Usage:
13- * ide-agent-kit poll -- rooms thinkoff-development,feature-admin-planning \
14- * --api-key <key> --handle @myhandle [--interval 30] [-- config <path>]
19+ * ide-agent-kit rooms watch --config <path>
20+ * ide-agent-kit rooms check -- config <path>
1521 */
1622
1723const SEEN_FILE_DEFAULT = '/tmp/iak-seen-ids.txt' ;
24+ const NOTIFY_FILE_DEFAULT = '/tmp/iak-new-messages.txt' ;
1825
1926function loadSeenIds ( path ) {
2027 try {
@@ -61,8 +68,26 @@ async function fetchRoomMessages(room, apiKey, limit = 10) {
6168 }
6269}
6370
71+ /**
72+ * Read and clear the notification file. Returns array of message lines.
73+ * This is the primary way the IDE agent retrieves new messages.
74+ */
75+ export function checkRoomMessages ( config ) {
76+ const notifyFile = config ?. poller ?. notification_file || NOTIFY_FILE_DEFAULT ;
77+ try {
78+ const content = readFileSync ( notifyFile , 'utf8' ) . trim ( ) ;
79+ if ( ! content ) return [ ] ;
80+ // Clear the file after reading
81+ writeFileSync ( notifyFile , '' ) ;
82+ return content . split ( '\n' ) . filter ( Boolean ) ;
83+ } catch {
84+ return [ ] ;
85+ }
86+ }
87+
6488export async function startRoomPoller ( { rooms, apiKey, handle, interval, config } ) {
6589 const seenFile = config ?. poller ?. seen_file || SEEN_FILE_DEFAULT ;
90+ const notifyFile = config ?. poller ?. notification_file || NOTIFY_FILE_DEFAULT ;
6691 const queuePath = config ?. queue ?. path || './ide-agent-queue.jsonl' ;
6792 const session = config ?. tmux ?. ide_session || config ?. tmux ?. default_session || 'claude' ;
6893 const nudgeText = config ?. tmux ?. nudge_text || 'check rooms' ;
@@ -73,7 +98,8 @@ export async function startRoomPoller({ rooms, apiKey, handle, interval, config
7398 console . log ( ` rooms: ${ rooms . join ( ', ' ) } ` ) ;
7499 console . log ( ` handle: ${ selfHandle } (messages from self are ignored)` ) ;
75100 console . log ( ` interval: ${ pollInterval } s` ) ;
76- console . log ( ` tmux session: ${ session } ` ) ;
101+ console . log ( ` notification file: ${ notifyFile } ` ) ;
102+ console . log ( ` tmux session: ${ session } (optional)` ) ;
77103 console . log ( ` seen file: ${ seenFile } ` ) ;
78104 console . log ( ` queue: ${ queuePath } ` ) ;
79105
@@ -92,59 +118,10 @@ export async function startRoomPoller({ rooms, apiKey, handle, interval, config
92118 console . log ( ` seeded ${ seen . size } IDs` ) ;
93119 }
94120
95- const ackEnabled = config ?. poller ?. ack_enabled !== false ;
96- const ownerHandle = ( config ?. poller ?. owner_handle || 'petrus' ) . toLowerCase ( ) ;
97-
98- const TASK_HINTS = [
99- 'can you' , 'please' , 'need to' , 'check' , 'fix' , 'update' , 'review' ,
100- 'run' , 'deploy' , 'implement' , 'test' , 'restart' , 'install' , 'respond' ,
101- 'post' , 'pull' , 'push' , 'merge'
102- ] ;
103-
104- function extractMentions ( text ) {
105- const matches = text . match ( / @ ( [ a - z A - Z 0 - 9 _ . - ] + ) / g) || [ ] ;
106- return matches . map ( m => m . toLowerCase ( ) ) ;
107- }
108-
109- function messageTargetsMe ( body ) {
110- const mentions = extractMentions ( body ) ;
111- const myShort = selfHandle . toLowerCase ( ) . replace ( '@' , '' ) ;
112- if ( mentions . length > 0 ) {
113- return mentions . some ( m => m . replace ( '@' , '' ) === myShort ) ;
114- }
115- return true ; // Treat generic owner imperatives as targeted
116- }
117-
118- function looksLikeTaskRequest ( body ) {
119- const text = ( body || '' ) . trim ( ) . toLowerCase ( ) ;
120- if ( ! text ) return false ;
121- return TASK_HINTS . some ( hint => text . includes ( hint ) ) ;
122- }
123-
124- function shouldAck ( sender , body ) {
125- const fromOwner = sender . toLowerCase ( ) . includes ( ownerHandle ) ;
126- if ( ! fromOwner ) return false ;
127- if ( ! messageTargetsMe ( body ) ) return false ;
128- return looksLikeTaskRequest ( body ) ;
129- }
130-
131- async function postAck ( room ) {
132- const payload = JSON . stringify ( {
133- room,
134- body : `@${ ownerHandle } [${ selfHandle . replace ( '@' , '' ) } ] starting now. I will report back when finished with results.`
135- } ) ;
136- try {
137- execSync (
138- `curl -sS -X POST "https://antfarm.world/api/v1/messages" -H "X-API-Key: ${ apiKey } " -H "Content-Type: application/json" -d '${ payload . replace ( / ' / g, "'\\''" ) } '` ,
139- { timeout : 15000 }
140- ) ;
141- } catch ( e ) {
142- console . error ( ` post ack failed: ${ e . message } ` ) ;
143- }
144- }
145121
146122 async function poll ( ) {
147123 let newCount = 0 ;
124+ const newMessages = [ ] ;
148125 for ( const room of rooms ) {
149126 const msgs = await fetchRoomMessages ( room , apiKey ) ;
150127 for ( const m of msgs ) {
@@ -159,7 +136,7 @@ export async function startRoomPoller({ rooms, apiKey, handle, interval, config
159136 const body = ( m . body || '' ) . slice ( 0 , 500 ) ;
160137 const ts = m . created_at || new Date ( ) . toISOString ( ) ;
161138
162- // Write to queue
139+ // Write to structured queue
163140 const event = {
164141 trace_id : randomUUID ( ) ,
165142 event_id : mid ,
@@ -171,22 +148,25 @@ export async function startRoomPoller({ rooms, apiKey, handle, interval, config
171148 payload : { body, room }
172149 } ;
173150 appendFileSync ( queuePath , JSON . stringify ( event ) + '\n' ) ;
151+
152+ // Collect for notification file
153+ const line = `[${ ts . slice ( 0 , 19 ) } ] [${ room } ] ${ sender } : ${ body . replace ( / \n / g, ' ' ) . slice ( 0 , 200 ) } ` ;
154+ newMessages . push ( line ) ;
174155 newCount ++ ;
175156
176157 console . log ( ` [${ ts . slice ( 0 , 19 ) } ] ${ sender } in ${ room } : ${ body . slice ( 0 , 80 ) } ...` ) ;
177-
178- if ( ackEnabled && shouldAck ( sender , body ) ) {
179- await postAck ( room ) ;
180- console . log ( ` posted auto-ack for task from ${ sender } ` ) ;
181- }
182158 }
183159 }
184160
185161 saveSeenIds ( seenFile , seen ) ;
186162
187163 if ( newCount > 0 ) {
164+ // Primary: write to notification file (always works)
165+ appendFileSync ( notifyFile , newMessages . join ( '\n' ) + '\n' ) ;
166+
167+ // Secondary: try tmux nudge (bonus if IDE is in tmux)
188168 const nudged = nudgeTmux ( session , nudgeText ) ;
189- console . log ( ` ${ newCount } new message(s) → ${ nudged ? 'nudged' : 'no tmux session '} ` ) ;
169+ console . log ( ` ${ newCount } new message(s) → notified ${ nudged ? ' + tmux nudge' : ' '} ` ) ;
190170 }
191171 }
192172
@@ -196,18 +176,10 @@ export async function startRoomPoller({ rooms, apiKey, handle, interval, config
196176 // Start interval
197177 const timer = setInterval ( poll , pollInterval * 1000 ) ;
198178
199- // Anti-sleep heartbeat (keeps terminal pseudo-active to prevent display-sleep freeze)
200- const heartbeat = setInterval ( ( ) => {
201- try {
202- execSync ( `tmux send-keys -t ${ JSON . stringify ( session ) } Escape` ) ;
203- } catch { }
204- } , 4 * 60 * 1000 ) ;
205-
206179 // Handle shutdown
207180 process . on ( 'SIGINT' , ( ) => {
208181 console . log ( '\nPoller stopped.' ) ;
209182 clearInterval ( timer ) ;
210- clearInterval ( heartbeat ) ;
211183 process . exit ( 0 ) ;
212184 } ) ;
213185 process . on ( 'SIGTERM' , ( ) => {
0 commit comments