Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENT-INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ made, not as an installation.

A production build of a registered site that has **no** credential — a hosted builder's publish, which builds from the committed project and so never sees the git-ignored key file — does not send its manifest or its map. `scan` and `mark-build` POST `monitor/pulse/build/<your site uuid>` instead, with no credential and four fields: the manifest checksum, `environment: "production"`, what started the run (`trigger`), and (`mark-build` only) the marker result. **No package names, no address, no name, no environment variable values.** Patchstack records the build against the package list it last received with a key, and shows it as reported rather than deployed until the live page is seen running it. Any other run with no key sends nothing to an existing site; in a Lovable or Replit project that is not an error, so an install without the key goes on.

Every request the CLI sends names the command that sent it (`X-Patchstack-Command`: `setup`, `scan`, `map`, `mark-build`, `dev` and so on) and what started it (`X-Patchstack-Trigger`: `install`, `build` or `other`). The claim-page check (`monitor/claim/preview`) also says whether the run holds the site's key (`X-Patchstack-Key`: `present` or `none`) — never the key itself — because a run without one sends nothing else. A command that sent nothing else the site's activity records — `summary`, `vulns`, `next`, `guide`, or any command in a copy without the key — then POSTs `monitor/pulse/run/<your site uuid>` once as it finishes, with no credential: the same three headers and a body of only its exit code (`{"exit_code": 0}`). Nothing about the project, its files or its packages is sent. These are names only — never arguments, paths or values — so the site's activity can show which run reached Patchstack.
Every request the CLI sends names the command that sent it (`X-Patchstack-Command`: `setup`, `scan`, `map`, `mark-build`, `dev` and so on) and what started it (`X-Patchstack-Trigger`: `install`, `build` or `other`). When the CLI runs under a coding agent it recognises, it also names that agent (`X-Patchstack-Agent`: `claude`, `codex`, `cursor`, `gemini`, `copilot`, `opencode` or `amp`), read from the variables the agent sets on the commands it runs; only the name is sent, never a variable's value. The claim-page check (`monitor/claim/preview`) also says whether the run holds the site's key (`X-Patchstack-Key`: `present` or `none`) — never the key itself — because a run without one sends nothing else. A command that sent nothing else the site's activity records — `summary`, `vulns`, `next`, `guide`, or any command in a copy without the key — then POSTs `monitor/pulse/run/<your site uuid>` once as it finishes, with no credential: the same three headers and a body of only its exit code (`{"exit_code": 0}`). Nothing about the project, its files or its packages is sent. These are names only — never arguments, paths or values — so the site's activity can show which run reached Patchstack.

It is server-only. Never put it in the widget tag, client bundles, or public env vars (`NEXT_PUBLIC_*`, etc.). Prefer `PATCHSTACK_API_KEY` in production; the git-ignored `.patchstackrc.local.json` is fine for local DX. If it is lost, `npx @patchstack/connect login` recovers it via dashboard approval — do not delete the file and re-provision, which would create a second site. Opt out of reporting with `PATCHSTACK_TELEMETRY=off`. If the project must not carry the widget, persist `"widget": false` in `.patchstackrc.json`; otherwise the next scan re-adds it.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ The site UUID identifies the site and is **not** a secret — the Patchstack Con

If it is ever lost, `npx @patchstack/connect login` recovers it — approval happens in the dashboard and rotates the credential.

The credential's file is never committed, so CI needs `PATCHSTACK_API_KEY` in the environment (and `PATCHSTACK_SITE_UUID` too where `.patchstackrc.json` is also absent). A production build that has the site UUID but no key — a hosted builder's publish, typically — reports only that the build ran: it POSTs the manifest checksum to `monitor/pulse/build/<uuid>` without a credential and without the package list or map, and Patchstack records it against the package list it last received with a key. Without a key, nothing else is sent to an existing site. Every CLI request names its command and trigger in `X-Patchstack-Command` and `X-Patchstack-Trigger` headers (names only), and the claim-page check says whether the run holds a key (`X-Patchstack-Key: present|none`, never the key). A command that sent nothing else the site's activity records POSTs `monitor/pulse/run/<uuid>` once as it finishes, keyless, with those headers and only its exit code. Precedence is CLI flag → env var → `.patchstackrc.local.json` → `.patchstackrc.json`.
The credential's file is never committed, so CI needs `PATCHSTACK_API_KEY` in the environment (and `PATCHSTACK_SITE_UUID` too where `.patchstackrc.json` is also absent). A production build that has the site UUID but no key — a hosted builder's publish, typically — reports only that the build ran: it POSTs the manifest checksum to `monitor/pulse/build/<uuid>` without a credential and without the package list or map, and Patchstack records it against the package list it last received with a key. Without a key, nothing else is sent to an existing site. Every CLI request names its command and trigger in `X-Patchstack-Command` and `X-Patchstack-Trigger` headers (names only), and the coding agent running it, when one is recognised, in `X-Patchstack-Agent` (`claude`, `codex`, `cursor` and so on), and the claim-page check says whether the run holds a key (`X-Patchstack-Key: present|none`, never the key). A command that sent nothing else the site's activity records POSTs `monitor/pulse/run/<uuid>` once as it finishes, keyless, with those headers and only its exit code. Precedence is CLI flag → env var → `.patchstackrc.local.json` → `.patchstackrc.json`.

A `pulseAuth` field is still read if present, and `PATCHSTACK_PULSE_AUTH` still overrides, for deployments that authenticate Patchstack for JavaScript ingest with a different credential from block-logs. Neither is written by default, and neither is needed when the two share one.

Expand Down
34 changes: 32 additions & 2 deletions src/report-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ export function reportTrigger(env: EnvLike = process.env): ReportTrigger {
return 'other';
}

/** The coding agents Connect can recognise from the environment of the commands they run. */
export type CodingAgent = 'amp' | 'claude' | 'codex' | 'copilot' | 'cursor' | 'gemini' | 'opencode';

/**
* The coding agent this CLI runs under, read from the variables agents set on the commands they start,
* or null when none is recognised. Only the agent's name leaves the machine, never a variable's value.
*
* Order matters where agents set each other's markers: Amp also sets `CLAUDECODE`, so it is checked
* before Claude Code. A child process inherits these variables, so this says an agent started the
* process tree, not that nobody is watching.
*/
export function codingAgent(env: EnvLike = process.env): CodingAgent | null {
const set = (name: string): boolean => {
const value = env[name];
return value !== undefined && value !== '' && value !== '0' && value.toLowerCase() !== 'false';
};

if (env.AGENT === 'amp' || set('AMP_CURRENT_THREAD_ID')) return 'amp';
if (set('CODEX_THREAD_ID') || set('CODEX_CI') || set('CODEX_SANDBOX')) return 'codex';
if (set('GEMINI_CLI')) return 'gemini';
if (set('COPILOT_CLI') || set('COPILOT_AGENT_SESSION_ID')) return 'copilot';
if (set('OPENCODE')) return 'opencode';
if (set('CLAUDECODE') || set('CLAUDE_CODE')) return 'claude';
if (set('CURSOR_AGENT')) return 'cursor';
return null;
}

let runCommand: string | null = null;

/** The CLI command this process runs, named on each request so the site's activity log can show it. */
Expand All @@ -30,14 +57,17 @@ export function setRunCommand(command: string | null): void {
}

/**
* Headers every request from the CLI carries: the client, the command that sent it and what started
* that command (an install, a build, or something else). Names only — never arguments or values.
* Headers every request from the CLI carries: the client, the command that sent it, what started that
* command (an install, a build, or something else) and the coding agent running it, when one is
* recognised. Names only — never arguments or values.
*/
export function connectHeaders(env: EnvLike = process.env): Record<string, string> {
const agent = codingAgent(env);
return {
'User-Agent': '@patchstack/connect',
...(runCommand !== null ? { 'X-Patchstack-Command': runCommand } : {}),
'X-Patchstack-Trigger': reportTrigger(env),
...(agent !== null ? { 'X-Patchstack-Agent': agent } : {}),
};
}

Expand Down
31 changes: 30 additions & 1 deletion tests/report-context.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';

import { buildManifestBody } from '../src/client.js';
import { reportTrigger } from '../src/report-context.js';
import { codingAgent, connectHeaders, reportTrigger } from '../src/report-context.js';
import type { Config } from '../src/types.js';

const config = {
Expand Down Expand Up @@ -39,3 +39,32 @@ describe('buildManifestBody report context', () => {
expect(buildManifestBody(config, payload, {}).trigger).toBe('other');
});
});

describe('codingAgent', () => {
it('names the agent from the variable it sets on the commands it runs', () => {
expect(codingAgent({ CLAUDECODE: '1' })).toBe('claude');
expect(codingAgent({ CODEX_THREAD_ID: 'thread' })).toBe('codex');
expect(codingAgent({ CODEX_CI: '1' })).toBe('codex');
expect(codingAgent({ CURSOR_AGENT: '1' })).toBe('cursor');
expect(codingAgent({ GEMINI_CLI: '1' })).toBe('gemini');
});

it('reads Amp as Amp although it also sets the Claude Code marker', () => {
expect(codingAgent({ AGENT: 'amp', CLAUDECODE: '1' })).toBe('amp');
});

it('reads Codex inside a Claude Code terminal as Codex, the agent that ran the command', () => {
expect(codingAgent({ CLAUDECODE: '1', CODEX_CI: '1' })).toBe('codex');
});

it('names no agent for a person in a plain terminal or a switched-off marker', () => {
expect(codingAgent({})).toBeNull();
expect(codingAgent({ CLAUDECODE: '0', CURSOR_AGENT: '' })).toBeNull();
expect(codingAgent({ CURSOR_TRACE_ID: 'trace' })).toBeNull();
});

it('sends the agent name, and only when one is recognised', () => {
expect(connectHeaders({ CLAUDECODE: '1' })['X-Patchstack-Agent']).toBe('claude');
expect(connectHeaders({})).not.toHaveProperty('X-Patchstack-Agent');
});
});