> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Queue, schedules, and tasks

> Use SessionRpc in Laravel Copilot SDK to manage the queue, schedules, and background tasks.

## Queue, schedules, and tasks

Scope: SessionRpc (per-session operations tied to a Session)

For an overview of RPC and the list of all RPC pages, see [RPC](https://kawax.biz/en/packages/laravel-copilot-sdk/rpc.md).

```php theme={null}
// queue: view and manage queued user-facing items
$pending = $session->rpc()->queue()->pendingItems();
// $pending->items - Array of QueuePendingItems
// $pending->items[0]->kind - QueuePendingItemsKind (Command / Message)

$removed = $session->rpc()->queue()->removeMostRecent();
// $removed->removed - Whether the most recent item was removed

$session->rpc()->queue()->clear();

// Insert an item at a given position (position is 0-based)
use Revolution\Copilot\Types\Rpc\QueueInsertAtRequest;
use Revolution\Copilot\Types\Rpc\QueueInsertMessage;

$queue = $session->rpc()->queue();
$inserted = $queue->insertAt(new QueueInsertAtRequest(
    position: 0,
    message: new QueueInsertMessage(prompt: 'Review the changes'),
));
// $inserted->id - ID of the inserted item

$queue->moveItem(['id' => $inserted->id, 'toPosition' => 1]);
$duplicated = $queue->duplicateAt(['id' => $inserted->id]);
$queue->updateText(['id' => $duplicated->id, 'prompt' => 'Review and test the changes']);
$removedAt = $queue->removeAt(['id' => $duplicated->id]);
// $removedAt->removed - Whether the item was removed

$sent = $queue->sendNow(['id' => $inserted->id]);
// $sent->steered - Whether it was sent to the running turn

// Pause draining. The pause isn't lifted automatically, so always resume with false
$queue->setDrainPaused(['paused' => true]);
$queue->setDrainPaused(['paused' => false]);

// schedule (experimental: scheduled prompt management)
$schedule = $session->rpc()->schedule()->list();
// $schedule->entries - Array of ScheduleEntry

use Revolution\Copilot\Types\Rpc\ScheduleStopRequest;

$stopped = $session->rpc()->schedule()->stop(new ScheduleStopRequest(id: 1));
// $stopped->stopped - Whether stopping succeeded

// tasks (experimental: background task management)
// Start an agent task and get its ID
use Revolution\Copilot\Types\Rpc\TasksStartAgentRequest;

$result = $session->rpc()->tasks()->startAgent(new TasksStartAgentRequest(
    agentType: 'explore',
    prompt: 'Search for all TODO comments in the codebase',
    name: 'todo-search',
    description: 'Find TODO items', // Optional
    model: 'gpt-4o', // Optional
));
// $result->agentId - ID of the background task

// Get the current task list
$list = $session->rpc()->tasks()->list();
// $list->tasks - Array of TaskAgentInfo, TaskShellInfo, and TaskClientInfo

// Incrementally read output from a detached shell with a cursor
use Revolution\Copilot\Types\Rpc\TasksReadOutputRequest;

$cursor = null;
do {
    $output = $session->rpc()->tasks()->readOutput(new TasksReadOutputRequest(
        id: 'shell-1',
        cursor: $cursor,
        waitMs: 1000,
    ));
    echo $output->text;
    $cursor = $output->cursor;
} while ($output->exit === null);
// $output->exit->status - Task exit status
// $output->exit->exitCode - Process exit code (nullable)

// Each read is up to 64 KiB. waitMs defaults to 0, with a maximum of 30000ms.
// stdout and stderr are merged. If the cursor becomes invalid, read again without a cursor.

// Register an application-owned job as a task
use Revolution\Copilot\Types\Rpc\TasksRegisterRequest;

$registered = $session->rpc()->tasks()->register(new TasksRegisterRequest(
    clientTaskId: 'import-123',
    description: 'Import customer data',
    cancellable: true,
    displayName: 'Customer import', // Optional
));
// $registered->task->id - Task ID assigned by the CLI
// $registered->created / $registered->reclaimed - Whether it was newly created or an orphaned task was reclaimed

// Update the progress of a client-owned task
use Revolution\Copilot\Enums\TaskClientActiveStatus;
use Revolution\Copilot\Enums\TaskClientUpdateKind;
use Revolution\Copilot\Types\Rpc\TaskClientUpdate;
use Revolution\Copilot\Types\Rpc\TasksUpdateRequest;

$updated = $session->rpc()->tasks()->update(new TasksUpdateRequest(
    id: $registered->task->id,
    sequence: 1,
    update: new TaskClientUpdate(
        kind: TaskClientUpdateKind::Progress,
        status: TaskClientActiveStatus::Running,
        phase: 'importing',
        percentage: 25,
    ),
));
// Increment sequence with each update

// Report completion
$completed = $session->rpc()->tasks()->update(new TasksUpdateRequest(
    id: $registered->task->id,
    sequence: 2,
    update: new TaskClientUpdate(
        kind: TaskClientUpdateKind::Completed,
        result: ['imported' => 100],
    ),
));

// Get task progress (null if not tracked)
$progress = $session->rpc()->tasks()->getProgress(['id' => $registered->task->id])->progress;
// TaskAgentProgress, TaskClientProgress, TaskShellProgress, or null

// Refresh the state of detached shells and wait for background work to finish
$session->rpc()->tasks()->refresh();
$session->rpc()->tasks()->waitForPending();

// Get the task that can currently be moved to the background, or move it to the background immediately
$promotable = $session->rpc()->tasks()->getCurrentPromotable()->task;
$promotedCurrent = $session->rpc()->tasks()->promoteCurrentToBackground()->task;

// Move a task to background mode (releases the synchronous wait)
use Revolution\Copilot\Types\Rpc\TasksPromoteToBackgroundRequest;

$promoted = $session->rpc()->tasks()->promoteToBackground(
    new TasksPromoteToBackgroundRequest(id: $result->agentId)
);
// $promoted->promoted - Whether the move succeeded

// Cancel a task
use Revolution\Copilot\Types\Rpc\TasksCancelRequest;

$cancelled = $session->rpc()->tasks()->cancel(
    new TasksCancelRequest(id: $result->agentId)
);
// $cancelled->cancelled - Whether the cancellation succeeded

// Remove a completed or cancelled task
use Revolution\Copilot\Types\Rpc\TasksRemoveRequest;

$removed = $session->rpc()->tasks()->remove(
    new TasksRemoveRequest(id: $result->agentId)
);
// $removed->removed - Whether the removal succeeded

// Send a message to a running agent task (steering)
use Revolution\Copilot\Types\Rpc\TasksSendMessageRequest;

$sent = $session->rpc()->tasks()->sendMessage(
    new TasksSendMessageRequest(id: $result->agentId, message: 'Please search other directories too')
);
// $sent->sent - Whether the message was sent successfully
// $sent->error - Error message if sending failed (nullable)

// Interrupt only the main turn and keep queued prompts for the next turn
use Revolution\Copilot\Types\Rpc\InterruptMainTurnRequest;

$interrupted = $session->rpc()->interruptMainTurn(
    new InterruptMainTurnRequest(flushQueued: true)
);
// $interrupted->interrupted - Whether the running main turn was interrupted

// Cancel all running background agents without interrupting the main turn
$cancelledCount = $session->rpc()->cancelAllBackgroundAgents();
// Number of cancelled agents
```


## Related topics

- [RPC (Remote Procedure Call)](https://kawax.biz/en/packages/laravel-copilot-sdk/rpc.md)
- [Bot tutorial - Laravel Bluesky](https://kawax.biz/en/packages/laravel-bluesky/bot-tutorial.md)
- [Queues](https://kawax.biz/en/queues.md)
- [Registering and maintaining scheduled tasks in packages](https://kawax.biz/en/advanced/package-scheduling.md)
- [Task scheduling](https://kawax.biz/en/scheduling.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.