-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestBot.ts
More file actions
694 lines (627 loc) · 22 KB
/
Copy pathtestBot.ts
File metadata and controls
694 lines (627 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/**
* ============================================================================
* Abstract Play — reference bot implementation
* ============================================================================
*
* This file is the **complete** source for AP's dev-only test bot. Bot authors
* should treat it as the canonical example of how to integrate with Abstract Play.
*
* Supporting libraries used by this bot (and reusable in your own deployment):
*
* lib/botVerify.ts — verify inbound AP webhook signatures (Ed25519)
* lib/botClient.ts — OAuth client_credentials token + POST moves to botQuery
*
* Everything else you need to study or copy is in **this file**.
*
* ---------------------------------------------------------------------------
* Protocol overview
* ---------------------------------------------------------------------------
*
* AP talks to your bot over HTTPS. Your bot talks back to AP's `botQuery`
* endpoint using a Cognito M2M access token.
*
* 1. PING (availability)
* AP → GET https://your-bot.example/ping-or-root
* You ← 200 { "operational": true }
*
* 2. CHALLENGE (synchronous accept/reject)
* AP → POST https://your-bot.example/ (signed JSON body)
* Body: { verb: "challenge", metaGame, variants, clockStart, clockInc,
* clockMax, challengers }
* You ← 200 accept | 400 reject
*
* 3. MOVE (async processing)
* AP → POST https://your-bot.example/ (signed JSON body)
* Body: { verb: "move", metaGame, variants, gameid, clockCurr, numPlayers,
* moves, context? }
* You ← 202 immediately (queue work in background)
* You → POST https://api…/botQuery (Bearer access token)
* Body: { verb: "move", gameid, metaGame, move }
* AP ← 200 + updated game JSON on success
*
* Full protocol docs: /backend/bots/ (node-backend docs; wiki RFC is legacy)
*
* ---------------------------------------------------------------------------
* Inbound authentication (AP → your bot)
* ---------------------------------------------------------------------------
*
* Every POST from AP includes:
*
* X-Signature-Timestamp — Unix seconds when AP signed the request
* X-Signature-Nonce — one-time random string
* X-Signature — base64 Ed25519 signature
*
* Signing string: `${timestamp}.${nonce}.${rawRequestBody}`
*
* Reject requests older than 5 minutes (replay protection). Use the **raw**
* request body bytes — do not re-serialize JSON. See `verifyBotRequest()` in
* lib/botVerify.ts (called in `handlePost` below).
*
* ---------------------------------------------------------------------------
* Outbound authentication (your bot → AP)
* ---------------------------------------------------------------------------
*
* Dev and production use **separate** Cognito bot pools, token URLs, OAuth scopes,
* DynamoDB tables, and botQuery base URLs. Register and test on dev first; create a
* new bot on production when you are ready for wide release (credentials do not transfer).
*
* Dev: BOT_TOKEN_URL → abstract-play-bots-dev.auth…/oauth2/token
* BOT_OAUTH_SCOPE → default-m2m-resource-server-dev/communicate
* botQuery → …/dev/botQuery
* Prod: BOT_TOKEN_URL → https://botauth.abstractplay.com/oauth2/token
* BOT_OAUTH_SCOPE → default-m2m-resource-server-zssvzy/communicate
* botQuery → …/prod/botQuery
*
* 1. POST to BOT_TOKEN_URL with grant_type=client_credentials, client_id,
* client_secret, and scope (see lib/botClient.ts).
* 2. Cache the access_token until shortly before expires_in.
* 3. POST moves to BOT_QUERY_URL with Authorization: Bearer <token>.
* 4. On HTTP 401, refresh the token once and retry.
*
* ---------------------------------------------------------------------------
* Environment variables (test bot Lambda on dev)
* ---------------------------------------------------------------------------
*
* TEST_BOT_CLIENT_ID — Dev-pool Cognito client id (= bot player id, JWT sub)
* TEST_BOT_CLIENT_SECRET — Dev-pool client secret for M2M token
* BOT_TOKEN_URL — Dev OAuth token endpoint (serverless stageConfig.dev)
* BOT_QUERY_URL — Dev botQuery URL (…/dev/botQuery)
* BOT_OAUTH_SCOPE — Dev OAuth scope (default-m2m-resource-server-dev/communicate)
* ABSTRACT_PLAY_TABLE — DynamoDB table (test bot event log + BOT record)
* API_BASE_URL — Used by dashboard to show this bot's public URL
*
* ---------------------------------------------------------------------------
* Dev dashboard (not part of a production bot)
* ---------------------------------------------------------------------------
*
* The test bot also exposes authQuery handlers (`testBotStatus`, `updateTestBot`)
* so the fixed owner can inspect recent traffic and tweak behaviour. Production
* bots manage their own logging and configuration.
*
* ---------------------------------------------------------------------------
* Adapting this for your bot
* ---------------------------------------------------------------------------
*
* 1. Host any HTTPS server (Lambda, Cloud Run, VPS, …).
* 2. Implement GET ping + signed POST handler (copy `handlePost` flow).
* 3. On move: return 202 quickly, compute move, call botQuery via botClient.
* 4. Register the bot in AP (createBot) with your endpoint URL and upload your
* public key if you also verify signatures locally.
*
* The `pickMove` function below is intentionally simple (replay `moves` through
* the gameslib engine, then play the first legal move). Replace it with your
* engine / search / ML pipeline.
*/
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import { GetCommand, PutCommand, UpdateCommand } from '@aws-sdk/lib-dynamodb';
import { GameFactory } from '@abstractplay/gameslib';
import { verifyBotRequest } from '../lib/botVerify';
import { submitBotMove } from '../lib/botClient';
import { getBotRecord } from '../lib/participants';
import { ddbDocClient } from '../lib/ddb';
// =============================================================================
// Test-bot dashboard state (dev only — production bots use their own storage)
// =============================================================================
/** AP user id allowed to view/edit test bot settings in the dev UI. */
export const TEST_BOT_OWNER_ID = '3ccb3a1f-3d25-441e-9efc-e526eac4fe9a';
const TEST_BOT_PK = 'TESTBOT';
const TEST_BOT_SK = 'dev';
const MAX_EVENTS = 50;
export type TestBotMovePolicy = 'pass' | 'firstLegal';
export type TestBotSettings = {
acceptChallenges: boolean;
rejectMetaGames: string[];
movePolicy: TestBotMovePolicy;
moveDelayMs: number;
};
export type TestBotEvent = {
ts: number;
direction: 'inbound' | 'outbound';
verb: string;
summary: string;
statusCode?: number;
error?: string;
detail?: Record<string, unknown>;
};
export type TestBotState = {
pk: string;
sk: string;
owner: string;
settings: TestBotSettings;
recentEvents: TestBotEvent[];
};
export const DEFAULT_TEST_BOT_SETTINGS: TestBotSettings = {
acceptChallenges: true,
rejectMetaGames: [],
movePolicy: 'firstLegal',
moveDelayMs: 0,
};
function defaultTestBotState(): TestBotState {
return {
pk: TEST_BOT_PK,
sk: TEST_BOT_SK,
owner: TEST_BOT_OWNER_ID,
settings: { ...DEFAULT_TEST_BOT_SETTINGS, rejectMetaGames: [] },
recentEvents: [],
};
}
export async function getOrCreateTestBotState(): Promise<TestBotState> {
const tableName = process.env.ABSTRACT_PLAY_TABLE;
if (!tableName) {
throw new Error('ABSTRACT_PLAY_TABLE environment variable is not set');
}
const data = await ddbDocClient.send(
new GetCommand({
TableName: tableName,
Key: { pk: TEST_BOT_PK, sk: TEST_BOT_SK },
})
);
if (data.Item) {
return data.Item as TestBotState;
}
const item = defaultTestBotState();
try {
await ddbDocClient.send(
new PutCommand({
TableName: tableName,
Item: item,
ConditionExpression: 'attribute_not_exists(pk)',
})
);
} catch (error: unknown) {
const err = error as { name?: string };
if (err.name !== 'ConditionalCheckFailedException') {
throw error;
}
const retry = await ddbDocClient.send(
new GetCommand({
TableName: tableName,
Key: { pk: TEST_BOT_PK, sk: TEST_BOT_SK },
})
);
if (!retry.Item) {
throw error;
}
return retry.Item as TestBotState;
}
return item;
}
async function appendTestBotEvent(event: TestBotEvent): Promise<void> {
const tableName = process.env.ABSTRACT_PLAY_TABLE;
if (!tableName) {
throw new Error('ABSTRACT_PLAY_TABLE environment variable is not set');
}
const state = await getOrCreateTestBotState();
const recentEvents = [...(state.recentEvents ?? []), event].slice(-MAX_EVENTS);
await ddbDocClient.send(
new UpdateCommand({
TableName: tableName,
Key: { pk: TEST_BOT_PK, sk: TEST_BOT_SK },
ExpressionAttributeValues: { ':events': recentEvents },
UpdateExpression: 'SET recentEvents = :events',
})
);
}
export async function updateTestBotSettings(patch: Partial<TestBotSettings>): Promise<TestBotSettings> {
const tableName = process.env.ABSTRACT_PLAY_TABLE;
if (!tableName) {
throw new Error('ABSTRACT_PLAY_TABLE environment variable is not set');
}
const state = await getOrCreateTestBotState();
const settings: TestBotSettings = {
...state.settings,
...patch,
rejectMetaGames: patch.rejectMetaGames ?? state.settings.rejectMetaGames ?? [],
};
await ddbDocClient.send(
new UpdateCommand({
TableName: tableName,
Key: { pk: TEST_BOT_PK, sk: TEST_BOT_SK },
ExpressionAttributeValues: { ':settings': settings },
UpdateExpression: 'SET settings = :settings',
})
);
return settings;
}
export function isTestBotOwner(userId: string | undefined): boolean {
return userId === TEST_BOT_OWNER_ID;
}
// =============================================================================
// authQuery dashboard handlers (wired from abstractplay.ts — dev only)
// =============================================================================
type PartialClaims = { sub?: string };
const dashboardHeaders = {
'content-type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': '*',
};
function dashboardForbidden() {
return {
statusCode: 403,
body: JSON.stringify({ message: 'You are not authorized to access the test bot dashboard' }),
headers: dashboardHeaders,
};
}
function dashboardError(message: string) {
return {
statusCode: 500,
body: JSON.stringify({ message }),
headers: dashboardHeaders,
};
}
/** authQuery: test_bot_status — recent events, settings, and BOT record health. */
export async function testBotStatus(claim: PartialClaims) {
if (!isTestBotOwner(claim?.sub)) {
return dashboardForbidden();
}
try {
const state = await getOrCreateTestBotState();
const clientId = process.env.TEST_BOT_CLIENT_ID?.trim();
const apiBase = process.env.API_BASE_URL?.replace(/\/$/, '');
const endpointUrl = apiBase ? `${apiBase}/testBot` : undefined;
let botRecord: {
lastseen?: number;
operational?: boolean;
lastStatusCode?: number;
name?: string;
endpoint?: string;
} | undefined;
if (clientId) {
const bot = await getBotRecord(clientId);
if (bot) {
botRecord = {
lastseen: bot.lastseen,
operational: (bot as { operational?: boolean }).operational,
lastStatusCode: (bot as { lastStatusCode?: number }).lastStatusCode,
name: bot.name,
endpoint: bot.endpoint,
};
}
}
return {
statusCode: 200,
body: JSON.stringify({
endpointUrl,
clientIdConfigured: Boolean(clientId),
clientId: clientId ?? null,
settings: state.settings,
recentEvents: state.recentEvents ?? [],
botRecord: botRecord ?? null,
}),
headers: dashboardHeaders,
};
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
console.error('Error loading test bot status:', error);
return dashboardError(`Unable to load test bot status: ${message}`);
}
}
/** authQuery: update_test_bot — change acceptChallenges, movePolicy, etc. */
export async function updateTestBot(
claim: PartialClaims,
pars: {
acceptChallenges?: boolean;
rejectMetaGames?: string[];
movePolicy?: TestBotMovePolicy;
moveDelayMs?: number;
}
) {
if (!isTestBotOwner(claim?.sub)) {
return dashboardForbidden();
}
const patch: Partial<TestBotSettings> = {};
if (pars.acceptChallenges !== undefined) {
patch.acceptChallenges = pars.acceptChallenges;
}
if (pars.rejectMetaGames !== undefined) {
patch.rejectMetaGames = pars.rejectMetaGames;
}
if (pars.movePolicy !== undefined) {
if (pars.movePolicy !== 'pass' && pars.movePolicy !== 'firstLegal') {
return {
statusCode: 400,
body: JSON.stringify({ message: "movePolicy must be 'pass' or 'firstLegal'" }),
headers: dashboardHeaders,
};
}
patch.movePolicy = pars.movePolicy;
}
if (pars.moveDelayMs !== undefined) {
if (!Number.isFinite(pars.moveDelayMs) || pars.moveDelayMs < 0) {
return {
statusCode: 400,
body: JSON.stringify({ message: 'moveDelayMs must be a non-negative number' }),
headers: dashboardHeaders,
};
}
patch.moveDelayMs = Math.floor(pars.moveDelayMs);
}
if (Object.keys(patch).length === 0) {
return {
statusCode: 400,
body: JSON.stringify({ message: 'No test bot settings were provided' }),
headers: dashboardHeaders,
};
}
try {
const settings = await updateTestBotSettings(patch);
return {
statusCode: 200,
body: JSON.stringify({ settings }),
headers: dashboardHeaders,
};
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
console.error('Error updating test bot settings:', error);
return dashboardError(`Unable to update test bot settings: ${message}`);
}
}
// =============================================================================
// Inbound webhook — AP → bot (Lambda handler)
// =============================================================================
/** Payload AP sends when a human challenges this bot. */
type OutChallengePayload = {
verb: 'challenge';
metaGame: string;
variants?: string[];
};
/** Payload AP sends when it is this bot's turn. */
type OutMovePayload = {
verb: 'move';
metaGame: string;
variants?: string[];
gameid: string;
moves: string[][];
};
const webhookHeaders = {
'content-type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': '*',
};
function jsonResponse(statusCode: number, body: Record<string, unknown>): APIGatewayProxyResult {
return {
statusCode,
body: JSON.stringify(body),
headers: webhookHeaders,
};
}
function sleep(ms: number): Promise<void> {
if (ms <= 0) {
return Promise.resolve();
}
return new Promise(resolve => setTimeout(resolve, ms));
}
/** Updates the shared BOT# record so AP knows this endpoint is reachable. */
async function recordTestBotContact(statusCode: number): Promise<void> {
const clientId = process.env.TEST_BOT_CLIENT_ID?.trim();
const tableName = process.env.ABSTRACT_PLAY_TABLE;
if (!clientId || !tableName) {
return;
}
const reachable = statusCode > 0;
const operational = reachable && statusCode >= 200 && statusCode < 300;
await ddbDocClient.send(
new UpdateCommand({
TableName: tableName,
Key: { pk: 'BOT', sk: clientId },
ExpressionAttributeValues: {
':ls': Date.now(),
':sc': statusCode,
':ok': operational,
},
UpdateExpression: 'SET lastseen = :ls, lastStatusCode = :sc, operational = :ok',
})
);
}
type MoveListEngine = {
move(move: string): void;
moves(): string[];
};
/**
* Reference move selection: replay `moves` (rounds × players) into a fresh engine,
* then return the first legal move. Replace this with your own logic.
*/
function pickMove(
metaGame: string,
variants: string[],
movePolicy: TestBotMovePolicy,
moves: string[][]
): string {
const engine = GameFactory(metaGame, undefined, variants) as MoveListEngine | undefined;
if (!engine) {
throw new Error(`Unknown metaGame ${metaGame}`);
}
for (const round of moves) {
for (const move of round) {
engine.move(move);
}
}
const legal = engine.moves();
if (movePolicy === 'pass' && legal.includes('pass')) {
return 'pass';
}
if (legal.includes('pass')) {
return 'pass';
}
if (legal.length === 0) {
throw new Error(`No legal moves for ${metaGame}`);
}
return legal[0];
}
async function handlePing(): Promise<APIGatewayProxyResult> {
await appendTestBotEvent({
ts: Date.now(),
direction: 'inbound',
verb: 'ping',
summary: 'GET ping',
statusCode: 200,
});
await recordTestBotContact(200);
return jsonResponse(200, { operational: true });
}
async function handleChallenge(payload: OutChallengePayload): Promise<APIGatewayProxyResult> {
const state = await getOrCreateTestBotState();
const { settings } = state;
await appendTestBotEvent({
ts: Date.now(),
direction: 'inbound',
verb: 'challenge',
summary: `challenge ${payload.metaGame}`,
});
if (!settings.acceptChallenges) {
await recordTestBotContact(400);
return jsonResponse(400, { message: 'Test bot is configured to reject challenges' });
}
if (settings.rejectMetaGames.includes(payload.metaGame)) {
await recordTestBotContact(400);
return jsonResponse(400, { message: `Test bot rejects metaGame ${payload.metaGame}` });
}
await recordTestBotContact(200);
return jsonResponse(200, { accepted: true });
}
async function handleMove(payload: OutMovePayload): Promise<APIGatewayProxyResult> {
const state = await getOrCreateTestBotState();
const { settings } = state;
await appendTestBotEvent({
ts: Date.now(),
direction: 'inbound',
verb: 'move',
summary: `move ${payload.metaGame} game ${payload.gameid}`,
});
const clientId = process.env.TEST_BOT_CLIENT_ID?.trim();
const clientSecret = process.env.TEST_BOT_CLIENT_SECRET?.trim();
if (!clientId || !clientSecret) {
await appendTestBotEvent({
ts: Date.now(),
direction: 'outbound',
verb: 'move',
summary: 'botQuery move not sent',
error: 'TEST_BOT_CLIENT_ID or TEST_BOT_CLIENT_SECRET is not configured',
});
await recordTestBotContact(202);
return jsonResponse(202, { queued: true, warning: 'Bot credentials not configured' });
}
// Optional artificial delay (dashboard setting) — real bots should still return 202 quickly.
await sleep(settings.moveDelayMs);
let move: string;
try {
move = pickMove(
payload.metaGame,
payload.variants ?? [],
settings.movePolicy,
payload.moves ?? []
);
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
await appendTestBotEvent({
ts: Date.now(),
direction: 'outbound',
verb: 'move',
summary: 'failed to pick move',
error: message,
});
await recordTestBotContact(202);
return jsonResponse(202, { queued: true, error: message });
}
try {
const result = await submitBotMove({
gameid: payload.gameid,
metaGame: payload.metaGame,
move,
clientId,
clientSecret,
});
await appendTestBotEvent({
ts: Date.now(),
direction: 'outbound',
verb: 'move',
summary: `submitted ${move}`,
statusCode: result.statusCode,
error: result.statusCode >= 300 ? result.body : undefined,
detail: result.statusCode >= 300 ? { ...result.debug } : undefined,
});
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
await appendTestBotEvent({
ts: Date.now(),
direction: 'outbound',
verb: 'move',
summary: 'botQuery request failed',
error: message,
});
}
await recordTestBotContact(202);
return jsonResponse(202, { queued: true });
}
async function handlePost(
rawBody: string,
eventHeaders: APIGatewayProxyEvent['headers']
): Promise<APIGatewayProxyResult> {
const verification = await verifyBotRequest(rawBody, eventHeaders ?? {});
if (!verification.ok) {
await appendTestBotEvent({
ts: Date.now(),
direction: 'inbound',
verb: 'unknown',
summary: 'signature verification failed',
statusCode: 401,
error: verification.reason,
});
return jsonResponse(401, { message: verification.reason });
}
let payload: { verb?: string };
try {
payload = JSON.parse(rawBody) as { verb?: string };
} catch {
return jsonResponse(400, { message: 'Invalid JSON body' });
}
switch (payload.verb) {
case 'challenge':
return handleChallenge(payload as OutChallengePayload);
case 'move':
return handleMove(payload as OutMovePayload);
default:
return jsonResponse(400, { message: `Unknown verb '${payload.verb ?? ''}'` });
}
}
/** Lambda entry point — GET ping, POST signed challenge/move webhooks. */
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
console.log('testBot', event.httpMethod, event.path);
try {
if (event.httpMethod === 'GET') {
return await handlePing();
}
if (event.httpMethod === 'POST') {
return await handlePost(event.body ?? '', event.headers);
}
return jsonResponse(405, { message: 'Method not allowed' });
} catch (error: unknown) {
console.error('testBot handler error', error);
const message = error instanceof Error ? error.message : String(error);
return jsonResponse(500, { message });
}
};