The TypeScript entry points of the pr-shepherd npm package and what each method accepts and returns. MCP host setup: mcp.md.
| Entry point | Exports | I/O |
|---|---|---|
pr-shepherd |
createPrShepherd, PrShepherdValidationError, PartialApplyError |
GitHub, local git, configuration |
pr-shepherd/journal |
extractShepherdJournal, reconcileShepherdJournal, appendJournalItem, validateJournalItem |
None — pure; no GitHub, tokens, or configuration |
pr-shepherd/mcp |
createPrShepherdMcpServer, runPrShepherdMcpStdio |
Same as pr-shepherd |
pr-shepherd/classify |
ClassifyRule, ClassifyItem types |
None |
import { createPrShepherd } from "pr-shepherd";
const shepherd = createPrShepherd({ cwd: "/path/to/repo", transport: "auto" });
const tick = await shepherd.iterate({ pr: 42, merge: true }); // one tick, with nextCheck
const group = await shepherd.iterate({ prs: [42, 43] });
const verdict = await shepherd.status({ pr: 42, merge: true, readyDelaySeconds: 0 }); // read-only
const stack = await shepherd.stack({ pr: 43 });
const journal = await shepherd.getJournal({ pr: "owner/repo#42" });
const applied = await shepherd.apply({
pr: 42,
transport: "rest",
operations: [
{
type: "merge",
requireSha: "<40-char lowercase sha>",
mergeAction: "direct_merge",
mergeMethod: "squash",
},
{
type: "review_mutations",
replyThreadIds: ["PRRT_…"],
message: "Fixed the naming.",
requireSha: "HEAD", // this checkout's commit, or a 40-char lowercase sha
},
],
});
const patches = await shepherd.suggest({
pr: 42,
suggestions: [
{ threadId: "PRRT_…", message: "Apply reviewer suggestion" },
{ threadId: "PRRT_…", message: "Apply second suggestion" },
],
});createPrShepherd({ cwd, transport }) returns these methods:
| Method | Same as |
|---|---|
iterate(input?) |
MCP iterate / pr-shepherd iterate |
status(input?) |
None — library only, no CLI or MCP |
stack({ refs }) |
MCP stack / pr-shepherd stack --once |
apply({ pr, operations }) |
MCP apply |
getJournal({ pr }) |
MCP get_journal |
suggest({ pr, suggestions }) |
MCP suggest |
transport (auto, graphql, rest) matches the CLI --transport option and the github.transport key. Fallback triggers: github-api.md.
pris a positive number,owner/repo#N, or a GitHub pull-request URL.getJournalrequirespr; the other methods infer the current branch's PR when it is omitted.- A repository-qualified reference is authoritative for GitHub reads and mutations and may name a repository other than
cwd. cwdsupplies local git state, configuration, classification rules, and per-worktree debug logging.- With a repository-qualified
pr,cwdneed not be a checkout of that repository, or a git repository at all. Shepherd then reads configuration and classification rules fromcwdand uses the defaults wherecwdhas none, rather than the target repository's own config. Pass a checkout of the target ascwdwhen its config should apply.
Every call runs one tick. A singular call returns the raw IterateResult, which carries nextCheck { at, inSeconds, reason } when another tick is meaningful (cloud.md).
A non-empty prs set runs one compact summary tick and returns PollSummaryResult. Every reference must resolve to one repository. It keeps no ready-delay state and emits no rebase/push commands; its only GitHub mutations (with their seen markers) are rule-driven classification auto-resolve, reported per row as ruleAutoResolve. The caller owns recurrence.
Every IterateResult carries headSha, the PR head commit that tick evaluated (omitted only when GitHub did not report it). The CLI prints it only with --verbose. Terminal cancel results (merged, closed) carry it too. Aggregate PollSummaryResult rows already carry their head as headRefOid; no headSha is added there.
status({ pr?, merge?, readyDelaySeconds?, stallTimeoutSeconds?, transport?, instructions? }) answers "is this PR ready to merge at this exact head?" for an outside supervisor. It runs one single-PR iterate tick through the same code path as iterate and returns the same IterateResult, so its verdict cannot drift from iterate's; read action and headSha together. There is no prs form (it rejects one), and status is library only: there is no CLI command or MCP tool.
The contract is read-only:
- No GitHub mutations. The tick skips every mutation site: classification-rule auto-resolve/minimize (those IDs stay in the result for the caller, as when GitHub does not confirm a mutation), the in-process minimize of already-seen review summaries, and mark-ready. As a backstop, the GraphQL and REST request layers throw
ReadOnlyViolationErrorfor a mutation document or any non-GET/HEAD REST method; a normal tick never reaches it. - No local state writes. Nothing under the state directory is created, updated, or deleted: seen markers, fix attempts, stall state, ready receipts, ready-delay marker, fingerprints, REST ETag cache and snapshots, check blockers, follow-up and retrigger markers, quota claims and warnings, signature markers, queue-removal acknowledgments, uncertain-reply records, identity cache (identities a REST tick fetches are kept in memory for that process, so its later reads still resolve). The per-worktree debug log is skipped too. Deletes and clears count as writes, so a stale ready receipt, ready-delay marker, or stall state is never cleared, and a concurrent
pr-shepherd <pr> --until-terminalsession on the same PR is not disturbed. Reads still work: seen markers decide first-look items (a followingiteratestill shows them), and the ready-delay marker decides the countdown. - Ready delay. The marker is never created, refreshed, or deleted. When all four PR event times are known on the read (see
iterate.ready-delay-minutes), the countdown runs from the newest event, marker or not. Otherwise, with no usable marker (none, another head, or a future timestamp) the countdown has not started, so a ready PR reportsaction: "ready"withremainingSeconds = readyDelaySeconds, and with a markerstatusreports the remaining time it implies, counted from the later of the marker and the newest event time that is known.readyDelaySeconds: 0has elapsed at once, so a READY PR withmerge: truereturnsaction: "merge". A current ready receipt also counts as elapsed.statusnever starts or restarts a countdown; only the poll loop oriteratedoes. A countdown they already started keeps elapsing with wall-clock time, so repeatedstatuscalls report less time remaining. - Drafts. Marking a draft ready is a GitHub mutation, so a clean draft is treated as if automatic mark-ready were disabled:
statusreturnscancelwith reasonauto-mark-ready-disabledinstead ofmark_ready. - Lock.
statustakes no watch lock, like the libraryiterate; only the CLI poll loop acquires one (src/state/watch-lock.mts), so concurrent calls and a running watcher do not block each other. - Printed commands. Instructions and generated commands in the result are only printed, never run.
- Stricter than
iterate. Skipped mutations only hold a PR back. When its only remaining work is somethingiteratewould minimize itself (a rule-matching comment, an already-seen review summary),statusreturnstriagewhereiteratewould go on tomerge. It never reports ready whereiteratewould not. - Reading the verdict. With
merge: true,action === "merge"together with a matchingheadShameans Shepherd would merge that head now. A clean PR can still return another action:waitwhile it is already queued or set to auto-merge,cancel(auto-mark-ready-disabled) for a draft, andwaitwhen a fresh re-read regresses. Treat every action other thanmergeas not ready.
stack({ pr }) takes the anchor PR of a native GitHub stack plus the same tick options as iterate, runs one aggregate tick, and returns PollSummaryResult with the same guarantees and nextCheck rule as an aggregate iterate.
| Native-stack action | When |
|---|---|
SHEPHERD |
Some layer is unready and autonomous. Every layer with work gets a one-PR Shepherd instruction; a mixed state also surfaces any human blocker. |
WAIT |
The stack is queued, or its layers can only wait. An unchanged idle wait that reaches stallTimeoutSeconds returns ESCALATE with stall-timeout. |
ESCALATE |
A closed-unmerged layer needs human direction, and no one-PR Shepherd session remains. |
MERGE |
With merge: true: the highest ready prefix whose bottom open layer targets the stack base. Run the printed SHA-pinned pr-shepherd apply merge for that prefix, then recheck until every layer merges and CANCEL returns. |
CANCEL |
Terminal READY or merged state, including clean drafts left in draft when auto mark-ready is disabled (row reason draft-auto-mark-ready-disabled, with no pollCommand). |
apply validates every operation, then runs them in list order. A validation failure throws PrShepherdValidationError before any GitHub mutation. A failure after earlier operations succeeded throws PartialApplyError with failedIndex and completed.
| Operation | Fields | Notes |
|---|---|---|
merge |
requireSha (full lowercase SHA), mergeAction, optional mergeMethod, optional expectedStack |
mergeAction is direct_merge, merge_queue, or default; mergeMethod (merge, squash, rebase) only for direct_merge. Requires REST, from the client transport option or input.transport. |
review_mutations |
resolveThreadIds, replyThreadIds, minimizeCommentIds, dismissReviewIds, message, requireSha |
message is required for replies and dismissals. requireSha is HEAD (read with git rev-parse HEAD) or a full 40-character lowercase SHA; mutations wait until GitHub reports that PR head. |
mark_files_viewed |
files, tests, matchPatterns |
Surfaces GitHub's per-file results. REST reports it as explicitly unsupported. |
append_journal |
item, optional dryRun |
Honors explicit caller intent. |
check_blocker |
check, then either blockedBy or clear: true |
Records or clears a local blocker for one exact failing check, as apply check-blocker does. blockedBy is a PR/issue URL, owner/repo#N, or issue:owner/repo#N. |
acknowledge_queue_removal |
requireSha, queueCommitOid (full SHAs), removedAtUnix (positive safe integer) |
Records a local acknowledgment of the current native-stack CI removal; never enqueues or merges. REST returns a transport-unsupported error before GitHub I/O. |
Direct review operations forward every supplied ID without iterate's author, capability, or current-state filtering; GitHub is authoritative for authorization and mutation validity.
A merge result is { pr, repo, status, details, uncertain?, note?, merge_route? }:
| Field | Values |
|---|---|
status |
pending, enqueued, merged, or failed. Pending and enqueued are not merged. |
details |
May include message, uuid, expected_head_sha, merge_action, merge_method, bypass_rules, merged sha. |
uncertain |
uncertain: true marks an outcome that needs reconciliation before another request. |
note |
Present only for a recorded enqueued outcome (queue membership is not confirmed), a pending request whose lower stack layer moved or could not be re-read (also uncertain), or a request that superseded an unreadable default request (names that request's UUID; it may still execute). |
merge_route |
Present only when the merge left the asynchronous route: sync-fallback (the status read was refused, so the SHA-guarded synchronous merge ran) or cached-method (the repository's remembered method skipped merge-async). |
The CLI prints the same result as text or JSON. Rerunning the same request resumes its persisted UUID and returns the recorded result; it never submits twice.
| Prior outcome | Treatment |
|---|---|
Definite failed |
Replacement with changed options allowed. |
Definite enqueued |
Replacement after the PR head changes (a fresh read verifies the new requireSha), or on the same head after an acknowledged CI queue removal newer than the request. |
pending or uncertain |
Cannot be replaced. |
HTTP 400/409 without the async result envelope |
Definite failure. |
| Malformed successful response | Uncertain — GitHub may have accepted it. |
REST cannot verify merge-queue removal history, so automatic same-head queue recovery is unsupported and never emitted. Interrupted local replacements resume their persisted intent.
Native-stack merges also accept expectedStack: { number, baseRefName, prefix }:
numberis the positive stack number;baseRefNameis its trunk.prefixis the observed ordered lower prefix through the requested PR. Each entry is{ pr, headRefName, headRefOid, baseRefName }with a unique positive PR number and full lowercase head SHA. The final entry must match the requested PR andrequireSha.- Generated stack merge commands, on either transport, pass the same object as
--expected-stack '<JSON>'. - Missing membership or a changed stack identity, trunk, prefix, or parent ref rejects the request before submission; current READY receipts and topology are also revalidated (a receipt earned over GraphQL is matched by one GraphQL read of that layer; if that read fails, the rejection says the receipt could not be verified and carries the error).
- GitHub's
merge-asyncrequest binds only the requested (top) PR'ssha; no per-layer expected SHA is documented or accepted, so none is sent. Instead, every open lower layer's head is re-read right before submission: a head that moved, a layer added below the prefix, or a validated layer (or the whole stack) that is gone since validation returnsfailed(notuncertain) and nothing is submitted or recorded; a re-read that fails errors out without recording anything, so rerunning the same command re-checks. Eachpendingresume re-reads the layers validated at submission too (persisted in the merge record, so a request submitted without--expected-stackis covered); a moved or newly added lower layer, or a re-read that fails, returns thependingresult withuncertain: trueand anote, because a submitted request cannot be cancelled and may merge the unapproved commit. On resume a validated layer GitHub no longer lists is tolerated, because a request that is merging lower layers removes them. Remaining window: a push to a lower layer between that last re-read and GitHub executing the request (including while it ispending) is detected at most on the next resume, never prevented. Report it to the user. - The guard is persisted with the merge intent, so changing or dropping it cannot bypass pending or uncertain request protection.
Native stacks always use asynchronous merge, including when membership is discovered without an explicit guard. If an older request has no recorded stack membership, Shepherd must verify the PR is standalone before considering synchronous fallback. Membership is checked again right before the fallback is sent. If the PR is stacked or membership cannot be verified, Shepherd preserves the UUID and reports an uncertain outcome; the message names the stack lookup's own HTTP status. If GitHub refuses the fallback because the PR is in a stack (its 405 says so, even if the PR has left the stack since), after the PR joined a stack, or when membership cannot be read then, the original record, UUID, and sent marker are kept rather than retired, and the refusal is still raised as an error; later runs report the unconfirmed fallback.
The acknowledge_queue_removal result contains pr, repo, and acknowledgment with headSha, queueCommitOid, and removedAtUnix. Stack recovery still requires fresh source checks, a current READY receipt, and aggregate lower-layer checks.
On REST, a review mutation GitHub denies (403 or 404) is listed in deniedIds. When there is no session refusal, the result's instructions say not to retry, escalate, or route around those IDs, then to rerun Shepherd; later generated commands omit them until the item is edited.
A proxy session refusal stops the current review batch and every later apply operation. The review result keeps confirmed successes, carries the proxy message in sessionRefusal, and lists pending IDs in unrepliedThreads, unresolvedThreads, unminimizedComments, and undismissedReviews when nonempty. Its instructions tell the caller to repair session repository access and retry only those IDs. No GitHub-denied marker is persisted. CLI text/JSON and MCP Markdown/structured output carry the same partial result; the CLI exits 77.
Retrying an uncertain GraphQL reply reconciles a fresh transcript against pre-write evidence; see uncertain reply recovery.
Fetches one PR body through the selected transport and returns the same result as extractShepherdJournal(body), without exposing the body or PR node ID.
| Journal | Result |
|---|---|
| Absent | { ok: true, journal: null } |
| Malformed | { ok: false, error } |
Accepts one or more { threadId, message, description? } items and returns an ordered patch list with per-patch commit metadata and shared instructions. It builds against the fetched PR-head blobs, permits a clean local descendant of that head, and returns nothing unless the ordered stream passes git apply --check. It never writes a patch file or mutates git.
import {
appendJournalItem,
extractShepherdJournal,
reconcileShepherdJournal,
validateJournalItem,
} from "pr-shepherd/journal";
const extracted = extractShepherdJournal(liveBody);
if (!extracted.ok) throw new Error(extracted.error);
for (const entry of extracted.journal?.entries ?? []) console.log(entry);
const result = reconcileShepherdJournal(suppliedBody, liveBody);
if (!result.ok) throw new Error(result.error);
const validation = validateJournalItem("- Kept the existing behavior.");
const updatedBody = validation.ok
? appendJournalItem(result.body, validation.item).body
: result.body;| Function | Returns |
|---|---|
extractShepherdJournal(body) |
{ ok: true, journal: { format, entries } } for the one visible structural journal; { ok: true, journal: null } when none; { ok: false, error } for malformed, duplicate, mixed, nested, or unrecognized journal content. |
reconcileShepherdJournal(suppliedBody, liveBody) |
{ ok: true, body } | { ok: false, error }. Appends a non-empty live journal verbatim when the supplied body omits it; fails closed on malformed, duplicate, ambiguous, or canonical-to-legacy-downgrade containers. |
appendJournalItem(body, item) |
Creates or migrates the canonical details container and idempotently appends one item. |
validateJournalItem(text) |
Discriminated validation result for a - <text> item. |
Extracted entries keep their leading hyphen-and-space marker and continuation indentation, with line endings normalized to LF; an empty journal returns entries: []. Journal-shaped examples inside fenced code, comments, raw HTML, quotes, and list containers are ignored. <details> tags inside raw HTML blocks still count toward nesting, so bodies such as Dependabot release notes, whose </details> closers follow HTML without a blank line, reconcile normally.
import { createPrShepherdMcpServer, runPrShepherdMcpStdio } from "pr-shepherd/mcp";
const server = createPrShepherdMcpServer({ cwd: "/path/to/repo" });
await runPrShepherdMcpStdio({ cwd: "/path/to/repo" });createPrShepherdMcpServer accepts an optional shepherd for tests and exposes iterate, stack, apply, suggest, extract_journal, get_journal, and playbook. Selectors, projections, and tool schemas: mcp.md.
import type { ClassifyRule } from "pr-shepherd/classify";
const rule: ClassifyRule = (item) => {
if (item.author !== "gemini-code-assist") return null;
if (!/daily quota limit/i.test(item.body)) return null;
return { suppress: true, autoResolve: true };
};
export default rule;Put rule files under .pr-shepherd/classification/. ClassifyItem has kind, id, author, authorType, optional authorAssociation, and body. Rule loading and suppress / autoResolve behavior: classification rules.