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-linkrecord is not written for every pull request.** A PR opened bygh pr createinside 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 thepr-linkrecords.Many sessions mention a PR; one made it. Each reference carries a
role:created(the URL printed by the very command that rangh pr create, quoted text and heredocs ignored),linked(Claude Code’spr-linkrecord, which it writes for any session that works on a PR, a merge included, so it is not evidence of making it),pushed/branchedfor branches, andseenfor anything else.who_made()rankscreatedfirst and then the earliest sighting, whatever itsrole, 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
Lower ranks first. |
Functions
|
Every PR and branch the transcript refers to, first sighting each, by strongest role. |
|
|
|
The store key for one session's refs. |
|
The default store: |
|
Add one session's refs to the index; returns whether the stored document changed. |
|
The sessions that referred to a PR (URL or |
Classes
|
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.
createdis the strongest evidence a session made the thing.
- class openloops.provenance.Ref(kind, ref, role, at='', cwd='', origin='')[source]#
Bases:
objectOne thing a session referred to: a pull request or a branch, and how.
refis normalized:owner/repo#N(lower-cased) for a PR, the bare name for a branch.atis the first time the session referred to it,cwdwhere it was.- origin: str = ''#
a live hook writes e.g.
hook:<account>@<host>here. Passed through bywho_made().- Type:
Who recorded it, when not
ol syncreading 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.
- openloops.provenance.normalize_pr(text)[source]#
owner/repo#Nfrom 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:
- openloops.provenance.provenance_key(source, session)[source]#
The store key for one session’s refs.
- Return type:
- openloops.provenance.provenance_store(rootdir=None)[source]#
The default store:
{source}/{session}.jsonfiles under the data root.Any
MutableMapping[str, str]works in its place (adictin tests).- Return type:
- 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:
- 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:
createdrows first, then every other row by its first sighting, earliest first. The first row is the likeliest maker; a row other thancreatedat 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).