Skip to content

Commit c729f2e

Browse files
authored
chore: sync extension version metadata (#45)
Generate the extension version from package.json for trace metadata and validate it during checks. Document the release process updates for generated version metadata.
1 parent 8a272e7 commit c729f2e

8 files changed

Lines changed: 65 additions & 17 deletions

File tree

AGENTS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ When working in this repo, optimize for:
2828
- This repo is managed by [mise](https://mise.jdx.dev/) using `.tool-versions`; prefer that as the toolchain source of truth.
2929
- Preserve package identity in `package.json` as a pi package.
3030
- Keep the extension entrypoint at `src/index.ts` unless there is a strong reason to change it.
31+
- Keep `src/version.ts` generated from `package.json`; do not edit it by hand. After a package version bump, run `pnpm run sync:version` and commit the generated change.
3132
- Prefer evolving the current design over rewriting it.
3233
- Tracing must be best-effort: log failures and continue.
3334
- Preserve the intended trace hierarchy unless there is a strong reason to change it:
@@ -73,9 +74,11 @@ If the change affects extension lifecycle behavior, session persistence/restore,
7374
pnpm run test:integration
7475
```
7576

76-
If packaging changes were made, also run:
77+
If packaging changes or release version bumps were made, also run:
7778

7879
```bash
80+
pnpm run sync:version
81+
pnpm run check
7982
pnpm run pack
8083
pnpm pack --dry-run
8184
```

CONTRIBUTING.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This repo is managed by [mise](https://mise.jdx.dev/). Use the pinned project to
3232
```bash
3333
mise install
3434
pnpm install
35+
pnpm run sync:version
3536
pnpm run check
3637
pnpm run pack
3738
pnpm run smoke
@@ -49,7 +50,8 @@ pnpm run smoke
4950

5051
Notes:
5152

52-
- `pnpm run check` is the main formatting, linting, and type-check entrypoint.
53+
- `pnpm run sync:version` regenerates `src/version.ts` from `package.json` after a version bump.
54+
- `pnpm run check` verifies `src/version.ts` is in sync, then runs the main formatting, linting, and type-check entrypoint.
5355
- `pnpm run typecheck` delegates to `vp check`.
5456
- `pnpm run pack` builds an optional library bundle in `dist/`.
5557
- pi loads the extension directly from `src/index.ts`, so local development does not require a build step.
@@ -77,6 +79,9 @@ Notes:
7779
- content normalization, truncation, IDs, small helpers
7880
- `src/types.ts`
7981
- shared TypeScript types for config, spans, state, and normalized messages
82+
- `src/version.ts`
83+
- generated extension version constant derived from `package.json`
84+
- do not edit by hand; run `pnpm run sync:version` after changing the package version
8085

8186
## Repo conventions
8287

@@ -87,6 +92,7 @@ Notes:
8792
- Prefer Node built-ins over adding packages.
8893
- This repo uses **Vite+** for checks and packaging.
8994
- pi loads the extension directly from TypeScript; no build output directory is required for local development.
95+
- `src/version.ts` is generated at package/build time from `package.json`; `prepack` regenerates it and `check` fails if it is stale.
9096

9197
### Style
9298

@@ -174,9 +180,11 @@ pnpm test
174180
pnpm run smoke
175181
```
176182

177-
If packaging changes were made, also run:
183+
If packaging changes or release version bumps were made, also run:
178184

179185
```bash
186+
pnpm run sync:version
187+
pnpm run check
180188
pnpm run pack
181189
pnpm pack --dry-run
182190
```

docs/PUBLISHING.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,20 @@ Before using the workflow, make sure:
2424
1. The package name on npm is correct: `@braintrust/pi-extension`
2525
2. npm trusted publishing is configured for this repository/package
2626
3. You have bumped `package.json` to the version you want to release
27-
4. The branch you want to publish is pushed to GitHub
27+
4. You have regenerated and committed `src/version.ts` with `pnpm run sync:version`
28+
5. The branch you want to publish is pushed to GitHub
2829

2930
## Release steps
3031

3132
1. Update `package.json` with the target version.
32-
2. Commit and push the version bump to the branch you want to release from.
33-
3. In GitHub, open **Actions**.
34-
4. Run the **Publish package** workflow.
35-
5. Enter the branch to publish from, such as `main`.
36-
6. Start the workflow.
37-
7. After it succeeds, verify:
33+
2. Run `pnpm run sync:version` to regenerate `src/version.ts`.
34+
3. Run the local preflight checks below.
35+
4. Commit and push the version bump, including `src/version.ts`, to the branch you want to release from.
36+
5. In GitHub, open **Actions**.
37+
6. Run the **Publish package** workflow.
38+
7. Enter the branch to publish from, such as `main`.
39+
8. Start the workflow.
40+
9. After it succeeds, verify:
3841
- the package version exists on npm
3942
- the provenance attestation is present on npm
4043
- the `pi-extension-v<version>` tag was pushed
@@ -73,9 +76,9 @@ This job:
7376
- sets up pnpm and Node.js with npm registry access using those pinned versions
7477
- runs `pnpm install --frozen-lockfile`
7578
- runs validation and packaging commands:
76-
- `pnpm run check`
79+
- `pnpm run check` (including generated version sync validation)
7780
- `pnpm test`
78-
- `pnpm run pack`
81+
- `pnpm run pack` (regenerates `src/version.ts` via `prepack`)
7982
- `pnpm pack --dry-run`
8083
- `pnpm run smoke`
8184
- publishes to npm with provenance:
@@ -117,7 +120,7 @@ Example for version `0.1.0`:
117120
The workflow will fail if:
118121

119122
- the target git tag already exists on the remote
120-
- formatting, linting, type checking, tests, pack, or smoke checks fail
123+
- generated version sync, formatting, linting, type checking, tests, pack, or smoke checks fail
121124
- npm trusted publishing is not configured correctly
122125
- npm rejects the publish for version or package metadata reasons
123126

@@ -131,6 +134,7 @@ Important behavior:
131134
Before triggering a release, it is a good idea to run:
132135

133136
```bash
137+
pnpm run sync:version
134138
pnpm run check
135139
pnpm test
136140
pnpm run pack
@@ -142,4 +146,5 @@ pnpm run smoke
142146

143147
- The package is public, and `package.json` includes `publishConfig.access = "public"`.
144148
- The workflow is manual (`workflow_dispatch`) rather than tag-triggered.
145-
- If the release process changes, update both this file and `.github/workflows/publish.yml`.
149+
- `src/version.ts` is generated from `package.json` so trace metadata reports the package version without runtime package metadata loading.
150+
- If the release process changes, update this file, `AGENTS.md`, `CONTRIBUTING.md`, and `.github/workflows/publish.yml` as needed.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
},
2626
"scripts": {
2727
"preinstall": "node -e \"const userAgent = process.env.npm_config_user_agent || ''; if (process.env.INIT_CWD === process.cwd() && !userAgent.includes('pnpm/')) { console.error('Use pnpm in this repo.'); process.exit(1); }\"",
28-
"check": "vp check",
28+
"check": "node scripts/sync-version.mjs --check && vp check",
2929
"fmt": "vp fmt",
3030
"lint": "vp lint",
31+
"prepack": "node scripts/sync-version.mjs",
3132
"pack": "vp pack",
33+
"sync:version": "node scripts/sync-version.mjs",
3234
"test": "vitest run",
3335
"test:integration": "vitest run src/index.integration.test.ts",
3436
"test:watch": "vitest",

scripts/sync-version.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env node
2+
import { readFileSync, writeFileSync } from "node:fs";
3+
4+
const check = process.argv.includes("--check");
5+
const packageJson = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
6+
const version = packageJson.version;
7+
8+
if (typeof version !== "string" || version.length === 0) {
9+
throw new Error("package.json version must be a non-empty string");
10+
}
11+
12+
const target = new URL("../src/version.ts", import.meta.url);
13+
const content = `// This file is generated by scripts/sync-version.mjs. Do not edit by hand.\nexport const EXTENSION_VERSION = ${JSON.stringify(version)};\n`;
14+
15+
if (check) {
16+
const current = readFileSync(target, "utf8");
17+
if (current !== content) {
18+
console.error("src/version.ts is out of sync with package.json. Run `pnpm run sync:version`.");
19+
process.exit(1);
20+
}
21+
} else {
22+
writeFileSync(target, content);
23+
}

src/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { readFileSync } from "node:fs";
12
import { beforeEach, describe, expect, it, vi } from "vitest";
23

4+
const packageVersion = (JSON.parse(readFileSync("package.json", "utf8")) as { version: string })
5+
.version;
6+
37
const mockState = vi.hoisted(() => ({
48
startSpans: [] as Array<Record<string, unknown>>,
59
logSpans: [] as Array<Record<string, unknown>>,
@@ -193,6 +197,7 @@ describe("braintrustPiExtension", () => {
193197
expect(mockState.widgets.at(-1)?.content?.[1]).toBe(
194198
"braintrust.dev/app/test-org/p/pi/logs?oid=trace-row-1",
195199
);
200+
expect(mockState.startSpans[0]?.metadata).toMatchObject({ extension_version: packageVersion });
196201

197202
await emit("session_shutdown");
198203

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { basename, resolve } from "node:path";
21
import { hostname, userInfo } from "node:os";
2+
import { basename, resolve } from "node:path";
33
import type { AgentEndEvent, ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
44
import { BraintrustClient, type BraintrustSpanHandle } from "./client.ts";
55
import { createLogger, loadConfig } from "./config.ts";
@@ -13,6 +13,7 @@ import type {
1313
NormalizedAssistantMessage,
1414
TraceConfig,
1515
} from "./types.ts";
16+
import { EXTENSION_VERSION } from "./version.ts";
1617
import {
1718
buildTurnInput,
1819
extractErrorText,
@@ -29,7 +30,6 @@ import {
2930
toUnixSeconds,
3031
} from "./utils.ts";
3132

32-
const EXTENSION_VERSION = "0.1.0";
3333
const TRACING_STATUS_KEY = "braintrust-tracing";
3434
const TRACING_WIDGET_KEY = "braintrust-trace-link";
3535

src/version.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file is generated by scripts/sync-version.mjs. Do not edit by hand.
2+
export const EXTENSION_VERSION = "0.5.0";

0 commit comments

Comments
 (0)