Observe a task
Build a small task observation loop and inspect its result.
Build a small intake loop: when an open task gains the intake tag, record
that it was seen. The record stays on the task for a person to inspect. This
example does not launch an agent or do the task’s work.
Complete installation first. This example uses the current source checkout. Use a scratch project so that its tasks do not mix with an existing workspace’s. It needs no agent credentials.
Write the observer
Create .foundry/marbles in the project and save this complete file as
.foundry/marbles/task-loop.ts:
import { observe, tasks } from "@foundry.sh/marbles";
observe(tasks, async function recordIntake(event) {
for (const change of event.tasks) {
const entered =
change.after?.tags.includes("intake") &&
!change.before?.tags.includes("intake");
if (!entered) {
continue;
}
const current = await tasks.get(change.id);
if (
current?.status !== "open" ||
!current.tags.includes("intake") ||
current.metadata.intakeRecorded === true
) {
continue;
}
await tasks.update(
current.id,
{ metadata: { intakeRecorded: true } },
{ expectedUpdatedAt: current.updatedAt }
);
}
});
observe registers a handler for committed task changes, as the observer
recordIntake, its function’s name. A change contains the task before and
after the write. The handler looks for the transition into
the intake tag, then reads the current task: the task may have changed since
the event was recorded. It only marks tasks that are still open and tagged.
The update sets the intakeRecorded key and keeps the task’s other metadata.
It uses updatedAt as an optimistic token. If another write wins first, this update fails instead of overwriting
it. The host tries the event again after a short backoff. The marker makes
repeated delivery harmless while it remains on the task. The observer’s own
update emits another event, but that event does not add the intake tag, so
it does not trigger another update.
Run it
From the scratch project, start the Marbles process and create a tagged task:
marbles run </dev/null
In another terminal in the same project:
marbles tasks create "Review the release notes" --tag intake
marbles tasks list --json
The observer runs asynchronously. Once it has processed the change, the task
has "intakeRecorded": true in its metadata. List again if the first read
arrives before the observer. If a task command says the workspace is
starting in the Marbles process, retry once it is ready. Stop the process with marbles stop when done.
Tasks and their events live in workspace state; the source file stays in the
project. See the state reference.
Grow the loop
This is an intake marker, not a worker: it does not claim a task, check its
dependencies, or complete it. A worker can use ready() and an atomic
requireReady transition before starting work, then leave the result for a
person to review. For external effects, a metadata marker alone is not a
transaction with the external system; use an idempotency strategy for that
system too.
The factory floor describes how several such workflows can cooperate.
Observers receive changes at least once, in order, from their first start.
An observer is known by its name, so editing the handler keeps its place and
renaming it starts a new observer from then. marbles status shows where
each observer is and whether it is failing. See
observation for delivery and storage
details.