Files
insights/services/mcp/scripts/build-cli-agent-scopes.ts
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

73 lines
3.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env tsx
/**
* Generates the scope list for the CLI `agent-cli` login use case.
*
* `insights-cli login` mints a Personal API Key whose scopes come from the
* `use_cases` it requests in the browser authorize flow. The `agent-cli` use case
* must grant exactly what the MCP / `insights-cli api` tool surface needs,
* otherwise agent commands fail with `API key missing required scope`.
*
* The authoritative set is `getAdvertisedOAuthScopes()` — the union of every
* tool's `required_scopes`. We strip the OIDC identity scopes (`openid`,
* `profile`, `email`): those ride an OAuth access token, not a Personal API Key.
*
* Reads:
* - src/tools/toolDefinitions.ts (getAdvertisedOAuthScopes, via tool-definitions.json)
*
* Produces:
* - frontend/src/lib/agentScopes.generated.ts
*
* Run via insightscli: `insightscli build:openapi-cli-agent-scopes` (also runs as part of
* `build:openapi`, after `build:openapi-mcp-tools` which produces the tool
* definitions this reads). See `insightscli.yaml`.
*/
import * as fs from 'node:fs'
import * as path from 'node:path'
import { getAdvertisedOAuthScopes } from '../src/tools/toolDefinitions'
const MCP_ROOT = path.resolve(__dirname, '..')
const REPO_ROOT = path.resolve(MCP_ROOT, '../..')
const OUTPUT_TS = path.resolve(REPO_ROOT, 'frontend/src/lib/agentScopes.generated.ts')
function main(): void {
// Identity scopes (openid/profile/email) have no `:` and don't apply to
// Personal API Keys — keep only the resource scopes the tools require.
const scopes = getAdvertisedOAuthScopes().filter((scope) => scope.includes(':'))
// Scope names are snake_case identifiers + colons and never need escaping,
// but assert it so the build fails loudly if that ever changes.
for (const scope of scopes) {
if (scope.includes("'") || scope.includes('\\')) {
throw new Error(`unexpected character in scope name: ${scope}`)
}
}
const body = scopes.map((scope) => ` '${scope}',`).join('\n')
const output =
'// AUTO-GENERATED by services/mcp/scripts/build-cli-agent-scopes.ts.\n' +
'// Regenerate with `insightscli build:openapi`. Do not edit.\n' +
'//\n' +
'// The scopes the CLI `agent-cli` login use case grants — the same set the MCP\n' +
'// requests (getAdvertisedOAuthScopes, the union of every tool’s\n' +
'// required_scopes), minus the OIDC identity scopes that only apply to OAuth\n' +
'// access tokens rather than Personal API Keys.\n' +
'\n' +
'export const AGENT_USE_CASE_SCOPES = [\n' +
`${body}\n` +
'] as const\n'
fs.mkdirSync(path.dirname(OUTPUT_TS), { recursive: true })
const existing = fs.existsSync(OUTPUT_TS) ? fs.readFileSync(OUTPUT_TS, 'utf8') : null
if (existing !== output) {
fs.writeFileSync(OUTPUT_TS, output)
// eslint-disable-next-line no-console
console.log(`wrote ${OUTPUT_TS} (${scopes.length} scopes)`)
} else {
// eslint-disable-next-line no-console
console.log(`${OUTPUT_TS} already up to date (${scopes.length} scopes)`)
}
}
main()