Implementation:23blocks OS Ai maestro Host Sync
| Knowledge Sources | |
|---|---|
| Domains | Mesh_Networking, Peer_Sync |
| Last Updated | 2026-10-26 10:00 GMT |
Overview
Concrete tool for registering a peer host, exchanging peer lists, and converging to eventual consistency across the mesh, provided by AI Maestro's `lib/host-sync.ts`.
Description
The addHostWithSync function is the core mesh registration step: it adds the host locally, registers ourselves with the remote host (back-registration), exchanges known peers, and propagates new peers to existing hosts with a depth limit to prevent infinite loops. It returns a `HostSyncResult` with what succeeded and what failed.
syncWithAllPeers manually triggers sync with all known enabled peer hosts — useful for recovery or manual mesh rebuild. getPublicUrl detects the host's public URL, preferring Tailscale IPs (100.x.x.x) over LAN, never returning localhost. markPropagationProcessed / generatePropagationId deduplicate gossip propagation.
Usage
- Call `addHostWithSync` from `registerPeer` when a new host joins the mesh.
- Call `syncWithAllPeers` for manual mesh rebuild or after network recovery.
Code Reference
Source Location
- Repository: ai-maestro
- File: lib/host-sync.ts (L38-40 generatePropagationId, L52-54 markPropagationProcessed, L65-80 getPublicUrl, L189-400 addHostWithSync, L633-680 syncWithAllPeers)
Signature
export async function addHostWithSync(
host: Host,
options?: {
skipBackRegistration?: boolean
skipPeerExchange?: boolean
skipPropagation?: boolean
propagationId?: string
propagationDepth?: number
}
): Promise<HostSyncResult>
export async function syncWithAllPeers(): Promise<{
synced: string[]
failed: string[]
}>
export function getPublicUrl(host?: Host): string
export function markPropagationProcessed(propagationId: string): void
interface HostSyncResult {
success: boolean
localAdd: boolean
backRegistered: boolean
peersExchanged: boolean
peersShared: number
errors: string[]
}
Import
import { addHostWithSync, syncWithAllPeers, getPublicUrl } from '@/lib/host-sync'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| host | Host | Yes (addHostWithSync) | New host to register |
| options.skipBackRegistration | boolean | No | Skip registering self with peer |
| options.propagationDepth | number | No | Prevents infinite gossip loops |
Outputs
| Name | Type | Description |
|---|---|---|
| addHostWithSync() returns | Promise<HostSyncResult> | localAdd, backRegistered, peersExchanged, errors |
| syncWithAllPeers() returns | Promise<{synced, failed}> | Arrays of synced and failed host IDs |
| getPublicUrl() returns | string | Public URL (Tailscale IP preferred) |
Usage Examples
import { addHostWithSync, syncWithAllPeers } from '@/lib/host-sync'
// Register a new peer host with full sync
const result = await addHostWithSync({
id: 'mac-mini',
name: 'Mac Mini',
url: 'http://192.168.1.50:23000',
enabled: true,
})
console.log(`Local add: ${result.localAdd}, peers shared: ${result.peersShared}`)
// Manual mesh rebuild after network recovery
const sync = await syncWithAllPeers()
console.log(`Synced: ${sync.synced}, Failed: ${sync.failed}`)