{"skill":{"slug":"cord-trees","displayName":"Cord Trees","summary":"Dynamic task tree orchestration inspired by Cord protocol. Agent builds its own coordination tree at runtime — deciding decomposition, parallelism, and depen...","description":"---\nname: cord-trees\ndescription: |\n  Dynamic task tree orchestration inspired by Cord protocol. Agent builds its own coordination tree at runtime — deciding decomposition, parallelism, and dependencies dynamically. Implements spawn (isolated context) vs fork (inherited context) as first-class primitives, plus ask (human elicitation) and serial (ordered sequences).\n  \n  Use when: complex goals that need dynamic decomposition, tasks where the agent should decide how to break down work, multi-agent coordination with runtime flexibility, human-in-the-loop checkpoints.\n  \n  Triggers: \"figure out how to do X\", \"decompose this task\", \"build a task tree for\", \"dynamic orchestration\", \"cord-style\", \"self-organizing agents\"\nversion: 1.0.0\nlicense: MIT\nmetadata:\n  openclaw:\n    requires:\n      tools: [\"sessions_spawn\", \"subagents\", \"read\", \"write\"]\n---\n\n# Cord Trees — Dynamic Task Tree Orchestration\n\nBuild coordination trees at runtime. You decide the decomposition, not the developer.\n\nInspired by [Cord](https://github.com/kimjune01/cord) by June Kim.\n\n## Core Concept\n\nInstead of following a pre-defined workflow, you analyze the goal and build your own task tree:\n\n```\nGoal: \"Evaluate whether to migrate from REST to GraphQL\"\n\nYou decide:\n├── #1 spawn: Audit current REST API surface\n├── #2 spawn: Research GraphQL trade-offs  \n├── #3 ask: How many concurrent users? (blocked-by: #1)\n├── #4 fork: Comparative analysis (blocked-by: #2, #3)\n└── #5 fork: Write recommendation (blocked-by: #4)\n```\n\nThe tree emerges from your analysis, not from hardcoded logic.\n\n## Five Primitives\n\n### 1. SPAWN — Isolated Context\n\nChild gets only its task prompt. Clean slate.\n\n```python\nspawn(\n    goal=\"Research GraphQL adoption patterns\",\n    prompt=\"Search for case studies of REST→GraphQL migrations...\",\n    blocked_by=[]  # Can start immediately\n)\n```\n\n**Use when:** Task is self-contained, doesn't need sibling context.\n\n### 2. FORK — Inherited Context\n\nChild receives all completed sibling results injected into prompt.\n\n```python\nfork(\n    goal=\"Synthesize findings into recommendation\",\n    prompt=\"Based on the research, write a recommendation...\",\n    blocked_by=[\"research-rest\", \"research-graphql\", \"user-scale\"]\n)\n```\n\n**Use when:** Synthesis, analysis, or integration requiring prior work.\n\n### 3. ASK — Human Elicitation\n\nPause for human input. Creates a checkpoint.\n\n```python\nask(\n    question=\"How many concurrent users do you serve?\",\n    options=[\"<1K\", \"1K-10K\", \"10K-100K\", \">100K\"],\n    blocked_by=[\"audit-api\"]  # Ask after audit provides context\n)\n```\n\n**Use when:** Decision requires human knowledge or approval.\n\n### 4. SERIAL — Ordered Sequence\n\nChildren execute in order. Implicit dependencies.\n\n```python\nserial([\n    {\"goal\": \"Draft report\", \"type\": \"spawn\"},\n    {\"goal\": \"Review draft\", \"type\": \"ask\"},\n    {\"goal\": \"Finalize report\", \"type\": \"fork\"}\n])\n```\n\n**Use when:** Strict ordering required.\n\n### 5. GOAL — Root Node\n\nThe top-level objective. You decompose it into children.\n\n## Implementation with OpenClaw\n\nMap Cord primitives to OpenClaw tools:\n\n| Cord Primitive | OpenClaw Implementation |\n|----------------|-------------------------|\n| `spawn` | `sessions_spawn(task=prompt, label=id)` |\n| `fork` | `sessions_spawn` with sibling results in task |\n| `ask` | Message human, wait for response |\n| `serial` | Spawn sequentially, wait between each |\n| `read_tree` | Read state file + `subagents list` |\n| `complete` | Write result to state file |\n\n## State File\n\nTrack the tree in `cord-state.json`:\n\n```json\n{\n  \"goal\": \"Evaluate REST to GraphQL migration\",\n  \"nodes\": {\n    \"#1\": {\n      \"type\": \"spawn\",\n      \"goal\": \"Audit REST API\",\n      \"status\": \"complete\",\n      \"result\": \"47 endpoints, 12 nested...\",\n      \"blockedBy\": [],\n      \"sessionKey\": \"abc123\"\n    },\n    \"#2\": {\n      \"type\": \"spawn\",\n      \"goal\": \"Research GraphQL\",\n      \"status\": \"running\",\n      \"blockedBy\": [],\n      \"sessionKey\": \"def456\"\n    },\n    \"#3\": {\n      \"type\": \"ask\",\n      \"goal\": \"Get user scale\",\n      \"status\": \"waiting\",\n      \"question\": \"How many concurrent users?\",\n      \"options\": [\"<1K\", \"1K-10K\", \"10K-100K\", \">100K\"],\n      \"blockedBy\": [\"#1\"]\n    },\n    \"#4\": {\n      \"type\": \"fork\",\n      \"goal\": \"Comparative analysis\",\n      \"status\": \"blocked\",\n      \"blockedBy\": [\"#2\", \"#3\"]\n    }\n  },\n  \"nextId\": 5\n}\n```\n\n## Workflow\n\n### Phase 1: Analyze Goal\n\nRead the goal. Think about:\n- What are the major components?\n- What can run in parallel?\n- What has dependencies?\n- Where do I need human input?\n- What needs synthesis (fork) vs isolation (spawn)?\n\n### Phase 2: Build Initial Tree\n\nCreate nodes for the first level of decomposition:\n\n```python\n# Initialize state\nstate = {\n    \"goal\": user_goal,\n    \"nodes\": {},\n    \"nextId\": 1\n}\n\n# Add initial nodes\nadd_node(state, type=\"spawn\", goal=\"Research A\", blockedBy=[])\nadd_node(state, type=\"spawn\", goal=\"Research B\", blockedBy=[])\nadd_node(state, type=\"fork\", goal=\"Synthesize\", blockedBy=[\"#1\", \"#2\"])\n\nwrite(\"cord-state.json\", state)\n```\n\n### Phase 3: Execute Ready Nodes\n\nFind nodes that are ready (all blockedBy complete):\n\n```python\ndef get_ready_nodes(state):\n    ready = []\n    for id, node in state[\"nodes\"].items():\n        if node[\"status\"] != \"blocked\":\n            continue\n        deps = node[\"blockedBy\"]\n        if all(state[\"nodes\"][d][\"status\"] == \"complete\" for d in deps):\n            ready.append(id)\n    return ready\n```\n\nFor each ready node:\n\n**If spawn:**\n```python\nsessions_spawn(\n    task=node[\"prompt\"],\n    label=node_id,\n    runTimeoutSeconds=600\n)\nnode[\"status\"] = \"running\"\n```\n\n**If fork:**\n```python\n# Inject sibling results\nsibling_context = collect_sibling_results(state, node)\nfull_prompt = f\"{node['prompt']}\\n\\nContext from prior work:\\n{sibling_context}\"\n\nsessions_spawn(task=full_prompt, label=node_id)\nnode[\"status\"] = \"running\"\n```\n\n**If ask:**\n```python\n# Message human\nmessage(action=\"send\", message=f\"Question: {node['question']}\\nOptions: {node['options']}\")\nnode[\"status\"] = \"waiting\"\n# Wait for response, then mark complete with answer\n```\n\n### Phase 4: Monitor & Update\n\nPoll running agents, update state on completion:\n\n```python\nwhile has_running_or_blocked(state):\n    # Check agent status\n    agents = subagents(action=\"list\")\n    \n    for agent in agents:\n        node = find_node_by_session(state, agent[\"sessionKey\"])\n        if agent[\"status\"] == \"complete\":\n            # Get result from session history\n            result = get_agent_result(agent)\n            node[\"status\"] = \"complete\"\n            node[\"result\"] = result\n    \n    # Dispatch newly ready nodes\n    for node_id in get_ready_nodes(state):\n        dispatch_node(state, node_id)\n    \n    save_state(state)\n    wait(30)  # Don't poll too aggressively\n```\n\n### Phase 5: Synthesize\n\nWhen all nodes complete, the final fork node produces the result.\n\n## Dynamic Restructuring\n\nAgents can modify their own subtree at runtime:\n\n```python\n# Child agent realizes it needs more research\nadd_child_node(\n    parent=\"#2\",\n    type=\"spawn\",\n    goal=\"Deep dive on performance implications\",\n    blockedBy=[]\n)\n```\n\nThis is what makes Cord-style orchestration powerful — the tree evolves based on what agents discover.\n\n## Spawn vs Fork Decision Guide\n\n| Situation | Use |\n|-----------|-----|\n| Independent research task | spawn |\n| Task that doesn't need sibling context | spawn |\n| Cheap to restart if it fails | spawn |\n| Synthesis or analysis across prior work | fork |\n| Final integration step | fork |\n| Task that builds on discoveries | fork |\n\n**Default to spawn.** Use fork only when context inheritance is required.\n\n## Human-in-the-Loop Patterns\n\n### Approval Gate\n```\n#1 spawn: Draft proposal\n#2 ask: \"Approve this proposal?\" (blocked-by: #1)\n#3 fork: Implement approved proposal (blocked-by: #2)\n```\n\n### Clarification\n```\n#1 spawn: Initial analysis\n#2 ask: \"Which direction should we focus?\" (blocked-by: #1)\n#3 spawn: Deep dive on chosen direction (blocked-by: #2)\n```\n\n### Periodic Checkpoints\n```\n#1 spawn: Phase 1\n#2 ask: \"Continue to phase 2?\" (blocked-by: #1)\n#3 spawn: Phase 2 (blocked-by: #2)\n#4 ask: \"Continue to phase 3?\" (blocked-by: #3)\n...\n```\n\n## Example: Full Decomposition\n\nGoal: \"Create a comprehensive competitor analysis report\"\n\n```\n#1 [spawn] List top 5 competitors\n    └── No dependencies, starts immediately\n\n#2 [spawn] Research Competitor A (blocked-by: #1)\n#3 [spawn] Research Competitor B (blocked-by: #1)\n#4 [spawn] Research Competitor C (blocked-by: #1)\n#5 [spawn] Research Competitor D (blocked-by: #1)\n#6 [spawn] Research Competitor E (blocked-by: #1)\n    └── All parallel, isolated research\n\n#7 [fork] Identify patterns across competitors (blocked-by: #2-#6)\n    └── Needs all research results\n\n#8 [ask] \"Focus on pricing, features, or positioning?\" (blocked-by: #7)\n    └── Human steers direction\n\n#9 [fork] Deep analysis on chosen focus (blocked-by: #8)\n    └── Builds on patterns + human input\n\n#10 [fork] Write final report (blocked-by: #9)\n    └── Synthesis of everything\n```\n\n## Error Handling\n\n```python\nif node[\"status\"] == \"failed\":\n    # Options:\n    # 1. Retry (reset to blocked)\n    node[\"status\"] = \"blocked\"\n    node[\"retries\"] = node.get(\"retries\", 0) + 1\n    \n    # 2. Skip (mark complete with error)\n    node[\"status\"] = \"complete\"\n    node[\"result\"] = f\"FAILED: {error}\"\n    \n    # 3. Escalate (ask human)\n    add_node(state, type=\"ask\", \n             question=f\"Node {id} failed. Retry, skip, or abort?\",\n             blockedBy=[])\n```\n\n## Attribution\n\nThis skill implements patterns from the [Cord protocol](https://github.com/kimjune01/cord) by June Kim, adapted for OpenClaw's `sessions_spawn` and `subagents` primitives.\n","tags":{"latest":"1.0.1"},"stats":{"comments":0,"downloads":878,"installsAllTime":33,"installsCurrent":0,"stars":2,"versions":2},"createdAt":1771688476140,"updatedAt":1778992118184},"latestVersion":{"version":"1.0.1","createdAt":1772121935978,"changelog":"No user-visible changes; version bump only.\n\n- Version updated to 1.0.1 with no file changes detected.\n- All features, documentation, and functionality remain unchanged.","license":null},"metadata":{"setup":[],"os":null,"systems":null},"owner":{"handle":"moltenbot000","userId":"s17f11azj37wbyd16gcab6gks583q6p3","displayName":"Molten Bot 000","image":"https://avatars.githubusercontent.com/u/260473928?v=4"},"moderation":{"isSuspicious":false,"isMalwareBlocked":false,"verdict":"clean","reasonCodes":["review.llm_review"],"summary":"Review: review.llm_review","engineVersion":"v2.4.24","updatedAt":1779947329635}}