@volter/twin-xai 0.1.0

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.
Files changed (56) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +246 -0
  3. package/client/xai-device-auth.css +246 -0
  4. package/client/xai-device-auth.tsx +138 -0
  5. package/dist/client/xai-device-auth.bundle.js +18 -0
  6. package/dist/client/xai-device-auth.css +246 -0
  7. package/dist/client/xai-device-auth.d.ts +19 -0
  8. package/dist/client/xai-device-auth.js +50 -0
  9. package/dist/client/xai-device-auth.tsx +138 -0
  10. package/dist/src/cli.d.ts +2 -0
  11. package/dist/src/cli.js +28 -0
  12. package/dist/src/index.d.ts +17 -0
  13. package/dist/src/index.js +70 -0
  14. package/dist/src/xai-budget.d.ts +60 -0
  15. package/dist/src/xai-budget.js +139 -0
  16. package/dist/src/xai-capabilities.d.ts +4 -0
  17. package/dist/src/xai-capabilities.js +1072 -0
  18. package/dist/src/xai-conformance.d.ts +13 -0
  19. package/dist/src/xai-conformance.js +148 -0
  20. package/dist/src/xai-connector.d.ts +82 -0
  21. package/dist/src/xai-connector.js +174 -0
  22. package/dist/src/xai-device-auth-css.gen.d.ts +1 -0
  23. package/dist/src/xai-device-auth-css.gen.js +6 -0
  24. package/dist/src/xai-device-auth-ui.d.ts +13 -0
  25. package/dist/src/xai-device-auth-ui.js +72 -0
  26. package/dist/src/xai-models.d.ts +57 -0
  27. package/dist/src/xai-models.js +102 -0
  28. package/dist/src/xai-oauth.d.ts +30 -0
  29. package/dist/src/xai-oauth.js +279 -0
  30. package/dist/src/xai-scenario.d.ts +33 -0
  31. package/dist/src/xai-scenario.js +139 -0
  32. package/dist/src/xai-server.d.ts +36 -0
  33. package/dist/src/xai-server.js +232 -0
  34. package/dist/src/xai-stub.d.ts +69 -0
  35. package/dist/src/xai-stub.js +210 -0
  36. package/dist/src/xai-twin.d.ts +89 -0
  37. package/dist/src/xai-twin.js +883 -0
  38. package/dist/src/xai-types.d.ts +118 -0
  39. package/dist/src/xai-types.js +6 -0
  40. package/package.json +76 -0
  41. package/src/cli.ts +27 -0
  42. package/src/index.ts +120 -0
  43. package/src/xai-budget.ts +165 -0
  44. package/src/xai-capabilities.ts +1046 -0
  45. package/src/xai-conformance.ts +136 -0
  46. package/src/xai-connector.ts +212 -0
  47. package/src/xai-device-auth-css.gen.ts +6 -0
  48. package/src/xai-device-auth-ui.ts +90 -0
  49. package/src/xai-journey.uitest.ts +155 -0
  50. package/src/xai-models.ts +154 -0
  51. package/src/xai-oauth.ts +301 -0
  52. package/src/xai-scenario.ts +148 -0
  53. package/src/xai-server.ts +258 -0
  54. package/src/xai-stub.ts +213 -0
  55. package/src/xai-twin.ts +960 -0
  56. package/src/xai-types.ts +111 -0
@@ -0,0 +1,1072 @@
1
+ // xAI capability manifest — the EXPECTED REAL-PRODUCT SURFACE (the target), authored top-down
2
+ // from what the xAI (Grok) API actually does — NOT from what this twin has built. This is the
3
+ // honest denominator: entries the twin hasn't reached are `todo` and coverage reads partial
4
+ // until the twin truly covers the API. `verify()` (required to count as done) is ground
5
+ // truth; `expected:'done'` only on capabilities we genuinely claim, so a broken one shows as
6
+ // a regression. Grow this toward the API's *full* surface every cycle.
7
+ //
8
+ // THERE ARE NO CARVE-OUTS. Every inference endpoint returns a clearly-labeled deterministic
9
+ // stub, Live Search returns deterministic labeled citations, and the image endpoint returns the
10
+ // faithful shape with labeled placeholder URLs/bytes — those ARE this twin's answers, not
11
+ // shortfalls from a "real" one. Every entry here is either done or todo. NOTE: xAI publishes NO embeddings endpoint (announced but not GA at
12
+ // authoring time), so embeddings are deliberately ABSENT from this manifest — listing them
13
+ // would be denominator padding, and the twin 404s the path exactly like the vendor. Scenario
14
+ // scripting (xai-scenario.ts) is twin scaffolding, not vendor surface, and is likewise absent.
15
+ //
16
+ // xAI's console remains outside this API-first pack, but Grok Build device authorization is a
17
+ // required browser leg of the OAuth protocol. Its entry/consent/result UI therefore receives the
18
+ // same data-coupled capability and real-browser scrutiny as the dedicated identity twins.
19
+ import { createElement } from 'react';
20
+ import { renderToStaticMarkup } from 'react-dom/server';
21
+ import { mkdtempSync, rmSync } from 'node:fs';
22
+ import { tmpdir } from 'node:os';
23
+ import { join } from 'node:path';
24
+ import { projectResources } from '@volter/world-core';
25
+ import { checkCapabilities, uiDataCoupled } from '@volter/world-tooling';
26
+ import { DeviceConsentPage } from "../client/xai-device-auth.js";
27
+ import { xaiDeviceAuthView } from "./xai-device-auth-ui.js";
28
+ import { startXaiTwinDeviceAuthorization } from "./xai-oauth.js";
29
+ import { createXaiTwinServer } from "./xai-server.js";
30
+ import { handleXaiTwinRequest } from "./xai-twin.js";
31
+ const AT = '2026-01-01T00:00:00Z'; // pinned occurredAt → deterministic ids/timestamps
32
+ /** Run a sequence of real xAI requests against an isolated root; return all responses. */
33
+ async function withRoot(steps) {
34
+ const root = mkdtempSync(join(tmpdir(), 'xai-cap-'));
35
+ const h = (s) => handleXaiTwinRequest({ method: s.m, path: s.p, body: s.raw ?? (s.b === undefined ? undefined : JSON.stringify(s.b)), root, occurredAt: AT, origin: 'http://127.0.0.1:4000' });
36
+ try {
37
+ return await steps(h);
38
+ }
39
+ catch {
40
+ return false;
41
+ }
42
+ finally {
43
+ rmSync(root, { recursive: true, force: true });
44
+ }
45
+ }
46
+ /** Collect the streaming SSE events for a chat request against an isolated root. */
47
+ function withStream(body, fn) {
48
+ return new Promise((resolve) => {
49
+ const root = mkdtempSync(join(tmpdir(), 'xai-cap-'));
50
+ const events = [];
51
+ handleXaiTwinRequest({ method: 'POST', path: '/v1/chat/completions', body: JSON.stringify(body), root, occurredAt: AT, sseSink: (e) => events.push(e) })
52
+ .then((final) => resolve(fn(events, final)))
53
+ .catch(() => resolve(false))
54
+ .finally(() => rmSync(root, { recursive: true, force: true }));
55
+ });
56
+ }
57
+ async function withRootH(steps) {
58
+ const root = mkdtempSync(join(tmpdir(), 'xai-cap-'));
59
+ const h = (s) => handleXaiTwinRequest({ method: s.m, path: s.p, body: s.raw ?? (s.b === undefined ? undefined : JSON.stringify(s.b)), root, occurredAt: AT, origin: 'http://127.0.0.1:4000', ...(s.headers ? { headers: s.headers } : {}), ...(s.requireTwinOauth === undefined ? {} : { requireTwinOauth: s.requireTwinOauth }) });
60
+ try {
61
+ return await steps(h, root);
62
+ }
63
+ catch {
64
+ return false;
65
+ }
66
+ finally {
67
+ rmSync(root, { recursive: true, force: true });
68
+ }
69
+ }
70
+ function withUiRoot(steps) {
71
+ return withRootH((request, root) => steps({ request, root }));
72
+ }
73
+ async function withLiveUi(steps) {
74
+ const root = mkdtempSync(join(tmpdir(), 'xai-cap-ui-'));
75
+ const server = await createXaiTwinServer({ root, port: 0 });
76
+ try {
77
+ return await steps(`http://127.0.0.1:${server.port}`, root);
78
+ }
79
+ catch {
80
+ return false;
81
+ }
82
+ finally {
83
+ server.stop();
84
+ rmSync(root, { recursive: true, force: true });
85
+ }
86
+ }
87
+ function pendingCode(root) {
88
+ return String(projectResources('xai', root).find((resource) => resource.type === 'oauth_device_authorization' && resource.status === 'pending')?.user_code ?? '');
89
+ }
90
+ const ok = (r) => r.status >= 200 && r.status < 300;
91
+ const id = (r) => r.body?.id;
92
+ // xAI errors are FLAT { code, error } strings — assert the shape, not just the status.
93
+ const flatError = (r) => typeof r.body?.code === 'string' && typeof r.body?.error === 'string';
94
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
95
+ // ── shorthands (mirror the openai/anthropic manifests) ──
96
+ const done = (id, area, title, dimension, tier, verify) => ({ id, area, title, dimension, tier, expected: 'done', verify });
97
+ const todo = (id, area, title, dimension, tier) => ({ id, area, title, dimension, tier, expected: 'todo' });
98
+ const CHAT = (extra = {}) => ({ model: 'grok-4-0709', messages: [{ role: 'user', content: 'hello twin' }], ...extra });
99
+ export const XAI_CAPABILITIES = [
100
+ todo('xai.images.served_bytes', 'images', 'Image URLs resolve: serve deterministic placeholder image bytes at the URLs /v1/images/generations returns (today they point at a host that answers nothing)', 'api', 'common'),
101
+ // ── Grok Build OAuth / account world ─────────────────────────────────────────────────
102
+ done('xai.oauth.device_authorization', 'oauth', 'Device authorization: issue → pending → user approval → one-use exchange', 'api', 'core', () => withRoot(async (h) => {
103
+ const start = await h({ m: 'POST', p: '/oauth2/device/code', raw: new URLSearchParams({ client_id: 'grok-build', scope: 'openid offline_access grok-cli:access' }).toString() });
104
+ const body = start.body;
105
+ if (!ok(start) || typeof body.device_code !== 'string' || !String(body.verification_uri_complete).includes(String(body.user_code)))
106
+ return false;
107
+ const pending = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'grok-build', device_code: body.device_code, grant_type: 'urn:ietf:params:oauth:grant-type:device_code' }).toString() });
108
+ if (pending.status !== 400 || pending.body.error !== 'authorization_pending')
109
+ return false;
110
+ const approved = await h({ m: 'POST', p: '/twin/oauth/authorizations', b: { approved: true, user_code: body.user_code } });
111
+ const wrongClient = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'other-client', device_code: body.device_code, grant_type: 'urn:ietf:params:oauth:grant-type:device_code' }).toString() });
112
+ const token = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'grok-build', device_code: body.device_code, grant_type: 'urn:ietf:params:oauth:grant-type:device_code' }).toString() });
113
+ const replay = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'grok-build', device_code: body.device_code, grant_type: 'urn:ietf:params:oauth:grant-type:device_code' }).toString() });
114
+ return ok(approved) && wrongClient.status === 400 && wrongClient.body.error === 'invalid_grant'
115
+ && ok(token) && typeof token.body.access_token === 'string' && replay.status === 400;
116
+ })),
117
+ done('xai.oauth.refresh', 'oauth', 'Refresh rotates the access token while retaining the virtual account and refresh authority', 'api', 'core', () => withRoot(async (h) => {
118
+ const start = await h({ m: 'POST', p: '/oauth2/device/code', raw: new URLSearchParams({ client_id: 'grok-build', scope: 'offline_access grok-cli:access' }).toString() });
119
+ const device = start.body;
120
+ await h({ m: 'POST', p: '/twin/oauth/authorizations', b: { approved: true, user_code: device.user_code } });
121
+ const first = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'grok-build', device_code: device.device_code, grant_type: 'urn:ietf:params:oauth:grant-type:device_code' }).toString() });
122
+ const credential = first.body;
123
+ const wrongClient = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'other-client', grant_type: 'refresh_token', refresh_token: credential.refresh_token }).toString() });
124
+ const refreshed = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'grok-build', grant_type: 'refresh_token', refresh_token: credential.refresh_token }).toString() });
125
+ const replay = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'grok-build', grant_type: 'refresh_token', refresh_token: credential.refresh_token }).toString() });
126
+ const next = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'grok-build', grant_type: 'refresh_token', refresh_token: refreshed.body.refresh_token }).toString() });
127
+ return wrongClient.status === 400 && wrongClient.body.error === 'invalid_grant'
128
+ && ok(refreshed) && refreshed.body.refresh_token !== credential.refresh_token
129
+ && refreshed.body.access_token !== credential.access_token && replay.status === 400
130
+ && ok(next) && next.body.refresh_token !== refreshed.body.refresh_token;
131
+ })),
132
+ todo('xai.cli_oauth.direct_device_redirect', 'oauth', 'Direct `grok login --device-auth` redirection to a sealed Twin origin (the official CLI fixes this device-login origin; brokered external auth is the supported seam)', 'api', 'common'),
133
+ // ── Grok Build device UI (the vendor-owned browser leg of the OAuth protocol) ─────────
134
+ done('xai.ui.device_entry', 'ui', 'The device-entry screen renders the issued code in a labeled field and advances through a real form POST', 'ui', 'core', async () => await withLiveUi(async (origin) => {
135
+ const start = await fetch(`${origin}/oauth2/device/code`, { method: 'POST', body: new URLSearchParams({ client_id: 'grok-build', scope: 'openid grok-cli:access' }) });
136
+ const issued = await start.json();
137
+ const entry = await fetch(String(issued.verification_uri_complete));
138
+ const markup = await entry.text();
139
+ if (entry.status !== 200 || !markup.includes('Sign in to Grok Build') || !markup.includes(`value="${String(issued.user_code)}"`)
140
+ || !markup.includes('name="user_code"') || !markup.includes('/oauth2/device/continue')
141
+ || !markup.includes('viewBox="0 0 834 318"') || !markup.includes('SpaceXAI account home link'))
142
+ return false;
143
+ const submitted = await fetch(`${origin}/oauth2/device/continue`, {
144
+ method: 'POST', redirect: 'manual', body: new URLSearchParams({ user_code: String(issued.user_code) }),
145
+ });
146
+ const location = submitted.headers.get('location') ?? '';
147
+ return submitted.status === 303 && new URL(location).pathname === '/oauth2/device/consent'
148
+ && new URL(location).searchParams.get('user_code') === issued.user_code;
149
+ })),
150
+ done('xai.ui.invalid_device_code', 'ui', 'Blank entry is disabled and an invalid or expired code follows the official clear-field PRG error state', 'ui', 'core', async () => await withLiveUi(async (origin, root) => {
151
+ const blank = await fetch(`${origin}/oauth2/device`);
152
+ const blankMarkup = await blank.text();
153
+ if (!blankMarkup.includes('placeholder="Enter device code"') || !/<button(?=[^>]*disabled)[^>]*>Continue<\/button>/.test(blankMarkup))
154
+ return false;
155
+ const submitted = await fetch(`${origin}/oauth2/device/continue`, {
156
+ method: 'POST', redirect: 'manual', body: new URLSearchParams({ user_code: 'NOPE-0000' }),
157
+ });
158
+ const location = submitted.headers.get('location') ?? '';
159
+ if (submitted.status !== 303 || new URL(location).pathname !== '/oauth2/device' || new URL(location).search !== '?error=invalid_code')
160
+ return false;
161
+ const invalid = await fetch(location);
162
+ const markup = await invalid.text();
163
+ if (invalid.status !== 200 || !markup.includes('Invalid or expired code. Please try again.')
164
+ || markup.includes('value="NOPE-0000"') || !/<button(?=[^>]*disabled)[^>]*>Continue<\/button>/.test(markup))
165
+ return false;
166
+ const expired = await startXaiTwinDeviceAuthorization({
167
+ body: new URLSearchParams({ client_id: 'grok-build', scope: 'grok-cli:access' }),
168
+ occurredAt: '2000-01-01T00:00:00.000Z', origin, root,
169
+ });
170
+ const expiredCode = String(expired.body.user_code);
171
+ const expiredSubmit = await fetch(`${origin}/oauth2/device/continue`, {
172
+ method: 'POST', redirect: 'manual', body: new URLSearchParams({ user_code: expiredCode }),
173
+ });
174
+ const expiredLocation = expiredSubmit.headers.get('location') ?? '';
175
+ const expiredPage = await fetch(expiredLocation);
176
+ const expiredMarkup = await expiredPage.text();
177
+ return expiredSubmit.status === 303 && new URL(expiredLocation).search === '?error=invalid_code'
178
+ && expiredPage.status === 200 && expiredMarkup.includes('Invalid or expired code. Please try again.')
179
+ && !expiredMarkup.includes(`value="${expiredCode}"`) && /<button(?=[^>]*disabled)[^>]*>Continue<\/button>/.test(expiredMarkup);
180
+ })),
181
+ done('xai.ui.consent_scope_projection', 'ui', 'The consent screen derives vendor-visible permission rows from the pending request scope instead of a static lookalike', 'ui', 'core', uiDataCoupled({
182
+ withRoot: withUiRoot,
183
+ seed: ({ request }) => request({ m: 'POST', p: '/oauth2/device/code', raw: new URLSearchParams({ client_id: 'grok-build', scope: 'openid email grok-cli:access' }).toString() }),
184
+ fetch: async ({ root }) => xaiDeviceAuthView(pendingCode(root), root, undefined, AT),
185
+ render: (view) => renderToStaticMarkup(createElement(DeviceConsentPage, { view })),
186
+ assert: (view, markup) => view.permissions.length === 3 && !!markup
187
+ && markup.includes('Verify your identity') && markup.includes('Read your email address') && markup.includes('Use the xAI API')
188
+ && !markup.includes('Read your profile (name, avatar)') && !markup.includes("Maintain access when you&#x27;re not present")
189
+ && !markup.includes('Read and write access Grok.com'),
190
+ })),
191
+ done('xai.ui.consent_decisions', 'ui', 'Deny and Allow are real named form submits carrying the projected device code into the OAuth decision route', 'ui', 'core', async () => await withLiveUi(async (origin, root) => {
192
+ const start = await fetch(`${origin}/oauth2/device/code`, { method: 'POST', body: new URLSearchParams({ client_id: 'grok-build', scope: 'openid profile email offline_access grok-cli:access api:access conversations:write' }) });
193
+ const issued = await start.json();
194
+ const consent = await fetch(`${origin}/oauth2/device/consent?user_code=${encodeURIComponent(String(issued.user_code))}`);
195
+ const markup = await consent.text();
196
+ if (consent.status !== 200 || !markup.includes('/oauth2/device/decision') || !markup.includes(`value="${String(issued.user_code)}"`)
197
+ || !/<button(?=[^>]*name="decision")(?=[^>]*value="deny")[^>]*>Deny<\/button>/.test(markup)
198
+ || !/<button(?=[^>]*name="decision")(?=[^>]*value="approve")[^>]*>Allow<\/button>/.test(markup))
199
+ return false;
200
+ const denied = await fetch(`${origin}/oauth2/device/decision`, { method: 'POST', redirect: 'manual', body: new URLSearchParams({ decision: 'deny', user_code: String(issued.user_code) }) });
201
+ const location = denied.headers.get('location') ?? '';
202
+ const done = await fetch(location);
203
+ const doneMarkup = await done.text();
204
+ const state = xaiDeviceAuthView(String(issued.user_code), root);
205
+ if (denied.status !== 303 || new URL(location).pathname !== '/oauth2/device/done'
206
+ || state.status !== 'denied' || done.status !== 200 || !doneMarkup.includes('Authorization Denied'))
207
+ return false;
208
+ const approveStart = await fetch(`${origin}/oauth2/device/code`, { method: 'POST', body: new URLSearchParams({ client_id: 'grok-build', scope: 'openid grok-cli:access' }) });
209
+ const approveIssued = await approveStart.json();
210
+ const allowed = await fetch(`${origin}/oauth2/device/decision`, { method: 'POST', redirect: 'manual', body: new URLSearchParams({ decision: 'approve', user_code: String(approveIssued.user_code) }) });
211
+ const allowedLocation = allowed.headers.get('location') ?? '';
212
+ const allowedDone = await fetch(allowedLocation);
213
+ const allowedMarkup = await allowedDone.text();
214
+ return allowed.status === 303 && new URL(allowedLocation).pathname === '/oauth2/device/done'
215
+ && xaiDeviceAuthView(String(approveIssued.user_code), root).status === 'approved'
216
+ && allowedDone.status === 200 && allowedMarkup.includes('Device Authorized');
217
+ })),
218
+ done('xai.ui.account_shell_sign_out', 'ui', 'The vendor-shaped account sign-out route leaves the pending OAuth grant undecided and returns to blank device entry', 'ui', 'common', async () => await withLiveUi(async (origin, root) => {
219
+ const start = await fetch(`${origin}/oauth2/device/code`, { method: 'POST', body: new URLSearchParams({ client_id: 'grok-build', scope: 'openid grok-cli:access' }) });
220
+ const issued = await start.json();
221
+ const consent = await fetch(`${origin}/oauth2/device/consent?user_code=${encodeURIComponent(String(issued.user_code))}`);
222
+ const markup = await consent.text();
223
+ if (!markup.includes('/sign-out?redirect=oauth2-provider&amp;return_to='))
224
+ return false;
225
+ const href = markup.match(/href="([^"]*\/sign-out[^"]*)"/)?.[1]?.replaceAll('&amp;', '&');
226
+ if (!href)
227
+ return false;
228
+ const signOutUrl = new URL(href, origin);
229
+ if (signOutUrl.pathname !== '/sign-out' || signOutUrl.searchParams.get('redirect') !== 'oauth2-provider'
230
+ // every link on the device pages is built from the World's public base (6c6ad4b2b), return_to included
231
+ || signOutUrl.searchParams.get('return_to') !== `${origin}/oauth2/device/consent?user_code=${String(issued.user_code)}`)
232
+ return false;
233
+ const signOut = await fetch(signOutUrl, { redirect: 'manual' });
234
+ const location = signOut.headers.get('location') ?? '';
235
+ return signOut.status === 303 && new URL(location).pathname === '/oauth2/device'
236
+ && xaiDeviceAuthView(String(issued.user_code), root).status === 'pending';
237
+ })),
238
+ done('xai.ui.completion_state', 'ui', 'The Twin-local completion route derives Device Authorized from the approved OAuth row, never from a query marker', 'ui', 'common', async () => await withLiveUi(async (origin, root) => {
239
+ const start = await fetch(`${origin}/oauth2/device/code`, { method: 'POST', body: new URLSearchParams({ client_id: 'grok-build', scope: 'openid grok-cli:access' }) });
240
+ const issued = await start.json();
241
+ await fetch(`${origin}/oauth2/device/decision`, { method: 'POST', redirect: 'manual', body: new URLSearchParams({ decision: 'approve', user_code: String(issued.user_code) }) });
242
+ const approved = await fetch(`${origin}/oauth2/device/done?user_code=${encodeURIComponent(String(issued.user_code))}`);
243
+ const approvedMarkup = await approved.text();
244
+ const forged = await fetch(`${origin}/oauth2/device/done?approved=true&user_code=NOT-REAL`);
245
+ const forgedMarkup = await forged.text();
246
+ const state = xaiDeviceAuthView(String(issued.user_code), root);
247
+ return state.status === 'approved' && approved.status === 200 && approvedMarkup.includes('Device Authorized')
248
+ && approvedMarkup.includes('Return to your terminal to continue.') && forged.status === 404 && !forgedMarkup.includes('Device Authorized');
249
+ })),
250
+ // ── Chat Completions (the OpenAI-compatible envelope + xAI deltas — faithful) ──────────
251
+ done('xai.chat.create', 'chat', 'Chat: create → faithful envelope (UUID id/object/created/model/choices/usage details)', 'api', 'core', () => withRoot(async (h) => {
252
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT() });
253
+ if (!ok(r))
254
+ return false;
255
+ const b = r.body;
256
+ if (b.object !== 'chat.completion' || b.model !== 'grok-4-0709' || typeof b.created !== 'number')
257
+ return false;
258
+ if (!UUID_RE.test(String(b.id)))
259
+ return false; // xAI ids are UUID-formatted, not 'chatcmpl-…'
260
+ const c = b.choices?.[0];
261
+ if (!c || c.message?.role !== 'assistant' || typeof c.message?.content !== 'string' || c.finish_reason !== 'stop')
262
+ return false;
263
+ const u = b.usage;
264
+ if (typeof u?.prompt_tokens !== 'number' || u.total_tokens !== u.prompt_tokens + u.completion_tokens)
265
+ return false;
266
+ // the xAI usage detail objects must be PRESENT and populated — typed check, so a missing
267
+ // detail object cannot slip past an optional-chained comparison (§9 hardening)
268
+ if (typeof u.prompt_tokens_details?.text_tokens !== 'number' || u.prompt_tokens_details.text_tokens <= 0)
269
+ return false;
270
+ return typeof u.completion_tokens_details?.reasoning_tokens === 'number' && typeof u.num_sources_used === 'number';
271
+ })),
272
+ done('xai.chat.stub_labeled', 'chat', 'Stub completion is clearly labeled as a twin stub (not real Grok output)', 'api', 'core', () => withRoot(async (h) => {
273
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT() });
274
+ const text = r.body.choices?.[0]?.message?.content;
275
+ return ok(r) && text.includes('[twin-stub') && text.includes('hello twin');
276
+ })),
277
+ done('xai.cli_proxy.grok_build', 'cli_proxy', 'Official Grok CLI proxy token marker + x-grok-model-override route the built-in grok-build model', 'api', 'core', () => withRootH(async (h, root) => {
278
+ const start = await h({ m: 'POST', p: '/oauth2/device/code', raw: new URLSearchParams({ client_id: 'grok-build', scope: 'offline_access grok-cli:access' }).toString() });
279
+ const device = start.body;
280
+ await h({ m: 'POST', p: '/twin/oauth/authorizations', b: { approved: true, user_code: device.user_code } });
281
+ const exchange = await h({ m: 'POST', p: '/oauth2/token', raw: new URLSearchParams({ client_id: 'grok-build', device_code: device.device_code, grant_type: 'urn:ietf:params:oauth:grant-type:device_code' }).toString() });
282
+ const accessToken = String(exchange.body.access_token);
283
+ const okResponse = await h({
284
+ m: 'POST',
285
+ p: '/v1/chat/completions',
286
+ b: CHAT({ model: 'ignored-by-cli-proxy' }),
287
+ headers: {
288
+ authorization: `Bearer ${accessToken}`,
289
+ 'x-grok-model-override': 'grok-build',
290
+ 'x-xai-token-auth': 'xai-grok-cli',
291
+ },
292
+ requireTwinOauth: true,
293
+ });
294
+ if (okResponse.status !== 200 || okResponse.body.model !== 'grok-code-fast-1')
295
+ return false;
296
+ const usage = projectResources('xai', root).find((resource) => resource.type === 'oauth_usage');
297
+ if (usage?.requests !== 1 || Number(usage.total_tokens) <= 0)
298
+ return false;
299
+ const wrongMarker = await h({
300
+ m: 'POST',
301
+ p: '/v1/chat/completions',
302
+ b: CHAT(),
303
+ headers: { authorization: `Bearer ${accessToken}`, 'x-xai-token-auth': 'wrong' },
304
+ });
305
+ const missingMarker = await h({
306
+ m: 'POST',
307
+ p: '/v1/chat/completions',
308
+ b: CHAT(),
309
+ headers: { authorization: 'Bearer foreign-token', 'x-grok-model-override': 'grok-build' },
310
+ requireTwinOauth: true,
311
+ });
312
+ const foreignBearer = await h({
313
+ m: 'POST',
314
+ p: '/v1/chat/completions',
315
+ b: CHAT(),
316
+ headers: { authorization: 'Bearer foreign-token', 'x-grok-model-override': 'grok-build', 'x-xai-token-auth': 'xai-grok-cli' },
317
+ requireTwinOauth: true,
318
+ });
319
+ return wrongMarker.status === 401 && String(wrongMarker.body.error).includes('token-auth marker')
320
+ && missingMarker.status === 401 && String(missingMarker.body.error).includes('token-auth marker')
321
+ && foreignBearer.status === 401 && String(foreignBearer.body.error).includes('sealed xAI Twin world');
322
+ })),
323
+ done('xai.chat.validation', 'chat', 'Chat validation (model required, non-empty messages) → flat {code,error} 400', 'api', 'core', () => withRoot(async (h) => {
324
+ const noModel = await h({ m: 'POST', p: '/v1/chat/completions', b: { messages: [{ role: 'user', content: 'x' }] } });
325
+ const noMsg = await h({ m: 'POST', p: '/v1/chat/completions', b: { model: 'grok-4-0709' } });
326
+ const empty = await h({ m: 'POST', p: '/v1/chat/completions', b: { model: 'grok-4-0709', messages: [] } });
327
+ if (noModel.status !== 400 || noMsg.status !== 400 || empty.status !== 400)
328
+ return false;
329
+ // the envelope is xAI's FLAT {code, error} strings — never OpenAI's nested error object
330
+ return flatError(noModel) && noModel.body.code === 'Client specified an invalid argument';
331
+ })),
332
+ done('xai.chat.model_not_found', 'chat', 'Unknown model → vendor 404 ("does not exist or your team has no access")', 'api', 'core', () => withRoot(async (h) => {
333
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ model: 'grok-99' }) });
334
+ if (r.status !== 404 || !flatError(r))
335
+ return false;
336
+ const b = r.body;
337
+ if (b.code !== 'Some requested entity was not found' || !String(b.error).includes('grok-99'))
338
+ return false;
339
+ // an image model cannot chat (400, not a fake completion)
340
+ const img = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ model: 'grok-2-image-1212' }) });
341
+ return img.status === 400;
342
+ })),
343
+ done('xai.chat.n_choices', 'chat', 'Chat: n returns multiple choices (indexed)', 'api', 'common', () => withRoot(async (h) => {
344
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ n: 3 }) });
345
+ const choices = r.body.choices;
346
+ if (!ok(r) || choices.length !== 3 || choices[0].index !== 0 || choices[2].index !== 2)
347
+ return false;
348
+ const bad = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ n: 0 }) });
349
+ return bad.status === 400;
350
+ })),
351
+ done('xai.chat.max_tokens', 'chat', 'Chat: max_tokens/max_completion_tokens caps output (finish_reason length)', 'api', 'common', () => withRoot(async (h) => {
352
+ const long = { messages: [{ role: 'user', content: 'please produce a long answer that exceeds one token' }] };
353
+ const a = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ max_tokens: 1, ...long }) });
354
+ const b = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ max_completion_tokens: 1, ...long }) });
355
+ if (!ok(a) || !ok(b))
356
+ return false;
357
+ if (a.body.choices[0].finish_reason !== 'length' || b.body.choices[0].finish_reason !== 'length')
358
+ return false;
359
+ const bad = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ max_tokens: 0 }) });
360
+ return bad.status === 400;
361
+ })),
362
+ done('xai.chat.system_multi_turn', 'chat', 'Chat: system messages count toward prompt_tokens; multi-turn history accepted', 'api', 'common', () => withRoot(async (h) => {
363
+ const without = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT() });
364
+ const withSys = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ messages: [{ role: 'system', content: 'You are a careful, verbose assistant.' }, { role: 'user', content: 'hello twin' }] }) });
365
+ if (!ok(without) || !ok(withSys))
366
+ return false;
367
+ if (withSys.body.usage.prompt_tokens <= without.body.usage.prompt_tokens)
368
+ return false;
369
+ const multi = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ messages: [{ role: 'user', content: 'first' }, { role: 'assistant', content: 'reply' }, { role: 'user', content: 'second' }] }) });
370
+ return ok(multi) && multi.body.object === 'chat.completion' && multi.body.choices[0].message.content.includes('second');
371
+ })),
372
+ done('xai.chat.stop', 'chat', 'Chat: stop sequences truncate at the earliest hit', 'api', 'common', () => withRoot(async (h) => {
373
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ stop: ['Echoing'] }) });
374
+ const text = r.body.choices[0].message.content;
375
+ if (!ok(r) || text.includes('Echoing') || text.length === 0)
376
+ return false;
377
+ const bad = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ stop: 42 }) });
378
+ return bad.status === 400;
379
+ })),
380
+ done('xai.chat.deterministic_usage', 'chat', 'Chat: usage token counts + UUID id deterministic for a fixed request', 'api', 'common', () => withRoot(async (h) => {
381
+ const a = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT() });
382
+ const b = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT() });
383
+ if (!ok(a) || !ok(b))
384
+ return false;
385
+ const ua = a.body.usage;
386
+ const ub = b.body.usage;
387
+ // usage must be a REAL populated token-count object, not just "the same as each other"
388
+ if (!ua || typeof ua.prompt_tokens !== 'number' || ua.prompt_tokens <= 0)
389
+ return false;
390
+ if (typeof ua.completion_tokens !== 'number' || ua.completion_tokens <= 0)
391
+ return false;
392
+ if (ua.total_tokens !== ua.prompt_tokens + ua.completion_tokens)
393
+ return false;
394
+ if (JSON.stringify(ua) !== JSON.stringify(ub))
395
+ return false;
396
+ const ida = id(a);
397
+ return typeof ida === 'string' && UUID_RE.test(ida) && ida === id(b);
398
+ })),
399
+ done('xai.chat.structured_outputs', 'chat', 'Structured outputs: response_format json_object / json_schema → valid JSON content', 'api', 'common', () => withRoot(async (h) => {
400
+ const obj = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ response_format: { type: 'json_object' } }) });
401
+ if (!ok(obj))
402
+ return false;
403
+ let parsedObj;
404
+ try {
405
+ parsedObj = JSON.parse(obj.body.choices[0].message.content);
406
+ }
407
+ catch {
408
+ return false;
409
+ }
410
+ if (typeof parsedObj !== 'object')
411
+ return false;
412
+ const schema = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ response_format: { type: 'json_schema', json_schema: { name: 'r', schema: { type: 'object', properties: { title: { type: 'string' }, count: { type: 'integer' }, ok: { type: 'boolean' } } } } } }) });
413
+ let parsed;
414
+ try {
415
+ parsed = JSON.parse(schema.body.choices[0].message.content);
416
+ }
417
+ catch {
418
+ return false;
419
+ }
420
+ if (!('title' in parsed) || typeof parsed.title !== 'string' || parsed.count !== 0 || parsed.ok !== false)
421
+ return false;
422
+ const bad = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ response_format: { type: 'yaml' } }) });
423
+ return bad.status === 400;
424
+ })),
425
+ done('xai.chat.seed', 'chat', 'seed accepted + reflected (same seed deterministic; distinct seed → distinct id)', 'api', 'niche', () => withRoot(async (h) => {
426
+ const a = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ seed: 42 }) });
427
+ const a2 = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ seed: 42 }) });
428
+ const b = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ seed: 7 }) });
429
+ if (!ok(a) || !ok(b))
430
+ return false;
431
+ if (id(a) !== id(a2) || id(a) === id(b))
432
+ return false;
433
+ const bad = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ seed: 1.5 }) });
434
+ return bad.status === 400 && flatError(bad);
435
+ })),
436
+ done('xai.chat.reasoning_effort', 'chat', 'reasoning_effort: grok-3-mini exposes reasoning_content + reasoning_tokens; grok-4 REJECTS the param (400) and never exposes content', 'api', 'common', () => withRoot(async (h) => {
437
+ // grok-3-mini + effort → reasoning_content (labeled stub) + effort-scaled reasoning_tokens
438
+ const hi = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ model: 'grok-3-mini', reasoning_effort: 'high' }) });
439
+ const lo = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ model: 'grok-3-mini', reasoning_effort: 'low' }) });
440
+ if (!ok(hi) || !ok(lo))
441
+ return false;
442
+ const hiMsg = hi.body.choices[0].message;
443
+ if (typeof hiMsg.reasoning_content !== 'string' || !hiMsg.reasoning_content.includes('[twin-stub'))
444
+ return false;
445
+ const hiRt = hi.body.usage.completion_tokens_details.reasoning_tokens;
446
+ const loRt = lo.body.usage.completion_tokens_details.reasoning_tokens;
447
+ if (!(hiRt > loRt) || loRt <= 0)
448
+ return false;
449
+ // reasoning tokens are COUNTED INTO completion_tokens (vendor accounting)
450
+ if (hi.body.usage.completion_tokens <= hiRt)
451
+ return false;
452
+ // grok-4: a reasoning model that REJECTS reasoning_effort and hides the trace
453
+ const g4bad = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ reasoning_effort: 'high' }) });
454
+ if (g4bad.status !== 400 || !String(g4bad.body.error).includes('reasoning_effort'))
455
+ return false;
456
+ const g4 = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT() });
457
+ if (g4.body.choices[0].message.reasoning_content !== undefined)
458
+ return false;
459
+ if (g4.body.usage.completion_tokens_details.reasoning_tokens <= 0)
460
+ return false;
461
+ // grok-3-mini WITHOUT the param still exposes its (stubbed) reasoning trace — the model
462
+ // always thinks; the param only scales the budget (§9 hardening).
463
+ const noParam = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ model: 'grok-3-mini' }) });
464
+ if (typeof noParam.body.choices[0].message.reasoning_content !== 'string')
465
+ return false;
466
+ // invalid effort value → 400
467
+ const bad = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ model: 'grok-3-mini', reasoning_effort: 'medium' }) });
468
+ return bad.status === 400;
469
+ })),
470
+ done('xai.chat.content_parts', 'chat', 'Chat: content-part array input (text + image_url vision parts → image_tokens counted)', 'api', 'common', () => withRoot(async (h) => {
471
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ messages: [{ role: 'user', content: [{ type: 'text', text: 'describe this' }, { type: 'image_url', image_url: { url: 'data:image/png;base64,iVBORw0KGgo=' } }] }] }) });
472
+ if (!ok(r))
473
+ return false;
474
+ const d = r.body.usage.prompt_tokens_details;
475
+ return d.image_tokens > 0 && d.text_tokens > 0 && r.body.usage.prompt_tokens === d.text_tokens + d.image_tokens;
476
+ })),
477
+ // ── Streaming ─────────────────────────────────────────────────────────────────────────
478
+ done('xai.streaming.chunks', 'streaming', 'Streaming chat.completion.chunk sequence ends with [DONE]', 'api', 'core', () => withStream(CHAT({ stream: true }), (events) => {
479
+ const data = events.filter((e) => !e.done);
480
+ // vendor ordering: the FIRST chunk carries the role delta (§9 hardening: pinned, not just present)
481
+ const roleFirst = (data[0]?.data.choices)[0]?.delta?.role === 'assistant';
482
+ const allChunks = data.every((e) => e.data.object === 'chat.completion.chunk');
483
+ const doneLast = events.length > 0 && events[events.length - 1].done === true;
484
+ return roleFirst && allChunks && doneLast && data.length > 2;
485
+ })),
486
+ done('xai.streaming.reconstruct', 'streaming', 'Streaming content deltas reconstruct the full message text', 'api', 'core', () => withStream(CHAT({ stream: true }), (events, final) => {
487
+ const text = events.filter((e) => !e.done).map((e) => e.data.choices[0]?.delta?.content ?? '').join('');
488
+ const full = final.body.choices[0].message.content;
489
+ return text === full && text.includes('[twin-stub');
490
+ })),
491
+ done('xai.streaming.finish_reason', 'streaming', 'Streaming emits a terminal finish_reason chunk', 'api', 'core', () => withStream(CHAT({ stream: true }), (events) => {
492
+ const data = events.filter((e) => !e.done);
493
+ const last = data[data.length - 1];
494
+ return (last?.data.choices)[0]?.finish_reason === 'stop'
495
+ && data.slice(0, -1).every((e) => e.data.choices[0]?.finish_reason === null);
496
+ })),
497
+ done('xai.streaming.tool_call_deltas', 'streaming', 'Streaming tool calls arrive as COMPLETE indexed delta.tool_calls chunks (xAI delta: never split across deltas)', 'api', 'common', () => withStream(CHAT({ stream: true, tools: [{ type: 'function', function: { name: 'get_weather', parameters: { type: 'object', properties: { city: { type: 'string' } } } } }] }), (events, final) => {
498
+ const deltas = events.filter((e) => !e.done).flatMap((e) => e.data.choices[0]?.delta?.tool_calls ?? []);
499
+ if (deltas.length !== 1)
500
+ return false;
501
+ // xAI streams each tool call COMPLETE in one delta (id + type + name + full arguments) —
502
+ // the real @ai-sdk/xai chunk schema REJECTS OpenAI-style split deltas.
503
+ const d = deltas[0];
504
+ if (d.function?.name !== 'get_weather' || typeof d.id !== 'string' || d.type !== 'function' || d.index !== 0)
505
+ return false;
506
+ try {
507
+ JSON.parse(d.function.arguments);
508
+ }
509
+ catch {
510
+ return false;
511
+ }
512
+ return final.body.choices[0].finish_reason === 'tool_calls';
513
+ })),
514
+ done('xai.streaming.include_usage', 'streaming', 'stream_options.include_usage emits a final usage-only chunk', 'api', 'common', () => withStream(CHAT({ stream: true, stream_options: { include_usage: true } }), (events, final) => {
515
+ const usageChunk = events.filter((e) => !e.done).find((e) => e.data.usage !== undefined && e.data.choices.length === 0);
516
+ if (!usageChunk)
517
+ return false;
518
+ const u = usageChunk.data.usage;
519
+ const fu = final.body.usage;
520
+ return u.total_tokens === fu.total_tokens && u.prompt_tokens === fu.prompt_tokens && u.total_tokens > 0;
521
+ })),
522
+ // ── Tools (function calling) ──────────────────────────────────────────────────────────
523
+ done('xai.tools.tool_calls', 'tools', 'Function calling: tools → tool_calls + finish_reason tool_calls', 'api', 'core', () => withRoot(async (h) => {
524
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ tools: [{ type: 'function', function: { name: 'get_weather', parameters: { type: 'object', properties: { city: { type: 'string' } } } } }] }) });
525
+ if (!ok(r))
526
+ return false;
527
+ const c = r.body.choices[0];
528
+ if (c.finish_reason !== 'tool_calls' || c.message.content !== null)
529
+ return false;
530
+ const tc = c.message.tool_calls?.[0];
531
+ if (!tc || tc.type !== 'function' || tc.function.name !== 'get_weather' || !String(tc.id).startsWith('call_'))
532
+ return false;
533
+ // no tools → plain text, never a fabricated tool call
534
+ const plain = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT() });
535
+ return plain.body.choices[0].message.tool_calls === undefined;
536
+ })),
537
+ done('xai.tools.tool_choice', 'tools', 'tool_choice none/required/named gates + forces the call', 'api', 'common', () => withRoot(async (h) => {
538
+ const tools = [{ type: 'function', function: { name: 'a', parameters: { type: 'object' } } }, { type: 'function', function: { name: 'b', parameters: { type: 'object' } } }];
539
+ const none = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ tools, tool_choice: 'none' }) });
540
+ if (!ok(none) || none.body.choices[0].message.tool_calls !== undefined)
541
+ return false;
542
+ if (typeof none.body.choices[0].message.content !== 'string')
543
+ return false;
544
+ const named = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ tools, tool_choice: { type: 'function', function: { name: 'b' } } }) });
545
+ const namedCalls = named.body.choices[0].message.tool_calls;
546
+ if (namedCalls.length !== 1 || namedCalls[0].function.name !== 'b')
547
+ return false;
548
+ const required = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ tools, tool_choice: 'required' }) });
549
+ if (required.body.choices[0].finish_reason !== 'tool_calls')
550
+ return false;
551
+ const bad = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ tools, tool_choice: 'sometimes' }) });
552
+ return bad.status === 400;
553
+ })),
554
+ done('xai.tools.parallel_tool_calls', 'tools', 'parallel_tool_calls (default on → one call per tool; false → single call)', 'api', 'common', () => withRoot(async (h) => {
555
+ const tools = [{ type: 'function', function: { name: 'a', parameters: { type: 'object' } } }, { type: 'function', function: { name: 'b', parameters: { type: 'object' } } }];
556
+ const par = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ tools }) });
557
+ const parCalls = par.body.choices[0].message.tool_calls;
558
+ if (parCalls.length !== 2 || parCalls[0].id === parCalls[1].id)
559
+ return false;
560
+ if (parCalls.map((c) => c.function.name).join(',') !== 'a,b')
561
+ return false;
562
+ const single = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ tools, parallel_tool_calls: false }) });
563
+ return single.body.choices[0].message.tool_calls.length === 1;
564
+ })),
565
+ done('xai.tools.schema_arguments', 'tools', 'Stub arguments validate against the declared JSON schema (typed placeholders)', 'api', 'common', () => withRoot(async (h) => {
566
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ tools: [{ type: 'function', function: { name: 'plan', parameters: { type: 'object', properties: { city: { type: 'string' }, days: { type: 'integer' }, indoor: { type: 'boolean' }, kind: { type: 'string', enum: ['walk', 'museum'] } } } } }] }) });
567
+ if (!ok(r))
568
+ return false;
569
+ const args = JSON.parse(r.body.choices[0].message.tool_calls[0].function.arguments);
570
+ return typeof args.city === 'string' && args.days === 0 && args.indoor === false && args.kind === 'walk';
571
+ })),
572
+ done('xai.tools.tool_result_roundtrip', 'tools', 'A tool-role result turn is accepted and answered with text', 'api', 'common', () => withRoot(async (h) => {
573
+ const base = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ messages: [{ role: 'user', content: 'weather in tokyo?' }] }) });
574
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ messages: [
575
+ { role: 'user', content: 'weather in tokyo?' },
576
+ { role: 'assistant', content: null, tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'get_weather', arguments: '{"city":"tokyo"}' } }] },
577
+ { role: 'tool', tool_call_id: 'call_1', content: '{"temp_c": 21}' },
578
+ ] }) });
579
+ if (!ok(base) || !ok(r))
580
+ return false;
581
+ const c = r.body.choices[0];
582
+ // the follow-up turn is TEXT (the tools were not re-sent), and the tool_call + tool-result
583
+ // history genuinely counts toward prompt_tokens (vs the single-message baseline — §9 hardening)
584
+ return typeof c.message.content === 'string' && c.finish_reason === 'stop'
585
+ && r.body.usage.prompt_tokens > base.body.usage.prompt_tokens;
586
+ })),
587
+ // ── Live Search (xAI delta: search_parameters + citations) ────────────────────────────
588
+ done('xai.search.parameters', 'search', 'search_parameters mode on → citations[] + usage.num_sources_used; off → none', 'api', 'common', () => withRoot(async (h) => {
589
+ const on = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ search_parameters: { mode: 'on' } }) });
590
+ if (!ok(on))
591
+ return false;
592
+ const cites = on.body.citations;
593
+ if (!Array.isArray(cites) || cites.length === 0 || !cites.every((c) => c.includes('twin.invalid')))
594
+ return false;
595
+ if (on.body.usage.num_sources_used !== cites.length)
596
+ return false;
597
+ // deterministic: the same request yields the same query-seeded citations (§9 hardening)
598
+ const again = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ search_parameters: { mode: 'on' } }) });
599
+ if (JSON.stringify(again.body.citations) !== JSON.stringify(cites))
600
+ return false;
601
+ const off = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ search_parameters: { mode: 'off' } }) });
602
+ if (off.body.citations !== undefined || off.body.usage.num_sources_used !== 0)
603
+ return false;
604
+ const plain = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT() });
605
+ return plain.body.citations === undefined;
606
+ })),
607
+ done('xai.search.mode_validation', 'search', 'search_parameters validation (bad mode / bad sources → 400)', 'api', 'common', () => withRoot(async (h) => {
608
+ const badMode = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ search_parameters: { mode: 'always' } }) });
609
+ const badSources = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ search_parameters: { mode: 'on', sources: [{ type: 'gopher' }] } }) });
610
+ const notObj = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ search_parameters: 'on' }) });
611
+ return badMode.status === 400 && badSources.status === 400 && notObj.status === 400 && flatError(badMode);
612
+ })),
613
+ done('xai.search.sources', 'search', 'sources (web/x/news/rss) select which source types are consulted', 'api', 'common', () => withRoot(async (h) => {
614
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ search_parameters: { mode: 'on', sources: [{ type: 'x' }, { type: 'news' }] } }) });
615
+ if (!ok(r))
616
+ return false;
617
+ const cites = r.body.citations;
618
+ if (!cites.some((c) => c.includes('/x/')) || !cites.some((c) => c.includes('/news/')) || cites.some((c) => c.includes('/web/')))
619
+ return false;
620
+ const def = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ search_parameters: { mode: 'on' } }) });
621
+ return def.body.citations.every((c) => c.includes('/web/'));
622
+ })),
623
+ done('xai.search.citations_toggle', 'search', 'return_citations:false omits citations[] but still reports num_sources_used', 'api', 'niche', () => withRoot(async (h) => {
624
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ search_parameters: { mode: 'on', return_citations: false } }) });
625
+ if (!ok(r))
626
+ return false;
627
+ return r.body.citations === undefined && r.body.usage.num_sources_used > 0;
628
+ })),
629
+ // ── Deferred completions (xAI delta — stateful, kernel-backed) ────────────────────────
630
+ done('xai.deferred.create', 'deferred', 'deferred:true → { request_id } (no completion body yet)', 'api', 'common', () => withRoot(async (h) => {
631
+ const r = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ deferred: true }) });
632
+ if (!ok(r))
633
+ return false;
634
+ const b = r.body;
635
+ if (typeof b.request_id !== 'string' || !UUID_RE.test(b.request_id))
636
+ return false;
637
+ // a deferred request answers with the id envelope ONLY — never an inline completion
638
+ if (b.choices !== undefined || b.object === 'chat.completion')
639
+ return false;
640
+ // two identical deferred requests still get DISTINCT request ids
641
+ const r2 = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ deferred: true }) });
642
+ return r2.body.request_id !== b.request_id;
643
+ })),
644
+ done('xai.deferred.lifecycle', 'deferred', 'Deferred retrieval: 202 pending → 200 completion → consumed (404 after; unknown id 404)', 'api', 'common', () => withRoot(async (h) => {
645
+ const create = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ deferred: true }) });
646
+ const reqId = create.body.request_id;
647
+ const p1 = await h({ m: 'GET', p: `/v1/chat/deferred-completion/${reqId}` });
648
+ if (p1.status !== 202)
649
+ return false;
650
+ const p2 = await h({ m: 'GET', p: `/v1/chat/deferred-completion/${reqId}` });
651
+ if (p2.status !== 200)
652
+ return false;
653
+ const b = p2.body;
654
+ if (b.object !== 'chat.completion' || !b.choices[0].message.content.includes('[twin-stub'))
655
+ return false;
656
+ if (b.usage.total_tokens !== b.usage.prompt_tokens + b.usage.completion_tokens)
657
+ return false;
658
+ // single-retrieval semantics: the result is consumed
659
+ const p3 = await h({ m: 'GET', p: `/v1/chat/deferred-completion/${reqId}` });
660
+ if (p3.status !== 404 || !flatError(p3))
661
+ return false;
662
+ const unknown = await h({ m: 'GET', p: '/v1/chat/deferred-completion/00000000-0000-0000-0000-000000000000' });
663
+ return unknown.status === 404;
664
+ })),
665
+ // ── Anthropic-compatible Messages (POST /v1/messages) ─────────────────────────────────
666
+ done('xai.messages_compat.create', 'messages_compat', 'Messages: create → Anthropic-shaped envelope (content blocks, stop_reason, usage)', 'api', 'common', () => withRoot(async (h) => {
667
+ const r = await h({ m: 'POST', p: '/v1/messages', b: { model: 'grok-4-0709', max_tokens: 256, messages: [{ role: 'user', content: 'hello messages' }] } });
668
+ if (!ok(r))
669
+ return false;
670
+ const b = r.body;
671
+ if (b.type !== 'message' || b.role !== 'assistant' || b.model !== 'grok-4-0709' || !String(b.id).startsWith('msg_'))
672
+ return false;
673
+ const block = b.content[0];
674
+ if (block?.type !== 'text' || !String(block.text).includes('[twin-stub') || !String(block.text).includes('hello messages'))
675
+ return false;
676
+ if (b.stop_reason !== 'end_turn' || b.stop_sequence !== null)
677
+ return false;
678
+ return b.usage.input_tokens > 0 && b.usage.output_tokens > 0;
679
+ })),
680
+ done('xai.messages_compat.validation', 'messages_compat', 'Messages: max_tokens is REQUIRED (Anthropic rule) + model/messages validated', 'api', 'common', () => withRoot(async (h) => {
681
+ const noMax = await h({ m: 'POST', p: '/v1/messages', b: { model: 'grok-4-0709', messages: [{ role: 'user', content: 'x' }] } });
682
+ const noModel = await h({ m: 'POST', p: '/v1/messages', b: { max_tokens: 10, messages: [{ role: 'user', content: 'x' }] } });
683
+ const unknown = await h({ m: 'POST', p: '/v1/messages', b: { model: 'grok-99', max_tokens: 10, messages: [{ role: 'user', content: 'x' }] } });
684
+ // every failure carries the FLAT {code,error} envelope, and unknown-model the vendor 404 code (§9 hardening)
685
+ return noMax.status === 400 && flatError(noMax)
686
+ && noModel.status === 400 && flatError(noModel)
687
+ && unknown.status === 404 && flatError(unknown) && unknown.body.code === 'Some requested entity was not found';
688
+ })),
689
+ done('xai.messages_compat.tools', 'messages_compat', 'Messages: Anthropic-style tools (input_schema) → tool_use block + stop_reason tool_use', 'api', 'common', () => withRoot(async (h) => {
690
+ const r = await h({ m: 'POST', p: '/v1/messages', b: { model: 'grok-4-0709', max_tokens: 256, messages: [{ role: 'user', content: 'weather?' }], tools: [{ name: 'get_weather', description: 'get weather', input_schema: { type: 'object', properties: { city: { type: 'string' } } } }] } });
691
+ if (!ok(r))
692
+ return false;
693
+ const b = r.body;
694
+ const block = b.content[0];
695
+ if (block?.type !== 'tool_use' || block.name !== 'get_weather' || !String(block.id).startsWith('toolu_'))
696
+ return false;
697
+ // input is a schema-typed OBJECT (Anthropic shape), not an OpenAI-style JSON string
698
+ return typeof block.input === 'object' && typeof block.input.city === 'string' && b.stop_reason === 'tool_use';
699
+ })),
700
+ // ── Legacy completions (POST /v1/completions) ─────────────────────────────────────────
701
+ done('xai.completions_legacy.create', 'completions_legacy', 'Legacy completions: prompt → text_completion envelope (labeled stub)', 'api', 'niche', () => withRoot(async (h) => {
702
+ const r = await h({ m: 'POST', p: '/v1/completions', b: { model: 'grok-3', prompt: 'complete me' } });
703
+ if (!ok(r))
704
+ return false;
705
+ const b = r.body;
706
+ if (b.object !== 'text_completion' || b.model !== 'grok-3')
707
+ return false;
708
+ const c = b.choices[0];
709
+ if (typeof c.text !== 'string' || !c.text.includes('[twin-stub') || !c.text.includes('complete me') || c.finish_reason !== 'stop')
710
+ return false;
711
+ // usage carries the xAI token-detail objects here too (§9 hardening)
712
+ const u = b.usage;
713
+ if (u.prompt_tokens <= 0 || u.total_tokens !== u.prompt_tokens + u.completion_tokens)
714
+ return false;
715
+ if (u.prompt_tokens_details?.text_tokens !== u.prompt_tokens || typeof u.completion_tokens_details?.reasoning_tokens !== 'number')
716
+ return false;
717
+ const noPrompt = await h({ m: 'POST', p: '/v1/completions', b: { model: 'grok-3' } });
718
+ const unknown = await h({ m: 'POST', p: '/v1/completions', b: { model: 'grok-99', prompt: 'x' } });
719
+ return noPrompt.status === 400 && unknown.status === 404;
720
+ })),
721
+ // ── Model catalogs ────────────────────────────────────────────────────────────────────
722
+ done('xai.models.list', 'models', 'GET /v1/models lists the grok catalog (OpenAI-compatible shape)', 'api', 'core', () => withRoot(async (h) => {
723
+ const r = await h({ m: 'GET', p: '/v1/models' });
724
+ if (!ok(r))
725
+ return false;
726
+ const b = r.body;
727
+ if (b.object !== 'list' || !Array.isArray(b.data))
728
+ return false;
729
+ const ids = b.data.map((m) => m.id);
730
+ if (!ids.includes('grok-4-0709') || !ids.includes('grok-3-mini') || !ids.includes('grok-2-image-1212'))
731
+ return false;
732
+ return b.data.every((m) => m.object === 'model' && m.owned_by === 'xai' && typeof m.created === 'number');
733
+ })),
734
+ done('xai.models.get', 'models', 'GET /v1/models/:id retrieves one model; unknown → vendor 404', 'api', 'core', () => withRoot(async (h) => {
735
+ const r = await h({ m: 'GET', p: '/v1/models/grok-4-0709' });
736
+ if (!ok(r) || r.body.object !== 'model' || r.body.id !== 'grok-4-0709')
737
+ return false;
738
+ const nf = await h({ m: 'GET', p: '/v1/models/grok-99' });
739
+ return nf.status === 404 && flatError(nf) && nf.body.code === 'Some requested entity was not found';
740
+ })),
741
+ done('xai.models.aliases', 'models', 'Model aliases resolve for retrieval AND inference (grok-4 → grok-4-0709)', 'api', 'common', () => withRoot(async (h) => {
742
+ const byAlias = await h({ m: 'GET', p: '/v1/models/grok-4' });
743
+ if (!ok(byAlias) || byAlias.body.id !== 'grok-4-0709')
744
+ return false;
745
+ const chat = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT({ model: 'grok-4-latest' }) });
746
+ return ok(chat) && chat.body.model === 'grok-4-0709';
747
+ })),
748
+ done('xai.models.language_models', 'models', 'GET /v1/language-models (+/:id) serves the rich xAI model objects (modalities, prices, aliases)', 'api', 'common', () => withRoot(async (h) => {
749
+ const r = await h({ m: 'GET', p: '/v1/language-models' });
750
+ if (!ok(r) || !Array.isArray(r.body.models))
751
+ return false;
752
+ const g4 = r.body.models.find((m) => m.id === 'grok-4-0709');
753
+ if (!g4 || !Array.isArray(g4.aliases) || !g4.aliases.includes('grok-4'))
754
+ return false;
755
+ if (!g4.input_modalities.includes('image') || typeof g4.prompt_text_token_price !== 'number')
756
+ return false;
757
+ const one = await h({ m: 'GET', p: '/v1/language-models/grok-4' }); // alias resolves
758
+ if (!ok(one) || one.body.id !== 'grok-4-0709')
759
+ return false;
760
+ const nf = await h({ m: 'GET', p: '/v1/language-models/grok-99' });
761
+ return nf.status === 404;
762
+ })),
763
+ done('xai.models.image_generation_models', 'models', 'GET /v1/image-generation-models (+/:id) serves the image-model catalog', 'api', 'niche', () => withRoot(async (h) => {
764
+ const r = await h({ m: 'GET', p: '/v1/image-generation-models' });
765
+ if (!ok(r))
766
+ return false;
767
+ const list = r.body.models;
768
+ if (!Array.isArray(list) || !list.some((m) => m.id === 'grok-2-image-1212' && m.output_modalities.includes('image')))
769
+ return false;
770
+ const one = await h({ m: 'GET', p: '/v1/image-generation-models/grok-2-image' });
771
+ if (!ok(one) || one.body.id !== 'grok-2-image-1212')
772
+ return false;
773
+ const nf = await h({ m: 'GET', p: '/v1/image-generation-models/grok-4-0709' });
774
+ return nf.status === 404;
775
+ })),
776
+ // ── Image generations ─────────────────────────────────────────────────────────────────
777
+ done('xai.images.generations', 'images', 'POST /v1/images/generations → data[].url | b64_json + revised_prompt (labeled stubs)', 'api', 'common', () => withRoot(async (h) => {
778
+ const r = await h({ m: 'POST', p: '/v1/images/generations', b: { model: 'grok-2-image', prompt: 'a red bicycle', n: 2 } });
779
+ if (!ok(r))
780
+ return false;
781
+ const data = r.body.data;
782
+ if (data.length !== 2 || typeof data[0].url !== 'string' || !String(data[0].revised_prompt).includes('a red bicycle'))
783
+ return false;
784
+ const b64 = await h({ m: 'POST', p: '/v1/images/generations', b: { model: 'grok-2-image', prompt: 'a red bicycle', response_format: 'b64_json' } });
785
+ const decoded = atob(b64.body.data[0].b64_json);
786
+ if (!decoded.includes('[twin-stub-image]'))
787
+ return false;
788
+ const tooMany = await h({ m: 'POST', p: '/v1/images/generations', b: { model: 'grok-2-image', prompt: 'x', n: 11 } });
789
+ const noPrompt = await h({ m: 'POST', p: '/v1/images/generations', b: { model: 'grok-2-image' } });
790
+ const chatModel = await h({ m: 'POST', p: '/v1/images/generations', b: { model: 'grok-4-0709', prompt: 'x' } });
791
+ return tooMany.status === 400 && noPrompt.status === 400 && chatModel.status === 400;
792
+ })),
793
+ done('xai.images.openai_params_rejected', 'images', 'xAI compatibility delta: OpenAI\'s size/quality/style params are REJECTED (400)', 'api', 'niche', () => withRoot(async (h) => {
794
+ for (const bad of [{ size: '1024x1024' }, { quality: 'hd' }, { style: 'vivid' }]) {
795
+ const r = await h({ m: 'POST', p: '/v1/images/generations', b: { model: 'grok-2-image', prompt: 'a cat', ...bad } });
796
+ if (r.status !== 400 || !String(r.body.error).includes('Argument not supported'))
797
+ return false;
798
+ }
799
+ return true;
800
+ })),
801
+ // ── api-key introspection ─────────────────────────────────────────────────────────────
802
+ done('xai.api_key.get', 'api_key', 'GET /v1/api-key introspects the presented key (redacted value, acls, blocked flags)', 'api', 'common', () => withRootH(async (h) => {
803
+ const r = await h({ m: 'GET', p: '/v1/api-key', headers: { authorization: 'Bearer xai-secret-key-12345' } });
804
+ if (!ok(r))
805
+ return false;
806
+ const b = r.body;
807
+ if (b.redacted_api_key !== 'xai-...2345')
808
+ return false; // derived from the PRESENTED key
809
+ if (!Array.isArray(b.acls) || !b.acls.includes('api-key:model:*'))
810
+ return false;
811
+ if (b.api_key_blocked !== false || b.team_blocked !== false || b.api_key_disabled !== false)
812
+ return false;
813
+ // timestamps derive from the pinned occurredAt (deterministic, not merely a string — §9 hardening)
814
+ if (!UUID_RE.test(String(b.api_key_id)) || b.create_time !== '2026-01-01T00:00:00.000Z')
815
+ return false;
816
+ // the blocked sentinel key can STILL introspect itself — and reports blocked
817
+ const blocked = await h({ m: 'GET', p: '/v1/api-key', headers: { authorization: 'Bearer xai-blocked' } });
818
+ return ok(blocked) && blocked.body.api_key_blocked === true;
819
+ })),
820
+ // ── tokenize-text ─────────────────────────────────────────────────────────────────────
821
+ done('xai.tokenize.text', 'tokenize', 'POST /v1/tokenize-text → token_ids[] (id/string_token/token_bytes; tokens re-join to the input)', 'api', 'niche', () => withRoot(async (h) => {
822
+ const r = await h({ m: 'POST', p: '/v1/tokenize-text', b: { text: 'hello twin world', model: 'grok-4-0709' } });
823
+ if (!ok(r))
824
+ return false;
825
+ const toks = r.body.token_ids;
826
+ if (!Array.isArray(toks) || toks.length < 3)
827
+ return false;
828
+ if (toks.some((t) => typeof t.token_id !== 'number' || typeof t.string_token !== 'string' || !Array.isArray(t.token_bytes)))
829
+ return false;
830
+ if (toks.map((t) => t.string_token).join('') !== 'hello twin world')
831
+ return false;
832
+ const noText = await h({ m: 'POST', p: '/v1/tokenize-text', b: { model: 'grok-4-0709' } });
833
+ const unknown = await h({ m: 'POST', p: '/v1/tokenize-text', b: { text: 'x', model: 'grok-99' } });
834
+ return noText.status === 400 && unknown.status === 404;
835
+ })),
836
+ // ── Errors / protocol ─────────────────────────────────────────────────────────────────
837
+ done('xai.errors.envelope', 'errors', 'Errors are xAI\'s FLAT {code, error} strings (gRPC-transcoded), never a nested error object', 'api', 'core', () => withRoot(async (h) => {
838
+ const nf = await h({ m: 'GET', p: '/v1/nonexistent' });
839
+ if (nf.status !== 404 || !flatError(nf))
840
+ return false;
841
+ // the delta vs OpenAI: `error` is a STRING, and there is no nested { error: {...} }
842
+ if (typeof nf.body.error === 'object')
843
+ return false;
844
+ const bad = await h({ m: 'POST', p: '/v1/chat/completions', b: { messages: [{ role: 'user', content: 'x' }] } });
845
+ return bad.status === 400 && flatError(bad) && bad.body.code === 'Client specified an invalid argument';
846
+ })),
847
+ done('xai.errors.auth_401', 'errors', 'Modeled auth: missing/invalid bearer → 401 (when a request carries an auth surface)', 'api', 'common', () => withRootH(async (h) => {
848
+ const missing = await h({ m: 'GET', p: '/v1/models', headers: {} });
849
+ if (missing.status !== 401 || !flatError(missing))
850
+ return false;
851
+ // the 401 code is the gRPC UNAUTHENTICATED description — NOT the 400's invalid-argument
852
+ // code (§9 caught the 401 reusing it; this pin keeps it fixed)
853
+ if (missing.body.code !== 'The request does not have valid authentication credentials for the operation')
854
+ return false;
855
+ const invalid = await h({ m: 'GET', p: '/v1/models', headers: { authorization: 'Bearer xai-invalid' } });
856
+ if (invalid.status !== 401 || !String(invalid.body.error).includes('Incorrect API key'))
857
+ return false;
858
+ if (invalid.body.code !== missing.body.code)
859
+ return false;
860
+ const valid = await h({ m: 'GET', p: '/v1/models', headers: { authorization: 'Bearer xai-any-other-key' } });
861
+ return valid.status === 200;
862
+ })),
863
+ done('xai.errors.blocked_403', 'errors', 'A blocked key → 403 on every route EXCEPT its own /v1/api-key introspection', 'api', 'niche', () => withRootH(async (h) => {
864
+ const chat = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT(), headers: { authorization: 'Bearer xai-blocked' } });
865
+ if (chat.status !== 403 || chat.body.code !== 'The caller does not have permission')
866
+ return false;
867
+ const models = await h({ m: 'GET', p: '/v1/models', headers: { authorization: 'Bearer xai-blocked' } });
868
+ if (models.status !== 403)
869
+ return false;
870
+ const introspect = await h({ m: 'GET', p: '/v1/api-key', headers: { authorization: 'Bearer xai-blocked' } });
871
+ return introspect.status === 200 && introspect.body.api_key_blocked === true;
872
+ })),
873
+ done('xai.errors.rate_limit_429', 'errors', 'Modeled 429 (deterministic opt-in trigger) → {code,error} + retry-after', 'api', 'niche', () => withRootH(async (h) => {
874
+ const limited = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT(), headers: { authorization: 'Bearer xai-ok', 'x-twin-force-rate-limit': '1' } });
875
+ if (limited.status !== 429 || limited.body.code !== 'Resource has been exhausted')
876
+ return false;
877
+ if (limited.headers?.['retry-after'] !== '1')
878
+ return false;
879
+ const normal = await h({ m: 'POST', p: '/v1/chat/completions', b: CHAT(), headers: { authorization: 'Bearer xai-ok' } });
880
+ return normal.status === 200;
881
+ })),
882
+ done('xai.errors.read_only_405', 'errors', 'A read-only twin refuses mutations (405), serves reads, and never advances state via a GET', 'api', 'niche', async () => {
883
+ const root = mkdtempSync(join(tmpdir(), 'xai-cap-'));
884
+ try {
885
+ const write = await handleXaiTwinRequest({ method: 'POST', path: '/v1/chat/completions', body: JSON.stringify(CHAT()), root, readOnly: true, occurredAt: AT });
886
+ if (write.status !== 405 || typeof write.body.error !== 'string')
887
+ return false;
888
+ const read = await handleXaiTwinRequest({ method: 'GET', path: '/v1/models', root, readOnly: true, occurredAt: AT });
889
+ if (read.status !== 200 || !Array.isArray(read.body.data))
890
+ return false;
891
+ // the STATE-ADVANCING GET path (deferred retrieval) must not mutate the kernel log in a
892
+ // read-only twin (§9 found the consume write unguarded): seed + one writable poll, then
893
+ // read-only GETs serve the result REPEATEDLY (never consuming), and the writable flow
894
+ // afterwards still consumes exactly once.
895
+ const created = await handleXaiTwinRequest({ method: 'POST', path: '/v1/chat/completions', body: JSON.stringify(CHAT({ deferred: true })), root, occurredAt: AT });
896
+ const reqId = created.body.request_id;
897
+ const pendingRo = await handleXaiTwinRequest({ method: 'GET', path: `/v1/chat/deferred-completion/${reqId}`, root, readOnly: true, occurredAt: AT });
898
+ if (pendingRo.status !== 202)
899
+ return false;
900
+ // a read-only pending poll did NOT advance polls — the next read-only poll is still 202
901
+ const pendingRo2 = await handleXaiTwinRequest({ method: 'GET', path: `/v1/chat/deferred-completion/${reqId}`, root, readOnly: true, occurredAt: AT });
902
+ if (pendingRo2.status !== 202)
903
+ return false;
904
+ // one WRITABLE poll advances to servable…
905
+ const pendingW = await handleXaiTwinRequest({ method: 'GET', path: `/v1/chat/deferred-completion/${reqId}`, root, occurredAt: AT });
906
+ if (pendingW.status !== 202)
907
+ return false;
908
+ // …and read-only retrieval serves the result WITHOUT consuming it (twice)
909
+ const ro1 = await handleXaiTwinRequest({ method: 'GET', path: `/v1/chat/deferred-completion/${reqId}`, root, readOnly: true, occurredAt: AT });
910
+ const ro2 = await handleXaiTwinRequest({ method: 'GET', path: `/v1/chat/deferred-completion/${reqId}`, root, readOnly: true, occurredAt: AT });
911
+ if (ro1.status !== 200 || ro2.status !== 200)
912
+ return false;
913
+ // the writable flow still consumes exactly once
914
+ const w = await handleXaiTwinRequest({ method: 'GET', path: `/v1/chat/deferred-completion/${reqId}`, root, occurredAt: AT });
915
+ const after = await handleXaiTwinRequest({ method: 'GET', path: `/v1/chat/deferred-completion/${reqId}`, root, occurredAt: AT });
916
+ if (w.status !== 200 || after.status !== 404)
917
+ return false;
918
+ const device = await startXaiTwinDeviceAuthorization({ body: new URLSearchParams({ client_id: 'grok-build', scope: 'grok-cli:access' }), origin: 'http://127.0.0.1', root });
919
+ const userCode = String(device.body.user_code);
920
+ const readOnlyServer = await createXaiTwinServer({ readOnly: true, root, port: 0 });
921
+ try {
922
+ const origin = `http://127.0.0.1:${readOnlyServer.port}`;
923
+ const decision = await fetch(`${origin}/oauth2/device/decision`, { method: 'POST', body: new URLSearchParams({ decision: 'approve', user_code: userCode }) });
924
+ const control = await fetch(`${origin}/twin/oauth/authorizations`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ approved: true, user_code: userCode }) });
925
+ return decision.status === 405 && control.status === 404 && xaiDeviceAuthView(userCode, root).status === 'pending';
926
+ }
927
+ finally {
928
+ readOnlyServer.stop();
929
+ }
930
+ }
931
+ catch {
932
+ return false;
933
+ }
934
+ finally {
935
+ rmSync(root, { recursive: true, force: true });
936
+ }
937
+ }),
938
+ // ── Connector (pull over an injected client; push refuses loudly) ─────────────────────
939
+ done('xai.connector.read_surface', 'connector', 'Connector read surface (the three model catalogs in one pass)', 'connector', 'core', () => withRoot(async (h) => {
940
+ const flat = await h({ m: 'GET', p: '/v1/models' });
941
+ const rich = await h({ m: 'GET', p: '/v1/language-models' });
942
+ const img = await h({ m: 'GET', p: '/v1/image-generation-models' });
943
+ return ok(flat) && Array.isArray(flat.body.data)
944
+ && ok(rich) && Array.isArray(rich.body.models)
945
+ && ok(img) && Array.isArray(img.body.models);
946
+ })),
947
+ done('xai.connector.pull_map', 'connector', 'Connector pulls + maps the real catalogs into the twin; served /v1/models merges them (offline, injected client)', 'connector', 'core', async () => {
948
+ const { mapModel, mapLanguageModel, syncXaiFromReal } = await import("./xai-connector.js");
949
+ const root = mkdtempSync(join(tmpdir(), 'xai-cap-'));
950
+ try {
951
+ const mapped = mapModel({ id: 'grok-secret-preview', created: 5, owned_by: 'xai' });
952
+ const mappedRich = mapLanguageModel({ id: 'grok-secret-preview', created: 5, aliases: ['gsp'], input_modalities: ['text'] });
953
+ if (mapped.type !== 'model' || mappedRich.type !== 'language_model' || mappedRich.fields.aliases[0] !== 'gsp')
954
+ return false;
955
+ const fake = async (_m, p) => {
956
+ if (p === '/v1/models')
957
+ return { data: [{ id: 'grok-secret-preview', created: 5, owned_by: 'xai' }] };
958
+ if (p === '/v1/language-models')
959
+ return { models: [{ id: 'grok-secret-preview', created: 5, aliases: ['gsp'], input_modalities: ['text'] }] };
960
+ return {};
961
+ };
962
+ const res = await syncXaiFromReal(fake, { root, occurredAt: AT });
963
+ if (res.observed < 2 || res.deltasAppended < 1)
964
+ return false;
965
+ // the twin's SERVED catalog now merges the observed model over the static list
966
+ const served = await handleXaiTwinRequest({ method: 'GET', path: '/v1/models', root, occurredAt: AT });
967
+ const ids = served.body.data.map((m) => m.id);
968
+ if (!ids.includes('grok-secret-preview') || !ids.includes('grok-4-0709'))
969
+ return false;
970
+ const one = await handleXaiTwinRequest({ method: 'GET', path: '/v1/models/grok-secret-preview', root, occurredAt: AT });
971
+ if (one.status !== 200 || one.body.created !== 5)
972
+ return false;
973
+ // idempotent: a second pull of identical state appends nothing
974
+ const again = await syncXaiFromReal(fake, { root, occurredAt: AT });
975
+ return again.deltasAppended === 0;
976
+ }
977
+ catch {
978
+ return false;
979
+ }
980
+ finally {
981
+ rmSync(root, { recursive: true, force: true });
982
+ }
983
+ }),
984
+ done('xai.connector.unsupported_op_fails', 'connector', 'Nothing can cross: the direct push refuses LOUDLY, and the head\'s perform SETTLES each entry with the reason (the xAI API has no client-writable resources) rather than fabricating a write', 'connector', 'common', async () => {
985
+ const { pushXaiAction, performXaiAction } = await import("./xai-connector.js");
986
+ const { deployableEntries } = await import('@volter/world-core');
987
+ const root = mkdtempSync(join(tmpdir(), 'xai-cap-'));
988
+ try {
989
+ const fake = async (_m, _p) => ({});
990
+ // no pending local writes → push is a clean no-op
991
+ // protocol 2: with nothing deployable there is nothing to perform
992
+ const clean = { pushed: deployableEntries('xai', root).length };
993
+ if (clean.pushed !== 0)
994
+ return false;
995
+ // a local write (deferred completion) becomes a pending action…
996
+ const created = await handleXaiTwinRequest({ method: 'POST', path: '/v1/chat/completions', body: JSON.stringify(CHAT({ deferred: true })), root, occurredAt: AT });
997
+ if (created.status !== 200 || deployableEntries('xai', root).length === 0)
998
+ return false;
999
+ // …and pushing it REFUSES loudly (never a silent drop, never a fabricated vendor write)
1000
+ let threwSingle = false;
1001
+ try {
1002
+ await pushXaiAction(fake, { operation: 'deferred_completion.create', subject: { type: 'deferred_completion', id: 'x' } });
1003
+ }
1004
+ catch (e) {
1005
+ threwSingle = e instanceof Error && e.message.includes('unsupported operation');
1006
+ }
1007
+ // …and at the head, the perform adapter SETTLES it with the same fact rather than sending anything:
1008
+ // there is nothing at xAI to write, and the receipt says so instead of claiming a write happened
1009
+ const settled = await performXaiAction(fake, { operation: 'deferred_completion.create', subject: { type: 'deferred_completion', id: 'x' }, fields: {} }, { resolve: (_t, id) => id, service: 'xai', root });
1010
+ const saysWhy = settled.data !== undefined
1011
+ && settled.data.performed === false
1012
+ && String(settled.data.reason ?? '').includes('no client-writable resources');
1013
+ return threwSingle && saysWhy;
1014
+ }
1015
+ catch {
1016
+ return false;
1017
+ }
1018
+ finally {
1019
+ rmSync(root, { recursive: true, force: true });
1020
+ }
1021
+ }),
1022
+ // ── Conformance harness ───────────────────────────────────────────────────────────────
1023
+ done('xai.conformance.envelopes', 'conformance', 'Offline conformance harness passes (envelope shapes across the surface)', 'connector', 'core', async () => {
1024
+ const { checkXaiConformance } = await import("./xai-conformance.js");
1025
+ const report = await checkXaiConformance();
1026
+ // exactly the 9 authored checks ran (a deleted check trips this — checksRun is a tripwire,
1027
+ // not a tautology; §9 hardening) and none reported a violation.
1028
+ return report.ok && report.violations.length === 0 && report.checksRun === 9;
1029
+ }),
1030
+ // ── Honest todos (real vendor surface the twin has not reached) ───────────────────────
1031
+ todo('xai.chat.logprobs', 'chat', 'logprobs + top_logprobs per-token detail on chat completions', 'api', 'niche'),
1032
+ todo('xai.chat.penalties', 'chat', 'presence_penalty / frequency_penalty accepted + validated', 'api', 'niche'),
1033
+ todo('xai.chat.cached_prompt_tokens', 'chat', 'Prompt caching — usage.prompt_tokens_details.cached_tokens reflects cache hits', 'api', 'common'),
1034
+ todo('xai.deferred.expiry', 'deferred', 'Deferred completions expire after the vendor retention window (unretrieved results lapse)', 'api', 'niche'),
1035
+ todo('xai.search.date_range', 'search', 'Live Search from_date/to_date window parameters', 'api', 'common'),
1036
+ todo('xai.search.max_results', 'search', 'Live Search max_search_results caps the consulted sources', 'api', 'common'),
1037
+ todo('xai.search.source_filters', 'search', 'Live Search per-source filters (allowed/excluded websites, X handles, RSS links, safe_search)', 'api', 'common'),
1038
+ todo('xai.messages_compat.streaming', 'messages_compat', 'Anthropic-compatible streaming (message_start → content_block_delta → message_stop SSE events)', 'api', 'common'),
1039
+ todo('xai.messages_compat.system', 'messages_compat', 'Anthropic-style system prompt + multi-block content parts on /v1/messages', 'api', 'niche'),
1040
+ todo('xai.completions_legacy.streaming', 'completions_legacy', 'Legacy completions streaming (text deltas over SSE)', 'api', 'niche'),
1041
+ todo('xai.completions_legacy.echo_suffix', 'completions_legacy', 'Legacy completions echo/suffix/logprobs parameters', 'api', 'niche'),
1042
+ todo('xai.api_key.acl_enforcement', 'api_key', 'ACL-scoped keys: api-key:model:*/api-key:endpoint:* ACLs actually restrict requests', 'api', 'niche'),
1043
+ // The Responses API — xAI's current first-class stateful inference surface (docs.x.ai now
1044
+ // files chat completions under "legacy"). A whole area the twin has not reached yet; found
1045
+ // by the §9 adversarial pass (the area census cannot flag an area its author never listed,
1046
+ // so this entered the denominator through review, not the gate).
1047
+ todo('xai.responses.create', 'responses', 'POST /v1/responses — stateful successor to chat completions (server-side stored responses)', 'api', 'core'),
1048
+ todo('xai.responses.retrieve_delete', 'responses', 'GET/DELETE /v1/responses/{response_id} — retrieve or delete a stored response', 'api', 'common'),
1049
+ todo('xai.responses.compact', 'responses', 'POST /v1/responses/compact — compact a stored conversation', 'api', 'niche'),
1050
+ // Pull-surface coverage audit gap (TWIN-46 / G2) — filed as a manifest todo so the
1051
+ // seed-conformance tooling turns it into real backlog work. See pull-audit.json (repo root).
1052
+ todo('xai.connector.pull_image_generation_models', 'connector', 'Connector: pull the image-generation model catalog (GET /v1/image-generation-models) into the twin', 'connector', 'niche'),
1053
+ ];
1054
+ // Committed area census — the vendor's top-level API product areas (docs.x.ai REST reference),
1055
+ // authored top-down independent of what a manifest entry happens to already exist for. The
1056
+ // area-census meta-test (assertAreaCensus) fails the gate if a declared area has zero manifest
1057
+ // entries OR a manifest entry's `area` drifts outside this list. HONEST LIMIT: the census only
1058
+ // protects areas someone LISTED — it cannot flag an area the author never thought of (the §9
1059
+ // adversarial pass caught `responses` missing this way); the per-cycle MANIFEST-COMPLETENESS
1060
+ // AUDIT re-walking the vendor docs is what closes that loop.
1061
+ //
1062
+ // Deliberately-excluded neighboring surface (a boundary, not an omission): xAI's Collections
1063
+ // API and Management API live on a DIFFERENT host (management-api.x.ai) and are a separate
1064
+ // console/administration product — this pack twins the api.x.ai inference surface only. See
1065
+ // README ## Coverage.
1066
+ export const XAI_AREAS = [
1067
+ 'api_key', 'chat', 'cli_proxy', 'completions_legacy', 'conformance', 'connector', 'deferred', 'errors',
1068
+ 'images', 'messages_compat', 'models', 'oauth', 'responses', 'search', 'streaming', 'tokenize', 'tools', 'ui',
1069
+ ];
1070
+ export function xaiCapabilities() {
1071
+ return checkCapabilities('xai', XAI_CAPABILITIES);
1072
+ }