Delegation

How Chat hands a job to Sysadmin, Developer or Code reviewer, goes to sleep, and wakes with the answer.

intermediate9 min read

You ask Chat, “How much memory is free on this machine?” The reply may start with a handoff. Chat can ask Sysadmin for a live reading, wait for the result, and use it to answer you. While it waits, there are two conversations on disk, tied together by one tool call.

In this chapter

  • Identify who chooses a specialist and what the specialist receives.
  • Follow a delegate call from its first execution to its saved result.
  • See what a specialist can finish within its budget and what survives a restart.
  • Find the specialist’s transcript and results from the parent chat.

Chat writes an assignment for a specialist

A handoff to another conversation is called delegationA handoff in which a conversation gives a self-contained task to a specialist conversation and waits for its answer.. The conversation you’re in is the parent. The new conversation, called the child, is the specialistA conversation that does a delegated task using the selected profile’s own instructions and grants. for that assignment. The parent model decides when to ask for help. Its profile sets the available targets in delegates and tells the model when to use them in the Markdown instructions. chat.md

For a live reading of the Pipal host, Chat’s instructions say to pick Sysadmin. For a bug investigation or implementation, they point to Developer. For a patch review, they point to Code reviewer. The model makes its choice by calling delegate with the target profile ID and the assignment text. chat.md delegate.ts

That’s why “and now?” can work in Chat and still make a poor assignment for Sysadmin. Chat can read the earlier turn and write out the measurement it wants repeated. A Developer chat can ask Code reviewer for a second look, but the reviewer’s view of a project may differ from Developer’s. Developer’s profile tells the model to send the relevant diff and context so the reviewer has the version under review. chat.md developer.md

One tool task owns one child

From assignment to answer
  • Parent chat
  • Delegate
  • Pi Durable
  • Specialist

One handoff

  1. Parent chat → Pi Durable · Tool call

    The parent model calls delegate

    Chat asks sysadmin for a host memory reading. It gives the specialist a self-contained task.

  2. Pi Durable → Delegate · Run call

    Pi starts the saved tool task

    Pi records the tool call before running it. The task ID will identify both the owner of the child and the request used to submit its work.

  3. Delegate → Pi Durable · Check target

    Pipal checks the caller and target

    Delegation must be enabled. The parent’s current profile must allow the specialist, and the calling conversation must be top-level. The task must contain text.

  4. Delegate → Pi Durable · Create child

    One commit creates and configures the child

    The tool first looks for a conversation owned by its task ID. On the first execution it creates one, applies the specialist’s profile, and carries over the parent’s workspace binding in the same commit.

  5. Delegate → Pi Durable · Child ID

    The parent can now point to a transcript

    The call’s live details include the child conversation ID and profile.

  6. Delegate → Specialist · Submit task

    The specialist gets a message of its own

    The submission sends the task text under delegate:<taskId> and asks Pi to notify the tool task when it settles.

  7. Delegate → Pi Durable · Sleep

    Pi saves the wait

    The invocation returns a sleep instruction. Pi records the wait for SubmissionSettled on the key answered, then the call can wake when the signal arrives.

  8. Specialist → Specialist · Measure

    Sysadmin gathers a reading

    The child runs with its own profile and can call any diagnostics granted to it. The delegate invocation is asleep.

  9. Pi Durable → Pi Durable · Settle

    Pi settles the specialist’s submission

    Pi records the settlement and delivers the answered signal in one commit. A completed input’s settlement names the saved assistant answer.

  10. Pi Durable → Delegate · Wake

    The same task gets a fresh invocation

    It checks the current grant again before reading the settlement. If the grant was removed, the call fails at that check.

  11. Delegate → Pi Durable · Tool result

    The tool returns the specialist’s answer

    The tool returns the saved answer text or the reason the child ended without an answer. Either result can carry references to artifacts the child already published.

  12. Pi Durable → Parent chat · Continue

    The parent gets the findings

    The next model request includes the tool result, so Chat can use the measurement in its reply.

Restart before park

  1. Parent chat → Pi Durable · Tool call

    The parent model calls delegate

    Chat asks sysadmin for a host memory reading. It gives the specialist a self-contained task.

  2. Pi Durable → Delegate · Run call

    Pi starts the saved tool task

    Pi records the tool call before running it. The task ID will identify both the owner of the child and the request used to submit its work.

  3. Delegate → Pi Durable · Check target

    Pipal checks the caller and target

    Delegation must be enabled. The parent’s current profile must allow the specialist, and the calling conversation must be top-level. The task must contain text.

  4. Delegate → Pi Durable · Create child

    One commit creates and configures the child

    The tool first looks for a conversation owned by its task ID. On the first execution it creates one, applies the specialist’s profile, and carries over the parent’s workspace binding in the same commit.

  5. Delegate → Pi Durable · Child ID

    The parent can now point to a transcript

    The call’s live details include the child conversation ID and profile.

  6. Delegate → Specialist · Submit task

    The specialist gets a message of its own

    The submission sends the task text under delegate:<taskId> and asks Pi to notify the tool task when it settles.

  7. Pi Durable → Pi Durable · Restart

    The process dies before the park is stored

    In this example the child submission was already committed and has yet to settle. Both it and the task that owns it survive.

  8. Pi Durable → Delegate · Replay

    Pi re-enters the replay-safe call

    With the grant still in place and replay within the restart limit, the same task checks for a settlement, then finds its existing child by owner task ID.

  9. Delegate → Specialist · Retry ID

    A retry names the original submission

    The same delegate:<taskId> request ID recovers the submission without giving Sysadmin a second assignment.

  10. Delegate → Pi Durable · Sleep

    Pi saves the wait

    The invocation returns a sleep instruction. Pi records the wait for SubmissionSettled on the key answered, then the call can wake when the signal arrives.

  11. Pi Durable → Pi Durable · Settle

    Pi settles the specialist’s submission

    Pi records the settlement and delivers the answered signal in one commit. A completed input’s settlement names the saved assistant answer.

  12. Pi Durable → Delegate · Wake

    The same task gets a fresh invocation

    It checks the current grant again before reading the settlement. If the grant was removed, the call fails at that check.

  13. Delegate → Pi Durable · Tool result

    The tool returns the specialist’s answer

    The tool returns the saved answer text or the reason the child ended without an answer. Either result can carry references to artifacts the child already published.

  14. Pi Durable → Parent chat · Continue

    The parent gets the findings

    The next model request includes the tool result, so Chat can use the measurement in its reply.

Restart after park

  1. Parent chat → Pi Durable · Tool call

    The parent model calls delegate

    Chat asks sysadmin for a host memory reading. It gives the specialist a self-contained task.

  2. Pi Durable → Delegate · Run call

    Pi starts the saved tool task

    Pi records the tool call before running it. The task ID will identify both the owner of the child and the request used to submit its work.

  3. Delegate → Pi Durable · Check target

    Pipal checks the caller and target

    Delegation must be enabled. The parent’s current profile must allow the specialist, and the calling conversation must be top-level. The task must contain text.

  4. Delegate → Pi Durable · Create child

    One commit creates and configures the child

    The tool first looks for a conversation owned by its task ID. On the first execution it creates one, applies the specialist’s profile, and carries over the parent’s workspace binding in the same commit.

  5. Delegate → Pi Durable · Child ID

    The parent can now point to a transcript

    The call’s live details include the child conversation ID and profile.

  6. Delegate → Specialist · Submit task

    The specialist gets a message of its own

    The submission sends the task text under delegate:<taskId> and asks Pi to notify the tool task when it settles.

  7. Delegate → Pi Durable · Sleep

    Pi saves the wait

    The invocation returns a sleep instruction. Pi records the wait for SubmissionSettled on the key answered, then the call can wake when the signal arrives.

  8. Pi Durable → Pi Durable · Restart

    The saved wait survives the restart

    Pi keeps the parent parked. Recovery does not rerun the delegate handler just to restore the wait. The child can continue under its own tasks’ recovery rules.

  9. Pi Durable → Pi Durable · Settle

    Pi settles the specialist’s submission

    Pi records the settlement and delivers the answered signal in one commit. A completed input’s settlement names the saved assistant answer.

  10. Pi Durable → Delegate · Wake

    The same task gets a fresh invocation

    It checks the current grant again before reading the settlement. If the grant was removed, the call fails at that check.

  11. Delegate → Pi Durable · Tool result

    The tool returns the specialist’s answer

    The tool returns the saved answer text or the reason the child ended without an answer. Either result can carry references to artifacts the child already published.

  12. Pi Durable → Parent chat · Continue

    The parent gets the findings

    The next model request includes the tool result, so Chat can use the measurement in its reply.

Parent chat
Delegate
Pi Durable
Specialist

Parent chat → Pi Durable

The parent model calls delegate

Chat asks sysadmin for a host memory reading. It gives the specialist a self-contained task.

Compare an ordinary handoff with a restart on either side of the saved park. Before the park, the tool can replay and find the same child. After the park, Pi preserves the wait.

Pipal checks the right to delegate before it creates the child. The caller must be top-level, delegation must be enabled, and the current profile must allow the target. Pipal creates the child and applies the specialist’s profile in one commit. Pi records the tool task as the child’s owner, giving Pipal a durable way to find the same conversation again. index.ts delegate.ts

After submitting the assignment, the tool asks Pi to park it. Parking saves what it is waiting for and ends the active invocation. The child can run while the parent waits. When the child’s submission settles, Pi saves the settlement and delivers the signal in the same commit, even if the child finishes before the park. On wake, the tool checks its grant again before it reads the result. delegate.ts

Deep diveHow the two IDs cover the gap before parking

delegate is declared replay-safe. Its schema uses a maximum length of 32,768. The handler also rejects an assignment that is all whitespace or longer than 32,768 UTF-16 code units. execute() calls authorizeDelegate() on each entry, then checks for a stored SubmissionSettled value on answered. If it needs to start or recover a submission, it scans for a conversation owned by api.taskId. It creates one only when the scan finds none. That commit also runs prepareChild(), so the child ID and its configured profile become visible together. delegate.ts contracts.ts index.ts

packages/core/src/delegate.ts
// Pi delivers the signal in the same commit that settles this submission.
await child.submit({
type: 'input', content: task, requestId: `delegate:${api.taskId}`,
notify: { taskId: api.taskId, key: 'answered' }
}, ctx);
return { kind: 'sleep', signals: [SubmissionSettled.key('answered')] };

The creating commit, the submission, and the park are separate writes. If the process dies before parking, Pi can replay the handler within the restart limit. If settlement already arrived, the handler can return the result without resubmitting. Otherwise it finds any committed child by owner task ID. If the submission already committed, the same request ID recovers it. Pi keeps a signal that arrives early, so a call that parks with the signal already present wakes immediately. If the park had already committed, Pi restores the wait without replaying the handler. The recovery test kills the process at that later point and checks the original child ID, one task message in the child, and one delegate result in the parent. delegate.ts README.md recovery-worker.ts

The wake starts a fresh invocation. If the current grant still allows it and the settlement refers to a completed input, the tool loads the named AssistantEntry and returns its text. A submission that settles without an answer gives the parent model an error result with the reason or status. If a completed input’s assistant message is missing, the handler throws. delegate.ts

The tree stays one level deep

Two handoffs in a saved chat
  • Chat conversation · Top-level

    A top-level Chat can make several separate delegate calls over its lifetime. This tree shows a project investigation followed by a review.

    • Original request

      The user’s message belongs to the parent. Relevant details reach a child through the assignment the parent model writes.

    • First delegate call · Task

      This task owns one Developer conversation. Its task ID identifies the handoff across a retry.

      • Developer · Child conversation

        Developer investigates under its own profile. Because this conversation has an owner, Developer cannot delegate from here.

        • Assignment

          The fault to trace and the relevant project details are written into a new user message.

        • Findings

          The answer is saved in the child. The tool returns its text to Chat, along with references to any files or web artifacts Developer published.

    • Later delegate call · Task

      The top-level model can ask Code reviewer to check a proposed change in a later call. This is a separate tool task under Chat.

      • Code reviewer · Child conversation

        This child starts with the new assignment. Its reviewer instructions ask it to lead with defects and regressions.

        • Assignment

          Chat can include the relevant diff and the behavior to verify.

        • Review

          Chat gets a second tool result, independently of the answer from Developer.

  • Findings → Chat conversation · Answer returned by first delegate call
  • Review → Chat conversation · Answer returned by later delegate call

Developer

Developer investigates under its own profile. Because this conversation has an owner, Developer cannot delegate from here.

Select the two call tasks and their children. These handoffs happened in sequence. Both children hang from calls made in the top-level chat.

Developer can call Code reviewer from a top-level Developer chat. When Developer is the child of a Chat call, delegation stops there. Pipal leaves delegate out of the child’s tool selection and its authorization check rejects a call from any conversation with an owner. Pi runs these calls one at a time, so a parent can build up several child transcripts over time while waiting on at most one foreground specialist at once. index.ts

Some settings do cross the boundary. A specialist uses its profile’s model if it pins one and the parent’s selected model otherwise. In prepareChild(), it inherits the parent’s workspace locationThe place a chat uses for project tools, either the Pipal host, an account reached over SSH, or a Docker project., project, and scope where present. The scope is the value Core checks before letting the chat use its bound workspace. If the parent’s YOLO grant matches the current scope and the specialist’s profile grants workspace tools, the child inherits the grant that lets eligible calls proceed without asking again. index.ts

Developer’s profile requests both pipal.web-search and pipal.public-web as optional extensions. The operator enables each through extensions.enabled. Developer’s instructions favor hosted search when the model and provider support it, and describe search_public_web as a fallback and read_url as the tool for a specific public page. developer.md config.ts

For a Developer specialist working in Docker, these grants can make documentation available while its shell stays offline. The host sends search_public_web queries to Exa and read_url requests to Jina Reader. public-web.ts

A foreground specialist shares the parent’s clock and concurrency slot. It has its own limit on tool roundsA model response that requests tools, together with the tool calls it requested.. By default, the cap is 50 for a top-level Developer chat and 20 for Developer on assignment from Chat. An explicit runtime.max_turns supplies the host cap in either case, and a smaller profile max_turns lowers it. index.ts config.ts

As that limit approaches, Pipal tells the specialist how many tool rounds it has left and reserves a final reply for what it finished and what still needs checking. If you ask Developer for a patch, its profile tells it to publish the candidate before verification. The file can reach you even if the specialist runs out of rounds before checking it. turn-budget.ts developer.md

Stop in the parent aborts the call, which aborts the child it owns. Workers covers the separate conversation that can work on a longer job with its own budget. index.ts README.md

Deep diveHow Pipal enforces and recovers the budgets

budgetOwner() resolves a foreground specialist to the owning conversation. During recovery, Pipal arms a deadline timer only for a conversation that owns its budget. A parent timeout marks its active specialists before aborting the parent, so the stopped work can report a time-limit failure. Admission counts ownerless submissions that are using a slot, skipping runs waiting for the user. Core skips expiry while the specialist waits for approval and adds that wait to the deadline when it records the decision. During normal operation, partial text from a specialist can earn the parent one finishing window, capped at 90 seconds and at the configured response timeout. Startup deadline checks skip that grace. index.ts

The generation hook in turn-budget.ts reads the child’s saved pi.agent.maxTurns and the round count in pi.live. It warns when two or one tool rounds remain before closeout, preserving one usable round even at a cap of one. For the final request it removes the tool declarations from that request and records the generation in pipal.chat.closeoutRun. Its tool hook blocks any call the model still requests on that generation. Pi can still report a hard turn_limit if the run reaches its cap. turn-budget.ts view.ts

Resume response belongs to an eligible top-level chat. A specialist that reaches its own limit returns its final text or a failure to the parent’s delegate call. To continue the specialist’s work, give the parent the next instruction, with the published file’s ID or the remaining check as context. A new delegate call creates a new child with the parent’s workspace binding. Restart recovery of a parked call instead uses the original child and submission. view.ts delegate.ts index.ts

The parent chat keeps the child within reach

Open the transcript while the specialist works and you can watch that conversation’s own updates. Reopen it after the call finishes and you can still see the saved messages. The sidebar lists top-level chats, so the route back to the specialist is the call in the parent transcript. DelegateTranscript.svelte view.ts

An approval can surface in the parent even while the specialist’s transcript is collapsed. Approvals and YOLO follows the decision back to the tool that asked for it. After the tool returns, file downloads and HTML previews handed back by the child appear below the specialist’s transcript button. You can use those cards while the transcript stays closed. App.svelte WebArtifact.svelte

Deep diveHow the browser discovers a child and its results

view.ts projects the child ID and profile from the tool’s details into delegation, on both a live slot and its saved result. While a delegate call is sleeping, Core includes that child in the list it passes to specialistRequests(). The function reads each child’s chat and live state and returns a map from child ID to a list of {name, approval} pairs. The parent snapshot attaches those requests to the live slot’s delegation field. view.ts index.ts

TranscriptView passes the projected child to delegateActivity in App.svelte. When the child transcript is collapsed, the snippet gives each approval card the child ID so the decision goes to the right conversation. Expanding mounts DelegateTranscript.svelte, which fetches the child, watches it while live, and can page earlier messages. It uses the same TranscriptView as the main chat, including the grouping of adjacent tool calls. TranscriptView.svelte App.svelte DelegateTranscript.svelte

When delegate reads a settlement, artifactRefs() scans the child’s transcript for both saved artifact kinds in publication order. This also happens when the settlement says the child ended without an answer. The parent model gets IDs and titles alongside the answer or error, and the browser gets references in delegation.artifacts. The handback leaves the HTML and file bytes in the child’s saved entries. Each card uses the publishing conversation’s ID for its preview or download request. The data model follows those references back to storage. delegate.ts web-artifacts.ts view.ts WebArtifact.svelte

Where does a published file or preview come from when you reopen the chat? Files in, artifacts out follows artifact delivery from disk to the browser.

Recap

  • The parent model writes the assignment. The child receives it under its own profile and inherits the parent’s workspace binding.
  • A durable tool task owns the child and submits under a stable request ID. That task can recover a saved settlement or park until it arrives.
  • Foreground specialists share the parent’s clock and have their own tool-round limit. Their transcripts, approvals, and published files are reachable from the parent.
Check yourself
  1. Chat sends Developer to investigate a bug. That Developer profile lists Code reviewer. Who can ask for a separate review now?
    • The delegated Developer can start its own child
    • The top-level Chat can call Code reviewer after Developer returns
    • The reviewer can inspect the child automatically
    Answer and explanation

    The top-level Chat can call Code reviewer after Developer returns

    The delegated conversation has an owner and cannot delegate. Chat can make a later call to another specialist listed in its profile.

  2. Pipal dies just after the child submission commits and before the delegate task parks. The child has yet to settle. How does the replayed task avoid duplicating work?
    • It waits for the browser to resend the task
    • It asks the model to generate the same text again
    • It finds the child by owner task ID and uses the same submission request ID
    Answer and explanation

    It finds the child by owner task ID and uses the same submission request ID

    Ownership recovers the conversation and the stable request ID recovers the submission. These cover separate commits.

  3. Developer publishes a patch, then reaches the hard turn limit without an answer. What can the parent get back when the delegate call handles that settlement?
    • A download card with the patch, alongside the failed specialist result
    • The entire patch pasted into the parent model’s tool result
    • A Resume response button for the specialist
    Answer and explanation

    A download card with the patch, alongside the failed specialist result

    The delegate handler collects the child’s published artifact references even for a settlement without an answer. The file was committed before the child hit the limit.

  1. Chat sends Developer to investigate a bug. That Developer profile lists Code reviewer. Who can ask for a separate review now?

  2. Pipal dies just after the child submission commits and before the delegate task parks. The child has yet to settle. How does the replayed task avoid duplicating work?

  3. Developer publishes a patch, then reaches the hard turn limit without an answer. What can the parent get back when the delegate call handles that settlement?

In the source

Book contents

Search the book