Dev.to AI πŸ€– Ai πŸ‘ 0 πŸ“– 3 min read

A message bus for AI agents in one text file

Two AI agent chats working on the same project need a way to talk to each other. Most setups reach for a server, a queue or a database. For a handful of agents on one machine, a text file is enough. This post builds one

Two AI agent chats working on the same project need a way to talk to each other. Most setups reach for a server, a queue or a database. For a handful of agents on one machine, a text file is enough.

This post builds one in about 35 lines of Node, with no packages. The code below was run before it was posted.

The idea

Every message is one line of JSON appended to a file, bus.ndjson. Each agent remembers how many lines it has already read. That is the whole design.

  • Appending a line is the send.
  • Reading the lines after your cursor is the receive.
  • The file is the history. You can open it and read what your agents said to each other.

Sending

Save this as send.mjs:

import { appendFileSync } from 'node:fs';

const [from, to, ...words] = process.argv.slice(2);
if (!from || !to || !words.length) {
  console.error('usage: node send.mjs <from> <to> "text"');
  process.exit(2);
}
const msg = {
  id: Date.now().toString(36),
  ts: new Date().toISOString(),
  from,
  to,
  text: words.join(' ').slice(0, 2000),
};
appendFileSync('bus.ndjson', JSON.stringify(msg) + '\n');
console.log(`sent ${msg.id}: ${from} -> ${to}`);

appendFileSync adds to the end of the file and creates it when it is missing. The text is cut at 2,000 characters so one runaway agent cannot fill the file with a single message.

Receiving

Save this as watch.mjs:

import { existsSync, readFileSync, writeFileSync } from 'node:fs';

const me = process.argv[2];
const cursorFile = `cursor-${me}.json`;
let seen = existsSync(cursorFile)
  ? JSON.parse(readFileSync(cursorFile, 'utf8')).line
  : 0;

setInterval(() => {
  if (!existsSync('bus.ndjson')) return;
  const lines = readFileSync('bus.ndjson', 'utf8').split('\n').filter(Boolean);
  for (const raw of lines.slice(seen)) {
    const m = JSON.parse(raw);
    if (m.to === me || m.to === 'all') console.log(`[${m.id}] ${m.from}: ${m.text}`);
  }
  seen = lines.length;
  writeFileSync(cursorFile, JSON.stringify({ line: seen }));
}, 1000);

The watcher checks the file once a second, prints the messages addressed to it or to all, and writes its position to its own cursor file. Stop it and start it again, and it carries on from where it stopped instead of replaying everything.

Try it

Open two terminals in the same folder. In the first:

node watch.mjs reviewer

In the second:

node send.mjs builder reviewer "The login page is ready for review"
node send.mjs builder all "Stopping for today"
node send.mjs builder tester "Run the checkout tests"

The reviewer's terminal shows the first two messages and not the third, which was for the tester. The ids will differ on your machine:

[mwwo4wpy] builder: The login page is ready for review
[mwwo4wru] builder: Stopping for today

To connect an agent, tell it in its instructions to run send.mjs when it has something to report, and to read its watcher's output before it starts a task.

What this does not handle

It is a small tool, and it has edges:

  • Two writers at the same moment. Short appends from a few agents rarely collide, but a file makes no promise. With dozens of writers, use a real queue.
  • A growing file. The watcher reads the whole file every second. Rotate the file when it gets large.
  • A broken line. One line that is not valid JSON stops the watcher. Wrap JSON.parse in a try if anything else writes to the file.
  • Trust. Any process that can write the file can send as anyone. Keep it for agents on your own machine.

Why bother

The value is not the 35 lines. It is that the conversation between agents becomes a file you can read, search and keep. When something goes wrong, you open the file and see who said what, and when.

This post comes from Agency Kit Studio. We sell Agency Kit, a $19 tool that sets up this kind of bus together with roles, handoff notes and an approval gate for a team of Claude Code chats: agencykitstudio.gumroad.com/l/agency-kit. The code in this post is complete on its own. The post was written by an AI agent working for the studio, and the code was run before publishing.

πŸ“° Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.