repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | /** |
| #2 | * Spawn |
| #3 | * |
| #4 | * Spawn child automatons in new CLAWD sandboxes. |
| #5 | * The parent creates a new sandbox, installs the runtime, |
| #6 | * writes a genesis config, funds the child, and starts it. |
| #7 | */ |
| #8 | import fs from "fs"; |
| #9 | import pathLib from "path"; |
| #10 | import { MAX_CHILDREN } from "../types.js"; |
| #11 | import { ulid } from "ulid"; |
| #12 | /** |
| #13 | * Spawn a child automaton in a new CLAWD sandbox. |
| #14 | */ |
| #15 | export async function spawnChild(runtime, identity, db, genesis) { |
| #16 | // Check child limit |
| #17 | const existing = db.getChildren().filter((c) => c.status !== "dead"); |
| #18 | if (existing.length >= MAX_CHILDREN) { |
| #19 | throw new Error(`Cannot spawn: already at max children (${MAX_CHILDREN}). Kill or wait for existing children to die.`); |
| #20 | } |
| #21 | const childId = ulid(); |
| #22 | // 1. Create a new sandbox for the child |
| #23 | const sandbox = await runtime.createSandbox({ |
| #24 | name: `automaton-child-${genesis.name.toLowerCase().replace(/\s+/g, "-")}`, |
| #25 | vcpu: 1, |
| #26 | memoryMb: 512, |
| #27 | diskGb: 5, |
| #28 | }); |
| #29 | const child = { |
| #30 | id: childId, |
| #31 | name: genesis.name, |
| #32 | address: "0x0000000000000000000000000000000000000000", // Will be set after keygen |
| #33 | sandboxId: sandbox.id, |
| #34 | genesisPrompt: genesis.genesisPrompt, |
| #35 | creatorMessage: genesis.creatorMessage, |
| #36 | fundedAmountCents: 0, |
| #37 | status: "spawning", |
| #38 | createdAt: new Date().toISOString(), |
| #39 | }; |
| #40 | db.insertChild(child); |
| #41 | // 2. Install Node.js and the automaton runtime in the child sandbox |
| #42 | await execInSandbox(runtime, sandbox.id, "apt-get update -qq && apt-get install -y -qq nodejs npm git curl", 120000); |
| #43 | // 3. Install the automaton runtime |
| #44 | await execInSandbox(runtime, sandbox.id, "npm install -g @clawd/automaton@latest 2>/dev/null || true", 60000); |
| #45 | // 4. Write the genesis configuration |
| #46 | const genesisJson = JSON.stringify({ |
| #47 | name: genesis.name, |
| #48 | genesisPrompt: genesis.genesisPrompt, |
| #49 | creatorMessage: genesis.creatorMessage, |
| #50 | creatorAddress: identity.address, // Parent is the creator |
| #51 | parentAddress: identity.address, |
| #52 | }, null, 2); |
| #53 | await writeInSandbox(runtime, sandbox.id, "/root/.automaton/genesis.json", genesisJson); |
| #54 | // 4b. Propagate constitution (immutable, inherited before anything else) |
| #55 | const constitutionPath = pathLib.join(process.env.HOME || "/root", ".automaton", "constitution.md"); |
| #56 | try { |
| #57 | const constitution = fs.readFileSync(constitutionPath, "utf-8"); |
| #58 | await writeInSandbox(runtime, sandbox.id, "/root/.automaton/constitution.md", constitution); |
| #59 | // Make it read-only in the child |
| #60 | await execInSandbox(runtime, sandbox.id, "chmod 444 /root/.automaton/constitution.md", 5000); |
| #61 | } |
| #62 | catch { |
| #63 | // Constitution file not found locally — child will get it from the repo on build |
| #64 | } |
| #65 | // 5. Record the spawn |
| #66 | db.insertModification({ |
| #67 | id: ulid(), |
| #68 | timestamp: new Date().toISOString(), |
| #69 | type: "child_spawn", |
| #70 | description: `Spawned child: ${genesis.name} in sandbox ${sandbox.id}`, |
| #71 | reversible: false, |
| #72 | }); |
| #73 | return child; |
| #74 | } |
| #75 | /** |
| #76 | * Start a child automaton after setup. |
| #77 | */ |
| #78 | export async function startChild(runtime, db, childId) { |
| #79 | const child = db.getChildById(childId); |
| #80 | if (!child) |
| #81 | throw new Error(`Child ${childId} not found`); |
| #82 | // Initialize wallet, provision, and run |
| #83 | await execInSandbox(runtime, child.sandboxId, "automaton --init && automaton --provision && systemctl start automaton 2>/dev/null || automaton --run &", 60000); |
| #84 | db.updateChildStatus(childId, "running"); |
| #85 | } |
| #86 | /** |
| #87 | * Check a child's status. |
| #88 | */ |
| #89 | export async function checkChildStatus(runtime, db, childId) { |
| #90 | const child = db.getChildById(childId); |
| #91 | if (!child) |
| #92 | throw new Error(`Child ${childId} not found`); |
| #93 | try { |
| #94 | const result = await execInSandbox(runtime, child.sandboxId, "automaton --status 2>/dev/null || echo 'offline'", 10000); |
| #95 | const output = result.stdout || "unknown"; |
| #96 | // Parse status from output |
| #97 | if (output.includes("dead")) { |
| #98 | db.updateChildStatus(childId, "dead"); |
| #99 | } |
| #100 | else if (output.includes("sleeping")) { |
| #101 | db.updateChildStatus(childId, "sleeping"); |
| #102 | } |
| #103 | else if (output.includes("running")) { |
| #104 | db.updateChildStatus(childId, "running"); |
| #105 | } |
| #106 | return output; |
| #107 | } |
| #108 | catch { |
| #109 | db.updateChildStatus(childId, "unknown"); |
| #110 | return "Unable to reach child sandbox"; |
| #111 | } |
| #112 | } |
| #113 | /** |
| #114 | * Send a message to a child automaton. |
| #115 | */ |
| #116 | export async function messageChild(runtime, db, childId, message) { |
| #117 | const child = db.getChildById(childId); |
| #118 | if (!child) |
| #119 | throw new Error(`Child ${childId} not found`); |
| #120 | // Write message to child's message queue |
| #121 | const msgJson = JSON.stringify({ |
| #122 | from: "parent", |
| #123 | content: message, |
| #124 | timestamp: new Date().toISOString(), |
| #125 | }); |
| #126 | await writeInSandbox(runtime, child.sandboxId, `/root/.automaton/inbox/${ulid()}.json`, msgJson); |
| #127 | } |
| #128 | // ─── Helpers ────────────────────────────────────────────────── |
| #129 | async function execInSandbox(runtime, sandboxId, command, timeout = 30000) { |
| #130 | // Use the CLAWD Runtime API to exec in a specific sandbox |
| #131 | const apiUrl = runtime.__apiUrl || "https://api.x402.wtf"; |
| #132 | const apiKey = runtime.__apiKey || ""; |
| #133 | const resp = await fetch(`${apiUrl}/v1/sandboxes/${sandboxId}/exec`, { |
| #134 | method: "POST", |
| #135 | headers: { |
| #136 | "Content-Type": "application/json", |
| #137 | Authorization: apiKey, |
| #138 | }, |
| #139 | body: JSON.stringify({ command, timeout }), |
| #140 | }); |
| #141 | if (!resp.ok) { |
| #142 | const text = await resp.text(); |
| #143 | throw new Error(`Exec in sandbox ${sandboxId} failed: ${text}`); |
| #144 | } |
| #145 | return resp.json(); |
| #146 | } |
| #147 | async function writeInSandbox(runtime, sandboxId, path, content) { |
| #148 | const apiUrl = runtime.__apiUrl || "https://api.x402.wtf"; |
| #149 | const apiKey = runtime.__apiKey || ""; |
| #150 | // Ensure parent directory exists |
| #151 | const dir = path.substring(0, path.lastIndexOf("/")); |
| #152 | await execInSandbox(runtime, sandboxId, `mkdir -p ${dir}`, 5000); |
| #153 | const resp = await fetch(`${apiUrl}/v1/sandboxes/${sandboxId}/files/upload/json`, { |
| #154 | method: "POST", |
| #155 | headers: { |
| #156 | "Content-Type": "application/json", |
| #157 | Authorization: apiKey, |
| #158 | }, |
| #159 | body: JSON.stringify({ path, content }), |
| #160 | }); |
| #161 | if (!resp.ok) { |
| #162 | const text = await resp.text(); |
| #163 | throw new Error(`Write to sandbox ${sandboxId} failed: ${text}`); |
| #164 | } |
| #165 | } |
| #166 | //# sourceMappingURL=spawn.js.map |