Files
sim/packages/sim-cli/src/version.ts
T
Waleed 5c37778bbd fix(cli): identify the CLI to the API and name the remedy on every key refusal (#6792)
Sends a `User-Agent` on both request paths. Without one a CLI request is
indistinguishable from any other API traffic, so a bug that reproduces on a
single CLI version cannot be found in the server's own logs; the runtime and
platform ride along because they are the first things asked about a
transport failure only some users hit.

The version moves into its own module so the HTTP client can read it. It
could not import `program.ts` — program builds the commands, which reach the
client — and duplicating the manifest read would let the two disagree.

Also recognises `PRINCIPAL_KIND_NOT_PERMITTED`. The same refusal is raised at
two layers under two codes, and only the workspace-key one was matched, so
the audit-log commands reported "Principal kind workspace_api_key cannot
perform operation audit_logs.list" — accurate, written for a server log, and
missing the one sentence that tells the reader a personal key resolves it.
2026-08-17 16:39:24 -07:00

42 lines
1.6 KiB
TypeScript

import { readFileSync } from 'node:fs'
/**
* The published package version, and the `User-Agent` built from it.
*
* Its own module because both the command tree and the HTTP client need the
* version, and the client cannot reach `program.ts` — `program` builds the
* commands, which reach the client, so importing it back would close a cycle.
*
* Read from `package.json` rather than inlined so a release cannot ship a
* version string that disagrees with the package it came from. The bundle keeps
* `dist/index.js` one directory below the manifest, and npm always publishes the
* manifest, so the relative path holds for an installed package as well as a
* local build.
*/
function readPackageVersion(): string {
const metadata: unknown = JSON.parse(
readFileSync(new URL('../package.json', import.meta.url), 'utf8')
)
if (
typeof metadata !== 'object' ||
metadata === null ||
!('version' in metadata) ||
typeof metadata.version !== 'string'
) {
throw new Error('CLI package metadata is missing a valid version')
}
return metadata.version
}
export const CLI_VERSION = readPackageVersion()
/**
* Identifies the CLI to the API, the way every other terminal client does.
*
* Without it a CLI request is indistinguishable from any other API traffic, so
* a bug that only reproduces on one CLI version cannot be found in the server's
* own logs. The runtime and platform ride along for the same reason: they are
* the first things asked about a transport failure that only some users see.
*/
export const USER_AGENT = `sim-cli/${CLI_VERSION} node/${process.versions.node} (${process.platform}; ${process.arch})`