@tangle-network/agent-app 0.45.29 → 0.45.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,190 @@
1
+ // src/alerting/slack.ts
2
+ var SLACK_API = "https://slack.com/api";
3
+ var DEFAULT_TIMEOUT_MS = 1e4;
4
+ var DEFAULT_ATTEMPTS = 3;
5
+ var CREDENTIAL_ERRORS = /* @__PURE__ */ new Set([
6
+ "invalid_auth",
7
+ "not_authed",
8
+ "account_inactive",
9
+ "token_revoked",
10
+ "token_expired",
11
+ "no_permission",
12
+ "missing_scope",
13
+ "ekm_access_denied"
14
+ ]);
15
+ var CHANNEL_ERRORS = /* @__PURE__ */ new Set([
16
+ "channel_not_found",
17
+ "not_in_channel",
18
+ "is_archived",
19
+ "restricted_action",
20
+ "restricted_action_read_only_channel"
21
+ ]);
22
+ function remedyFor(reason, error, channel) {
23
+ switch (reason) {
24
+ case "credential":
25
+ return `Slack rejected the bot token (${error}) \u2014 no alert can arrive until a human mints a new one. Reinstall the Slack app and update SLACK_BOT_TOKEN wherever it is stored.`;
26
+ case "channel":
27
+ return `Slack accepted the token but refused ${channel} (${error}) \u2014 invite the bot to ${channel}, or correct the channel name.`;
28
+ default:
29
+ return `Slack refused the post (${error}).`;
30
+ }
31
+ }
32
+ function classifySlackError(error, channel) {
33
+ const reason = CREDENTIAL_ERRORS.has(error) ? "credential" : CHANNEL_ERRORS.has(error) ? "channel" : "api";
34
+ return { delivered: false, reason, detail: remedyFor(reason, error, channel) };
35
+ }
36
+ function retryDelayMs(response, attempt) {
37
+ const header = Number(response.headers.get("retry-after"));
38
+ if (Number.isFinite(header) && header > 0) return Math.min(header * 1e3, 3e4);
39
+ return Math.min(500 * 2 ** (attempt - 1), 8e3);
40
+ }
41
+ var defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
42
+ async function postSlackAlert(options) {
43
+ const token = options.token?.trim();
44
+ const channel = options.channel?.trim();
45
+ const webhookUrl = options.webhookUrl?.trim();
46
+ if (!token && !webhookUrl) {
47
+ return {
48
+ delivered: false,
49
+ reason: "not-configured",
50
+ detail: "no Slack credential configured \u2014 set SLACK_BOT_TOKEN (preferred, verifiable) or SLACK_WEBHOOK_URL to route alerts to Slack"
51
+ };
52
+ }
53
+ if (token && !channel) {
54
+ return {
55
+ delivered: false,
56
+ reason: "not-configured",
57
+ detail: "a Slack bot token is set but no channel is \u2014 set the alert channel (e.g. #infra-alerts)"
58
+ };
59
+ }
60
+ const fetchImpl = options.fetchImpl ?? fetch;
61
+ const sleep = options.sleepImpl ?? defaultSleep;
62
+ const attempts = Math.max(1, options.attempts ?? DEFAULT_ATTEMPTS);
63
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
64
+ const transport = token ? { kind: "token", token, channel } : { kind: "webhook", url: webhookUrl };
65
+ let lastTransient = {
66
+ delivered: false,
67
+ reason: "transport",
68
+ detail: "Slack was never reached"
69
+ };
70
+ for (let attempt = 1; attempt <= attempts; attempt += 1) {
71
+ const controller = new AbortController();
72
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
73
+ let response;
74
+ try {
75
+ response = await fetchImpl(requestUrl(transport), {
76
+ method: "POST",
77
+ headers: requestHeaders(transport),
78
+ body: JSON.stringify(requestBody(transport, options.text)),
79
+ signal: controller.signal
80
+ });
81
+ } catch (error) {
82
+ lastTransient = {
83
+ delivered: false,
84
+ reason: "transport",
85
+ detail: `could not reach Slack: ${error instanceof Error ? error.message : String(error)}`
86
+ };
87
+ if (attempt < attempts) await sleep(Math.min(500 * 2 ** (attempt - 1), 8e3));
88
+ continue;
89
+ } finally {
90
+ clearTimeout(timer);
91
+ }
92
+ if (response.status === 429 || response.status >= 500) {
93
+ lastTransient = {
94
+ delivered: false,
95
+ reason: response.status === 429 ? "rate-limited" : "transport",
96
+ detail: `Slack returned ${response.status}`
97
+ };
98
+ if (attempt < attempts) await sleep(retryDelayMs(response, attempt));
99
+ continue;
100
+ }
101
+ return transport.kind === "token" ? await settleTokenResponse(response, transport.channel) : await settleWebhookResponse(response);
102
+ }
103
+ return lastTransient;
104
+ }
105
+ function requestUrl(transport) {
106
+ return transport.kind === "token" ? `${SLACK_API}/chat.postMessage` : transport.url;
107
+ }
108
+ function requestHeaders(transport) {
109
+ const headers = { "Content-Type": "application/json; charset=utf-8" };
110
+ if (transport.kind === "token") headers.Authorization = `Bearer ${transport.token}`;
111
+ return headers;
112
+ }
113
+ function requestBody(transport, text) {
114
+ return transport.kind === "token" ? { channel: transport.channel, text } : { text };
115
+ }
116
+ async function settleTokenResponse(response, channel) {
117
+ let body;
118
+ try {
119
+ body = await response.json();
120
+ } catch {
121
+ return {
122
+ delivered: false,
123
+ reason: "api",
124
+ detail: `Slack returned ${response.status} with an unreadable body`
125
+ };
126
+ }
127
+ if (body.ok === true) {
128
+ return { delivered: true, channel: body.channel ?? channel, ts: body.ts ?? "" };
129
+ }
130
+ return classifySlackError(body.error ?? `http_${response.status}`, channel);
131
+ }
132
+ async function settleWebhookResponse(response) {
133
+ const body = (await response.text().catch(() => "")).trim();
134
+ if (response.ok && body === "ok") {
135
+ return { delivered: true, channel: "(webhook)", ts: "" };
136
+ }
137
+ const error = body || `http_${response.status}`;
138
+ const reason = WEBHOOK_CREDENTIAL_ERRORS.has(error) ? "credential" : WEBHOOK_CHANNEL_ERRORS.has(error) ? "channel" : "api";
139
+ return {
140
+ delivered: false,
141
+ reason,
142
+ detail: reason === "credential" ? `the Slack webhook is revoked (${error}) \u2014 no alert can arrive until a human mints a new one. Prefer replacing it with a bot token, whose liveness can be checked before an alert needs it.` : reason === "channel" ? `Slack refused the webhook's channel (${error}) \u2014 the channel was archived, or the app lost access.` : `Slack refused the webhook post (${error}).`
143
+ };
144
+ }
145
+ var WEBHOOK_CREDENTIAL_ERRORS = /* @__PURE__ */ new Set(["no_service", "no_team", "invalid_token"]);
146
+ var WEBHOOK_CHANNEL_ERRORS = /* @__PURE__ */ new Set(["channel_not_found", "channel_is_archived", "action_prohibited"]);
147
+ async function checkSlackCredential(options) {
148
+ const token = options.token?.trim();
149
+ if (!token) {
150
+ return {
151
+ live: false,
152
+ reason: "not-configured",
153
+ detail: "no Slack bot token configured \u2014 set SLACK_BOT_TOKEN"
154
+ };
155
+ }
156
+ const fetchImpl = options.fetchImpl ?? fetch;
157
+ const controller = new AbortController();
158
+ const timer = setTimeout(() => controller.abort(), options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
159
+ try {
160
+ const response = await fetchImpl(`${SLACK_API}/auth.test`, {
161
+ method: "POST",
162
+ headers: { Authorization: `Bearer ${token}` },
163
+ signal: controller.signal
164
+ });
165
+ const body = await response.json();
166
+ if (body.ok === true) {
167
+ return { live: true, team: body.team ?? "unknown", botId: body.bot_id ?? "unknown" };
168
+ }
169
+ const outcome = classifySlackError(body.error ?? `http_${response.status}`, "(auth.test)");
170
+ return {
171
+ live: false,
172
+ reason: outcome.reason === "channel" ? "credential" : outcome.reason,
173
+ detail: outcome.detail
174
+ };
175
+ } catch (error) {
176
+ return {
177
+ live: false,
178
+ reason: "transport",
179
+ detail: `could not reach Slack: ${error instanceof Error ? error.message : String(error)}`
180
+ };
181
+ } finally {
182
+ clearTimeout(timer);
183
+ }
184
+ }
185
+
186
+ export {
187
+ postSlackAlert,
188
+ checkSlackCredential
189
+ };
190
+ //# sourceMappingURL=chunk-COP2K4LF.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/alerting/slack.ts"],"sourcesContent":["/**\n * `/alerting` — post an operational alert to Slack, and say honestly when it\n * did not arrive.\n *\n * WHY THIS EXISTS: on 2026-08-06 an audit of every Slack credential the org\n * held found three of four dead — the fleet incoming webhook 404ing\n * (`no_service`), a second company webhook likewise, and the bot token\n * answering `account_inactive`. One ops webhook was still live. Nobody knew\n * which was which until each was tested by hand, and that is the actual\n * problem: the credentials were indistinguishable from where the code stood.\n *\n * This failure has already been paid for once, with numbers: in\n * `agent-dev-container`, a revoked webhook let the CI healthcheck sit dead for\n * 18 days — 2,567 consecutive failures, 27 successes, zero alerts — because\n * fifteen call sites across six workflows posted with no body inspection and\n * then asserted delivery. That repo fixed the reporting (its `post-slack.sh`\n * confirms 2xx AND Slack's literal `ok` body, and fails closed otherwise) and\n * that half of the lesson is theirs, adopted here.\n *\n * What their fix cannot do, and this module can, is answer the question BEFORE\n * an alert needs to fire. A fail-closed post still only discovers a dead\n * credential at the moment a page is lost. That is the second half.\n *\n * Two design consequences, and they are the whole module:\n *\n * 1. **A bot token and `chat.postMessage` is the preferred transport; an\n * incoming webhook is supported because one is usually what you already\n * have.** A token reaches every channel (a webhook is bolted to one), is\n * revocable and rotatable in place, and — the part that matters — is\n * VERIFIABLE: `auth.test` answers whether the credential is alive without\n * posting anything, which is what lets `/preflight` fail a deploy on a dead\n * alerting channel instead of discovering it during an incident. A webhook\n * can only be tested by posting to it, which is why the three dead\n * credentials above went unnoticed. A token is never fallen back FROM: if\n * one is configured and dead, delivering over a webhook instead would hide\n * the very condition worth reporting.\n *\n * 2. **Slack answers `ok:false` under HTTP 200.** `invalid_auth`,\n * `channel_not_found` and `not_in_channel` all arrive as a successful\n * response with a failure inside it, so a status check reads a dead channel\n * as a delivered page. The body is always parsed, and the outcome\n * distinguishes a MISSING credential (configuration absent — not an\n * incident) from a DEAD one (the alerting channel itself is broken — the\n * loudest thing this module can report). A summary that calls those two the\n * same thing is the defect wearing a different hat.\n *\n * The caller decides what to do with a non-delivery; this module never throws\n * on one, because an alerting path that can take down the thing it reports on\n * is worse than the outage it was watching for. It is also never the only\n * channel: a durable record (an issue, an audit row) is the caller's job, and\n * that is what survives the credential going dead again.\n *\n * Server-only: holds a bot token. This subpath must never reach a browser\n * bundle.\n */\n\nconst SLACK_API = 'https://slack.com/api'\nconst DEFAULT_TIMEOUT_MS = 10_000\nconst DEFAULT_ATTEMPTS = 3\n\n/**\n * Why an alert did not reach Slack. The split is by what a human must DO about\n * it, since that is the only distinction a caller can act on.\n */\nexport type SlackFailureReason =\n /** No token or no channel configured. Configuration is absent, nothing is broken. */\n | 'not-configured'\n /**\n * The token is dead — revoked, or its app removed from the workspace. No\n * alert will EVER arrive until a human mints a new one. This is itself an\n * incident and the caller should escalate it on a channel that does not\n * depend on Slack.\n */\n | 'credential'\n /**\n * The credential is alive but cannot post HERE: the channel is wrong,\n * archived, or the bot was never invited to it. One human action fixes it.\n */\n | 'channel'\n /** Slack asked us to slow down. Transient; the alert is worth retrying. */\n | 'rate-limited'\n /** The request never got an answer — network, DNS, timeout. Transient. */\n | 'transport'\n /** Slack refused for some other reason; `detail` carries its error code. */\n | 'api'\n\n/** An alert that did not arrive, and why. Named because the classifiers only ever produce this half. */\nexport interface SlackAlertFailure {\n delivered: false\n reason: SlackFailureReason\n /** One line naming what is wrong and what fixes it. Safe to log; carries no token. */\n detail: string\n}\n\n/** What one `postSlackAlert` call did. Never a bare boolean — the caller pages differently per reason. */\nexport type SlackAlertOutcome =\n | {\n delivered: true\n /** The channel id Slack resolved (not necessarily the name that was passed). */\n channel: string\n /** Slack's message timestamp — the message's identity, for a later thread reply. */\n ts: string\n }\n | SlackAlertFailure\n\n/** Define configuration options for posting an alert message to a Slack channel */\nexport interface SlackAlertOptions {\n /**\n * Slack bot token (`xoxb-…`) with `chat:write`. The PREFERRED transport,\n * because it is the only one whose liveness can be checked before an alert\n * needs it. An empty or absent value is `not-configured`, never an error — a\n * product that has not adopted Slack yet must not fail its alerting path.\n */\n token?: string | undefined\n /**\n * Channel to post to: a name (`#infra-alerts`) or an id (`C01234567`). The\n * bot must be a member; Slack answers `not_in_channel` otherwise. Required\n * with `token`, meaningless with `webhookUrl` (a webhook carries its own\n * channel, fixed when it was created).\n */\n channel?: string | undefined\n /**\n * Slack incoming-webhook URL, used only when no `token` is configured.\n *\n * It works and it needs no setup, which is why it is supported — but it\n * cannot be verified without posting, cannot be pointed at a second channel,\n * and gives back an error token instead of a code. Two of the three webhooks\n * this org has held were found revoked. Treat it as the transport you have,\n * not the one you want.\n *\n * When a `token` is also configured this is IGNORED rather than used as a\n * fallback: a dead token must surface as the incident it is, and quietly\n * succeeding over a second transport is how the last outage stayed invisible.\n */\n webhookUrl?: string | undefined\n /** Message body as Slack mrkdwn. Newlines are preserved. */\n text: string\n /**\n * Attempts for a TRANSIENT failure (429 / 5xx / transport). Default 3. A\n * dead credential or a wrong channel is never retried — the answer will not\n * change, and retrying an auth failure is how a token gets rate-limited.\n */\n attempts?: number\n /** Per-request deadline. Default 10s. */\n timeoutMs?: number\n /** Injection seam for tests; defaults to global `fetch`. */\n fetchImpl?: typeof fetch\n /** Injection seam for tests; defaults to a real delay between retries. */\n sleepImpl?: (ms: number) => Promise<void>\n}\n\n/** Slack error codes that mean the CREDENTIAL is dead, not the request. */\nconst CREDENTIAL_ERRORS = new Set([\n 'invalid_auth',\n 'not_authed',\n 'account_inactive',\n 'token_revoked',\n 'token_expired',\n 'no_permission',\n 'missing_scope',\n 'ekm_access_denied',\n])\n\n/** Slack error codes that mean the credential is fine but this CHANNEL is not reachable. */\nconst CHANNEL_ERRORS = new Set([\n 'channel_not_found',\n 'not_in_channel',\n 'is_archived',\n 'restricted_action',\n 'restricted_action_read_only_channel',\n])\n\nfunction remedyFor(reason: SlackFailureReason, error: string, channel: string): string {\n switch (reason) {\n case 'credential':\n return (\n `Slack rejected the bot token (${error}) — no alert can arrive until a human mints a new one. ` +\n 'Reinstall the Slack app and update SLACK_BOT_TOKEN wherever it is stored.'\n )\n case 'channel':\n return (\n `Slack accepted the token but refused ${channel} (${error}) — ` +\n `invite the bot to ${channel}, or correct the channel name.`\n )\n default:\n return `Slack refused the post (${error}).`\n }\n}\n\nfunction classifySlackError(error: string, channel: string): SlackAlertFailure {\n const reason: SlackFailureReason = CREDENTIAL_ERRORS.has(error)\n ? 'credential'\n : CHANNEL_ERRORS.has(error)\n ? 'channel'\n : 'api'\n return { delivered: false, reason, detail: remedyFor(reason, error, channel) }\n}\n\n/** Slack's `Retry-After` is in SECONDS. Bounded so a hostile header cannot park the caller. */\nfunction retryDelayMs(response: Response, attempt: number): number {\n const header = Number(response.headers.get('retry-after'))\n if (Number.isFinite(header) && header > 0) return Math.min(header * 1000, 30_000)\n return Math.min(500 * 2 ** (attempt - 1), 8_000)\n}\n\nconst defaultSleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms))\n\n/**\n * Post one alert to a Slack channel.\n *\n * Never throws and never retries a failure whose answer cannot change. The\n * returned outcome is the whole result — a caller that ignores it has an\n * alerting path it cannot prove works, which is the failure this module was\n * written for.\n */\nexport async function postSlackAlert(options: SlackAlertOptions): Promise<SlackAlertOutcome> {\n const token = options.token?.trim()\n const channel = options.channel?.trim()\n const webhookUrl = options.webhookUrl?.trim()\n\n if (!token && !webhookUrl) {\n return {\n delivered: false,\n reason: 'not-configured',\n detail:\n 'no Slack credential configured — set SLACK_BOT_TOKEN (preferred, verifiable) ' +\n 'or SLACK_WEBHOOK_URL to route alerts to Slack',\n }\n }\n if (token && !channel) {\n return {\n delivered: false,\n reason: 'not-configured',\n detail: 'a Slack bot token is set but no channel is — set the alert channel (e.g. #infra-alerts)',\n }\n }\n\n const fetchImpl = options.fetchImpl ?? fetch\n const sleep = options.sleepImpl ?? defaultSleep\n const attempts = Math.max(1, options.attempts ?? DEFAULT_ATTEMPTS)\n const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS\n\n // A token is never fallen back FROM. If one is configured and dead, that is\n // the incident, and delivering over a webhook instead would hide exactly the\n // condition this module exists to surface.\n const transport: SlackTransport = token\n ? { kind: 'token', token, channel: channel as string }\n : { kind: 'webhook', url: webhookUrl as string }\n\n let lastTransient: SlackAlertFailure = {\n delivered: false,\n reason: 'transport',\n detail: 'Slack was never reached',\n }\n\n for (let attempt = 1; attempt <= attempts; attempt += 1) {\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), timeoutMs)\n let response: Response\n try {\n response = await fetchImpl(requestUrl(transport), {\n method: 'POST',\n headers: requestHeaders(transport),\n body: JSON.stringify(requestBody(transport, options.text)),\n signal: controller.signal,\n })\n } catch (error) {\n lastTransient = {\n delivered: false,\n reason: 'transport',\n detail: `could not reach Slack: ${error instanceof Error ? error.message : String(error)}`,\n }\n if (attempt < attempts) await sleep(Math.min(500 * 2 ** (attempt - 1), 8_000))\n continue\n } finally {\n clearTimeout(timer)\n }\n\n if (response.status === 429 || response.status >= 500) {\n lastTransient = {\n delivered: false,\n reason: response.status === 429 ? 'rate-limited' : 'transport',\n detail: `Slack returned ${response.status}`,\n }\n if (attempt < attempts) await sleep(retryDelayMs(response, attempt))\n continue\n }\n\n // Everything below is a settled answer. BOTH transports put the verdict in\n // the BODY under a 200 — the API as `{\"ok\":false,\"error\":…}`, a webhook as\n // an error token where the literal `ok` should be — so a status check alone\n // reads a revoked credential as a delivered page. That misreading is why\n // this module exists.\n return transport.kind === 'token'\n ? await settleTokenResponse(response, transport.channel)\n : await settleWebhookResponse(response)\n }\n\n return lastTransient\n}\n\ntype SlackTransport =\n | { kind: 'token'; token: string; channel: string }\n | { kind: 'webhook'; url: string }\n\nfunction requestUrl(transport: SlackTransport): string {\n return transport.kind === 'token' ? `${SLACK_API}/chat.postMessage` : transport.url\n}\n\nfunction requestHeaders(transport: SlackTransport): Record<string, string> {\n const headers: Record<string, string> = { 'Content-Type': 'application/json; charset=utf-8' }\n // The token rides a header, never a query string: a URL is logged by proxies\n // and retained in error messages; a header is not. (A webhook URL IS the\n // credential and has no such option — one more reason to prefer a token.)\n if (transport.kind === 'token') headers.Authorization = `Bearer ${transport.token}`\n return headers\n}\n\nfunction requestBody(transport: SlackTransport, text: string): Record<string, string> {\n // A webhook carries its own channel, fixed when it was created; naming one\n // here would be ignored at best and rejected at worst.\n return transport.kind === 'token' ? { channel: transport.channel, text } : { text }\n}\n\nasync function settleTokenResponse(\n response: Response,\n channel: string,\n): Promise<SlackAlertOutcome> {\n let body: { ok?: boolean; error?: string; channel?: string; ts?: string }\n try {\n body = (await response.json()) as typeof body\n } catch {\n return {\n delivered: false,\n reason: 'api',\n detail: `Slack returned ${response.status} with an unreadable body`,\n }\n }\n if (body.ok === true) {\n return { delivered: true, channel: body.channel ?? channel, ts: body.ts ?? '' }\n }\n return classifySlackError(body.error ?? `http_${response.status}`, channel)\n}\n\n/**\n * A webhook confirms delivery with a 2xx AND the literal three-byte body `ok`.\n * A revoked one answers `no_service` / `no_team`, sometimes under a 200. Both\n * halves are required: this is the shape `agent-dev-container` proved against a\n * stand-in webhook, where a healthy, a revoked and a deleted webhook produced\n * byte-identical output under a status-only check.\n */\nasync function settleWebhookResponse(response: Response): Promise<SlackAlertOutcome> {\n const body = (await response.text().catch(() => '')).trim()\n if (response.ok && body === 'ok') {\n // A webhook reports neither the channel it posted to nor a message id.\n return { delivered: true, channel: '(webhook)', ts: '' }\n }\n const error = body || `http_${response.status}`\n const reason: SlackFailureReason = WEBHOOK_CREDENTIAL_ERRORS.has(error)\n ? 'credential'\n : WEBHOOK_CHANNEL_ERRORS.has(error)\n ? 'channel'\n : 'api'\n return {\n delivered: false,\n reason,\n detail:\n reason === 'credential'\n ? `the Slack webhook is revoked (${error}) — no alert can arrive until a human mints a new one. ` +\n 'Prefer replacing it with a bot token, whose liveness can be checked before an alert needs it.'\n : reason === 'channel'\n ? `Slack refused the webhook's channel (${error}) — the channel was archived, or the app lost access.`\n : `Slack refused the webhook post (${error}).`,\n }\n}\n\n/** Webhook error tokens that mean the webhook itself is gone. */\nconst WEBHOOK_CREDENTIAL_ERRORS = new Set(['no_service', 'no_team', 'invalid_token'])\n\n/** Webhook error tokens that mean the webhook is valid but its channel is not usable. */\nconst WEBHOOK_CHANNEL_ERRORS = new Set(['channel_not_found', 'channel_is_archived', 'action_prohibited'])\n\n/** Define configuration options for verifying that a Slack bot token is live */\nexport interface SlackCredentialCheckOptions {\n token: string | undefined\n /** Per-request deadline. Default 10s. */\n timeoutMs?: number\n /** Injection seam for tests; defaults to global `fetch`. */\n fetchImpl?: typeof fetch\n}\n\n/** What `checkSlackCredential` concluded. `team`/`botId` are present only when live. */\nexport type SlackCredentialVerdict =\n | { live: true; team: string; botId: string }\n | { live: false; reason: SlackFailureReason; detail: string }\n\n/**\n * Ask Slack whether the bot token is alive, WITHOUT posting anything.\n *\n * This is the check that was missing. An incoming webhook offers no equivalent\n * — the only way to test one is to post to it — which is how a revoked webhook\n * sat in a repo secret for months looking configured. Wire it into `/preflight`\n * so a dead alerting channel fails a deploy rather than an incident.\n */\nexport async function checkSlackCredential(\n options: SlackCredentialCheckOptions,\n): Promise<SlackCredentialVerdict> {\n const token = options.token?.trim()\n if (!token) {\n return {\n live: false,\n reason: 'not-configured',\n detail: 'no Slack bot token configured — set SLACK_BOT_TOKEN',\n }\n }\n const fetchImpl = options.fetchImpl ?? fetch\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), options.timeoutMs ?? DEFAULT_TIMEOUT_MS)\n try {\n const response = await fetchImpl(`${SLACK_API}/auth.test`, {\n method: 'POST',\n headers: { Authorization: `Bearer ${token}` },\n signal: controller.signal,\n })\n const body = (await response.json()) as {\n ok?: boolean\n error?: string\n team?: string\n bot_id?: string\n }\n if (body.ok === true) {\n return { live: true, team: body.team ?? 'unknown', botId: body.bot_id ?? 'unknown' }\n }\n const outcome = classifySlackError(body.error ?? `http_${response.status}`, '(auth.test)')\n // `classifySlackError` is shaped for a post; auth.test only ever answers\n // about the credential, so a non-credential code here still means the\n // token cannot be used.\n return {\n live: false,\n reason: outcome.reason === 'channel' ? 'credential' : outcome.reason,\n detail: outcome.detail,\n }\n } catch (error) {\n return {\n live: false,\n reason: 'transport',\n detail: `could not reach Slack: ${error instanceof Error ? error.message : String(error)}`,\n }\n } finally {\n clearTimeout(timer)\n }\n}\n"],"mappings":";AAwDA,IAAM,YAAY;AAClB,IAAM,qBAAqB;AAC3B,IAAM,mBAAmB;AA8FzB,IAAM,oBAAoB,oBAAI,IAAI;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAG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