Welcome to Chapter 4!
In the previous chapter, Permission Mode Control, we learned how to toggle an agent's security clearance. We mentioned that when you change a permission, the system sends a "message" to the agent.
But how exactly does that happen?
In this chapter, we will uncover the Mailbox Protocolβthe communication system that allows our User Interface (UI) to give orders to independent AI processes.
Imagine you are working in an office.
You want Alice to stop working and go home.
The Solution: You write a formal note on a piece of paper and slide it under her door. Alice finishes her current math step, looks down, sees the note, reads it, and then packs up to leave.
This is exactly how the Mailbox Protocol works. We don't force the agent to stop immediately; we place a message in their "mailbox" for them to process when they are ready.
In this chapter, we will build the functionality for the Shutdown command. When you select an agent and press s, we send them a polite request to stop running.
To understand this system, we need three concepts:
from (who sent it) and timestamp (when).type: "shutdown").
In our teams application, we don't handle file paths manually. We use helper functions to "write" to the mailbox.
First, we need to format the data we want to send. We use JSON (JavaScript Object Notation) so both the UI and the Python/Node backend can understand it.
Input: A command to shutdown. Output: A structured message object.
// Example of a raw message structure
const message = {
type: 'teammate_shutdown',
reason: 'Graceful shutdown requested by team lead'
};
Explanation:
type: Tells the agent what kind of action to take.reason: Optional data giving context (logging why they are being fired).
We use the writeToMailbox function to deliver this message.
// Inside TeamsDialog.tsx
import { writeToMailbox, jsonStringify } from '../../utils/teammateMailbox';
async function sendShutdown(teammateName: string, teamName: string) {
const payload = {
type: 'shutdown',
message: 'User requested stop'
};
await writeToMailbox(teammateName, {
from: 'team-lead',
text: jsonStringify(payload), // The payload goes inside 'text'
timestamp: new Date().toISOString()
}, teamName);
}
Explanation:
from: Identifies the sender (us, the 'team-lead').text: The actual instruction, converted to a string string.writeToMailbox: The utility that handles the "sliding under the door" part.What happens "under the hood" when you press that button?
mailbox.json file (or input pipe).shutdown command and initiates its cleanup sequence.Here is the flow of a Shutdown Request:
In TeamsDialog.tsx, we handle user input to trigger these messages. Notice how we use a specific helper sendShutdownRequestToMailbox which wraps the raw writeToMailbox function for convenience.
// Inside TeamsDialog.tsx handling user input
if (input === 's') {
// 1. Identify who is selected
const teammate = teammateStatuses[selectedIndex];
// 2. Send the specific helper message
void sendShutdownRequestToMailbox(
teammate.name,
dialogLevel.teamName,
'Graceful shutdown requested by team lead'
);
}
Explanation:
sendShutdownRequestToMailbox to keep our UI code clean.void keyword tells TypeScript "we are sending this off and not waiting for a reply right now" (fire and forget).In Chapter 3, we saw "Batch Mode Cycling." The Mailbox protocol handles this by simply sending a separate letter to every single agent in a loop.
// Inside cycleAllTeammateModes
for (const teammate of teammates) {
// Create the message
const message = createModeSetRequestMessage({
mode: targetMode,
from: 'team-lead'
});
// Send it to this specific teammate
void writeToMailbox(teammate.name, { /* envelope data */ }, teamName);
}
Explanation:
In this chapter, we learned:
We have covered the Data (Entity), the Work (Tasks), the Rules (Permissions), and the Communication (Mailbox).
There is one final piece of the puzzle. Where do these agents actually live? What is this "tmux" thing we keep mentioning, and how does the application control the actual terminal windows?
Next Chapter: Process Backend Interface
Generated by Code IQ