Skip to main content

Opinionated Python client for gfa (Git for Agents) — smart routing, session cache, profile-aware hints.

Project description

gfa-sdk — Python SDK for gfa (Git for Agents)

Opinionated Python client for gfa. Picks the right endpoint for the access pattern, caches what is safe to cache, and emits hints when a strategy change would help. Designed for AI agents reading, mutating, and forking gfa-hosted repos.

Install

pip install gfa-sdk

Python >= 3.10 required.

Quickstart

import os
import gfa

client = gfa.Client(
    endpoint=os.environ["GFA_ENDPOINT"],
    token=os.environ["GFA_JWT"],
)

# Profile a repo (cheap, cached for the session)
profile = client.profile_repo("myrepo")
print(f"{profile.file_count_at_head} files, {profile.storage_bytes} bytes")

# Read one file
src = client.read_file("myrepo", "src/main.go")

# Read many files — SDK picks /file vs /files/batch vs partial-clone
files = client.read_files("myrepo", ["a.go", "b.go", "c.go"])

# Create a commit
sha = client.create_commit(
    "myrepo",
    branch="main",
    message="agent: refactor",
    files=[gfa.FileChange(path="a.go", content=b"// rewritten\n")],
)

# Workspace for isolated changes
with client.create_workspace("myrepo", base_ref="main", ttl_hours=2) as ws:
    ws.create_commit(branch="main", message="WIP", files=[...])
    ws.merge()

Smart routing

client.read_files(repo, paths) auto-routes by len(paths):

Count Surface
1 GET /file
2–200 POST /files/batch
> 200 (default) SuggestPartialCloneError — opt into auto_clone=True for automatic partial clone

Threshold tuning:

from gfa import Client, ClientConfig
client = Client(
    endpoint,
    token,
    config=ClientConfig(batch_read_threshold=500),
)

Hints

The SDK emits structured hints when access patterns suggest a strategy change. The default handler writes to stderr; override with a logging hook:

def my_handler(hint):
    logger.info("gfa hint", extra={"code": hint.code, "repo": hint.repo})

client = gfa.Client(endpoint, token, hint_handler=my_handler)

Silence entirely:

client = gfa.Client(endpoint, token, hint_handler=lambda _: None)

Auth

Either a pre-minted JWT string, or a TokenProvider that mints per-call:

provider = gfa.FileKeyTokenProvider("/path/to/preview-jwt-priv.pem", ttl_hours=1)
client = gfa.Client(endpoint, provider)

FileKeyTokenProvider mints ES256 JWTs with sub=<repo>, iat=now, exp=now + ttl_hours. Mirrors the Go tools/mint-token binary.

Diagnostics

client.read_file("myrepo", "x.go")
client.read_files("myrepo", ["a.go", "b.go"])
print(client.stats)
# ClientStats(request_count=2, cache_hit_rate=0.0, ...)

Partial clone

with client.partial_clone("myrepo", filter="blob:none", depth=1) as repo:
    content = repo.read("src/foo.go")

Requires git binary on PATH.

Troubleshooting

Symptom Likely cause
UnauthorizedError Token missing, malformed, or expired
ForbiddenError JWT sub does not match the repo being accessed
SuggestPartialCloneError Asked for too many files; pass auto_clone=True or use partial_clone
GitBinaryMissingError partial_clone needs git on PATH
ConflictError on merge Workspace and source diverged — inspect via conflict_surface()

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

gfa_sdk-0.1.1.tar.gz (42.8 kB view details)

Uploaded Source

Built Distribution

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

gfa_sdk-0.1.1-py3-none-any.whl (33.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gfa_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 42.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for gfa_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 59805a82266357d30f75f48a12de89d4cc26d23ec9ee6fe1e8aaff27a3f41b76
MD5 e99a2b074837c2386bb22e216e027136
BLAKE2b-256 dc9643b02c63948b4e415a0fc0401eaf45e19260560761b1b1c0c171e13178f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gfa_sdk-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 33.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for gfa_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fee629dd86d57d6bda7fdffafea133860d5cfecce7baff2927bb1ee5345572f6
MD5 0fe9d3edc6b61e5fde3272de5a5c1595
BLAKE2b-256 14cc25d774acf045f44a3386793619f44a18a222c927062b42630238a46fdd9e

See more details on using hashes here.

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