Loops¶
Declare plain shell or Python scripts as loops in bf.yaml to keep tool feeds fresh and run continuous, always-on agents. Every loop communicates over standard UNIX streams with per-loop secret allowlists and zero built-in LLMs.
1. Write your first sensor¶
A sensor is any script that prints JSON Lines records to stdout and logs to stderr. Here is a minimal sensor in sensors/github-issues.py:
#!/usr/bin/env python3
"""Emit JSONL issue records to stdout and status logs to stderr."""
import json
import os
import sys
repo = sys.argv[1]
since = os.environ["BF_START"] # Window start managed automatically by bf
token = os.environ["GH_TOKEN"] # Forwarded only because env_from allows it
# Fetch from your CLI or API, then write one JSON record per line to stdout:
record = {
"id": "42",
"title": "Add pricing table to product page",
"type": "issue",
"text": f"Opened in {repo} since {since}",
"created": "2026-11-20T10:00:00Z",
}
print(json.dumps(record))
print(json.dumps({"level": "info", "message": "collected", "count": 1}), file=sys.stderr)
Declare the sensor in bf.yaml:
# yaml-language-server: $schema=https://fmind.github.io/brain-framework/bf.schema.json
version: 20
name: my-brain
loops:
github-issues:
description: Collect GitHub issues incrementally into memories/github-issues.jsonl.
run: [python3, sensors/github-issues.py, owner/repo]
env_from: [GH_TOKEN]
ingest: upsert
when: 30m
Preview, run once, and inspect health from the CLI (export GH_TOKEN or place it in a 0600 .env file first):
# Preview the resolved command, allowed env vars, and time window safely
bf run github-issues --dry-run
# Run once now and verify health
bf run github-issues
bf status --format text
Standard UNIX streams
Every loop uses the same contract: matched events arrive on stdin, JSONL records (ingest:) or events (emit:) go to stdout, and stderr is redacted and saved to logs/<loop>.jsonl.
- Ingestion modes (
ingest:): Useupsertto merge records byid, orsnapshotto replace the full source set on each run (protected against accidental mass removals by--allow-removaland.forgotten). - Time windows (
window:):bfpassesBF_STARTandBF_ENDon every cycle.window.backfill(default1d) sets the first-run lookback, andwindow.overlap(default0s) rewinds later runs slightly to catch late-arriving edits.
A complete runnable offline brain with sensors, routines, event-triggered agents, and schemas lives in examples/brain/.
2. Choose how loops trigger¶
Every loop runs in either mode: cycle (default; manual, scheduled, or event-triggered) or mode: daemon (long-running listener supervised continuously by bf watch).
Trigger in when: (cycle loops) |
Behavior |
|---|---|
Omit when: |
Manual only via bf run <loop> |
30m, @daily, "30 7 * * 1" |
Interval or 5-field cron schedule |
{every: 1d, backfill: 7d, overlap: 1h} |
Schedule with per-trigger time-window overrides (cron: works too) |
{event: "records.ingested", debounce: 10s} |
Runs after matching events, coalescing bursts with optional debounce: |
{event: {kind: ..., source: ..., where: {...}}} |
Matches events using declarative dot-path predicates |
3. React to events and run always-on agents¶
Combine small, focused loops in bf.yaml as your needs grow.
React to newly ingested records¶
loops:
verify:
description: Validate notes and links after new records arrive.
when:
event: records.ingested
debounce: 10s
run: [bf, check, --only, notes, --strict]
Combine a live daemon with an always-on agent¶
Passive mode: daemon listeners stay running cheaply in plain code, while agents wake on a schedule or matching event, run your preferred coding harness, and write outcomes back to the brain. Combine both in bf.yaml so the same agent runs scheduled checks and replies immediately when live messages or delayed wakeups arrive:
concurrency:
default: 4
interactive: 2
loops:
chat:
description: Stream incoming chat messages as events.
mode: daemon
run: [python3, routines/chat.py, listen]
emit: loop.message
triage:
description: Run hourly checks, Monday briefs, and direct-message replies.
run: [python3, agents/triage.py]
pool: interactive
timeout: 15m
env_from: [ANTHROPIC_API_KEY]
when:
- every: 1h
- cron: "0 8 * * 1"
- event:
kind: loop.message
source: chat
where:
data.direct: true
- event:
kind: loop.wake
where:
data.target: triage
emit: loop.event
4. Coordinate pools, timers, and supporting files¶
- Zero hidden magic: You can omit
run:only for zero-argument scripts at<dir>/<name>(or.sh/.py) undersensors/,routines/, oragents/. Always declareingest:explicitly on loops that write records tomemories/. - Concurrency pools: Map
concurrency:to named pools and assign slow agent tasks withpool:so long runs never starve quick sensors or interactive replies. Event triggers always dispatch ahead of schedules. - Delayed wakeups: Any loop or script can schedule a future wakeup with
bf emit loop.wake --in 15m --data '{"id":"wake-triage","target":"triage"}' --dedupeand inspect pending timers withbf events --timers --format text. - Supporting folders: Keep reusable API clients in
clients/and prompt templates inprompts/.
Next, run loops continuously in Operations, review trust boundaries in Security, or look up exact BF_* variables and predicate operators in Reference.