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
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).

### Added

- **`[context]` can declare the IDENTITY a repo must use, and kit asserts it** (#503). `kit check`
printed `✓ vercel authenticated <account>` and compared it to nothing, while `kit context check`
asserted only ids read from repo-local files — so a CLI answering as the wrong account was
green. New fields: `vercel.user`, `github.user` (distinct from `github.org`, which is the
remote's owner) and `convex.account`. They behave exactly like `git.email`: declared → asserted,
mismatch → red and non-zero, unreadable → `unknown` rather than a mismatch, since "cannot tell"
and "wrong account" are different findings.

What the gap cost, measured: a session whose CLI was logged in as a personal account with
read-only rights on the production environment read a FILTERED variable list as a complete one.
`env ls` showed four variables and looked whole (the ones that also existed in preview), the
production-only ones were invisible, and `env pull --environment=production` failed as though
the variable did not exist. Two contradictory conclusions were drawn before a web UI settled it.

A mismatch now also names the mechanism that scopes an identity to a repo, because a global
login is what produces a wrong one: `vercel -Q <dir>` / `VERCEL_TOKEN`, `gh auth switch`,
`gcloud config configurations activate`, `AWS_PROFILE`, stripe project profiles — and for
convex, that it has **no** profiles (`~/.convex/config.json` is global and `convex login`
overwrites it), so the isolation is `CONVEX_DEPLOY_KEY` / `CONVEX_DEPLOYMENT` per repo.

And it says the part that turns a wrong identity into wrong conclusions out loud: *anything you
already read as this identity may have been a PARTIAL view — permissions filter listings
silently.*

`kit context check`'s ready-to-paste block offers the detected identities, hedged (`⚠ the gh
account logged in NOW — VERIFY it is right for THIS repo`), since the live identity is exactly
what the lock exists to question. A value kit could not resolve to a real identity is reported
but never offered as something to declare.

### Added

- **`kit tools list` — the inventory that did not exist**, and the measurement three surfaces
were missing (#500). Per tool: the resolved path, the installer that owns it (classified from
the path, never assumed), the installed version, and with `--latest` how far behind it is.
Expand Down
1 change: 1 addition & 0 deletions docs/COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ scope/RoE, so a profile can move to a fresh host without trusting the transport.
| `kit ci [--strict] [--attest]` | One-shot CI gate (check + design + tests). Scanner-health gate: a crashed / missing / token-less scanner can no longer exit 0 (default warns); `--strict` / `KIT_CI_STRICT=1` (or `[governance.scan].required_scanners`) hard-fails any non-running scanner. |
| `kit context [--format json]` | Print kit context for agent introspection. |
| `kit context check [--json] [--require-declaration]` | Verify the live CLI state against `[context]` — each tool's declared (account, project) pair, never inferred from whatever is logged in. With no `[context]` block it prints a ready-to-paste one from the detected state and exits 0; `--require-declaration` makes that absence a **failure** instead, which is what the `context-check` pre-push hook runs — a gate with nothing to compare against is a check that could not run, not a pass. |
| `[context.vercel].user` · `[context.github].user` · `[context.convex].account` | The **identity** each CLI must be logged in as — asserted like `git.email`, not merely reported. A mismatch is red and exits non-zero, and names the per-tool mechanism that scopes an identity to a repo (`vercel -Q`, `gh auth switch`, `gcloud config configurations activate`; convex has none, so `CONVEX_DEPLOY_KEY` per repo). An unreadable identity is `unknown`, never a mismatch. Why it matters: when a command's output depends on the caller's role, a wrong identity returns a filtered subset that looks complete. |
| `kit create-plugin <name>` | Scaffold a new adapter. |
| `kit add <service>` · `kit add --list` | Add service to `.kit.toml`; `--list` prints every available adapter (so does bare `kit add`, which exits 1 as a usage error). |
| `kit version` | Print kit version + exit. |
Expand Down
20 changes: 20 additions & 0 deletions src/commands/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ async function cmdContextCheck(): Promise<boolean> {
console.log(
`\n${c.red}${mismatches.length} mismatch(es) — you are NOT locked to the declared account/project. Do not run outward/destructive commands until this is green.${c.reset}`,
);
// An identity mismatch is not fixed by logging in again — a global login is what produced
// it. Name the per-tool mechanism that scopes an identity to a repo (#503).
const { identityRemediation } = await import("../context-lock.js");
const seen = new Set<string>();
for (const f of mismatches) {
if (f.field !== "user" && f.field !== "account") continue;
const hint = identityRemediation(f.tool);
if (hint && !seen.has(hint)) {
seen.add(hint);
console.log(` ${c.dim}${hint}${c.reset}`);
}
}
// A wrong identity does not merely mean "wrong account": the output of everything you ran
// as it may have been a filtered subset, which is how this defect cost two wrong
// conclusions in a row.
if (mismatches.some((f) => f.field === "user" || f.field === "account")) {
console.log(
` ${c.yellow}Anything you already read as this identity may have been a PARTIAL view — permissions filter listings silently.${c.reset}`,
);
}
}
// Mismatch = non-zero exit so this can gate a hook / agent before acting.
// Unknown (tool absent / unreadable) does not block.
Expand Down
26 changes: 22 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,17 @@ export interface EnvOverride {
*/
export interface ContextConfig {
gcloud?: { account?: string; project?: string; config?: string; region?: string };
vercel?: { team?: string; project?: string };
github?: { org?: string; remote?: string };
/**
* `user` is the IDENTITY the CLI must be logged in as — declared and asserted like
* `gcloud.account` already was. Reporting an identity without comparing it is how a session
* ran as a personal account with read-only rights on the production environment and read a
* FILTERED variable list as a complete one, with `kit check` green throughout (#503).
*/
vercel?: { team?: string; project?: string; user?: string };
/** `user` is the logged-in gh account, as distinct from `org` (the remote's owner). */
github?: { org?: string; remote?: string; user?: string };
/** Convex has no profiles: `~/.convex/config.json` is global and `convex login` overwrites it. */
convex?: { account?: string };
gitlab?: { group?: string; remote?: string };
bitbucket?: { workspace?: string; remote?: string };
/** SSH identity this repo must push/deploy with. Declare any of these. */
Expand Down Expand Up @@ -815,13 +824,22 @@ export const kitConfigSchema = z
.passthrough()
.optional(),
vercel: z
.object({ team: z.string().optional(), project: z.string().optional() })
.object({
team: z.string().optional(),
project: z.string().optional(),
user: z.string().optional(),
})
.passthrough()
.optional(),
github: z
.object({ org: z.string().optional(), remote: z.string().optional() })
.object({
org: z.string().optional(),
remote: z.string().optional(),
user: z.string().optional(),
})
.passthrough()
.optional(),
convex: z.object({ account: z.string().optional() }).passthrough().optional(),
gitlab: z
.object({ group: z.string().optional(), remote: z.string().optional() })
.passthrough()
Expand Down
178 changes: 178 additions & 0 deletions src/context-identity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* Identity is now asserted, not merely printed.
*
* `kit check` reported `✓ vercel authenticated <account>` and compared it to nothing, while
* `kit context check` asserted only ids from repo-local files. The gap cost real work: a session
* whose CLI was logged in as a personal account with read-only rights on the production
* environment read a FILTERED variable list as a complete one — `env ls` showed four variables
* and looked whole, `env pull --environment=production` failed as if the variable did not exist,
* and two contradictory conclusions were drawn before a web UI settled it (#503).
*
* The tests below pin the three properties that make the fix worth having: a declared identity
* that differs is a MISMATCH (red, non-zero), an identity that cannot be read is UNKNOWN rather
* than a mismatch ("cannot tell" ≠ "wrong account"), and a mismatch tells you the per-tool
* mechanism that scopes an identity to a repo — because "log in again" is what created the
* problem.
*/

import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import {
compareContext,
identityRemediation,
normaliseIdentity,
readConvexAccount,
suggestContextToml,
type LiveContext,
} from "./context-lock.js";

describe("identity assertion", () => {
it("a declared identity that differs is a mismatch, not a note", () => {
const findings = compareContext(
{ vercel: { user: "deploy-bot" }, github: { user: "octocat" } },
{
vercel: { orgId: null, projectId: null, user: "someones-personal-account" },
github: { org: null, remote: null, user: "octocat" },
} as LiveContext,
);
const vercel = findings.find((f) => f.tool === "vercel" && f.field === "user");
const github = findings.find((f) => f.tool === "github" && f.field === "user");
assert.equal(vercel?.status, "mismatch");
assert.equal(vercel?.expected, "deploy-bot");
assert.equal(vercel?.actual, "someones-personal-account");
assert.equal(github?.status, "ok");
});

it("an unreadable identity is unknown — cannot tell is not wrong account", () => {
const findings = compareContext({ vercel: { user: "deploy-bot" } }, {
vercel: { orgId: null, projectId: null, user: null },
} as LiveContext);
const f = findings.find((x) => x.tool === "vercel" && x.field === "user");
assert.equal(f?.status, "unknown");
assert.equal(f?.actual, null);
});

it("an undeclared identity is not checked at all", () => {
const findings = compareContext({ vercel: { team: "team_x" } }, {
vercel: { orgId: "team_x", projectId: null, user: "whoever" },
} as LiveContext);
assert.equal(
findings.some((f) => f.field === "user"),
false,
"nothing declared, nothing asserted",
);
});

it("convex is checked as an account, since it has no profiles to scope", () => {
const findings = compareContext({ convex: { account: "ops@example.test" } }, {
convex: { account: "someone-else@example.test" },
} as LiveContext);
const f = findings.find((x) => x.tool === "convex");
assert.equal(f?.status, "mismatch");
});
});

describe("normaliseIdentity", () => {
it("takes the answer, not the CLI's chatter", () => {
assert.equal(normaliseIdentity("Retrieving scope…\nacme-team\n"), "acme-team");
assert.equal(normaliseIdentity(" octocat "), "octocat");
});

it("returns null for no answer, an error, or a not-authenticated notice", () => {
assert.equal(normaliseIdentity(null), null);
assert.equal(normaliseIdentity(""), null);
assert.equal(normaliseIdentity("Error: not authenticated"), null);
assert.equal(normaliseIdentity("You are not authenticated. Run login."), null);
});

it("refuses an implausibly long line rather than reporting a paragraph as an identity", () => {
assert.equal(normaliseIdentity("x".repeat(300)), null);
});
});

describe("readConvexAccount", () => {
let home = "";
const withConfig = (body: unknown): string => {
home = mkdtempSync(join(tmpdir(), "kit-convex-"));
mkdirSync(join(home, ".convex"), { recursive: true });
writeFileSync(join(home, ".convex", "config.json"), JSON.stringify(body));
return home;
};

it("reads the identity out of the global config", () => {
const h = withConfig({ email: "ops@example.test" });
try {
assert.equal(readConvexAccount(h), "ops@example.test");
} finally {
rmSync(h, { recursive: true, force: true });
}
});

it("says logged-in-but-unreadable rather than nothing when only a token is present", () => {
const h = withConfig({ accessToken: "secret" });
try {
const got = readConvexAccount(h);
assert.match(String(got), /logged in/);
assert.doesNotMatch(String(got), /secret/, "never echo the token");
} finally {
rmSync(h, { recursive: true, force: true });
}
});

it("returns null when there is no config to read", () => {
const h = mkdtempSync(join(tmpdir(), "kit-convex-empty-"));
try {
assert.equal(readConvexAccount(h), null);
} finally {
rmSync(h, { recursive: true, force: true });
}
});
});

describe("identityRemediation", () => {
it("names the per-tool mechanism, never 'log in again'", () => {
assert.match(String(identityRemediation("vercel")), /-Q|VERCEL_TOKEN/);
assert.match(String(identityRemediation("github")), /gh auth switch/);
assert.match(String(identityRemediation("gcloud")), /configurations activate/);
for (const tool of ["vercel", "github", "gcloud", "aws", "stripe"]) {
assert.doesNotMatch(
String(identityRemediation(tool)),
/^log in again/i,
`${tool}: a global login is what produced the wrong identity`,
);
}
});

it("says outright that convex has no profiles, instead of inventing one", () => {
const hint = String(identityRemediation("convex"));
assert.match(hint, /NO profiles/);
assert.match(hint, /CONVEX_DEPLOY_KEY/);
});

it("has nothing to say about a tool with no mechanism", () => {
assert.equal(identityRemediation("jq"), null);
});
});

describe("suggestContextToml", () => {
it("offers the identities it detected, hedged — they are what the lock questions", () => {
const out = suggestContextToml({
github: { org: "acme", remote: "github.com/acme/x", user: "octocat" },
vercel: { orgId: "team_1", projectId: "prj_1", user: "octocat" },
} as LiveContext);
assert.match(out, /user = "octocat"/);
assert.match(out, /VERIFY it is right for THIS repo/);
});

it("does not offer a parenthetical note as a value to declare", () => {
const out = suggestContextToml({
convex: { account: "(logged in, identity not in config)" },
} as LiveContext);
assert.doesNotMatch(out, /logged in, identity not in config/);
assert.doesNotMatch(out, /\[context\.convex\]/);
});
});
Loading
Loading