Welcome to the final chapter of the Ultraplan tutorial!
In the previous chapter, Event Stream State Machine, we built a logic engine that acts as a scorekeeper, deciding if a plan is approved or rejected based on server events.
But there is one final, powerful feature we need to handle.
Sometimes, the AI generates a perfect plan in the cloud, but you don't want it to run in the cloud. You want to run it on your machineβwhere your local database, private keys, and VPN connections live. We need a way to beam the plan from the server down to your terminal. We call this Plan Teleportation.
Here is the challenge: The remote server (CCR) thinks it is in charge. If you click "Approve," it expects to execute the code itself.
If we want to run the code locally, we need to communicate two things simultaneously:
We solve this by using a disguise. We treat the plan as a "Rejection" message to the server, but inside that message, we hide the plan and a secret password.
The mechanism relies on a specific "Sentinel" string (a unique secret code):
__ULTRAPLAN_TELEPORT_LOCAL__
Think of this like a secret agent passing a briefcase.
From the perspective of our main application, this is handled by the pollForApprovedExitPlanMode function we built earlier.
It returns an object containing an executionTarget.
const result = await pollForApprovedExitPlanMode(sessionId, 60000);
if (result.executionTarget === 'local') {
// Teleportation! Run the plan on this computer.
console.log("π Teleporting plan to local execution...");
await executeShellScript(result.plan);
}
else {
// Normal approval. The server is already running it.
console.log("βοΈ Plan executing in the cloud...");
}
This simple switch allows the user to seamlessly move execution between the cloud and their laptop.
How does the system spot this secret message? It happens inside our event processing loop.
Here is the flow of data when a user triggers a teleport:
Let's look at ccrSession.ts to see how we implement this detection logic.
First, we define the magic string. It must be unique enough that no normal user would ever type it by accident.
// The secret code that flags a teleportation attempt
export const ULTRAPLAN_TELEPORT_SENTINEL = '__ULTRAPLAN_TELEPORT_LOCAL__'
We use a helper function called extractTeleportPlan. It looks at the text content of the "Error" message.
If the sentinel is missing, it returns null (meaning it's just a normal rejection). If found, it slices the string to get the code.
function extractTeleportPlan(content: any): string | null {
const text = contentToText(content) // Helper to get string
const marker = `${ULTRAPLAN_TELEPORT_SENTINEL}\n`
// Look for the secret handshake
const idx = text.indexOf(marker)
if (idx === -1) return null // Not a teleport
// Return everything AFTER the marker
return text.slice(idx + marker.length).trimEnd()
}
Finally, we update our ExitPlanModeScanner (from Chapter 4) to use this logic.
When the scanner sees a tool result that is an error (is_error === true), it checks for the teleport marker before deciding it's a rejection.
// Inside the scanner loop...
if (tr.is_error === true) {
// Check if this "error" is actually a teleport disguise
const teleportPlan = extractTeleportPlan(tr.content)
if (teleportPlan !== null) {
found = { kind: 'teleport', plan: teleportPlan }
} else {
// It's a real rejection
found = { kind: 'rejected', id }
}
}
In this final chapter, we learned how to perform Plan Teleportation.
Congratulations! You have built the core architecture of Ultraplan.
Let's recap what we've built:
You now understand the fundamental abstractions that make an AI planning agent feel fast, responsive, and magical. Happy coding!
Generated by Code IQ