@opengeni/db 1.0.2 → 1.2.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 (86) hide show
  1. package/dist/artifact-catalog.d.ts +12 -0
  2. package/dist/canonical-human-identities.js +3 -3
  3. package/dist/capability-reconciliation.js +2 -2
  4. package/dist/{chunk-KH3TYDGY.js → chunk-3GAR5UN3.js} +2 -2
  5. package/dist/{chunk-UEQQ2WWU.js → chunk-3TGYFFZR.js} +3 -26
  6. package/dist/chunk-3TGYFFZR.js.map +1 -0
  7. package/dist/{chunk-F3YMVRF4.js → chunk-6EIEGHPK.js} +4 -4
  8. package/dist/{chunk-OQZOGCLG.js → chunk-CONWNUSL.js} +2 -2
  9. package/dist/{chunk-7S53WLPI.js → chunk-CYP72JTQ.js} +11 -4
  10. package/dist/{chunk-7S53WLPI.js.map → chunk-CYP72JTQ.js.map} +1 -1
  11. package/dist/{chunk-LDTQGYD2.js → chunk-FWQNHNOU.js} +54 -2
  12. package/dist/chunk-FWQNHNOU.js.map +1 -0
  13. package/dist/{chunk-KEDWSSNP.js → chunk-GDN5TSZP.js} +2 -2
  14. package/dist/{chunk-PQGTWFRF.js → chunk-JICUP7ZR.js} +2 -2
  15. package/dist/{chunk-NLSG7GGA.js → chunk-JLVWBIEM.js} +3 -3
  16. package/dist/{chunk-Y77PVZZD.js → chunk-LXPFNG4L.js} +2 -2
  17. package/dist/{chunk-56MYWKQV.js → chunk-O2Y5E5M3.js} +68 -32
  18. package/dist/chunk-O2Y5E5M3.js.map +1 -0
  19. package/dist/{chunk-7POTLB24.js → chunk-QFJNEXFL.js} +2 -2
  20. package/dist/{chunk-MACELCXG.js → chunk-UCIMHNII.js} +101 -21
  21. package/dist/chunk-UCIMHNII.js.map +1 -0
  22. package/dist/{chunk-OMHBVHX7.js → chunk-VDPCIFKL.js} +5 -5
  23. package/dist/{chunk-N5QCKHYL.js → chunk-YOYILDLY.js} +87 -19
  24. package/dist/chunk-YOYILDLY.js.map +1 -0
  25. package/dist/connect-attempts.js +3 -3
  26. package/dist/credit-balances.d.ts +18 -0
  27. package/dist/credit-promotion-policy.d.ts +10 -0
  28. package/dist/editable-artifact-durable-export.js +2 -2
  29. package/dist/editable-artifacts.js +3 -3
  30. package/dist/index.d.ts +35 -10
  31. package/dist/index.js +2432 -2255
  32. package/dist/index.js.map +1 -1
  33. package/dist/managed-auth-session-sets.js +3 -3
  34. package/dist/managed-user-setup.d.ts +1 -0
  35. package/dist/mcp-operations.js +2 -2
  36. package/dist/organization-integration-policy.js +3 -3
  37. package/dist/organization-membership-lifecycle.d.ts +1 -0
  38. package/dist/persistence-errors.d.ts +9 -6
  39. package/dist/provision-roles.js +1 -1
  40. package/dist/retained-provider-commands.js +6 -6
  41. package/dist/runtime-posture.d.ts +7 -6
  42. package/dist/schema.d.ts +214 -2
  43. package/dist/schema.js +5 -1
  44. package/dist/session-background-commands.js +4 -4
  45. package/dist/session-command-output.js +5 -5
  46. package/dist/session-event-slices.js +3 -3
  47. package/dist/session-mcp-credential-rotation.js +5 -5
  48. package/dist/session-tenancy.d.ts +6 -1
  49. package/dist/session-tenancy.js +3 -5
  50. package/dist/video-generation.js +3 -3
  51. package/dist/workspace-tool-defaults.js +4 -4
  52. package/drizzle/0610_artifact_catalog_pins.sql +165 -0
  53. package/drizzle/0611_universal_session_tenancy_activation.sql +166 -0
  54. package/drizzle/0612_scheduled_model_unavailable_refusal.sql +21 -0
  55. package/drizzle/0613_model_scoped_promotional_credits.sql +238 -0
  56. package/drizzle/0614_quiescence_command_containment.sql +7 -0
  57. package/drizzle/0615_credit_promotion_policy_validation.sql +63 -0
  58. package/package.json +7 -7
  59. package/src/artifact-catalog.ts +31 -5
  60. package/src/credit-balances.ts +109 -0
  61. package/src/credit-promotion-policy.ts +12 -0
  62. package/src/database.ts +81 -42
  63. package/src/index.ts +295 -79
  64. package/src/managed-user-setup.ts +8 -0
  65. package/src/organization-membership-lifecycle.ts +8 -0
  66. package/src/persistence-errors.ts +17 -6
  67. package/src/provision-roles.ts +25 -0
  68. package/src/runtime-posture-cli.ts +0 -2
  69. package/src/runtime-posture.ts +86 -27
  70. package/src/schema.ts +57 -1
  71. package/src/session-tenancy.ts +9 -31
  72. package/src/video-generation.ts +2 -7
  73. package/dist/chunk-56MYWKQV.js.map +0 -1
  74. package/dist/chunk-LDTQGYD2.js.map +0 -1
  75. package/dist/chunk-MACELCXG.js.map +0 -1
  76. package/dist/chunk-N5QCKHYL.js.map +0 -1
  77. package/dist/chunk-UEQQ2WWU.js.map +0 -1
  78. /package/dist/{chunk-KH3TYDGY.js.map → chunk-3GAR5UN3.js.map} +0 -0
  79. /package/dist/{chunk-F3YMVRF4.js.map → chunk-6EIEGHPK.js.map} +0 -0
  80. /package/dist/{chunk-OQZOGCLG.js.map → chunk-CONWNUSL.js.map} +0 -0
  81. /package/dist/{chunk-KEDWSSNP.js.map → chunk-GDN5TSZP.js.map} +0 -0
  82. /package/dist/{chunk-PQGTWFRF.js.map → chunk-JICUP7ZR.js.map} +0 -0
  83. /package/dist/{chunk-NLSG7GGA.js.map → chunk-JLVWBIEM.js.map} +0 -0
  84. /package/dist/{chunk-Y77PVZZD.js.map → chunk-LXPFNG4L.js.map} +0 -0
  85. /package/dist/{chunk-7POTLB24.js.map → chunk-QFJNEXFL.js.map} +0 -0
  86. /package/dist/{chunk-OMHBVHX7.js.map → chunk-VDPCIFKL.js.map} +0 -0
@@ -7,9 +7,9 @@ import {
7
7
  getConnectAttempt,
8
8
  getConnectBeginReplay,
9
9
  listPendingConnectAttempts
10
- } from "./chunk-OQZOGCLG.js";
11
- import "./chunk-56MYWKQV.js";
12
- import "./chunk-LDTQGYD2.js";
10
+ } from "./chunk-CONWNUSL.js";
11
+ import "./chunk-O2Y5E5M3.js";
12
+ import "./chunk-FWQNHNOU.js";
13
13
  import "./chunk-P7YDM2RI.js";
14
14
  import "./chunk-PZ5AY32C.js";
15
15
  export {
@@ -0,0 +1,18 @@
1
+ import type { BillingBalance } from "@opengeni/contracts";
2
+ import type { Database } from "./database.js";
3
+ /** Read totals and grant remainders from one database snapshot. */
4
+ type CreditBalanceSnapshot = BillingBalance & {
5
+ creditPolicyRevision?: number;
6
+ };
7
+ export declare function getBillingBalance(db: Database, accountId: string, policyRevision?: number): Promise<CreditBalanceSnapshot>;
8
+ /** Missing model means a non-model resource, which can only use general credits. */
9
+ export declare function spendableCreditMicros(balance: BillingBalance, modelId?: string): number;
10
+ export declare function getSpendableCreditBalance(db: Database, accountId: string, modelId?: string, policyRevision?: number): Promise<CreditBalanceSnapshot>;
11
+ export declare function planCreditDebit(balance: BillingBalance, requestedMicros: number, modelId?: string): {
12
+ debitedMicros: number;
13
+ allocations: {
14
+ grantEntryId: string;
15
+ amountMicros: number;
16
+ }[];
17
+ };
18
+ export {};
@@ -0,0 +1,10 @@
1
+ import { type Database } from "./database.js";
2
+ /** Runtime policy supersedes deployment bootstrap configuration. No process cache. */
3
+ export declare function readCreditPromotionPolicy(db: Database): Promise<{
4
+ defaultModelIds?: string[] | undefined;
5
+ signupModelIds?: string[] | undefined;
6
+ offers: Record<string, {
7
+ label: string;
8
+ eligibleModelIds?: string[] | undefined;
9
+ }>;
10
+ } | null>;
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  rawRows,
3
3
  withRlsContext
4
- } from "./chunk-56MYWKQV.js";
5
- import "./chunk-LDTQGYD2.js";
4
+ } from "./chunk-O2Y5E5M3.js";
5
+ import "./chunk-FWQNHNOU.js";
6
6
  import "./chunk-P7YDM2RI.js";
7
7
  import "./chunk-PZ5AY32C.js";
8
8
 
@@ -10,9 +10,9 @@ import {
10
10
  listEditableArtifactIdsForSession,
11
11
  touchEditableArtifactSessionLink,
12
12
  transactionallyAuthorizeEditableArtifactActor
13
- } from "./chunk-Y77PVZZD.js";
14
- import "./chunk-56MYWKQV.js";
15
- import "./chunk-LDTQGYD2.js";
13
+ } from "./chunk-LXPFNG4L.js";
14
+ import "./chunk-O2Y5E5M3.js";
15
+ import "./chunk-FWQNHNOU.js";
16
16
  import "./chunk-P7YDM2RI.js";
17
17
  import "./chunk-PZ5AY32C.js";
18
18
  export {
package/dist/index.d.ts CHANGED
@@ -1,4 +1,7 @@
1
+ export { readCreditPromotionPolicy } from "./credit-promotion-policy.js";
2
+ export { getBillingBalance, getSpendableCreditBalance, spendableCreditMicros, } from "./credit-balances.js";
1
3
  export { SubscriptionAccountChangedError } from "./subscription-account-conflict.js";
4
+ import type { GoalAdmissionPausedReason } from "@opengeni/contracts";
2
5
  export declare const getScheduledTaskClaudeProviderAccountAuthoritySnapshot: (db: Database, workspaceId: string, taskId: string) => Promise<{
3
6
  version: 1;
4
7
  scope: "organization";
@@ -95,6 +98,7 @@ import { type SQL } from "drizzle-orm";
95
98
  export { frozenInitiatorForCommandActor, type FrozenTurnInitiator } from "./turn-initiator.js";
96
99
  export { LOSSLESS_TEXT_PREFIX } from "./lossless-json.js";
97
100
  import { type NewSessionDraftSnapshot } from "./new-session-drafts.js";
101
+ export { DatabaseTransactionError } from "./persistence-errors.js";
98
102
  import { type SessionDiscoveryControl, type SessionCommandActor, type SessionTurnAttemptOutcome } from "./session-control.js";
99
103
  import { type ConnectedMachineBackgroundCommandClaim } from "./session-background-commands.js";
100
104
  import { type SessionRealtimeContinuityEntry } from "./session-realtime-context.js";
@@ -1045,6 +1049,7 @@ export declare function applyCreditLedgerEntry(db: Database, input: {
1045
1049
  workspaceId?: string | null;
1046
1050
  type: string;
1047
1051
  amountMicros: number;
1052
+ eligibleModelIds?: string[] | undefined;
1048
1053
  sourceType?: string | null;
1049
1054
  sourceId?: string | null;
1050
1055
  idempotencyKey: string;
@@ -1056,6 +1061,9 @@ export declare function applyCreditDebitUpToBalance(db: Database, input: {
1056
1061
  workspaceId?: string | null;
1057
1062
  type: string;
1058
1063
  requestedAmountMicros: number;
1064
+ /** Canonical, accepted model ID. Omitted for non-model usage. */
1065
+ modelId?: string;
1066
+ creditPolicyRevision?: number | undefined;
1059
1067
  sourceType?: string | null;
1060
1068
  sourceId?: string | null;
1061
1069
  idempotencyKey: string;
@@ -1111,7 +1119,6 @@ export declare function recordStripeWebhookEvent(db: Database, input: {
1111
1119
  }): Promise<boolean>;
1112
1120
  export declare function isStripeWebhookProcessed(db: Database, id: string): Promise<boolean>;
1113
1121
  export declare function markStripeWebhookProcessed(db: Database, id: string): Promise<void>;
1114
- export declare function getBillingBalance(db: Database, accountId: string): Promise<BillingBalance>;
1115
1122
  /**
1116
1123
  * Whether the organization holds a positive OpenGeni credit balance, whatever
1117
1124
  * its source: a purchase, an operator grant, a test credit, or the one-time
@@ -3306,7 +3313,7 @@ export declare function recordScheduledTaskAdmissionFailure(db: Database, input:
3306
3313
  diagnostic: ConnectionAccountSelectionDiagnostic;
3307
3314
  }): Promise<ScheduledTaskRun>;
3308
3315
  /** Reasons a scheduled occurrence is refused before accepting execution. */
3309
- export type ScheduledTaskAdmissionRefusalReason = "scheduled_authority_unavailable" | "machine_target_unavailable" | "machine_enrollment_inactive" | "variable_set_unavailable" | "rig_version_unavailable" | "insufficient_credits" | "allowance_exhausted" | "monthly_model_cost_limit" | "monthly_agent_run_limit";
3316
+ export type ScheduledTaskAdmissionRefusalReason = "scheduled_authority_unavailable" | "machine_target_unavailable" | "machine_enrollment_inactive" | "variable_set_unavailable" | "rig_version_unavailable" | "scheduled_model_unavailable" | "insufficient_credits" | "allowance_exhausted" | "monthly_model_cost_limit" | "monthly_agent_run_limit";
3310
3317
  /**
3311
3318
  * Record a refused occurrence as an immutable run receipt under its producer
3312
3319
  * identity (migration 0539). A transient refusal is `skipped`, a terminal one
@@ -8295,16 +8302,24 @@ export declare function reapStaleLeaseHolders(db: Database, input: {
8295
8302
  drained: ReapDrainable[];
8296
8303
  }>;
8297
8304
  /** Settlement reasons for legacy retained commands that containment stopped
8298
- * after a verified checkpoint. Neither is exit proof: the command settles
8305
+ * after a verified checkpoint. None is exit proof: the command settles
8299
8306
  * `lost` with no exit code. */
8300
8307
  export declare const IDLE_COMMAND_CONTAINMENT_REASON = "idle_containment";
8301
8308
  export declare const DEADLINE_COMMAND_CONTAINMENT_REASON = "provider_deadline_containment";
8309
+ export declare const QUIESCENCE_COMMAND_CONTAINMENT_REASON = "quiescence_containment";
8310
+ type SettledCommandOwner = {
8311
+ sessionId: string;
8312
+ attemptId: string;
8313
+ temporalWorkflowId: string;
8314
+ temporalWorkflowRunId: string;
8315
+ temporalActivityId: string;
8316
+ };
8302
8317
  /** Bounded per-candidate containment inspection outcomes (metric labels). */
8303
- export type CommandContainmentInspection = "idle_enrolled" | "deadline_enrolled" | "resumed_enrolled" | "not_eligible" | "inspection_failed";
8318
+ export type CommandContainmentInspection = "idle_enrolled" | "deadline_enrolled" | "quiescence_enrolled" | "resumed_enrolled" | "not_eligible" | "inspection_failed";
8304
8319
  export type CommandContainmentEnrollment = ReapDrainable & {
8305
- /** `idle`/`deadline` newly enrolled this call; `resumed` continues an
8320
+ /** `idle`/`deadline`/`quiescence` newly enrolled this call; `resumed` continues an
8306
8321
  * existing enrollment whose drain has not committed yet. */
8307
- mode: "idle" | "deadline" | "resumed";
8322
+ mode: "idle" | "deadline" | "quiescence" | "resumed";
8308
8323
  };
8309
8324
  /** Pending machine input that would start a turn in an idle session, by the
8310
8325
  * wake class map: every immediate kind except terminal command results (held
@@ -8336,6 +8351,8 @@ export declare function enrollRetainedCommandContainment(db: Database, input: {
8336
8351
  sandboxGroupId: string;
8337
8352
  /** Omit to evaluate only the provider-deadline rule. */
8338
8353
  idleCommandContainmentMs?: number | undefined;
8354
+ /** Internal recovery authority: caller verified exact Temporal settlement. */
8355
+ settledOwner?: SettledCommandOwner;
8339
8356
  }): Promise<CommandContainmentEnrollment | null>;
8340
8357
  export declare function reapStaleLeaseHoldersGlobal(db: Database, input: {
8341
8358
  viewerHolderTtlMs: number;
@@ -10400,6 +10417,11 @@ export declare function updateSessionTitleWithEvent(db: Database, input: {
10400
10417
  title: string | null;
10401
10418
  events: SessionEvent[];
10402
10419
  }>;
10420
+ /** Read-only admission at a locked paused-to-active transition. */
10421
+ export type SessionGoalResumeValidation = (tx: Database, session: Pick<Session, "accountId" | "workspaceId" | "model" | "codexCompactionMode" | "latencyMode">, causalTurn: {
10422
+ id: string;
10423
+ initiatingHumanSubjectId: string | null;
10424
+ } | null) => Promise<void>;
10403
10425
  /**
10404
10426
  * Status transition helper. Idempotent: requesting the current status returns
10405
10427
  * `changed: false` so callers can skip emitting a duplicate event. `completed`
@@ -10417,6 +10439,8 @@ export declare function setSessionGoalStatus(db: Database, workspaceId: string,
10417
10439
  commandActor?: Extract<SessionCommandActor, {
10418
10440
  type: "agent_attempt";
10419
10441
  }>;
10442
+ /** Runs under the canonical control/session/goal locks, before a paused goal changes. */
10443
+ beforeResume?: SessionGoalResumeValidation;
10420
10444
  }): Promise<{
10421
10445
  goal: SessionGoal;
10422
10446
  changed: boolean;
@@ -10445,6 +10469,7 @@ export declare function setSessionGoalStatusWithEvent(db: Database, workspaceId:
10445
10469
  commandActor?: Extract<SessionCommandActor, {
10446
10470
  type: "agent_attempt";
10447
10471
  }>;
10472
+ beforeResume?: SessionGoalResumeValidation;
10448
10473
  event: SetSessionGoalStatusEvent;
10449
10474
  }): Promise<{
10450
10475
  goal: SessionGoal;
@@ -10459,7 +10484,7 @@ export type GoalContinuationDecision = {
10459
10484
  decision: "queue";
10460
10485
  } | {
10461
10486
  decision: "paused";
10462
- reason: "max_auto_continuations" | "limits" | "allowance";
10487
+ reason: "max_auto_continuations" | "limits" | GoalAdmissionPausedReason;
10463
10488
  goal: SessionGoal;
10464
10489
  } | {
10465
10490
  decision: "continue";
@@ -10479,7 +10504,7 @@ export declare function evaluateGoalContinuation(db: Database, input: {
10479
10504
  sessionId: string;
10480
10505
  defaultMaxAutoContinuations?: number | null;
10481
10506
  budgetBlocked?: string | null;
10482
- budgetPausedReason?: "limits" | "allowance" | undefined;
10507
+ budgetPausedReason?: "limits" | GoalAdmissionPausedReason | undefined;
10483
10508
  }): Promise<GoalContinuationDecision>;
10484
10509
  export declare const GOAL_IDLE_BACKOFF_WAKE_REASON = "goal_idle_backoff";
10485
10510
  /**
@@ -10526,7 +10551,7 @@ export declare function materializeGoalContinuation(db: Database, input: {
10526
10551
  workflowId: string;
10527
10552
  defaultMaxAutoContinuations?: number | null;
10528
10553
  budgetBlocked?: string | null;
10529
- budgetPausedReason?: "limits" | "allowance" | undefined;
10554
+ budgetPausedReason?: "limits" | GoalAdmissionPausedReason | undefined;
10530
10555
  /** Trusted worker admission, evaluated under the same session/goal locks
10531
10556
  * as lineage materialization. Never substitutes a mutable latest human. */
10532
10557
  admission?: (tx: Database, causalTurn: {
@@ -10534,7 +10559,7 @@ export declare function materializeGoalContinuation(db: Database, input: {
10534
10559
  initiatingHumanSubjectId: string | null;
10535
10560
  } | null) => Promise<{
10536
10561
  budgetBlocked: string | null;
10537
- budgetPausedReason?: "limits" | "allowance";
10562
+ budgetPausedReason?: "limits" | GoalAdmissionPausedReason;
10538
10563
  }>;
10539
10564
  idleBackoff?: GoalIdleBackoffPolicy | null;
10540
10565
  policy: {