Skip to main content

Python library for the Brainfile task management protocol

Project description

Brainfile Logo

brainfile

The Python engine behind Brainfile.

This library provides the core logic for managing Brainfile v2 projects: reading/writing task files, managing contracts, validating boards, and querying task state. It is the Python equivalent of @brainfile/core with full API parity.

Used by Cecli and other tools that need programmatic access to Brainfile workspaces.

Installation

pip install brainfile

Or with uv:

uv add brainfile

v2 Architecture

Brainfile v2 uses a directory-based structure. Each task is its own markdown file.

.brainfile/
├── brainfile.md          # Board config (columns, types, rules)
├── board/                # Active tasks
│   ├── task-1.md
│   └── task-2.md
└── logs/                 # Completed tasks (history)
    └── task-0.md

Quick Start

from brainfile import ensureV2Dirs, addTaskFile, readTasksDir, completeTaskFile

# Initialize workspace
dirs = ensureV2Dirs(".brainfile/brainfile.md")

# Add a task
result = addTaskFile(
    dirs.boardDir,
    {"title": "Implement auth", "column": "in-progress", "priority": "high"},
    body="## Description\nAdd JWT authentication to the API.\n",
)
print(result["task"].id)  # "task-1"

# List all active tasks
for doc in readTasksDir(dirs.boardDir):
    t = doc.task
    print(f"{t.id}: {t.title} [{t.column}]")

# Complete a task (moves to logs/)
completeTaskFile(result["filePath"], dirs.logsDir)

Task File Operations

Read and write individual task files.

from brainfile import readTaskFile, writeTaskFile, findV2Task, getV2Dirs

# Read a single task
doc = readTaskFile(".brainfile/board/task-1.md")
print(doc.task.title)
print(doc.body)  # Markdown content below frontmatter

# Find a task across board and logs
dirs = getV2Dirs(".brainfile/brainfile.md")
result = findV2Task(dirs, "task-1", searchLogs=True)
if result:
    print(result["doc"].task.title, "in", "logs" if result["isLog"] else "board")

Contracts

Tasks can carry formal contracts for AI agent coordination: deliverables, validation commands, constraints, and feedback for rework.

from brainfile import readTaskFile, writeTaskFile, Contract, Deliverable

doc = readTaskFile(".brainfile/board/task-1.md")
task = doc.task

# Attach a contract
task.contract = Contract(
    status="ready",
    deliverables=[
        Deliverable(path="src/auth.py", description="JWT auth module"),
        Deliverable(path="tests/test_auth.py", description="Unit tests"),
    ],
    validation={"commands": ["pytest tests/test_auth.py"]},
    constraints=["Use PyJWT library", "Token expiry must be configurable"],
)

writeTaskFile(".brainfile/board/task-1.md", task, doc.body)

Contract Lifecycle

ready  →  in_progress  →  delivered  →  done
                │                         │
                └─────────→  failed  ←────┘
                             (add feedback, reset to ready)
from brainfile import setTaskContractStatus

# Agent picks up work
setTaskContractStatus(board, "task-1", "in_progress")

# Agent delivers
setTaskContractStatus(board, "task-1", "delivered")

# PM validates
setTaskContractStatus(board, "task-1", "done")

Board Operations (V1)

Immutable operations on in-memory board objects. Useful for single-file workflows or building custom tools.

from brainfile import Brainfile, add_task, move_task, patch_task, TaskInput, TaskPatch

# Parse a brainfile
result = Brainfile.parse(markdown_content)
board = result.board

# Add a task
result = add_task(board, "todo", TaskInput(title="New task", priority="high"))
board = result.board

# Move between columns
result = move_task(board, "task-1", "todo", "in-progress")

# Patch fields
result = patch_task(board, "task-1", TaskPatch(tags=["urgent"], assignee="codex"))

# Serialize back
output = Brainfile.serialize(board)

Queries

from brainfile import (
    find_task_by_id,
    get_all_tasks,
    get_tasks_by_tag,
    get_tasks_by_assignee,
    search_tasks,
)

task_info = find_task_by_id(board, "task-1")
urgent = get_tasks_by_tag(board, "urgent")
results = search_tasks(board, "auth")

Validation and Linting

from brainfile import BrainfileValidator, BrainfileLinter, LintOptions

# Validate board structure
result = BrainfileValidator.validate(board)
for error in result.errors:
    print(f"{error.path}: {error.message}")

# Lint with auto-fix
result = BrainfileLinter.lint(content, LintOptions(auto_fix=True))

File Discovery

from brainfile import discover, find_nearest_brainfile, isV2

# Find brainfiles in a project
result = discover("/path/to/project")
for f in result.files:
    print(f"{f.name}: {f.item_count} tasks")

# Walk up to find nearest brainfile
path = find_nearest_brainfile()

# Check if a workspace is v2
if isV2(".brainfile/brainfile.md"):
    print("V2 workspace detected")

Ecosystem

Package Description
brainfile (Python) This library
@brainfile/core TypeScript core library
@brainfile/cli CLI with TUI and MCP server
Protocol Specification and JSON Schema

Development

git clone https://github.com/brainfile/py.git
cd py
uv sync --dev
uv run pytest

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brainfile-0.1.1.tar.gz (113.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

brainfile-0.1.1-py3-none-any.whl (60.4 kB view details)

Uploaded Python 3

File details

Details for the file brainfile-0.1.1.tar.gz.

File metadata

  • Download URL: brainfile-0.1.1.tar.gz
  • Upload date:
  • Size: 113.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for brainfile-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d48bbfd30d1c78f2041a6b9702a7c422fe371cb1dc6c0a99d0623542468a818d
MD5 f2fd698c6e9ccd76dfbe746f751346d2
BLAKE2b-256 de23ac7ffa45b5326dec4194a0292bb597983199c65d22a2f34cfa4dec9092e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for brainfile-0.1.1.tar.gz:

Publisher: publish.yml on brainfile/py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file brainfile-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: brainfile-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 60.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for brainfile-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a532b6c50fb7e142584e48eeadbaebeabdbdb8ccb2ba34dcfab32f13d93e674e
MD5 04e0e055bbe3d17c4327cd7c64bd540d
BLAKE2b-256 2aa4355728b400e96a7e5a0ebf025343e0f26fefe910ac9d69af8c5e61542dfe

See more details on using hashes here.

Provenance

The following attestation bundles were made for brainfile-0.1.1-py3-none-any.whl:

Publisher: publish.yml on brainfile/py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page