@feltdb/feltkit 0.3.31 → 0.3.33

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 (118) hide show
  1. package/dist/app.d.ts +40 -2
  2. package/dist/app.js +22 -0
  3. package/dist/app.js.map +1 -1
  4. package/dist/broker/assertion.d.ts +68 -0
  5. package/dist/broker/assertion.js +104 -0
  6. package/dist/broker/assertion.js.map +1 -0
  7. package/dist/broker/keys.d.ts +9 -0
  8. package/dist/broker/keys.js +47 -0
  9. package/dist/broker/keys.js.map +1 -0
  10. package/dist/broker/server.d.ts +54 -0
  11. package/dist/broker/server.js +305 -0
  12. package/dist/broker/server.js.map +1 -0
  13. package/dist/cli/broker.d.ts +11 -0
  14. package/dist/cli/broker.js +72 -0
  15. package/dist/cli/broker.js.map +1 -0
  16. package/dist/cli/deploy.js +6 -0
  17. package/dist/cli/deploy.js.map +1 -1
  18. package/dist/cli/main.js +123 -0
  19. package/dist/cli/main.js.map +1 -1
  20. package/dist/client/index.d.ts +8 -2
  21. package/dist/client/index.js +45 -1
  22. package/dist/client/index.js.map +1 -1
  23. package/dist/docs.d.ts +1 -1
  24. package/dist/docs.js +2 -0
  25. package/dist/docs.js.map +1 -1
  26. package/dist/extension/build.d.ts +50 -0
  27. package/dist/extension/build.js +205 -0
  28. package/dist/extension/build.js.map +1 -0
  29. package/dist/extension/client-shim.d.ts +7 -0
  30. package/dist/extension/client-shim.js +13 -0
  31. package/dist/extension/client-shim.js.map +1 -0
  32. package/dist/extension/connect-ui.d.ts +24 -0
  33. package/dist/extension/connect-ui.js +67 -0
  34. package/dist/extension/connect-ui.js.map +1 -0
  35. package/dist/extension/connection.d.ts +109 -0
  36. package/dist/extension/connection.js +190 -0
  37. package/dist/extension/connection.js.map +1 -0
  38. package/dist/extension/host-core.d.ts +36 -0
  39. package/dist/extension/host-core.js +50 -0
  40. package/dist/extension/host-core.js.map +1 -0
  41. package/dist/extension/host.d.ts +1 -0
  42. package/dist/extension/host.js +8 -0
  43. package/dist/extension/host.js.map +1 -0
  44. package/dist/extension/icons.d.ts +13 -0
  45. package/dist/extension/icons.js +75 -0
  46. package/dist/extension/icons.js.map +1 -0
  47. package/dist/extension/identity.d.ts +40 -0
  48. package/dist/extension/identity.js +110 -0
  49. package/dist/extension/identity.js.map +1 -0
  50. package/dist/extension/options.d.ts +1 -0
  51. package/dist/extension/options.js +57 -0
  52. package/dist/extension/options.js.map +1 -0
  53. package/dist/extension/service-worker.d.ts +1 -0
  54. package/dist/extension/service-worker.js +38 -0
  55. package/dist/extension/service-worker.js.map +1 -0
  56. package/dist/extension/validate.d.ts +7 -0
  57. package/dist/extension/validate.js +143 -0
  58. package/dist/extension/validate.js.map +1 -0
  59. package/dist/extension/zip.d.ts +7 -0
  60. package/dist/extension/zip.js +94 -0
  61. package/dist/extension/zip.js.map +1 -0
  62. package/dist/index.d.ts +1 -0
  63. package/dist/index.js.map +1 -1
  64. package/dist/policy.d.ts +7 -0
  65. package/dist/policy.js +2 -2
  66. package/dist/policy.js.map +1 -1
  67. package/dist/runtime/activity.d.ts +2 -0
  68. package/dist/runtime/activity.js.map +1 -1
  69. package/dist/runtime/application-authority.d.ts +10 -10
  70. package/dist/runtime/application-authority.js +40 -14
  71. package/dist/runtime/application-authority.js.map +1 -1
  72. package/dist/runtime/authority-contract.d.ts +55 -0
  73. package/dist/runtime/authority-contract.js +60 -0
  74. package/dist/runtime/authority-contract.js.map +1 -0
  75. package/dist/runtime/authority.d.ts +6 -1
  76. package/dist/runtime/authority.js +2 -1
  77. package/dist/runtime/authority.js.map +1 -1
  78. package/dist/runtime/context.d.ts +24 -0
  79. package/dist/runtime/delegation.d.ts +47 -0
  80. package/dist/runtime/delegation.js +55 -0
  81. package/dist/runtime/delegation.js.map +1 -0
  82. package/dist/runtime/integrations.d.ts +2 -0
  83. package/dist/runtime/integrations.js +9 -4
  84. package/dist/runtime/integrations.js.map +1 -1
  85. package/dist/runtime/invocations.d.ts +3 -0
  86. package/dist/runtime/invocations.js +1 -1
  87. package/dist/runtime/invocations.js.map +1 -1
  88. package/dist/runtime/local-authority.d.ts +43 -10
  89. package/dist/runtime/local-authority.js +84 -21
  90. package/dist/runtime/local-authority.js.map +1 -1
  91. package/dist/runtime/readiness.js +31 -1
  92. package/dist/runtime/readiness.js.map +1 -1
  93. package/dist/runtime/runtime.d.ts +5 -0
  94. package/dist/runtime/runtime.js +99 -10
  95. package/dist/runtime/runtime.js.map +1 -1
  96. package/dist/server/index.js +24 -2
  97. package/dist/server/index.js.map +1 -1
  98. package/dist/server/oauth.d.ts +32 -2
  99. package/dist/server/oauth.js +130 -23
  100. package/dist/server/oauth.js.map +1 -1
  101. package/dist/server/project.d.ts +3 -0
  102. package/dist/server/project.js +2 -2
  103. package/dist/server/project.js.map +1 -1
  104. package/docs/CONNECTIONS.md +10 -3
  105. package/docs/EXTENSION-HOSTS.md +64 -5
  106. package/docs/GUARANTEES.md +4 -2
  107. package/docs/LOCAL-AUTHORITY.md +155 -0
  108. package/docs/POLICIES.md +1 -1
  109. package/docs/README.md +4 -2
  110. package/docs/SECURITY.md +39 -0
  111. package/docs/SIGN-IN-BROKER.md +77 -0
  112. package/package.json +6 -6
  113. package/skills/.claude-plugin/marketplace.json +1 -1
  114. package/skills/.claude-plugin/plugin.json +1 -1
  115. package/skills/feltkit-agent/SKILL.md +1 -1
  116. package/skills/feltkit-build/SKILL.md +10 -0
  117. package/skills/feltkit-data/SKILL.md +13 -1
  118. package/skills/feltkit-services/SKILL.md +14 -1
@@ -43,7 +43,9 @@ import { claimed, isRecoverable, isSequenceConflict, outboxStore, performOutbox,
43
43
  import { OAuthCredentials } from "./oauth-credentials.js";
44
44
  import { INVOCATIONS_COLLECTION, InvocationStore } from "./invocations.js";
45
45
  import { ExecutionHost } from "./execution-host.js";
46
- import { LocalAuthorityHost } from "./local-authority.js";
46
+ import { LocalAuthorityHost, applicationCapabilities, authorityAgentId } from "./local-authority.js";
47
+ import { authorityPrincipalOf } from "./application-authority.js";
48
+ import { verifyDelegation } from "./delegation.js";
47
49
  import { credentialsFor, isCustodyError } from "./credentials.js";
48
50
  import { AgentHost } from "./agent.js";
49
51
  import { FeltDbIdempotencyStore } from "./idempotency.js";
@@ -140,6 +142,10 @@ export class FeltKitRuntime {
140
142
  /** The settings this runtime reads (RuntimeStartOptions.env). */
141
143
  #env = process.env;
142
144
  recentErrors = [];
145
+ /** The settings this runtime reads (where the authority's address resolves, for one). */
146
+ get settings() {
147
+ return this.#env;
148
+ }
143
149
  constructor(environment, instance, logger, env = process.env) {
144
150
  this.#env = env;
145
151
  this.environment = environment;
@@ -493,7 +499,7 @@ export class FeltKitRuntime {
493
499
  capabilities,
494
500
  // app.event(): AppPort events; observation is authorized by roles, then feltkit({ authorize }).
495
501
  events: eventDefinitions(app),
496
- authorizer: observationAuthorizer(app, authority.authorizer, () => new Reader(this.instance.handle("app"), new CollectionResolver(app.collections))),
502
+ authorizer: this.#delegatedAuthorizer(observationAuthorizer(app, authority.authorizer, () => new Reader(this.instance.handle("app"), new CollectionResolver(app.collections)))),
497
503
  db: this.instance.handle("appport"),
498
504
  sessions: this.sessions,
499
505
  // Durable replay protection for capabilities that declare idempotency: the claim is
@@ -507,7 +513,7 @@ export class FeltKitRuntime {
507
513
  // Every transport (HTTP, WebSocket, MCP) dispatches through these two methods.
508
514
  for (const method of ["handleRequest", "handleStreamRequest"]) {
509
515
  const original = appport[method].bind(appport);
510
- appport[method] = (request, options = {}) => this.#appportCaller.run({ principal: fromAppPortPrincipal(options?.principal ?? options?.session?.principal) }, () => original(request, options));
516
+ appport[method] = (request, options = {}) => this.#appportCaller.run(callerScope(options?.principal ?? options?.session?.principal), () => original(request, options));
511
517
  }
512
518
  this.#project = project;
513
519
  this.#authority = authority;
@@ -530,11 +536,12 @@ export class FeltKitRuntime {
530
536
  appName: this.app.name,
531
537
  env: this.#env,
532
538
  localUrl: () => this.localAuthority.url(),
539
+ localState: () => this.localAuthority.status.state,
533
540
  grants: this.localAuthority.grantsFor(principal, requestId),
534
541
  agents: this.localAuthority.agentsFor(principal, requestId),
535
542
  agentFor: (requested) => this.localAuthority.agentDecision(principal, requested),
536
- record: async ({ capability, executor, onBehalfOf, decision, durationMs }) => {
537
- await this.invocations.noteDecision(requestId, { capability, onBehalfOf, outcome: decision.outcome, decisionId: decision.decisionId, delegationId: decision.delegationId, executor: decision.executorProof });
543
+ record: async ({ capability, executor, onBehalfOf, decision, durationMs, authorityMode }) => {
544
+ await this.invocations.noteDecision(requestId, { capability, onBehalfOf, outcome: decision.outcome, decisionId: decision.decisionId, delegationId: decision.delegationId, executor: decision.executorProof, authorityMode });
538
545
  this.activity.record({
539
546
  kind: "auth",
540
547
  name: "authority.decide",
@@ -543,7 +550,7 @@ export class FeltKitRuntime {
543
550
  principal: principal ? { id: principal.id, type: principal.type, roles: principal.roles } : null,
544
551
  outcome: decision.allowed ? "success" : decision.outcome === "deny" ? "denied" : "error",
545
552
  durationMs,
546
- detail: { capability, executor, onBehalfOf, outcome: decision.outcome, decisionId: decision.decisionId, delegationId: decision.delegationId, reason: decision.reason, executorProof: decision.executorProof },
553
+ detail: { capability, executor, onBehalfOf, outcome: decision.outcome, decisionId: decision.decisionId, delegationId: decision.delegationId, reason: decision.reason, executorProof: decision.executorProof, authorityMode },
547
554
  });
548
555
  },
549
556
  });
@@ -646,6 +653,8 @@ export class FeltKitRuntime {
646
653
  integration: integrationDescriptor(integration, op),
647
654
  attributes: { feltkitIntegration: integration.name },
648
655
  handler: async (input, context) => {
656
+ if (this.#appportCaller.getStore()?.onBehalfOf)
657
+ throw appportErrors.forbidden("An agent invokes only published functions on behalf of a person.");
649
658
  const principal = fromAppPortPrincipal(context.principal ?? context.session?.principal);
650
659
  this.#dispatched.add(context.requestId);
651
660
  try {
@@ -668,6 +677,68 @@ export class FeltKitRuntime {
668
677
  }
669
678
  /** The caller of the AppPort request being dispatched (see #gate). */
670
679
  #appportCaller = new AsyncLocalStorage();
680
+ /**
681
+ * AppPort's authorizer, extended for an agent working for a person (runtime/delegation.ts). A request
682
+ * without a person is decided by `inner` unchanged. One with a person is decided here, before AppPort
683
+ * claims an idempotency key or runs anything: one AuthBoundry decision as the proven agent for exactly
684
+ * this capability on behalf of that person, then the person's own roles for the capability's
685
+ * permission. The verified delegation is kept in the request's scope for the handler.
686
+ */
687
+ #delegatedAuthorizer(inner) {
688
+ return {
689
+ name: inner.name,
690
+ authorize: async (request) => {
691
+ const scope = this.#appportCaller.getStore();
692
+ if (!scope?.onBehalfOf)
693
+ return inner.authorize(request);
694
+ const deny = (reason, code = "FORBIDDEN") => ({ allowed: false, code, reason });
695
+ const agent = scope.principal;
696
+ if (!agent || agent.type !== "agent")
697
+ return deny("Only an authenticated agent acts on behalf of a person.", "UNAUTHORIZED");
698
+ const capability = String(request.capability?.name ?? "");
699
+ // The protocol's own capabilities (manifest, ping) describe the application, not the person: the agent's own.
700
+ if (BUILTIN_CAPABILITY_NAMES.includes(capability))
701
+ return inner.authorize(request);
702
+ const fn = [...this.#project.functions.values()].find((f) => f.capability?.name === capability && f.capability.version === request.capability?.version);
703
+ if (!fn)
704
+ return deny(`${capability} is not a published function: an agent invokes only those on behalf of a person.`);
705
+ if (fn.approval === "required")
706
+ return deny(`${capability} requires a person's approval: an agent cannot run it on a person's behalf.`);
707
+ const config = this.app.options.authority;
708
+ if (!config || !applicationCapabilities(this).includes(capability))
709
+ return deny(`${capability} is not delegable: the application does not list it in authority.capabilities.`);
710
+ const person = await this.#delegator(scope.onBehalfOf);
711
+ if (!person)
712
+ return deny(`No person ${scope.onBehalfOf} in this application.`);
713
+ const requestId = String(request.requestId ?? `req_${randomUUID()}`);
714
+ const agentAuthority = authorityAgentId(agent.id);
715
+ const onBehalfOf = authorityPrincipalOf(config, person.id);
716
+ const decision = await this.decideAsExecutor(agent, requestId, { capability, onBehalfOf: { person: person.id } });
717
+ const verified = verifyDelegation(decision, { agent: agent.id, agentAuthority, person: person.id, onBehalfOf, capability });
718
+ if ("refused" in verified)
719
+ return deny(`No delegation from ${person.id} to ${agentAuthority} for ${capability}: ${verified.refused}`);
720
+ const own = await inner.authorize({ ...request, principal: toAppPortPrincipal(person), session: undefined });
721
+ if (!own.allowed)
722
+ return deny(`${person.id} does not hold ${capability} themselves, so no delegation can grant it.`);
723
+ scope.delegation = verified;
724
+ scope.person = person;
725
+ return {
726
+ allowed: true,
727
+ decision: { decisionId: verified.decisionId, authorizer: "authboundry", capability: request.capability, principalId: agent.id, evidenceId: verified.delegationId, ...(request.session?.id ? { sessionId: request.session.id } : {}) },
728
+ };
729
+ },
730
+ };
731
+ }
732
+ /** The person a delegation is for, as they are now: an account (not disabled), else their current identity. Never an agent or a service. */
733
+ async #delegator(id) {
734
+ if (this.accounts) {
735
+ const account = await this.accounts.get(id).catch(() => null);
736
+ if (account)
737
+ return account.disabled ? null : { id, type: "user", roles: [...account.roles], ...(account.name ? { displayName: account.name } : {}) };
738
+ }
739
+ const found = await this.resolvePrincipal(id, { devUsers: true });
740
+ return found?.type === "user" ? { id: found.id, type: "user", roles: [...found.roles], ...(found.displayName ? { displayName: found.displayName } : {}) } : null;
741
+ }
671
742
  /**
672
743
  * The approved agent action being executed (agent.ts #execute), in this process only: the
673
744
  * one way a non-person principal runs an `approval: "required"` mutation. Nothing a caller
@@ -701,6 +772,9 @@ export class FeltKitRuntime {
701
772
  const scope = this.#appportCaller.getStore();
702
773
  if (!scope || fn.access === "public")
703
774
  return;
775
+ // On behalf of a person: decided by the authorizer, from the delegation and the person's own roles.
776
+ if (scope.onBehalfOf)
777
+ return;
704
778
  if (!scope.principal)
705
779
  throw appportErrors.unauthorized("Authentication is required");
706
780
  const required = Authority.required(fn.access);
@@ -714,7 +788,11 @@ export class FeltKitRuntime {
714
788
  #capabilityFor(name, fn) {
715
789
  const exposure = fn.capability;
716
790
  return capabilityDefinition(name, fn, async (input, context) => {
717
- const principal = fromAppPortPrincipal(context.principal ?? context.session?.principal);
791
+ const scope = this.#appportCaller.getStore();
792
+ const delegation = scope?.onBehalfOf ? scope.delegation : undefined;
793
+ if (scope?.onBehalfOf && (!delegation || !scope.person || delegation.capability !== exposure.name))
794
+ throw appportErrors.forbidden("No verified delegation for this capability.");
795
+ const principal = delegation ? scope.person : fromAppPortPrincipal(context.principal ?? context.session?.principal);
718
796
  this.#dispatched.add(context.requestId);
719
797
  const transport = context.metadata?.transport === "mcp" ? "mcp" : context.metadata?.transport === "agent" ? "agent" : "appport";
720
798
  const result = await this.call(name, input, {
@@ -724,6 +802,7 @@ export class FeltKitRuntime {
724
802
  capability: { name: exposure.name, version: exposure.version },
725
803
  ...(context.authorizationDecision?.decisionId ? { appportDecisionId: context.authorizationDecision.decisionId } : {}),
726
804
  ...(fn.kind === "mutation" && context.idempotencyKey ? { idempotencyReplayKey: replayKey(context, exposure, principal) } : {}),
805
+ ...(delegation ? { delegation } : {}),
727
806
  });
728
807
  if (result.ok)
729
808
  return result.value;
@@ -1422,7 +1501,11 @@ export class FeltKitRuntime {
1422
1501
  policyVersion: decision.policyVersion,
1423
1502
  ...(decision.reason ? { reason: decision.reason } : {}),
1424
1503
  ...(options.appportDecisionId ? { appportDecisionId: options.appportDecisionId } : {}),
1504
+ ...(options.delegation ? { delegation: { ...options.delegation } } : {}),
1425
1505
  };
1506
+ if (options.delegation && (options.delegation.person !== principal?.id || fn.approval === "required")) {
1507
+ return failure(new FeltKitError("FORBIDDEN", "A delegated call runs only as the verified person, and never an approval-required function."), { authority: { ...authorityRecord, allowed: false } });
1508
+ }
1426
1509
  if (!decision.allowed) {
1427
1510
  // Durable evidence for a signed-in caller's refusal (an anonymous caller refused before sign-in is in the activity log only).
1428
1511
  if (principal)
@@ -1459,7 +1542,7 @@ export class FeltKitRuntime {
1459
1542
  const db = fn.kind === "mutation" ? new Writer(handle, this.#resolver) : new Reader(handle, this.#resolver);
1460
1543
  const used = new Set();
1461
1544
  const shared = {
1462
- auth: authContext(this.#authority, principal, options.context),
1545
+ auth: authContext(this.#authority, principal, options.context, options.delegation ?? null),
1463
1546
  tenancy: {
1464
1547
  members: async () => {
1465
1548
  if (!this.tenancy)
@@ -1671,7 +1754,7 @@ export class FeltKitRuntime {
1671
1754
  };
1672
1755
  const agent = this.#agent;
1673
1756
  const ctx = {
1674
- auth: authContext(this.#authority, principal, options.context),
1757
+ auth: authContext(this.#authority, principal, options.context, options.delegation ?? null),
1675
1758
  environment: { name: this.environment.name, production: this.environment.production, storage: describeStorage(this.environment.storage) },
1676
1759
  requestId,
1677
1760
  log: this.logger,
@@ -1881,7 +1964,7 @@ export class FeltKitRuntime {
1881
1964
  // A bare id in a record or argument names a person (AuthBoundry's "user:<id>").
1882
1965
  const person = onBehalfOf.includes(":") ? onBehalfOf : `user:${onBehalfOf}`;
1883
1966
  const decision = await this.#applicationAuthority(principal, requestId).decide({ capability, onBehalfOf: person });
1884
- return { allowed: decision.allowed, decisionId: decision.decisionId, delegationId: decision.delegationId, ...(decision.reason ? { reason: decision.reason } : {}) };
1967
+ return { allowed: decision.allowed, decisionId: decision.decisionId, delegationId: decision.delegationId, authorityMode: decision.authorityMode, ...(decision.reason ? { reason: decision.reason } : {}) };
1885
1968
  },
1886
1969
  };
1887
1970
  }
@@ -2129,6 +2212,12 @@ function toAppPortError(error) {
2129
2212
  }
2130
2213
  }
2131
2214
  /** AppPort's replay key, computed exactly as AppPort scopes it (session, else principal). */
2215
+ const BUILTIN_CAPABILITY_NAMES = ["appport.manifest", "appport.ping"];
2216
+ function callerScope(appportPrincipal) {
2217
+ const principal = fromAppPortPrincipal(appportPrincipal);
2218
+ const named = appportPrincipal?.attributes?.feltkitOnBehalfOf;
2219
+ return { principal, onBehalfOf: typeof named === "string" ? named : null };
2220
+ }
2132
2221
  function replayKey(context, exposure, principal) {
2133
2222
  const scope = context.session?.id ?? (principal ? `principal:${principal.id}` : undefined);
2134
2223
  return idempotencyStorageKey({ key: String(context.idempotencyKey), capability: exposure.name, version: exposure.version, ...(scope ? { sessionId: scope } : {}) });