@yolk-sdk/pool 0.1.0-canary.123 → 0.1.0-canary.124

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -18,11 +18,11 @@ It is the in-process counterpart of a CLIProxyAPI gateway with the
18
18
  ## Install
19
19
 
20
20
  ```bash
21
- pnpm add @yolk-sdk/pool@canary @yolk-sdk/agent@canary effect@4.0.5
21
+ pnpm add @yolk-sdk/pool@canary @yolk-sdk/agent@canary effect@4.1.2
22
22
  ```
23
23
 
24
24
  Canary APIs are unstable. Keep all `@yolk-sdk/*` packages on the same version.
25
- Use the SDK's matching Effect version (`4.0.5`) in host code.
25
+ Use the SDK's matching Effect version (`4.1.2`) in host code.
26
26
  Published package metadata requires Node.js 22+. The package is runtime-neutral.
27
27
 
28
28
  ## Subpaths
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { PoolAccountId, PoolAuthCooldown, PoolConfigError, PoolCooldown, PoolPin, PoolQuotaCooldown, PoolStoreError, PoolUnknownAccountError } from "./model.mjs";
2
1
  import { Pool, PoolAccount, PoolAccountState, PoolOptions, PoolState, defaultPoolMaxAgeMs, makePool, poolAuthCooldownMs, poolFailureCooldown, poolMaxCooldownMs, poolQuotaRetryAfterFloorMs } from "./pool.mjs";
2
+ import { PoolAccountId, PoolAuthCooldown, PoolConfigError, PoolCooldown, PoolPin, PoolQuotaCooldown, PoolStoreError, PoolUnknownAccountError } from "./model.mjs";
3
3
  import { PoolCandidate, PoolDecision, PoolRankInput, PoolReading, PoolWindow, isCooldownActive, poolReadingFromSnapshot, poolRoute, rankPool, staleQuotaCooldowns } from "./rank.mjs";
4
4
  export { type Pool, type PoolAccount, PoolAccountId, type PoolAccountState, PoolAuthCooldown, type PoolCandidate, PoolConfigError, PoolCooldown, PoolDecision, type PoolOptions, PoolPin, PoolQuotaCooldown, type PoolRankInput, type PoolReading, type PoolState, PoolStoreError, PoolUnknownAccountError, type PoolWindow, defaultPoolMaxAgeMs, isCooldownActive, makePool, poolAuthCooldownMs, poolFailureCooldown, poolMaxCooldownMs, poolQuotaRetryAfterFloorMs, poolReadingFromSnapshot, poolRoute, rankPool, staleQuotaCooldowns };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
+ import { defaultPoolMaxAgeMs, makePool, poolAuthCooldownMs, poolFailureCooldown, poolMaxCooldownMs, poolQuotaRetryAfterFloorMs } from "./pool.mjs";
1
2
  import { PoolAccountId, PoolAuthCooldown, PoolConfigError, PoolCooldown, PoolPin, PoolQuotaCooldown, PoolStoreError, PoolUnknownAccountError } from "./model.mjs";
2
3
  import { PoolDecision, isCooldownActive, poolReadingFromSnapshot, poolRoute, rankPool, staleQuotaCooldowns } from "./rank.mjs";
3
- import { defaultPoolMaxAgeMs, makePool, poolAuthCooldownMs, poolFailureCooldown, poolMaxCooldownMs, poolQuotaRetryAfterFloorMs } from "./pool.mjs";
4
4
  export { PoolAccountId, PoolAuthCooldown, PoolConfigError, PoolCooldown, PoolDecision, PoolPin, PoolQuotaCooldown, PoolStoreError, PoolUnknownAccountError, defaultPoolMaxAgeMs, isCooldownActive, makePool, poolAuthCooldownMs, poolFailureCooldown, poolMaxCooldownMs, poolQuotaRetryAfterFloorMs, poolReadingFromSnapshot, poolRoute, rankPool, staleQuotaCooldowns };
package/dist/model.d.mts CHANGED
@@ -1,8 +1,6 @@
1
1
  import * as Schema from "effect/Schema";
2
-
3
- //#region src/model.d.ts
4
2
  /** A host-chosen account id, unique within the accounts a store serves. */
5
- declare const PoolAccountId: Schema.Trimmed;
3
+ export declare const PoolAccountId: Schema.Trimmed;
6
4
  declare const PoolPin_base: Schema.Class<PoolPin, Schema.Struct<{
7
5
  readonly poolId: Schema.Trimmed;
8
6
  readonly route: Schema.Trimmed;
@@ -13,46 +11,45 @@ declare const PoolPin_base: Schema.Class<PoolPin, Schema.Struct<{
13
11
  * The account currently serving one route of a pool. A route is a lowercased model id, so every
14
12
  * reasoning effort of a model shares one pin.
15
13
  */
16
- declare class PoolPin extends PoolPin_base {}
14
+ export declare class PoolPin extends PoolPin_base {}
17
15
  declare const PoolQuotaCooldown_base: Schema.Class<PoolQuotaCooldown, Schema.TaggedStruct<"PoolQuotaCooldown", {
18
16
  readonly accountId: Schema.Trimmed;
19
17
  readonly startedAtMs: Schema.Finite;
20
18
  readonly untilMs: Schema.Finite;
21
19
  }>, {}>;
22
20
  /** The account's subscription quota is exhausted until `untilMs` (a provider quota reset). */
23
- declare class PoolQuotaCooldown extends PoolQuotaCooldown_base {}
21
+ export declare class PoolQuotaCooldown extends PoolQuotaCooldown_base {}
24
22
  declare const PoolAuthCooldown_base: Schema.Class<PoolAuthCooldown, Schema.TaggedStruct<"PoolAuthCooldown", {
25
23
  readonly accountId: Schema.Trimmed;
26
24
  readonly startedAtMs: Schema.Finite;
27
25
  readonly untilMs: Schema.Finite;
28
26
  }>, {}>;
29
27
  /** The provider rejected the account's credentials; it sits out until `untilMs`. */
30
- declare class PoolAuthCooldown extends PoolAuthCooldown_base {}
28
+ export declare class PoolAuthCooldown extends PoolAuthCooldown_base {}
31
29
  /**
32
30
  * Why an account sits out. One cooldown per account: writing keeps the one that ends later (on a
33
31
  * tie, the one that started later; see `PoolStoreApi.putCooldown`), and a reading can clear only a
34
32
  * quota cooldown it postdates.
35
33
  */
36
- declare const PoolCooldown: Schema.Union<readonly [typeof PoolQuotaCooldown, typeof PoolAuthCooldown]>;
37
- type PoolCooldown = typeof PoolCooldown.Type;
34
+ export declare const PoolCooldown: Schema.Union<readonly [typeof PoolQuotaCooldown, typeof PoolAuthCooldown]>;
35
+ export type PoolCooldown = typeof PoolCooldown.Type;
38
36
  declare const PoolStoreError_base: Schema.Class<PoolStoreError, Schema.TaggedStruct<"PoolStoreError", {
39
37
  readonly operation: Schema.Literals<readonly ["load", "movePin", "putCooldown", "clearCooldown", "putReading", "state"]>;
40
38
  readonly message: Schema.String;
41
39
  }>, import("effect/Cause").YieldableError>;
42
40
  /** A pool store operation failed. The pool degrades to stateless fill-first routing on reads. */
43
- declare class PoolStoreError extends PoolStoreError_base {}
41
+ export declare class PoolStoreError extends PoolStoreError_base {}
44
42
  declare const PoolConfigError_base: Schema.Class<PoolConfigError, Schema.TaggedStruct<"PoolConfigError", {
45
43
  readonly reason: Schema.Literals<readonly ["invalid_pool_id", "no_accounts", "invalid_account_id", "duplicate_account_id", "invalid_priority", "invalid_max_age", "invalid_reserve_percent"]>;
46
44
  readonly message: Schema.String;
47
45
  }>, import("effect/Cause").YieldableError>;
48
46
  /** `makePool` rejected its configuration. */
49
- declare class PoolConfigError extends PoolConfigError_base {}
47
+ export declare class PoolConfigError extends PoolConfigError_base {}
50
48
  declare const PoolUnknownAccountError_base: Schema.Class<PoolUnknownAccountError, Schema.TaggedStruct<"PoolUnknownAccountError", {
51
49
  readonly poolId: Schema.String;
52
50
  readonly accountId: Schema.String;
53
51
  }>, import("effect/Cause").YieldableError>;
54
52
  /** A reading or lookup named an account the pool was not configured with. */
55
- declare class PoolUnknownAccountError extends PoolUnknownAccountError_base {}
56
- //#endregion
57
- export { PoolAccountId, PoolAuthCooldown, PoolConfigError, PoolCooldown, PoolPin, PoolQuotaCooldown, PoolStoreError, PoolUnknownAccountError };
53
+ export declare class PoolUnknownAccountError extends PoolUnknownAccountError_base {}
54
+ export {};
58
55
  //# sourceMappingURL=model.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"model.d.mts","names":[],"sources":["../src/model.ts"],"mappings":";;;;cAOa,aAAA,EAAa,MAAA,CAAA,OAAwB;AAAA,cAAA,YAAA;;;;;;;;;;cAMrC,OAAA,SAAgB,YAK3B;AAAA,cAAG,sBAAA;;;;;;cAGQ,iBAAA,SAA0B,sBAOtC;AAAA,cAAG,qBAAA;;;;;;cAGS,gBAAA,SAAyB,qBAIpC;;;;;;cAOW,YAAA,EAAY,MAAA,CAAA,KAAA,kBAAA,iBAAA,SAAA,gBAAA;AAAA,KAEb,YAAA,UAAsB,YAAA,CAAa,IAAI;AAAA,cAAA,mBAAA;;;;;cAGtC,cAAA,SAAuB,mBAUlC;AAAA,cAAG,oBAAA;;;;;cAGQ,eAAA,SAAwB,oBAWnC;AAAA,cAAG,4BAAA;;;;;cAGQ,uBAAA,SAAgC,4BAM5C"}
1
+ {"version":3,"file":"model.d.mts","names":[],"sources":["../src/model.ts"],"mappings":";;qBAOa,eAAa,OAAA;;;;;;;;;;;qBAMb,gBAAgB;;;;;;;qBAQhB,0BAA0B;;;;;;;qBAU1B,yBAAyB;;;;;;qBAWzB,cAAY,OAAA,uBAAA,0BAAA;YAEb,sBAAsB,aAAa;;;;;;qBAGlC,uBAAuB;;;;;;qBAavB,wBAAwB;;;;;;qBAcxB,gCAAgC"}
package/dist/model.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as Schema from "effect/Schema";
2
- //#region src/model.ts
2
+ // src/model.ts
3
3
  const NonEmptyTrimmedString = Schema.Trimmed.pipe(Schema.check(Schema.isNonEmpty()));
4
4
  const EpochMs = Schema.Finite;
5
5
  /** A host-chosen account id, unique within the accounts a store serves. */
@@ -62,7 +62,6 @@ var PoolUnknownAccountError = class extends Schema.TaggedError()("PoolUnknownAcc
62
62
  poolId: Schema.String,
63
63
  accountId: Schema.String
64
64
  }) {};
65
- //#endregion
66
65
  export { PoolAccountId, PoolAuthCooldown, PoolConfigError, PoolCooldown, PoolPin, PoolQuotaCooldown, PoolStoreError, PoolUnknownAccountError };
67
66
 
68
67
  //# sourceMappingURL=model.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"model.mjs","names":[],"sources":["../src/model.ts"],"sourcesContent":["import * as Schema from 'effect/Schema'\n\nconst NonEmptyTrimmedString = Schema.Trimmed.pipe(Schema.check(Schema.isNonEmpty()))\n\nconst EpochMs = Schema.Finite\n\n/** A host-chosen account id, unique within the accounts a store serves. */\nexport const PoolAccountId = NonEmptyTrimmedString\n\n/**\n * The account currently serving one route of a pool. A route is a lowercased model id, so every\n * reasoning effort of a model shares one pin.\n */\nexport class PoolPin extends Schema.Class<PoolPin>('PoolPin')({\n poolId: NonEmptyTrimmedString,\n route: NonEmptyTrimmedString,\n accountId: PoolAccountId,\n pinnedAtMs: EpochMs\n}) {}\n\n/** The account's subscription quota is exhausted until `untilMs` (a provider quota reset). */\nexport class PoolQuotaCooldown extends Schema.TaggedClass<PoolQuotaCooldown>()(\n 'PoolQuotaCooldown',\n {\n accountId: PoolAccountId,\n startedAtMs: EpochMs,\n untilMs: EpochMs\n }\n) {}\n\n/** The provider rejected the account's credentials; it sits out until `untilMs`. */\nexport class PoolAuthCooldown extends Schema.TaggedClass<PoolAuthCooldown>()('PoolAuthCooldown', {\n accountId: PoolAccountId,\n startedAtMs: EpochMs,\n untilMs: EpochMs\n}) {}\n\n/**\n * Why an account sits out. One cooldown per account: writing keeps the one that ends later (on a\n * tie, the one that started later; see `PoolStoreApi.putCooldown`), and a reading can clear only a\n * quota cooldown it postdates.\n */\nexport const PoolCooldown = Schema.Union([PoolQuotaCooldown, PoolAuthCooldown])\n\nexport type PoolCooldown = typeof PoolCooldown.Type\n\n/** A pool store operation failed. The pool degrades to stateless fill-first routing on reads. */\nexport class PoolStoreError extends Schema.TaggedError<PoolStoreError>()('PoolStoreError', {\n operation: Schema.Literals([\n 'load',\n 'movePin',\n 'putCooldown',\n 'clearCooldown',\n 'putReading',\n 'state'\n ]),\n message: Schema.String\n}) {}\n\n/** `makePool` rejected its configuration. */\nexport class PoolConfigError extends Schema.TaggedError<PoolConfigError>()('PoolConfigError', {\n reason: Schema.Literals([\n 'invalid_pool_id',\n 'no_accounts',\n 'invalid_account_id',\n 'duplicate_account_id',\n 'invalid_priority',\n 'invalid_max_age',\n 'invalid_reserve_percent'\n ]),\n message: Schema.String\n}) {}\n\n/** A reading or lookup named an account the pool was not configured with. */\nexport class PoolUnknownAccountError extends Schema.TaggedError<PoolUnknownAccountError>()(\n 'PoolUnknownAccountError',\n {\n poolId: Schema.String,\n accountId: Schema.String\n }\n) {}\n"],"mappings":";;AAEA,MAAM,wBAAwB,OAAO,QAAQ,KAAK,OAAO,MAAM,OAAO,WAAW,CAAC,CAAC;AAEnF,MAAM,UAAU,OAAO;;AAGvB,MAAa,gBAAgB;;;;;AAM7B,IAAa,UAAb,cAA6B,OAAO,MAAe,SAAS,EAAE;CAC5D,QAAQ;CACR,OAAO;CACP,WAAW;CACX,YAAY;AACd,CAAC,EAAE,CAAC;;AAGJ,IAAa,oBAAb,cAAuC,OAAO,YAA+B,EAC3E,qBACA;CACE,WAAW;CACX,aAAa;CACb,SAAS;AACX,CACF,EAAE,CAAC;;AAGH,IAAa,mBAAb,cAAsC,OAAO,YAA8B,EAAE,oBAAoB;CAC/F,WAAW;CACX,aAAa;CACb,SAAS;AACX,CAAC,EAAE,CAAC;;;;;;AAOJ,MAAa,eAAe,OAAO,MAAM,CAAC,mBAAmB,gBAAgB,CAAC;;AAK9E,IAAa,iBAAb,cAAoC,OAAO,YAA4B,EAAE,kBAAkB;CACzF,WAAW,OAAO,SAAS;EACzB;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CACD,SAAS,OAAO;AAClB,CAAC,EAAE,CAAC;;AAGJ,IAAa,kBAAb,cAAqC,OAAO,YAA6B,EAAE,mBAAmB;CAC5F,QAAQ,OAAO,SAAS;EACtB;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CACD,SAAS,OAAO;AAClB,CAAC,EAAE,CAAC;;AAGJ,IAAa,0BAAb,cAA6C,OAAO,YAAqC,EACvF,2BACA;CACE,QAAQ,OAAO;CACf,WAAW,OAAO;AACpB,CACF,EAAE,CAAC"}
1
+ {"version":3,"file":"model.mjs","names":[],"sources":["../src/model.ts"],"sourcesContent":["import * as Schema from 'effect/Schema'\n\nconst NonEmptyTrimmedString = Schema.Trimmed.pipe(Schema.check(Schema.isNonEmpty()))\n\nconst EpochMs = Schema.Finite\n\n/** A host-chosen account id, unique within the accounts a store serves. */\nexport const PoolAccountId = NonEmptyTrimmedString\n\n/**\n * The account currently serving one route of a pool. A route is a lowercased model id, so every\n * reasoning effort of a model shares one pin.\n */\nexport class PoolPin extends Schema.Class<PoolPin>('PoolPin')({\n poolId: NonEmptyTrimmedString,\n route: NonEmptyTrimmedString,\n accountId: PoolAccountId,\n pinnedAtMs: EpochMs\n}) {}\n\n/** The account's subscription quota is exhausted until `untilMs` (a provider quota reset). */\nexport class PoolQuotaCooldown extends Schema.TaggedClass<PoolQuotaCooldown>()(\n 'PoolQuotaCooldown',\n {\n accountId: PoolAccountId,\n startedAtMs: EpochMs,\n untilMs: EpochMs\n }\n) {}\n\n/** The provider rejected the account's credentials; it sits out until `untilMs`. */\nexport class PoolAuthCooldown extends Schema.TaggedClass<PoolAuthCooldown>()('PoolAuthCooldown', {\n accountId: PoolAccountId,\n startedAtMs: EpochMs,\n untilMs: EpochMs\n}) {}\n\n/**\n * Why an account sits out. One cooldown per account: writing keeps the one that ends later (on a\n * tie, the one that started later; see `PoolStoreApi.putCooldown`), and a reading can clear only a\n * quota cooldown it postdates.\n */\nexport const PoolCooldown = Schema.Union([PoolQuotaCooldown, PoolAuthCooldown])\n\nexport type PoolCooldown = typeof PoolCooldown.Type\n\n/** A pool store operation failed. The pool degrades to stateless fill-first routing on reads. */\nexport class PoolStoreError extends Schema.TaggedError<PoolStoreError>()('PoolStoreError', {\n operation: Schema.Literals([\n 'load',\n 'movePin',\n 'putCooldown',\n 'clearCooldown',\n 'putReading',\n 'state'\n ]),\n message: Schema.String\n}) {}\n\n/** `makePool` rejected its configuration. */\nexport class PoolConfigError extends Schema.TaggedError<PoolConfigError>()('PoolConfigError', {\n reason: Schema.Literals([\n 'invalid_pool_id',\n 'no_accounts',\n 'invalid_account_id',\n 'duplicate_account_id',\n 'invalid_priority',\n 'invalid_max_age',\n 'invalid_reserve_percent'\n ]),\n message: Schema.String\n}) {}\n\n/** A reading or lookup named an account the pool was not configured with. */\nexport class PoolUnknownAccountError extends Schema.TaggedError<PoolUnknownAccountError>()(\n 'PoolUnknownAccountError',\n {\n poolId: Schema.String,\n accountId: Schema.String\n }\n) {}\n"],"mappings":";;AAEA,MAAM,wBAAwB,OAAO,QAAQ,KAAK,OAAO,MAAM,OAAO,WAAW,CAAC,CAAC;AAEnF,MAAM,UAAU,OAAO;;AAGvB,MAAa,gBAAgB;;;;;AAM7B,IAAa,UAAb,cAA6B,OAAO,MAAe,SAAS,CAAC,CAAC;CAC5D,QAAQ;CACR,OAAO;CACP,WAAW;CACX,YAAY;AACd,CAAC,CAAC,CAAC,CAAC;;AAGJ,IAAa,oBAAb,cAAuC,OAAO,YAA+B,CAAC,CAC5E,qBACA;CACE,WAAW;CACX,aAAa;CACb,SAAS;AACX,CACF,CAAC,CAAC,CAAC;;AAGH,IAAa,mBAAb,cAAsC,OAAO,YAA8B,CAAC,CAAC,oBAAoB;CAC/F,WAAW;CACX,aAAa;CACb,SAAS;AACX,CAAC,CAAC,CAAC,CAAC;;;;;;AAOJ,MAAa,eAAe,OAAO,MAAM,CAAC,mBAAmB,gBAAgB,CAAC;;AAK9E,IAAa,iBAAb,cAAoC,OAAO,YAA4B,CAAC,CAAC,kBAAkB;CACzF,WAAW,OAAO,SAAS;EACzB;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CACD,SAAS,OAAO;AAClB,CAAC,CAAC,CAAC,CAAC;;AAGJ,IAAa,kBAAb,cAAqC,OAAO,YAA6B,CAAC,CAAC,mBAAmB;CAC5F,QAAQ,OAAO,SAAS;EACtB;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CAAC;CACD,SAAS,OAAO;AAClB,CAAC,CAAC,CAAC,CAAC;;AAGJ,IAAa,0BAAb,cAA6C,OAAO,YAAqC,CAAC,CACxF,2BACA;CACE,QAAQ,OAAO;CACf,WAAW,OAAO;AACpB,CACF,CAAC,CAAC,CAAC"}
package/dist/pool.d.mts CHANGED
@@ -3,57 +3,66 @@ import { PoolStoreApi } from "./store.mjs";
3
3
  import { Effect } from "effect";
4
4
  import { LLMError, LLMProvider } from "@yolk-sdk/agent/loop";
5
5
  import { ProviderSubscriptionUsageSnapshot } from "@yolk-sdk/agent/providers/subscription-usage";
6
-
7
- //#region src/pool.d.ts
8
6
  type LLMProviderService = typeof LLMProvider.Service;
9
7
  /** One subscription account of a pool: its own provider, built by the host with its credentials. */
10
- type PoolAccount = {
11
- /** Unique within the store; cooldowns and readings are keyed by it. */readonly id: string; /** Breaks ranking ties and orders fill-first (higher first). Default 0. */
8
+ export type PoolAccount = {
9
+ /** Unique within the store; cooldowns and readings are keyed by it. */
10
+ readonly id: string;
11
+ /** Breaks ranking ties and orders fill-first (higher first). Default 0. */
12
12
  readonly priority?: number;
13
13
  readonly provider: LLMProviderService;
14
14
  };
15
- type PoolOptions = {
16
- /** Pins are keyed by pool id and route. */readonly id: string; /** Accounts of one provider; requests must suit every one of them. */
15
+ export type PoolOptions = {
16
+ /** Pins are keyed by pool id and route. */
17
+ readonly id: string;
18
+ /** Accounts of one provider; requests must suit every one of them. */
17
19
  readonly accounts: ReadonlyArray<PoolAccount>;
18
- readonly store: PoolStoreApi; /** How old a reading may be and still rank accounts. Default 30 minutes. */
19
- readonly maxAgeMs?: number; /** Quota left untouched on every window, in percent (0 to below 100). Default 0. */
20
+ readonly store: PoolStoreApi;
21
+ /** How old a reading may be and still rank accounts. Default 30 minutes. */
22
+ readonly maxAgeMs?: number;
23
+ /** Quota left untouched on every window, in percent (0 to below 100). Default 0. */
20
24
  readonly reservePercent?: number;
21
25
  };
22
26
  /** One account as the host UI sees it. */
23
- type PoolAccountState = {
27
+ export type PoolAccountState = {
24
28
  readonly id: string;
25
- readonly priority: number; /** The account's cooldown while it is active. */
29
+ readonly priority: number;
30
+ /** The account's cooldown while it is active. */
26
31
  readonly cooldown: PoolCooldown | undefined;
27
32
  readonly reading: ProviderSubscriptionUsageSnapshot | undefined;
28
33
  };
29
- type PoolState = {
30
- readonly poolId: string; /** The account serving each route. */
34
+ export type PoolState = {
35
+ readonly poolId: string;
36
+ /** The account serving each route. */
31
37
  readonly pins: ReadonlyArray<PoolPin>;
32
38
  readonly accounts: ReadonlyArray<PoolAccountState>;
33
39
  };
34
- type Pool = {
35
- readonly id: string; /** The pooled provider: provide it as the loop's `LLMProvider`, innermost under host wrappers. */
36
- readonly provider: LLMProviderService; /** Records an account's usage snapshot; hosts poll usage on their own schedule. */
37
- readonly recordReading: (accountId: string, snapshot: ProviderSubscriptionUsageSnapshot) => Effect.Effect<void, PoolUnknownAccountError | PoolStoreError>; /** Pins, active cooldowns, and readings for host UIs. */
40
+ export type Pool = {
41
+ readonly id: string;
42
+ /** The pooled provider: provide it as the loop's `LLMProvider`, innermost under host wrappers. */
43
+ readonly provider: LLMProviderService;
44
+ /** Records an account's usage snapshot; hosts poll usage on their own schedule. */
45
+ readonly recordReading: (accountId: string, snapshot: ProviderSubscriptionUsageSnapshot) => Effect.Effect<void, PoolUnknownAccountError | PoolStoreError>;
46
+ /** Pins, active cooldowns, and readings for host UIs. */
38
47
  readonly state: Effect.Effect<PoolState, PoolStoreError>;
39
48
  };
40
49
  /** Default `maxAgeMs`: readings older than 30 minutes no longer rank accounts. */
41
- declare const defaultPoolMaxAgeMs: number;
50
+ export declare const defaultPoolMaxAgeMs: number;
42
51
  /**
43
52
  * A `rate_limit` whose `Retry-After` reaches this is a quota lockout, not a transient limit
44
53
  * (CLIProxyAPI `expiring-first` `QuotaCooldownFloor`).
45
54
  */
46
- declare const poolQuotaRetryAfterFloorMs: number;
55
+ export declare const poolQuotaRetryAfterFloorMs: number;
47
56
  /** The longest cooldown the pool records; a later or non-finite hint is not trusted. */
48
- declare const poolMaxCooldownMs: number;
57
+ export declare const poolMaxCooldownMs: number;
49
58
  /** How long an account whose credentials were rejected sits out. */
50
- declare const poolAuthCooldownMs: number;
59
+ export declare const poolAuthCooldownMs: number;
51
60
  /**
52
61
  * The cooldown an account-scoped failure puts its account in, or undefined when the failure is not
53
62
  * the account's (the loop's retry handles those): a `rate_limit` with a quota reset or a
54
63
  * `Retry-After` of at least `poolQuotaRetryAfterFloorMs` (quota), or `auth`.
55
64
  */
56
- declare const poolFailureCooldown: (error: LLMError, accountId: string, nowMs: number) => PoolCooldown | undefined;
65
+ export declare const poolFailureCooldown: (error: LLMError, accountId: string, nowMs: number) => PoolCooldown | undefined;
57
66
  /**
58
67
  * Pools several subscription accounts of one provider behind one `LLMProvider` (ADR 0011).
59
68
  *
@@ -67,7 +76,6 @@ declare const poolFailureCooldown: (error: LLMError, accountId: string, nowMs: n
67
76
  * Store failures never fail a request: reads fall back to stateless fill-first and writes are
68
77
  * logged.
69
78
  */
70
- declare const makePool: (options: PoolOptions) => Effect.Effect<Pool, PoolConfigError>;
71
- //#endregion
72
- export { Pool, PoolAccount, PoolAccountState, PoolOptions, PoolState, defaultPoolMaxAgeMs, makePool, poolAuthCooldownMs, poolFailureCooldown, poolMaxCooldownMs, poolQuotaRetryAfterFloorMs };
79
+ export declare const makePool: (options: PoolOptions) => Effect.Effect<Pool, PoolConfigError>;
80
+ export {};
73
81
  //# sourceMappingURL=pool.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"pool.d.mts","names":[],"sources":["../src/pool.ts"],"mappings":";;;;;;;KA+BK,kBAAA,UAA4B,WAAA,CAAY,OAAO;;KAGxC,WAAA;EAHW,gFAKZ,EAAA,UALsB;EAAA,SAOtB,QAAA;EAAA,SACA,QAAA,EAAU,kBAAkB;AAAA;AAAA,KAG3B,WAAA;EAH2B,oDAK5B,EAAA,UANA;EAAA,SAQA,QAAA,EAAU,aAAA,CAAc,WAAA;EAAA,SACxB,KAAA,EAAO,YAAA,EARqB;EAAA,SAU5B,QAAA,WAPC;EAAA,SASD,cAAA;AAAA;;KAIC,gBAAA;EAAA,SACD,EAAA;EAAA,SACA,QAAA,UAVmB;EAAA,SAYnB,QAAA,EAAU,YAAA;EAAA,SACV,OAAA,EAAS,iCAAiC;AAAA;AAAA,KAGzC,SAAA;EAAA,SACD,MAAA,UAjBO;EAAA,SAmBP,IAAA,EAAM,aAAA,CAAc,OAAA;EAAA,SACpB,QAAA,EAAU,aAAA,CAAc,gBAAA;AAAA;AAAA,KAGvB,IAAA;EAAA,SACD,EAAA,UAhBiB;EAAA,SAkBjB,QAAA,EAAU,kBAAA,EAbgC;EAAA,SAe1C,aAAA,GACP,SAAA,UACA,QAAA,EAAU,iCAAA,KACP,MAAA,CAAO,MAAA,OAAa,uBAAA,GAA0B,cAAA,GArB1C;EAAA,SAuBA,KAAA,EAAO,MAAA,CAAO,MAAA,CAAO,SAAA,EAAW,cAAA;AAAA;;cAI9B,mBAAA;;AAxBwC;AAGrD;;cA2Ba,0BAAA;;cAGA,iBAAA;;cAGA,kBAAA;;;;;;cAYA,mBAAA,GACX,KAAA,EAAO,QAAA,EACP,SAAA,UACA,KAAA,aACC,YAAY;;;;;AA7CoC;AAGnD;;;;;;;;cA+Ga,QAAA,GAAY,OAAA,EAAS,WAAA,KAAc,MAAA,CAAO,MAAA,CAAO,IAAA,EAAM,eAAA"}
1
+ {"version":3,"file":"pool.d.mts","names":[],"sources":["../src/pool.ts"],"mappings":";;;;;KA+BK,4BAA4B,YAAY;;YAGjC;;WAED;;WAEA;WACA,UAAU;;YAGT;;WAED;;WAEA,UAAU,cAAc;WACxB,OAAO;;WAEP;;WAEA;;;YAIC;WACD;WACA;;WAEA,UAAU;WACV,SAAS;;YAGR;WACD;;WAEA,MAAM,cAAc;WACpB,UAAU,cAAc;;YAGvB;WACD;;WAEA,UAAU;;WAEV,gBACP,mBACA,UAAU,sCACP,OAAO,aAAa,0BAA0B;;WAE1C,OAAO,OAAO,OAAO,WAAW;;;qBAI9B;;;;;qBAMA;;qBAGA;;qBAGA;;;;;;qBAYA,sBAAmB,OACvB,UAAQ,mBACE,kBAEhB;;;;;;;;;;;;;;qBAqEU,WAAQ,SAAa,gBAAc,OAAO,OAAO,MAAM"}
package/dist/pool.mjs CHANGED
@@ -4,18 +4,18 @@ import { PoolStoredReading } from "./store.mjs";
4
4
  import { Clock, Effect, Match, Predicate, Ref, Stream } from "effect";
5
5
  import { LLMError } from "@yolk-sdk/agent/loop";
6
6
  import { ProviderErrorInfo } from "@yolk-sdk/agent/protocol";
7
- //#region src/pool.ts
7
+ // src/pool.ts
8
8
  /** Default `maxAgeMs`: readings older than 30 minutes no longer rank accounts. */
9
- const defaultPoolMaxAgeMs = 30 * 6e4;
9
+ const defaultPoolMaxAgeMs = 18e5;
10
10
  /**
11
11
  * A `rate_limit` whose `Retry-After` reaches this is a quota lockout, not a transient limit
12
12
  * (CLIProxyAPI `expiring-first` `QuotaCooldownFloor`).
13
13
  */
14
- const poolQuotaRetryAfterFloorMs = 30 * 6e4;
14
+ const poolQuotaRetryAfterFloorMs = 18e5;
15
15
  /** The longest cooldown the pool records; a later or non-finite hint is not trusted. */
16
- const poolMaxCooldownMs = 366 * 24 * 60 * 6e4;
16
+ const poolMaxCooldownMs = 316224e5;
17
17
  /** How long an account whose credentials were rejected sits out. */
18
- const poolAuthCooldownMs = 30 * 6e4;
18
+ const poolAuthCooldownMs = 18e5;
19
19
  const configError = (reason, message) => Effect.fail(new PoolConfigError({
20
20
  reason,
21
21
  message
@@ -175,7 +175,7 @@ const makePool = (options) => Effect.gen(function* () {
175
175
  return pick({
176
176
  request: input.request,
177
177
  route: input.route,
178
- excluded: new Set([...input.excluded, input.account.id]),
178
+ excluded: /* @__PURE__ */ new Set([...input.excluded, input.account.id]),
179
179
  recorded: [...input.recorded, cooldown],
180
180
  lastError: error
181
181
  });
@@ -195,9 +195,10 @@ const makePool = (options) => Effect.gen(function* () {
195
195
  startedAtMs: cooldown.startedAtMs
196
196
  }).pipe(Effect.flatMap((cleared) => cleared ? Effect.logInfo(`pool: cleared cooldown (until ${iso(cooldown.untilMs)}): a newer reading shows quota left`).pipe(Effect.annotateLogs({ account: cooldown.accountId })) : Effect.void), Effect.catch(logStoreFailure("clearCooldown"))), { discard: true });
197
197
  const cooldowns = [...snapshot.cooldowns.filter((cooldown) => isCooldownActive(cooldown, nowMs) && !stale.includes(cooldown)), ...input.recorded.filter((cooldown) => isCooldownActive(cooldown, nowMs))];
198
+ const unavailable = /* @__PURE__ */ new Set([...input.excluded, ...cooldowns.map((cooldown) => cooldown.accountId)]);
198
199
  const decision = rankPool({
199
200
  accounts,
200
- unavailable: new Set([...input.excluded, ...cooldowns.map((cooldown) => cooldown.accountId)]),
201
+ unavailable,
201
202
  readings,
202
203
  pin: snapshot.pin?.accountId,
203
204
  route: input.route,
@@ -284,7 +285,6 @@ const makePool = (options) => Effect.gen(function* () {
284
285
  })
285
286
  };
286
287
  });
287
- //#endregion
288
288
  export { defaultPoolMaxAgeMs, makePool, poolAuthCooldownMs, poolFailureCooldown, poolMaxCooldownMs, poolQuotaRetryAfterFloorMs };
289
289
 
290
290
  //# sourceMappingURL=pool.mjs.map
package/dist/pool.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"pool.mjs","names":[],"sources":["../src/pool.ts"],"sourcesContent":["import { Clock, Effect, Match, Predicate, Ref, Stream } from 'effect'\nimport {\n LLMError,\n type LLMEvent,\n type LLMProvider,\n type LLMProviderError,\n type LLMRequest\n} from '@yolk-sdk/agent/loop'\nimport { ProviderErrorInfo } from '@yolk-sdk/agent/protocol'\nimport type { ProviderSubscriptionUsageSnapshot } from '@yolk-sdk/agent/providers/subscription-usage'\nimport {\n PoolAuthCooldown,\n PoolConfigError,\n type PoolCooldown,\n PoolPin,\n PoolQuotaCooldown,\n type PoolStoreError,\n PoolUnknownAccountError\n} from './model.ts'\nimport {\n isCooldownActive,\n type PoolCandidate,\n type PoolDecision,\n type PoolReading,\n poolReadingFromSnapshot,\n poolRoute,\n rankPool,\n staleQuotaCooldowns\n} from './rank.ts'\nimport { type PoolStoreApi, type PoolStoreSnapshot, PoolStoredReading } from './store.ts'\n\ntype LLMProviderService = typeof LLMProvider.Service\n\n/** One subscription account of a pool: its own provider, built by the host with its credentials. */\nexport type PoolAccount = {\n /** Unique within the store; cooldowns and readings are keyed by it. */\n readonly id: string\n /** Breaks ranking ties and orders fill-first (higher first). Default 0. */\n readonly priority?: number\n readonly provider: LLMProviderService\n}\n\nexport type PoolOptions = {\n /** Pins are keyed by pool id and route. */\n readonly id: string\n /** Accounts of one provider; requests must suit every one of them. */\n readonly accounts: ReadonlyArray<PoolAccount>\n readonly store: PoolStoreApi\n /** How old a reading may be and still rank accounts. Default 30 minutes. */\n readonly maxAgeMs?: number\n /** Quota left untouched on every window, in percent (0 to below 100). Default 0. */\n readonly reservePercent?: number\n}\n\n/** One account as the host UI sees it. */\nexport type PoolAccountState = {\n readonly id: string\n readonly priority: number\n /** The account's cooldown while it is active. */\n readonly cooldown: PoolCooldown | undefined\n readonly reading: ProviderSubscriptionUsageSnapshot | undefined\n}\n\nexport type PoolState = {\n readonly poolId: string\n /** The account serving each route. */\n readonly pins: ReadonlyArray<PoolPin>\n readonly accounts: ReadonlyArray<PoolAccountState>\n}\n\nexport type Pool = {\n readonly id: string\n /** The pooled provider: provide it as the loop's `LLMProvider`, innermost under host wrappers. */\n readonly provider: LLMProviderService\n /** Records an account's usage snapshot; hosts poll usage on their own schedule. */\n readonly recordReading: (\n accountId: string,\n snapshot: ProviderSubscriptionUsageSnapshot\n ) => Effect.Effect<void, PoolUnknownAccountError | PoolStoreError>\n /** Pins, active cooldowns, and readings for host UIs. */\n readonly state: Effect.Effect<PoolState, PoolStoreError>\n}\n\n/** Default `maxAgeMs`: readings older than 30 minutes no longer rank accounts. */\nexport const defaultPoolMaxAgeMs = 30 * 60_000\n\n/**\n * A `rate_limit` whose `Retry-After` reaches this is a quota lockout, not a transient limit\n * (CLIProxyAPI `expiring-first` `QuotaCooldownFloor`).\n */\nexport const poolQuotaRetryAfterFloorMs = 30 * 60_000\n\n/** The longest cooldown the pool records; a later or non-finite hint is not trusted. */\nexport const poolMaxCooldownMs = 366 * 24 * 60 * 60_000\n\n/** How long an account whose credentials were rejected sits out. */\nexport const poolAuthCooldownMs = 30 * 60_000\n\ntype ValidAccount = PoolCandidate & { readonly provider: LLMProviderService }\n\nconst configError = (reason: PoolConfigError['reason'], message: string) =>\n Effect.fail(new PoolConfigError({ reason, message }))\n\n/**\n * The cooldown an account-scoped failure puts its account in, or undefined when the failure is not\n * the account's (the loop's retry handles those): a `rate_limit` with a quota reset or a\n * `Retry-After` of at least `poolQuotaRetryAfterFloorMs` (quota), or `auth`.\n */\nexport const poolFailureCooldown = (\n error: LLMError,\n accountId: string,\n nowMs: number\n): PoolCooldown | undefined => {\n const info = error.provider\n\n if (info === undefined) {\n return undefined\n }\n\n if (info.kind === 'auth') {\n return PoolAuthCooldown.make({\n accountId,\n startedAtMs: nowMs,\n untilMs: nowMs + poolAuthCooldownMs\n })\n }\n\n if (info.kind !== 'rate_limit') {\n return undefined\n }\n\n const trusted = (untilMs: number) =>\n Number.isFinite(untilMs) && untilMs > nowMs && untilMs - nowMs <= poolMaxCooldownMs\n\n if (info.quotaResetsAtMs !== undefined && trusted(info.quotaResetsAtMs)) {\n return PoolQuotaCooldown.make({ accountId, startedAtMs: nowMs, untilMs: info.quotaResetsAtMs })\n }\n\n if (\n info.retryAfterMs !== undefined &&\n info.retryAfterMs >= poolQuotaRetryAfterFloorMs &&\n trusted(nowMs + info.retryAfterMs)\n ) {\n return PoolQuotaCooldown.make({\n accountId,\n startedAtMs: nowMs,\n untilMs: nowMs + info.retryAfterMs\n })\n }\n\n return undefined\n}\n\nconst iso = (ms: number) => {\n const date = new Date(ms)\n\n return Number.isFinite(date.getTime()) ? date.toISOString() : String(ms)\n}\n\nconst minUntil = (cooldowns: ReadonlyArray<PoolCooldown>) =>\n cooldowns.reduce<number | undefined>(\n (earliest, cooldown) =>\n earliest === undefined || cooldown.untilMs < earliest ? cooldown.untilMs : earliest,\n undefined\n )\n\nconst emptySnapshot: PoolStoreSnapshot = { pin: undefined, cooldowns: [], readings: [] }\n\n/**\n * Pools several subscription accounts of one provider behind one `LLMProvider` (ADR 0011).\n *\n * Each request goes to the account whose longest usage window resets soonest and still has quota\n * (expiring-first), or, without fresh readings, stays on the route's pinned account. An\n * account-scoped failure before the first event (an exhausted quota, rejected credentials) cools the\n * account down and the same request moves to the next account; every other failure, and any failure\n * after an event, reaches the caller unchanged. With every account out it fails `rate_limit`\n * (non-retryable) carrying the earliest quota reset, or `auth` when only auth cooldowns remain.\n *\n * Store failures never fail a request: reads fall back to stateless fill-first and writes are\n * logged.\n */\nexport const makePool = (options: PoolOptions): Effect.Effect<Pool, PoolConfigError> =>\n Effect.gen(function* () {\n const poolId = options.id.trim()\n\n if (poolId === '') {\n return yield* configError('invalid_pool_id', 'Pool id must be a non-empty string')\n }\n\n if (options.accounts.length === 0) {\n return yield* configError('no_accounts', `Pool ${poolId} needs at least one account`)\n }\n\n const maxAgeMs = options.maxAgeMs ?? defaultPoolMaxAgeMs\n const reservePercent = options.reservePercent ?? 0\n\n if (!Number.isFinite(maxAgeMs) || maxAgeMs <= 0) {\n return yield* configError('invalid_max_age', 'maxAgeMs must be a positive number')\n }\n\n if (!Number.isFinite(reservePercent) || reservePercent < 0 || reservePercent >= 100) {\n return yield* configError(\n 'invalid_reserve_percent',\n 'reservePercent must be at least 0 and below 100'\n )\n }\n\n const accounts: Array<ValidAccount> = []\n\n for (const account of options.accounts) {\n const id = account.id.trim()\n const priority = account.priority ?? 0\n\n if (id === '' || id !== account.id) {\n return yield* configError(\n 'invalid_account_id',\n 'Account ids must be non-empty without surrounding whitespace'\n )\n }\n\n if (accounts.some(existing => existing.id === id)) {\n return yield* configError('duplicate_account_id', `Duplicate account id: ${id}`)\n }\n\n if (!Number.isFinite(priority)) {\n return yield* configError('invalid_priority', `Account ${id} has a non-finite priority`)\n }\n\n accounts.push({ id, priority, provider: account.provider })\n }\n\n const accountIds = accounts.map(account => account.id)\n const accountsById = new Map(accounts.map(account => [account.id, account]))\n const store = options.store\n\n const loadSnapshot = (route: string) =>\n store\n .load({ poolId, route, accountIds })\n .pipe(\n Effect.catch(error =>\n Effect.logWarning('pool: store load failed; routing without pool state').pipe(\n Effect.annotateLogs({ operation: error.operation }),\n Effect.as(emptySnapshot)\n )\n )\n )\n\n // Host store messages may carry connection details: log the operation only.\n const logStoreFailure = (operation: PoolStoreError['operation']) => () =>\n Effect.logWarning(`pool: store ${operation} failed`)\n\n const readingsOf = (snapshot: PoolStoreSnapshot) => {\n const readings = new Map<string, PoolReading>()\n\n for (const stored of snapshot.readings) {\n const reading = poolReadingFromSnapshot(stored.snapshot)\n\n if (reading !== undefined) {\n readings.set(stored.accountId, reading)\n }\n }\n\n return readings\n }\n\n const moveReason = (input: {\n readonly decision: PoolDecision\n readonly from: string\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly excluded: ReadonlySet<string>\n }) =>\n Match.value(input.decision).pipe(\n Match.tag('Quota', decision =>\n decision.resetsAtMs === undefined\n ? 'quota ranking, window not started'\n : `quota ranking, window resets ${iso(decision.resetsAtMs)}`\n ),\n Match.orElse(() => {\n const cooldown = input.cooldowns.find(current => current.accountId === input.from)\n\n if (cooldown !== undefined) {\n return Match.value(cooldown).pipe(\n Match.tag(\n 'PoolQuotaCooldown',\n current => `quota cooldown until ${iso(current.untilMs)}`\n ),\n Match.tag(\n 'PoolAuthCooldown',\n current => `auth cooldown until ${iso(current.untilMs)}`\n ),\n Match.exhaustive\n )\n }\n\n if (input.excluded.has(input.from)) {\n return 'failed this request'\n }\n\n return accountsById.has(input.from)\n ? 'a fresh reading shows it used up'\n : 'not configured'\n })\n )\n\n const updatePin = (input: {\n readonly route: string\n readonly decision: PoolDecision\n readonly snapshot: PoolStoreSnapshot\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly excluded: ReadonlySet<string>\n readonly nowMs: number\n }) => {\n const current = input.snapshot.pin?.accountId\n const next = input.decision.accountId\n\n if (current === next) {\n return Effect.void\n }\n\n const pin = PoolPin.make({\n poolId,\n route: input.route,\n accountId: next,\n pinnedAtMs: input.nowMs\n })\n\n const log =\n current === undefined\n ? Effect.logDebug('pool: route pinned').pipe(Effect.annotateLogs({ to: next }))\n : Effect.logInfo(\n `pool: pin moved: ${moveReason({\n decision: input.decision,\n from: current,\n cooldowns: input.cooldowns,\n excluded: input.excluded\n })}`\n ).pipe(Effect.annotateLogs({ from: current, to: next }))\n\n return store.movePin({ expected: current, pin }).pipe(\n Effect.flatMap(written => (written ? log : Effect.void)),\n Effect.catch(logStoreFailure('movePin'))\n )\n }\n\n const exhaustedError = (input: {\n /** Cooldowns still active at the final pick. */\n readonly active: ReadonlyArray<PoolCooldown>\n /** Every cooldown that put an account out, including ones this request recorded that ran out. */\n readonly all: ReadonlyArray<PoolCooldown>\n readonly lastError: LLMError | undefined\n }) => {\n const isQuota = (cooldown: PoolCooldown) => Predicate.isTagged(cooldown, 'PoolQuotaCooldown')\n const quotaResetsAtMs = minUntil(input.active.filter(isQuota))\n const until = minUntil(input.active)\n // The vendor id of the last failure; the pool id when no account was tried in this request.\n const provider = input.lastError?.provider?.provider ?? poolId\n\n const message =\n until === undefined\n ? `Every account of pool ${poolId} is unavailable`\n : `Every account of pool ${poolId} is unavailable until ${iso(until)}`\n\n if (!input.all.some(isQuota)) {\n return new LLMError({\n cause: 'provider_error',\n message,\n retryable: false,\n provider: ProviderErrorInfo.make({ provider, kind: 'auth' })\n })\n }\n\n return new LLMError({\n cause: 'rate_limit',\n message,\n retryable: false,\n provider:\n quotaResetsAtMs === undefined\n ? ProviderErrorInfo.make({ provider, kind: 'rate_limit' })\n : ProviderErrorInfo.make({ provider, kind: 'rate_limit', quotaResetsAtMs })\n })\n }\n\n const attempt = (input: {\n readonly request: LLMRequest\n readonly route: string\n readonly account: ValidAccount\n readonly excluded: ReadonlySet<string>\n readonly recorded: ReadonlyArray<PoolCooldown>\n }): Stream.Stream<LLMEvent, LLMProviderError> =>\n Stream.unwrap(\n Ref.make(false).pipe(\n Effect.map(emitted =>\n input.account.provider.stream(input.request).pipe(\n Stream.tap(() => Ref.set(emitted, true)),\n Stream.catchTags({\n LLMError: error =>\n Stream.unwrap(\n Effect.gen(function* () {\n const nowMs = yield* Clock.currentTimeMillis\n const cooldown = poolFailureCooldown(error, input.account.id, nowMs)\n\n if ((yield* Ref.get(emitted)) || cooldown === undefined) {\n return Stream.fail(error)\n }\n\n yield* store\n .putCooldown(cooldown)\n .pipe(Effect.catch(logStoreFailure('putCooldown')))\n yield* Effect.logInfo(\n `pool: failover after ${Predicate.isTagged(cooldown, 'PoolQuotaCooldown') ? 'quota exhaustion' : 'auth failure'}, cooldown until ${iso(cooldown.untilMs)}`\n ).pipe(Effect.annotateLogs({ account: input.account.id }))\n\n return pick({\n request: input.request,\n route: input.route,\n excluded: new Set([...input.excluded, input.account.id]),\n recorded: [...input.recorded, cooldown],\n lastError: error\n })\n })\n )\n })\n )\n )\n )\n )\n\n const pick = (input: {\n readonly request: LLMRequest\n readonly route: string\n readonly excluded: ReadonlySet<string>\n readonly recorded: ReadonlyArray<PoolCooldown>\n readonly lastError: LLMError | undefined\n }): Stream.Stream<LLMEvent, LLMProviderError> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const nowMs = yield* Clock.currentTimeMillis\n const snapshot = yield* loadSnapshot(input.route)\n const readings = readingsOf(snapshot)\n\n const stale = staleQuotaCooldowns({\n cooldowns: snapshot.cooldowns,\n readings,\n nowMs,\n maxAgeMs\n })\n\n yield* Effect.forEach(\n stale,\n cooldown =>\n store\n .clearCooldown({ accountId: cooldown.accountId, startedAtMs: cooldown.startedAtMs })\n .pipe(\n Effect.flatMap(cleared =>\n cleared\n ? Effect.logInfo(\n `pool: cleared cooldown (until ${iso(cooldown.untilMs)}): a newer reading shows quota left`\n ).pipe(Effect.annotateLogs({ account: cooldown.accountId }))\n : Effect.void\n ),\n Effect.catch(logStoreFailure('clearCooldown'))\n ),\n { discard: true }\n )\n\n const cooldowns = [\n ...snapshot.cooldowns.filter(\n cooldown => isCooldownActive(cooldown, nowMs) && !stale.includes(cooldown)\n ),\n ...input.recorded.filter(cooldown => isCooldownActive(cooldown, nowMs))\n ]\n\n const unavailable = new Set([\n ...input.excluded,\n ...cooldowns.map(cooldown => cooldown.accountId)\n ])\n\n const decision = rankPool({\n accounts,\n unavailable,\n readings,\n pin: snapshot.pin?.accountId,\n route: input.route,\n nowMs,\n maxAgeMs,\n reservePercent\n })\n\n const account = decision === undefined ? undefined : accountsById.get(decision.accountId)\n\n if (decision === undefined || account === undefined) {\n yield* Effect.logWarning('pool: every account is unavailable')\n\n return Stream.fail(\n exhaustedError({\n active: cooldowns,\n // Cooldowns recorded by this request decide the kind even if they already ran out.\n all: [\n ...cooldowns,\n ...input.recorded.filter(cooldown => !cooldowns.includes(cooldown))\n ],\n lastError: input.lastError\n })\n )\n }\n\n yield* updatePin({\n route: input.route,\n decision,\n snapshot,\n cooldowns,\n excluded: input.excluded,\n nowMs\n })\n yield* Effect.annotateCurrentSpan({ account: account.id, source: decision._tag })\n\n return attempt({\n request: input.request,\n route: input.route,\n account,\n excluded: input.excluded,\n recorded: input.recorded\n })\n }).pipe(\n Effect.withSpan('pool.pick', { attributes: { pool: poolId, route: input.route } }),\n Effect.annotateLogs({ pool: poolId, route: input.route })\n )\n )\n\n const provider: LLMProviderService = {\n stream: request => {\n const route = poolRoute(request.model)\n\n return route === ''\n ? Stream.fail(\n new LLMError({\n cause: 'validation_error',\n message: `Pool ${poolId} needs a model id`,\n retryable: false\n })\n )\n : pick({ request, route, excluded: new Set(), recorded: [], lastError: undefined })\n }\n }\n\n const recordReading = (accountId: string, snapshot: ProviderSubscriptionUsageSnapshot) =>\n accountsById.has(accountId)\n ? store.putReading(PoolStoredReading.make({ accountId, snapshot }))\n : Effect.fail(new PoolUnknownAccountError({ poolId, accountId }))\n\n const state = Effect.gen(function* () {\n const nowMs = yield* Clock.currentTimeMillis\n const stored = yield* store.state({ poolId, accountIds })\n\n return {\n poolId,\n pins: stored.pins,\n accounts: accounts.map(account => ({\n id: account.id,\n priority: account.priority,\n cooldown: stored.cooldowns.find(\n cooldown => cooldown.accountId === account.id && isCooldownActive(cooldown, nowMs)\n ),\n reading: stored.readings.find(reading => reading.accountId === account.id)?.snapshot\n }))\n }\n })\n\n return { id: poolId, provider, recordReading, state }\n })\n"],"mappings":";;;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Q,KAAK;EAErC,OAAO,UAAU,KACb,OAAO,KACL,IAAI,SAAS;GACX,OAAO;GACP,SAAS,QAAQ,OAAO;GACxB,WAAW;EACb,CAAC,CACH,IACA,KAAK;GAAE;GAAS;GAAO,0BAAU,IAAI,IAAI;GAAG,UAAU,CAAC;GAAG,WAAW,KAAA;EAAU,CAAC;CACtF,EACF;CAEA,MAAM,iBAAiB,WAAmB,aACxC,aAAa,IAAI,SAAS,IACtB,MAAM,WAAW,kBAAkB,KAAK;EAAE;EAAW;CAAS,CAAC,CAAC,IAChE,OAAO,KAAK,IAAI,wBAAwB;EAAE;EAAQ;CAAU,CAAC,CAAC;CAoBpE,OAAO;EAAE,IAAI;EAAQ;EAAU;EAAe,OAlBhC,OAAO,IAAI,aAAa;GACpC,MAAM,QAAQ,OAAO,MAAM;GAC3B,MAAM,SAAS,OAAO,MAAM,MAAM;IAAE;IAAQ;GAAW,CAAC;GAExD,OAAO;IACL;IACA,MAAM,OAAO;IACb,UAAU,SAAS,KAAI,aAAY;KACjC,IAAI,QAAQ;KACZ,UAAU,QAAQ;KAClB,UAAU,OAAO,UAAU,MACzB,aAAY,SAAS,cAAc,QAAQ,MAAM,iBAAiB,UAAU,KAAK,CACnF;KACA,SAAS,OAAO,SAAS,MAAK,YAAW,QAAQ,cAAc,QAAQ,EAAE,GAAG;IAC9E,EAAE;GACJ;EACF,CAEkD;CAAE;AACtD,CAAC"}
1
+ {"version":3,"file":"pool.mjs","names":[],"sources":["../src/pool.ts"],"sourcesContent":["import { Clock, Effect, Match, Predicate, Ref, Stream } from 'effect'\nimport {\n LLMError,\n type LLMEvent,\n type LLMProvider,\n type LLMProviderError,\n type LLMRequest\n} from '@yolk-sdk/agent/loop'\nimport { ProviderErrorInfo } from '@yolk-sdk/agent/protocol'\nimport type { ProviderSubscriptionUsageSnapshot } from '@yolk-sdk/agent/providers/subscription-usage'\nimport {\n PoolAuthCooldown,\n PoolConfigError,\n type PoolCooldown,\n PoolPin,\n PoolQuotaCooldown,\n type PoolStoreError,\n PoolUnknownAccountError\n} from './model.ts'\nimport {\n isCooldownActive,\n type PoolCandidate,\n type PoolDecision,\n type PoolReading,\n poolReadingFromSnapshot,\n poolRoute,\n rankPool,\n staleQuotaCooldowns\n} from './rank.ts'\nimport { type PoolStoreApi, type PoolStoreSnapshot, PoolStoredReading } from './store.ts'\n\ntype LLMProviderService = typeof LLMProvider.Service\n\n/** One subscription account of a pool: its own provider, built by the host with its credentials. */\nexport type PoolAccount = {\n /** Unique within the store; cooldowns and readings are keyed by it. */\n readonly id: string\n /** Breaks ranking ties and orders fill-first (higher first). Default 0. */\n readonly priority?: number\n readonly provider: LLMProviderService\n}\n\nexport type PoolOptions = {\n /** Pins are keyed by pool id and route. */\n readonly id: string\n /** Accounts of one provider; requests must suit every one of them. */\n readonly accounts: ReadonlyArray<PoolAccount>\n readonly store: PoolStoreApi\n /** How old a reading may be and still rank accounts. Default 30 minutes. */\n readonly maxAgeMs?: number\n /** Quota left untouched on every window, in percent (0 to below 100). Default 0. */\n readonly reservePercent?: number\n}\n\n/** One account as the host UI sees it. */\nexport type PoolAccountState = {\n readonly id: string\n readonly priority: number\n /** The account's cooldown while it is active. */\n readonly cooldown: PoolCooldown | undefined\n readonly reading: ProviderSubscriptionUsageSnapshot | undefined\n}\n\nexport type PoolState = {\n readonly poolId: string\n /** The account serving each route. */\n readonly pins: ReadonlyArray<PoolPin>\n readonly accounts: ReadonlyArray<PoolAccountState>\n}\n\nexport type Pool = {\n readonly id: string\n /** The pooled provider: provide it as the loop's `LLMProvider`, innermost under host wrappers. */\n readonly provider: LLMProviderService\n /** Records an account's usage snapshot; hosts poll usage on their own schedule. */\n readonly recordReading: (\n accountId: string,\n snapshot: ProviderSubscriptionUsageSnapshot\n ) => Effect.Effect<void, PoolUnknownAccountError | PoolStoreError>\n /** Pins, active cooldowns, and readings for host UIs. */\n readonly state: Effect.Effect<PoolState, PoolStoreError>\n}\n\n/** Default `maxAgeMs`: readings older than 30 minutes no longer rank accounts. */\nexport const defaultPoolMaxAgeMs = 30 * 60_000\n\n/**\n * A `rate_limit` whose `Retry-After` reaches this is a quota lockout, not a transient limit\n * (CLIProxyAPI `expiring-first` `QuotaCooldownFloor`).\n */\nexport const poolQuotaRetryAfterFloorMs = 30 * 60_000\n\n/** The longest cooldown the pool records; a later or non-finite hint is not trusted. */\nexport const poolMaxCooldownMs = 366 * 24 * 60 * 60_000\n\n/** How long an account whose credentials were rejected sits out. */\nexport const poolAuthCooldownMs = 30 * 60_000\n\ntype ValidAccount = PoolCandidate & { readonly provider: LLMProviderService }\n\nconst configError = (reason: PoolConfigError['reason'], message: string) =>\n Effect.fail(new PoolConfigError({ reason, message }))\n\n/**\n * The cooldown an account-scoped failure puts its account in, or undefined when the failure is not\n * the account's (the loop's retry handles those): a `rate_limit` with a quota reset or a\n * `Retry-After` of at least `poolQuotaRetryAfterFloorMs` (quota), or `auth`.\n */\nexport const poolFailureCooldown = (\n error: LLMError,\n accountId: string,\n nowMs: number\n): PoolCooldown | undefined => {\n const info = error.provider\n\n if (info === undefined) {\n return undefined\n }\n\n if (info.kind === 'auth') {\n return PoolAuthCooldown.make({\n accountId,\n startedAtMs: nowMs,\n untilMs: nowMs + poolAuthCooldownMs\n })\n }\n\n if (info.kind !== 'rate_limit') {\n return undefined\n }\n\n const trusted = (untilMs: number) =>\n Number.isFinite(untilMs) && untilMs > nowMs && untilMs - nowMs <= poolMaxCooldownMs\n\n if (info.quotaResetsAtMs !== undefined && trusted(info.quotaResetsAtMs)) {\n return PoolQuotaCooldown.make({ accountId, startedAtMs: nowMs, untilMs: info.quotaResetsAtMs })\n }\n\n if (\n info.retryAfterMs !== undefined &&\n info.retryAfterMs >= poolQuotaRetryAfterFloorMs &&\n trusted(nowMs + info.retryAfterMs)\n ) {\n return PoolQuotaCooldown.make({\n accountId,\n startedAtMs: nowMs,\n untilMs: nowMs + info.retryAfterMs\n })\n }\n\n return undefined\n}\n\nconst iso = (ms: number) => {\n const date = new Date(ms)\n\n return Number.isFinite(date.getTime()) ? date.toISOString() : String(ms)\n}\n\nconst minUntil = (cooldowns: ReadonlyArray<PoolCooldown>) =>\n cooldowns.reduce<number | undefined>(\n (earliest, cooldown) =>\n earliest === undefined || cooldown.untilMs < earliest ? cooldown.untilMs : earliest,\n undefined\n )\n\nconst emptySnapshot: PoolStoreSnapshot = { pin: undefined, cooldowns: [], readings: [] }\n\n/**\n * Pools several subscription accounts of one provider behind one `LLMProvider` (ADR 0011).\n *\n * Each request goes to the account whose longest usage window resets soonest and still has quota\n * (expiring-first), or, without fresh readings, stays on the route's pinned account. An\n * account-scoped failure before the first event (an exhausted quota, rejected credentials) cools the\n * account down and the same request moves to the next account; every other failure, and any failure\n * after an event, reaches the caller unchanged. With every account out it fails `rate_limit`\n * (non-retryable) carrying the earliest quota reset, or `auth` when only auth cooldowns remain.\n *\n * Store failures never fail a request: reads fall back to stateless fill-first and writes are\n * logged.\n */\nexport const makePool = (options: PoolOptions): Effect.Effect<Pool, PoolConfigError> =>\n Effect.gen(function* () {\n const poolId = options.id.trim()\n\n if (poolId === '') {\n return yield* configError('invalid_pool_id', 'Pool id must be a non-empty string')\n }\n\n if (options.accounts.length === 0) {\n return yield* configError('no_accounts', `Pool ${poolId} needs at least one account`)\n }\n\n const maxAgeMs = options.maxAgeMs ?? defaultPoolMaxAgeMs\n const reservePercent = options.reservePercent ?? 0\n\n if (!Number.isFinite(maxAgeMs) || maxAgeMs <= 0) {\n return yield* configError('invalid_max_age', 'maxAgeMs must be a positive number')\n }\n\n if (!Number.isFinite(reservePercent) || reservePercent < 0 || reservePercent >= 100) {\n return yield* configError(\n 'invalid_reserve_percent',\n 'reservePercent must be at least 0 and below 100'\n )\n }\n\n const accounts: Array<ValidAccount> = []\n\n for (const account of options.accounts) {\n const id = account.id.trim()\n const priority = account.priority ?? 0\n\n if (id === '' || id !== account.id) {\n return yield* configError(\n 'invalid_account_id',\n 'Account ids must be non-empty without surrounding whitespace'\n )\n }\n\n if (accounts.some(existing => existing.id === id)) {\n return yield* configError('duplicate_account_id', `Duplicate account id: ${id}`)\n }\n\n if (!Number.isFinite(priority)) {\n return yield* configError('invalid_priority', `Account ${id} has a non-finite priority`)\n }\n\n accounts.push({ id, priority, provider: account.provider })\n }\n\n const accountIds = accounts.map(account => account.id)\n const accountsById = new Map(accounts.map(account => [account.id, account]))\n const store = options.store\n\n const loadSnapshot = (route: string) =>\n store\n .load({ poolId, route, accountIds })\n .pipe(\n Effect.catch(error =>\n Effect.logWarning('pool: store load failed; routing without pool state').pipe(\n Effect.annotateLogs({ operation: error.operation }),\n Effect.as(emptySnapshot)\n )\n )\n )\n\n // Host store messages may carry connection details: log the operation only.\n const logStoreFailure = (operation: PoolStoreError['operation']) => () =>\n Effect.logWarning(`pool: store ${operation} failed`)\n\n const readingsOf = (snapshot: PoolStoreSnapshot) => {\n const readings = new Map<string, PoolReading>()\n\n for (const stored of snapshot.readings) {\n const reading = poolReadingFromSnapshot(stored.snapshot)\n\n if (reading !== undefined) {\n readings.set(stored.accountId, reading)\n }\n }\n\n return readings\n }\n\n const moveReason = (input: {\n readonly decision: PoolDecision\n readonly from: string\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly excluded: ReadonlySet<string>\n }) =>\n Match.value(input.decision).pipe(\n Match.tag('Quota', decision =>\n decision.resetsAtMs === undefined\n ? 'quota ranking, window not started'\n : `quota ranking, window resets ${iso(decision.resetsAtMs)}`\n ),\n Match.orElse(() => {\n const cooldown = input.cooldowns.find(current => current.accountId === input.from)\n\n if (cooldown !== undefined) {\n return Match.value(cooldown).pipe(\n Match.tag(\n 'PoolQuotaCooldown',\n current => `quota cooldown until ${iso(current.untilMs)}`\n ),\n Match.tag(\n 'PoolAuthCooldown',\n current => `auth cooldown until ${iso(current.untilMs)}`\n ),\n Match.exhaustive\n )\n }\n\n if (input.excluded.has(input.from)) {\n return 'failed this request'\n }\n\n return accountsById.has(input.from)\n ? 'a fresh reading shows it used up'\n : 'not configured'\n })\n )\n\n const updatePin = (input: {\n readonly route: string\n readonly decision: PoolDecision\n readonly snapshot: PoolStoreSnapshot\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly excluded: ReadonlySet<string>\n readonly nowMs: number\n }) => {\n const current = input.snapshot.pin?.accountId\n const next = input.decision.accountId\n\n if (current === next) {\n return Effect.void\n }\n\n const pin = PoolPin.make({\n poolId,\n route: input.route,\n accountId: next,\n pinnedAtMs: input.nowMs\n })\n\n const log =\n current === undefined\n ? Effect.logDebug('pool: route pinned').pipe(Effect.annotateLogs({ to: next }))\n : Effect.logInfo(\n `pool: pin moved: ${moveReason({\n decision: input.decision,\n from: current,\n cooldowns: input.cooldowns,\n excluded: input.excluded\n })}`\n ).pipe(Effect.annotateLogs({ from: current, to: next }))\n\n return store.movePin({ expected: current, pin }).pipe(\n Effect.flatMap(written => (written ? log : Effect.void)),\n Effect.catch(logStoreFailure('movePin'))\n )\n }\n\n const exhaustedError = (input: {\n /** Cooldowns still active at the final pick. */\n readonly active: ReadonlyArray<PoolCooldown>\n /** Every cooldown that put an account out, including ones this request recorded that ran out. */\n readonly all: ReadonlyArray<PoolCooldown>\n readonly lastError: LLMError | undefined\n }) => {\n const isQuota = (cooldown: PoolCooldown) => Predicate.isTagged(cooldown, 'PoolQuotaCooldown')\n const quotaResetsAtMs = minUntil(input.active.filter(isQuota))\n const until = minUntil(input.active)\n // The vendor id of the last failure; the pool id when no account was tried in this request.\n const provider = input.lastError?.provider?.provider ?? poolId\n\n const message =\n until === undefined\n ? `Every account of pool ${poolId} is unavailable`\n : `Every account of pool ${poolId} is unavailable until ${iso(until)}`\n\n if (!input.all.some(isQuota)) {\n return new LLMError({\n cause: 'provider_error',\n message,\n retryable: false,\n provider: ProviderErrorInfo.make({ provider, kind: 'auth' })\n })\n }\n\n return new LLMError({\n cause: 'rate_limit',\n message,\n retryable: false,\n provider:\n quotaResetsAtMs === undefined\n ? ProviderErrorInfo.make({ provider, kind: 'rate_limit' })\n : ProviderErrorInfo.make({ provider, kind: 'rate_limit', quotaResetsAtMs })\n })\n }\n\n const attempt = (input: {\n readonly request: LLMRequest\n readonly route: string\n readonly account: ValidAccount\n readonly excluded: ReadonlySet<string>\n readonly recorded: ReadonlyArray<PoolCooldown>\n }): Stream.Stream<LLMEvent, LLMProviderError> =>\n Stream.unwrap(\n Ref.make(false).pipe(\n Effect.map(emitted =>\n input.account.provider.stream(input.request).pipe(\n Stream.tap(() => Ref.set(emitted, true)),\n Stream.catchTags({\n LLMError: error =>\n Stream.unwrap(\n Effect.gen(function* () {\n const nowMs = yield* Clock.currentTimeMillis\n const cooldown = poolFailureCooldown(error, input.account.id, nowMs)\n\n if ((yield* Ref.get(emitted)) || cooldown === undefined) {\n return Stream.fail(error)\n }\n\n yield* store\n .putCooldown(cooldown)\n .pipe(Effect.catch(logStoreFailure('putCooldown')))\n yield* Effect.logInfo(\n `pool: failover after ${Predicate.isTagged(cooldown, 'PoolQuotaCooldown') ? 'quota exhaustion' : 'auth failure'}, cooldown until ${iso(cooldown.untilMs)}`\n ).pipe(Effect.annotateLogs({ account: input.account.id }))\n\n return pick({\n request: input.request,\n route: input.route,\n excluded: new Set([...input.excluded, input.account.id]),\n recorded: [...input.recorded, cooldown],\n lastError: error\n })\n })\n )\n })\n )\n )\n )\n )\n\n const pick = (input: {\n readonly request: LLMRequest\n readonly route: string\n readonly excluded: ReadonlySet<string>\n readonly recorded: ReadonlyArray<PoolCooldown>\n readonly lastError: LLMError | undefined\n }): Stream.Stream<LLMEvent, LLMProviderError> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const nowMs = yield* Clock.currentTimeMillis\n const snapshot = yield* loadSnapshot(input.route)\n const readings = readingsOf(snapshot)\n\n const stale = staleQuotaCooldowns({\n cooldowns: snapshot.cooldowns,\n readings,\n nowMs,\n maxAgeMs\n })\n\n yield* Effect.forEach(\n stale,\n cooldown =>\n store\n .clearCooldown({ accountId: cooldown.accountId, startedAtMs: cooldown.startedAtMs })\n .pipe(\n Effect.flatMap(cleared =>\n cleared\n ? Effect.logInfo(\n `pool: cleared cooldown (until ${iso(cooldown.untilMs)}): a newer reading shows quota left`\n ).pipe(Effect.annotateLogs({ account: cooldown.accountId }))\n : Effect.void\n ),\n Effect.catch(logStoreFailure('clearCooldown'))\n ),\n { discard: true }\n )\n\n const cooldowns = [\n ...snapshot.cooldowns.filter(\n cooldown => isCooldownActive(cooldown, nowMs) && !stale.includes(cooldown)\n ),\n ...input.recorded.filter(cooldown => isCooldownActive(cooldown, nowMs))\n ]\n\n const unavailable = new Set([\n ...input.excluded,\n ...cooldowns.map(cooldown => cooldown.accountId)\n ])\n\n const decision = rankPool({\n accounts,\n unavailable,\n readings,\n pin: snapshot.pin?.accountId,\n route: input.route,\n nowMs,\n maxAgeMs,\n reservePercent\n })\n\n const account = decision === undefined ? undefined : accountsById.get(decision.accountId)\n\n if (decision === undefined || account === undefined) {\n yield* Effect.logWarning('pool: every account is unavailable')\n\n return Stream.fail(\n exhaustedError({\n active: cooldowns,\n // Cooldowns recorded by this request decide the kind even if they already ran out.\n all: [\n ...cooldowns,\n ...input.recorded.filter(cooldown => !cooldowns.includes(cooldown))\n ],\n lastError: input.lastError\n })\n )\n }\n\n yield* updatePin({\n route: input.route,\n decision,\n snapshot,\n cooldowns,\n excluded: input.excluded,\n nowMs\n })\n yield* Effect.annotateCurrentSpan({ account: account.id, source: decision._tag })\n\n return attempt({\n request: input.request,\n route: input.route,\n account,\n excluded: input.excluded,\n recorded: input.recorded\n })\n }).pipe(\n Effect.withSpan('pool.pick', { attributes: { pool: poolId, route: input.route } }),\n Effect.annotateLogs({ pool: poolId, route: input.route })\n )\n )\n\n const provider: LLMProviderService = {\n stream: request => {\n const route = poolRoute(request.model)\n\n return route === ''\n ? Stream.fail(\n new LLMError({\n cause: 'validation_error',\n message: `Pool ${poolId} needs a model id`,\n retryable: false\n })\n )\n : pick({ request, route, excluded: new Set(), recorded: [], lastError: undefined })\n }\n }\n\n const recordReading = (accountId: string, snapshot: ProviderSubscriptionUsageSnapshot) =>\n accountsById.has(accountId)\n ? store.putReading(PoolStoredReading.make({ accountId, snapshot }))\n : Effect.fail(new PoolUnknownAccountError({ poolId, accountId }))\n\n const state = Effect.gen(function* () {\n const nowMs = yield* Clock.currentTimeMillis\n const stored = yield* store.state({ poolId, accountIds })\n\n return {\n poolId,\n pins: stored.pins,\n accounts: accounts.map(account => ({\n id: account.id,\n priority: account.priority,\n cooldown: stored.cooldowns.find(\n cooldown => cooldown.accountId === account.id && isCooldownActive(cooldown, nowMs)\n ),\n reading: stored.readings.find(reading => reading.accountId === account.id)?.snapshot\n }))\n }\n })\n\n return { id: poolId, provider, recordReading, state }\n })\n"],"mappings":";;;;;;;;AAoFA,MAAa,sBAAsB;;;;;AAMnC,MAAa,6BAA6B;;AAG1C,MAAa,oBAAoB;;AAGjC,MAAa,qBAAqB;AAIlC,MAAM,eAAe,QAAmC,YACtD,OAAO,KAAK,IAAI,gBAAgB;CAAE;CAAQ;AAAQ,CAAC,CAAC;;;;;;AAOtD,MAAa,uBACX,OACA,WACA,UAC6B;CAC7B,MAAM,OAAO,MAAM;CAEnB,IAAI,SAAS,KAAA,GACX;CAGF,IAAI,KAAK,SAAS,QAChB,OAAO,iBAAiB,KAAK;EAC3B;EACA,aAAa;EACb,SAAS,QAAQ;CACnB,CAAC;CAGH,IAAI,KAAK,SAAS,cAChB;CAGF,MAAM,WAAW,YACf,OAAO,SAAS,OAAO,KAAK,UAAU,SAAS,UAAU,SAAA;CAE3D,IAAI,KAAK,oBAAoB,KAAA,KAAa,QAAQ,KAAK,eAAe,GACpE,OAAO,kBAAkB,KAAK;EAAE;EAAW,aAAa;EAAO,SAAS,KAAK;CAAgB,CAAC;CAGhG,IACE,KAAK,iBAAiB,KAAA,KACtB,KAAK,gBAAA,QACL,QAAQ,QAAQ,KAAK,YAAY,GAEjC,OAAO,kBAAkB,KAAK;EAC5B;EACA,aAAa;EACb,SAAS,QAAQ,KAAK;CACxB,CAAC;AAIL;AAEA,MAAM,OAAO,OAAe;CAC1B,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AS,OAAO,SAAS,MAAK,YAAW,QAAQ,cAAc,QAAQ,EAAE,CAAC,EAAE;IAC9E,EAAE;GACJ;EACF,CAEkD;CAAE;AACtD,CAAC"}
package/dist/rank.d.mts CHANGED
@@ -1,22 +1,23 @@
1
1
  import { PoolCooldown } from "./model.mjs";
2
2
  import { Data } from "effect";
3
3
  import { ProviderSubscriptionUsageSnapshot } from "@yolk-sdk/agent/providers/subscription-usage";
4
-
5
- //#region src/rank.d.ts
6
4
  /** One usage window of a reading, as the ranking reads it. */
7
- type PoolWindow = {
8
- /** Window length; absent when the provider's window has no fixed length (a billing month). */readonly durationMinutes?: number;
9
- readonly usedPercent: number; /** When the window resets; absent when it has not started. */
10
- readonly resetsAtMs?: number; /** Lowercase model substrings the window limits; empty for every model. */
5
+ export type PoolWindow = {
6
+ /** Window length; absent when the provider's window has no fixed length (a billing month). */
7
+ readonly durationMinutes?: number;
8
+ readonly usedPercent: number;
9
+ /** When the window resets; absent when it has not started. */
10
+ readonly resetsAtMs?: number;
11
+ /** Lowercase model substrings the window limits; empty for every model. */
11
12
  readonly models: ReadonlyArray<string>;
12
13
  };
13
14
  /** The latest quota reading of one account. */
14
- type PoolReading = {
15
+ export type PoolReading = {
15
16
  readonly fetchedAtMs: number;
16
17
  readonly windows: ReadonlyArray<PoolWindow>;
17
18
  };
18
19
  /** A configured account as the ranking sees it. */
19
- type PoolCandidate = {
20
+ export type PoolCandidate = {
20
21
  readonly id: string;
21
22
  readonly priority: number;
22
23
  };
@@ -24,114 +25,47 @@ type PoolCandidate = {
24
25
  * The route a request pins: its model id, trimmed and lowercased. Reasoning effort is a separate
25
26
  * request field and never part of the route.
26
27
  */
27
- declare const poolRoute: (model: string) => string;
28
+ export declare const poolRoute: (model: string) => string;
28
29
  /**
29
30
  * A usage snapshot as the ranking reads it; an unreadable `fetchedAt` yields no reading. Windows
30
31
  * without a length still block a used-up account but never rank it. A window that reports only
31
32
  * `resetsAfterSeconds` resets that long after the reading.
32
33
  */
33
- declare const poolReadingFromSnapshot: (snapshot: ProviderSubscriptionUsageSnapshot) => PoolReading | undefined;
34
+ export declare const poolReadingFromSnapshot: (snapshot: ProviderSubscriptionUsageSnapshot) => PoolReading | undefined;
34
35
  /** Why the ranking chose an account. */
35
- type PoolDecision = Data.TaggedEnum<{
36
- /** Fresh readings ranked it first; `resetsAtMs` is its longest window's reset, if started. */Quota: {
36
+ export type PoolDecision = Data.TaggedEnum<{
37
+ /** Fresh readings ranked it first; `resetsAtMs` is its longest window's reset, if started. */
38
+ Quota: {
37
39
  readonly accountId: string;
38
40
  readonly resetsAtMs: number | undefined;
39
- }; /** No candidate is usable by a fresh reading; the route's pin is still available. */
41
+ };
42
+ /** No candidate is usable by a fresh reading; the route's pin is still available. */
40
43
  Sticky: {
41
44
  readonly accountId: string;
42
- }; /** No candidate is usable by a fresh reading and no pin is: highest priority, then lowest id. */
45
+ };
46
+ /** No candidate is usable by a fresh reading and no pin is: highest priority, then lowest id. */
43
47
  FillFirst: {
44
48
  readonly accountId: string;
45
49
  };
46
50
  }>;
47
- declare const PoolDecision: {
48
- readonly Quota: Data.TaggedEnum.ConstructorFrom<{
49
- readonly _tag: "Quota";
50
- readonly accountId: string;
51
- readonly resetsAtMs: number | undefined;
52
- }, "_tag">;
53
- readonly Sticky: Data.TaggedEnum.ConstructorFrom<{
54
- readonly _tag: "Sticky";
55
- readonly accountId: string;
56
- }, "_tag">;
57
- readonly FillFirst: Data.TaggedEnum.ConstructorFrom<{
58
- readonly _tag: "FillFirst";
59
- readonly accountId: string;
60
- }, "_tag">;
61
- readonly $is: <Tag extends "Quota" | "Sticky" | "FillFirst">(tag: Tag) => (u: unknown) => u is Extract<{
62
- readonly _tag: "Quota";
63
- readonly accountId: string;
64
- readonly resetsAtMs: number | undefined;
65
- }, {
66
- readonly _tag: Tag;
67
- }> | Extract<{
68
- readonly _tag: "Sticky";
69
- readonly accountId: string;
70
- }, {
71
- readonly _tag: Tag;
72
- }> | Extract<{
73
- readonly _tag: "FillFirst";
74
- readonly accountId: string;
75
- }, {
76
- readonly _tag: Tag;
77
- }>;
78
- readonly $match: {
79
- <Cases extends {
80
- readonly Quota: (args: {
81
- readonly _tag: "Quota";
82
- readonly accountId: string;
83
- readonly resetsAtMs: number | undefined;
84
- }) => any;
85
- readonly Sticky: (args: {
86
- readonly _tag: "Sticky";
87
- readonly accountId: string;
88
- }) => any;
89
- readonly FillFirst: (args: {
90
- readonly _tag: "FillFirst";
91
- readonly accountId: string;
92
- }) => any;
93
- }>(cases: Cases): (value: {
94
- readonly _tag: "Quota";
95
- readonly accountId: string;
96
- readonly resetsAtMs: number | undefined;
97
- } | {
98
- readonly _tag: "Sticky";
99
- readonly accountId: string;
100
- } | {
101
- readonly _tag: "FillFirst";
102
- readonly accountId: string;
103
- }) => import("effect/Unify").Unify<ReturnType<Cases["Quota" | "Sticky" | "FillFirst"]>>;
104
- <Cases extends {
105
- readonly Quota: (args: {
106
- readonly _tag: "Quota";
107
- readonly accountId: string;
108
- readonly resetsAtMs: number | undefined;
109
- }) => any;
110
- readonly Sticky: (args: {
111
- readonly _tag: "Sticky";
112
- readonly accountId: string;
113
- }) => any;
114
- readonly FillFirst: (args: {
115
- readonly _tag: "FillFirst";
116
- readonly accountId: string;
117
- }) => any;
118
- }>(value: {
119
- readonly _tag: "Quota";
120
- readonly accountId: string;
121
- readonly resetsAtMs: number | undefined;
122
- } | {
123
- readonly _tag: "Sticky";
124
- readonly accountId: string;
125
- } | {
126
- readonly _tag: "FillFirst";
127
- readonly accountId: string;
128
- }, cases: Cases): import("effect/Unify").Unify<ReturnType<Cases["Quota" | "Sticky" | "FillFirst"]>>;
129
- };
130
- };
131
- type PoolRankInput = {
132
- /** Configured accounts. */readonly accounts: ReadonlyArray<PoolCandidate>; /** Accounts that cannot serve now: an active cooldown, or already tried by this request. */
51
+ export declare const PoolDecision: Data.TaggedEnum.Constructor<{
52
+ readonly _tag: "FillFirst";
53
+ readonly accountId: string;
54
+ } | {
55
+ readonly _tag: "Quota";
56
+ readonly accountId: string;
57
+ readonly resetsAtMs: number | undefined;
58
+ } | {
59
+ readonly _tag: "Sticky";
60
+ readonly accountId: string;
61
+ }>;
62
+ export type PoolRankInput = {
63
+ /** Configured accounts. */
64
+ readonly accounts: ReadonlyArray<PoolCandidate>;
65
+ /** Accounts that cannot serve now: an active cooldown, or already tried by this request. */
133
66
  readonly unavailable: ReadonlySet<string>;
134
- readonly readings: ReadonlyMap<string, PoolReading>; /** The account the route is pinned to, if any. */
67
+ readonly readings: ReadonlyMap<string, PoolReading>;
68
+ /** The account the route is pinned to, if any. */
135
69
  readonly pin: string | undefined;
136
70
  readonly route: string;
137
71
  readonly nowMs: number;
@@ -145,20 +79,18 @@ type PoolRankInput = {
145
79
  * reading shows used up; those serve only when nothing else is left, so the provider's own answer
146
80
  * decides. Undefined when no account is available.
147
81
  */
148
- declare const rankPool: (input: PoolRankInput) => PoolDecision | undefined;
82
+ export declare const rankPool: (input: PoolRankInput) => PoolDecision | undefined;
149
83
  /** Whether a cooldown still keeps its account out at `nowMs`. */
150
- declare const isCooldownActive: (cooldown: PoolCooldown, nowMs: number) => boolean;
84
+ export declare const isCooldownActive: (cooldown: PoolCooldown, nowMs: number) => boolean;
151
85
  /**
152
86
  * Active quota cooldowns that a fresh reading taken after they began shows to be over (a limit
153
87
  * reset was spent, or the provider reset early). A reading from before the lockout never clears
154
88
  * one, and auth cooldowns are never cleared by readings.
155
89
  */
156
- declare const staleQuotaCooldowns: (input: {
90
+ export declare const staleQuotaCooldowns: (input: {
157
91
  readonly cooldowns: ReadonlyArray<PoolCooldown>;
158
92
  readonly readings: ReadonlyMap<string, PoolReading>;
159
93
  readonly nowMs: number;
160
94
  readonly maxAgeMs: number;
161
95
  }) => ReadonlyArray<PoolCooldown>;
162
- //#endregion
163
- export { PoolCandidate, PoolDecision, PoolRankInput, PoolReading, PoolWindow, isCooldownActive, poolReadingFromSnapshot, poolRoute, rankPool, staleQuotaCooldowns };
164
96
  //# sourceMappingURL=rank.d.mts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,MACP,aAAA,CAAc,YAAA"}
1
+ {"version":3,"file":"rank.d.mts","names":[],"sources":["../src/rank.ts"],"mappings":";;;;YAKY;;WAED;WACA;;WAEA;;WAEA,QAAQ;;;YAMP;WACD;WACA,SAAS,cAAc;;;YAItB;WACD;WACA;;;;;;qBAOE,YAAS;;;;;;qBAOT,0BAAuB,UACxB,sCACT;;YAsCS,eAAe,KAAK;;EAE9B;aAAkB;aAA4B;;;EAE9C;aAAmB;;;EAEnB;aAAsB;;;qBAGX,cAAY,KAAA,WAAA;;WAHU;;;WAJJ;WAA6B;;;WAE5B;;YAOpB;;WAED,UAAU,cAAc;;WAExB,aAAa;WACb,UAAU,oBAAoB;;WAE9B;WACA;WACA;WACA;WACA;;;;;;;;;qBAkHE,WAAQ,OAAW,kBAAgB;;qBA+CnC,mBAAgB,UAAc,cAAY;;;;;;qBAqB1C,sBAAmB;WACrB,WAAW,cAAc;WACzB,UAAU,oBAAoB;WAC9B;WACA;MACP,cAAc"}
package/dist/rank.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Data, Predicate } from "effect";
2
- //#region src/rank.ts
2
+ // src/rank.ts
3
3
  /**
4
4
  * The route a request pins: its model id, trimmed and lowercased. Reasoning effort is a separate
5
5
  * request field and never part of the route.
@@ -133,7 +133,6 @@ const staleQuotaCooldowns = (input) => input.cooldowns.filter((cooldown) => {
133
133
  const reading = input.readings.get(cooldown.accountId);
134
134
  return reading !== void 0 && input.nowMs - reading.fetchedAtMs <= input.maxAgeMs && reading.fetchedAtMs > cooldown.startedAtMs && hasQuotaLeft(reading, input.nowMs);
135
135
  });
136
- //#endregion
137
136
  export { PoolDecision, isCooldownActive, poolReadingFromSnapshot, poolRoute, rankPool, staleQuotaCooldowns };
138
137
 
139
138
  //# sourceMappingURL=rank.mjs.map
package/dist/rank.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"rank.mjs","names":[],"sources":["../src/rank.ts"],"sourcesContent":["import { Data, Predicate } from 'effect'\nimport type { ProviderSubscriptionUsageSnapshot } from '@yolk-sdk/agent/providers/subscription-usage'\nimport type { PoolCooldown } from './model.ts'\n\n/** One usage window of a reading, as the ranking reads it. */\nexport type PoolWindow = {\n /** Window length; absent when the provider's window has no fixed length (a billing month). */\n readonly durationMinutes?: number\n readonly usedPercent: number\n /** When the window resets; absent when it has not started. */\n readonly resetsAtMs?: number\n /** Lowercase model substrings the window limits; empty for every model. */\n readonly models: ReadonlyArray<string>\n}\n\ntype MutablePoolWindow = { -readonly [Key in keyof PoolWindow]: PoolWindow[Key] }\n\n/** The latest quota reading of one account. */\nexport type PoolReading = {\n readonly fetchedAtMs: number\n readonly windows: ReadonlyArray<PoolWindow>\n}\n\n/** A configured account as the ranking sees it. */\nexport type PoolCandidate = {\n readonly id: string\n readonly priority: number\n}\n\n/**\n * The route a request pins: its model id, trimmed and lowercased. Reasoning effort is a separate\n * request field and never part of the route.\n */\nexport const poolRoute = (model: string) => model.trim().toLowerCase()\n\n/**\n * A usage snapshot as the ranking reads it; an unreadable `fetchedAt` yields no reading. Windows\n * without a length still block a used-up account but never rank it. A window that reports only\n * `resetsAfterSeconds` resets that long after the reading.\n */\nexport const poolReadingFromSnapshot = (\n snapshot: ProviderSubscriptionUsageSnapshot\n): PoolReading | undefined => {\n const fetchedAtMs = Date.parse(snapshot.fetchedAt)\n\n if (!Number.isFinite(fetchedAtMs)) {\n return undefined\n }\n\n const windows = Array.from(snapshot.windows).map((window): PoolWindow => {\n const resetsAtMs =\n window.resetsAt !== undefined\n ? Date.parse(window.resetsAt)\n : window.resetsAfterSeconds !== undefined\n ? fetchedAtMs + window.resetsAfterSeconds * 1000\n : Number.NaN\n\n const models = (window.models ?? []).flatMap(model => {\n const normalized = model.trim().toLowerCase()\n\n return normalized === '' ? [] : [normalized]\n })\n\n const fields: MutablePoolWindow = { usedPercent: window.usedPercent, models }\n\n if (window.windowDurationMinutes !== undefined) {\n fields.durationMinutes = window.windowDurationMinutes\n }\n\n if (Number.isFinite(resetsAtMs)) {\n fields.resetsAtMs = resetsAtMs\n }\n\n return fields\n })\n\n return { fetchedAtMs, windows }\n}\n\n/** Why the ranking chose an account. */\nexport type PoolDecision = Data.TaggedEnum<{\n /** Fresh readings ranked it first; `resetsAtMs` is its longest window's reset, if started. */\n Quota: { readonly accountId: string; readonly resetsAtMs: number | undefined }\n /** No candidate is usable by a fresh reading; the route's pin is still available. */\n Sticky: { readonly accountId: string }\n /** No candidate is usable by a fresh reading and no pin is: highest priority, then lowest id. */\n FillFirst: { readonly accountId: string }\n}>\n\nexport const PoolDecision = Data.taggedEnum<PoolDecision>()\n\nexport type PoolRankInput = {\n /** Configured accounts. */\n readonly accounts: ReadonlyArray<PoolCandidate>\n /** Accounts that cannot serve now: an active cooldown, or already tried by this request. */\n readonly unavailable: ReadonlySet<string>\n readonly readings: ReadonlyMap<string, PoolReading>\n /** The account the route is pinned to, if any. */\n readonly pin: string | undefined\n readonly route: string\n readonly nowMs: number\n readonly maxAgeMs: number\n readonly reservePercent: number\n}\n\nconst appliesTo = (window: PoolWindow, route: string) =>\n window.models.length === 0 || window.models.some(model => route.includes(model))\n\nconst hasStarted = (window: PoolWindow, nowMs: number) =>\n window.resetsAtMs !== undefined && window.resetsAtMs > nowMs\n\ntype AccountRank =\n /** No window of the reading applies to the route: the reading says nothing. */\n | { readonly kind: 'unknown' }\n /** An applicable started window is used up to the reserve. */\n | { readonly kind: 'exhausted' }\n | { readonly kind: 'usable'; readonly resetsAtMs: number | undefined }\n\n/**\n * The reset of the account's longest window with a length that applies to the route (undefined\n * when it has not started, or no applicable window has a length). Unusable when no window applies\n * or an applicable started window is used up to the reserve; a window whose reset has passed counts\n * as fresh, whatever the reading said.\n */\nconst accountRank = (\n reading: PoolReading,\n route: string,\n nowMs: number,\n reservePercent: number\n): AccountRank => {\n let usable = false\n let longest = 0\n let resetsAtMs: number | undefined\n\n for (const window of reading.windows) {\n if (!appliesTo(window, route)) {\n continue\n }\n\n const started = hasStarted(window, nowMs)\n\n if (started && window.usedPercent >= 100 - reservePercent) {\n return { kind: 'exhausted' }\n }\n\n usable = true\n\n if (window.durationMinutes === undefined) {\n continue\n }\n\n if (window.durationMinutes > longest) {\n longest = window.durationMinutes\n resetsAtMs = started ? window.resetsAtMs : undefined\n } else if (\n window.durationMinutes === longest &&\n started &&\n window.resetsAtMs !== undefined &&\n (resetsAtMs === undefined || window.resetsAtMs < resetsAtMs)\n ) {\n resetsAtMs = window.resetsAtMs\n }\n }\n\n return usable ? { kind: 'usable', resetsAtMs } : { kind: 'unknown' }\n}\n\ntype Ranked = { readonly candidate: PoolCandidate; readonly resetsAtMs: number | undefined }\n\n/** Code-unit id order, so ties never depend on locale. */\nconst idBefore = (a: string, b: string) => a < b\n\n/**\n * Started windows by soonest reset, then windows that have not started; ties by higher priority,\n * then lower id.\n */\nconst ranksBefore = (a: Ranked, b: Ranked) => {\n if (a.resetsAtMs !== undefined && b.resetsAtMs !== undefined) {\n if (a.resetsAtMs !== b.resetsAtMs) {\n return a.resetsAtMs < b.resetsAtMs\n }\n } else if (a.resetsAtMs !== undefined || b.resetsAtMs !== undefined) {\n return a.resetsAtMs !== undefined\n }\n\n if (a.candidate.priority !== b.candidate.priority) {\n return a.candidate.priority > b.candidate.priority\n }\n\n return idBefore(a.candidate.id, b.candidate.id)\n}\n\n/** Highest priority, then lowest id. */\nconst fillFirst = (candidates: ReadonlyArray<PoolCandidate>): PoolCandidate | undefined => {\n let best: PoolCandidate | undefined\n\n for (const candidate of candidates) {\n if (\n best === undefined ||\n candidate.priority > best.priority ||\n (candidate.priority === best.priority && idBefore(candidate.id, best.id))\n ) {\n best = candidate\n }\n }\n\n return best\n}\n\n/**\n * Expiring-first: the available account whose longest applicable window resets soonest and still\n * has quota. When no available account is usable by a fresh reading (within `maxAgeMs`), it routes\n * stickily: the pin while it is available, else fill-first, preferring accounts that no fresh\n * reading shows used up; those serve only when nothing else is left, so the provider's own answer\n * decides. Undefined when no account is available.\n */\nexport const rankPool = (input: PoolRankInput): PoolDecision | undefined => {\n const candidates = input.accounts.filter(account => !input.unavailable.has(account.id))\n const exhausted = new Set<string>()\n\n let best: Ranked | undefined\n\n for (const candidate of candidates) {\n const reading = input.readings.get(candidate.id)\n\n if (reading === undefined || input.nowMs - reading.fetchedAtMs > input.maxAgeMs) {\n continue\n }\n\n const rank = accountRank(reading, input.route, input.nowMs, input.reservePercent)\n\n if (rank.kind === 'exhausted') {\n exhausted.add(candidate.id)\n }\n\n if (rank.kind !== 'usable') {\n continue\n }\n\n const next: Ranked = { candidate, resetsAtMs: rank.resetsAtMs }\n\n if (best === undefined || ranksBefore(next, best)) {\n best = next\n }\n }\n\n if (best !== undefined) {\n return PoolDecision.Quota({ accountId: best.candidate.id, resetsAtMs: best.resetsAtMs })\n }\n\n const unexhausted = candidates.filter(candidate => !exhausted.has(candidate.id))\n const fallback = unexhausted.length > 0 ? unexhausted : candidates\n\n if (input.pin !== undefined && fallback.some(candidate => candidate.id === input.pin)) {\n return PoolDecision.Sticky({ accountId: input.pin })\n }\n\n const first = fillFirst(fallback)\n\n return first === undefined ? undefined : PoolDecision.FillFirst({ accountId: first.id })\n}\n\n/** Whether a cooldown still keeps its account out at `nowMs`. */\nexport const isCooldownActive = (cooldown: PoolCooldown, nowMs: number) => cooldown.untilMs > nowMs\n\n/**\n * Every account-wide window of the reading has quota left or has reset; model-scoped windows block\n * only their models through ranking, never the whole account. A reading without account-wide\n * windows proves nothing.\n */\nconst hasQuotaLeft = (reading: PoolReading, nowMs: number) => {\n const accountWide = reading.windows.filter(window => window.models.length === 0)\n\n return (\n accountWide.length > 0 &&\n accountWide.every(window => !hasStarted(window, nowMs) || window.usedPercent < 100)\n )\n}\n\n/**\n * Active quota cooldowns that a fresh reading taken after they began shows to be over (a limit\n * reset was spent, or the provider reset early). A reading from before the lockout never clears\n * one, and auth cooldowns are never cleared by readings.\n */\nexport const staleQuotaCooldowns = (input: {\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly readings: ReadonlyMap<string, PoolReading>\n readonly nowMs: number\n readonly maxAgeMs: number\n}): ReadonlyArray<PoolCooldown> =>\n input.cooldowns.filter(cooldown => {\n if (\n !Predicate.isTagged(cooldown, 'PoolQuotaCooldown') ||\n !isCooldownActive(cooldown, input.nowMs)\n ) {\n return false\n }\n\n const reading = input.readings.get(cooldown.accountId)\n\n return (\n reading !== undefined &&\n input.nowMs - reading.fetchedAtMs <= input.maxAgeMs &&\n reading.fetchedAtMs > cooldown.startedAtMs &&\n hasQuotaLeft(reading, input.nowMs)\n )\n })\n"],"mappings":";;;;;;AAiCA,MAAa,aAAa,UAAkB,MAAM,KAAK,EAAE,YAAY;;;;;;AAOrE,MAAa,2BACX,aAC4B;CAC5B,MAAM,cAAc,KAAK,MAAM,SAAS,SAAS;CAEjD,IAAI,CAAC,OAAO,SAAS,WAAW,GAC9B;CA8BF,OAAO;EAAE;EAAa,SA3BN,MAAM,KAAK,SAAS,OAAO,EAAE,KAAK,WAAuB;GACvE,MAAM,aACJ,OAAO,aAAa,KAAA,IAChB,KAAK,MAAM,OAAO,QAAQ,IAC1B,OAAO,uBAAuB,KAAA,IAC5B,cAAc,OAAO,qBAAqB,MAC1C;GAER,MAAM,UAAU,OAAO,UAAU,CAAC,GAAG,SAAQ,UAAS;IACpD,MAAM,aAAa,MAAM,KAAK,EAAE,YAAY;IAE5C,OAAO,eAAe,KAAK,CAAC,IAAI,CAAC,UAAU;GAC7C,CAAC;GAED,MAAM,SAA4B;IAAE,aAAa,OAAO;IAAa;GAAO;GAE5E,IAAI,OAAO,0BAA0B,KAAA,GACnC,OAAO,kBAAkB,OAAO;GAGlC,IAAI,OAAO,SAAS,UAAU,GAC5B,OAAO,aAAa;GAGtB,OAAO;EACT,CAE4B;CAAE;AAChC;AAYA,MAAa,eAAe,KAAK,WAAyB;AAgB1D,MAAM,aAAa,QAAoB,UACrC,OAAO,OAAO,WAAW,KAAK,OAAO,OAAO,MAAK,UAAS,MAAM,SAAS,KAAK,CAAC;AAEjF,MAAM,cAAc,QAA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AAO,WAAU,OAAO,OAAO,WAAW,CAAC;CAE/E,OACE,YAAY,SAAS,KACrB,YAAY,OAAM,WAAU,CAAC,WAAW,QAAQ,KAAK,KAAK,OAAO,cAAc,GAAG;AAEtF;;;;;;AAOA,MAAa,uBAAuB,UAMlC,MAAM,UAAU,QAAO,aAAY;CACjC,IACE,CAAC,UAAU,SAAS,UAAU,mBAAmB,KACjD,CAAC,iBAAiB,UAAU,MAAM,KAAK,GAEvC,OAAO;CAGT,MAAM,UAAU,MAAM,SAAS,IAAI,SAAS,SAAS;CAErD,OACE,YAAY,KAAA,KACZ,MAAM,QAAQ,QAAQ,eAAe,MAAM,YAC3C,QAAQ,cAAc,SAAS,eAC/B,aAAa,SAAS,MAAM,KAAK;AAErC,CAAC"}
1
+ {"version":3,"file":"rank.mjs","names":[],"sources":["../src/rank.ts"],"sourcesContent":["import { Data, Predicate } from 'effect'\nimport type { ProviderSubscriptionUsageSnapshot } from '@yolk-sdk/agent/providers/subscription-usage'\nimport type { PoolCooldown } from './model.ts'\n\n/** One usage window of a reading, as the ranking reads it. */\nexport type PoolWindow = {\n /** Window length; absent when the provider's window has no fixed length (a billing month). */\n readonly durationMinutes?: number\n readonly usedPercent: number\n /** When the window resets; absent when it has not started. */\n readonly resetsAtMs?: number\n /** Lowercase model substrings the window limits; empty for every model. */\n readonly models: ReadonlyArray<string>\n}\n\ntype MutablePoolWindow = { -readonly [Key in keyof PoolWindow]: PoolWindow[Key] }\n\n/** The latest quota reading of one account. */\nexport type PoolReading = {\n readonly fetchedAtMs: number\n readonly windows: ReadonlyArray<PoolWindow>\n}\n\n/** A configured account as the ranking sees it. */\nexport type PoolCandidate = {\n readonly id: string\n readonly priority: number\n}\n\n/**\n * The route a request pins: its model id, trimmed and lowercased. Reasoning effort is a separate\n * request field and never part of the route.\n */\nexport const poolRoute = (model: string) => model.trim().toLowerCase()\n\n/**\n * A usage snapshot as the ranking reads it; an unreadable `fetchedAt` yields no reading. Windows\n * without a length still block a used-up account but never rank it. A window that reports only\n * `resetsAfterSeconds` resets that long after the reading.\n */\nexport const poolReadingFromSnapshot = (\n snapshot: ProviderSubscriptionUsageSnapshot\n): PoolReading | undefined => {\n const fetchedAtMs = Date.parse(snapshot.fetchedAt)\n\n if (!Number.isFinite(fetchedAtMs)) {\n return undefined\n }\n\n const windows = Array.from(snapshot.windows).map((window): PoolWindow => {\n const resetsAtMs =\n window.resetsAt !== undefined\n ? Date.parse(window.resetsAt)\n : window.resetsAfterSeconds !== undefined\n ? fetchedAtMs + window.resetsAfterSeconds * 1000\n : Number.NaN\n\n const models = (window.models ?? []).flatMap(model => {\n const normalized = model.trim().toLowerCase()\n\n return normalized === '' ? [] : [normalized]\n })\n\n const fields: MutablePoolWindow = { usedPercent: window.usedPercent, models }\n\n if (window.windowDurationMinutes !== undefined) {\n fields.durationMinutes = window.windowDurationMinutes\n }\n\n if (Number.isFinite(resetsAtMs)) {\n fields.resetsAtMs = resetsAtMs\n }\n\n return fields\n })\n\n return { fetchedAtMs, windows }\n}\n\n/** Why the ranking chose an account. */\nexport type PoolDecision = Data.TaggedEnum<{\n /** Fresh readings ranked it first; `resetsAtMs` is its longest window's reset, if started. */\n Quota: { readonly accountId: string; readonly resetsAtMs: number | undefined }\n /** No candidate is usable by a fresh reading; the route's pin is still available. */\n Sticky: { readonly accountId: string }\n /** No candidate is usable by a fresh reading and no pin is: highest priority, then lowest id. */\n FillFirst: { readonly accountId: string }\n}>\n\nexport const PoolDecision = Data.taggedEnum<PoolDecision>()\n\nexport type PoolRankInput = {\n /** Configured accounts. */\n readonly accounts: ReadonlyArray<PoolCandidate>\n /** Accounts that cannot serve now: an active cooldown, or already tried by this request. */\n readonly unavailable: ReadonlySet<string>\n readonly readings: ReadonlyMap<string, PoolReading>\n /** The account the route is pinned to, if any. */\n readonly pin: string | undefined\n readonly route: string\n readonly nowMs: number\n readonly maxAgeMs: number\n readonly reservePercent: number\n}\n\nconst appliesTo = (window: PoolWindow, route: string) =>\n window.models.length === 0 || window.models.some(model => route.includes(model))\n\nconst hasStarted = (window: PoolWindow, nowMs: number) =>\n window.resetsAtMs !== undefined && window.resetsAtMs > nowMs\n\ntype AccountRank =\n /** No window of the reading applies to the route: the reading says nothing. */\n | { readonly kind: 'unknown' }\n /** An applicable started window is used up to the reserve. */\n | { readonly kind: 'exhausted' }\n | { readonly kind: 'usable'; readonly resetsAtMs: number | undefined }\n\n/**\n * The reset of the account's longest window with a length that applies to the route (undefined\n * when it has not started, or no applicable window has a length). Unusable when no window applies\n * or an applicable started window is used up to the reserve; a window whose reset has passed counts\n * as fresh, whatever the reading said.\n */\nconst accountRank = (\n reading: PoolReading,\n route: string,\n nowMs: number,\n reservePercent: number\n): AccountRank => {\n let usable = false\n let longest = 0\n let resetsAtMs: number | undefined\n\n for (const window of reading.windows) {\n if (!appliesTo(window, route)) {\n continue\n }\n\n const started = hasStarted(window, nowMs)\n\n if (started && window.usedPercent >= 100 - reservePercent) {\n return { kind: 'exhausted' }\n }\n\n usable = true\n\n if (window.durationMinutes === undefined) {\n continue\n }\n\n if (window.durationMinutes > longest) {\n longest = window.durationMinutes\n resetsAtMs = started ? window.resetsAtMs : undefined\n } else if (\n window.durationMinutes === longest &&\n started &&\n window.resetsAtMs !== undefined &&\n (resetsAtMs === undefined || window.resetsAtMs < resetsAtMs)\n ) {\n resetsAtMs = window.resetsAtMs\n }\n }\n\n return usable ? { kind: 'usable', resetsAtMs } : { kind: 'unknown' }\n}\n\ntype Ranked = { readonly candidate: PoolCandidate; readonly resetsAtMs: number | undefined }\n\n/** Code-unit id order, so ties never depend on locale. */\nconst idBefore = (a: string, b: string) => a < b\n\n/**\n * Started windows by soonest reset, then windows that have not started; ties by higher priority,\n * then lower id.\n */\nconst ranksBefore = (a: Ranked, b: Ranked) => {\n if (a.resetsAtMs !== undefined && b.resetsAtMs !== undefined) {\n if (a.resetsAtMs !== b.resetsAtMs) {\n return a.resetsAtMs < b.resetsAtMs\n }\n } else if (a.resetsAtMs !== undefined || b.resetsAtMs !== undefined) {\n return a.resetsAtMs !== undefined\n }\n\n if (a.candidate.priority !== b.candidate.priority) {\n return a.candidate.priority > b.candidate.priority\n }\n\n return idBefore(a.candidate.id, b.candidate.id)\n}\n\n/** Highest priority, then lowest id. */\nconst fillFirst = (candidates: ReadonlyArray<PoolCandidate>): PoolCandidate | undefined => {\n let best: PoolCandidate | undefined\n\n for (const candidate of candidates) {\n if (\n best === undefined ||\n candidate.priority > best.priority ||\n (candidate.priority === best.priority && idBefore(candidate.id, best.id))\n ) {\n best = candidate\n }\n }\n\n return best\n}\n\n/**\n * Expiring-first: the available account whose longest applicable window resets soonest and still\n * has quota. When no available account is usable by a fresh reading (within `maxAgeMs`), it routes\n * stickily: the pin while it is available, else fill-first, preferring accounts that no fresh\n * reading shows used up; those serve only when nothing else is left, so the provider's own answer\n * decides. Undefined when no account is available.\n */\nexport const rankPool = (input: PoolRankInput): PoolDecision | undefined => {\n const candidates = input.accounts.filter(account => !input.unavailable.has(account.id))\n const exhausted = new Set<string>()\n\n let best: Ranked | undefined\n\n for (const candidate of candidates) {\n const reading = input.readings.get(candidate.id)\n\n if (reading === undefined || input.nowMs - reading.fetchedAtMs > input.maxAgeMs) {\n continue\n }\n\n const rank = accountRank(reading, input.route, input.nowMs, input.reservePercent)\n\n if (rank.kind === 'exhausted') {\n exhausted.add(candidate.id)\n }\n\n if (rank.kind !== 'usable') {\n continue\n }\n\n const next: Ranked = { candidate, resetsAtMs: rank.resetsAtMs }\n\n if (best === undefined || ranksBefore(next, best)) {\n best = next\n }\n }\n\n if (best !== undefined) {\n return PoolDecision.Quota({ accountId: best.candidate.id, resetsAtMs: best.resetsAtMs })\n }\n\n const unexhausted = candidates.filter(candidate => !exhausted.has(candidate.id))\n const fallback = unexhausted.length > 0 ? unexhausted : candidates\n\n if (input.pin !== undefined && fallback.some(candidate => candidate.id === input.pin)) {\n return PoolDecision.Sticky({ accountId: input.pin })\n }\n\n const first = fillFirst(fallback)\n\n return first === undefined ? undefined : PoolDecision.FillFirst({ accountId: first.id })\n}\n\n/** Whether a cooldown still keeps its account out at `nowMs`. */\nexport const isCooldownActive = (cooldown: PoolCooldown, nowMs: number) => cooldown.untilMs > nowMs\n\n/**\n * Every account-wide window of the reading has quota left or has reset; model-scoped windows block\n * only their models through ranking, never the whole account. A reading without account-wide\n * windows proves nothing.\n */\nconst hasQuotaLeft = (reading: PoolReading, nowMs: number) => {\n const accountWide = reading.windows.filter(window => window.models.length === 0)\n\n return (\n accountWide.length > 0 &&\n accountWide.every(window => !hasStarted(window, nowMs) || window.usedPercent < 100)\n )\n}\n\n/**\n * Active quota cooldowns that a fresh reading taken after they began shows to be over (a limit\n * reset was spent, or the provider reset early). A reading from before the lockout never clears\n * one, and auth cooldowns are never cleared by readings.\n */\nexport const staleQuotaCooldowns = (input: {\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly readings: ReadonlyMap<string, PoolReading>\n readonly nowMs: number\n readonly maxAgeMs: number\n}): ReadonlyArray<PoolCooldown> =>\n input.cooldowns.filter(cooldown => {\n if (\n !Predicate.isTagged(cooldown, 'PoolQuotaCooldown') ||\n !isCooldownActive(cooldown, input.nowMs)\n ) {\n return false\n }\n\n const reading = input.readings.get(cooldown.accountId)\n\n return (\n reading !== undefined &&\n input.nowMs - reading.fetchedAtMs <= input.maxAgeMs &&\n reading.fetchedAtMs > cooldown.startedAtMs &&\n hasQuotaLeft(reading, input.nowMs)\n )\n })\n"],"mappings":";;;;;;AAiCA,MAAa,aAAa,UAAkB,MAAM,KAAK,CAAC,CAAC,YAAY;;;;;;AAOrE,MAAa,2BACX,aAC4B;CAC5B,MAAM,cAAc,KAAK,MAAM,SAAS,SAAS;CAEjD,IAAI,CAAC,OAAO,SAAS,WAAW,GAC9B;CA8BF,OAAO;EAAE;EAAa,SA3BN,MAAM,KAAK,SAAS,OAAO,CAAC,CAAC,KAAK,WAAuB;GACvE,MAAM,aACJ,OAAO,aAAa,KAAA,IAChB,KAAK,MAAM,OAAO,QAAQ,IAC1B,O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AS,KAAA,GACX,OAAO,aAAa,MAAM;EAAE,WAAW,KAAK,UAAU;EAAI,YAAY,KAAK;CAAW,CAAC;CAGzF,MAAM,cAAc,WAAW,QAAO,cAAa,CAAC,UAAU,IAAI,UAAU,EAAE,CAAC;CAC/E,MAAM,WAAW,YAAY,SAAS,IAAI,cAAc;CAExD,IAAI,MAAM,QAAQ,KAAA,KAAa,SAAS,MAAK,cAAa,UAAU,OAAO,MAAM,GAAG,GAClF,OAAO,aAAa,OAAO,EAAE,WAAW,MAAM,IAAI,CAAC;CAGrD,MAAM,QAAQ,UAAU,QAAQ;CAEhC,OAAO,UAAU,KAAA,IAAY,KAAA,IAAY,aAAa,UAAU,EAAE,WAAW,MAAM,GAAG,CAAC;AACzF;;AAGA,MAAa,oBAAoB,UAAwB,UAAkB,SAAS,UAAU;;;;;;AAO9F,MAAM,gBAAgB,SAAsB,UAAkB;CAC5D,MAAM,cAAc,QAAQ,QAAQ,QAAO,WAAU,OAAO,OAAO,WAAW,CAAC;CAE/E,OACE,YAAY,SAAS,KACrB,YAAY,OAAM,WAAU,CAAC,WAAW,QAAQ,KAAK,KAAK,OAAO,cAAc,GAAG;AAEtF;;;;;;AAOA,MAAa,uBAAuB,UAMlC,MAAM,UAAU,QAAO,aAAY;CACjC,IACE,CAAC,UAAU,SAAS,UAAU,mBAAmB,KACjD,CAAC,iBAAiB,UAAU,MAAM,KAAK,GAEvC,OAAO;CAGT,MAAM,UAAU,MAAM,SAAS,IAAI,SAAS,SAAS;CAErD,OACE,YAAY,KAAA,KACZ,MAAM,QAAQ,QAAQ,eAAe,MAAM,YAC3C,QAAQ,cAAc,SAAS,eAC/B,aAAa,SAAS,MAAM,KAAK;AAErC,CAAC"}
package/dist/store.d.mts CHANGED
@@ -2,22 +2,20 @@ import { PoolCooldown, PoolPin, PoolStoreError } from "./model.mjs";
2
2
  import { Effect } from "effect";
3
3
  import * as Schema from "effect/Schema";
4
4
  import { ProviderSubscriptionUsageSnapshot } from "@yolk-sdk/agent/providers/subscription-usage";
5
-
6
- //#region src/store.d.ts
7
5
  declare const PoolStoredReading_base: Schema.Class<PoolStoredReading, Schema.Struct<{
8
6
  readonly accountId: Schema.Trimmed;
9
7
  readonly snapshot: typeof ProviderSubscriptionUsageSnapshot;
10
8
  }>, {}>;
11
9
  /** The latest usage snapshot of one account. */
12
- declare class PoolStoredReading extends PoolStoredReading_base {}
10
+ export declare class PoolStoredReading extends PoolStoredReading_base {}
13
11
  /** What one pick reads: the route's pin plus the cooldowns and readings of the pool's accounts. */
14
- type PoolStoreSnapshot = {
12
+ export type PoolStoreSnapshot = {
15
13
  readonly pin: PoolPin | undefined;
16
14
  readonly cooldowns: ReadonlyArray<PoolCooldown>;
17
15
  readonly readings: ReadonlyArray<PoolStoredReading>;
18
16
  };
19
17
  /** Everything a store holds for one pool, for host UIs. */
20
- type PoolStoreState = {
18
+ export type PoolStoreState = {
21
19
  readonly pins: ReadonlyArray<PoolPin>;
22
20
  readonly cooldowns: ReadonlyArray<PoolCooldown>;
23
21
  readonly readings: ReadonlyArray<PoolStoredReading>;
@@ -32,8 +30,9 @@ type PoolStoreState = {
32
30
  * - Operations must be atomic per key: concurrent `movePin` calls with the same `expected` let
33
31
  * exactly one through.
34
32
  */
35
- type PoolStoreApi = {
36
- /** The route's pin and the requested accounts' cooldowns (expired ones included) and readings. */readonly load: (input: {
33
+ export type PoolStoreApi = {
34
+ /** The route's pin and the requested accounts' cooldowns (expired ones included) and readings. */
35
+ readonly load: (input: {
37
36
  readonly poolId: string;
38
37
  readonly route: string;
39
38
  readonly accountIds: ReadonlyArray<string>;
@@ -59,8 +58,10 @@ type PoolStoreApi = {
59
58
  readonly clearCooldown: (input: {
60
59
  readonly accountId: string;
61
60
  readonly startedAtMs: number;
62
- }) => Effect.Effect<boolean, PoolStoreError>; /** Records a reading unless the stored one was fetched later. */
63
- readonly putReading: (reading: PoolStoredReading) => Effect.Effect<void, PoolStoreError>; /** The pool's pins and the requested accounts' cooldowns and readings. */
61
+ }) => Effect.Effect<boolean, PoolStoreError>;
62
+ /** Records a reading unless the stored one was fetched later. */
63
+ readonly putReading: (reading: PoolStoredReading) => Effect.Effect<void, PoolStoreError>;
64
+ /** The pool's pins and the requested accounts' cooldowns and readings. */
64
65
  readonly state: (input: {
65
66
  readonly poolId: string;
66
67
  readonly accountIds: ReadonlyArray<string>;
@@ -70,20 +71,19 @@ type PoolStoreApi = {
70
71
  * An in-memory `PoolStoreApi`: state lives in this process only, so it suits tests and hosts that
71
72
  * run one long-lived process. Serverless hosts implement the port on their database.
72
73
  */
73
- declare const makeMemoryPoolStore: () => PoolStoreApi;
74
+ export declare const makeMemoryPoolStore: () => PoolStoreApi;
74
75
  declare const PoolStoreContractViolation_base: Schema.Class<PoolStoreContractViolation, Schema.TaggedStruct<"PoolStoreContractViolation", {
75
76
  readonly case: Schema.String;
76
77
  readonly message: Schema.String;
77
78
  }>, import("effect/Cause").YieldableError>;
78
79
  /** A `PoolStoreApi` implementation broke the store contract. */
79
- declare class PoolStoreContractViolation extends PoolStoreContractViolation_base {}
80
+ export declare class PoolStoreContractViolation extends PoolStoreContractViolation_base {}
80
81
  /** One semantic every `PoolStoreApi` must meet. Run each case against a fresh, empty store. */
81
- type PoolStoreContractCase = {
82
+ export type PoolStoreContractCase = {
82
83
  readonly name: string;
83
84
  readonly run: (store: PoolStoreApi) => Effect.Effect<void, PoolStoreContractViolation | PoolStoreError>;
84
85
  };
85
86
  /** The `PoolStoreApi` contract. Hosts run every case against a fresh store of their implementation. */
86
- declare const poolStoreContract: ReadonlyArray<PoolStoreContractCase>;
87
- //#endregion
88
- export { PoolStoreApi, PoolStoreContractCase, PoolStoreContractViolation, PoolStoreError, PoolStoreSnapshot, PoolStoreState, PoolStoredReading, makeMemoryPoolStore, poolStoreContract };
87
+ export declare const poolStoreContract: ReadonlyArray<PoolStoreContractCase>;
88
+ export { PoolStoreError };
89
89
  //# sourceMappingURL=store.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"store.d.mts","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W,qBAAA;EAAA,SACD,IAAA;EAAA,SACA,GAAA,GACP,KAAA,EAAO,YAAA,KACJ,MAAA,CAAO,MAAA,OAAa,0BAAA,GAA6B,cAAA;AAAA;;cA+B3C,iBAAA,EAAmB,aAAa,CAAC,qBAAA"}
1
+ {"version":3,"file":"store.d.mts","names":[],"sources":["../src/store.ts"],"mappings":";;;;;;;;;qBAea,0BAA0B;;YAM3B;WACD,KAAK;WACL,WAAW,cAAc;WACzB,UAAU,cAAc;;;YAIvB;WACD,MAAM,cAAc;WACpB,WAAW,cAAc;WACzB,UAAU,cAAc;;;;;;;;;;;;YAavB;;WAED,OAAO;aACL;aACA;aACA,YAAY;QACjB,OAAO,OAAO,mBAAmB;;;;;WAK9B,UAAU;aACR;aACA,KAAK;QACV,OAAO,gBAAgB;;;;;;WAMpB,cAAc,UAAU,iBAAiB,OAAO,aAAa;;;;;WAK7D,gBAAgB;aACd;aACA;QACL,OAAO,gBAAgB;;WAEpB,aAAa,SAAS,sBAAsB,OAAO,aAAa;;WAEhE,QAAQ;aACN;aACA,YAAY;QACjB,OAAO,OAAO,gBAAgB;;;;;;qBAWzB,2BAA0B;;;;;;qBA2E1B,mCAAmC;;YASpC;WACD;WACA,MACP,OAAO,iBACJ,OAAO,aAAa,6BAA6B;;;qBA+B3C,mBAAmB,cAAc"}
package/dist/store.mjs CHANGED
@@ -2,7 +2,7 @@ import { PoolAccountId, PoolAuthCooldown, PoolPin, PoolQuotaCooldown, PoolStoreE
2
2
  import { Chunk, Effect, Predicate } from "effect";
3
3
  import * as Schema from "effect/Schema";
4
4
  import { ProviderSubscriptionUsageSnapshot } from "@yolk-sdk/agent/providers/subscription-usage";
5
- //#region src/store.ts
5
+ // src/store.ts
6
6
  /** The latest usage snapshot of one account. */
7
7
  var PoolStoredReading = class extends Schema.Class("PoolStoredReading")({
8
8
  accountId: PoolAccountId,
@@ -247,7 +247,6 @@ const poolStoreContract = [
247
247
  yield* expect((yield* load).readings[0]?.snapshot.fetchedAt === "2026-10-15T13:00:00.000Z", "a newer reading replaces the stored one");
248
248
  }))
249
249
  ];
250
- //#endregion
251
250
  export { PoolStoreContractViolation, PoolStoreError, PoolStoredReading, makeMemoryPoolStore, poolStoreContract };
252
251
 
253
252
  //# sourceMappingURL=store.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"store.mjs","names":[],"sources":["../src/store.ts"],"sourcesContent":["import { Chunk, Effect, Predicate } from 'effect'\nimport * as Schema from 'effect/Schema'\nimport { ProviderSubscriptionUsageSnapshot } from '@yolk-sdk/agent/providers/subscription-usage'\nimport {\n PoolAccountId,\n PoolAuthCooldown,\n PoolPin,\n PoolQuotaCooldown,\n type PoolCooldown,\n type PoolStoreError\n} from './model.ts'\n\nexport { PoolStoreError } from './model.ts'\n\n/** The latest usage snapshot of one account. */\nexport class PoolStoredReading extends Schema.Class<PoolStoredReading>('PoolStoredReading')({\n accountId: PoolAccountId,\n snapshot: ProviderSubscriptionUsageSnapshot\n}) {}\n\n/** What one pick reads: the route's pin plus the cooldowns and readings of the pool's accounts. */\nexport type PoolStoreSnapshot = {\n readonly pin: PoolPin | undefined\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly readings: ReadonlyArray<PoolStoredReading>\n}\n\n/** Everything a store holds for one pool, for host UIs. */\nexport type PoolStoreState = {\n readonly pins: ReadonlyArray<PoolPin>\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly readings: ReadonlyArray<PoolStoredReading>\n}\n\n/**\n * Pool state shared by every instance of a host (ADR 0011). Hosts implement it on their database;\n * `makeMemoryPoolStore` serves tests and single-process hosts, and `poolStoreContract` pins the\n * semantics every implementation must meet.\n *\n * - Pins are keyed by `(poolId, route)`; cooldowns and readings by account id, so a cooldown or\n * reading applies to every pool the account is in.\n * - Operations must be atomic per key: concurrent `movePin` calls with the same `expected` let\n * exactly one through.\n */\nexport type PoolStoreApi = {\n /** The route's pin and the requested accounts' cooldowns (expired ones included) and readings. */\n readonly load: (input: {\n readonly poolId: string\n readonly route: string\n readonly accountIds: ReadonlyArray<string>\n }) => Effect.Effect<PoolStoreSnapshot, PoolStoreError>\n /**\n * Compare-and-set: writes `pin` when the route's current account is `expected` (`undefined`:\n * the route has no pin yet). Returns whether it wrote.\n */\n readonly movePin: (input: {\n readonly expected: string | undefined\n readonly pin: PoolPin\n }) => Effect.Effect<boolean, PoolStoreError>\n /**\n * Records a cooldown; when the account already has one, the one that ends later stays, and of two\n * that end together the one that started later (a newer failure must not be cleared by a reading\n * taken before it).\n */\n readonly putCooldown: (cooldown: PoolCooldown) => Effect.Effect<void, PoolStoreError>\n /**\n * Removes the account's cooldown only when it is the one observed (same `startedAtMs`). Returns\n * whether it removed one.\n */\n readonly clearCooldown: (input: {\n readonly accountId: string\n readonly startedAtMs: number\n }) => Effect.Effect<boolean, PoolStoreError>\n /** Records a reading unless the stored one was fetched later. */\n readonly putReading: (reading: PoolStoredReading) => Effect.Effect<void, PoolStoreError>\n /** The pool's pins and the requested accounts' cooldowns and readings. */\n readonly state: (input: {\n readonly poolId: string\n readonly accountIds: ReadonlyArray<string>\n }) => Effect.Effect<PoolStoreState, PoolStoreError>\n}\n\nconst pinKey = (poolId: string, route: string) => JSON.stringify([poolId, route])\n\nconst fetchedAtMs = (reading: PoolStoredReading) => Date.parse(reading.snapshot.fetchedAt)\n\n/**\n * An in-memory `PoolStoreApi`: state lives in this process only, so it suits tests and hosts that\n * run one long-lived process. Serverless hosts implement the port on their database.\n */\nexport const makeMemoryPoolStore = (): PoolStoreApi => {\n const pins = new Map<string, PoolPin>()\n const cooldowns = new Map<string, PoolCooldown>()\n const readings = new Map<string, PoolStoredReading>()\n\n const pick = <A>(source: Map<string, A>, accountIds: ReadonlyArray<string>) =>\n accountIds.flatMap(accountId => {\n const value = source.get(accountId)\n\n return value === undefined ? [] : [value]\n })\n\n return {\n load: input =>\n Effect.sync(() => ({\n pin: pins.get(pinKey(input.poolId, input.route)),\n cooldowns: pick(cooldowns, input.accountIds),\n readings: pick(readings, input.accountIds)\n })),\n movePin: input =>\n Effect.sync(() => {\n const key = pinKey(input.pin.poolId, input.pin.route)\n\n if (pins.get(key)?.accountId !== input.expected) {\n return false\n }\n\n pins.set(key, input.pin)\n\n return true\n }),\n putCooldown: cooldown =>\n Effect.sync(() => {\n const current = cooldowns.get(cooldown.accountId)\n\n if (\n current === undefined ||\n cooldown.untilMs > current.untilMs ||\n (cooldown.untilMs === current.untilMs && cooldown.startedAtMs > current.startedAtMs)\n ) {\n cooldowns.set(cooldown.accountId, cooldown)\n }\n }),\n clearCooldown: input =>\n Effect.sync(() => {\n const current = cooldowns.get(input.accountId)\n\n if (current === undefined || current.startedAtMs !== input.startedAtMs) {\n return false\n }\n\n cooldowns.delete(input.accountId)\n\n return true\n }),\n putReading: reading =>\n Effect.sync(() => {\n const current = readings.get(reading.accountId)\n\n if (current === undefined || fetchedAtMs(reading) >= fetchedAtMs(current)) {\n readings.set(reading.accountId, reading)\n }\n }),\n state: input =>\n Effect.sync(() => ({\n pins: Array.from(pins.values())\n .filter(pin => pin.poolId === input.poolId)\n .sort((a, b) => (a.route < b.route ? -1 : a.route > b.route ? 1 : 0)),\n cooldowns: pick(cooldowns, input.accountIds),\n readings: pick(readings, input.accountIds)\n }))\n }\n}\n\n/** A `PoolStoreApi` implementation broke the store contract. */\nexport class PoolStoreContractViolation extends Schema.TaggedError<PoolStoreContractViolation>()(\n 'PoolStoreContractViolation',\n {\n case: Schema.String,\n message: Schema.String\n }\n) {}\n\n/** One semantic every `PoolStoreApi` must meet. Run each case against a fresh, empty store. */\nexport type PoolStoreContractCase = {\n readonly name: string\n readonly run: (\n store: PoolStoreApi\n ) => Effect.Effect<void, PoolStoreContractViolation | PoolStoreError>\n}\n\nconst contractCase = (\n name: string,\n run: (\n store: PoolStoreApi,\n expect: (condition: boolean, message: string) => Effect.Effect<void, PoolStoreContractViolation>\n ) => Effect.Effect<void, PoolStoreContractViolation | PoolStoreError>\n): PoolStoreContractCase => ({\n name,\n run: store =>\n run(store, (condition, message) =>\n condition ? Effect.void : Effect.fail(new PoolStoreContractViolation({ case: name, message }))\n )\n})\n\nconst contractPin = (route: string, accountId: string, pinnedAtMs: number, poolId = 'pool') =>\n PoolPin.make({ poolId, route, accountId, pinnedAtMs })\n\nconst contractReading = (accountId: string, fetchedAt: string) =>\n PoolStoredReading.make({\n accountId,\n snapshot: ProviderSubscriptionUsageSnapshot.make({\n provider: 'contract',\n fetchedAt,\n windows: Chunk.empty()\n })\n })\n\n/** The `PoolStoreApi` contract. Hosts run every case against a fresh store of their implementation. */\nexport const poolStoreContract: ReadonlyArray<PoolStoreContractCase> = [\n contractCase('pins are created once and moved by compare-and-set', (store, expect) =>\n Effect.gen(function* () {\n const load = store.load({ poolId: 'pool', route: 'model', accountIds: [] })\n\n yield* expect((yield* load).pin === undefined, 'a new route has no pin')\n yield* expect(\n yield* store.movePin({ expected: undefined, pin: contractPin('model', 'a', 1) }),\n 'the first pin is written'\n )\n yield* expect(\n !(yield* store.movePin({ expected: undefined, pin: contractPin('model', 'b', 2) })),\n 'creating a pin that already exists is refused'\n )\n yield* expect(\n !(yield* store.movePin({ expected: 'b', pin: contractPin('model', 'c', 3) })),\n 'a move expecting another account is refused'\n )\n yield* expect(\n yield* store.movePin({ expected: 'a', pin: contractPin('model', 'b', 4) }),\n 'a move expecting the current account is written'\n )\n\n const pin = (yield* load).pin\n\n yield* expect(\n pin?.accountId === 'b' && pin.pinnedAtMs === 4,\n 'load returns the latest pin with its time'\n )\n })\n ),\n contractCase('pins are keyed by pool and route', (store, expect) =>\n Effect.gen(function* () {\n yield* store.movePin({ expected: undefined, pin: contractPin('one', 'a', 1) })\n yield* store.movePin({ expected: undefined, pin: contractPin('two', 'b', 1) })\n yield* store.movePin({ expected: undefined, pin: contractPin('one', 'c', 1, 'other') })\n\n const one = yield* store.load({ poolId: 'pool', route: 'one', accountIds: [] })\n const two = yield* store.load({ poolId: 'pool', route: 'two', accountIds: [] })\n const other = yield* store.load({ poolId: 'other', route: 'one', accountIds: [] })\n\n yield* expect(\n one.pin?.accountId === 'a' && two.pin?.accountId === 'b' && other.pin?.accountId === 'c',\n 'each (pool, route) keeps its own pin'\n )\n\n const state = yield* store.state({ poolId: 'pool', accountIds: [] })\n\n yield* expect(\n state.pins.map(pin => pin.route).join() === 'one,two',\n 'state lists the pool’s pins only, ordered by route'\n )\n })\n ),\n contractCase('concurrent moves from the same pin let exactly one through', (store, expect) =>\n Effect.gen(function* () {\n yield* store.movePin({ expected: undefined, pin: contractPin('model', 'a', 1) })\n\n const results = yield* Effect.all(\n ['b', 'c', 'd', 'e'].map(accountId =>\n store.movePin({ expected: 'a', pin: contractPin('model', accountId, 2) })\n ),\n { concurrency: 'unbounded' }\n )\n\n yield* expect(results.filter(Boolean).length === 1, 'exactly one move is written')\n })\n ),\n contractCase('cooldowns keep the later end and clear only when observed', (store, expect) =>\n Effect.gen(function* () {\n const load = store.load({ poolId: 'pool', route: 'model', accountIds: ['a', 'b'] })\n\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'a', startedAtMs: 1, untilMs: 100 })\n )\n yield* store.putCooldown(\n PoolAuthCooldown.make({ accountId: 'a', startedAtMs: 2, untilMs: 50 })\n )\n\n const kept = (yield* load).cooldowns\n\n yield* expect(\n kept.length === 1 &&\n Predicate.isTagged(kept[0], 'PoolQuotaCooldown') &&\n kept[0].untilMs === 100,\n 'an earlier-ending cooldown does not replace a later one'\n )\n\n yield* store.putCooldown(\n PoolAuthCooldown.make({ accountId: 'a', startedAtMs: 3, untilMs: 200 })\n )\n\n const replaced = (yield* load).cooldowns\n\n yield* expect(\n replaced.length === 1 &&\n Predicate.isTagged(replaced[0], 'PoolAuthCooldown') &&\n replaced[0].startedAtMs === 3,\n 'a later-ending cooldown replaces the current one'\n )\n\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'a', startedAtMs: 4, untilMs: 200 })\n )\n\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'a', startedAtMs: 2, untilMs: 200 })\n )\n\n const renewed = (yield* load).cooldowns\n\n yield* expect(\n renewed.length === 1 && renewed[0]?.startedAtMs === 4,\n 'of two cooldowns that end together, the later-started one stays, whatever the write order'\n )\n yield* expect(\n !(yield* store.clearCooldown({ accountId: 'a', startedAtMs: 3 })),\n 'clearing a cooldown that is no longer current is refused'\n )\n yield* expect(\n yield* store.clearCooldown({ accountId: 'a', startedAtMs: 4 }),\n 'clearing the observed cooldown succeeds'\n )\n yield* expect((yield* load).cooldowns.length === 0, 'a cleared cooldown is gone')\n yield* expect(\n !(yield* store.clearCooldown({ accountId: 'b', startedAtMs: 3 })),\n 'clearing an account without a cooldown is refused'\n )\n })\n ),\n contractCase(\n 'cooldowns and readings are returned for the requested accounts only',\n (store, expect) =>\n Effect.gen(function* () {\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'a', startedAtMs: 1, untilMs: 9 })\n )\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'b', startedAtMs: 1, untilMs: 9 })\n )\n yield* store.putReading(contractReading('a', '2026-10-15T12:00:00.000Z'))\n yield* store.putReading(contractReading('b', '2026-10-15T12:00:00.000Z'))\n\n const snapshot = yield* store.load({ poolId: 'pool', route: 'model', accountIds: ['b'] })\n const state = yield* store.state({ poolId: 'pool', accountIds: ['a'] })\n\n yield* expect(\n snapshot.cooldowns.every(cooldown => cooldown.accountId === 'b') &&\n snapshot.cooldowns.length === 1 &&\n snapshot.readings.every(reading => reading.accountId === 'b') &&\n snapshot.readings.length === 1,\n 'load filters by account'\n )\n yield* expect(\n state.cooldowns.length === 1 &&\n state.cooldowns[0]?.accountId === 'a' &&\n state.readings.length === 1 &&\n state.readings[0]?.accountId === 'a',\n 'state filters by account'\n )\n })\n ),\n contractCase('readings keep the latest fetch', (store, expect) =>\n Effect.gen(function* () {\n const load = store.load({ poolId: 'pool', route: 'model', accountIds: ['a'] })\n\n yield* store.putReading(contractReading('a', '2026-10-15T12:00:00.000Z'))\n yield* store.putReading(contractReading('a', '2026-10-15T11:00:00.000Z'))\n\n yield* expect(\n (yield* load).readings[0]?.snapshot.fetchedAt === '2026-10-15T12:00:00.000Z',\n 'an older reading does not replace a newer one'\n )\n\n yield* store.putReading(contractReading('a', '2026-10-15T13:00:00.000Z'))\n\n yield* expect(\n (yield* load).readings[0]?.snapshot.fetchedAt === '2026-10-15T13:00:00.000Z',\n 'a newer reading replaces the stored one'\n )\n })\n )\n]\n"],"mappings":";;;;;;AAeA,IAAa,oBAAb,cAAuC,OAAO,MAAyB,mBAAmB,EAAE;CAC1F,WAAW;CACX,UAAU;AACZ,CAAC,EAAE,CAAC;AAgEJ,MAAM,UAAU,QAAgB,UAAkB,KAAK,UAAU,CAAC,QAAQ,KAAK,CAAC;AAEhF,MAAM,eAAe,YAA+B,KAAK,MAAM,QAAQ,SAAS,SAAS;;;;;AAMzF,MAAa,4BAA0C;CACrD,MAAM,uBAAO,IAAI,IAAqB;CACtC,MAAM,4BAAY,IAAI,IAA0B;CAChD,MAAM,2BAAW,IAAI,IAA+B;CAEpD,MAAM,QAAW,QAAwB,eACvC,WAAW,SAAQ,cAAa;EAC9B,MAAM,QAAQ,OAAO,IAAI,SAAS;EAElC,OAAO,UAAU,KAAA,IAAY,CAAC,IAAI,CAAC,KAAK;CAC1C,CAAC;CAEH,OAAO;EACL,OAAM,UACJ,OAAO,YAAY;GACjB,KAAK,KAAK,IAAI,OAAO,MAAM,QAAQ,MAAM,KAAK,CAAC;GAC/C,WAAW,KAAK,WAAW,MAAM,UAAU;GAC3C,UAAU,KAAK,UAAU,MAAM,UAAU;EAC3C,EAAE;EACJ,UAAS,UACP,OAAO,WAAW;GAChB,MAAM,MAAM,OAAO,MAAM,IAAI,QAAQ,MAAM,IAAI,KAAK;GAEpD,IAAI,KAAK,IAAI,GAAG,GAAG,cAAc,MAAM,UACrC,OAAO;GAGT,KAAK,IAAI,KAAK,MAAM,GAAG;GAEvB,OAAO;EACT,CAAC;EACH,cAAa,aACX,OAAO,WAAW;GAChB,MAAM,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KAAK;GAAE,WAAW;GAAK,aAAa;GAAG,SAAS;EAAE,CAAC,CACvE;EACA,OAAO,MAAM,WAAW,gBAAgB,KAAK,0BAA0B,CAAC;EACxE,OAAO,MAAM,WAAW,gBAAgB,KAAK,0BAA0B,CAAC;EAExE,MAAM,WAAW,OAAO,MAAM,KAAK;GAAE,QAAQ;GAAQ,OAAO;GAAS,YAAY,CAAC,GAAG;EAAE,CAAC;EACxF,MAAM,QAAQ,OAAO,MAAM,MAAM;GAAE,QAAQ;GAAQ,YAAY,CAAC,GAAG;EAAE,CAAC;EAEtE,OAAO,OACL,SAAS,UAAU,OAAM,aAAY,SAAS,cAAc,GAAG,KAC7D,SAAS,UAAU,WAAW,KAC9B,SAAS,SAAS,OAAM,YAAW,QAAQ,cAAc,GAAG,KAC5D,SAAS,SAAS,WAAW,GAC/B,yBACF;EACA,OAAO,OACL,MAAM,UAAU,WAAW,KACzB,MAAM,UAAU,IAAI,cAAc,OAClC,MAAM,SAAS,WAAW,KAC1B,MAAM,SAAS,IAAI,cAAc,KACnC,0BACF;CACF,CAAC,CACL;CACA,aAAa,mCAAmC,OAAO,WACrD,OAAO,IAAI,aAAa;EACtB,MAAM,OAAO,MAAM,KAAK;GAAE,QAAQ;GAAQ,OAAO;GAAS,YAAY,CAAC,GAAG;EAAE,CAAC;EAE7E,OAAO,MAAM,WAAW,gBAAgB,KAAK,0BAA0B,CAAC;EACxE,OAAO,MAAM,WAAW,gBAAgB,KAAK,0BAA0B,CAAC;EAExE,OAAO,QACJ,OAAO,MAAM,SAAS,IAAI,SAAS,cAAc,4BAClD,+CACF;EAEA,OAAO,MAAM,WAAW,gBAAgB,KAAK,0BAA0B,CAAC;EAExE,OAAO,QACJ,OAAO,MAAM,SAAS,IAAI,SAAS,cAAc,4BAClD,yCACF;CACF,CAAC,CACH;AACF"}
1
+ {"version":3,"file":"store.mjs","names":[],"sources":["../src/store.ts"],"sourcesContent":["import { Chunk, Effect, Predicate } from 'effect'\nimport * as Schema from 'effect/Schema'\nimport { ProviderSubscriptionUsageSnapshot } from '@yolk-sdk/agent/providers/subscription-usage'\nimport {\n PoolAccountId,\n PoolAuthCooldown,\n PoolPin,\n PoolQuotaCooldown,\n type PoolCooldown,\n type PoolStoreError\n} from './model.ts'\n\nexport { PoolStoreError } from './model.ts'\n\n/** The latest usage snapshot of one account. */\nexport class PoolStoredReading extends Schema.Class<PoolStoredReading>('PoolStoredReading')({\n accountId: PoolAccountId,\n snapshot: ProviderSubscriptionUsageSnapshot\n}) {}\n\n/** What one pick reads: the route's pin plus the cooldowns and readings of the pool's accounts. */\nexport type PoolStoreSnapshot = {\n readonly pin: PoolPin | undefined\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly readings: ReadonlyArray<PoolStoredReading>\n}\n\n/** Everything a store holds for one pool, for host UIs. */\nexport type PoolStoreState = {\n readonly pins: ReadonlyArray<PoolPin>\n readonly cooldowns: ReadonlyArray<PoolCooldown>\n readonly readings: ReadonlyArray<PoolStoredReading>\n}\n\n/**\n * Pool state shared by every instance of a host (ADR 0011). Hosts implement it on their database;\n * `makeMemoryPoolStore` serves tests and single-process hosts, and `poolStoreContract` pins the\n * semantics every implementation must meet.\n *\n * - Pins are keyed by `(poolId, route)`; cooldowns and readings by account id, so a cooldown or\n * reading applies to every pool the account is in.\n * - Operations must be atomic per key: concurrent `movePin` calls with the same `expected` let\n * exactly one through.\n */\nexport type PoolStoreApi = {\n /** The route's pin and the requested accounts' cooldowns (expired ones included) and readings. */\n readonly load: (input: {\n readonly poolId: string\n readonly route: string\n readonly accountIds: ReadonlyArray<string>\n }) => Effect.Effect<PoolStoreSnapshot, PoolStoreError>\n /**\n * Compare-and-set: writes `pin` when the route's current account is `expected` (`undefined`:\n * the route has no pin yet). Returns whether it wrote.\n */\n readonly movePin: (input: {\n readonly expected: string | undefined\n readonly pin: PoolPin\n }) => Effect.Effect<boolean, PoolStoreError>\n /**\n * Records a cooldown; when the account already has one, the one that ends later stays, and of two\n * that end together the one that started later (a newer failure must not be cleared by a reading\n * taken before it).\n */\n readonly putCooldown: (cooldown: PoolCooldown) => Effect.Effect<void, PoolStoreError>\n /**\n * Removes the account's cooldown only when it is the one observed (same `startedAtMs`). Returns\n * whether it removed one.\n */\n readonly clearCooldown: (input: {\n readonly accountId: string\n readonly startedAtMs: number\n }) => Effect.Effect<boolean, PoolStoreError>\n /** Records a reading unless the stored one was fetched later. */\n readonly putReading: (reading: PoolStoredReading) => Effect.Effect<void, PoolStoreError>\n /** The pool's pins and the requested accounts' cooldowns and readings. */\n readonly state: (input: {\n readonly poolId: string\n readonly accountIds: ReadonlyArray<string>\n }) => Effect.Effect<PoolStoreState, PoolStoreError>\n}\n\nconst pinKey = (poolId: string, route: string) => JSON.stringify([poolId, route])\n\nconst fetchedAtMs = (reading: PoolStoredReading) => Date.parse(reading.snapshot.fetchedAt)\n\n/**\n * An in-memory `PoolStoreApi`: state lives in this process only, so it suits tests and hosts that\n * run one long-lived process. Serverless hosts implement the port on their database.\n */\nexport const makeMemoryPoolStore = (): PoolStoreApi => {\n const pins = new Map<string, PoolPin>()\n const cooldowns = new Map<string, PoolCooldown>()\n const readings = new Map<string, PoolStoredReading>()\n\n const pick = <A>(source: Map<string, A>, accountIds: ReadonlyArray<string>) =>\n accountIds.flatMap(accountId => {\n const value = source.get(accountId)\n\n return value === undefined ? [] : [value]\n })\n\n return {\n load: input =>\n Effect.sync(() => ({\n pin: pins.get(pinKey(input.poolId, input.route)),\n cooldowns: pick(cooldowns, input.accountIds),\n readings: pick(readings, input.accountIds)\n })),\n movePin: input =>\n Effect.sync(() => {\n const key = pinKey(input.pin.poolId, input.pin.route)\n\n if (pins.get(key)?.accountId !== input.expected) {\n return false\n }\n\n pins.set(key, input.pin)\n\n return true\n }),\n putCooldown: cooldown =>\n Effect.sync(() => {\n const current = cooldowns.get(cooldown.accountId)\n\n if (\n current === undefined ||\n cooldown.untilMs > current.untilMs ||\n (cooldown.untilMs === current.untilMs && cooldown.startedAtMs > current.startedAtMs)\n ) {\n cooldowns.set(cooldown.accountId, cooldown)\n }\n }),\n clearCooldown: input =>\n Effect.sync(() => {\n const current = cooldowns.get(input.accountId)\n\n if (current === undefined || current.startedAtMs !== input.startedAtMs) {\n return false\n }\n\n cooldowns.delete(input.accountId)\n\n return true\n }),\n putReading: reading =>\n Effect.sync(() => {\n const current = readings.get(reading.accountId)\n\n if (current === undefined || fetchedAtMs(reading) >= fetchedAtMs(current)) {\n readings.set(reading.accountId, reading)\n }\n }),\n state: input =>\n Effect.sync(() => ({\n pins: Array.from(pins.values())\n .filter(pin => pin.poolId === input.poolId)\n .sort((a, b) => (a.route < b.route ? -1 : a.route > b.route ? 1 : 0)),\n cooldowns: pick(cooldowns, input.accountIds),\n readings: pick(readings, input.accountIds)\n }))\n }\n}\n\n/** A `PoolStoreApi` implementation broke the store contract. */\nexport class PoolStoreContractViolation extends Schema.TaggedError<PoolStoreContractViolation>()(\n 'PoolStoreContractViolation',\n {\n case: Schema.String,\n message: Schema.String\n }\n) {}\n\n/** One semantic every `PoolStoreApi` must meet. Run each case against a fresh, empty store. */\nexport type PoolStoreContractCase = {\n readonly name: string\n readonly run: (\n store: PoolStoreApi\n ) => Effect.Effect<void, PoolStoreContractViolation | PoolStoreError>\n}\n\nconst contractCase = (\n name: string,\n run: (\n store: PoolStoreApi,\n expect: (condition: boolean, message: string) => Effect.Effect<void, PoolStoreContractViolation>\n ) => Effect.Effect<void, PoolStoreContractViolation | PoolStoreError>\n): PoolStoreContractCase => ({\n name,\n run: store =>\n run(store, (condition, message) =>\n condition ? Effect.void : Effect.fail(new PoolStoreContractViolation({ case: name, message }))\n )\n})\n\nconst contractPin = (route: string, accountId: string, pinnedAtMs: number, poolId = 'pool') =>\n PoolPin.make({ poolId, route, accountId, pinnedAtMs })\n\nconst contractReading = (accountId: string, fetchedAt: string) =>\n PoolStoredReading.make({\n accountId,\n snapshot: ProviderSubscriptionUsageSnapshot.make({\n provider: 'contract',\n fetchedAt,\n windows: Chunk.empty()\n })\n })\n\n/** The `PoolStoreApi` contract. Hosts run every case against a fresh store of their implementation. */\nexport const poolStoreContract: ReadonlyArray<PoolStoreContractCase> = [\n contractCase('pins are created once and moved by compare-and-set', (store, expect) =>\n Effect.gen(function* () {\n const load = store.load({ poolId: 'pool', route: 'model', accountIds: [] })\n\n yield* expect((yield* load).pin === undefined, 'a new route has no pin')\n yield* expect(\n yield* store.movePin({ expected: undefined, pin: contractPin('model', 'a', 1) }),\n 'the first pin is written'\n )\n yield* expect(\n !(yield* store.movePin({ expected: undefined, pin: contractPin('model', 'b', 2) })),\n 'creating a pin that already exists is refused'\n )\n yield* expect(\n !(yield* store.movePin({ expected: 'b', pin: contractPin('model', 'c', 3) })),\n 'a move expecting another account is refused'\n )\n yield* expect(\n yield* store.movePin({ expected: 'a', pin: contractPin('model', 'b', 4) }),\n 'a move expecting the current account is written'\n )\n\n const pin = (yield* load).pin\n\n yield* expect(\n pin?.accountId === 'b' && pin.pinnedAtMs === 4,\n 'load returns the latest pin with its time'\n )\n })\n ),\n contractCase('pins are keyed by pool and route', (store, expect) =>\n Effect.gen(function* () {\n yield* store.movePin({ expected: undefined, pin: contractPin('one', 'a', 1) })\n yield* store.movePin({ expected: undefined, pin: contractPin('two', 'b', 1) })\n yield* store.movePin({ expected: undefined, pin: contractPin('one', 'c', 1, 'other') })\n\n const one = yield* store.load({ poolId: 'pool', route: 'one', accountIds: [] })\n const two = yield* store.load({ poolId: 'pool', route: 'two', accountIds: [] })\n const other = yield* store.load({ poolId: 'other', route: 'one', accountIds: [] })\n\n yield* expect(\n one.pin?.accountId === 'a' && two.pin?.accountId === 'b' && other.pin?.accountId === 'c',\n 'each (pool, route) keeps its own pin'\n )\n\n const state = yield* store.state({ poolId: 'pool', accountIds: [] })\n\n yield* expect(\n state.pins.map(pin => pin.route).join() === 'one,two',\n 'state lists the pool’s pins only, ordered by route'\n )\n })\n ),\n contractCase('concurrent moves from the same pin let exactly one through', (store, expect) =>\n Effect.gen(function* () {\n yield* store.movePin({ expected: undefined, pin: contractPin('model', 'a', 1) })\n\n const results = yield* Effect.all(\n ['b', 'c', 'd', 'e'].map(accountId =>\n store.movePin({ expected: 'a', pin: contractPin('model', accountId, 2) })\n ),\n { concurrency: 'unbounded' }\n )\n\n yield* expect(results.filter(Boolean).length === 1, 'exactly one move is written')\n })\n ),\n contractCase('cooldowns keep the later end and clear only when observed', (store, expect) =>\n Effect.gen(function* () {\n const load = store.load({ poolId: 'pool', route: 'model', accountIds: ['a', 'b'] })\n\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'a', startedAtMs: 1, untilMs: 100 })\n )\n yield* store.putCooldown(\n PoolAuthCooldown.make({ accountId: 'a', startedAtMs: 2, untilMs: 50 })\n )\n\n const kept = (yield* load).cooldowns\n\n yield* expect(\n kept.length === 1 &&\n Predicate.isTagged(kept[0], 'PoolQuotaCooldown') &&\n kept[0].untilMs === 100,\n 'an earlier-ending cooldown does not replace a later one'\n )\n\n yield* store.putCooldown(\n PoolAuthCooldown.make({ accountId: 'a', startedAtMs: 3, untilMs: 200 })\n )\n\n const replaced = (yield* load).cooldowns\n\n yield* expect(\n replaced.length === 1 &&\n Predicate.isTagged(replaced[0], 'PoolAuthCooldown') &&\n replaced[0].startedAtMs === 3,\n 'a later-ending cooldown replaces the current one'\n )\n\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'a', startedAtMs: 4, untilMs: 200 })\n )\n\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'a', startedAtMs: 2, untilMs: 200 })\n )\n\n const renewed = (yield* load).cooldowns\n\n yield* expect(\n renewed.length === 1 && renewed[0]?.startedAtMs === 4,\n 'of two cooldowns that end together, the later-started one stays, whatever the write order'\n )\n yield* expect(\n !(yield* store.clearCooldown({ accountId: 'a', startedAtMs: 3 })),\n 'clearing a cooldown that is no longer current is refused'\n )\n yield* expect(\n yield* store.clearCooldown({ accountId: 'a', startedAtMs: 4 }),\n 'clearing the observed cooldown succeeds'\n )\n yield* expect((yield* load).cooldowns.length === 0, 'a cleared cooldown is gone')\n yield* expect(\n !(yield* store.clearCooldown({ accountId: 'b', startedAtMs: 3 })),\n 'clearing an account without a cooldown is refused'\n )\n })\n ),\n contractCase(\n 'cooldowns and readings are returned for the requested accounts only',\n (store, expect) =>\n Effect.gen(function* () {\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'a', startedAtMs: 1, untilMs: 9 })\n )\n yield* store.putCooldown(\n PoolQuotaCooldown.make({ accountId: 'b', startedAtMs: 1, untilMs: 9 })\n )\n yield* store.putReading(contractReading('a', '2026-10-15T12:00:00.000Z'))\n yield* store.putReading(contractReading('b', '2026-10-15T12:00:00.000Z'))\n\n const snapshot = yield* store.load({ poolId: 'pool', route: 'model', accountIds: ['b'] })\n const state = yield* store.state({ poolId: 'pool', accountIds: ['a'] })\n\n yield* expect(\n snapshot.cooldowns.every(cooldown => cooldown.accountId === 'b') &&\n snapshot.cooldowns.length === 1 &&\n snapshot.readings.every(reading => reading.accountId === 'b') &&\n snapshot.readings.length === 1,\n 'load filters by account'\n )\n yield* expect(\n state.cooldowns.length === 1 &&\n state.cooldowns[0]?.accountId === 'a' &&\n state.readings.length === 1 &&\n state.readings[0]?.accountId === 'a',\n 'state filters by account'\n )\n })\n ),\n contractCase('readings keep the latest fetch', (store, expect) =>\n Effect.gen(function* () {\n const load = store.load({ poolId: 'pool', route: 'model', accountIds: ['a'] })\n\n yield* store.putReading(contractReading('a', '2026-10-15T12:00:00.000Z'))\n yield* store.putReading(contractReading('a', '2026-10-15T11:00:00.000Z'))\n\n yield* expect(\n (yield* load).readings[0]?.snapshot.fetchedAt === '2026-10-15T12:00:00.000Z',\n 'an older reading does not replace a newer one'\n )\n\n yield* store.putReading(contractReading('a', '2026-10-15T13:00:00.000Z'))\n\n yield* expect(\n (yield* load).readings[0]?.snapshot.fetchedAt === '2026-10-15T13:00:00.000Z',\n 'a newer reading replaces the stored one'\n )\n })\n )\n]\n"],"mappings":";;;;;;AAeA,IA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,CAAC;EAExE,MAAM,WAAW,OAAO,MAAM,KAAK;GAAE,QAAQ;GAAQ,OAAO;GAAS,YAAY,CAAC,GAAG;EAAE,CAAC;EACxF,MAAM,QAAQ,OAAO,MAAM,MAAM;GAAE,QAAQ;GAAQ,YAAY,CAAC,GAAG;EAAE,CAAC;EAEtE,OAAO,OACL,SAAS,UAAU,OAAM,aAAY,SAAS,cAAc,GAAG,KAC7D,SAAS,UAAU,WAAW,KAC9B,SAAS,SAAS,OAAM,YAAW,QAAQ,cAAc,GAAG,KAC5D,SAAS,SAAS,WAAW,GAC/B,yBACF;EACA,OAAO,OACL,MAAM,UAAU,WAAW,KACzB,MAAM,UAAU,EAAE,EAAE,cAAc,OAClC,MAAM,SAAS,WAAW,KAC1B,MAAM,SAAS,EAAE,EAAE,cAAc,KACnC,0BACF;CACF,CAAC,CACL;CACA,aAAa,mCAAmC,OAAO,WACrD,OAAO,IAAI,aAAa;EACtB,MAAM,OAAO,MAAM,KAAK;GAAE,QAAQ;GAAQ,OAAO;GAAS,YAAY,CAAC,GAAG;EAAE,CAAC;EAE7E,OAAO,MAAM,WAAW,gBAAgB,KAAK,0BAA0B,CAAC;EACxE,OAAO,MAAM,WAAW,gBAAgB,KAAK,0BAA0B,CAAC;EAExE,OAAO,QACJ,OAAO,KAAA,CAAM,SAAS,EAAE,EAAE,SAAS,cAAc,4BAClD,+CACF;EAEA,OAAO,MAAM,WAAW,gBAAgB,KAAK,0BAA0B,CAAC;EAExE,OAAO,QACJ,OAAO,KAAA,CAAM,SAAS,EAAE,EAAE,SAAS,cAAc,4BAClD,yCACF;CACF,CAAC,CACH;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yolk-sdk/pool",
3
- "version": "0.1.0-canary.123",
3
+ "version": "0.1.0-canary.124",
4
4
  "description": "Subscription account pools for Yolk agents: expiring-first routing and failover across several accounts of one provider behind one LLMProvider.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -50,12 +50,12 @@
50
50
  "provenance": true
51
51
  },
52
52
  "dependencies": {
53
- "effect": "4.0.5",
54
- "@yolk-sdk/agent": "^0.1.0-canary.123"
53
+ "effect": "4.1.2",
54
+ "@yolk-sdk/agent": "^0.1.0-canary.124"
55
55
  },
56
56
  "scripts": {
57
57
  "build": "tsdown",
58
- "check": "tsc -p tsconfig.json --noEmit",
58
+ "check": "effect-tsgo tsrs -p tsconfig.json --noEmit",
59
59
  "test": "vitest run --passWithNoTests",
60
60
  "test:run": "vitest run --passWithNoTests"
61
61
  }