Implementation:23blocks OS Ai maestro Commit Index
| Knowledge Sources | |
|---|---|
| Domains | Persistence, Git_Introspection |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete tool for incrementally building and persisting a per-agent commit/PR index from Claude Code transcripts, resolving shas via git, provided by AI Maestro's `lib/commit-index.ts`.
Description
The commit-index module is the storage half of the commit-to-session trace. It maintains one JSON index per agent at `~/.aimaestro/agents/<agentId>/commits.json`, built from that agent's Claude Code transcripts and refreshed incrementally: each transcript is read only from where the last read stopped (tracked via `FileState` with size/offset). Git is used to turn a printed or missing sha into the full sha — `git rev-parse` for an abbreviation, `git log` in a time window (matching the commit message first line) when `git commit -q` printed nothing. That works while the repository still exists on this host; a removed worktree's commits live on in the main repository, so the agent's own working directory is tried too. `runGit` is argv-only (never a shell string), with a 3s timeout and `GIT_TERMINAL_PROMPT=0`. The index is saved atomically (temp + rename) and capped at 5000 commits / 2000 PRs. Only Claude Code transcripts are read; other agent kinds' commits are not indexed yet.
Usage
Call `loadIndex(agentId)` to read the current index (or an empty one), `rebuild(agentId)` / `refresh` to incrementally scan transcripts and resolve shas, and read `index.commits` / `index.prs` for the UI. Use `runGit` as a safe git runner for custom queries.
Code Reference
Source Location
- Repository: ai-maestro
- File: lib/commit-index.ts
- Lines: L1-L328
Signature
export const INDEX_VERSION = 1
export interface FileState {
size: number; offset: number; sessionId: string; lines: number
open: Record<string, PendingCall>
}
export interface AgentCommitIndex {
version: number; agentId: string; updatedAt: string
files: Record<string, FileState>
commits: CommitRecord[]
prs: PrRecord[]
}
export type GitRunner = (dir: string, args: string[]) => Promise<string | null>
export const runGit: GitRunner
export function indexFilePath(agentId: string): string
export function emptyIndex(agentId: string): AgentCommitIndex
export function loadIndex(agentId: string): AgentCommitIndex
Import
import { loadIndex, runGit, type AgentCommitIndex } from '@/lib/commit-index'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| agentId | string | Yes | Safe agent ID (validated) |
| dir | string | Yes (runGit) | Absolute path to a git repository |
| args | string[] | Yes (runGit) | git subcommand arguments (argv, never shell) |
Outputs
| Name | Type | Description |
|---|---|---|
| loadIndex() returns | AgentCommitIndex | Parsed index or emptyIndex on error |
| runGit() returns | null> | git stdout, or null on any failure |
| indexFilePath() returns | string | Path to the agent's commits.json |
Usage Examples
Read an agent's commits
import { loadIndex } from '@/lib/commit-index'
const index = loadIndex(agentId)
console.log(`${index.commits.length} commits across ${Object.keys(index.files).length} transcripts`)
Safe git query
import { runGit } from '@/lib/commit-index'
// Resolve an abbreviated sha to a full sha (argv only, never shell)
const fullSha = await runGit(repoDir, ['rev-parse', shortSha])