@curless/hotel-storefront 0.75.0 → 0.75.1

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 (61) hide show
  1. package/dist/account/index.d.ts +12 -3
  2. package/dist/account/index.d.ts.map +1 -1
  3. package/dist/account/index.js +38 -1
  4. package/dist/account/index.js.map +1 -1
  5. package/dist/account/member-login.d.ts +6 -1
  6. package/dist/account/member-login.d.ts.map +1 -1
  7. package/dist/account/member-login.js +1 -1
  8. package/dist/account/member-login.js.map +1 -1
  9. package/dist/backend.d.ts +21 -1
  10. package/dist/backend.d.ts.map +1 -1
  11. package/dist/backend.js.map +1 -1
  12. package/dist/card.generated.d.ts +1 -1
  13. package/dist/card.generated.js +2 -2
  14. package/dist/card.generated.js.map +1 -1
  15. package/dist/choices.d.ts +16 -17
  16. package/dist/choices.d.ts.map +1 -1
  17. package/dist/choices.js +18 -43
  18. package/dist/choices.js.map +1 -1
  19. package/dist/huazhu/backend.d.ts +16 -1
  20. package/dist/huazhu/backend.d.ts.map +1 -1
  21. package/dist/huazhu/backend.js +39 -4
  22. package/dist/huazhu/backend.js.map +1 -1
  23. package/dist/huazhu/catalog.d.ts.map +1 -1
  24. package/dist/huazhu/catalog.js +20 -0
  25. package/dist/huazhu/catalog.js.map +1 -1
  26. package/dist/huazhu/english.d.ts +12 -1
  27. package/dist/huazhu/english.d.ts.map +1 -1
  28. package/dist/huazhu/english.js +55 -4
  29. package/dist/huazhu/english.js.map +1 -1
  30. package/dist/legal.d.ts.map +1 -1
  31. package/dist/legal.js +4 -5
  32. package/dist/legal.js.map +1 -1
  33. package/dist/otaless/backend.d.ts +6 -0
  34. package/dist/otaless/backend.d.ts.map +1 -1
  35. package/dist/otaless/backend.js +86 -7
  36. package/dist/otaless/backend.js.map +1 -1
  37. package/dist/otaless/booking.d.ts +29 -7
  38. package/dist/otaless/booking.d.ts.map +1 -1
  39. package/dist/otaless/booking.js +179 -34
  40. package/dist/otaless/booking.js.map +1 -1
  41. package/dist/present.d.ts.map +1 -1
  42. package/dist/present.js +3 -5
  43. package/dist/present.js.map +1 -1
  44. package/dist/server.d.ts +21 -2
  45. package/dist/server.d.ts.map +1 -1
  46. package/dist/server.js +340 -24
  47. package/dist/server.js.map +1 -1
  48. package/dist/surface.d.ts.map +1 -1
  49. package/dist/surface.js +10 -3
  50. package/dist/surface.js.map +1 -1
  51. package/dist/tools.d.ts +18 -1
  52. package/dist/tools.d.ts.map +1 -1
  53. package/dist/tools.js +153 -77
  54. package/dist/tools.js.map +1 -1
  55. package/dist/trip-ids.d.ts +44 -0
  56. package/dist/trip-ids.d.ts.map +1 -1
  57. package/dist/trip-ids.js +105 -1
  58. package/dist/trip-ids.js.map +1 -1
  59. package/dist/types.d.ts +16 -2
  60. package/dist/types.d.ts.map +1 -1
  61. package/package.json +1 -1
package/dist/server.js CHANGED
@@ -4,12 +4,13 @@ import { dirname, join, resolve } from 'node:path';
4
4
  import { BRAND_FONTS, BRAND_FONTS_VERSION, familiesOf, fontAsset, fontHeaders, isBrandFont, weightsOf, } from '@curless/brand-fonts';
5
5
  import { Hono } from 'hono';
6
6
  import { bodyLimit } from 'hono/body-limit';
7
- import { addressKey, makeAccount } from './account/index.js';
7
+ import { SIGN_IN_META_KEY, addressKey, makeAccount } from './account/index.js';
8
8
  import { makeMemberLogin } from './account/member-login.js';
9
+ import { hashOf } from './account/requests.js';
9
10
  import { cacheFor, readAsset, versionedUrl } from './assets.js';
10
11
  import { FULL_CAPABILITIES } from './backend.js';
11
12
  import { HOTEL_CARD_HTML } from './card.generated.js';
12
- import { networkOfAddress, parseAddress } from './cidr.js';
13
+ import { isPrivateAddress, networkOfAddress, parseAddress } from './cidr.js';
13
14
  import { GUESTS_META_KEY, MEMBER_META_KEY, guestsApart, memberApart } from './guests.js';
14
15
  import { docsPage, privacyPage, supportPage, termsPage } from './legal.js';
15
16
  import { Misses, WindowCounter } from './limits.js';
@@ -21,10 +22,11 @@ import { REFUSED_SAID, SOURCE_FILE_MAX_BYTES, checkSourceAllowlist, makeSourceAl
21
22
  import { StateDir } from './state.js';
22
23
  import { makeSupplier } from './supplier.js';
23
24
  import { buildInstructions, listTools, tripToolNames } from './surface.js';
24
- import { TERMS_META_KEY, agreeToTerms, bookingRefusal, buildOutputSchemas, buildTools, commonFields, contextFor, handleTool, homeListing, identityFor, outputSchemasFor, tripGone, withheldTools, } from './tools.js';
25
- import { ID_TTL_MS, SAME_NETWORK_MS, TAKE_OVER_MS, TripIds, networkOf } from './trip-ids.js';
25
+ import { MEMBER_SIGN_IN_META_KEY, TERMS_META_KEY, agreeToTerms, bookingRefusal, buildOutputSchemas, buildTools, commonFields, contextFor, handleTool, homeListing, identityFor, outputSchemasFor, tripGone, withheldTools, } from './tools.js';
26
+ import { ID_TTL_MS, SAME_NETWORK_MS, SignInClaims, TAKE_OVER_MS, TripIds, networkOf, } from './trip-ids.js';
26
27
  import { TripStore, tripKey } from './trip.js';
27
28
  import { startUsage } from './usage/boot.js';
29
+ import { OPENAI, rangesOf } from './usage/platform.js';
28
30
  import { checkDemoData, resolveBrand } from './vocab.js';
29
31
  // The connector itself: one config in, one HTTP app out.
30
32
  //
@@ -54,7 +56,7 @@ export const INITIALIZE_PER_MINUTE = 300;
54
56
  * number. Found by reading the boot banner of a container that had just been
55
57
  * deployed (2026-09-22): seventeen tools listed, and "0.7.0" above them.
56
58
  */
57
- export const VERSION = '0.75.0';
59
+ export const VERSION = '0.75.1';
58
60
  /**
59
61
  * The card's resource URI.
60
62
  *
@@ -63,7 +65,7 @@ export const VERSION = '0.75.0';
63
65
  * connector went storefront-v35 → v40 in a single day of UI work for exactly
64
66
  * this reason.
65
67
  */
66
- export const CARD_URI = 'ui://curless/hotel/booking-v152.html';
68
+ export const CARD_URI = 'ui://curless/hotel/booking-v153.html';
67
69
  /** The card as it ships, hashed once - what a host caches under the URI. */
68
70
  const CARD_HTML_SHA = createHash('sha256').update(HOTEL_CARD_HTML).digest('hex');
69
71
  /**
@@ -400,6 +402,62 @@ export const buildServer = (opts) => {
400
402
  againFile?.changed();
401
403
  return key;
402
404
  })();
405
+ /**
406
+ * ⚠️ ONE CHATGPT CONVERSATION, ONE KEY (0.75.1, 总线程 11-18). ChatGPT opens a new MCP
407
+ * connection - a new `initialize`, a new `Mcp-Session-Id` - for every tool call, so a
408
+ * conversation keyed by its connection was a new one on every card: the sign-in, the
409
+ * membership links and the trip of the card before were not on the next (k=3OUE2Bfr,
410
+ * then k=BHb9E1dS, in one conversation). Every call ChatGPT makes - the model's and the
411
+ * card's - carries `X-OpenAI-Session`, its pseudonymous id of the conversation, so that
412
+ * is the conversation's key - on three conditions, or the call is keyed as before:
413
+ * - the request reached Caddy from one of OpenAI's published ranges (`OPENAI`), by
414
+ * `sourceOf` ALONE: the last `X-Forwarded-For` entry, which Caddy writes (it has no
415
+ * trusted_proxies, so whatever the caller sent there is replaced), or the connection's
416
+ * own address where that is public. NEVER `X-Caller-Forwarded-For` - Caddy's verbatim
417
+ * copy of the caller's own header - or any other header the caller writes: the
418
+ * header's value is not a secret (OpenAI sends it to the connectors a conversation
419
+ * uses; whether it differs per connector is not documented), the address it is
420
+ * called from is what nobody else has;
421
+ * - the value has the shape of ChatGPT's (`OPENAI_SESSION_SHAPE`);
422
+ * - the client did not say it draws no card: a client in words carries its
423
+ * conversation by its id, in words (`Sessions.bind`), and that is left as it is.
424
+ * The key is `oai:` and 32 hex characters of an HMAC of the value - never the value
425
+ * itself, in a key, a log line or the state directory - under a key of its own, made
426
+ * from `againKey` with a label: kept in the state directory, so a restart still finds
427
+ * the conversation; unknown to anyone who reads the header elsewhere. Then `tripKey`,
428
+ * as any conversation's.
429
+ * `HOTEL_OPENAI_CONVERSATION=off` turns it off: every call keyed by its connection again.
430
+ */
431
+ const openaiOn = (opts.env ?? process.env).HOTEL_OPENAI_CONVERSATION !== 'off';
432
+ const openaiMac = createHmac('sha256', againKey).update('hotel-openai-conversation-v1').digest();
433
+ /** Headers not taken since the line that last said so - counts only, never a value or an address. */
434
+ const openaiSkipped = { outside: 0, shape: 0, loggedAt: Number.NEGATIVE_INFINITY };
435
+ const openaiConversation = (c) => {
436
+ if (!openaiOn)
437
+ return undefined;
438
+ const raw = c.req.header('x-openai-session');
439
+ if (raw === undefined)
440
+ return undefined;
441
+ const why = !OPENAI.has(sourceOf(c))
442
+ ? 'outside'
443
+ : !OPENAI_SESSION_SHAPE.test(raw)
444
+ ? 'shape'
445
+ : undefined;
446
+ if (why) {
447
+ // So a list of OpenAI's ranges that has fallen behind shows up as lines.
448
+ openaiSkipped[why] += 1;
449
+ const t = now();
450
+ if (t - openaiSkipped.loggedAt >= OPENAI_SKIPPED_LOG_MS) {
451
+ console.log(`[mcp] x-openai-session not taken: outside OpenAI's ranges=${openaiSkipped.outside} not its shape=${openaiSkipped.shape}`);
452
+ openaiSkipped.outside = 0;
453
+ openaiSkipped.shape = 0;
454
+ openaiSkipped.loggedAt = t;
455
+ }
456
+ return undefined;
457
+ }
458
+ const mac = createHmac('sha256', openaiMac).update(raw).digest('hex').slice(0, 32);
459
+ return tripKey(`${OPENAI_KEY_PREFIX}${mac}`, config.brand);
460
+ };
403
461
  const misses = new Misses(now);
404
462
  // ⚠️ THE "refused: no session id" LINE, ONE A MINUTE A METHOD (2026-11-12). Claude
405
463
  // (`Claude-User`) probes `server/discover` - the 2026-07-28 revision's
@@ -547,6 +605,36 @@ export const buildServer = (opts) => {
547
605
  now,
548
606
  quiet: opts.background === false,
549
607
  }) ?? undefined;
608
+ /**
609
+ * ⚠️ A SIGN-IN GIVES THE CONVERSATION A NEW TRIP ID (P1-a, risk A - 0.75.1, 总线程 11-18):
610
+ * the account's or the membership's, on its page - see `TripIds.renew`, and tools/call for
611
+ * who may still name the one before (its own calls alone). A trip id someone came by before
612
+ * the guest signed in no longer signs them out, signs anyone in or reaches a booking. The
613
+ * card that started the sign-in takes the new id with the sign-in's claim (`SignInClaims`,
614
+ * `sign`: that sign-in's link code, hashed). Never in the way of the sign-in itself: a fault
615
+ * here is logged, and the sign-in stands.
616
+ * `HOTEL_TRIP_RENEW=off` turns it off - the way back if a real card is turned away: no new
617
+ * id at a sign-in, no claim, and an id given out again before names its conversation for
618
+ * anyone again (tools/call) - risk A as it was before 0.75.1.
619
+ */
620
+ const renewOn = (opts.env ?? process.env).HOTEL_TRIP_RENEW !== 'off';
621
+ const claims = new SignInClaims(now);
622
+ /** `HOTEL_CARD_SOURCES=off`: a card's call from anywhere, as before 0.75.1 (tools/call). */
623
+ const cardSourcesOn = (opts.env ?? process.env).HOTEL_CARD_SOURCES !== 'off';
624
+ /** Sign-ins and sign-outs by conversation, in ten minutes (tools/call) - in memory. */
625
+ const signSteps = new WindowCounter(10 * 60_000, now);
626
+ const renewTrip = (key, by, sign) => {
627
+ if (!renewOn)
628
+ return;
629
+ try {
630
+ claims.done(key, sign);
631
+ if (tripIds.renew(key))
632
+ console.log(`[mcp] trip id given out again: ${by} sign-in k=${h8(key)}`);
633
+ }
634
+ catch (err) {
635
+ console.error(`[mcp] trip id not given out again: ${err instanceof Error ? err.name : 'error'}`);
636
+ }
637
+ };
550
638
  // The deployment's own account, where it has one - see account/index.ts.
551
639
  const accountLinksFile = config.account ? state.file('account-links.json') : undefined;
552
640
  const account = config.account
@@ -571,6 +659,8 @@ export const buildServer = (opts) => {
571
659
  groupLogo: (code) => groupLogos.get(code),
572
660
  // Memberships linked, remembered per account across conversations and restarts (0.74.0).
573
661
  ...(accountLinksFile ? { linksFile: accountLinksFile } : {}),
662
+ // A sign-in gives the conversation a new trip id (0.75.1, risk A).
663
+ onSignedIn: (key, sign) => renewTrip(key, 'account', sign),
574
664
  // Sign-ins, accounts made, memberships linked - by category only, against
575
665
  // the conversation that began them (usage/recorder.ts bindKey).
576
666
  ...(usage
@@ -655,7 +745,11 @@ export const buildServer = (opts) => {
655
745
  publicUrl,
656
746
  signIn: (number, surname) => backend.signInMember(number, surname),
657
747
  signedIn: (key) => !!members.read(key),
658
- onSignedIn: (key, member) => members.set(key, member),
748
+ onSignedIn: (key, member, sign) => {
749
+ members.set(key, member);
750
+ // …as the account's does (0.75.1, risk A).
751
+ renewTrip(key, 'member', sign);
752
+ },
659
753
  misses,
660
754
  now,
661
755
  ...(logoUrl ? { logoUrl } : {}),
@@ -828,11 +922,18 @@ export const buildServer = (opts) => {
828
922
  ...(memberLogin ? [brand.tools.memberCard] : []),
829
923
  brand.tools.acceptTerms,
830
924
  ]);
831
- const appOnly = (m) => ({
832
- ...m,
833
- ui: { ...m.ui, visibility: ['app'] },
834
- 'openai/visibility': 'private',
835
- });
925
+ // ⚠️ NO `openai/outputTemplate` ON A CARD-ONLY TOOL (0.75.1, 总线程 11-18): ChatGPT's
926
+ // plugin settings marked the four "cannot render its widget" - private, yet naming a
927
+ // template. The card calls them (`openai/widgetAccessible`) and draws their answer
928
+ // itself; MCP Apps hosts go by `ui.resourceUri`, which stays.
929
+ const appOnly = (m) => {
930
+ const { 'openai/outputTemplate': _template, ...rest } = m;
931
+ return {
932
+ ...rest,
933
+ ui: { ...m.ui, visibility: ['app'] },
934
+ 'openai/visibility': 'private',
935
+ };
936
+ };
836
937
  // Worked out per answer: an API backend's photographs arrive with its hotel
837
938
  // list, which is still filling in on a first boot.
838
939
  //
@@ -940,10 +1041,13 @@ export const buildServer = (opts) => {
940
1041
  * false for a client that said it draws no card (0.59.0, review C2): no id is
941
1042
  * minted for it either.
942
1043
  */
943
- const tripMeta = (key, withId = true, from) => {
1044
+ const tripMeta = (key, withId = true, from, renews) => {
944
1045
  const lines = trips.read(key);
945
1046
  return {
946
1047
  ...(withId ? { tripId: tripIds.idFor(key, from) } : {}),
1048
+ // The id the call named, which this one replaces - given out again at a sign-in (0.75.1,
1049
+ // risk A): a card that sees its own id here takes the new one (`TripIds.renew`).
1050
+ ...(withId && renews ? { renews } : {}),
947
1051
  // Which deployment's trip this is: a card takes only its own
948
1052
  // (Shangri-La, Huazhu and the market all say curless/trip,
949
1053
  // and one conversation often has all three - 2026-10-14).
@@ -1514,7 +1618,7 @@ export const buildServer = (opts) => {
1514
1618
  // ⚠️ A TRIP THE CARD NAMES AND WE DO NOT KNOW IS SAID SO - see `tripGone`.
1515
1619
  // Anything else it asked is still answered, but with no trip beside it:
1516
1620
  // the card would take that trip's id as its own.
1517
- const lost = asked !== undefined && named === undefined;
1621
+ let lost = asked !== undefined && named === undefined;
1518
1622
  // ⚠️ ONLY A CLIENT THAT SAID IT DRAWS CARDS - never one we know nothing
1519
1623
  // of (`drawsCards` below; 总线程's review of 0.56.1): a session from
1520
1624
  // before a restart, or a connection that never initialized here, is not
@@ -1556,7 +1660,73 @@ export const buildServer = (opts) => {
1556
1660
  const client = sessions.clientOf(presented);
1557
1661
  /** This connection's conversation, in words - only once it has one (anchored). */
1558
1662
  const ownInWords = (k) => sessions.isAnchored(presented) ? tripIds.idFor(k, from, true) : undefined;
1559
- let own = (words ? sessions.boundOf(presented) : undefined) ?? tripKey(session, config.brand);
1663
+ /** This ChatGPT conversation's key, from OpenAI's own addresses (0.75.1) - see `openaiConversation`. */
1664
+ const chatgpt = words ? undefined : openaiConversation(c);
1665
+ let own = (words ? sessions.boundOf(presented) : undefined) ??
1666
+ chatgpt ??
1667
+ tripKey(session, config.brand);
1668
+ // ⚠️ A CARD'S CALL COMES FROM A PLATFORM'S OWN ADDRESSES (P1-a, risk A - 0.75.1, 总线程
1669
+ // 11-18). A call that names a conversation not its own - by a trip id or a sign-in's
1670
+ // claim - is a card's, or says it is: ChatGPT's card calls from OpenAI's published
1671
+ // ranges, claude.ai's from Anthropic's (source-ranges.ts), and nothing else draws a card
1672
+ // that does this. Anyone's script elsewhere that said it draws cards and named a trip id
1673
+ // it had come by was the conversation's card; now it is turned away before any tool runs.
1674
+ // By `sourceOf` ALONE, as `openaiConversation`: never a header the caller writes. Our own
1675
+ // side of the proxy passes too (`cardSourceOf` 'local': no address, a private or a
1676
+ // loopback one - a test, a local preview): a request from outside comes through Caddy,
1677
+ // which writes its public address. `HOTEL_CARD_SOURCES=off` turns it off. NOT for a
1678
+ // client in words: its own rules (`inWords`, below).
1679
+ // ⚠️ A RANGE LIST FALLEN BEHIND (`ranges-check`) turns real cards away here.
1680
+ const cardShaped = !words && asked !== undefined && (named === undefined ? claims.has(asked) : named !== own);
1681
+ if (cardShaped && cardSourcesOn && cardSourceOf(sourceOf(c)) === undefined) {
1682
+ calls.notCarried += 1;
1683
+ console.log(`[mcp] ${printable(name, 60)} refused: not_this_conversation (a card's call from outside the platforms' ranges) - client=${printable(client ?? '-', 60)}`);
1684
+ return ok(c, id, {
1685
+ content: [{ type: 'text', text: notCarriedSaid(undefined) }],
1686
+ structuredContent: {
1687
+ view: 'error',
1688
+ brand: config.brand,
1689
+ error: 'not_this_conversation',
1690
+ code: 'not_this_conversation',
1691
+ },
1692
+ isError: true,
1693
+ });
1694
+ }
1695
+ // ⚠️ A TRIP ID GIVEN OUT AGAIN AT A SIGN-IN (P1-a, risk A - 0.75.1, 总线程 11-18). Since a
1696
+ // sign-in, the conversation goes by a new id (`TripIds.renew`, `renewTrip`), and the one
1697
+ // before names it for the conversation's own calls alone (its own key: a ChatGPT call with
1698
+ // its header) - to anyone else it is gone (trip-gone, as an id never known). The card
1699
+ // that STARTED the sign-in was handed that sign-in's claim (`SignInClaims`): it names the
1700
+ // claim as its trip once the guest is signed in, and is handed the new id (`renews`) - the
1701
+ // claim is then used up. Whoever was using the old id before the sign-in has no claim.
1702
+ // NOT for a client in words: its ids are never given out again.
1703
+ /** The id (or claim) this call named, and the new id it is handed for it. */
1704
+ let renews;
1705
+ /** This call named a sign-in's claim, and took the conversation's new id with it. */
1706
+ let claimed = false;
1707
+ /** The id this call named was given out again, and this call may not name it any more. */
1708
+ let renewedAway = false;
1709
+ if (!words && asked !== undefined && renewOn) {
1710
+ if (named === undefined) {
1711
+ const k = claims.redeem(asked);
1712
+ if (k !== undefined) {
1713
+ named = k;
1714
+ lost = false;
1715
+ renews = asked;
1716
+ claimed = true;
1717
+ console.log(`[mcp] claim taken tool=${printable(name, 60)} s=${h8(presented)} k=${h8(k)} client=${printable(client ?? '-', 60)}`);
1718
+ }
1719
+ }
1720
+ else if (tripIds.renewedOf(asked)) {
1721
+ if (named === own)
1722
+ renews = asked;
1723
+ else {
1724
+ named = undefined;
1725
+ lost = true;
1726
+ renewedAway = true;
1727
+ }
1728
+ }
1729
+ }
1560
1730
  if (words && lost) {
1561
1731
  console.log(`[mcp] trip-gone tool=${printable(name, 60)} id=${h8(asked ?? '')} knownIds=${tripIds.size} client=${printable(sessions.clientOf(presented) ?? '-', 60)} cards=none`);
1562
1732
  const mine = ownInWords(own);
@@ -1576,7 +1746,7 @@ export const buildServer = (opts) => {
1576
1746
  // Which id, from what app - never the address, the arguments or
1577
1747
  // anything said in it (2026-10-14: robin met this on a new
1578
1748
  // conversation, and nothing said why).
1579
- console.log(`[mcp] trip-gone tool=${printable(name, 60)} id=${h8(asked ?? '')} knownIds=${tripIds.size} client=${printable(sessions.clientOf(presented) ?? '-', 60)} ua=${printable(c.req.header('user-agent') ?? '-', 80)}`);
1749
+ console.log(`[mcp] trip-gone tool=${printable(name, 60)} id=${h8(asked ?? '')} knownIds=${tripIds.size} client=${printable(sessions.clientOf(presented) ?? '-', 60)} ua=${printable(c.req.header('user-agent') ?? '-', 80)}${renewedAway ? ' renewed=sign-in' : asked !== undefined && claims.has(asked) ? ' claim=not-yet' : ''}`);
1580
1750
  const gone = tripGone(deps);
1581
1751
  return ok(c, id, {
1582
1752
  content: [{ type: 'text', text: gone.text }],
@@ -1603,6 +1773,33 @@ export const buildServer = (opts) => {
1603
1773
  isError: true,
1604
1774
  });
1605
1775
  }
1776
+ // ⚠️ A CHATGPT CONVERSATION'S TRIP IS ITS OWN ALONE (0.75.1 - and P1-a, risk A, 总线程
1777
+ // 11-18). Its own cards carry its header, as every ChatGPT call does, so a trip under an
1778
+ // `oai:` key is named only by a call of that same conversation (`chatgpt`: its
1779
+ // `X-OpenAI-Session`, from OpenAI's own addresses). From another conversation, from
1780
+ // OpenAI's ranges with no header, from anywhere else - a script that copied the header
1781
+ // included - it is not followed, whichever tool names it, before any tool runs: no
1782
+ // account token, no sign-in link, no booking. A trip from before 0.75.1 (a card drawn
1783
+ // before the deploy, keyed by its connection) is under no `oai:` key, and is named as it
1784
+ // always was - P2-1's rules below, and the id given out again at a sign-in (above).
1785
+ // ⚠️ SO A LIST OF OPENAI'S RANGES FALLEN BEHIND (source-ranges.ts, `ranges-check`) turns
1786
+ // a real card of such a conversation away, as it already stops the key itself; and so
1787
+ // does HOTEL_OPENAI_CONVERSATION=off, for the cards drawn while it was on. Not for a
1788
+ // client in words: it names no card's id (`inWords`), and its answer is its own (below).
1789
+ if (!words && named !== undefined && named !== own && named.startsWith(OPENAI_KEY_PREFIX)) {
1790
+ calls.notCarried += 1;
1791
+ console.log(`[mcp] ${printable(name, 60)} refused: not_this_conversation (${chatgpt !== undefined ? "another ChatGPT conversation's trip" : "a ChatGPT conversation's trip, from outside it"}) - client=${printable(client ?? '-', 60)}`);
1792
+ return ok(c, id, {
1793
+ content: [{ type: 'text', text: notCarriedSaid(undefined) }],
1794
+ structuredContent: {
1795
+ view: 'error',
1796
+ brand: config.brand,
1797
+ error: 'not_this_conversation',
1798
+ code: 'not_this_conversation',
1799
+ },
1800
+ isError: true,
1801
+ });
1802
+ }
1606
1803
  /** The conversation the trip id named was quiet too long to be taken over (0.67.1) - said in the answer. */
1607
1804
  let expired = false;
1608
1805
  /** This call took a conversation over: from the same network, or not ('-': not known). */
@@ -1727,7 +1924,46 @@ export const buildServer = (opts) => {
1727
1924
  // deploy would otherwise lose its conversation's trip (see sessions.ts).
1728
1925
  // (Since 0.67.0 such a client's model is given the id in the answer's
1729
1926
  // words instead, on purpose - `inWords` below - and never in `_meta`.)
1730
- const handsTripId = ours && cardsSaid !== 'none';
1927
+ // …and to a call that named a sign-in's claim, or (its own conversation's call) the id
1928
+ // given out again at a sign-in (`renews`): the new one, so the card moves on to it (0.75.1).
1929
+ const handsTripId = (ours || renews !== undefined) && cardsSaid !== 'none';
1930
+ /**
1931
+ * ⚠️ WHO IS TOLD WHOSE ACCOUNT IT IS (0.75.1 - P1-a, the review's C1 ④, 总线程 11-18): the
1932
+ * conversation's own calls, and a call naming the conversation's CURRENT trip id - the
1933
+ * one given out at its latest sign-in (claude.ai's card names it on every call) - or a
1934
+ * sign-in's claim: the name, the masked facts, the level and a membership's hidden number,
1935
+ * as robin 11-10 and 总线程 11-16 had them. Any other - an id given out again, while
1936
+ * `HOTEL_TRIP_RENEW=off` lets it be named - is told whether it is signed in, and no more
1937
+ * (`account.meta`).
1938
+ */
1939
+ const wholeAccount = ours ||
1940
+ claimed ||
1941
+ (asked !== undefined && named !== undefined && !tripIds.renewedOf(asked));
1942
+ /**
1943
+ * The claim on a sign-in this answer starts (0.75.1, risk A): beside its link, in
1944
+ * `curless/trip` - for the card the call came from, or the card drawn from the
1945
+ * conversation's own answer. Not to a client in words (its ids are not given out again).
1946
+ */
1947
+ const claimOf = (m) => {
1948
+ if (!renewOn || words || !m)
1949
+ return {};
1950
+ const link = (m[SIGN_IN_META_KEY] ?? m[MEMBER_SIGN_IN_META_KEY]);
1951
+ if (typeof link?.url !== 'string')
1952
+ return {};
1953
+ let code = null;
1954
+ try {
1955
+ code = new URL(link.url).searchParams.get('code');
1956
+ }
1957
+ catch {
1958
+ return {};
1959
+ }
1960
+ if (!code)
1961
+ return {};
1962
+ const until = typeof link.expiresAt === 'string' && Number.isFinite(Date.parse(link.expiresAt))
1963
+ ? Date.parse(link.expiresAt)
1964
+ : now() + 15 * 60_000;
1965
+ return { claim: claims.issue(key, hashOf(code), until) };
1966
+ };
1731
1967
  // ⚠️ DIAGNOSTICS, WHERE THERE IS AN ACCOUNT (0.56.1): which connection
1732
1968
  // reached which conversation, by 8-character fingerprints - never an id,
1733
1969
  // a trip id or an argument. claude.ai spread one conversation's calls
@@ -1735,7 +1971,9 @@ export const buildServer = (opts) => {
1735
1971
  // assistant looked at another. These lines say how.
1736
1972
  if (account) {
1737
1973
  const trip = asked === undefined ? 'none' : lost ? 'lost' : named === own ? 'own' : 'named';
1738
- console.log(`[mcp] call ${printable(name, 60)} s=${h8(presented)} k=${h8(key)} trip=${trip} client=${printable(sessions.clientOf(presented) ?? '-', 40)} cards=${sessions.cardsOf(presented) ?? '-'}`);
1974
+ console.log(
1975
+ // `conv=oai`: the conversation is ChatGPT's (0.75.1) - `k` is still a fingerprint of the key.
1976
+ `[mcp] call ${printable(name, 60)} s=${h8(presented)} k=${h8(key)} trip=${trip} client=${printable(sessions.clientOf(presented) ?? '-', 40)} cards=${sessions.cardsOf(presented) ?? '-'}${chatgpt ? ' conv=oai' : ''}`);
1739
1977
  }
1740
1978
  // ⚠️ LOOKING IS NOT ACTING, FOR A CLIENT THAT DRAWS NO CARD (0.67.0): a new
1741
1979
  // connection whose model looked something up before passing the trip id
@@ -1796,6 +2034,44 @@ export const buildServer = (opts) => {
1796
2034
  }
1797
2035
  /** What every answer to such a client says beside its own words - see `inWordsOf`. */
1798
2036
  const said = (text, structured) => inWordsOf(text, structured, inWords, looking && STATE_READS.has(name), expired);
2037
+ // ⚠️ SIGN-INS AND SIGN-OUTS, SO MANY A CONVERSATION (P1-a, risk A - 0.75.1, 总线程 11-18):
2038
+ // `SIGN_STEPS_PER_TEN_MINUTES` of each in ten minutes, the account's and the
2039
+ // membership's together, whichever id or claim names the conversation - its ids change
2040
+ // at every sign-in, so counted by the conversation they name. A guest signs in once or
2041
+ // twice; a trip id someone came by does not sign them out and in all afternoon. Said in
2042
+ // the log by fingerprints only.
2043
+ const signStep = (account &&
2044
+ (name === account.names.signIn ||
2045
+ (name === account.names.card && args.action === 'sign_in'))) ||
2046
+ (memberLogin &&
2047
+ (name === brand.tools.signIn ||
2048
+ (name === brand.tools.memberCard && args.action === 'sign_in')))
2049
+ ? 'in'
2050
+ : (account &&
2051
+ (name === account.names.signOut ||
2052
+ (name === account.names.card && args.action === 'sign_out'))) ||
2053
+ (memberLogin && name === brand.tools.signOut)
2054
+ ? 'out'
2055
+ : undefined;
2056
+ if (signStep) {
2057
+ const counted = `${signStep}|${key}`;
2058
+ if (signSteps.count(counted) >= SIGN_STEPS_PER_TEN_MINUTES) {
2059
+ calls.accountRefused += 1;
2060
+ console.log(`[mcp] ${printable(name, 60)} refused: too_many_sign_${signStep === 'in' ? 'ins' : 'outs'} k=${h8(key)} id=${asked === undefined ? '-' : h8(asked)} (${SIGN_STEPS_PER_TEN_MINUTES} in ten minutes)`);
2061
+ const code = signStep === 'in' ? 'too_many_sign_ins' : 'too_many_sign_outs';
2062
+ return ok(c, id, {
2063
+ content: [
2064
+ {
2065
+ type: 'text',
2066
+ text: `Too many ${signStep === 'in' ? 'sign-ins' : 'sign-outs'} in this conversation just now, so nothing was done. Try again in a few minutes.`,
2067
+ },
2068
+ ],
2069
+ structuredContent: { view: 'error', brand: config.brand, error: code, code },
2070
+ isError: true,
2071
+ });
2072
+ }
2073
+ signSteps.add(counted);
2074
+ }
1799
2075
  if (account?.has(name)) {
1800
2076
  // ⚠️ WHO MAY NAME WHICH CONVERSATION (review P2-1). The assistant's
1801
2077
  // three: their own, never one a trip id names. The card's: the one
@@ -1866,10 +2142,13 @@ export const buildServer = (opts) => {
1866
2142
  ...(lost
1867
2143
  ? {}
1868
2144
  : {
1869
- [TRIP_META_KEY]: tripMeta(key, handsTripId, ip ? addressKey(ip) : undefined),
2145
+ [TRIP_META_KEY]: {
2146
+ ...tripMeta(key, handsTripId, ip ? addressKey(ip) : undefined, renews),
2147
+ ...claimOf(answer.meta),
2148
+ },
1870
2149
  }),
1871
2150
  ...(CARDLESS.has(name) ? {} : meta()),
1872
- ...account.meta(key, { cards: saidCards, ours }),
2151
+ ...account.meta(key, { cards: saidCards, ours: wholeAccount }),
1873
2152
  ...(answer.meta ?? {}),
1874
2153
  },
1875
2154
  });
@@ -1982,8 +2261,12 @@ export const buildServer = (opts) => {
1982
2261
  ...(saidCards ? { drawsCards: true } : {}),
1983
2262
  // The conversation's id, for what its words tell the model to pass on (0.67.0).
1984
2263
  ...(inWords ? { tripInWords: inWords } : {}),
1985
- // …and the conversation it took over, whose bookings are its own (0.67.1).
1986
- ...(words && own !== tripKey(session, config.brand) ? { conversation: own } : {}),
2264
+ // …and the conversation it took over, whose bookings are its own (0.67.1) - or
2265
+ // the ChatGPT conversation it is a call of (0.75.1): a booking is the
2266
+ // conversation's, not the one connection's it was made on.
2267
+ ...((words || chatgpt !== undefined) && own !== tripKey(session, config.brand)
2268
+ ? { conversation: own }
2269
+ : {}),
1987
2270
  });
1988
2271
  // The guest's own details beside the answer, never in it - see `guestsApart`.
1989
2272
  const { shown, guests } = guestsApart(answer.structured);
@@ -2010,8 +2293,13 @@ export const buildServer = (opts) => {
2010
2293
  _meta: {
2011
2294
  ...(lost
2012
2295
  ? {}
2013
- : { [TRIP_META_KEY]: tripMeta(key, handsTripId, ip ? addressKey(ip) : undefined) }),
2014
- ...(account ? account.meta(key, { cards: saidCards, ours }) : {}),
2296
+ : {
2297
+ [TRIP_META_KEY]: {
2298
+ ...tripMeta(key, handsTripId, ip ? addressKey(ip) : undefined, renews),
2299
+ ...claimOf(answer.meta),
2300
+ },
2301
+ }),
2302
+ ...(account ? account.meta(key, { cards: saidCards, ours: wholeAccount }) : {}),
2015
2303
  ...(CARDLESS.has(name) ? {} : meta()),
2016
2304
  ...(Object.keys(guests).length ? { [GUESTS_META_KEY]: guests } : {}),
2017
2305
  // Only to a client that said it draws cards: one that draws none may
@@ -2143,6 +2431,34 @@ export const HEADER_APPS = 200;
2143
2431
  */
2144
2432
  const sourceOf = (c) => sourceAddress(clientIp(c.req.header('x-forwarded-for')), c.env?.incoming?.socket
2145
2433
  ?.remoteAddress);
2434
+ /**
2435
+ * ChatGPT's conversation id, as it is taken (0.75.1): 16 to 256 characters, every one
2436
+ * printable ASCII other than the space (0x21-0x7E). ChatGPT sends 79 opaque ones, one
2437
+ * per conversation (moody's access log, docs/shopify-connectors.md). Anything else -
2438
+ * shorter, longer, a space, a control character or a non-ASCII one, or two headers
2439
+ * joined with ", " - is not taken, and the call is keyed by its connection as before.
2440
+ */
2441
+ export const OPENAI_SESSION_SHAPE = /^[\x21-\x7e]{16,256}$/;
2442
+ /** What a ChatGPT conversation's key starts with - a session id we issue (a UUID) never does. */
2443
+ export const OPENAI_KEY_PREFIX = 'oai:';
2444
+ /** How often, at most, the count of `X-OpenAI-Session` headers not taken is logged. */
2445
+ const OPENAI_SKIPPED_LOG_MS = 10 * 60_000;
2446
+ /** The platforms whose cards call us: ChatGPT's from OpenAI's published ranges, claude.ai's from Anthropic's. */
2447
+ const CARD_PLATFORMS = [OPENAI, rangesOf('anthropic')];
2448
+ /**
2449
+ * Where a card's call came from (0.75.1, risk A), by `sourceOf` alone: 'platform' - OpenAI's or
2450
+ * Anthropic's published ranges; 'local' - our own side of the proxy: no address, a private or a
2451
+ * loopback one (a test, a local preview - a request from outside comes through Caddy, which
2452
+ * writes its public address); undefined - anywhere else.
2453
+ */
2454
+ export const cardSourceOf = (address) => {
2455
+ const a = parseAddress(address);
2456
+ if (!a || isPrivateAddress(a))
2457
+ return 'local';
2458
+ return CARD_PLATFORMS.some((r) => r.has(address)) ? 'platform' : undefined;
2459
+ };
2460
+ /** Sign-ins, and sign-outs, one conversation may make in ten minutes (0.75.1, risk A) - each. */
2461
+ export const SIGN_STEPS_PER_TEN_MINUTES = 6;
2146
2462
  export const clientIp = (header) => {
2147
2463
  const last = (header ?? '').split(',').at(-1)?.trim();
2148
2464
  return last || undefined;