@senad-d/branchme 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -6,5 +6,7 @@
6
6
  - Added strict BranchMe tools: `branch_status`, `change_branch`, `create_branch`, `push_branch`, and `pull_request`.
7
7
  - Added argv-style git helpers for repository status, branch validation/creation/switching, clean-worktree preflight, upstream detection, and current-branch push/publish.
8
8
  - Added GitHub repository resolution, environment-token and local `.env` token fallback handling, REST pull request creation, response validation, and token redaction.
9
- - Added unit tests with mocked `pi.exec` and `fetch` for git helpers, GitHub helpers, command behavior, tool schemas, prompt metadata, and extension registration.
10
- - Updated public documentation for implemented behavior, security boundaries, package structure, and validation commands.
9
+ - Added bounded automatic Git context before each agent run with branch/upstream state, working-tree counts and unstaged paths, authenticated related-open-PR lookup, and recent commits.
10
+ - Expanded `branch_status` into a shared, explicit, read-only context refresh for state that may change during a run.
11
+ - Added unit tests with mocked `pi.exec` and `fetch` for Git context collection and prompt safety, git helpers, GitHub helpers, command behavior, tool schemas, prompt metadata, and extension registration.
12
+ - Updated public documentation for automatic context behavior, authenticated lookup and prompt-insertion security boundaries, package structure, and validation commands.
package/README.md CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  ---
18
18
 
19
- BranchMe is a Pi extension for safe branch workflow automation. It adds an informational `/branchme` command and five agent-callable tools that inspect the current repository, switch to an existing local branch, create a branch from the current `HEAD`, push the current branch, and create a GitHub pull request.
19
+ BranchMe is a Pi extension for safe branch workflow automation. Before each agent run, it appends a bounded, read-only snapshot of the current Git repository to the system prompt. It also adds an informational `/branchme` command and five agent-callable tools that explicitly refresh state, switch to an existing local branch, create a branch from the current `HEAD`, push the current branch, and create a GitHub pull request.
20
20
 
21
21
  <table align="center">
22
22
  <tr>
@@ -29,12 +29,13 @@ BranchMe is a Pi extension for safe branch workflow automation. It adds an infor
29
29
  </tr>
30
30
  </table>
31
31
 
32
+ - **Context-aware:** every agent run starts with bounded branch, working-tree, related-PR, and recent-commit metadata; repository metadata is untrusted data, not instructions.
32
33
  - **Current-repository only:** Git and GitHub operations are scoped to the checkout where pi is running.
33
- - **Commit-safe:** BranchMe never stages files, creates commits, generates commit messages, rebases, merges, resets, or edits files directly.
34
+ - **Commit-safe:** context collection is read-only, and BranchMe never stages files, creates commits, generates commit messages, rebases, merges, resets, or edits files directly.
34
35
  - **Strict tools:** tool schemas reject extra properties such as `force`, `stash`, `discard`, `owner`, `repo`, `path`, or `baseRef`.
35
36
  - **PR-ready:** create GitHub pull requests from existing local branches after verifying the `headBranch` matches GitHub and the base is visible, with explicit PR fields and `GITHUB_TOKEN` or `GH_TOKEN` from the process environment or a local `.env` fallback.
36
37
 
37
- > **Security:** pi packages run with your full system permissions. BranchMe can run local `git` commands, switch/create branches, push the current branch, and call the GitHub REST API to create pull requests. Read [`SECURITY.md`](SECURITY.md).
38
+ > **Security:** pi packages run with your full system permissions. BranchMe runs local `git` commands, may make an automatic authenticated GitHub request to find a related open pull request, can switch/create branches and push the current branch, and can create GitHub pull requests. Read [`SECURITY.md`](SECURITY.md).
38
39
 
39
40
  ## Table of Contents
40
41
 
@@ -70,19 +71,20 @@ Inside pi:
70
71
  /branchme help
71
72
  ```
72
73
 
73
- Ask the agent to use BranchMe tools explicitly, for example:
74
+ A normal prompt can use the automatic start-of-run snapshot without a tool call. Ask for an explicit refresh when needed, for example:
74
75
 
75
76
  ```text
76
- Use branch_status, then create a branch named feature/update-docs with create_branch.
77
+ Refresh the repository state with branch_status, then create a branch named feature/update-docs with create_branch.
77
78
  ```
78
79
 
79
80
  A typical BranchMe flow is:
80
81
 
81
- 1. Inspect state with `branch_status`.
82
- 2. Switch with `change_branch` or create from current `HEAD` with `create_branch`.
83
- 3. Make edits and commit outside BranchMe.
84
- 4. Push the current branch with `push_branch`.
85
- 5. After `push_branch` completes and GitHub can see the branches, create a pull request with `pull_request`.
82
+ 1. Use the automatic snapshot to understand state at the start of the agent run.
83
+ 2. Use `branch_status` when you explicitly need fresh state, especially after a Git mutation in the same run.
84
+ 3. Switch with `change_branch` or create from current `HEAD` with `create_branch`.
85
+ 4. Make edits and commit outside BranchMe.
86
+ 5. Push the current branch with `push_branch`.
87
+ 6. After `push_branch` completes and GitHub can see the branches, create a pull request with `pull_request`.
86
88
 
87
89
  BranchMe is tool-based. The slash command is informational only and never changes branches, pushes, commits, stages, edits files, or opens pull requests.
88
90
 
@@ -98,6 +100,8 @@ BranchMe is tool-based. The slash command is informational only and never change
98
100
  | Git | `pi install git:github.com/senad-d/branchme@<tag>` | Pin a tag or commit. |
99
101
  | Local checkout | `pi --no-extensions -e .` | Develop or test this repository in isolation. |
100
102
 
103
+ Once loaded, BranchMe collects automatic Git context before each agent run. This happens for ordinary prompts; there is no `/branchme context` command and no additional tool to enable it.
104
+
101
105
  Source checkout:
102
106
 
103
107
  ```bash
@@ -135,7 +139,7 @@ git remote set-url origin git@github.com:OWNER/REPO.git
135
139
  export GITHUB_REPOSITORY=OWNER/REPO
136
140
  ```
137
141
 
138
- For `pull_request`, set a token in the process environment before starting pi:
142
+ For automatic related-PR lookup and `pull_request`, set a token in the process environment before starting pi:
139
143
 
140
144
  ```bash
141
145
  export GITHUB_TOKEN=github_pat_...
@@ -160,15 +164,15 @@ Run `pull_request` only after `push_branch` has completed; `pull_request` prefli
160
164
 
161
165
  ## Configuration
162
166
 
163
- BranchMe has no project config file. It reads process environment variables for GitHub pull request creation and optional repository boundary checks, with a local `.env` token fallback when no process token is set. Token lookup checks `process.env.GITHUB_TOKEN`, then `process.env.GH_TOKEN`; if neither is set, BranchMe reads `.env` from the verified git root and checks `GITHUB_TOKEN`, then `GH_TOKEN`.
167
+ BranchMe has no project config file. It reads process environment variables for automatic related-PR lookup, GitHub pull request creation, and optional repository boundary checks, with a local `.env` token fallback when no process token is set. Token lookup checks `process.env.GITHUB_TOKEN`, then `process.env.GH_TOKEN`; if neither is set, BranchMe reads `.env` from the verified git root and checks `GITHUB_TOKEN`, then `GH_TOKEN`.
164
168
 
165
169
  | Variable | Meaning |
166
170
  | --- | --- |
167
- | `GITHUB_TOKEN` | Preferred token for `pull_request`; process environment first, then local `.env` fallback. |
168
- | `GH_TOKEN` | Fallback token for `pull_request`; process environment first, then local `.env` fallback. |
171
+ | `GITHUB_TOKEN` | Preferred token for automatic related-PR lookup and `pull_request`; process environment first, then local `.env` fallback. |
172
+ | `GH_TOKEN` | Fallback token for automatic related-PR lookup and `pull_request`; process environment first, then local `.env` fallback. |
169
173
  | `GITHUB_REPOSITORY=owner/repo` | Optional CI fallback and boundary check for the current GitHub repository; process environment only. |
170
174
 
171
- BranchMe reads only `GITHUB_TOKEN` and `GH_TOKEN` from a small regular `.env` file; it rejects directories, symlinks, special files, and oversized files. BranchMe does not import other `.env` keys, read shell profiles, GitHub CLI credentials, or local credential stores. Token values are redacted from errors, tool content, and tool details.
175
+ BranchMe reads only `GITHUB_TOKEN` and `GH_TOKEN` from a small regular `.env` file; it rejects directories, symlinks, special files, and oversized files. BranchMe does not import other `.env` keys, read shell profiles, GitHub CLI credentials, or local credential stores. Token values are redacted from automatic context, errors, tool content, and tool details.
172
176
 
173
177
  If local `origin` and `GITHUB_REPOSITORY` both resolve but disagree, `pull_request` fails closed.
174
178
 
@@ -191,7 +195,7 @@ Commands are informational only. BranchMe actions are performed by agent-callabl
191
195
 
192
196
  | Tool | Schema | Behavior |
193
197
  | --- | --- | --- |
194
- | `branch_status` | `{}` | Read-only git status: repo root, current branch or detached state, upstream, dirty state, ahead/behind counts, and GitHub repository when resolvable. |
198
+ | `branch_status` | `{}` | Explicitly refreshes the same bounded context used at agent start: repo root in structured details, branch/detached state, upstream and ahead/behind counts, working-tree counts and unstaged/untracked paths, related open PR, and recent commits. It is read-only. |
195
199
  | `change_branch` | `{ "branchName": string }` | Validates `branchName`, requires `refs/heads/<branchName>` to exist locally, rejects dirty worktrees, and runs `git switch <branchName>`. |
196
200
  | `create_branch` | `{ "branchName": string }` | Validates `branchName`, rejects existing local branches, and runs `git switch -c <branchName>` from current `HEAD`. |
197
201
  | `push_branch` | `{}` | Pushes the current branch to its configured upstream remote with an explicit `HEAD:<upstream-branch-ref>` refspec, or publishes it with `git push --set-upstream origin <currentBranch>` when no upstream exists. |
@@ -203,6 +207,24 @@ All schemas reject additional properties. `change_branch` never accepts `baseRef
203
207
 
204
208
  ## Workflow and Boundaries
205
209
 
210
+ ### Automatic context and freshness
211
+
212
+ Before each agent run, BranchMe appends an **Automatic Git Context** snapshot to the existing system prompt. The snapshot contains these fields in order:
213
+
214
+ - current branch or detached `HEAD`, plus upstream and ahead/behind counts when available;
215
+ - working-tree state and staged, unstaged, and untracked counts;
216
+ - up to 20 unstaged or untracked change entries with Git status, path, and original path for renames/copies;
217
+ - related open PR status and, when found, its number, title, repository, head/base branches, URL, state, and draft flag;
218
+ - up to 5 recent commits with short hash, date, and subject.
219
+
220
+ Collection defaults are a 5-second timeout per local Git command, a 4-second related-PR lookup timeout, at most 512 characters per metadata value, and at most 4,000 characters for the rendered snapshot. GitHub response bodies are limited to 64 KiB. The formatter can further shorten values or omit entries to stay within the total limit.
221
+
222
+ The snapshot is fresh at agent start but is not live. A branch switch, commit, file change, push, or other mutation later in the same run can make it stale. `branch_status` performs an explicit current-state refresh through the same shared collector and remains read-only; it does not mutate files, Git state, or GitHub state.
223
+
224
+ Related-PR metadata does not come from Git alone. When repository, branch, and credentials resolve, automatic collection and explicit `branch_status` may make an authenticated `GET /repos/{owner}/{repo}/pulls?state=open&head={owner}:{branch}&per_page=1` request. Without a token there is no unauthenticated fallback or GitHub request; the PR field is reported as unavailable while local Git context remains usable.
225
+
226
+ Repository paths, commit subjects, branch names, and PR titles are treated as untrusted metadata. BranchMe redacts token values, escapes control characters, quotes and bounds values before prompt insertion, and never captures diffs or file contents. Staged files contribute only to the staged count; the path list is limited to unstaged and untracked entries.
227
+
206
228
  Use BranchMe from pi prompts or automation that drives pi with explicit tool calls:
207
229
 
208
230
  ```text
@@ -214,6 +236,7 @@ After push_branch completes, create a draft pull request from feature/docs-refre
214
236
 
215
237
  BranchMe operates only on the repository where pi is running:
216
238
 
239
+ - Automatic collection and `branch_status` run bounded, read-only Git commands from the verified git root.
217
240
  - Git commands use `pi.exec("git", args, { cwd, signal, timeout })` with argv arrays; repository mutations run from the verified git root.
218
241
  - `change_branch` switches only to existing local branches and has no `force`, `stash`, `discard`, remote, or path input.
219
242
  - `create_branch` creates from the current `HEAD` only and has no `baseRef` input.
@@ -269,6 +292,7 @@ Ensure the token and Git credentials have permission for the branch and pull req
269
292
  | Branch does not exist locally | Create a local branch first; `change_branch` does not checkout remote branches. |
270
293
  | Dirty worktree before branch switch | Commit, stash, or discard changes outside BranchMe before using `change_branch`. |
271
294
  | Push fails | Confirm the current branch is correct and your normal Git remote credentials can push. |
295
+ | Related PR is unavailable | Set `GITHUB_TOKEN` or `GH_TOKEN` before starting pi if related-PR context is wanted. Without a token, BranchMe keeps local context and intentionally makes no unauthenticated GitHub request. |
272
296
  | PR auth fails | Set `GITHUB_TOKEN` or `GH_TOKEN` before starting pi, or copy `.env.example` to `.env` and fill in one token. |
273
297
  | PR branch does not exist locally | Create or fetch/check out the local `headBranch` and `baseBranch` branches first; BranchMe does not use remote-only or cross-repository PR refs. |
274
298
  | PR branch is not visible or is stale on GitHub | Run `push_branch`, wait for it to complete, then retry `pull_request`; do not batch `push_branch` and `pull_request` in the same assistant tool call. |
@@ -288,7 +312,7 @@ npm run check:pack
288
312
  printf '/branchme help\n/quit\n' | pi --no-extensions -e .
289
313
  ```
290
314
 
291
- Validation covers TypeScript typechecking, formatting checks, unit tests, package checks, checkout Pi runtime smoke, and package-content verification. The checkout smoke loads BranchMe through Pi, then uses a temporary verifier command to confirm all five BranchMe tools are visible through `pi.getAllTools()` with strict schemas and prompt metadata. Smoke-test notes are recorded in [`docs/SMOKE_TEST.md`](docs/SMOKE_TEST.md), and TUI/help captures are stored in [`docs/TUI_CAPTURE.md`](docs/TUI_CAPTURE.md).
315
+ Validation covers TypeScript typechecking, formatting checks, automatic context collection and prompt injection, mocked GitHub lookup, unit tests, package checks, checkout Pi runtime smoke, and package-content verification. The checkout smoke loads BranchMe through Pi, then uses a temporary verifier command to confirm all five BranchMe tools are visible through `pi.getAllTools()` with strict schemas and prompt metadata. Smoke-test notes are recorded in [`docs/SMOKE_TEST.md`](docs/SMOKE_TEST.md), and TUI/help captures are stored in [`docs/TUI_CAPTURE.md`](docs/TUI_CAPTURE.md).
292
316
 
293
317
  Refresh TUI captures intentionally with:
294
318
 
package/SECURITY.md CHANGED
@@ -19,19 +19,24 @@ Implemented git mutations are limited to:
19
19
  - `create_branch`: `git switch -c <branchName>` from current `HEAD` after branch-name validation and existing-branch checks.
20
20
  - `push_branch`: `git push <upstreamRemote> HEAD:<upstreamBranchRef>` for the current branch when an upstream exists, or `git push --set-upstream origin <currentBranch>` when no upstream exists.
21
21
 
22
+ Before each agent run, BranchMe also runs bounded, read-only Git commands to collect branch/upstream/ahead-behind state, working-tree counts, up to 20 unstaged or untracked path entries, and up to 5 recent commits. The same collector runs when `branch_status` explicitly refreshes context. Collection does not run `switch`, `push`, `add`, `commit`, or any other mutation, and it never reads diffs or file contents.
23
+
22
24
  Branch switching can update working-tree files as normal Git checkout behavior. Mutating branch operations for the same repository are serialized to avoid same-turn branch races. `pull_request` also uses the same repository queue around PR preflight and creation so it can wait behind an already-started same-repository push. BranchMe rejects dirty worktrees before `change_branch` and does not force checkout, stash, stage files, create commits, reset, rebase, merge, or edit files directly.
23
25
 
24
26
  ## Network behavior
25
27
 
26
- BranchMe makes network requests only for `pull_request`, which calls the GitHub REST API:
28
+ BranchMe's GitHub helpers use these REST API requests:
27
29
 
28
30
  ```text
31
+ GET https://api.github.com/repos/{owner}/{repo}/pulls?state=open&head={owner}:{currentBranch}&per_page=1
29
32
  GET https://api.github.com/repos/{owner}/{repo}/branches/{headBranch}
30
33
  GET https://api.github.com/repos/{owner}/{repo}/branches/{baseBranch}
31
34
  POST https://api.github.com/repos/{owner}/{repo}/pulls
32
35
  ```
33
36
 
34
- The branch preflight requests have no body. BranchMe uses the `headBranch` preflight response to compare GitHub's branch commit with the local branch commit before creating the PR. The PR request body contains only the explicit PR fields supplied to the tool: title, head branch, base branch, body, and draft flag.
37
+ The first request is an automatic network boundary: it can run before each agent run and whenever `branch_status` explicitly refreshes context. It is a bounded, read-only lookup for one open pull request whose head is the current local branch (`per_page=1`), with a default 4-second timeout and a 64 KiB response-body limit. It is skipped when repository/branch resolution or authentication is unavailable, so BranchMe never makes an unauthenticated fallback request. Timeout, HTTP, network, malformed, and oversized-response failures become a safe unavailable state without exposing response bodies or raw network errors. Git alone is not used or claimed to provide PR metadata.
38
+
39
+ The branch preflight requests have no body. BranchMe uses the `headBranch` preflight response to compare GitHub's branch commit with the local branch commit before creating the PR. The PR request body contains only the explicit PR fields supplied to the tool: title, head branch, base branch, body, and draft flag. All GitHub response reads are bounded.
35
40
 
36
41
  ## Repository boundary
37
42
 
@@ -40,22 +45,28 @@ BranchMe operates on the current repository only.
40
45
  - The GitHub repository is inferred from local `origin` and/or `GITHUB_REPOSITORY`.
41
46
  - Tool inputs never accept filesystem paths, `owner`, `repo`, or owner-prefixed `owner:branch` PR refs.
42
47
  - `change_branch` accepts only `branchName` and never creates branches, checks out remote branches, forces, stashes, or discards changes.
43
- - If local `origin` and `GITHUB_REPOSITORY` both resolve but disagree, PR creation fails closed.
48
+ - If local `origin` and `GITHUB_REPOSITORY` both resolve but disagree, PR creation and related-PR lookup fail closed.
44
49
  - PR branch inputs are validated as existing local branch-name refs; missing local branches and cross-repository `head` values are rejected before token lookup or any GitHub request.
45
50
  - PR branch inputs must also be visible on GitHub before the PR is created, and `headBranch` must match the local branch commit; unpublished or stale `headBranch` values fail with guidance to run `push_branch`, wait for it to complete, and retry `pull_request`.
46
51
 
47
52
  ## Credentials
48
53
 
49
- `pull_request` checks `process.env.GITHUB_TOKEN`, then `process.env.GH_TOKEN`. If neither process token is set, BranchMe reads a local `.env` file from the verified git root and checks:
54
+ `pull_request` and related-PR lookup check `process.env.GITHUB_TOKEN`, then `process.env.GH_TOKEN`. If neither process token is set, BranchMe reads a local `.env` file from the verified git root and checks:
50
55
 
51
56
  - `GITHUB_TOKEN` (preferred)
52
57
  - `GH_TOKEN` (fallback)
53
58
 
54
- Only these two token keys are read from `.env`; other `.env` keys are ignored. The fallback uses async file I/O, requires `.env` to be a small regular file, and rejects directories, symlinks, special files, and oversized files. BranchMe does not read shell profiles, GitHub CLI credentials, or local credential stores. Token values are redacted from thrown errors, tool content, and tool details.
59
+ Only these two token keys are read from `.env`; other `.env` keys are ignored. The fallback uses async file I/O, requires `.env` to be a small regular file, and rejects directories, symlinks, special files, and oversized files. BranchMe does not read shell profiles, GitHub CLI credentials, or local credential stores. Token values are redacted from thrown errors, automatic context, tool content, and tool details.
60
+
61
+ ## System prompt boundary
62
+
63
+ Automatic context is appended to Pi's system prompt before each agent run. Branch names, paths, Git status values, commit subjects, and GitHub PR metadata are repository-controlled, untrusted data and must never be interpreted as instructions. BranchMe redacts recognized token values, escapes control and format characters, quotes metadata, limits individual values to 512 characters by default, and limits the complete rendered snapshot to 4,000 characters. It further shortens values or omits entries when needed to enforce the total bound.
64
+
65
+ The automatic snapshot can become stale after a Git or filesystem mutation during the same run. `branch_status` is the explicit, read-only refresh path. Neither automatic collection nor `branch_status` captures diff hunks or file contents; staged paths are not listed, although the staged file count is included.
55
66
 
56
67
  ## Telemetry
57
68
 
58
- BranchMe does not collect telemetry and does not send repository contents to any service beyond the explicit GitHub pull request fields provided to `pull_request`.
69
+ BranchMe does not collect telemetry. Related-PR lookup sends only the resolved repository owner/name and current branch in the authenticated GitHub API URL. PR creation sends only the explicit GitHub pull request fields described above; BranchMe does not send diff contents, filenames, commit subjects, or local file contents to GitHub during context collection.
59
70
 
60
71
  ## Reporting vulnerabilities
61
72
 
@@ -1,6 +1,6 @@
1
1
  # BranchMe Smoke Test Notes
2
2
 
3
- Date: 2026-06-30
3
+ Date: 2026-07-15
4
4
 
5
5
  Validated from the repository checkout with no discovered extensions enabled.
6
6
 
@@ -17,14 +17,15 @@ pi --no-extensions -e .
17
17
 
18
18
  ## Automated smoke behavior
19
19
 
20
- - `npm run smoke:pi` runs isolated checkout Pi processes from a temporary working directory: one with `pi --no-extensions -e <package> -e <temporary verifier>` and `/branchmeverify verify`, then one with `pi --no-extensions -e <package>` and `/branchme help`.
21
- - The temporary verifier is a non-LLM Pi extension command that calls `pi.getAllTools()` after BranchMe loads and confirms `branch_status`, `change_branch`, `create_branch`, `push_branch`, and `pull_request` are active runtime tools with strict schemas, prompt guidelines, descriptions, and extension source metadata.
22
- - The checkout smoke accepts either `/branchme help` text or the read-only BranchMe status fallback as equivalent non-mutating command output.
20
+ - `npm run smoke:pi` first runs isolated checkout Pi processes from a temporary non-Git working directory: one with `pi --no-extensions -e <package> -e <temporary verifier>` and `/branchmeverify verify`, then one with `pi --no-extensions -e <package>` and `/branchme help`.
21
+ - The temporary command verifier calls `pi.getAllTools()` after BranchMe loads and confirms `branch_status`, `change_branch`, `create_branch`, `push_branch`, and `pull_request` are active runtime tools with strict schemas, prompt guidelines, descriptions, and extension source metadata.
22
+ - A second temporary verifier registers a deterministic local smoke model, blocks `fetch`, and runs normal prompts through real Pi lifecycle handling. It verifies automatic no-tool Git context in a temporary repository, one real `branch_status` tool refresh after a verifier-created local change, safe credential-free related-PR status with no request, and non-Git startup fallback.
23
+ - The checkout command smoke accepts either `/branchme help` text or the read-only BranchMe status fallback as equivalent non-mutating command output.
23
24
  - `npm run smoke:pi:packed` creates an npm tarball under a temporary directory, installs that tarball into a separate temporary package with `npm install --omit=dev`, and runs pi against the installed package instead of the source checkout.
24
25
  - `npm run smoke:pi:packed` is the release gate for packaged runtime imports and required packaged files; `npm run release:check` and `node scripts/publish-npm.mjs` run it before publish, while everyday `npm run validate` keeps the faster checkout smoke.
25
26
  - Both smoke runs disable discovered extensions, skills, prompt templates, themes, context files, persistent sessions, telemetry, startup network checks, and GitHub token environment variables.
26
27
  - Both smoke runs are credential-free, allow documented credential variable names in help text, reject credential value patterns, do not call BranchMe mutation tools, and do not contact GitHub.
27
- - Pi's documented `getAllTools()` metadata currently exposes parameter schemas and prompt guidelines, but not `promptSnippet`, and the ExtensionAPI does not expose a safe non-LLM tool invocation method. The checkout runtime smoke therefore gates real Pi tool registration metadata; isolated `branch_status` execution remains covered by mocked unit tests and real-git helper tests until Pi exposes a direct runtime invocation API.
28
+ - Pi's documented `getAllTools()` metadata currently exposes parameter schemas and prompt guidelines, but not `promptSnippet`. The deterministic local smoke model exercises `branch_status` through Pi's normal model tool-call loop without provider or GitHub network access.
28
29
  - Set `BRANCHME_SKIP_PI_SMOKE=1` to skip intentionally, or `BRANCHME_PI_BIN=/path/to/pi` to test a specific Pi binary for the checkout smoke.
29
30
  - If no Pi binary is available, the checkout smoke script prints an explicit skip message; default CI installs the Pi dev dependency, so `npm run validate` exercises the real Pi loading path.
30
31
 
@@ -32,6 +33,7 @@ pi --no-extensions -e .
32
33
 
33
34
  - `npm run validate` passed.
34
35
  - `npm run smoke:pi` loaded BranchMe through Pi, verified all five BranchMe tools through the real `pi.getAllTools()` runtime surface, and confirmed non-mutating BranchMe command output.
36
+ - The isolated Git-context prompt smoke observed the `before_agent_start` snapshot, answered branch and dirty-tree state without a tool call, refreshed a verifier-created local change through one real `branch_status` call, returned safe unavailable context without credentials or outside Git, and attempted no network request.
35
37
  - `npm run smoke:pi:packed` packed BranchMe outside the repository, installed the artifact in a temporary production workspace, loaded the installed package through Pi, and confirmed non-mutating BranchMe command output.
36
38
  - `npm run check:pack` confirmed the package contents are limited to public docs, images, source, license, package metadata, `.env.example`, and `tsconfig.json`.
37
39
  - The isolated Pi smoke command loaded BranchMe and displayed BranchMe help or status output instead of template behavior.
package/docs/STRUCTURE.md CHANGED
@@ -6,9 +6,10 @@ BranchMe is a TypeScript Pi extension package for current-repository git branch,
6
6
 
7
7
  ```text
8
8
  src/
9
- ├── extension.ts # extension entry point; registers command and tools only
10
- ├── constants.ts # command/tool names, timeouts, GitHub API constants
9
+ ├── extension.ts # extension entry point; registers command, tools, and context hook
10
+ ├── constants.ts # names plus Git, context, timeout, API, and output bounds
11
11
  ├── types.ts # serializable tool details and helper result types
12
+ ├── git-context.ts # shared collector/formatter and before_agent_start prompt hook
12
13
  ├── commands/
13
14
  │ └── branchme-command.ts # /branchme status/help command; informational only
14
15
  ├── tools/
@@ -21,27 +22,34 @@ src/
21
22
 
22
23
  ## Module boundaries
23
24
 
24
- 1. `src/extension.ts` stays small and only calls `registerBranchMeCommand(pi)` and `registerBranchMeTools(pi)`.
25
- 2. `src/commands/branchme-command.ts` parses `/branchme`, `/branchme help`, `--help`, and `-h`; it never performs git or GitHub mutations and avoids raw stdout in JSON mode.
26
- 3. `src/tools/branchme-tools.ts` owns TypeBox schemas, prompt metadata, tool content, and safe structured details.
27
- 4. `src/git.ts` owns current-repository git behavior: root detection, branch/upstream/status inspection, branch validation, branch creation, existing-local-branch switching, clean-worktree preflight, current-branch push/publish, and the per-repository workflow queue.
28
- 5. `src/github.ts` owns GitHub `owner/repo` parsing, repository boundary checks, `GITHUB_TOKEN`/`GH_TOKEN` process-env and hardened git-root `.env` fallback resolution, PR branch-name syntax validation, GitHub branch visibility/commit preflight, PR REST calls, response validation, and redacted errors.
29
- 6. `src/redaction.ts` owns shared credential redaction for Git and GitHub messages.
30
- 7. `src/types.ts` keeps serializable details shared by helpers and tools.
31
- 8. `src/ui/branchme-panel.ts` renders a compact status panel and clips lines to terminal width.
25
+ 1. `src/extension.ts` stays small and registers the command, five tools, and one `before_agent_start` context hook.
26
+ 2. `src/git-context.ts` owns the shared read-only collector, escaped/bounded formatter, automatic system-prompt append, and the current-state output used by `branch_status`.
27
+ 3. `src/commands/branchme-command.ts` parses `/branchme`, `/branchme help`, `--help`, and `-h`; it never performs git or GitHub mutations and avoids raw stdout in JSON mode.
28
+ 4. `src/tools/branchme-tools.ts` owns TypeBox schemas, prompt metadata, tool content, and safe structured details. `branch_status` delegates to the shared context collector for an explicit refresh.
29
+ 5. `src/git.ts` owns current-repository git behavior: root detection, branch/upstream/ahead-behind inspection, working-tree parsing, recent-commit collection, branch validation, branch creation, existing-local-branch switching, clean-worktree preflight, current-branch push/publish, and the per-repository workflow queue.
30
+ 6. `src/github.ts` owns GitHub `owner/repo` parsing, repository boundary checks, `GITHUB_TOKEN`/`GH_TOKEN` process-env and hardened git-root `.env` fallback resolution, authenticated related-open-PR lookup, PR branch-name syntax validation, GitHub branch visibility/commit preflight, PR REST calls, bounded response validation, and redacted errors.
31
+ 7. `src/redaction.ts` owns shared credential redaction for Git, GitHub, and prompt-bound metadata.
32
+ 8. `src/types.ts` keeps serializable details shared by helpers, context, and tools.
33
+ 9. `src/ui/branchme-panel.ts` renders a compact status panel and clips lines to terminal width.
32
34
 
33
35
  ## Pi extension conventions
34
36
 
35
37
  - No long-lived processes, watchers, timers, sockets, or background jobs start in the extension factory.
36
- - Slash commands are informational; tools perform branch, push, and PR actions.
38
+ - A single `before_agent_start` handler synchronously collects a fresh snapshot for each agent run and appends it to the existing system prompt; failures degrade to bounded unavailable context rather than blocking startup.
39
+ - Slash commands are informational; tools perform branch, push, and PR actions. There is no context command or sixth tool.
37
40
  - Every tool uses a strict TypeBox object schema with `additionalProperties: false`.
38
41
  - Every tool defines a description, `promptSnippet`, and tool-specific `promptGuidelines` that explicitly name the tool.
39
42
  - Git commands use `pi.exec("git", args, { cwd, signal, timeout })` with argv arrays; repository mutations run from the verified git root and same-repository mutation/PR windows are serialized per repository.
40
43
  - Tool details avoid token values and unbounded raw command/API output.
44
+ - Automatic and explicit context include branch/upstream/ahead-behind state, working-tree counts, up to 20 unstaged/untracked entries, related open PR state, and up to 5 recent commits. Metadata values default to 512 characters and rendered context to 4,000 characters.
41
45
  - Pi core packages, including `@earendil-works/pi-tui` for key/width utilities, remain in `peerDependencies` with `"*"`.
42
46
 
43
47
  ## Security-sensitive areas
44
48
 
49
+ - Automatic context and `branch_status` share bounded, read-only collection. They run no mutations and capture repository metadata only—never diffs or file contents.
50
+ - Repository-controlled paths, branch names, commit subjects, and PR fields are escaped, quoted, redacted, bounded, and labeled untrusted before system-prompt insertion.
51
+ - Related-PR lookup may issue an authenticated `GET /pulls` before every agent run and on explicit refresh. It has a 4-second timeout and 64 KiB response limit, and makes no unauthenticated fallback request.
52
+ - The start-of-run snapshot may be stale after a mutation in that same run; `branch_status` is the explicit read-only refresh.
45
53
  - `change_branch` mutates local HEAD and working-tree files only through `git switch <branchName>` for existing local branches after a clean-worktree preflight.
46
54
  - `create_branch` mutates local branch/HEAD only with `git switch -c`.
47
55
  - `push_branch` mutates remote refs only for the current branch and uses an explicit upstream remote/refspec instead of bare `git push` when an upstream exists.
@@ -61,11 +69,12 @@ src/
61
69
  ```text
62
70
  test/
63
71
  ├── command.test.mjs # /branchme parsing, help, fallback, panel width
72
+ ├── git-context.test.mjs # collection, prompt hook, formatting, safety, and output bounds
64
73
  ├── git.test.mjs # git helper command construction and failures
65
- ├── git-integration.test.mjs # isolated real-git branch helper coverage
66
- ├── github.test.mjs # GitHub parsing, token resolution, fetch wrapper, redaction
74
+ ├── git-integration.test.mjs # isolated real-git context and branch helper coverage
75
+ ├── github.test.mjs # GitHub parsing, token resolution, related-PR lookup, redaction
67
76
  ├── preparation.test.mjs # package/docs/source metadata checks
68
- ├── tools.test.mjs # extension registration, schemas, prompt metadata, tool behavior
77
+ ├── tools.test.mjs # extension registration, schemas, shared refresh, tool behavior
69
78
  └── tui-capture.test.mjs # generated text capture for TUI/help visual baselines
70
79
  ```
71
80
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@senad-d/branchme",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "description": "Minimal Pi extension for current-repository git branch, push, and pull request workflows.",
6
6
  "license": "MIT",
@@ -50,9 +50,9 @@
50
50
  "format:check": "node scripts/check-format.mjs",
51
51
  "test": "node --test test/*.test.mjs",
52
52
  "test:coverage": "mkdir -p coverage && node --test --experimental-test-coverage --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=coverage/lcov.info test/*.test.mjs",
53
- "smoke:pi": "node scripts/smoke-pi-runtime.mjs",
53
+ "smoke:pi": "node scripts/smoke-pi-runtime.mjs && node scripts/smoke-pi-git-context.mjs",
54
54
  "smoke:pi:packed": "node scripts/smoke-pi-installed-package.mjs",
55
- "check": "node --check scripts/check-format.mjs && node --check scripts/check-package-contents.mjs && node --check scripts/publish-npm.mjs && node --check scripts/smoke-pi-installed-package.mjs && node --check scripts/smoke-pi-runtime.mjs",
55
+ "check": "node --check scripts/check-format.mjs && node --check scripts/check-package-contents.mjs && node --check scripts/publish-npm.mjs && node --check scripts/smoke-pi-git-context.mjs && node --check scripts/smoke-pi-installed-package.mjs && node --check scripts/smoke-pi-runtime.mjs",
56
56
  "pack:dry-run": "npm pack --dry-run --json",
57
57
  "check:pack": "node scripts/check-package-contents.mjs",
58
58
  "validate": "npm run typecheck && npm run format:check && npm run test && npm run smoke:pi && npm run check && npm run check:pack",
package/src/constants.ts CHANGED
@@ -18,6 +18,12 @@ export const BRANCHME_TOOL_NAMES = [
18
18
  export const GIT_STATUS_TIMEOUT_MS = 5_000;
19
19
  export const GIT_MUTATION_TIMEOUT_MS = 30_000;
20
20
  export const GIT_PUSH_TIMEOUT_MS = 120_000;
21
+ export const GITHUB_RELATED_PR_TIMEOUT_MS = 4_000;
22
+
23
+ export const GIT_CONTEXT_RECENT_COMMIT_LIMIT = 5;
24
+ export const GIT_CONTEXT_CHANGE_LIMIT = 20;
25
+ export const GIT_CONTEXT_VALUE_LIMIT_CHARS = 512;
26
+ export const GIT_CONTEXT_SUMMARY_LIMIT_CHARS = 4_000;
21
27
 
22
28
  export const GITHUB_API_BASE_URL = "https://api.github.com";
23
29
  export const GITHUB_API_VERSION = "2022-11-28";
package/src/extension.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
2
  import { registerBranchMeCommand } from "./commands/branchme-command.ts";
3
+ import { registerGitContextAwareness } from "./git-context.ts";
3
4
  import { registerBranchMeTools } from "./tools/branchme-tools.ts";
4
5
 
5
6
  export function branchMeExtension(pi: ExtensionAPI): void {
6
7
  registerBranchMeCommand(pi);
7
8
  registerBranchMeTools(pi);
9
+ registerGitContextAwareness(pi);
8
10
  }
9
11
 
10
12
  export default branchMeExtension;