# Inside Google Antigravity Dynamic Workflows: Python Asyncio Primitives, Worktrees & Fail-Fast Concurrency

![Google Antigravity Dynamic Workflows Architecture](https://softreviewed.com/wp-content/uploads/2026/11/antigravity_dynamic_workflows_cover-1.png)

When Google introduced Antigravity 2.0, the biggest architectural shift for backend engineers was the transition away from static Markdown instructions toward executable orchestration scripts via `/run-workflow`.

Instead of relying on prompt chains that accumulate conversational dead weight, **Google Antigravity Dynamic Workflows** compile orchestration logic into asynchronous Python code. The runtime spins up parallel subagents in isolated sandboxes, enforces strict JSON Schema contracts, and uses native `asyncio` event loops for fail-fast error recovery.

To see the concurrent execution loop in action in under 60 seconds, watch my video breakdown below:

%[https://www.youtube.com/shorts/vpIIwVZQnjI]

---

## 1. The Architectural Problem: Conversational Drift in Agent Loops

When an LLM attempts to execute iterative engineering tasks across dozens of files inside a single chat window, three systemic issues emerge:

1. **Context Bloat:** Accumulating source files and terminal stderr dumps degrades attention mechanisms, leading to hallucinated imports and forgotten constraints.
2. **Heuristic Loops:** Natural language prompts like *"review all 20 files in src/"* are probabilistic. LLMs frequently skip files or terminate prematurely without completion guarantees.
3. **Cascading Failure Modes:** When a sub-task fails in a prompt chain, the agent often proceeds blindly, compounding errors across downstream dependencies.

Dynamic workflows resolve this by decoupling the *planner* from the *workers*. The lead model authors an asynchronous Python script that coordinates execution with deterministic state control.

```
Traditional Prompt Chaining ❌
User Chat ──► 20 Files Read ──► Massive Token Bloat ──► Prompt Drift & Broken Code

Antigravity Dynamic Workflows ✅
User Chat ──► Writes Python Script ──► Parallel Worker Agents ──► Clean Summary in Chat
```

---

## 2. Core Python Asyncio Primitives

Antigravity executes dynamic workflow scripts in a dedicated Python 3.8+ environment. The platform injects several powerful orchestration primitives:

```python
# Real example of a Dynamic Workflow script in Antigravity
phase("Discover")
modules = await agent(
    "List all Python module paths (.py files) in src/inventory. "
    "Only inspect file paths. Return relative paths sorted alphabetically.",
    schema={"type": "array", "items": {"type": "string"}},
    role="Discover Inventory Modules"
)

phase("Review")
reviews = await parallel(modules, lambda mod: agent(
    f"Inspect {mod} for unhandled database exceptions. Return a 1-line verdict.",
    schema={"type": "string"},
    role=f"Audit {mod}",
    workspace="branch",
    model="flash"
))

for r in reviews:
    log(r)
```

### Breakdown of Primitives:

* **`phase(name)`**: Visual milestone markers. In both the Antigravity desktop IDE and CLI (`agy`), phases group logs and agent cards so developers can monitor execution stages in real time.
* **`agent(prompt, schema, role, workspace, model)`**: Constructs a lazy helper agent. It does not spawn until awaited or passed into an execution runner. Pinned to lightweight tiers like `model="flash"` to optimize token consumption.
* **`schema` (Strict JSON Contracts)**: Forces the worker agent to terminate by calling its internal `finish` tool with structured arguments. Unstructured text responses are rejected.
* **`parallel(items, fn)`**: Maps concurrent async tasks across items. Crucially, if any worker throws an unhandled exception or fails schema validation, all sibling workers are cancelled immediately via `asyncio` task cancellation.
* **`workspace="branch"`**: Automatically provisions isolated Git worktrees for each subagent. Workers can modify files, execute linters, and stage commits concurrently without git lock collisions.

![Google Antigravity Dynamic Workflows Architecture Infographic](https://softreviewed.com/wp-content/uploads/2026/11/antigravity_dynamic_workflows_infographic-1.png)

---

## 3. Dynamic Workflows vs. Swarm vs. Subagents

Antigravity provides three distinct multi-agent paradigms depending on execution shape:

| Evaluation Metric | Dynamic Workflows (`/run-workflow`) | Native Swarm (`/swarm`) | Standard Subagents (`invoke_subagent`) |
| :--- | :--- | :--- | :--- |
| **Coordination Model** | Deterministic Python script | Shared Message Board (`@mentions`) | Direct parent-child invocation |
| **Communication** | Structured JSON returned to Python | Multi-agent debate and handoffs | Return string or tool output |
| **Failure Handling** | Fail-fast cancellation across workers | Stall monitor reassignments | Error returned to caller |
| **Token Efficiency** | Very high (Flash workers) | Higher (workers read board posts) | Low (isolated execution) |
| **Primary Use Case** | Codebase audits, batch refactoring, test suites | Multi-role specialist teams | Single browser test, graphic design |

---

## 4. Script Persistence and CLI Automation

Unlike ephemeral chat prompts, dynamic workflow scripts can be saved directly into your repository as permanent automation assets:

```powershell
# Running a saved, parameterized dynamic workflow via the Antigravity CLI
agy /run-workflow --file ./workflows/security_audit.py --args '{"target_dir": "src/auth"}'
```

This transforms dynamic workflows from a conversational assistant feature into a deterministic CI/CD and developer tooling platform.

For a full breakdown of the 4-stage pipeline, bookstore audit benchmarks, and security approval gates, read my complete guide on [Google Antigravity Dynamic Workflows](https://softreviewed.com/google-antigravity-dynamic-workflows/) at SoftReviewed.
