Skip to content
Open
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
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ headlines describe the result without internal outcome names or numeric codes.
`--json` retains the stable envelope, including `outcome` and `exit_code`.
Color follows terminal detection and `NO_COLOR`; piped output stays plain.

Terminal help and version output can show a cached update notice. At most once per
24 hours, a background worker checks npm's public `latest` tag; the command never
waits for the network. A newly discovered version appears on the next run. Failed
checks are silent and cached for the same interval. JSON, piped output and CI skip
both the notice and check. Set `METERGRAPH_NO_UPDATE_CHECK=1` to disable them.
The cache is `$XDG_CACHE_HOME/metergraph/update.json` (default
`~/.cache/metergraph/update.json`), or `%LOCALAPPDATA%\Metergraph\Cache\update.json`
on Windows. The check sends no project data or credentials.

The
[CLI reference](https://github.com/metergraph/cli/blob/main/docs/reference.md) covers every option, what each command reads and
writes, and full JSON examples.
Expand Down Expand Up @@ -265,7 +274,8 @@ Exit codes are stable. Changing one is a breaking change. Codes 10 to 18 are not
## Security and privacy

- **No usage telemetry.** The CLI collects no analytics about how you use it. It talks
only to the Metergraph origin you point it at.
to the Metergraph origin you point it at. The optional update check contacts
npm's public registry for this package's release tags.
- **Least access.** Sign in grants only the Metadata scope, `agent:metadata`. The CLI
never requests Debug or Replay access, never falls back to broader access, and the read
commands never read retained prompts, responses or other content, whichever credential
Expand All @@ -288,7 +298,8 @@ Exit codes are stable. Changing one is a breaking change. Codes 10 to 18 are not
Public pricing lookup/calculation use unauthenticated `POST` with metadata-only
inputs. Fixed paths, no redirects followed,
bounded response sizes and fixed timeouts. Mistyped arguments are never echoed, since
they may hold a secret. `--help`, `--version` and `skill` make no network requests.
they may hold a secret. `skill` makes no network requests. Help and version work offline; interactive
output may start the optional background update check described above.

The [CLI reference](https://github.com/metergraph/cli/blob/main/docs/reference.md) states what each command does and does not do.
Report vulnerabilities privately through
Expand Down
13 changes: 11 additions & 2 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,17 @@ metergraph experiments start --workload ID --treatment SPEC [--treatment SPEC]..
metergraph experiments get --id UUID [read options] [--json]
```

`--help`, `--version` and the `skill` commands work offline and make no network
requests. `login`, `logout`, `setup`, `verify` and the read commands are not in the published `0.1.0`
`--help` and `--version` work offline. For terminal output only, they may show a
cached update notice on stderr and launch a background check of npm's public
`latest` tag. Checks, including failed attempts, are cached for 24 hours and have
a 1.5-second network deadline; the foreground command does not wait. First-run
results appear on the next invocation. No project data or credentials are sent.
JSON, piped output and CI skip this behavior. Set `METERGRAPH_NO_UPDATE_CHECK=1`
to disable it entirely. Cache location: `$XDG_CACHE_HOME/metergraph/update.json`
(default `~/.cache/metergraph/update.json`), or
`%LOCALAPPDATA%\Metergraph\Cache\update.json` on Windows.

The `skill` commands work offline and make no network requests. `login`, `logout`, `setup`, `verify` and the read commands are not in the published `0.1.0`
package.

### Exact trace verification
Expand Down
2 changes: 2 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { maybeUpdateNotice } from "./update-notice.js";
import { runPricing } from "./pricing.js";
import { pricingText, pricingHtml } from "./pricing-output.js";
import { READ_COMMANDS, parseArgs } from "./args.js";
Expand Down Expand Up @@ -58,6 +59,7 @@ export async function main(argv, { stdout, stderr, stdin = null }) {
if (json) stdout.write(toJsonLine(result));
else stderr.write(`Error: ${result.error.message}\n`);
}
if (result.ok) maybeUpdateNotice({ command: result.command, json, stdout, stderr });
return result.exit_code;
}

Expand Down
159 changes: 159 additions & 0 deletions src/update-notice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { spawn } from "node:child_process";
import { randomUUID } from "node:crypto";
import { fileURLToPath } from "node:url";
import { VERSION } from "./constants.js";
import { createStyle } from "./style.js";

export const UPDATE_TTL_MS = 24 * 60 * 60 * 1000;
const WORKER = fileURLToPath(import.meta.url);
const TAGS_URL = "https://registry.npmjs.org/-/package/metergraph-cli/dist-tags";
const STABLE = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;

// Only stable versions from npm's latest tag can appear in terminal output.
function stableParts(version) {
if (typeof version !== "string" || version.length > 64 || !STABLE.test(version)) return null;
const parts = version.split(".").map(Number);
return parts.every(Number.isSafeInteger) ? parts : null;
}

export function isNewer(latest, current) {
const target = stableParts(latest);
const match = typeof current === "string" && current.match(/^(\d+\.\d+\.\d+)(-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/);
const installed = match && stableParts(match[1]);
if (!target || !installed) return false;
for (let i = 0; i < 3; i++) {
if (target[i] !== installed[i]) return target[i] > installed[i];
}
return Boolean(match[2]);
}

export function updateCacheDir(env = process.env) {
if (process.platform === "win32") {
const base = env.LOCALAPPDATA && path.isAbsolute(env.LOCALAPPDATA) ? env.LOCALAPPDATA : path.join(os.homedir(), "AppData", "Local");
return path.join(base, "Metergraph", "Cache");
}
const base = env.XDG_CACHE_HOME && path.isAbsolute(env.XDG_CACHE_HOME) ? env.XDG_CACHE_HOME : path.join(os.homedir(), ".cache");
return path.join(base, "metergraph");
}

function readCache(dir) {
let fd;
try {
fd = fs.openSync(path.join(dir, "update.json"), fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0) | (fs.constants.O_NONBLOCK ?? 0));
const stat = fs.fstatSync(fd);
if (!stat.isFile() || stat.size > 4096) return null;
const data = JSON.parse(fs.readFileSync(fd, "utf8"));
if (!Number.isSafeInteger(data.checked_at) || data.checked_at < 0) return null;
if (data.latest !== null && !stableParts(data.latest)) return null;
return data;
} catch {
return null;
} finally {
if (fd !== undefined) fs.closeSync(fd);
}
}

function fresh(cache, now) {
return cache && cache.checked_at <= now && now - cache.checked_at < UPDATE_TTL_MS;
}

function startRefresh() {
const child = spawn(process.execPath, [WORKER], { detached: true, stdio: "ignore", windowsHide: true });
child.on("error", () => {});
child.unref();
}

// The foreground only reads a tiny cache. A detached worker does all network
// work; the first successful refresh can produce a notice on the next run.
export function maybeUpdateNotice({ command, json, stdout, stderr }, {
env = process.env, current = VERSION, now = Date.now(), dir = updateCacheDir(env), refresh = startRefresh,
} = {}) {
try {
if (!["help", "version"].includes(command) || json || !stdout?.isTTY || !stderr?.isTTY
|| env.METERGRAPH_NO_UPDATE_CHECK || (env.CI && !["0", "false"].includes(env.CI.toLowerCase()))) return;
const cache = readCache(dir);
if (!fresh(cache, now)) {
refresh();
return;
}
if (isNewer(cache.latest, current)) {
const style = createStyle(stderr, env);
stderr.write(`\n${style.cyan(`Update available: ${current} → ${cache.latest}`)}\nRun npm install -g metergraph-cli@latest\n`);
}
} catch {
// Optional presentation must never change the command result.
}
}

// Fixed public endpoint: no project settings, credentials, or configured npm
// registry are read. Bound the response, deadline and redirects independently.
export async function latestVersion(fetcher = fetch) {
const signal = AbortSignal.timeout(1500);
const response = await fetcher(TAGS_URL, { signal, redirect: "error", credentials: "omit", headers: { accept: "application/json" } });
if (!response.ok) {
await response.body?.cancel();
return null;
}
const reader = response.body.getReader();
const chunks = [];
let size = 0;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
size += value.byteLength;
if (size > 4096) return null;
chunks.push(Buffer.from(value));
}
const data = JSON.parse(Buffer.concat(chunks).toString("utf8"));
return stableParts(data.latest) ? data.latest : null;
} finally {
await reader.cancel();
}
}

function writeCache(dir, data) {
const temp = path.join(dir, `update-${randomUUID()}.tmp`);
try {
fs.writeFileSync(temp, JSON.stringify(data), { mode: 0o600, flag: "wx" });
fs.renameSync(temp, path.join(dir, "update.json"));
} finally {
try { fs.unlinkSync(temp); } catch { /* Renamed or unwritable. */ }
}
}

export async function refreshUpdateCache({ dir = updateCacheDir(), now = Date.now(), load = latestVersion } = {}) {
let locked = false;
const lock = path.join(dir, "update.lock");
try {
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
if (!fs.lstatSync(dir).isDirectory()) return;
try {
fs.mkdirSync(lock, { mode: 0o700 });
} catch {
// Recover a worker killed before cleanup, without removing live locks.
const stat = fs.lstatSync(lock);
if (!stat.isDirectory() || now - stat.mtimeMs < 60000) return;
fs.rmdirSync(lock);
fs.mkdirSync(lock, { mode: 0o700 });
}
locked = true;
if (fresh(readCache(dir), now)) return;
// Failures are cached too: an offline terminal does not spawn a request on
// every help invocation. A successful refresh replaces this same record.
writeCache(dir, { checked_at: now, latest: null });
const latest = await load();
if (stableParts(latest)) writeCache(dir, { checked_at: now, latest });
} catch {
// Missing network or an unwritable cache is not a CLI error.
} finally {
if (locked) { try { fs.rmdirSync(lock); } catch { /* Best effort. */ } }
}
}

if (process.argv[1] && path.resolve(process.argv[1]) === WORKER) {
if (!process.env.METERGRAPH_NO_UPDATE_CHECK) await refreshUpdateCache();
}
1 change: 1 addition & 0 deletions test/package/pack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const EXPECTED_FILES = [
"src/trace-contract.js",
"src/trace-open.js",
"src/transport.js",
"src/update-notice.js",
"src/usage-aggregates.js",
"src/verify-output.js",
"src/verify.js",
Expand Down
Loading
Loading