@substrat-run/vertical-host 0.220.0 → 0.222.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.
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import { assertPlatformCall, PlatformCallError } from '@substrat-run/contracts/w
7
7
  import { wirePersonActor, wireVerifiedCallerOf } from '@substrat-run/contracts/wire-signing';
8
8
  import { platformSweeperOf, registerScopeSweepHost } from './scope-sweep-host.js';
9
9
  import { isVerticalConnectors, mountConnectorCalls } from './connectors.js';
10
- import { z, PROBLEM_CONTENT_TYPE, scopeLineage, scopeId as scopeIdOf, scopeDumpSample, tenantId as tenantIdOf, principalId as principalIdOf, connectionId as connectionIdOf, permissionKey as permissionKeyOf, moduleId as moduleIdOf, switchedOffInUnit, visibility, instant, entitlementGrant, projectedConnectionGrant, projectedConnectionKey, projectedIdentityLink, ownerSeat, ownerClaimLink, coverage, coverageRefusal, memberInviteLink, memberRemoval, scopeMembers, platformActorId, ownerTransferPair, distinctOwnerTransfer, ownerTransferRecord, ownerTransferAbandon, platformRequestFilter, denialFilter, capabilityFilterQuery, BECOME_LINK_STATES_MAX_IDS, capabilityId, becomeLinkState, capabilityRevokeAnswer, capabilityRevokeRequest, platformRequestId, platformRequestStatus, platformRequestFailure, platformOutcomeEvent, readScopeTableInput, entityHistoryInput, eventFacetInput, eventCauseInput, eventEffectsInput, invocationEventsInput, deadLettersInput, lifecycleFlowInput, operationSeriesShape, operationSeriesWindow, queryScopeInput, scopeDumpObject, errorCodeOf, PARENT_REQUEST_ID_HEADER, TRACEPARENT_HEADER, scopeLifecycle, verticalCaller, verticalSlug as verticalSlugOf, PEER_CLIENT_OPERATION, peerClientAnswer, peerOnBehalf, substratError, exportReadInput, importBatch, importCursorMoveAt, CONNECTOR_ATTACHMENT_META_HEADER, CONNECTOR_ATTACHMENT_BUFFERED_MAX_BYTES, CONNECTOR_ATTACHMENT_META_MAX_BYTES, CONNECTOR_ATTACHMENT_MULTIPART_ENVELOPE_BYTES, CONNECTOR_ATTACHMENT_RECORD_HEADER, CONNECTOR_ATTACHMENT_STREAM_MAX_BYTES, connectorAttachmentBodyRefusal, connectorAttachmentBufferedRefusal, connectorAttachmentMetaBytes, connectorAttachmentMetaRefusal, connectorAttachmentSizeRefusal, LOAD_STAMP_HEADER, WRITE_REVISION_HEADER, entityGrantShape, ATTACHMENT_FIELD_MAX, attachmentRecord, attachmentUploadFields, PERSONAS_PATH, SEED_WORLD_PATH, personasBody, seedWorldBody, seededPersonas, BROKER_RELAY_PATH, DELIVERY_COMPARISON_PATH, PLATFORM_BROKER_BINDING, BROKER_DEAD_LETTERS_PATH, BROKER_DEAD_LETTER_RESEND_PATH, BROKER_DEAD_LETTER_DISCARD_PATH, brokerDeadLetterActionRequest, brokerDeadLettersRequest, deadLetterView, relayAnswer, relayEnvelope, } from '@substrat-run/contracts';
10
+ import { z, PROBLEM_CONTENT_TYPE, scopeLineage, scopeId as scopeIdOf, scopeDumpSample, tenantId as tenantIdOf, principalId as principalIdOf, connectionId as connectionIdOf, permissionKey as permissionKeyOf, moduleId as moduleIdOf, switchedOffInUnit, visibility, instant, entitlementGrant, projectedConnectionGrant, projectedConnectionKey, projectedIdentityLink, ownerSeat, ownerClaimLink, coverage, coverageRefusal, memberInviteLink, memberRemoval, scopeMembers, platformActorId, ownerTransferPair, distinctOwnerTransfer, ownerTransferRecord, ownerTransferAbandon, platformRequestFilter, denialFilter, capabilityFilterQuery, BECOME_LINK_STATES_MAX_IDS, capabilityId, becomeLinkState, capabilityRevokeAnswer, capabilityRevokeRequest, platformRequestId, platformRequestStatus, platformRequestFailure, platformOutcomeEvent, readScopeTableInput, entityHistoryInput, eventFacetInput, eventCauseInput, eventEffectsInput, invocationEventsInput, deadLettersInput, lifecycleFlowInput, operationSeriesShape, operationSeriesWindow, queryScopeInput, scopeDumpObject, errorCodeOf, PARENT_REQUEST_ID_HEADER, TRACEPARENT_HEADER, scopeLifecycle, verticalCaller, verticalSlug as verticalSlugOf, PEER_CLIENT_OPERATION, peerClientAnswer, peerOnBehalf, substratError, exportReadInput, importBatch, importCursorMoveAt, CONNECTOR_ATTACHMENT_META_HEADER, CONNECTOR_ATTACHMENT_BUFFERED_MAX_BYTES, CONNECTOR_ATTACHMENT_META_MAX_BYTES, CONNECTOR_ATTACHMENT_MULTIPART_ENVELOPE_BYTES, CONNECTOR_ATTACHMENT_RECORD_HEADER, CONNECTOR_ATTACHMENT_STREAM_MAX_BYTES, connectorAttachmentBodyRefusal, connectorAttachmentBufferedRefusal, connectorAttachmentMetaBytes, connectorAttachmentMetaRefusal, connectorAttachmentSizeRefusal, LOAD_STAMP_HEADER, WRITE_REVISION_HEADER, entityGrantShape, ATTACHMENT_FIELD_MAX, attachmentRecord, attachmentUploadFields, PERSONAS_PATH, SEED_WORLD_PATH, personasBody, seedWorldBody, seededPersonas, BROKER_RELAY_PATH, DELIVERY_COMPARISON_PATH, PLATFORM_BROKER_BINDING, BROKER_DEAD_LETTERS_PATH, BROKER_DEAD_LETTER_RESEND_PATH, BROKER_DEAD_LETTER_DISCARD_PATH, brokerDeadLetterActionRequest, brokerDeadLettersRequest, environmentName, deadLetterView, relayAnswer, relayEnvelope, SCOPE_DUE_HOST_KEY, personErasure, personErasureRequest, } from '@substrat-run/contracts';
11
11
  /**
12
12
  * The denial-log filter, off the query string (#867). Both denial routes take the same
13
13
  * one, and it is PARSED here rather than forwarded: what arrives is text, and what it
@@ -44,6 +44,14 @@ const provisionBody = z.object({
44
44
  tenantHeldPeers: z.array(verticalSlugOf).optional(),
45
45
  /** #2045: each recorded-off subject's fence, by tuple subject. */
46
46
  switchFences: z.record(z.string().min(1), z.string().min(1)).optional(),
47
+ /** #2993 part 3: the scope's declared type, sent only when it is not `home`; answered back once fixed. */
48
+ scopeType: z.string().min(1).optional(),
49
+ /**
50
+ * #2995 (K-64): the scope this one is a CHILD of, sent by `provision-child` only; answered back once
51
+ * fixed in the scope's own storage. Not `parentScopeId`, which a sibling's provision sends too and
52
+ * which names the scope that asked, not a link.
53
+ */
54
+ childOf: scopeIdOf.optional(),
47
55
  });
48
56
  /** `/internal/reconcile` carries no owner — the vertical re-sources it via `resolveOwner` (#332). */
49
57
  const reconcileBody = z.object({
@@ -537,6 +545,9 @@ export function mountPlatformSurface(app, deps) {
537
545
  // (`scope-sweep-host.ts`). Registered on every mount, since nothing at mount time can tell
538
546
  // a hosted script from a local one, and a registration nobody reads costs nothing.
539
547
  registerScopeSweepHost(deps.hostFor);
548
+ // #3540: and for the vertical's own scope object, whose due alarm runs its work due by event
549
+ // through this host rather than asking the sweeper to (`SCOPE_DUE_HOST_KEY` says why a key of its own).
550
+ globalThis[Symbol.for(SCOPE_DUE_HOST_KEY)] = deps.hostFor;
540
551
  if (deps.connectors !== undefined && !isVerticalConnectors(deps.connectors)) {
541
552
  throw new Error('mountPlatformSurface: connectors must be the value defineConnectors returns');
542
553
  }
@@ -986,16 +997,19 @@ export function mountPlatformSurface(app, deps) {
986
997
  // K-74, A5 (#3471): the shadow comparison for one tenant of this app — what the drain delivered
987
998
  // against what the broker recorded, with latency — read from the broker the upload binds as
988
999
  // `SUBSTRAT_DELIVERY_BROKER`. Ids, types and times only. 404 with no broker bound; 501 when the
989
- // broker records nothing (one from before A5).
1000
+ // broker records nothing (one from before A5). #3639: `environment` names a named environment's
1001
+ // broker; absent, production's.
990
1002
  app.get(DELIVERY_COMPARISON_PATH, async (c) => {
991
1003
  const tenant = tenantIdOf.parse(c.req.query('tenantId'));
992
1004
  const cursor = c.req.query('cursor') || undefined;
1005
+ const named = c.req.query('environment');
1006
+ const environment = named ? environmentName.parse(named) : undefined;
993
1007
  const env = c.env;
994
1008
  const ns = env[PLATFORM_BROKER_BINDING];
995
1009
  if (!ns || typeof ns.idFromName !== 'function')
996
1010
  return refuseStatus(c, 404, 'this deployment runs no delivery broker (K-74)');
997
1011
  const app = typeof env.SUBSTRAT_VERTICAL === 'string' ? env.SUBSTRAT_VERTICAL : 'vertical';
998
- const broker = ns.get(ns.idFromName(brokerName(tenant, app)));
1012
+ const broker = ns.get(ns.idFromName(brokerName(tenant, app, environment)));
999
1013
  const out = await broker.deliveryComparison?.(cursor ? { cursor } : {});
1000
1014
  if (out == null)
1001
1015
  return refuseStatus(c, 501, "this deployment's delivery broker records nothing to compare (#3471). Redeploy it");
@@ -1110,6 +1124,15 @@ export function mountPlatformSurface(app, deps) {
1110
1124
  await deps.onDeleteScope?.(c.env, body.scopeId, body.tenantId);
1111
1125
  return c.json({ deleted: body.scopeId });
1112
1126
  });
1127
+ // K-63 (#3624): the master erased a person, and this app takes its people from it. The door removes
1128
+ // their rows in one scope and answers what it removed, for the erasure's receipt. Safe to repeat.
1129
+ app.post('/internal/people/erase', async (c) => {
1130
+ const body = personErasureRequest.parse(await c.req.json());
1131
+ if (!deps.erasePerson)
1132
+ return refuseStatus(c, 501, 'this vertical takes no people from another app');
1133
+ const removed = await deps.erasePerson(c.env, { tenantId: body.tenantId, scopeId: body.scopeId }, principalIdOf.parse(body.principal));
1134
+ return c.json(personErasure.parse(removed));
1135
+ });
1113
1136
  app.post('/internal/redact-subject', async (c) => {
1114
1137
  const body = z.object({ scopeId: scopeIdOf, subjectId: z.string().min(1) }).strict().parse(await c.req.json());
1115
1138
  const host = deps.hostFor(c.env);
@@ -1521,8 +1544,9 @@ export function mountPlatformSurface(app, deps) {
1521
1544
  // The broker is the deployment's own (`deliveryBroker`) or the platform's the upload supplies.
1522
1545
  // The deployment's broker for a tenant: the one `deliveryBroker` names, else the one the upload
1523
1546
  // supplies as `SUBSTRAT_DELIVERY_BROKER` (A5, #3471), reached as the comparison door reaches it.
1524
- const brokerFor = (env, tenantId) => {
1525
- const given = deps.deliveryBroker?.(env, tenantId);
1547
+ // #3639 (D-83): of the environment named, production's when none is.
1548
+ const brokerFor = (env, tenantId, environment) => {
1549
+ const given = environment === undefined ? deps.deliveryBroker?.(env, tenantId) : deps.deliveryBroker?.(env, tenantId, environment);
1526
1550
  if (given)
1527
1551
  return given;
1528
1552
  const e = env;
@@ -1530,11 +1554,11 @@ export function mountPlatformSurface(app, deps) {
1530
1554
  if (!ns || typeof ns.idFromName !== 'function')
1531
1555
  return null;
1532
1556
  const appName = typeof e.SUBSTRAT_VERTICAL === 'string' ? e.SUBSTRAT_VERTICAL : 'vertical';
1533
- return ns.get(ns.idFromName(brokerName(tenantId, appName)));
1557
+ return ns.get(ns.idFromName(brokerName(tenantId, appName, environment)));
1534
1558
  };
1535
1559
  app.post(BROKER_DEAD_LETTERS_PATH, async (c) => {
1536
1560
  const body = brokerDeadLettersRequest.parse(await c.req.json());
1537
- const broker = brokerFor(c.env, body.tenantId);
1561
+ const broker = brokerFor(c.env, body.tenantId, body.environment);
1538
1562
  if (!broker)
1539
1563
  return refuseStatus(c, 404, 'this deployment runs no delivery broker (K-74)');
1540
1564
  if (!broker.deadLetters)
@@ -1545,7 +1569,7 @@ export function mountPlatformSurface(app, deps) {
1545
1569
  content: body.content,
1546
1570
  });
1547
1571
  const entries = rows
1548
- // One broker per (tenant, app): every row is this tenant's, and one that is not is never shown.
1572
+ // One broker per (tenant, app, environment): every row is this tenant's, and one that is not is never shown.
1549
1573
  .filter((r) => r.tenantId === body.tenantId)
1550
1574
  .map((r) => ({
1551
1575
  messageId: r.messageId,
@@ -1570,7 +1594,7 @@ export function mountPlatformSurface(app, deps) {
1570
1594
  app.post(BROKER_DEAD_LETTER_DISCARD_PATH, async (c) => brokerDeadLetterMove(c, 'discard'));
1571
1595
  async function brokerDeadLetterMove(c, verb) {
1572
1596
  const body = brokerDeadLetterActionRequest.parse(await c.req.json());
1573
- const broker = brokerFor(c.env, body.tenantId);
1597
+ const broker = brokerFor(c.env, body.tenantId, body.environment);
1574
1598
  if (!broker)
1575
1599
  return refuseStatus(c, 404, 'this deployment runs no delivery broker (K-74)');
1576
1600
  const move = broker[verb];
@@ -1808,6 +1832,8 @@ export function mountPlatformSurface(app, deps) {
1808
1832
  switchedOffPeers: body.switchedOffPeers,
1809
1833
  tenantHeldPeers: body.tenantHeldPeers,
1810
1834
  switchFences: body.switchFences,
1835
+ ...(body.scopeType === undefined ? {} : { scopeType: body.scopeType }),
1836
+ ...(body.childOf === undefined ? {} : { parentScopeId: body.childOf }),
1811
1837
  });
1812
1838
  const hooked = await deps.onProvision?.(c.env, body);
1813
1839
  // #3202: a manager provisioned this tenant for one person's address, and only the hook can say
@@ -1828,6 +1854,11 @@ export function mountPlatformSurface(app, deps) {
1828
1854
  scopeId: body.scopeId,
1829
1855
  owner,
1830
1856
  ...(ownerAddressReserved ? { ownerAddressReserved: true } : {}),
1857
+ // #2993 part 3: the type the scope holds, answered when the platform named one, so a platform
1858
+ // can tell this provision fixed it from one by a host that ignored the field.
1859
+ ...(body.scopeType !== undefined && provisioned?.scopeType ? { scopeType: provisioned.scopeType } : {}),
1860
+ // #2995: and the parent it holds, for the same reason: a host that ignored the field fixed no link.
1861
+ ...(body.childOf !== undefined && provisioned?.parentScopeId ? { childOf: provisioned.parentScopeId } : {}),
1831
1862
  ...switchedOffAnswer(provisioned?.switchedOff),
1832
1863
  ...skippedConnectionGrantsAnswer(provisioned?.skippedConnectionGrants),
1833
1864
  }, 201);