@contractspec/lib.contracts 1.44.0 → 1.45.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/README.md +1 -1
  2. package/dist/app-config/app-config.feature.js +6 -6
  3. package/dist/app-config/contracts.d.ts +50 -50
  4. package/dist/app-config/contracts.js +13 -13
  5. package/dist/app-config/events.d.ts +27 -27
  6. package/dist/app-config/events.js +12 -12
  7. package/dist/app-config/lifecycle-contracts.d.ts +54 -54
  8. package/dist/app-config/lifecycle-contracts.js +19 -19
  9. package/dist/app-config/lifecycle.d.ts +2 -2
  10. package/dist/app-config/runtime.d.ts +3 -3
  11. package/dist/app-config/spec.d.ts +10 -12
  12. package/dist/app-config/spec.js +5 -26
  13. package/dist/capabilities/capabilities.d.ts +4 -11
  14. package/dist/capabilities/capabilities.js +3 -5
  15. package/dist/capabilities/openbanking.js +10 -10
  16. package/dist/client/react/form-render.js +1 -0
  17. package/dist/contract-registry/schemas.d.ts +8 -8
  18. package/dist/contract-registry/schemas.js +1 -1
  19. package/dist/contract-registry/types.d.ts +1 -1
  20. package/dist/data-views/data-views.js +1 -0
  21. package/dist/data-views/registry.d.ts +3 -37
  22. package/dist/data-views/registry.js +4 -66
  23. package/dist/data-views/runtime.d.ts +1 -1
  24. package/dist/data-views/runtime.js +2 -0
  25. package/dist/data-views/spec.d.ts +2 -2
  26. package/dist/docs/index.d.ts +2 -2
  27. package/dist/docs/index.js +2 -2
  28. package/dist/docs/presentations.d.ts +2 -4
  29. package/dist/docs/presentations.js +2 -4
  30. package/dist/docs/tech/contracts/ops-to-presentation-linking.docblock.js +1 -1
  31. package/dist/docs/tech/contracts/tests.docblock.js +1 -1
  32. package/dist/docs/tech/lifecycle-stage-system.docblock.js +1 -1
  33. package/dist/docs/tech-contracts.docs.js +1 -1
  34. package/dist/docs/types.d.ts +1 -1
  35. package/dist/events.d.ts +3 -3
  36. package/dist/examples/docs/examples.docblock.d.ts +6 -0
  37. package/dist/examples/docs/examples.docblock.js +165 -0
  38. package/dist/examples/index.d.ts +5 -0
  39. package/dist/examples/index.js +6 -0
  40. package/dist/examples/registry.d.ts +65 -0
  41. package/dist/examples/registry.js +144 -0
  42. package/dist/examples/schema.d.ts +282 -0
  43. package/dist/examples/schema.js +125 -0
  44. package/dist/examples/types.d.ts +167 -0
  45. package/dist/examples/types.js +43 -0
  46. package/dist/examples/validation.d.ts +65 -0
  47. package/dist/examples/validation.js +144 -0
  48. package/dist/experiments/docs/experiments.docblock.js +1 -1
  49. package/dist/experiments/evaluator.d.ts +1 -1
  50. package/dist/experiments/spec-resolver.d.ts +2 -2
  51. package/dist/experiments/spec.d.ts +6 -8
  52. package/dist/experiments/spec.js +5 -23
  53. package/dist/features/index.d.ts +9 -1
  54. package/dist/features/index.js +8 -1
  55. package/dist/features/install.js +2 -2
  56. package/dist/features/types.d.ts +7 -7
  57. package/dist/forms/forms.d.ts +2 -2
  58. package/dist/forms/forms.js +3 -6
  59. package/dist/index.d.ts +13 -8
  60. package/dist/index.js +11 -2
  61. package/dist/install.d.ts +9 -9
  62. package/dist/integrations/connection.d.ts +1 -1
  63. package/dist/integrations/docs/integrations.docblock.js +1 -1
  64. package/dist/integrations/integrations.feature.js +8 -8
  65. package/dist/integrations/openbanking/contracts/accounts.d.ts +66 -66
  66. package/dist/integrations/openbanking/contracts/accounts.js +3 -3
  67. package/dist/integrations/openbanking/contracts/balances.d.ts +34 -34
  68. package/dist/integrations/openbanking/contracts/balances.js +2 -2
  69. package/dist/integrations/openbanking/contracts/transactions.d.ts +48 -48
  70. package/dist/integrations/openbanking/contracts/transactions.js +2 -2
  71. package/dist/integrations/openbanking/models.d.ts +55 -55
  72. package/dist/integrations/openbanking/openbanking.feature.js +10 -10
  73. package/dist/integrations/operations.d.ts +102 -102
  74. package/dist/integrations/operations.js +10 -10
  75. package/dist/integrations/providers/elevenlabs.js +2 -2
  76. package/dist/integrations/providers/gcs-storage.js +2 -2
  77. package/dist/integrations/providers/gmail.js +3 -3
  78. package/dist/integrations/providers/google-calendar.js +2 -2
  79. package/dist/integrations/providers/mistral.js +3 -3
  80. package/dist/integrations/providers/postmark.js +2 -2
  81. package/dist/integrations/providers/powens.js +4 -4
  82. package/dist/integrations/providers/qdrant.js +3 -3
  83. package/dist/integrations/providers/stripe.js +2 -2
  84. package/dist/integrations/providers/twilio-sms.js +2 -2
  85. package/dist/integrations/runtime.d.ts +5 -5
  86. package/dist/integrations/spec.d.ts +3 -5
  87. package/dist/integrations/spec.js +5 -26
  88. package/dist/jobs/gcp-cloud-tasks.js +1 -1
  89. package/dist/jobs/gcp-pubsub.js +1 -1
  90. package/dist/jobs/handlers/ping-handler.d.ts +3 -3
  91. package/dist/jobs/handlers/ping-handler.js +2 -2
  92. package/dist/jobs/memory-queue.js +1 -1
  93. package/dist/jobs/queue.d.ts +4 -4
  94. package/dist/jobs/scaleway-sqs-queue.js +2 -2
  95. package/dist/jsonschema.d.ts +4 -4
  96. package/dist/knowledge/binding.d.ts +1 -1
  97. package/dist/knowledge/knowledge.feature.js +8 -8
  98. package/dist/knowledge/operations.d.ts +66 -66
  99. package/dist/knowledge/operations.js +12 -12
  100. package/dist/knowledge/source.d.ts +1 -1
  101. package/dist/knowledge/spaces/email-threads.js +2 -2
  102. package/dist/knowledge/spaces/financial-docs.js +2 -2
  103. package/dist/knowledge/spaces/financial-overview.js +2 -2
  104. package/dist/knowledge/spaces/product-canon.js +2 -2
  105. package/dist/knowledge/spaces/support-faq.js +2 -2
  106. package/dist/knowledge/spaces/uploaded-docs.js +2 -2
  107. package/dist/knowledge/spec.d.ts +3 -5
  108. package/dist/knowledge/spec.js +6 -28
  109. package/dist/llm/exporters.js +2 -2
  110. package/dist/llm/types.d.ts +4 -4
  111. package/dist/markdown.js +1 -1
  112. package/dist/migrations.d.ts +2 -2
  113. package/dist/migrations.js +7 -9
  114. package/dist/onboarding-base.d.ts +29 -29
  115. package/dist/onboarding-base.js +4 -4
  116. package/dist/openapi.js +9 -2
  117. package/dist/operations/operation.d.ts +3 -3
  118. package/dist/operations/registry.d.ts +9 -58
  119. package/dist/operations/registry.js +17 -93
  120. package/dist/ownership.d.ts +1 -1
  121. package/dist/policy/docs/policy.docblock.js +1 -1
  122. package/dist/policy/engine.js +2 -0
  123. package/dist/policy/spec.d.ts +1 -1
  124. package/dist/prompt.d.ts +6 -6
  125. package/dist/promptRegistry.d.ts +4 -4
  126. package/dist/promptRegistry.js +2 -5
  127. package/dist/regenerator/docs/regenerator.docblock.js +1 -1
  128. package/dist/regenerator/types.d.ts +1 -1
  129. package/dist/registry.d.ts +3 -2
  130. package/dist/registry.js +5 -5
  131. package/dist/resources.d.ts +5 -5
  132. package/dist/server/graphql-pothos.js +2 -2
  133. package/dist/server/mcp/registerTools.js +1 -1
  134. package/dist/server/rest-elysia.d.ts +1 -1
  135. package/dist/server/rest-elysia.js +1 -1
  136. package/dist/server/rest-express.d.ts +1 -1
  137. package/dist/server/rest-express.js +1 -1
  138. package/dist/server/rest-generic.d.ts +1 -1
  139. package/dist/server/rest-generic.js +1 -1
  140. package/dist/server/rest-next-app.d.ts +1 -1
  141. package/dist/server/rest-next-mcp.d.ts +1 -1
  142. package/dist/server/rest-next-mcp.js +1 -1
  143. package/dist/server/rest-next-pages.d.ts +1 -1
  144. package/dist/telemetry/docs/telemetry.docblock.js +1 -1
  145. package/dist/telemetry/spec.d.ts +7 -7
  146. package/dist/telemetry/spec.js +17 -44
  147. package/dist/telemetry/tracker.d.ts +2 -2
  148. package/dist/tests/runner.d.ts +1 -1
  149. package/dist/tests/spec.d.ts +5 -5
  150. package/dist/tests/spec.js +3 -5
  151. package/dist/themes.d.ts +4 -6
  152. package/dist/themes.js +5 -27
  153. package/dist/translations/catalog.d.ts +1 -1
  154. package/dist/types.d.ts +5 -5
  155. package/dist/workflow/adapters/db-adapter.js +3 -3
  156. package/dist/workflow/runner.d.ts +4 -2
  157. package/dist/workflow/spec.d.ts +4 -17
  158. package/dist/workflow/spec.js +4 -48
  159. package/dist/workflow/state.d.ts +1 -1
  160. package/dist/workflow/validation.js +1 -1
  161. package/dist/workspace-config/contractsrc-schema.d.ts +419 -419
  162. package/dist/workspace-config/contractsrc-schema.js +86 -86
  163. package/package.json +28 -12
@@ -13,7 +13,7 @@ const tech_contracts_policy_DocBlocks = [{
13
13
  "contracts",
14
14
  "policy"
15
15
  ],
16
- body: "# PolicySpec & PolicyEngine\n\n## Purpose\n\n`PolicySpec` gives a declarative, typed home for access-control logic covering:\n- **Who** can perform an action (ABAC/ReBAC style rules)\n- **What** they can access (resources + optional field-level overrides)\n- **When** special conditions apply (contextual expressions)\n- **How** PII should be handled (consent/retention hints)\n\n`PolicyEngine` evaluates one or more policies and returns an `allow`/`deny` decision, field-level outcomes, and PII metadata suitable for downstream enforcement (`OperationSpecRegistry` → `ctx.decide`).\n\n## Location\n\n- Types & registry: `packages/libs/contracts/src/policy/spec.ts`\n- Runtime evaluation: `packages/libs/contracts/src/policy/engine.ts`\n- Tests: `packages/.../policy/engine.test.ts`\n\n## `PolicySpec`\n\n```ts\nexport interface PolicySpec {\n meta: PolicyMeta; // ownership metadata + { name, version, scope? }\n rules: PolicyRule[]; // allow/deny rules for actions\n fieldPolicies?: FieldPolicyRule[];\n pii?: { fields: string[]; consentRequired?: boolean; retentionDays?: number };\n relationships?: RelationshipDefinition[];\n consents?: ConsentDefinition[];\n rateLimits?: RateLimitDefinition[];\n opa?: { package: string; decision?: string };\n}\n```\n\n- `PolicyRule`\n - `effect`: `'allow' | 'deny'`\n - `actions`: e.g., `['read', 'write', 'delete']` (string namespace is flexible)\n - `subject`: `{ roles?: string[]; attributes?: { attr: matcher } }`\n - `resource`: `{ type: string; fields?: string[]; attributes?: {...} }`\n - `relationships`: `{ relation, objectId?, objectType? }[]` → ReBAC checks (use `objectId: '$resource'` to target the current resource)\n - `requiresConsent`: `['consent_id']` → references spec-level consent definitions\n - `flags`: feature flags that must be enabled (`DecisionContext.flags`)\n - `rateLimit`: string reference to `rateLimits` entry or inline object `{ rpm, key?, windowSeconds?, burst? }`\n - `escalate`: `'human_review' | null` to indicate manual approval\n - `conditions`: optional expression snippets evaluated against `{ subject, resource, context }`\n- `FieldPolicyRule`\n - `field`: dot-path string (e.g., `contact.email`)\n - `actions`: subset of `['read', 'write']`\n - Same `subject` / `resource` / `conditions` shape\n - Useful for redacting specific fields, even when the global action is allowed\n- `RelationshipDefinition`\n - Canonical tuples for relationship graph (`subjectType`, `relation`, `objectType`, `transitive?`)\n- `ConsentDefinition`\n - `{ id, scope, purpose, lawfulBasis?, expiresInDays?, required? }`\n- `RateLimitDefinition`\n - `{ id, rpm, key?, windowSeconds?, burst? }`\n- `PolicyRef`\n - `{ name: string; version: number }` → attach to contract specs / workflows\n\n## Registry\n\n```ts\nconst registry = new PolicyRegistry();\nregistry.register(CorePolicySpec);\nconst spec = registry.get('core.default', 1);\n```\n\nGuarantees uniqueness per `(name, version)` and exposes helpers to resolve highest versions.\n\n## Engine\n\n```ts\nconst engine = new PolicyEngine(policyRegistry);\n\nconst decision = engine.decide({\n action: 'read',\n subject: { roles: ['admin'] },\n resource: { type: 'resident', fields: ['contact.email'] },\n policies: [{ name: 'core.default', version: 1 }],\n});\n/*\n{\n effect: 'allow',\n reason: 'core.default',\n fieldDecisions: [{ field: 'contact.email', effect: 'allow' }],\n pii: { fields: ['contact.email'], consentRequired: true }\n}\n*/\n```\n\n- First matching **deny** wins; otherwise the first **allow** is returned.\n- Field policies are aggregated across referenced policies:\n - Later denies override earlier allows for a given field.\n - Returned as `fieldDecisions` to simplify downstream masking.\n- PII metadata is surfaced when defined to help adapt logging/telemetry.\n\n### Expression Support\n\nConditions accept small JS snippets (e.g., `subject.attributes.orgId === context.orgId`). The engine runs them in a constrained scope (`subject`, `resource`, `context`) without access to global state.\n\n### ReBAC & Relationships\n\n- Provide relationship tuples via `PolicySpec.relationships` for documentation/validation.\n- Reference them inside rules with `relationships: [{ relation: 'manager_of', objectType: 'resident', objectId: '$resource' }]`.\n- The execution context must populate `subject.relationships` (`[{ relation, object, objectType }]`) for the engine to evaluate ReBAC guards.\n\n### Consent & Rate Limits\n\n- Declare reusable consent definitions under `consents`. Rules list the IDs they require; if a user session lacks the consent (`DecisionContext.consents`), the engine returns `effect: 'deny'` with `reason: 'consent_required'` and enumerates missing consents.\n- Attach rate limits either inline or via `rateLimits` references. When a rule matches, the engine surfaces `{ rpm, key, windowSeconds?, burst? }` so callers can feed it to shared limiters.\n\n### OPA Adapter\n\n- `OPAPolicyAdapter` bridges engine decisions to Open Policy Agent (OPA). It forwards the evaluation context + policies to OPA and merges any override result (`effect`, `reason`, `fieldDecisions`, `requiredConsents`).\n- Use when migrating to OPA policies or running defense-in-depth: call `engine.decide()`, then pass the preliminary decision to `adapter.evaluate(...)`. The adapter marks merged decisions with `evaluatedBy: 'opa'`.\n- OPA inputs include meta, rules, relationships, rate limits, and consent catalogs to simplify policy authoring on the OPA side.\n\n## Contract Integration\n\n`ContractSpec.policy` now supports:\n\n```ts\npolicy: {\n auth: 'anonymous' | 'user' | 'admin';\n ...\n policies?: PolicyRef[]; // policies evaluated before execution\n fieldPolicies?: { // field hints (read/write) per policy\n field: string;\n actions: ('read' | 'write')[];\n policy?: PolicyRef;\n }[];\n}\n```\n\nAdapters can resolve refs through a shared `PolicyEngine` and populate `ctx.decide` so `OperationSpecRegistry.execute` benefits from centralized enforcement.\n\n## Authoring Guidelines\n\n1. Prefer **allow-by-default** policies but explicitly deny sensitive flows (defense-in-depth).\n2. Keep rule scopes narrow (per feature/operation) and compose multiple `PolicyRef`s when necessary.\n3. Store PII field lists here to avoid duplication across logs/telemetry.\n4. Use explicit rule reasons for auditability and better developer feedback.\n5. Treat versioning seriously; bump `meta.version` whenever behavior changes.\n\n## Future Enhancements\n\n- Richer expression language (composable predicates, time-based conditions).\n- Multi-tenant relationship graph services (store/resolve relationships at scale).\n- Tooling that auto-generates docs/tests for policies referenced in specs.\n\n"
16
+ body: "# PolicySpec & PolicyEngine\n\n## Purpose\n\n`PolicySpec` gives a declarative, typed home for access-control logic covering:\n- **Who** can perform an action (ABAC/ReBAC style rules)\n- **What** they can access (resources + optional field-level overrides)\n- **When** special conditions apply (contextual expressions)\n- **How** PII should be handled (consent/retention hints)\n\n`PolicyEngine` evaluates one or more policies and returns an `allow`/`deny` decision, field-level outcomes, and PII metadata suitable for downstream enforcement (`OperationSpecRegistry` → `ctx.decide`).\n\n## Location\n\n- Types & registry: `packages/libs/contracts/src/policy/spec.ts`\n- Runtime evaluation: `packages/libs/contracts/src/policy/engine.ts`\n- Tests: `packages/.../policy/engine.test.ts`\n\n## `PolicySpec`\n\n```ts\nexport interface PolicySpec {\n meta: PolicyMeta; // ownership metadata + { name, version, scope? }\n rules: PolicyRule[]; // allow/deny rules for actions\n fieldPolicies?: FieldPolicyRule[];\n pii?: { fields: string[]; consentRequired?: boolean; retentionDays?: number };\n relationships?: RelationshipDefinition[];\n consents?: ConsentDefinition[];\n rateLimits?: RateLimitDefinition[];\n opa?: { package: string; decision?: string };\n}\n```\n\n- `PolicyRule`\n - `effect`: `'allow' | 'deny'`\n - `actions`: e.g., `['read', 'write', 'delete']` (string namespace is flexible)\n - `subject`: `{ roles?: string[]; attributes?: { attr: matcher } }`\n - `resource`: `{ type: string; fields?: string[]; attributes?: {...} }`\n - `relationships`: `{ relation, objectId?, objectType? }[]` → ReBAC checks (use `objectId: '$resource'` to target the current resource)\n - `requiresConsent`: `['consent_id']` → references spec-level consent definitions\n - `flags`: feature flags that must be enabled (`DecisionContext.flags`)\n - `rateLimit`: string reference to `rateLimits` entry or inline object `{ rpm, key?, windowSeconds?, burst? }`\n - `escalate`: `'human_review' | null` to indicate manual approval\n - `conditions`: optional expression snippets evaluated against `{ subject, resource, context }`\n- `FieldPolicyRule`\n - `field`: dot-path string (e.g., `contact.email`)\n - `actions`: subset of `['read', 'write']`\n - Same `subject` / `resource` / `conditions` shape\n - Useful for redacting specific fields, even when the global action is allowed\n- `RelationshipDefinition`\n - Canonical tuples for relationship graph (`subjectType`, `relation`, `objectType`, `transitive?`)\n- `ConsentDefinition`\n - `{ id, scope, purpose, lawfulBasis?, expiresInDays?, required? }`\n- `RateLimitDefinition`\n - `{ id, rpm, key?, windowSeconds?, burst? }`\n- `PolicyRef`\n - `{ name: string; version: string }` → attach to contract specs / workflows\n\n## Registry\n\n```ts\nconst registry = new PolicyRegistry();\nregistry.register(CorePolicySpec);\nconst spec = registry.get('core.default', 1);\n```\n\nGuarantees uniqueness per `(name, version)` and exposes helpers to resolve highest versions.\n\n## Engine\n\n```ts\nconst engine = new PolicyEngine(policyRegistry);\n\nconst decision = engine.decide({\n action: 'read',\n subject: { roles: ['admin'] },\n resource: { type: 'resident', fields: ['contact.email'] },\n policies: [{ name: 'core.default', version: '1.0.0' }],\n});\n/*\n{\n effect: 'allow',\n reason: 'core.default',\n fieldDecisions: [{ field: 'contact.email', effect: 'allow' }],\n pii: { fields: ['contact.email'], consentRequired: true }\n}\n*/\n```\n\n- First matching **deny** wins; otherwise the first **allow** is returned.\n- Field policies are aggregated across referenced policies:\n - Later denies override earlier allows for a given field.\n - Returned as `fieldDecisions` to simplify downstream masking.\n- PII metadata is surfaced when defined to help adapt logging/telemetry.\n\n### Expression Support\n\nConditions accept small JS snippets (e.g., `subject.attributes.orgId === context.orgId`). The engine runs them in a constrained scope (`subject`, `resource`, `context`) without access to global state.\n\n### ReBAC & Relationships\n\n- Provide relationship tuples via `PolicySpec.relationships` for documentation/validation.\n- Reference them inside rules with `relationships: [{ relation: 'manager_of', objectType: 'resident', objectId: '$resource' }]`.\n- The execution context must populate `subject.relationships` (`[{ relation, object, objectType }]`) for the engine to evaluate ReBAC guards.\n\n### Consent & Rate Limits\n\n- Declare reusable consent definitions under `consents`. Rules list the IDs they require; if a user session lacks the consent (`DecisionContext.consents`), the engine returns `effect: 'deny'` with `reason: 'consent_required'` and enumerates missing consents.\n- Attach rate limits either inline or via `rateLimits` references. When a rule matches, the engine surfaces `{ rpm, key, windowSeconds?, burst? }` so callers can feed it to shared limiters.\n\n### OPA Adapter\n\n- `OPAPolicyAdapter` bridges engine decisions to Open Policy Agent (OPA). It forwards the evaluation context + policies to OPA and merges any override result (`effect`, `reason`, `fieldDecisions`, `requiredConsents`).\n- Use when migrating to OPA policies or running defense-in-depth: call `engine.decide()`, then pass the preliminary decision to `adapter.evaluate(...)`. The adapter marks merged decisions with `evaluatedBy: 'opa'`.\n- OPA inputs include meta, rules, relationships, rate limits, and consent catalogs to simplify policy authoring on the OPA side.\n\n## Contract Integration\n\n`ContractSpec.policy` now supports:\n\n```ts\npolicy: {\n auth: 'anonymous' | 'user' | 'admin';\n ...\n policies?: PolicyRef[]; // policies evaluated before execution\n fieldPolicies?: { // field hints (read/write) per policy\n field: string;\n actions: ('read' | 'write')[];\n policy?: PolicyRef;\n }[];\n}\n```\n\nAdapters can resolve refs through a shared `PolicyEngine` and populate `ctx.decide` so `OperationSpecRegistry.execute` benefits from centralized enforcement.\n\n## Authoring Guidelines\n\n1. Prefer **allow-by-default** policies but explicitly deny sensitive flows (defense-in-depth).\n2. Keep rule scopes narrow (per feature/operation) and compose multiple `PolicyRef`s when necessary.\n3. Store PII field lists here to avoid duplication across logs/telemetry.\n4. Use explicit rule reasons for auditability and better developer feedback.\n5. Treat versioning seriously; bump `meta.version` whenever behavior changes.\n\n## Future Enhancements\n\n- Richer expression language (composable predicates, time-based conditions).\n- Multi-tenant relationship graph services (store/resolve relationships at scale).\n- Tooling that auto-generates docs/tests for policies referenced in specs.\n\n"
17
17
  }];
18
18
  registerDocBlocks(tech_contracts_policy_DocBlocks);
19
19
 
@@ -1,3 +1,5 @@
1
+ import "./registry.js";
2
+
1
3
  //#region src/policy/engine.ts
2
4
  var PolicyEngine = class {
3
5
  constructor(registry) {
@@ -97,7 +97,7 @@ interface PolicySpec {
97
97
  }
98
98
  interface PolicyRef {
99
99
  key: string;
100
- version: number;
100
+ version: string;
101
101
  }
102
102
  //#endregion
103
103
  export { AttributeMatcher, ConsentDefinition, FieldPolicyRule, PIIPolicy, PolicyCondition, PolicyEffect, PolicyMeta, PolicyOPAConfig, PolicyRef, PolicyRule, PolicySpec, RateLimitDefinition, RelationshipDefinition, RelationshipMatcher, ResourceMatcher, SubjectMatcher };
package/dist/prompt.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as z$1 from "zod";
1
+ import * as z$2 from "zod";
2
2
 
3
3
  //#region src/prompt.d.ts
4
4
  type PromptStability = 'experimental' | 'beta' | 'stable' | 'deprecated';
@@ -7,7 +7,7 @@ interface PromptArg {
7
7
  name: string;
8
8
  description?: string;
9
9
  required?: boolean;
10
- schema: z$1.ZodType;
10
+ schema: z$2.ZodType;
11
11
  completeWith?: string;
12
12
  }
13
13
  /**
@@ -24,7 +24,7 @@ type PromptContentPart = {
24
24
  /** Prompt metadata for discoverability and governance. */
25
25
  interface PromptMeta {
26
26
  key: string;
27
- version: number;
27
+ version: string;
28
28
  title: string;
29
29
  description: string;
30
30
  tags?: string[];
@@ -41,13 +41,13 @@ interface PromptPolicy {
41
41
  };
42
42
  }
43
43
  /** Full prompt specification including args schema and render function. */
44
- interface PromptSpec<I extends z$1.ZodType> {
44
+ interface PromptSpec<I extends z$2.ZodType> {
45
45
  meta: PromptMeta;
46
46
  args: PromptArg[];
47
47
  input: I;
48
48
  policy?: PromptPolicy;
49
49
  /** Render MCP-friendly content parts. DO NOT perform side effects here. */
50
- render: (args: z$1.infer<I>, ctx: {
50
+ render: (args: z$2.infer<I>, ctx: {
51
51
  userId?: string | null;
52
52
  orgId?: string | null;
53
53
  locale?: string;
@@ -55,6 +55,6 @@ interface PromptSpec<I extends z$1.ZodType> {
55
55
  }) => Promise<PromptContentPart[]>;
56
56
  }
57
57
  /** Identity helper that preserves generic inference when declaring prompts. */
58
- declare function definePrompt<I extends z$1.ZodType>(spec: PromptSpec<I>): PromptSpec<I>;
58
+ declare function definePrompt<I extends z$2.ZodType>(spec: PromptSpec<I>): PromptSpec<I>;
59
59
  //#endregion
60
60
  export { PromptArg, PromptContentPart, PromptMeta, PromptPolicy, PromptSpec, PromptStability, definePrompt };
@@ -1,15 +1,15 @@
1
1
  import { PromptSpec } from "./prompt.js";
2
- import * as z$1 from "zod";
2
+ import * as z$2 from "zod";
3
3
 
4
4
  //#region src/promptRegistry.d.ts
5
5
  declare class PromptRegistry {
6
6
  private prompts;
7
7
  /** Register a prompt. Throws on duplicate name+version. */
8
- register<I extends z$1.ZodType>(p: PromptSpec<I>): this;
8
+ register<I extends z$2.ZodType>(p: PromptSpec<I>): this;
9
9
  /** List all registered prompts. */
10
- list(): PromptSpec<z$1.ZodType<unknown, unknown, z$1.core.$ZodTypeInternals<unknown, unknown>>>[];
10
+ list(): PromptSpec<z$2.ZodType<unknown, unknown, z$2.core.$ZodTypeInternals<unknown, unknown>>>[];
11
11
  /** Get prompt by name; when version omitted, returns highest version. */
12
- get(name: string, version?: number): PromptSpec<z$1.ZodType<unknown, unknown, z$1.core.$ZodTypeInternals<unknown, unknown>>> | undefined;
12
+ get(name: string, version?: string): PromptSpec<z$2.ZodType<unknown, unknown, z$2.core.$ZodTypeInternals<unknown, unknown>>> | undefined;
13
13
  }
14
14
  //#endregion
15
15
  export { PromptRegistry };
@@ -1,3 +1,4 @@
1
+ import { compareVersions } from "compare-versions";
1
2
  import "zod";
2
3
 
3
4
  //#region src/promptRegistry.ts
@@ -18,13 +19,9 @@ var PromptRegistry = class {
18
19
  get(name, version) {
19
20
  if (version != null) return this.prompts.get(`${name}-v${version}`);
20
21
  let candidate;
21
- let max = -Infinity;
22
22
  for (const [k, p] of this.prompts.entries()) {
23
23
  if (!k.startsWith(`${name}.v`)) continue;
24
- if (p.meta.version > max) {
25
- max = p.meta.version;
26
- candidate = p;
27
- }
24
+ if (!candidate || compareVersions(p.meta.version, candidate.meta.version) > 0) candidate = p;
28
25
  }
29
26
  return candidate;
30
27
  }
@@ -13,7 +13,7 @@ const tech_contracts_regenerator_DocBlocks = [{
13
13
  "contracts",
14
14
  "regenerator"
15
15
  ],
16
- body: "## Regenerator Service\n\nThe Regenerator daemon observes telemetry, error, and behavior streams, then suggests spec-level changes (not code patches) that can be reviewed and applied through the App Studio.\n\n- Runtime entrypoint: `packages/libs/contracts/src/regenerator/service.ts`\n- Types/interfaces: `packages/libs/contracts/src/regenerator/types.ts`\n- Signal adapters: `packages/libs/contracts/src/regenerator/adapters.ts`\n\n### Architecture\n\n```text\nSignal Adapters ──► RegeneratorService ──► Rules ──► ProposalSink\n ▲ │\n │ ▼\n Telemetry / Errors / Behavior Spec change proposals\n```\n\n1. **Signal adapters** pull batches of telemetry, error logs, or behavior metrics for each `RegenerationContext`.\n2. `RegeneratorService` schedules polling (`resolveAppConfig` + `composeAppConfig` provide context).\n3. **Rules** implement domain heuristics and emit `SpecChangeProposal` objects.\n4. **Proposal sinks** persist or forward proposals for human review.\n\n### Key types\n\n```ts\nexport interface RegenerationContext {\n id: string;\n blueprint: AppBlueprintSpec;\n tenantConfig: TenantAppConfig;\n resolved: ResolvedAppConfig;\n}\n\nexport interface RegeneratorRule {\n id: string;\n description: string;\n evaluate(\n context: RegenerationContext,\n signals: RegeneratorSignal[]\n ): Promise<SpecChangeProposal[]>;\n}\n\nexport interface SpecChangeProposal {\n id: string;\n title: string;\n summary: string;\n confidence: 'low' | 'medium' | 'high';\n target: ProposalTarget;\n actions: ProposalAction[];\n blockers?: ProposalBlocker[];\n signalIds: string[];\n createdAt: Date;\n}\n```\n\n- Signals are normalized envelopes: telemetry (`count`, anomaly score), errors, and behavior trends.\n- Proposals reference blueprint or tenant specs via `ProposalTarget`.\n- Actions encode what the automation should perform (update blueprint, run tests/migrations, trigger regeneration).\n\n### Providing signals\n\nImplement `TelemetrySignalProvider`, `ErrorSignalProvider`, or `BehaviorSignalProvider`:\n\n```ts\nconst service = new RegeneratorService({\n contexts,\n adapters: {\n telemetry: new PosthogTelemetryAdapter(),\n errors: new SentryErrorAdapter(),\n },\n rules: [new WorkflowFailureRule(), new DataViewUsageRule()],\n sink: new ProposalQueueSink(),\n pollIntervalMs: 60_000,\n});\n```\n\nAdapters receive the full `RegenerationContext`, making it easy to scope queries per tenant/app.\n\n### Authoring rules\n\nRules focus on signals → proposals:\n\n```ts\nclass WorkflowFailureRule implements RegeneratorRule {\n id = 'workflow-failure';\n description = 'Suggest splitting workflows that exceed failure thresholds';\n\n async evaluate(context, signals) {\n const failures = signals.filter(\n (signal) =>\n signal.type === 'telemetry' &&\n signal.signal.eventName === 'workflow.failure' &&\n signal.signal.count >= 10\n );\n\n if (failures.length === 0) return [];\n\n return [\n {\n id: `${this.id}-${context.id}`,\n title: 'Split onboarding workflow',\n summary: 'Step 3 fails consistently; propose dedicated remediation branch.',\n confidence: 'medium',\n rationale: ['Failure count ≥ 10 within last window'],\n target: {\n specType: 'workflow',\n reference: { name: 'onboarding.workflow', version: 1 },\n tenantScoped: true,\n },\n actions: [\n { kind: 'update_tenant_config', summary: 'Add alternate fallback path' },\n { kind: 'run_tests', tests: ['workflows/onboarding.spec.ts'] },\n ],\n signalIds: failures.map((f) => f.signal.eventName),\n createdAt: new Date(),\n },\n ];\n }\n}\n```\n\n### Reviewing proposals\n\nProposals flow to a `ProposalSink` (queue, DB, messaging bus). The Studio will surface:\n\n1. Signal evidence (telemetry counts, error metadata)\n2. Proposed spec diffs and required actions (tests/migrations)\n3. Approval workflow (approve → write spec diff → run automation)\n\n### CLI driver\n\nRun the regenerator daemon from the CLI:\n\n```bash\nbunx contracts regenerator ./app.blueprint.ts ./tenant.config.ts ./regenerator.rules.ts auto \\\n --executor ./regenerator.executor.ts \\\n --poll-interval 60000 \\\n --batch-duration 300000 \\\n --dry-run\n```\n\n- Expects modules exporting default `AppBlueprintSpec`, `TenantAppConfig`, and one or more `RegenerationRule`s.\n- Pass a sink module path, or use the special `auto` value with `--executor <module>` to instantiate an `ExecutorProposalSink`.\n- Executor modules can export a `ProposalExecutor` instance, a factory, or a plain dependency object for the executor constructor. Optional exports: `sinkOptions`, `logger`, `onResult`, `dryRun`.\n- Optionally provide `--contexts ./contexts.ts` to load custom context arrays (advanced multi-tenant scenarios).\n- Use `--dry-run` to preview actions without mutating specs/configs, and `--once` for CI smoke tests.\n\n### Proposal executor\n\n`ProposalExecutor` + `ExecutorProposalSink` orchestrate follow-up actions once a proposal is approved:\n\n- Interfaces for applying blueprint or tenant-config updates (`BlueprintUpdater`, `TenantConfigUpdater`).\n- Hooks for running contract tests and migrations (`TestExecutor`, `MigrationExecutor`).\n- Optional trigger to recompose the runtime (`RegenerationTrigger`).\n- Built-in `dryRun` mode to preview outcomes.\n- Pluggable result logging/forwarding via `ExecutorSinkOptions`.\n\n```ts\nimport {\n ProposalExecutor,\n ExecutorProposalSink,\n} from '@contractspec/lib.contracts/regenerator';\n\nconst executor = new ProposalExecutor({\n tenantConfigUpdater,\n testExecutor,\n migrationExecutor,\n regenerationTrigger,\n});\n\nconst sink = new ExecutorProposalSink(executor, {\n dryRun: false,\n onResult: ({ result }) => console.log(result.status),\n});\n```\n\nExecution results include per-action status (`success`, `skipped`, `failed`) plus aggregated proposal status (`success`, `partial`, `failed`). Missing dependencies mark actions as `skipped`, making it easy to plug into partial automation flows today and extend later.\n\n### Next steps\n\n- Build adapters for existing telemetry/error providers.\n- Encode canonical rules (workflow failure, feature under-use, high latency).\n- Integrate with App Studio proposal inbox and automate acceptance (write spec diff, run tests, queue migrations).\n\n"
16
+ body: "## Regenerator Service\n\nThe Regenerator daemon observes telemetry, error, and behavior streams, then suggests spec-level changes (not code patches) that can be reviewed and applied through the App Studio.\n\n- Runtime entrypoint: `packages/libs/contracts/src/regenerator/service.ts`\n- Types/interfaces: `packages/libs/contracts/src/regenerator/types.ts`\n- Signal adapters: `packages/libs/contracts/src/regenerator/adapters.ts`\n\n### Architecture\n\n```text\nSignal Adapters ──► RegeneratorService ──► Rules ──► ProposalSink\n ▲ │\n │ ▼\n Telemetry / Errors / Behavior Spec change proposals\n```\n\n1. **Signal adapters** pull batches of telemetry, error logs, or behavior metrics for each `RegenerationContext`.\n2. `RegeneratorService` schedules polling (`resolveAppConfig` + `composeAppConfig` provide context).\n3. **Rules** implement domain heuristics and emit `SpecChangeProposal` objects.\n4. **Proposal sinks** persist or forward proposals for human review.\n\n### Key types\n\n```ts\nexport interface RegenerationContext {\n id: string;\n blueprint: AppBlueprintSpec;\n tenantConfig: TenantAppConfig;\n resolved: ResolvedAppConfig;\n}\n\nexport interface RegeneratorRule {\n id: string;\n description: string;\n evaluate(\n context: RegenerationContext,\n signals: RegeneratorSignal[]\n ): Promise<SpecChangeProposal[]>;\n}\n\nexport interface SpecChangeProposal {\n id: string;\n title: string;\n summary: string;\n confidence: 'low' | 'medium' | 'high';\n target: ProposalTarget;\n actions: ProposalAction[];\n blockers?: ProposalBlocker[];\n signalIds: string[];\n createdAt: Date;\n}\n```\n\n- Signals are normalized envelopes: telemetry (`count`, anomaly score), errors, and behavior trends.\n- Proposals reference blueprint or tenant specs via `ProposalTarget`.\n- Actions encode what the automation should perform (update blueprint, run tests/migrations, trigger regeneration).\n\n### Providing signals\n\nImplement `TelemetrySignalProvider`, `ErrorSignalProvider`, or `BehaviorSignalProvider`:\n\n```ts\nconst service = new RegeneratorService({\n contexts,\n adapters: {\n telemetry: new PosthogTelemetryAdapter(),\n errors: new SentryErrorAdapter(),\n },\n rules: [new WorkflowFailureRule(), new DataViewUsageRule()],\n sink: new ProposalQueueSink(),\n pollIntervalMs: 60_000,\n});\n```\n\nAdapters receive the full `RegenerationContext`, making it easy to scope queries per tenant/app.\n\n### Authoring rules\n\nRules focus on signals → proposals:\n\n```ts\nclass WorkflowFailureRule implements RegeneratorRule {\n id = 'workflow-failure';\n description = 'Suggest splitting workflows that exceed failure thresholds';\n\n async evaluate(context, signals) {\n const failures = signals.filter(\n (signal) =>\n signal.type === 'telemetry' &&\n signal.signal.eventName === 'workflow.failure' &&\n signal.signal.count >= 10\n );\n\n if (failures.length === 0) return [];\n\n return [\n {\n id: `${this.id}-${context.id}`,\n title: 'Split onboarding workflow',\n summary: 'Step 3 fails consistently; propose dedicated remediation branch.',\n confidence: 'medium',\n rationale: ['Failure count ≥ 10 within last window'],\n target: {\n specType: 'workflow',\n reference: { name: 'onboarding.workflow', version: '1.0.0' },\n tenantScoped: true,\n },\n actions: [\n { kind: 'update_tenant_config', summary: 'Add alternate fallback path' },\n { kind: 'run_tests', tests: ['workflows/onboarding.spec.ts'] },\n ],\n signalIds: failures.map((f) => f.signal.eventName),\n createdAt: new Date(),\n },\n ];\n }\n}\n```\n\n### Reviewing proposals\n\nProposals flow to a `ProposalSink` (queue, DB, messaging bus). The Studio will surface:\n\n1. Signal evidence (telemetry counts, error metadata)\n2. Proposed spec diffs and required actions (tests/migrations)\n3. Approval workflow (approve → write spec diff → run automation)\n\n### CLI driver\n\nRun the regenerator daemon from the CLI:\n\n```bash\nbunx contracts regenerator ./app.blueprint.ts ./tenant.config.ts ./regenerator.rules.ts auto \\\n --executor ./regenerator.executor.ts \\\n --poll-interval 60000 \\\n --batch-duration 300000 \\\n --dry-run\n```\n\n- Expects modules exporting default `AppBlueprintSpec`, `TenantAppConfig`, and one or more `RegenerationRule`s.\n- Pass a sink module path, or use the special `auto` value with `--executor <module>` to instantiate an `ExecutorProposalSink`.\n- Executor modules can export a `ProposalExecutor` instance, a factory, or a plain dependency object for the executor constructor. Optional exports: `sinkOptions`, `logger`, `onResult`, `dryRun`.\n- Optionally provide `--contexts ./contexts.ts` to load custom context arrays (advanced multi-tenant scenarios).\n- Use `--dry-run` to preview actions without mutating specs/configs, and `--once` for CI smoke tests.\n\n### Proposal executor\n\n`ProposalExecutor` + `ExecutorProposalSink` orchestrate follow-up actions once a proposal is approved:\n\n- Interfaces for applying blueprint or tenant-config updates (`BlueprintUpdater`, `TenantConfigUpdater`).\n- Hooks for running contract tests and migrations (`TestExecutor`, `MigrationExecutor`).\n- Optional trigger to recompose the runtime (`RegenerationTrigger`).\n- Built-in `dryRun` mode to preview outcomes.\n- Pluggable result logging/forwarding via `ExecutorSinkOptions`.\n\n```ts\nimport {\n ProposalExecutor,\n ExecutorProposalSink,\n} from '@contractspec/lib.contracts/regenerator';\n\nconst executor = new ProposalExecutor({\n tenantConfigUpdater,\n testExecutor,\n migrationExecutor,\n regenerationTrigger,\n});\n\nconst sink = new ExecutorProposalSink(executor, {\n dryRun: false,\n onResult: ({ result }) => console.log(result.status),\n});\n```\n\nExecution results include per-action status (`success`, `skipped`, `failed`) plus aggregated proposal status (`success`, `partial`, `failed`). Missing dependencies mark actions as `skipped`, making it easy to plug into partial automation flows today and extend later.\n\n### Next steps\n\n- Build adapters for existing telemetry/error providers.\n- Encode canonical rules (workflow failure, feature under-use, high latency).\n- Integrate with App Studio proposal inbox and automate acceptance (write spec diff, run tests, queue migrations).\n\n"
17
17
  }];
18
18
  registerDocBlocks(tech_contracts_regenerator_DocBlocks);
19
19
 
@@ -65,7 +65,7 @@ interface ProposalTarget {
65
65
  specType: 'workflow' | 'capability' | 'policy' | 'dataView' | 'telemetry' | 'experiment' | 'theme' | 'unknown';
66
66
  reference: {
67
67
  key: string;
68
- version?: number;
68
+ version?: string;
69
69
  };
70
70
  tenantScoped?: boolean;
71
71
  }
@@ -10,12 +10,13 @@ declare const keyOfSpecContract: (spec: AnySpecContract) => string;
10
10
  /** In-memory registry for PresentationSpec. */
11
11
  declare abstract class SpecContractRegistry<ContractType extends ContractSpecType, SpecContract extends AnySpecContract> {
12
12
  protected readonly contractType: ContractType;
13
- private items;
13
+ protected items: Map<string, SpecContract>;
14
14
  protected constructor(contractType: ContractType, items?: SpecContract[]);
15
15
  register(p: SpecContract): this;
16
16
  count(): number;
17
17
  list(): SpecContract[];
18
- get(key: string, version?: number): SpecContract | undefined;
18
+ get(key: string, version?: string): SpecContract | undefined;
19
+ has(key: string, version?: string): boolean;
19
20
  /** Filter presentations by criteria. */
20
21
  filter(criteria: RegistryFilter): SpecContract[];
21
22
  /** List presentations with specific tag. */
package/dist/registry.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { filterBy, getUniqueTags, groupBy, init_registry_utils } from "./registry-utils.js";
2
+ import { compareVersions } from "compare-versions";
2
3
 
3
4
  //#region src/registry.ts
4
5
  init_registry_utils();
@@ -25,16 +26,15 @@ var SpecContractRegistry = class {
25
26
  get(key, version) {
26
27
  if (version != null) return this.items.get(`${key}.v${version}`);
27
28
  let candidate;
28
- let max = -Infinity;
29
29
  for (const [k, p] of this.items.entries()) {
30
30
  if (!k.startsWith(`${key}.v`)) continue;
31
- if (p.meta.version > max) {
32
- max = p.meta.version;
33
- candidate = p;
34
- }
31
+ if (!candidate || compareVersions(p.meta.version, candidate.meta.version) > 0) candidate = p;
35
32
  }
36
33
  return candidate;
37
34
  }
35
+ has(key, version) {
36
+ return !!this.get(key, version);
37
+ }
38
38
  /** Filter presentations by criteria. */
39
39
  filter(criteria) {
40
40
  return filterBy(this.list(), criteria);
@@ -1,5 +1,5 @@
1
1
  import { Tag } from "./ownership.js";
2
- import * as z$1 from "zod";
2
+ import * as z$2 from "zod";
3
3
 
4
4
  //#region src/resources.d.ts
5
5
  interface ResourceMeta {
@@ -14,7 +14,7 @@ interface ResourceMeta {
14
14
  /** Tags for filtering/grouping */
15
15
  tags?: Tag[];
16
16
  }
17
- interface ResourceTemplateSpec<I extends z$1.ZodType> {
17
+ interface ResourceTemplateSpec<I extends z$2.ZodType> {
18
18
  meta: ResourceMeta;
19
19
  /** Arguments to materialize the URI (zod validates input) */
20
20
  input: I;
@@ -22,7 +22,7 @@ interface ResourceTemplateSpec<I extends z$1.ZodType> {
22
22
  * Resolve returns the resource body and a resolved URI.
23
23
  * It MUST be read-only (no side effects).
24
24
  */
25
- resolve: (args: z$1.infer<I>, ctx: {
25
+ resolve: (args: z$2.infer<I>, ctx: {
26
26
  userId?: string | null;
27
27
  orgId?: string | null;
28
28
  locale?: string;
@@ -32,10 +32,10 @@ interface ResourceTemplateSpec<I extends z$1.ZodType> {
32
32
  data: Uint8Array | string;
33
33
  }>;
34
34
  }
35
- declare function defineResourceTemplate<I extends z$1.ZodType>(spec: ResourceTemplateSpec<I>): ResourceTemplateSpec<I>;
35
+ declare function defineResourceTemplate<I extends z$2.ZodType>(spec: ResourceTemplateSpec<I>): ResourceTemplateSpec<I>;
36
36
  declare class ResourceRegistry {
37
37
  private templates;
38
- register<I extends z$1.ZodType>(tmpl: ResourceTemplateSpec<I>): this;
38
+ register<I extends z$2.ZodType>(tmpl: ResourceTemplateSpec<I>): this;
39
39
  listTemplates(): ResourceTemplateSpec<any>[];
40
40
  /** Try to match a concrete URI to a template by naive pattern substitution */
41
41
  match(uri: string): {
@@ -29,7 +29,7 @@ import "@pothos/plugin-tracing";
29
29
  function registerContractsOnBuilder(builder, reg, resources) {
30
30
  const { buildInputFieldArgs } = createInputTypeBuilder(builder);
31
31
  const outputTypeCache = /* @__PURE__ */ new Map();
32
- for (const spec of reg.listSpecs()) {
32
+ for (const spec of reg.list()) {
33
33
  const out = spec.io.output;
34
34
  if (out && "getZod" in out && typeof out.getZod === "function") {
35
35
  const model = out;
@@ -77,7 +77,7 @@ function registerContractsOnBuilder(builder, reg, resources) {
77
77
  }
78
78
  return "JSON";
79
79
  }
80
- for (const spec of reg.listSpecs()) {
80
+ for (const spec of reg.list()) {
81
81
  const fieldName = spec.transport?.gql?.field ?? defaultGqlField(spec.meta.key, spec.meta.version);
82
82
  const byIdField = spec.transport?.gql?.byIdField ?? "id";
83
83
  const returnsResource = spec.transport?.gql?.resource;
@@ -2,7 +2,7 @@ import { defaultMcpTool } from "../../jsonschema.js";
2
2
 
3
3
  //#region src/server/mcp/registerTools.ts
4
4
  function registerMcpTools(server, ops, ctx) {
5
- for (const spec of ops.listSpecs()) {
5
+ for (const spec of ops.list()) {
6
6
  if (spec.meta.kind !== "command") continue;
7
7
  const toolName = spec.transport?.mcp?.toolName ?? defaultMcpTool(spec.meta.key, spec.meta.version);
8
8
  server.registerTool(toolName, {
@@ -1,5 +1,5 @@
1
- import { HandlerCtx } from "../types.js";
2
1
  import { OperationSpecRegistry } from "../operations/registry.js";
2
+ import { HandlerCtx } from "../types.js";
3
3
  import { RestOptions } from "./rest-generic.js";
4
4
  import { Elysia } from "elysia";
5
5
 
@@ -7,7 +7,7 @@ function elysiaPlugin(app, reg, ctxFactory, options) {
7
7
  request: req,
8
8
  store: app.store
9
9
  }), options);
10
- for (const spec of reg.listSpecs()) {
10
+ for (const spec of reg.list()) {
11
11
  const method = spec.transport?.rest?.method ?? (spec.meta.kind === "query" ? "GET" : "POST");
12
12
  const path = (options?.basePath ?? "") + (spec.transport?.rest?.path ?? `/${spec.meta.key.replace(/\./g, "/")}/v${spec.meta.version}`);
13
13
  app[method.toLowerCase()](path, ({ request }) => handler(request));
@@ -1,5 +1,5 @@
1
- import { HandlerCtx } from "../types.js";
2
1
  import { OperationSpecRegistry } from "../operations/registry.js";
2
+ import { HandlerCtx } from "../types.js";
3
3
  import { RestOptions } from "./rest-generic.js";
4
4
  import { Request, Router } from "express";
5
5
 
@@ -7,7 +7,7 @@ import { createFetchHandler } from "./rest-generic.js";
7
7
  */
8
8
  function expressRouter(express, reg, ctxFactory, options) {
9
9
  const router = express.Router();
10
- for (const spec of reg.listSpecs()) {
10
+ for (const spec of reg.list()) {
11
11
  const method = spec.transport?.rest?.method ?? (spec.meta.kind === "query" ? "GET" : "POST");
12
12
  const path = (options?.basePath ?? "") + (spec.transport?.rest?.path ?? `/${spec.meta.key.replace(/\./g, "/")}/v${spec.meta.version}`);
13
13
  router[method.toLowerCase()](path, async (req, res) => {
@@ -1,5 +1,5 @@
1
- import { HandlerCtx } from "../types.js";
2
1
  import { OperationSpecRegistry } from "../operations/registry.js";
2
+ import { HandlerCtx } from "../types.js";
3
3
 
4
4
  //#region src/server/rest-generic.d.ts
5
5
  interface RestOptions {
@@ -33,7 +33,7 @@ function createFetchHandler(reg, ctxFactory, options) {
33
33
  prettyJson: options?.prettyJson ?? false,
34
34
  onError: options?.onError
35
35
  };
36
- const routes = reg.listSpecs().map((spec) => ({
36
+ const routes = reg.list().map((spec) => ({
37
37
  method: spec.transport?.rest?.method ?? (spec.meta.kind === "query" ? "GET" : "POST"),
38
38
  path: joinPath(opts.basePath, spec.transport?.rest?.path ?? defaultRestPath(spec.meta.key, spec.meta.version)),
39
39
  name: spec.meta.key,
@@ -1,5 +1,5 @@
1
- import { HandlerCtx } from "../types.js";
2
1
  import { OperationSpecRegistry } from "../operations/registry.js";
2
+ import { HandlerCtx } from "../types.js";
3
3
  import { RestOptions } from "./rest-generic.js";
4
4
 
5
5
  //#region src/server/rest-next-app.d.ts
@@ -1,5 +1,5 @@
1
- import { HandlerCtx } from "../types.js";
2
1
  import { OperationSpecRegistry } from "../operations/registry.js";
2
+ import { HandlerCtx } from "../types.js";
3
3
 
4
4
  //#region src/server/rest-next-mcp.d.ts
5
5
  declare function makeNextMcpServerFromRegistry(reg: OperationSpecRegistry, ctxFactory: () => HandlerCtx): {
@@ -5,7 +5,7 @@ import { createMcpHandler } from "mcp-handler";
5
5
  //#region src/server/rest-next-mcp.ts
6
6
  function makeNextMcpServerFromRegistry(reg, ctxFactory) {
7
7
  const handler = createMcpHandler((server) => {
8
- for (const spec of reg.listSpecs()) {
8
+ for (const spec of reg.list()) {
9
9
  const { input, meta } = jsonSchemaForSpec(spec);
10
10
  if (meta.kind === "query") {
11
11
  const resourceName = spec.transport?.mcp?.toolName ?? defaultMcpTool(spec.meta.key, spec.meta.version);
@@ -1,5 +1,5 @@
1
- import { HandlerCtx } from "../types.js";
2
1
  import { OperationSpecRegistry } from "../operations/registry.js";
2
+ import { HandlerCtx } from "../types.js";
3
3
  import { RestOptions } from "./rest-generic.js";
4
4
  import { NextApiRequest, NextApiResponse } from "next";
5
5
 
@@ -13,7 +13,7 @@ const tech_contracts_telemetry_DocBlocks = [{
13
13
  "contracts",
14
14
  "telemetry"
15
15
  ],
16
- body: "## TelemetrySpec\n\nTelemetry specs describe product analytics in a durable, type-safe way. They reference existing `EventSpec`s (same name/version) but layer on privacy classification, retention, sampling, and anomaly detection so instrumentation stays compliant and observable.\n\n- **File location**: `packages/libs/contracts/src/telemetry/spec.ts`\n- **Runtime tracker**: `packages/libs/contracts/src/telemetry/tracker.ts`\n- **Anomaly monitor**: `packages/libs/contracts/src/telemetry/anomaly.ts`\n\n### Core concepts\n\n```ts\nexport interface TelemetrySpec {\n meta: TelemetryMeta;\n events: TelemetryEventDef[];\n config?: TelemetryConfig;\n}\n```\n\n- `meta`: ownership + identifiers (`name`, `version`, `domain`)\n- `events`: per-event semantics, property definitions, privacy level, retention, sampling, anomaly rules\n- `config`: defaults and provider configuration\n- `TelemetryRegistry`: registers specs, resolves latest version, finds event definitions by name/version\n\n### An example\n\n```ts\nexport const SigilTelemetry: TelemetrySpec = {\n meta: {\n name: 'sigil.telemetry',\n version: 1,\n title: 'Sigil telemetry',\n description: 'Core Sigil product telemetry',\n domain: 'sigil',\n owners: ['@team.analytics'],\n tags: ['telemetry'],\n stability: StabilityEnum.Experimental,\n },\n config: {\n defaultRetentionDays: 30,\n defaultSamplingRate: 1,\n providers: [\n { type: 'posthog', config: { projectApiKey: process.env.POSTHOG_KEY } },\n ],\n },\n events: [\n {\n name: 'sigil.telemetry.workflow_step',\n version: 1,\n semantics: {\n what: 'Workflow step executed',\n who: 'Actor executing the workflow',\n },\n privacy: 'internal',\n properties: {\n workflow: { type: 'string', required: true },\n step: { type: 'string', required: true },\n durationMs: { type: 'number' },\n userId: { type: 'string', pii: true, redact: true },\n },\n anomalyDetection: {\n enabled: true,\n minimumSample: 10,\n thresholds: [\n { metric: 'durationMs', max: 1500 },\n ],\n actions: ['alert', 'trigger_regen'],\n },\n },\n ],\n};\n```\n\n### Tracking events at runtime\n\n`TelemetryTracker` performs sampling, PII redaction, provider dispatch, and anomaly detection.\n\n```ts\nconst tracker = new TelemetryTracker({\n registry: telemetryRegistry,\n providers: [\n {\n id: 'posthog',\n async send(dispatch) {\n posthog.capture({\n event: dispatch.name,\n properties: dispatch.properties,\n distinctId: dispatch.context.userId ?? dispatch.context.sessionId,\n });\n },\n },\n ],\n anomalyMonitor: new TelemetryAnomalyMonitor({\n onAnomaly(event) {\n console.warn('Telemetry anomaly detected', event);\n },\n }),\n});\n\nawait tracker.track('sigil.telemetry.workflow_step', 1, {\n workflow: 'onboarding',\n step: 'verify_email',\n durationMs: 2100,\n userId: 'user-123',\n});\n```\n\n- Sampling obeys the event-specific rate (fallback to spec defaults)\n- Properties flagged with `pii` or `redact` are masked before dispatch\n- Anomaly monitor evaluates thresholds and triggers actions (e.g., log, alert, regeneration)\n\n### Spec integration\n\n- `ContractSpec.telemetry` allows operations to emit success/failure events automatically\n- `OperationSpecRegistry.execute()` uses the tracker when `ctx.telemetry` is provided\n- `WorkflowRunner` (Phase 4 follow-up) will emit telemetry during step transitions\n- `TelemetrySpec` events should reuse `EventSpec` names/versions to keep analytics/contract parity\n\n### CLI workflow\n\n```\ncontracts-cli create telemetry\n```\n\n- Interactive wizard prompts for meta, providers, events, properties, retention, anomaly rules\n- Output: `*.telemetry.ts` file using `TelemetrySpec`\n\n### Best practices\n\n- Prefer `internal` privacy for non-PII; mark PII properties explicitly with `pii` + `redact`\n- Keep sampling ≥0.05 except for high-volume events\n- Configure anomaly detection on key metrics (duration, error count, conversion)\n- Check telemetry into source control alongside contracts; regenerate via CLI when specs change\n\n### Next steps\n\n- Phase 5: Regenerator monitors telemetry anomalies to propose spec improvements\n- Phase 6: Studio surfaces telemetry controls per tenant via `TenantAppConfig`\n\n"
16
+ body: "## TelemetrySpec\n\nTelemetry specs describe product analytics in a durable, type-safe way. They reference existing `EventSpec`s (same name/version) but layer on privacy classification, retention, sampling, and anomaly detection so instrumentation stays compliant and observable.\n\n- **File location**: `packages/libs/contracts/src/telemetry/spec.ts`\n- **Runtime tracker**: `packages/libs/contracts/src/telemetry/tracker.ts`\n- **Anomaly monitor**: `packages/libs/contracts/src/telemetry/anomaly.ts`\n\n### Core concepts\n\n```ts\nexport interface TelemetrySpec {\n meta: TelemetryMeta;\n events: TelemetryEventDef[];\n config?: TelemetryConfig;\n}\n```\n\n- `meta`: ownership + identifiers (`name`, `version`, `domain`)\n- `events`: per-event semantics, property definitions, privacy level, retention, sampling, anomaly rules\n- `config`: defaults and provider configuration\n- `TelemetryRegistry`: registers specs, resolves latest version, finds event definitions by name/version\n\n### An example\n\n```ts\nexport const SigilTelemetry: TelemetrySpec = {\n meta: {\n name: 'sigil.telemetry',\n version: '1.0.0',\n title: 'Sigil telemetry',\n description: 'Core Sigil product telemetry',\n domain: 'sigil',\n owners: ['@team.analytics'],\n tags: ['telemetry'],\n stability: StabilityEnum.Experimental,\n },\n config: {\n defaultRetentionDays: 30,\n defaultSamplingRate: 1,\n providers: [\n { type: 'posthog', config: { projectApiKey: process.env.POSTHOG_KEY } },\n ],\n },\n events: [\n {\n name: 'sigil.telemetry.workflow_step',\n version: '1.0.0',\n semantics: {\n what: 'Workflow step executed',\n who: 'Actor executing the workflow',\n },\n privacy: 'internal',\n properties: {\n workflow: { type: 'string', required: true },\n step: { type: 'string', required: true },\n durationMs: { type: 'number' },\n userId: { type: 'string', pii: true, redact: true },\n },\n anomalyDetection: {\n enabled: true,\n minimumSample: 10,\n thresholds: [\n { metric: 'durationMs', max: 1500 },\n ],\n actions: ['alert', 'trigger_regen'],\n },\n },\n ],\n};\n```\n\n### Tracking events at runtime\n\n`TelemetryTracker` performs sampling, PII redaction, provider dispatch, and anomaly detection.\n\n```ts\nconst tracker = new TelemetryTracker({\n registry: telemetryRegistry,\n providers: [\n {\n id: 'posthog',\n async send(dispatch) {\n posthog.capture({\n event: dispatch.name,\n properties: dispatch.properties,\n distinctId: dispatch.context.userId ?? dispatch.context.sessionId,\n });\n },\n },\n ],\n anomalyMonitor: new TelemetryAnomalyMonitor({\n onAnomaly(event) {\n console.warn('Telemetry anomaly detected', event);\n },\n }),\n});\n\nawait tracker.track('sigil.telemetry.workflow_step', 1, {\n workflow: 'onboarding',\n step: 'verify_email',\n durationMs: 2100,\n userId: 'user-123',\n});\n```\n\n- Sampling obeys the event-specific rate (fallback to spec defaults)\n- Properties flagged with `pii` or `redact` are masked before dispatch\n- Anomaly monitor evaluates thresholds and triggers actions (e.g., log, alert, regeneration)\n\n### Spec integration\n\n- `ContractSpec.telemetry` allows operations to emit success/failure events automatically\n- `OperationSpecRegistry.execute()` uses the tracker when `ctx.telemetry` is provided\n- `WorkflowRunner` (Phase 4 follow-up) will emit telemetry during step transitions\n- `TelemetrySpec` events should reuse `EventSpec` names/versions to keep analytics/contract parity\n\n### CLI workflow\n\n```\ncontracts-cli create telemetry\n```\n\n- Interactive wizard prompts for meta, providers, events, properties, retention, anomaly rules\n- Output: `*.telemetry.ts` file using `TelemetrySpec`\n\n### Best practices\n\n- Prefer `internal` privacy for non-PII; mark PII properties explicitly with `pii` + `redact`\n- Keep sampling ≥0.05 except for high-volume events\n- Configure anomaly detection on key metrics (duration, error count, conversion)\n- Check telemetry into source control alongside contracts; regenerate via CLI when specs change\n\n### Next steps\n\n- Phase 5: Regenerator monitors telemetry anomalies to propose spec improvements\n- Phase 6: Studio surfaces telemetry controls per tenant via `TenantAppConfig`\n\n"
17
17
  }];
18
18
  registerDocBlocks(tech_contracts_telemetry_DocBlocks);
19
19
 
@@ -1,4 +1,5 @@
1
1
  import { OwnerShipMeta } from "../ownership.js";
2
+ import { SpecContractRegistry } from "../registry.js";
2
3
 
3
4
  //#region src/telemetry/spec.d.ts
4
5
  type TelemetryPrivacyLevel = 'public' | 'internal' | 'pii' | 'sensitive';
@@ -38,7 +39,7 @@ interface TelemetryEventDef {
38
39
  /** Name of the event (should match EventSpec.key for cross-reference). */
39
40
  key: string;
40
41
  /** Version of the underlying event. */
41
- version: number;
42
+ version: string;
42
43
  /** High-level semantics for docs/analyzers. */
43
44
  semantics: {
44
45
  who?: string;
@@ -76,15 +77,14 @@ interface TelemetrySpec {
76
77
  events: TelemetryEventDef[];
77
78
  config?: TelemetryConfig;
78
79
  }
79
- declare class TelemetryRegistry {
80
- private readonly items;
80
+ declare class TelemetryRegistry extends SpecContractRegistry<'telemetry', TelemetrySpec> {
81
81
  private readonly eventsByKey;
82
82
  private readonly specByEventKey;
83
+ constructor(items?: TelemetrySpec[]);
83
84
  register(spec: TelemetrySpec): this;
84
- list(): TelemetrySpec[];
85
- get(key: string, version?: number): TelemetrySpec | undefined;
86
- findEventDef(name: string, version?: number): TelemetryEventDef | undefined;
87
- getSpecForEvent(name: string, version?: number): TelemetrySpec | undefined;
85
+ private indexEvents;
86
+ findEventDef(name: string, version?: string): TelemetryEventDef | undefined;
87
+ getSpecForEvent(name: string, version: string): TelemetrySpec | undefined;
88
88
  }
89
89
  declare function makeTelemetryKey(meta: TelemetryMeta): string;
90
90
  //#endregion
@@ -1,64 +1,37 @@
1
+ import { SpecContractRegistry } from "../registry.js";
2
+ import { compareVersions } from "compare-versions";
3
+
1
4
  //#region src/telemetry/spec.ts
2
5
  const telemetryKey = (meta) => `${meta.key}.v${meta.version}`;
3
- var TelemetryRegistry = class {
4
- items = /* @__PURE__ */ new Map();
6
+ var TelemetryRegistry = class extends SpecContractRegistry {
5
7
  eventsByKey = /* @__PURE__ */ new Map();
6
8
  specByEventKey = /* @__PURE__ */ new Map();
9
+ constructor(items) {
10
+ super("telemetry", items);
11
+ if (items) items.forEach((spec) => this.indexEvents(spec));
12
+ }
7
13
  register(spec) {
8
- const key = telemetryKey(spec.meta);
9
- if (this.items.has(key)) throw new Error(`Duplicate TelemetrySpec registration for ${key}`);
10
- this.items.set(key, spec);
14
+ super.register(spec);
15
+ this.indexEvents(spec);
16
+ return this;
17
+ }
18
+ indexEvents(spec) {
11
19
  for (const event of spec.events) {
12
20
  this.eventsByKey.set(`${event.key}.v${event.version}`, event);
13
21
  this.specByEventKey.set(`${event.key}.v${event.version}`, spec);
14
22
  }
15
- return this;
16
- }
17
- list() {
18
- return [...this.items.values()];
19
- }
20
- get(key, version) {
21
- if (version != null) return this.items.get(`${key}.v${version}`);
22
- let latest;
23
- let maxVersion = -Infinity;
24
- for (const item of this.items.values()) {
25
- if (item.meta.key !== key) continue;
26
- if (item.meta.version > maxVersion) {
27
- maxVersion = item.meta.version;
28
- latest = item;
29
- }
30
- }
31
- return latest;
32
23
  }
33
24
  findEventDef(name, version) {
34
25
  if (version != null) return this.eventsByKey.get(`${name}.v${version}`);
35
26
  let latest;
36
- let maxVersion = -Infinity;
37
- for (const [key, event] of this.eventsByKey.entries()) {
38
- const [eventName, versionPart] = key.split(".v");
39
- if (eventName !== name) continue;
40
- const ver = Number(versionPart);
41
- if (Number.isFinite(ver) && ver > maxVersion) {
42
- maxVersion = ver;
43
- latest = event;
44
- }
27
+ for (const event of this.eventsByKey.values()) {
28
+ if (event.key !== name) continue;
29
+ if (!latest || compareVersions(event.version, latest.version) > 0) latest = event;
45
30
  }
46
31
  return latest;
47
32
  }
48
33
  getSpecForEvent(name, version) {
49
- if (version != null) return this.specByEventKey.get(`${name}.v${version}`);
50
- let latest;
51
- let maxVersion = -Infinity;
52
- for (const [key, spec] of this.specByEventKey.entries()) {
53
- const [eventName, versionPart] = key.split(".v");
54
- if (eventName !== name) continue;
55
- const ver = Number(versionPart);
56
- if (Number.isFinite(ver) && ver > maxVersion) {
57
- maxVersion = ver;
58
- latest = spec;
59
- }
60
- }
61
- return latest;
34
+ return this.specByEventKey.get(`${name}.v${version}`);
62
35
  }
63
36
  };
64
37
  function makeTelemetryKey(meta) {
@@ -14,7 +14,7 @@ interface TelemetryEventContext {
14
14
  interface TelemetryDispatch {
15
15
  id: string;
16
16
  name: string;
17
- version: number;
17
+ version: string;
18
18
  occurredAt: string;
19
19
  properties: Record<string, unknown>;
20
20
  privacy: TelemetryEventDef['privacy'];
@@ -43,7 +43,7 @@ declare class TelemetryTracker {
43
43
  constructor(options: TelemetryTrackerOptions);
44
44
  registerProvider(provider: RuntimeTelemetryProvider): void;
45
45
  unregisterProvider(providerId: string): void;
46
- track(name: string, version: number, properties: Record<string, unknown>, context?: TelemetryEventContext): Promise<boolean>;
46
+ track(name: string, version: string, properties: Record<string, unknown>, context?: TelemetryEventContext): Promise<boolean>;
47
47
  private shouldSample;
48
48
  private redactProperties;
49
49
  }
@@ -1,6 +1,6 @@
1
- import { HandlerCtx } from "../types.js";
2
1
  import { Assertion, TestScenario, TestSpec } from "./spec.js";
3
2
  import { OperationSpecRegistry } from "../operations/registry.js";
3
+ import { HandlerCtx } from "../types.js";
4
4
  import "../index.js";
5
5
 
6
6
  //#region src/tests/runner.d.ts
@@ -3,11 +3,11 @@ import { OwnerShipMeta } from "../ownership.js";
3
3
  //#region src/tests/spec.d.ts
4
4
  interface OperationTargetRef {
5
5
  key: string;
6
- version?: number;
6
+ version?: string;
7
7
  }
8
8
  interface WorkflowTargetRef {
9
9
  key: string;
10
- version?: number;
10
+ version?: string;
11
11
  }
12
12
  type TestTarget = {
13
13
  type: 'operation';
@@ -35,7 +35,7 @@ interface ExpectErrorAssertion {
35
35
  }
36
36
  interface ExpectedEvent {
37
37
  key: string;
38
- version: number;
38
+ version: string;
39
39
  min?: number;
40
40
  max?: number;
41
41
  }
@@ -68,13 +68,13 @@ interface TestSpec {
68
68
  }
69
69
  interface TestSpecRef {
70
70
  key: string;
71
- version?: number;
71
+ version?: string;
72
72
  }
73
73
  declare class TestRegistry {
74
74
  private readonly items;
75
75
  register(spec: TestSpec): this;
76
76
  list(): TestSpec[];
77
- get(name: string, version?: number): TestSpec | undefined;
77
+ get(name: string, version?: string): TestSpec | undefined;
78
78
  }
79
79
  declare function makeTestKey(meta: TestSpecMeta): string;
80
80
  //#endregion
@@ -1,3 +1,5 @@
1
+ import { compareVersions } from "compare-versions";
2
+
1
3
  //#region src/tests/spec.ts
2
4
  const testKey = (meta) => `${meta.key}.v${meta.version}`;
3
5
  var TestRegistry = class {
@@ -14,13 +16,9 @@ var TestRegistry = class {
14
16
  get(name, version) {
15
17
  if (version != null) return this.items.get(`${name}.v${version}`);
16
18
  let latest;
17
- let maxVersion = -Infinity;
18
19
  for (const spec of this.items.values()) {
19
20
  if (spec.meta.key !== name) continue;
20
- if (spec.meta.version > maxVersion) {
21
- maxVersion = spec.meta.version;
22
- latest = spec;
23
- }
21
+ if (!latest || compareVersions(spec.meta.version, latest.meta.version) > 0) latest = spec;
24
22
  }
25
23
  return latest;
26
24
  }