|
1 | 1 | import { uncoveredStatuses } from "../hookdeck/provision.js"; |
2 | 2 | import { RETRYABLE_STATUS_CODES } from "../protocol/outcome.js"; |
| 3 | +import { |
| 4 | + defaultCliConfigPath, |
| 5 | + readCliProject, |
| 6 | +} from "../transport/cli-project.js"; |
3 | 7 | import { type ToolDeps } from "./deps.js"; |
4 | 8 | import { reportedPersistence } from "./status.js"; |
5 | 9 |
|
@@ -61,6 +65,15 @@ export async function doctorHandler(deps: ToolDeps) { |
61 | 65 | }); |
62 | 66 | } |
63 | 67 |
|
| 68 | + // In `cli` transport, "which project" has two independent answers: |
| 69 | + // provisioning acts on the API key's project, while `hookdeck listen` looks |
| 70 | + // for that connection in whichever project the CLI's session points at. When |
| 71 | + // they differ the Gateway reports healthy and receives nothing, so the only |
| 72 | + // thing standing between an operator and a silent outage is this check. |
| 73 | + if (deps.config.transport.mode === "cli") { |
| 74 | + checks.push(await projectMatchCheck(deps)); |
| 75 | + } |
| 76 | + |
64 | 77 | checks.push({ |
65 | 78 | name: "api key", |
66 | 79 | ok: deps.client !== undefined, |
@@ -105,3 +118,71 @@ export async function doctorHandler(deps: ToolDeps) { |
105 | 118 |
|
106 | 119 | return { ok: checks.every((c) => c.ok), checks }; |
107 | 120 | } |
| 121 | + |
| 122 | +interface Check { |
| 123 | + name: string; |
| 124 | + ok: boolean; |
| 125 | + detail: string; |
| 126 | +} |
| 127 | + |
| 128 | +async function projectMatchCheck(deps: ToolDeps): Promise<Check> { |
| 129 | + const name = "cli/api-key project"; |
| 130 | + |
| 131 | + const cli = await readCliProject( |
| 132 | + deps.config.transport.cliConfigPath ?? defaultCliConfigPath(), |
| 133 | + deps.readFile ?? |
| 134 | + (async (p) => (await import("node:fs/promises")).readFile(p, "utf8")), |
| 135 | + ); |
| 136 | + |
| 137 | + if (cli.projectId === undefined) { |
| 138 | + // Not a mismatch. A CLI with no session is its own failure — `hookdeck |
| 139 | + // listen` cannot start at all — and reporting it as a mismatch would point |
| 140 | + // at the wrong fix. |
| 141 | + return { |
| 142 | + name, |
| 143 | + ok: true, |
| 144 | + detail: |
| 145 | + cli.reason === "no_config" |
| 146 | + ? "unverified — no Hookdeck CLI config found, so the CLI's project is unknown. Run `hookdeck login`." |
| 147 | + : "unverified — the CLI config holds no project, so no session is logged in. Run `hookdeck login`.", |
| 148 | + }; |
| 149 | + } |
| 150 | + |
| 151 | + if (deps.client === undefined) { |
| 152 | + return { |
| 153 | + name, |
| 154 | + ok: true, |
| 155 | + detail: `unverified — the CLI forwards from ${cli.projectId}, but with no API key there is nothing to compare it against`, |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + const connections = await deps.client.listConnections(1); |
| 160 | + if (!connections.ok) { |
| 161 | + return { name, ok: true, detail: `unverified — ${connections.message}` }; |
| 162 | + } |
| 163 | + |
| 164 | + const apiProject = connections.data[0]?.team_id; |
| 165 | + if (apiProject === undefined) { |
| 166 | + // A project can legitimately be empty, and sending someone to fix that |
| 167 | + // would be worse than saying nothing. |
| 168 | + return { |
| 169 | + name, |
| 170 | + ok: true, |
| 171 | + detail: `unverified — the API key reaches no connections, so its project cannot be read. The CLI forwards from ${cli.projectId}.`, |
| 172 | + }; |
| 173 | + } |
| 174 | + |
| 175 | + if (apiProject === cli.projectId) { |
| 176 | + return { name, ok: true, detail: `both ${apiProject}` }; |
| 177 | + } |
| 178 | + |
| 179 | + return { |
| 180 | + name, |
| 181 | + ok: false, |
| 182 | + detail: |
| 183 | + `MISMATCH: the CLI forwards from ${cli.projectId} but the API key acts on ${apiProject}. ` + |
| 184 | + `hookdeck_setup creates connections in the API key's project while \`hookdeck listen\` looks ` + |
| 185 | + `for them in the CLI's, so the Gateway will report healthy and receive nothing. Point the CLI ` + |
| 186 | + `at the same project with \`hookdeck login\`, or configure an API key belonging to ${cli.projectId}.`, |
| 187 | + }; |
| 188 | +} |
0 commit comments