@ait-co/devtools 0.1.28 → 0.1.29

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.
@@ -18,13 +18,23 @@
18
18
  * `false` and could never pass. Layer A is the consumer guard; B and C are
19
19
  * here.
20
20
  *
21
+ * Layer B has two parts. Both must pass:
22
+ * B1 — host allowlist: `hostname` must be a `*.private-apps.tossmini.com`
23
+ * subdomain. The Toss app serves dogfood / private mini-apps from a
24
+ * separate `private-apps` host; a production (`intoss://`) entry is
25
+ * served from `*.apps.tossmini.com` WITHOUT the `private-apps` segment.
26
+ * This is the security gate against a dogfood build that somehow lands
27
+ * on a production entry — see the comment on {@link isPrivateAppsHost}.
28
+ * B2 — entry query: `_deploymentId` must be present and non-empty.
29
+ *
21
30
  * Decision matrix (the gate only ever runs in a debug build — Layer A already
22
31
  * passed by the time this code is reachable):
23
32
  *
24
- * _deploymentId | debug=1 | result
25
- * absent | (any) | BLOCKED (Layer Bentry gate)
26
- * present | absent | BLOCKED (Layer Copt-in gate)
27
- * present | present | ATTACH
33
+ * private-apps host | _deploymentId | debug=1 | result
34
+ * no | (any) | (any) | BLOCKED (Layer B1host)
35
+ * yes | absent | (any) | BLOCKED (Layer B2entry)
36
+ * yes | present | absent | BLOCKED (Layer C — opt-in)
37
+ * yes | present | present | ATTACH
28
38
  */
29
39
  /** Shape returned when the gate allows attachment. */
30
40
  interface GateResultAttach {
@@ -38,38 +48,57 @@ interface GateResultAttach {
38
48
  interface GateResultBlocked {
39
49
  readonly attach: false;
40
50
  /**
41
- * - `'entry'` Layer B: `_deploymentId` param is absent or empty.
51
+ * - `'host'` Layer B1: `hostname` is not a `*.private-apps.tossmini.com` host.
52
+ * - `'entry'` Layer B2: `_deploymentId` param is absent or empty.
42
53
  * - `'opt-in'` Layer C: `debug=1` param is absent.
43
54
  * - `'invalid-relay'` Layer C: `relay` param is absent, empty, or not a `wss:` URL.
44
55
  *
45
56
  * There is no `'build'` reason: Layer A is enforced by the consumer's
46
57
  * `if (__DEBUG_BUILD__)` guard, not by this function.
47
58
  */
48
- readonly reason: 'entry' | 'opt-in' | 'invalid-relay';
59
+ readonly reason: 'host' | 'entry' | 'opt-in' | 'invalid-relay';
49
60
  }
50
61
  type GateResult = GateResultAttach | GateResultBlocked;
51
62
  /**
52
63
  * Input for {@link evaluateDebugGate}.
53
64
  *
54
- * Keeping the field explicit makes the function trivially testable without
55
- * needing to manipulate `window.location`.
65
+ * Both fields are explicit so the function is trivially testable without
66
+ * touching `window`.
56
67
  */
57
68
  interface GateInput {
58
69
  /**
59
- * The URL search params to inspect for gate signals.
70
+ * The host the page is served from `window.location.hostname`.
71
+ *
72
+ * This is the Layer B1 security signal. Why hostname and not the entry
73
+ * scheme: the Toss SDK normalises `intoss-private://` to `intoss://` in
74
+ * `getSchemeUri()`, and `getOperationalEnvironment()` / `getWebViewType()`
75
+ * return the same value (`"toss"` / `"partner"`) for both dogfood and
76
+ * production entries — none of them distinguish a dogfood entry. The host
77
+ * does: a dogfood / private-apps entry is served from
78
+ * `*.private-apps.tossmini.com`, a production entry is not. This was
79
+ * confirmed live over CDP against mini-app 31146 (see spec open question 2).
80
+ */
81
+ readonly hostname: string;
82
+ /**
83
+ * The URL search params to inspect for gate signals (Layers B2 and C).
60
84
  *
61
85
  * Prefer `URLSearchParams` so callers can pass `new URLSearchParams(location.search)`
62
86
  * without coupling the pure function to `window`.
63
- *
64
- * Layer B open seam (spec open question 2): if the Toss SDK ever exposes
65
- * `getEntryScheme()` or a similar API that reliably signals a dogfood entry,
66
- * that signal should be checked before `_deploymentId` here. For now only the
67
- * `_deploymentId` query param fallback is implemented. Pass a custom
68
- * `URLSearchParams` to inject the SDK signal at the call site without
69
- * modifying this function.
70
87
  */
71
88
  readonly searchParams: URLSearchParams;
72
89
  }
90
+ /**
91
+ * Returns whether `hostname` is a `*.private-apps.tossmini.com` subdomain —
92
+ * the host the Toss app reserves for dogfood / private mini-app entries.
93
+ *
94
+ * The match is an exact suffix check, not a substring `.includes()`: a
95
+ * substring test would also accept an attacker-controlled host like
96
+ * `private-apps.tossmini.com.evil.example`, which ends in `.example`, not in
97
+ * `.tossmini.com`. Requiring the string to END with the suffix closes that.
98
+ * The leading `.` in the suffix also forces a real subdomain label, so a
99
+ * bare `private-apps.tossmini.com` (no mini-app subdomain) does not match.
100
+ */
101
+ declare function isPrivateAppsHost(hostname: string): boolean;
73
102
  /**
74
103
  * Pure function that evaluates the runtime debug activation layers (B and C).
75
104
  *
@@ -83,6 +112,7 @@ interface GateInput {
83
112
  * @example
84
113
  * ```ts
85
114
  * const result = evaluateDebugGate({
115
+ * hostname: window.location.hostname,
86
116
  * searchParams: new URLSearchParams(window.location.search),
87
117
  * });
88
118
  * if (result.attach) {
@@ -135,11 +165,14 @@ declare function maybeAttach(gateResult?: GateResult): void;
135
165
  * Returns the gate result. Callers can check `result.attach` to decide whether
136
166
  * to proceed with debug surface attachment.
137
167
  *
138
- * This function reads `window.location` only. Layer A (build-time) is enforced
139
- * by the consumer's `if (__DEBUG_BUILD__)` guard around the import site, not
140
- * here see the file-level comment.
168
+ * This function reads `window.location` only both the hostname (Layer B1
169
+ * host allowlist) and the search params (Layers B2 and C). Layer A
170
+ * (build-time) is enforced by the consumer's `if (__DEBUG_BUILD__)` guard
171
+ * around the import site, not here — see the file-level comment. Consumers
172
+ * call this with no arguments, so the Layer B1 host check is picked up with
173
+ * no change at the call site.
141
174
  */
142
175
  declare function checkDebugGate(): GateResult;
143
176
  //#endregion
144
- export { type GateInput, type GateResult, type GateResultAttach, type GateResultBlocked, checkDebugGate, deriveTargetScriptUrl, evaluateDebugGate, maybeAttach };
177
+ export { type GateInput, type GateResult, type GateResultAttach, type GateResultBlocked, checkDebugGate, deriveTargetScriptUrl, evaluateDebugGate, isPrivateAppsHost, maybeAttach };
145
178
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/in-app/gate.ts","../../src/in-app/attach.ts","../../src/in-app/index.ts"],"mappings":";;AA6BA;;;;;;;;;AASA;;;;;AAaA;;;;;AAQA;;;;;AAqCA;;;UAnEiB,gBAAA;EAAA,SACN,MAAA;EAkEuB;EAAA,SAhEvB,QAAA;EAgEoD;EAAA,SA9DpD,YAAA;AAAA;;UAIM,iBAAA;EAAA,SACN,MAAA;ECV0B;;;;AA4BrC;;;;EA5BqC,SDmB1B,MAAA;AAAA;AAAA,KAGC,UAAA,GAAa,gBAAA,GAAmB,iBAAA;;AEV5C;;;;;UFkBiB,SAAA;;;;;;;;;;;;;;WAcN,YAAA,EAAc,eAAA;AAAA;;;;;;;;;;;;;;;;;;;;;iBAuBT,iBAAA,CAAkB,KAAA,EAAO,SAAA,GAAY,UAAA;;;;;;AA7CrD;;;;;AAQA;;;;;AAqCA;;iBCnEgB,qBAAA,CAAsB,QAAA;;;;;;;;;;AAAtC;;;;;AA4BA;;iBAAgB,WAAA,CAAY,UAAA,GAAY,UAAA;;;;;;;;;;;;;AA5BxC;iBCYgB,cAAA,CAAA,GAAkB,UAAA"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/in-app/gate.ts","../../src/in-app/attach.ts","../../src/in-app/index.ts"],"mappings":";;AAuCA;;;;;;;;;AASA;;;;;AAcA;;;;;AAQA;;;;;;;;;AA8CA;;;;;AAyBA;;;;UAtGiB,gBAAA;EAAA,SACN,MAAA;EAqG0C;EAAA,SAnG1C,QAAA;EAmGoD;EAAA,SAjGpD,YAAA;AAAA;;UAIM,iBAAA;EAAA,SACN,MAAA;;;;ACQX;;;;;;WDEW,MAAA;AAAA;AAAA,KAGC,UAAA,GAAa,gBAAA,GAAmB,iBAAA;;;;;;;UAQ3B,SAAA;;;;;;;;;;;;;WAaN,QAAA;;;;;;;WAQA,YAAA,EAAc,eAAA;AAAA;;;;;;;;;;;;iBAyBT,iBAAA,CAAkB,QAAA;;;;;;;;;;;;;;;;;;;;;;iBAyBlB,iBAAA,CAAkB,KAAA,EAAO,SAAA,GAAY,UAAA;;;;;;AA/ErD;;;;;AAQA;;;;;;;iBCzCgB,qBAAA,CAAsB,QAAA;;ADuFtC;;;;;AAyBA;;;;;;;;;;iBCpFgB,WAAA,CAAY,UAAA,GAAY,UAAA;;;;;AD2DxC;;;;;AAyBA;;;;;;;iBEjGgB,cAAA,CAAA,GAAkB,UAAA"}
@@ -1,5 +1,29 @@
1
1
  //#region src/in-app/gate.ts
2
2
  /**
3
+ * The host suffix the Toss app uses to serve dogfood / private mini-apps.
4
+ *
5
+ * A `intoss-private://` (dogfood) entry maps to a host such as
6
+ * `aitc-sdk-example.private-apps.tossmini.com`. A production `intoss://`
7
+ * entry is served from `*.apps.tossmini.com` — the `.private-apps.` segment
8
+ * is absent. Confirmed live over CDP for mini-app 31146; the exact production
9
+ * host is to be re-confirmed once 31146 passes review (spec open question 2).
10
+ */
11
+ const PRIVATE_APPS_HOST_SUFFIX = ".private-apps.tossmini.com";
12
+ /**
13
+ * Returns whether `hostname` is a `*.private-apps.tossmini.com` subdomain —
14
+ * the host the Toss app reserves for dogfood / private mini-app entries.
15
+ *
16
+ * The match is an exact suffix check, not a substring `.includes()`: a
17
+ * substring test would also accept an attacker-controlled host like
18
+ * `private-apps.tossmini.com.evil.example`, which ends in `.example`, not in
19
+ * `.tossmini.com`. Requiring the string to END with the suffix closes that.
20
+ * The leading `.` in the suffix also forces a real subdomain label, so a
21
+ * bare `private-apps.tossmini.com` (no mini-app subdomain) does not match.
22
+ */
23
+ function isPrivateAppsHost(hostname) {
24
+ return hostname.endsWith(PRIVATE_APPS_HOST_SUFFIX);
25
+ }
26
+ /**
3
27
  * Pure function that evaluates the runtime debug activation layers (B and C).
4
28
  *
5
29
  * Has no side effects. The input is explicit. Returns a discriminated union
@@ -12,6 +36,7 @@
12
36
  * @example
13
37
  * ```ts
14
38
  * const result = evaluateDebugGate({
39
+ * hostname: window.location.hostname,
15
40
  * searchParams: new URLSearchParams(window.location.search),
16
41
  * });
17
42
  * if (result.attach) {
@@ -20,6 +45,10 @@
20
45
  * ```
21
46
  */
22
47
  function evaluateDebugGate(input) {
48
+ if (!isPrivateAppsHost(input.hostname)) return {
49
+ attach: false,
50
+ reason: "host"
51
+ };
23
52
  const deploymentId = input.searchParams.get("_deploymentId") ?? "";
24
53
  if (deploymentId === "") return {
25
54
  attach: false,
@@ -157,14 +186,20 @@ function maybeAttach(gateResult = checkDebugGate()) {
157
186
  * Returns the gate result. Callers can check `result.attach` to decide whether
158
187
  * to proceed with debug surface attachment.
159
188
  *
160
- * This function reads `window.location` only. Layer A (build-time) is enforced
161
- * by the consumer's `if (__DEBUG_BUILD__)` guard around the import site, not
162
- * here see the file-level comment.
189
+ * This function reads `window.location` only both the hostname (Layer B1
190
+ * host allowlist) and the search params (Layers B2 and C). Layer A
191
+ * (build-time) is enforced by the consumer's `if (__DEBUG_BUILD__)` guard
192
+ * around the import site, not here — see the file-level comment. Consumers
193
+ * call this with no arguments, so the Layer B1 host check is picked up with
194
+ * no change at the call site.
163
195
  */
164
196
  function checkDebugGate() {
165
- return evaluateDebugGate({ searchParams: new URLSearchParams(window.location.search) });
197
+ return evaluateDebugGate({
198
+ hostname: window.location.hostname,
199
+ searchParams: new URLSearchParams(window.location.search)
200
+ });
166
201
  }
167
202
  //#endregion
168
- export { checkDebugGate, deriveTargetScriptUrl, evaluateDebugGate, maybeAttach };
203
+ export { checkDebugGate, deriveTargetScriptUrl, evaluateDebugGate, isPrivateAppsHost, maybeAttach };
169
204
 
170
205
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/in-app/gate.ts","../../src/in-app/attach.ts","../../src/in-app/index.ts"],"sourcesContent":["/**\n * Runtime activation gate for the in-app debug surface.\n *\n * Spec: docs/superpowers/specs/2026-05-18-in-app-debug-mcp.md\n * \"3-layer activation gate\". This is the pure gate decision; the Chii client,\n * WebSocket transport, MCP server, and CLI that consume it live in src/mcp/.\n *\n * This function evaluates the two RUNTIME layers, B and C. Layer A — the\n * build-time gate — is NOT evaluated here, and deliberately so: it is enforced\n * entirely by the consumer's `if (__DEBUG_BUILD__) { … }` guard around the\n * import site (see sdk-example `src/main.tsx`). `__DEBUG_BUILD__` is a\n * consumer-build-time constant; a release consumer build folds it to `false`\n * and dead-code-eliminates the whole import of `@ait-co/devtools/in-app`, so\n * this code is simply absent from release bundles. A pre-built npm package\n * cannot re-check that flag — it was already baked at devtools' own publish\n * time — so any `isDebugBuild` check inside this function would be permanently\n * `false` and could never pass. Layer A is the consumer guard; B and C are\n * here.\n *\n * Decision matrix (the gate only ever runs in a debug build — Layer A already\n * passed by the time this code is reachable):\n *\n * _deploymentId | debug=1 | result\n * absent | (any) | BLOCKED (Layer B — entry gate)\n * present | absent | BLOCKED (Layer C — opt-in gate)\n * present | present | ATTACH\n */\n\n/** Shape returned when the gate allows attachment. */\nexport interface GateResultAttach {\n readonly attach: true;\n /** The validated `wss:` relay URL from the `relay` query param. */\n readonly relayUrl: string;\n /** The deployment ID extracted from the `_deploymentId` query param. */\n readonly deploymentId: string;\n}\n\n/** Shape returned when the gate blocks attachment, with a reason code. */\nexport interface GateResultBlocked {\n readonly attach: false;\n /**\n * - `'entry'` Layer B: `_deploymentId` param is absent or empty.\n * - `'opt-in'` Layer C: `debug=1` param is absent.\n * - `'invalid-relay'` Layer C: `relay` param is absent, empty, or not a `wss:` URL.\n *\n * There is no `'build'` reason: Layer A is enforced by the consumer's\n * `if (__DEBUG_BUILD__)` guard, not by this function.\n */\n readonly reason: 'entry' | 'opt-in' | 'invalid-relay';\n}\n\nexport type GateResult = GateResultAttach | GateResultBlocked;\n\n/**\n * Input for {@link evaluateDebugGate}.\n *\n * Keeping the field explicit makes the function trivially testable without\n * needing to manipulate `window.location`.\n */\nexport interface GateInput {\n /**\n * The URL search params to inspect for gate signals.\n *\n * Prefer `URLSearchParams` so callers can pass `new URLSearchParams(location.search)`\n * without coupling the pure function to `window`.\n *\n * Layer B open seam (spec open question 2): if the Toss SDK ever exposes\n * `getEntryScheme()` or a similar API that reliably signals a dogfood entry,\n * that signal should be checked before `_deploymentId` here. For now only the\n * `_deploymentId` query param fallback is implemented. Pass a custom\n * `URLSearchParams` to inject the SDK signal at the call site without\n * modifying this function.\n */\n readonly searchParams: URLSearchParams;\n}\n\n/**\n * Pure function that evaluates the runtime debug activation layers (B and C).\n *\n * Has no side effects. The input is explicit. Returns a discriminated union\n * so callers can pattern-match on `result.attach`.\n *\n * Layer A (build-time) is intentionally not evaluated here — see the file-level\n * comment. By the time this function runs, the consumer's `if (__DEBUG_BUILD__)`\n * guard has already passed; this function only decides B and C.\n *\n * @example\n * ```ts\n * const result = evaluateDebugGate({\n * searchParams: new URLSearchParams(window.location.search),\n * });\n * if (result.attach) {\n * // Proceed to load Chii client\n * }\n * ```\n */\nexport function evaluateDebugGate(input: GateInput): GateResult {\n // Layer B — runtime entry scheme gate.\n // `_deploymentId` must be present and non-empty. The `intoss-private://`\n // scheme used for dogfood entries includes this param; general user entry\n // paths do not.\n //\n // Open seam (spec open question 2): if the Toss SDK exposes getEntryScheme()\n // or similar, that should be the 1st-priority signal checked here, with\n // `_deploymentId` as fallback. Extend this check at the call site by\n // pre-populating `searchParams` with the SDK signal, or add an optional\n // `entryScheme` field to `GateInput` in a later phase.\n const deploymentId = input.searchParams.get('_deploymentId') ?? '';\n if (deploymentId === '') {\n return { attach: false, reason: 'entry' };\n }\n\n // Layer C — explicit opt-in gate.\n // Require `debug=1` so that an operator who opens a dogfood URL by accident\n // does not inadvertently trigger the debug surface.\n const debugParam = input.searchParams.get('debug');\n if (debugParam !== '1') {\n return { attach: false, reason: 'opt-in' };\n }\n\n // Layer C continued — relay URL validation.\n // `relay=<wss-url>` must be present and must use the `wss:` scheme.\n // Plain `ws:` is rejected (no TLS). `http:`/`https:` are rejected.\n const relayRaw = input.searchParams.get('relay') ?? '';\n if (relayRaw === '') {\n return { attach: false, reason: 'invalid-relay' };\n }\n\n let relayUrl: URL;\n try {\n relayUrl = new URL(relayRaw);\n } catch {\n return { attach: false, reason: 'invalid-relay' };\n }\n\n if (relayUrl.protocol !== 'wss:') {\n return { attach: false, reason: 'invalid-relay' };\n }\n\n return { attach: true, relayUrl: relayUrl.href, deploymentId };\n}\n","/**\n * In-app Chii target injection for the debug attach flow.\n *\n * Spec: docs/superpowers/specs/2026-05-18-in-app-debug-mcp.md\n * \"MCP attach\" topology section — Phase 1 browser-side implementation.\n *\n * This module bridges the 3-layer gate result to a Chii `target.js` script\n * injection. The Chii npm package is the relay SERVER — the in-app side is\n * a plain `<script src=\"…/target.js\">` pointing at the relay host. No chii\n * npm dependency is needed here.\n */\n\nimport { checkDebugGate, type GateResult } from './index.js';\n\n/**\n * Converts a validated `wss:` relay URL into the Chii `target.js` script URL.\n *\n * Scheme is mapped `wss:` → `https:`. Host and port are preserved.\n * Pathname is set to `/target.js` regardless of the relay path.\n * Query params and hash from the relay URL are dropped — the target script\n * URL is a static asset path on the same host.\n *\n * @example\n * deriveTargetScriptUrl('wss://abc.trycloudflare.com/relay')\n * // → 'https://abc.trycloudflare.com/target.js'\n *\n * deriveTargetScriptUrl('wss://h.example.com:9100/')\n * // → 'https://h.example.com:9100/target.js'\n */\nexport function deriveTargetScriptUrl(relayUrl: string): string {\n const u = new URL(relayUrl);\n u.protocol = 'https:';\n u.pathname = '/target.js';\n u.search = '';\n u.hash = '';\n return u.toString();\n}\n\n/** Module-level guard against double-injection within a page lifecycle. */\nlet attached = false;\n\n/**\n * Evaluates the 3-layer debug gate and, if the gate passes, injects the Chii\n * `target.js` script into `document.head`.\n *\n * Idempotent — calling more than once is safe. The second call is a no-op if\n * a script with the same `src` is already present in the document, and the\n * module-level `attached` flag prevents redundant DOM queries after the first\n * successful injection.\n *\n * Safe to call even if `document` is somehow unavailable (defensive boundary\n * guard — in practice this always runs in a real WebView).\n *\n * @param gateResult - Optional pre-evaluated gate result for testability.\n * Defaults to `checkDebugGate()` which reads the current page URL. Passing a\n * custom value avoids the need to manipulate `window.location` in tests.\n */\nexport function maybeAttach(gateResult: GateResult = checkDebugGate()): void {\n if (!gateResult.attach) {\n console.debug(\n `[@ait-co/devtools] debug attach skipped — gate blocked (reason: ${gateResult.reason})`,\n );\n return;\n }\n\n // Guard against double-injection across repeated calls.\n if (attached) {\n return;\n }\n\n // Defensive: if document is not available (unusual, but possible in some\n // SSR-adjacent edge cases), bail silently rather than throwing.\n if (typeof document === 'undefined') {\n return;\n }\n\n const src = deriveTargetScriptUrl(gateResult.relayUrl);\n\n // Also guard against a script with the same src already in the DOM\n // (e.g. injected by a different code path or a page reload within SPA).\n const existing = document.querySelector<HTMLScriptElement>(`script[src=\"${src}\"]`);\n if (existing !== null) {\n attached = true;\n return;\n }\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n (document.head ?? document.documentElement).appendChild(script);\n\n attached = true;\n}\n","/**\n * @ait-co/devtools/in-app entry point.\n *\n * Spec: docs/superpowers/specs/2026-05-18-in-app-debug-mcp.md\n *\n * Phase 1 — gate + browser-side Chii target injection.\n * WebSocket relay, QR/paste UI, and AI-host MCP bin are later phases that\n * require real-device validation and are not included here.\n *\n * This thin entry reads `window.location` and calls the pure\n * {@link evaluateDebugGate} function. All testable logic lives in `./gate.ts`\n * and `./attach.ts`, not here.\n *\n * Layer A of the activation gate (build-time) is NOT enforced in this module.\n * It is the consumer's responsibility: the consumer wraps its\n * `import('@ait-co/devtools/in-app')` call site in `if (__DEBUG_BUILD__) { … }`\n * (see sdk-example `src/main.tsx`), where `__DEBUG_BUILD__` is a\n * consumer-build-time constant. A release consumer build folds that constant\n * to `false` and dead-code-eliminates this whole module. This package is\n * pre-built and ships with `__DEBUG_BUILD__` already resolved at devtools'\n * publish time, so it could never re-evaluate the consumer's build channel —\n * which is exactly why Layer A lives at the consumer guard, not here.\n */\n\nimport { evaluateDebugGate, type GateResult } from './gate.js';\n\nexport { deriveTargetScriptUrl, maybeAttach } from './attach.js';\nexport type { GateInput, GateResult, GateResultAttach, GateResultBlocked } from './gate.js';\nexport { evaluateDebugGate } from './gate.js';\n\n/**\n * Evaluates the runtime debug activation layers (B and C) against the current\n * page URL.\n *\n * Returns the gate result. Callers can check `result.attach` to decide whether\n * to proceed with debug surface attachment.\n *\n * This function reads `window.location` only. Layer A (build-time) is enforced\n * by the consumer's `if (__DEBUG_BUILD__)` guard around the import site, not\n * here — see the file-level comment.\n */\nexport function checkDebugGate(): GateResult {\n return evaluateDebugGate({\n searchParams: new URLSearchParams(window.location.search),\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAgGA,SAAgB,kBAAkB,OAA8B;CAW9D,MAAM,eAAe,MAAM,aAAa,IAAI,gBAAgB,IAAI;AAChE,KAAI,iBAAiB,GACnB,QAAO;EAAE,QAAQ;EAAO,QAAQ;EAAS;AAO3C,KADmB,MAAM,aAAa,IAAI,QAAQ,KAC/B,IACjB,QAAO;EAAE,QAAQ;EAAO,QAAQ;EAAU;CAM5C,MAAM,WAAW,MAAM,aAAa,IAAI,QAAQ,IAAI;AACpD,KAAI,aAAa,GACf,QAAO;EAAE,QAAQ;EAAO,QAAQ;EAAiB;CAGnD,IAAI;AACJ,KAAI;AACF,aAAW,IAAI,IAAI,SAAS;SACtB;AACN,SAAO;GAAE,QAAQ;GAAO,QAAQ;GAAiB;;AAGnD,KAAI,SAAS,aAAa,OACxB,QAAO;EAAE,QAAQ;EAAO,QAAQ;EAAiB;AAGnD,QAAO;EAAE,QAAQ;EAAM,UAAU,SAAS;EAAM;EAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC9GhE,SAAgB,sBAAsB,UAA0B;CAC9D,MAAM,IAAI,IAAI,IAAI,SAAS;AAC3B,GAAE,WAAW;AACb,GAAE,WAAW;AACb,GAAE,SAAS;AACX,GAAE,OAAO;AACT,QAAO,EAAE,UAAU;;;AAIrB,IAAI,WAAW;;;;;;;;;;;;;;;;;AAkBf,SAAgB,YAAY,aAAyB,gBAAgB,EAAQ;AAC3E,KAAI,CAAC,WAAW,QAAQ;AACtB,UAAQ,MACN,mEAAmE,WAAW,OAAO,GACtF;AACD;;AAIF,KAAI,SACF;AAKF,KAAI,OAAO,aAAa,YACtB;CAGF,MAAM,MAAM,sBAAsB,WAAW,SAAS;AAKtD,KADiB,SAAS,cAAiC,eAAe,IAAI,IAAI,KACjE,MAAM;AACrB,aAAW;AACX;;CAGF,MAAM,SAAS,SAAS,cAAc,SAAS;AAC/C,QAAO,MAAM;AACb,QAAO,QAAQ;AACf,EAAC,SAAS,QAAQ,SAAS,iBAAiB,YAAY,OAAO;AAE/D,YAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AClDb,SAAgB,iBAA6B;AAC3C,QAAO,kBAAkB,EACvB,cAAc,IAAI,gBAAgB,OAAO,SAAS,OAAO,EAC1D,CAAC"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/in-app/gate.ts","../../src/in-app/attach.ts","../../src/in-app/index.ts"],"sourcesContent":["/**\n * Runtime activation gate for the in-app debug surface.\n *\n * Spec: docs/superpowers/specs/2026-05-18-in-app-debug-mcp.md\n * \"3-layer activation gate\". This is the pure gate decision; the Chii client,\n * WebSocket transport, MCP server, and CLI that consume it live in src/mcp/.\n *\n * This function evaluates the two RUNTIME layers, B and C. Layer A — the\n * build-time gate — is NOT evaluated here, and deliberately so: it is enforced\n * entirely by the consumer's `if (__DEBUG_BUILD__) { … }` guard around the\n * import site (see sdk-example `src/main.tsx`). `__DEBUG_BUILD__` is a\n * consumer-build-time constant; a release consumer build folds it to `false`\n * and dead-code-eliminates the whole import of `@ait-co/devtools/in-app`, so\n * this code is simply absent from release bundles. A pre-built npm package\n * cannot re-check that flag — it was already baked at devtools' own publish\n * time — so any `isDebugBuild` check inside this function would be permanently\n * `false` and could never pass. Layer A is the consumer guard; B and C are\n * here.\n *\n * Layer B has two parts. Both must pass:\n * B1 — host allowlist: `hostname` must be a `*.private-apps.tossmini.com`\n * subdomain. The Toss app serves dogfood / private mini-apps from a\n * separate `private-apps` host; a production (`intoss://`) entry is\n * served from `*.apps.tossmini.com` WITHOUT the `private-apps` segment.\n * This is the security gate against a dogfood build that somehow lands\n * on a production entry — see the comment on {@link isPrivateAppsHost}.\n * B2 — entry query: `_deploymentId` must be present and non-empty.\n *\n * Decision matrix (the gate only ever runs in a debug build — Layer A already\n * passed by the time this code is reachable):\n *\n * private-apps host | _deploymentId | debug=1 | result\n * no | (any) | (any) | BLOCKED (Layer B1 — host)\n * yes | absent | (any) | BLOCKED (Layer B2 — entry)\n * yes | present | absent | BLOCKED (Layer C — opt-in)\n * yes | present | present | ATTACH\n */\n\n/** Shape returned when the gate allows attachment. */\nexport interface GateResultAttach {\n readonly attach: true;\n /** The validated `wss:` relay URL from the `relay` query param. */\n readonly relayUrl: string;\n /** The deployment ID extracted from the `_deploymentId` query param. */\n readonly deploymentId: string;\n}\n\n/** Shape returned when the gate blocks attachment, with a reason code. */\nexport interface GateResultBlocked {\n readonly attach: false;\n /**\n * - `'host'` Layer B1: `hostname` is not a `*.private-apps.tossmini.com` host.\n * - `'entry'` Layer B2: `_deploymentId` param is absent or empty.\n * - `'opt-in'` Layer C: `debug=1` param is absent.\n * - `'invalid-relay'` Layer C: `relay` param is absent, empty, or not a `wss:` URL.\n *\n * There is no `'build'` reason: Layer A is enforced by the consumer's\n * `if (__DEBUG_BUILD__)` guard, not by this function.\n */\n readonly reason: 'host' | 'entry' | 'opt-in' | 'invalid-relay';\n}\n\nexport type GateResult = GateResultAttach | GateResultBlocked;\n\n/**\n * Input for {@link evaluateDebugGate}.\n *\n * Both fields are explicit so the function is trivially testable without\n * touching `window`.\n */\nexport interface GateInput {\n /**\n * The host the page is served from — `window.location.hostname`.\n *\n * This is the Layer B1 security signal. Why hostname and not the entry\n * scheme: the Toss SDK normalises `intoss-private://` to `intoss://` in\n * `getSchemeUri()`, and `getOperationalEnvironment()` / `getWebViewType()`\n * return the same value (`\"toss\"` / `\"partner\"`) for both dogfood and\n * production entries — none of them distinguish a dogfood entry. The host\n * does: a dogfood / private-apps entry is served from\n * `*.private-apps.tossmini.com`, a production entry is not. This was\n * confirmed live over CDP against mini-app 31146 (see spec open question 2).\n */\n readonly hostname: string;\n\n /**\n * The URL search params to inspect for gate signals (Layers B2 and C).\n *\n * Prefer `URLSearchParams` so callers can pass `new URLSearchParams(location.search)`\n * without coupling the pure function to `window`.\n */\n readonly searchParams: URLSearchParams;\n}\n\n/**\n * The host suffix the Toss app uses to serve dogfood / private mini-apps.\n *\n * A `intoss-private://` (dogfood) entry maps to a host such as\n * `aitc-sdk-example.private-apps.tossmini.com`. A production `intoss://`\n * entry is served from `*.apps.tossmini.com` — the `.private-apps.` segment\n * is absent. Confirmed live over CDP for mini-app 31146; the exact production\n * host is to be re-confirmed once 31146 passes review (spec open question 2).\n */\nconst PRIVATE_APPS_HOST_SUFFIX = '.private-apps.tossmini.com';\n\n/**\n * Returns whether `hostname` is a `*.private-apps.tossmini.com` subdomain —\n * the host the Toss app reserves for dogfood / private mini-app entries.\n *\n * The match is an exact suffix check, not a substring `.includes()`: a\n * substring test would also accept an attacker-controlled host like\n * `private-apps.tossmini.com.evil.example`, which ends in `.example`, not in\n * `.tossmini.com`. Requiring the string to END with the suffix closes that.\n * The leading `.` in the suffix also forces a real subdomain label, so a\n * bare `private-apps.tossmini.com` (no mini-app subdomain) does not match.\n */\nexport function isPrivateAppsHost(hostname: string): boolean {\n return hostname.endsWith(PRIVATE_APPS_HOST_SUFFIX);\n}\n\n/**\n * Pure function that evaluates the runtime debug activation layers (B and C).\n *\n * Has no side effects. The input is explicit. Returns a discriminated union\n * so callers can pattern-match on `result.attach`.\n *\n * Layer A (build-time) is intentionally not evaluated here — see the file-level\n * comment. By the time this function runs, the consumer's `if (__DEBUG_BUILD__)`\n * guard has already passed; this function only decides B and C.\n *\n * @example\n * ```ts\n * const result = evaluateDebugGate({\n * hostname: window.location.hostname,\n * searchParams: new URLSearchParams(window.location.search),\n * });\n * if (result.attach) {\n * // Proceed to load Chii client\n * }\n * ```\n */\nexport function evaluateDebugGate(input: GateInput): GateResult {\n // Layer B1 — host allowlist (the security gate).\n // The page must be served from a `*.private-apps.tossmini.com` host. A\n // production `intoss://` entry is served from `*.apps.tossmini.com` and is\n // rejected here. This is what stops a dogfood build that somehow reaches a\n // production entry from attaching: Layer A keeps debug code out of release\n // bundles, and this layer keeps a dogfood bundle that lands on a production\n // host from attaching even though its code is present.\n if (!isPrivateAppsHost(input.hostname)) {\n return { attach: false, reason: 'host' };\n }\n\n // Layer B2 — runtime entry query gate.\n // `_deploymentId` must be present and non-empty. The `intoss-private://`\n // scheme used for dogfood entries includes this param; general user entry\n // paths do not.\n const deploymentId = input.searchParams.get('_deploymentId') ?? '';\n if (deploymentId === '') {\n return { attach: false, reason: 'entry' };\n }\n\n // Layer C — explicit opt-in gate.\n // Require `debug=1` so that an operator who opens a dogfood URL by accident\n // does not inadvertently trigger the debug surface.\n const debugParam = input.searchParams.get('debug');\n if (debugParam !== '1') {\n return { attach: false, reason: 'opt-in' };\n }\n\n // Layer C continued — relay URL validation.\n // `relay=<wss-url>` must be present and must use the `wss:` scheme.\n // Plain `ws:` is rejected (no TLS). `http:`/`https:` are rejected.\n const relayRaw = input.searchParams.get('relay') ?? '';\n if (relayRaw === '') {\n return { attach: false, reason: 'invalid-relay' };\n }\n\n let relayUrl: URL;\n try {\n relayUrl = new URL(relayRaw);\n } catch {\n return { attach: false, reason: 'invalid-relay' };\n }\n\n if (relayUrl.protocol !== 'wss:') {\n return { attach: false, reason: 'invalid-relay' };\n }\n\n return { attach: true, relayUrl: relayUrl.href, deploymentId };\n}\n","/**\n * In-app Chii target injection for the debug attach flow.\n *\n * Spec: docs/superpowers/specs/2026-05-18-in-app-debug-mcp.md\n * \"MCP attach\" topology section — Phase 1 browser-side implementation.\n *\n * This module bridges the 3-layer gate result to a Chii `target.js` script\n * injection. The Chii npm package is the relay SERVER — the in-app side is\n * a plain `<script src=\"…/target.js\">` pointing at the relay host. No chii\n * npm dependency is needed here.\n */\n\nimport { checkDebugGate, type GateResult } from './index.js';\n\n/**\n * Converts a validated `wss:` relay URL into the Chii `target.js` script URL.\n *\n * Scheme is mapped `wss:` → `https:`. Host and port are preserved.\n * Pathname is set to `/target.js` regardless of the relay path.\n * Query params and hash from the relay URL are dropped — the target script\n * URL is a static asset path on the same host.\n *\n * @example\n * deriveTargetScriptUrl('wss://abc.trycloudflare.com/relay')\n * // → 'https://abc.trycloudflare.com/target.js'\n *\n * deriveTargetScriptUrl('wss://h.example.com:9100/')\n * // → 'https://h.example.com:9100/target.js'\n */\nexport function deriveTargetScriptUrl(relayUrl: string): string {\n const u = new URL(relayUrl);\n u.protocol = 'https:';\n u.pathname = '/target.js';\n u.search = '';\n u.hash = '';\n return u.toString();\n}\n\n/** Module-level guard against double-injection within a page lifecycle. */\nlet attached = false;\n\n/**\n * Evaluates the 3-layer debug gate and, if the gate passes, injects the Chii\n * `target.js` script into `document.head`.\n *\n * Idempotent — calling more than once is safe. The second call is a no-op if\n * a script with the same `src` is already present in the document, and the\n * module-level `attached` flag prevents redundant DOM queries after the first\n * successful injection.\n *\n * Safe to call even if `document` is somehow unavailable (defensive boundary\n * guard — in practice this always runs in a real WebView).\n *\n * @param gateResult - Optional pre-evaluated gate result for testability.\n * Defaults to `checkDebugGate()` which reads the current page URL. Passing a\n * custom value avoids the need to manipulate `window.location` in tests.\n */\nexport function maybeAttach(gateResult: GateResult = checkDebugGate()): void {\n if (!gateResult.attach) {\n console.debug(\n `[@ait-co/devtools] debug attach skipped — gate blocked (reason: ${gateResult.reason})`,\n );\n return;\n }\n\n // Guard against double-injection across repeated calls.\n if (attached) {\n return;\n }\n\n // Defensive: if document is not available (unusual, but possible in some\n // SSR-adjacent edge cases), bail silently rather than throwing.\n if (typeof document === 'undefined') {\n return;\n }\n\n const src = deriveTargetScriptUrl(gateResult.relayUrl);\n\n // Also guard against a script with the same src already in the DOM\n // (e.g. injected by a different code path or a page reload within SPA).\n const existing = document.querySelector<HTMLScriptElement>(`script[src=\"${src}\"]`);\n if (existing !== null) {\n attached = true;\n return;\n }\n\n const script = document.createElement('script');\n script.src = src;\n script.async = true;\n (document.head ?? document.documentElement).appendChild(script);\n\n attached = true;\n}\n","/**\n * @ait-co/devtools/in-app entry point.\n *\n * Spec: docs/superpowers/specs/2026-05-18-in-app-debug-mcp.md\n *\n * Phase 1 — gate + browser-side Chii target injection.\n * WebSocket relay, QR/paste UI, and AI-host MCP bin are later phases that\n * require real-device validation and are not included here.\n *\n * This thin entry reads `window.location` and calls the pure\n * {@link evaluateDebugGate} function. All testable logic lives in `./gate.ts`\n * and `./attach.ts`, not here.\n *\n * Layer A of the activation gate (build-time) is NOT enforced in this module.\n * It is the consumer's responsibility: the consumer wraps its\n * `import('@ait-co/devtools/in-app')` call site in `if (__DEBUG_BUILD__) { … }`\n * (see sdk-example `src/main.tsx`), where `__DEBUG_BUILD__` is a\n * consumer-build-time constant. A release consumer build folds that constant\n * to `false` and dead-code-eliminates this whole module. This package is\n * pre-built and ships with `__DEBUG_BUILD__` already resolved at devtools'\n * publish time, so it could never re-evaluate the consumer's build channel —\n * which is exactly why Layer A lives at the consumer guard, not here.\n */\n\nimport { evaluateDebugGate, type GateResult } from './gate.js';\n\nexport { deriveTargetScriptUrl, maybeAttach } from './attach.js';\nexport type { GateInput, GateResult, GateResultAttach, GateResultBlocked } from './gate.js';\nexport { evaluateDebugGate, isPrivateAppsHost } from './gate.js';\n\n/**\n * Evaluates the runtime debug activation layers (B and C) against the current\n * page URL.\n *\n * Returns the gate result. Callers can check `result.attach` to decide whether\n * to proceed with debug surface attachment.\n *\n * This function reads `window.location` only — both the hostname (Layer B1\n * host allowlist) and the search params (Layers B2 and C). Layer A\n * (build-time) is enforced by the consumer's `if (__DEBUG_BUILD__)` guard\n * around the import site, not here — see the file-level comment. Consumers\n * call this with no arguments, so the Layer B1 host check is picked up with\n * no change at the call site.\n */\nexport function checkDebugGate(): GateResult {\n return evaluateDebugGate({\n hostname: window.location.hostname,\n searchParams: new URLSearchParams(window.location.search),\n });\n}\n"],"mappings":";;;;;;;;;;AAuGA,MAAM,2BAA2B;;;;;;;;;;;;AAajC,SAAgB,kBAAkB,UAA2B;AAC3D,QAAO,SAAS,SAAS,yBAAyB;;;;;;;;;;;;;;;;;;;;;;;AAwBpD,SAAgB,kBAAkB,OAA8B;AAQ9D,KAAI,CAAC,kBAAkB,MAAM,SAAS,CACpC,QAAO;EAAE,QAAQ;EAAO,QAAQ;EAAQ;CAO1C,MAAM,eAAe,MAAM,aAAa,IAAI,gBAAgB,IAAI;AAChE,KAAI,iBAAiB,GACnB,QAAO;EAAE,QAAQ;EAAO,QAAQ;EAAS;AAO3C,KADmB,MAAM,aAAa,IAAI,QAAQ,KAC/B,IACjB,QAAO;EAAE,QAAQ;EAAO,QAAQ;EAAU;CAM5C,MAAM,WAAW,MAAM,aAAa,IAAI,QAAQ,IAAI;AACpD,KAAI,aAAa,GACf,QAAO;EAAE,QAAQ;EAAO,QAAQ;EAAiB;CAGnD,IAAI;AACJ,KAAI;AACF,aAAW,IAAI,IAAI,SAAS;SACtB;AACN,SAAO;GAAE,QAAQ;GAAO,QAAQ;GAAiB;;AAGnD,KAAI,SAAS,aAAa,OACxB,QAAO;EAAE,QAAQ;EAAO,QAAQ;EAAiB;AAGnD,QAAO;EAAE,QAAQ;EAAM,UAAU,SAAS;EAAM;EAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AChKhE,SAAgB,sBAAsB,UAA0B;CAC9D,MAAM,IAAI,IAAI,IAAI,SAAS;AAC3B,GAAE,WAAW;AACb,GAAE,WAAW;AACb,GAAE,SAAS;AACX,GAAE,OAAO;AACT,QAAO,EAAE,UAAU;;;AAIrB,IAAI,WAAW;;;;;;;;;;;;;;;;;AAkBf,SAAgB,YAAY,aAAyB,gBAAgB,EAAQ;AAC3E,KAAI,CAAC,WAAW,QAAQ;AACtB,UAAQ,MACN,mEAAmE,WAAW,OAAO,GACtF;AACD;;AAIF,KAAI,SACF;AAKF,KAAI,OAAO,aAAa,YACtB;CAGF,MAAM,MAAM,sBAAsB,WAAW,SAAS;AAKtD,KADiB,SAAS,cAAiC,eAAe,IAAI,IAAI,KACjE,MAAM;AACrB,aAAW;AACX;;CAGF,MAAM,SAAS,SAAS,cAAc,SAAS;AAC/C,QAAO,MAAM;AACb,QAAO,QAAQ;AACf,EAAC,SAAS,QAAQ,SAAS,iBAAiB,YAAY,OAAO;AAE/D,YAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC/Cb,SAAgB,iBAA6B;AAC3C,QAAO,kBAAkB;EACvB,UAAU,OAAO,SAAS;EAC1B,cAAc,IAAI,gBAAgB,OAAO,SAAS,OAAO;EAC1D,CAAC"}
package/dist/mcp/cli.js CHANGED
@@ -636,7 +636,7 @@ function createDebugServer(deps) {
636
636
  const { connection, aitSource, getTunnelStatus } = deps;
637
637
  const server = new Server({
638
638
  name: "ait-debug",
639
- version: "0.1.28"
639
+ version: "0.1.29"
640
640
  }, { capabilities: { tools: {} } });
641
641
  server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: DEBUG_TOOL_DEFINITIONS.map((tool) => ({ ...tool })) }));
642
642
  server.setRequestHandler(CallToolRequestSchema, async (request) => {
@@ -898,7 +898,7 @@ function createDevServer(deps = {}) {
898
898
  const aitSource = deps.aitSource ?? new HttpAitSource({ stateEndpoint });
899
899
  const server = new Server({
900
900
  name: "ait-devtools",
901
- version: "0.1.28"
901
+ version: "0.1.29"
902
902
  }, { capabilities: { tools: {} } });
903
903
  server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: DEV_TOOL_DEFINITIONS.map((tool) => ({ ...tool })) }));
904
904
  server.setRequestHandler(CallToolRequestSchema, async (request) => {
@@ -234,7 +234,7 @@ function createDevServer(deps = {}) {
234
234
  const aitSource = deps.aitSource ?? new HttpAitSource({ stateEndpoint });
235
235
  const server = new Server({
236
236
  name: "ait-devtools",
237
- version: "0.1.28"
237
+ version: "0.1.29"
238
238
  }, { capabilities: { tools: {} } });
239
239
  server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: DEV_TOOL_DEFINITIONS.map((tool) => ({ ...tool })) }));
240
240
  server.setRequestHandler(CallToolRequestSchema, async (request) => {
@@ -1050,7 +1050,7 @@ function readGlobalString(key) {
1050
1050
  }
1051
1051
  const TELEMETRY_ENDPOINT = readGlobalString("__TELEMETRY_ENDPOINT__") ?? "https://t.aitc.dev";
1052
1052
  function getVersion() {
1053
- return "0.1.28";
1053
+ return "0.1.29";
1054
1054
  }
1055
1055
  let panelVisibleSince = null;
1056
1056
  let accumulatedMs = 0;
@@ -4182,7 +4182,7 @@ function mount() {
4182
4182
  mockBadge.textContent = aitState.state.panelEditable ? t("panel.editMode.on") : t("panel.editMode.off");
4183
4183
  refreshPanel();
4184
4184
  });
4185
- const headerRight = h("span", { style: "display:flex;align-items:center;gap:6px" }, mockBadge, h("span", { style: "font-size:11px;color:#666;font-weight:400" }, `v0.1.28`), closeBtn);
4185
+ const headerRight = h("span", { style: "display:flex;align-items:center;gap:6px" }, mockBadge, h("span", { style: "font-size:11px;color:#666;font-weight:400" }, `v0.1.29`), closeBtn);
4186
4186
  const header = h("div", { className: "ait-panel-header" }, h("span", {}, t("panel.title")), headerRight);
4187
4187
  tabsEl = h("div", { className: "ait-panel-tabs" });
4188
4188
  for (const tab of getTabs()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ait-co/devtools",
3
- "version": "0.1.28",
3
+ "version": "0.1.29",
4
4
  "description": "Development tools for Apps in Toss mini-apps — mock SDK, floating devtools panel, and universal bundler plugin",
5
5
  "type": "module",
6
6
  "engines": {