Engineering Long-Running Tasks with Multi-Agent Collaboration

    This article examines how to engineer AI agents for long-running software engineering tasks, where the real difficulty lies not in generating runnable code but in understanding existing project structures, decomposing work, orchestrating tools, and recovering from errors across multiple rounds. It explains a four-stage workflow (understanding, planning, execution, verification), a layered memory model that externalizes task state for persistence and recovery, and a coordinator-worker-verifier pattern that runs isolated agents in parallel with conflict detection before merging. It also covers executable verification loops with rollback to known-good checkpoints, tiering model capability by task difficulty, and observability with human-in-the-loop controls. A practical checklist and a closing note on building isolated, observable execution pipelines with general-purpose cloud capabilities are included.

    When an agent is asked to complete a complex software engineering task, the real difficulty is usually not generating a piece of runnable code. It is understanding the existing project structure, identifying constraints, breaking the work into steps, orchestrating a toolchain, and continuously correcting course across multiple rounds of feedback. Once the task cycle gets longer, the system has to solve three things at the same time: how to preserve context, how to persist state, and how to recover from errors. Recent product moves in the industry essentially point in the same direction: upgrading one-shot generation from a single model into a long-running execution system built around multiple collaborating roles. Setting aside specific product features, this article discusses how such a capability should be designed from a general engineering perspective.

    1. Why Long Tasks Are Hard: The Gap Between “Generation” and “Delivery”

    Short question-and-answer tasks have relatively clear evaluation functions: whether the answer is correct, or whether the code passes unit tests. The success criteria for long-cycle tasks are far more complex:

    • They require understanding existing implementations across files and modules, rather than writing new code in a vacuum.
    • They require turning vague requirements into verifiable sub-goals and ensuring those sub-goals do not conflict with each other.
    • They require running builds, tests, and static checks after each step, then deciding the next step based on the results.
    • They require rolling back to a known-good state on failure, instead of stacking more changes on top of a broken state.

    This means the core design metric shifts from “quality of a single output” to “task completion rate,” “average number of repair rounds,” and “number of human interventions.” In engineering practice, a long task is usually split into four stages: understanding (parsing the codebase and documentation), planning (generating a task graph), execution (tool calls and code changes), and verification (testing and review). If any one of these is missing, the process degrades into a pseudo-delivery that only looks finished.

    2. Context and State: The First-Order Constraint of Long Tasks

    A limited context window is an objective fact, so you cannot rely on stuffing all information into the prompt. A workable general approach is layered memory:

    1. Short-term working memory: file snippets for the current sub-task and the most recent command output.
    2. Mid-term task memory: the task list, completed items, open questions, and interface agreements.
    3. Long-term project memory: a summary of the directory structure, dependencies, coding conventions, and past decisions.

    A schematic task state structure can be written as:

    1. {
    2. "task_id": "refactor-auth",
    3. "goal": "Split the auth module and add missing tests",
    4. "subtasks": [
    5. {"id": "s1", "owner": "planner", "status": "done"},
    6. {"id": "s2", "owner": "worker", "status": "running", "branch": "iso/s2"},
    7. {"id": "s3", "owner": "verifier", "status": "pending"}
    8. ],
    9. "constraints": ["Keep the external interface unchanged", "Do not decrease test coverage"],
    10. "artifacts": ["docs/design.md", "tests/auth_test.py"]
    11. }

    The key point is that state must be externalized as readable and writable data rather than implied inside the conversation history. Only when it is externalized can the task be resumed after an interruption, and only then can multiple roles share the same source of truth.

    3. Multi-Agent Parallelism: Flattening a Serial Work Chain

    The bottleneck of a single-agent architecture is clear: the same model has to understand requirements, dispatch tasks, aggregate results, and review code. For cross-module changes, all operations are squeezed onto one chain, and both context and progress become limiting factors.

    The general pattern for multi-agent collaboration is “coordinator–worker–verifier”:

    • Coordinator agent: decomposes the task, generates the dependency graph, assigns sub-tasks, and handles conflicts.
    • Worker agents: modify code in parallel inside mutually isolated workspaces without interfering with each other.
    • Verifier agent: runs tests, checks diffs, raises objections, and reports failure reasons back.

    Isolation is the key engineering detail. A common practice is to give each worker an independent working copy or a separate branch, then have the coordinator merge them in dependency order. Conflict detection must happen before merging; otherwise parallelism introduces problems that are harder to diagnose.

    1. Example task graph (DAG):
    2. +--> Sub-task A (data layer) --+
    3. Req. | +--> Integration verification --> Delivery
    4. parsing +
    5. +--> Sub-task B (API layer) --+

    4. The Verification Loop: Putting the Model in a “Read Files, Run Code, Verify” Cycle

    Traditional text benchmarks struggle to cover real engineering work, because real work involves large amounts of chart-heavy material, engineering diagrams, logs, and build output. A reliable execution loop should be:

    1. while not done:
    2. obs = workspace.read_diff() # Read the current changes
    3. plan = planner.next_step(obs) # Decide the next step
    4. result = executor.run(plan) # Modify files or call tools
    5. report = verifier.check(result) # Run tests / static checks
    6. if report.failed:
    7. state.rollback_to_last_good() # Return to the last known-good state
    8. planner.revise(report.reason) # Re-plan based on the failure reason
    9. else:
    10. state.commit(result) # Persist progress

    There are three design points here. First, verification must be executable rather than a model’s self-assessment. Second, failures must carry a structured reason so re-planning is easier. Third, every success must be persisted as a checkpoint to avoid “getting worse with every change.”

    5. Tiering Role Capability: Put Stronger Reasoning Where the Difficulty Is High

    Not every sub-task needs the same level of capability. A reasonable approach is to tier by difficulty: positions that require long thinking and multiple rounds of verification, such as architecture design, task coordination, and code review, go to models with stronger reasoning; lighter work such as concrete file edits, formatting, and adding unit tests goes to lower-cost models. This both controls spend and puts strong models where the payoff is highest.

    6. Human-AI Collaboration and Observability

    Long tasks must have an observable interface: each agent’s current goal, scope of changes, command output, and failure reasons should all be visible, and humans should be able to step in and adjust direction at any time. On the engineering side, it is advisable to pair this with general-purpose capabilities: use a logging service to collect execution traces, use monitoring and alerting to track task success rate and average repair rounds, use object storage to keep intermediate artifacts and checkpoints, and use a container platform to provide isolated and reproducible execution environments.

    7. Implementation Checklist

    • Start with a narrow scenario: pick one task type with clear boundaries and complete tests for a pilot.
    • Enforce isolation: parallel execution must use workspace isolation, with conflict detection before merging.
    • Verification first: tasks without executable verification do not enter the automated loop.
    • Externalize state: persist the task list and checkpoints so interrupted runs can resume.
    • Tier cost: allocate model capability by difficulty and avoid using high-cost reasoning everywhere.
    • Keep human gates: high-risk changes (data migration, permission changes) require human confirmation.

    Conclusion

    The essence of long tasks and multi-agent collaboration is migrating the existing methodology of software engineering—requirement decomposition, branch isolation, continuous integration, code review—into an agent execution framework. Model capability is only one part of it; what truly determines delivery quality is workflow design, state management, and the verification loop. If an organization wants to build a similar system in its own environment, it can use general-purpose capabilities such as Baidu AI Cloud’s container platform, logging service, monitoring and alerting, and object storage to set up an isolated execution and observability pipeline, moving from “generating code” to “reliably delivering runnable results.”