openloops.provenance#

Which session made this pull request or branch? A reverse index, kept by ol sync.

A digest keeps a dozen pull-request links per session, because a digest is meant to be read. Tracing a pull request back to the session that opened it needs the opposite: every link and every pushed branch, none dropped, and a lookup that reads no transcript. This module is that index.

Three facts about real transcripts shape it.

  • **Claude Code’s own pr-link record is not written for every pull request.** A PR opened by gh pr create inside a script, a loop or a background shell reaches the transcript only as text in a tool result, sometimes minutes later, when the session reads the script’s log. So every GitHub pull-request URL in a tool result or in the assistant’s text is collected, not only the pr-link records.

  • Many sessions mention a PR; one made it. Each reference carries a role: created (the URL printed by the very command that ran gh pr create, quoted text and heredocs ignored), linked (Claude Code’s pr-link record, which it writes for any session that works on a PR, a merge included, so it is not evidence of making it), pushed / branched for branches, and seen for anything else. who_made() ranks created first and then the earliest sighting, whatever its

    role, which for a PR opened by a script is the session that ran the script.

  • The public-repo trailer is stripped. Claude-Session: is removed from commits and PR bodies on public repositories, so the transcript is the only link left.

The store is a MutableMapping[str, str] of JSON documents keyed {source}/{session}.json (one per session, derived and rebuildable, like a digest), so several machines write into one place without colliding and a lookup is a scan of small files.

>>> recs = [
...     {"type": "assistant", "timestamp": "T1", "cwd": "/w/r", "message": {"content": [
...         {"type": "tool_use", "id": "u1", "name": "Bash", "input": {"command":
...          "git push -u origin fix-x && gh pr create --head fix-x --title t"}}]}},
...     {"type": "user", "timestamp": "T2", "message": {"content": [
...         {"type": "tool_result", "tool_use_id": "u1",
...          "content": "https://github.com/o/r/pull/7"}]}},
... ]
>>> [(r.kind, r.ref, r.role) for r in extract_refs(recs)]
[('branch', 'fix-x', 'pushed'), ('pr', 'o/r#7', 'created')]
>>> store = {}
>>> record_refs(store, 'mac', 's1', extract_refs(recs))
True
>>> [(h['session'], h['role']) for h in who_made('https://github.com/o/r/pull/7', store=store)]
[('s1', 'created')]
>>> [(h['session'], h['role']) for h in who_made('fix-x', store=store)]
[('s1', 'pushed')]

A command that only searches for the words does not make a PR:

>>> recs[0]["message"]["content"][0]["input"]["command"] = 'rg -l "gh pr create" .'
>>> [(r.kind, r.ref, r.role) for r in extract_refs(recs)]
[('pr', 'o/r#7', 'seen')]

Module Attributes

ROLE_RANK

Lower ranks first.

Functions

extract_refs(records)

Every PR and branch the transcript refers to, first sighting each, by strongest role.

normalize_pr(text)

owner/repo#N from a PR URL or a short reference, lower-cased; else "".

provenance_key(source, session)

The store key for one session's refs.

provenance_store([rootdir])

The default store: {source}/{session}.json files under the data root.

record_refs(store, source, session, refs)

Add one session's refs to the index; returns whether the stored document changed.

who_made(target, *[, store, source, limit])

The sessions that referred to a PR (URL or owner/repo#N) or a branch name.

Classes

Ref(kind, ref, role[, at, cwd, origin])

One thing a session referred to: a pull request or a branch, and how.

openloops.provenance.ROLE_RANK = {'branched': 2, 'created': 0, 'linked': 3, 'on': 5, 'pushed': 1, 'seen': 4}#

Lower ranks first. created is the strongest evidence a session made the thing.

class openloops.provenance.Ref(kind, ref, role, at='', cwd='', origin='')[source]#

Bases: object

One thing a session referred to: a pull request or a branch, and how.

ref is normalized: owner/repo#N (lower-cased) for a PR, the bare name for a branch. at is the first time the session referred to it, cwd where it was.

as_dict()[source]#

JSON-ready form.

Return type:

dict[str, str]

origin: str = ''#

a live hook writes e.g. hook:<account>@<host> here. Passed through by who_made().

Type:

Who recorded it, when not ol sync reading a transcript

openloops.provenance.extract_refs(records)[source]#

Every PR and branch the transcript refers to, first sighting each, by strongest role.

Pure: the same records give the same refs. Order is by first sighting.

Return type:

tuple[Ref, ...]

openloops.provenance.normalize_pr(text)[source]#

owner/repo#N from a PR URL or a short reference, lower-cased; else "".

>>> normalize_pr('https://github.com/Addaix/aix/pull/70/files')
'addaix/aix#70'
>>> normalize_pr('addaix/aix#70'), normalize_pr('fix-x')
('addaix/aix#70', '')
Return type:

str

openloops.provenance.provenance_key(source, session)[source]#

The store key for one session’s refs.

Return type:

str

openloops.provenance.provenance_store(rootdir=None)[source]#

The default store: {source}/{session}.json files under the data root.

Any MutableMapping[str, str] works in its place (a dict in tests).

Return type:

MutableMapping[str, str]

openloops.provenance.record_refs(store, source, session, refs)[source]#

Add one session’s refs to the index; returns whether the stored document changed.

Merges with what is stored rather than replacing it, so a ref recorded by another writer (a live hook) or read from an earlier, longer copy of the transcript is never lost, and an empty read never erases a good one.

>>> store = {}
>>> record_refs(store, 'm', 's', [Ref('pr', 'o/r#1', 'created', 'T2', origin='hook')])
True
>>> record_refs(store, 'm', 's', [Ref('pr', 'o/r#1', 'seen', 'T1')])
True
>>> record_refs(store, 'm', 's', [])
False
>>> json.loads(store['m/s.json'])['refs'][0]['role'], json.loads(store['m/s.json'])['refs'][0]['at']
('created', 'T1')
Return type:

bool

openloops.provenance.who_made(target, *, store=None, source=None, limit=10)[source]#

The sessions that referred to a PR (URL or owner/repo#N) or a branch name.

Ranked: created rows first, then every other row by its first sighting, earliest first. The first row is the likeliest maker; a row other than created at the top means no session recorded making it, and the earliest sighting is the best guess (a PR opened by a script is first seen when its log is read; later sessions that merge or fix it come after).

Return type:

list[dict[str, str]]