@volter/twin-pinecone 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +57 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +26 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.js +50 -0
- package/dist/src/pinecone-budget.d.ts +85 -0
- package/dist/src/pinecone-budget.js +411 -0
- package/dist/src/pinecone-capabilities.d.ts +4 -0
- package/dist/src/pinecone-capabilities.js +430 -0
- package/dist/src/pinecone-conformance.d.ts +7 -0
- package/dist/src/pinecone-conformance.js +32 -0
- package/dist/src/pinecone-connector.d.ts +45 -0
- package/dist/src/pinecone-connector.js +96 -0
- package/dist/src/pinecone-filter.d.ts +3 -0
- package/dist/src/pinecone-filter.js +69 -0
- package/dist/src/pinecone-server.d.ts +14 -0
- package/dist/src/pinecone-server.js +67 -0
- package/dist/src/pinecone-similarity.d.ts +15 -0
- package/dist/src/pinecone-similarity.js +48 -0
- package/dist/src/pinecone-twin.d.ts +52 -0
- package/dist/src/pinecone-twin.js +491 -0
- package/package.json +51 -0
- package/src/cli.ts +25 -0
- package/src/index.ts +91 -0
- package/src/pinecone-budget.ts +457 -0
- package/src/pinecone-capabilities.ts +486 -0
- package/src/pinecone-conformance.ts +37 -0
- package/src/pinecone-connector.ts +130 -0
- package/src/pinecone-filter.ts +84 -0
- package/src/pinecone-server.ts +75 -0
- package/src/pinecone-similarity.ts +71 -0
- package/src/pinecone-twin.ts +513 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// @volter/twin-pinecone — the Pinecone (pinecone.io) vector-database API twin, built on the
|
|
2
|
+
// shared @volter/world-core kernel. Host-split REST transport: control plane (api.pinecone.io — index/
|
|
3
|
+
// collection management) + per-index data plane (a minted host — vector upsert/fetch/update/
|
|
4
|
+
// delete, REAL cosine/dotproduct/euclidean similarity query with a REAL Mongo-style metadata
|
|
5
|
+
// filter, namespaces, describe_index_stats). State lives entirely in the kernel action log (no
|
|
6
|
+
// side-store). API-first vendor: no mirror, no UI capabilities (ui-scope.json).
|
|
7
|
+
//
|
|
8
|
+
// INTEGRATED EMBEDDING IS A TODO: callers supply their own vectors today. The QUERY itself is a
|
|
9
|
+
// genuine strong done: exact brute-force top-K is a faithful, CORRECT superset of Pinecone's
|
|
10
|
+
// approximate-NN recall (see README ## Coverage). (Conformance/capability tooling lives in @volter/world-tooling, a dev dependency —
|
|
11
|
+
// NOT shipped.)
|
|
12
|
+
export {
|
|
13
|
+
handlePineconeTwinRequest,
|
|
14
|
+
routePineconeSurface,
|
|
15
|
+
mintHost,
|
|
16
|
+
recoverIndexFromHost,
|
|
17
|
+
pineconeTwinSnapshot,
|
|
18
|
+
PINECONE_RESOURCE_TYPES,
|
|
19
|
+
} from './pinecone-twin.ts';
|
|
20
|
+
export type {
|
|
21
|
+
PineconeRequest,
|
|
22
|
+
PineconeResponse,
|
|
23
|
+
PineconeSurface,
|
|
24
|
+
PineconeResourceType,
|
|
25
|
+
PineconeTwinSnapshot,
|
|
26
|
+
} from './pinecone-twin.ts';
|
|
27
|
+
export { createPineconeTwinFetch, createPineconeTwinServer, type PineconeTwinFetchOptions } from './pinecone-server.ts';
|
|
28
|
+
export {
|
|
29
|
+
mapIndex,
|
|
30
|
+
mapVector,
|
|
31
|
+
pullPineconeIndexes,
|
|
32
|
+
pullPineconeVectors,
|
|
33
|
+
syncPineconeFromReal,
|
|
34
|
+
} from './pinecone-connector.ts';
|
|
35
|
+
export type { PineconeLikeClient, PineconeRealIndex, PineconeRealVector, PineconeBudgetedOptions } from './pinecone-connector.ts';
|
|
36
|
+
// The client-side rate budget — the fail-closed backstop every live call goes through. The
|
|
37
|
+
// MECHANISM is the kernel's shared, vendor-agnostic `RateBudget`; what lives here is this vendor's
|
|
38
|
+
// DECLARATION (window/ceiling/per-method weights) plus `guardPineconeClient`, the choke point the
|
|
39
|
+
// connector entrypoints apply unconditionally. Exported so an operator can inspect spend
|
|
40
|
+
// (`snapshot`) and a caller can catch `PineconeBudgetError` by type; there is deliberately no export
|
|
41
|
+
// that disables the guard.
|
|
42
|
+
export {
|
|
43
|
+
PINECONE_BUDGETED_METHODS,
|
|
44
|
+
PINECONE_BUDGET_CEILING,
|
|
45
|
+
PINECONE_BUDGET_MAX_RETRY_AFTER_S,
|
|
46
|
+
PINECONE_BUDGET_WINDOW_MS,
|
|
47
|
+
PINECONE_CALL_WEIGHTS,
|
|
48
|
+
PINECONE_RATE_BUDGET,
|
|
49
|
+
PineconeBudget,
|
|
50
|
+
PineconeBudgetError,
|
|
51
|
+
pineconeBudgetPath,
|
|
52
|
+
pineconeCallWeight,
|
|
53
|
+
pineconeClientBudget,
|
|
54
|
+
guardPineconeClient,
|
|
55
|
+
} from './pinecone-budget.ts';
|
|
56
|
+
export type { PineconeBudgetErrorKind, PineconeBudgetOptions, PineconeBudgetReservation, PineconeBudgetSnapshot } from './pinecone-budget.ts';
|
|
57
|
+
export { matchesFilter } from './pinecone-filter.ts';
|
|
58
|
+
export type { PineconeFilter } from './pinecone-filter.ts';
|
|
59
|
+
export { cosineSimilarity, dotProduct, squaredEuclideanDistance, scoreVector, sortByScore } from './pinecone-similarity.ts';
|
|
60
|
+
export type { PineconeMetric } from './pinecone-similarity.ts';
|
|
61
|
+
|
|
62
|
+
// Registry descriptor: the pack self-describes so tooling can discover it.
|
|
63
|
+
import type { TwinPack } from '@volter/world-core';
|
|
64
|
+
import { PINECONE_RATE_BUDGET as RATE_BUDGET } from './pinecone-budget.ts';
|
|
65
|
+
export const pack: TwinPack = {
|
|
66
|
+
// The SAME object pinecone-budget.ts declares at module load — one source of truth, so registering
|
|
67
|
+
// the pack and importing the connector can never arm two different ceilings.
|
|
68
|
+
rateBudget: RATE_BUDGET,
|
|
69
|
+
vendor: 'pinecone',
|
|
70
|
+
transport: 'rest',
|
|
71
|
+
archetype: 'crud',
|
|
72
|
+
bin: 'world-pinecone',
|
|
73
|
+
resources: ['index', 'collection', 'vector'],
|
|
74
|
+
specSource: 'pinecone-conformance.ts (endpoint/resource inventory grounded against the installed @pinecone-database/pinecone SDK\'s own generated TypeScript-fetch client, fetched read-only via npm during the build — see spec-sources.json)',
|
|
75
|
+
description: 'Pinecone vector-database twin — index/collection management (control plane), vector upsert/fetch/update/delete, REAL brute-force cosine/dotproduct/euclidean similarity query with a REAL Mongo-style metadata filter evaluator, namespaces, describe_index_stats, host-split control/data-plane routing. Kernel-backed. No mirror (API-first vendor).',
|
|
76
|
+
browserRouting: { apiPathPrefix: '/', loaderHost: 'https://api.pinecone.io' },
|
|
77
|
+
// INTERCEPTION RULING — hostsNone, the pack's own home for it: control/data host split with
|
|
78
|
+
// per-index data-plane hostnames ({index}-{project}.svc.{env}.pinecone.io, see the pack
|
|
79
|
+
// README) — SDK is pointed at the twin by explicit host config.
|
|
80
|
+
hostsNone:
|
|
81
|
+
"control/data host split with per-index data-plane hostnames ({index}-{project}.svc.{env}.pinecone.io, see the pack README) — SDK is pointed at the twin by explicit host config",
|
|
82
|
+
// Adoption, moved off the central maps unchanged (descriptor-first back-migration, adding-a-twin.md §3,
|
|
83
|
+
// 2026-08-31).
|
|
84
|
+
adoption: {
|
|
85
|
+
// Pinecone's official Python SDK under its current name and its deprecated one - repos carry both.
|
|
86
|
+
pypi: ['pinecone', 'pinecone-client'],
|
|
87
|
+
sdks: ['@pinecone-database/pinecone'],
|
|
88
|
+
scopes: ['@pinecone-database/'],
|
|
89
|
+
envStems: ['PINECONE'],
|
|
90
|
+
},
|
|
91
|
+
};
|
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
// Pinecone's CLIENT-SIDE RATE BUDGET — the pack's DECLARATION (the numbers) plus `guardPineconeClient`,
|
|
2
|
+
// the choke point every live Pinecone call goes through. The MECHANISM — the durable token-keyed
|
|
3
|
+
// ledger, the rolling window, reserve-under-lock, the `Retry-After`/429 cooldown, fail-CLOSED on a
|
|
4
|
+
// corrupt ledger — lives ONCE in the vendor-agnostic kernel (`@volter/world-core` → `rateBudget.ts`).
|
|
5
|
+
// Read that module's header for the full rationale AND for the honest list of what the guard does
|
|
6
|
+
// NOT guarantee (an injected clock or ledger path still defeats it — it guards carelessness, not
|
|
7
|
+
// malice). This module is modeled on notion-budget.ts, the reference injected-client decorator.
|
|
8
|
+
//
|
|
9
|
+
// ── WHY PINECONE NEEDS ONE ──────────────────────────────────────────────────────────
|
|
10
|
+
// Pinecone publishes a detailed DATA-PLANE limits table (docs.pinecone.io/reference/api/
|
|
11
|
+
// database-limits): per namespace per second, Query/Upsert/Delete/Update 100 each, and update- or
|
|
12
|
+
// delete-by-metadata only 5/s; per index per second, List 200, Fetch 100, DescribeIndexStats 100;
|
|
13
|
+
// per project, Inference 100/s and 2000/min. Its 429 body names the namespace and the QPS limit.
|
|
14
|
+
// But the CONTROL PLANE — `listIndexes` / `describeIndex`, which is what this connector's index pull
|
|
15
|
+
// actually calls — has NO published row at all. And this connector's `listVectors` is not one vendor
|
|
16
|
+
// endpoint but an AGGREGATION: a real implementation loops `listIndexes()` then `listPaginated()` per
|
|
17
|
+
// index per namespace, so a single call fans out across the whole project.
|
|
18
|
+
//
|
|
19
|
+
// ── HOW THE CEILING WAS CHOSEN ──────────────────────────────────────────────────────────────
|
|
20
|
+
// The control-plane endpoint this connector calls has NO PUBLISHED LIMIT, so the ceiling is not
|
|
21
|
+
// derived from Pinecone's data-plane table (which would be far looser: List alone is 200/s per index).
|
|
22
|
+
// It is pinned to the kernel's conservative fallback instead (DEFAULT_RATE_BUDGET: 60 weighted units
|
|
23
|
+
// / 60s at weight 2 = 30 calls/minute) — no more permissive than an undeclared vendor already gets.
|
|
24
|
+
// Worth recording: the tightest documented Pinecone figure is 5/s for by-metadata mutations, 20x
|
|
25
|
+
// tighter than plain ops, so "Pinecone is generous" is only true endpoint by endpoint.
|
|
26
|
+
//
|
|
27
|
+
// ── WHAT THIS DOES NOT DO: PACE ─────────────────────────────────────────────────────────────
|
|
28
|
+
// It bounds the 60s AVERAGE; it does NOT bound the instantaneous rate. The window has no
|
|
29
|
+
// spacing, so a tight `await` loop can legitimately fire the whole allowance in a fraction of a
|
|
30
|
+
// second. In that shape the vendor's own 429 can arrive BEFORE this ceiling does, and the backstop
|
|
31
|
+
// is then the COOLDOWN: the guard reads the back-off off the thrown error (or the response's
|
|
32
|
+
// exhaustion headers) and refuses every later call without touching the vendor. So the honest claim
|
|
33
|
+
// is "bounds the 60s average, and converts the vendor's first 429 into a hard stop" — never
|
|
34
|
+
// "refuses before the vendor ever 429s". Pacing is the CALLER's job; this module REFUSES, it never
|
|
35
|
+
// sleeps (see the kernel header: it is deliberately not a scheduler). A tighter window would not
|
|
36
|
+
// close the gap, it would only turn every legitimate multi-page pull into a cascade of refusals.
|
|
37
|
+
//
|
|
38
|
+
// ── HOW THE WEIGHTS WERE CHOSEN (and what is a judgement call) ──────────────────────────────
|
|
39
|
+
// `listVectors` costs 4, twice `listIndexes`. A JUDGEMENT CALL about THIS CONNECTOR's shape rather
|
|
40
|
+
// than a published cost: `listVectors` is the pack's own aggregation shape (see
|
|
41
|
+
// pinecone-connector.ts's header), and behind it a real implementation walks every index and every
|
|
42
|
+
// namespace with a cursor, so one call is an unbounded number of requests. Doubling its price halves
|
|
43
|
+
// how far a runaway walk gets while barely touching a normal pull.
|
|
44
|
+
import {
|
|
45
|
+
declareRateBudget,
|
|
46
|
+
rateBudgetPath,
|
|
47
|
+
rateBudgetWeight,
|
|
48
|
+
RateBudget,
|
|
49
|
+
RateBudgetError,
|
|
50
|
+
type RateBudgetDeclaration,
|
|
51
|
+
type RateBudgetOptions,
|
|
52
|
+
type RateBudgetReservation,
|
|
53
|
+
type RateBudgetSnapshot,
|
|
54
|
+
} from '@volter/world-core';
|
|
55
|
+
import type { PineconeLikeClient } from './pinecone-connector.ts';
|
|
56
|
+
|
|
57
|
+
const VENDOR = 'pinecone';
|
|
58
|
+
|
|
59
|
+
/** Rolling window, in ms. Spend older than this is pruned. */
|
|
60
|
+
export const PINECONE_BUDGET_WINDOW_MS = 60_000;
|
|
61
|
+
|
|
62
|
+
/** Weighted units allowed inside one window. See the header for where this number comes from. */
|
|
63
|
+
export const PINECONE_BUDGET_CEILING = 60;
|
|
64
|
+
|
|
65
|
+
/** Seconds. A `Retry-After` above this means the credential is throttled hard — fail loudly. */
|
|
66
|
+
export const PINECONE_BUDGET_MAX_RETRY_AFTER_S = 300;
|
|
67
|
+
|
|
68
|
+
/** Per-call cost, keyed by the client method the guard is about to invoke. See the header. */
|
|
69
|
+
export const PINECONE_CALL_WEIGHTS = {
|
|
70
|
+
/** `listVectors` — the per-index, per-namespace paginated aggregation. */
|
|
71
|
+
fanout: 4,
|
|
72
|
+
/** Every other modeled call (`listIndexes`, and anything unclassified). */
|
|
73
|
+
other: 2,
|
|
74
|
+
} as const;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The client methods this pack PRICES BY NAME, as dotted paths into the injected client.
|
|
78
|
+
*
|
|
79
|
+
* Two kinds of entry: (a) every method this connector actually calls, and (b) an endpoint the
|
|
80
|
+
* VENDOR documents in a distinct tier which a consumer can reach through the guarded client even
|
|
81
|
+
* though this connector never calls it (see the weights section of the header). Used to build the
|
|
82
|
+
* guarded surface — and, in the pack's own suite, the counting fake.
|
|
83
|
+
*
|
|
84
|
+
* NOT a closed list, and not a claim about the injected client's shape. A path the real client does
|
|
85
|
+
* not have is SKIPPED (this connector's members are optional; inventing one would turn "observe
|
|
86
|
+
* nothing" into "call something that isn't there"), and a method absent from this list is still
|
|
87
|
+
* PRICED at `defaultWeight` when a caller reaches for it — an unmodeled endpoint must never be
|
|
88
|
+
* free, and dropping one would be worse than free because it would be invisible.
|
|
89
|
+
*/
|
|
90
|
+
export const PINECONE_BUDGETED_METHODS = [
|
|
91
|
+
'listIndexes',
|
|
92
|
+
'listVectors',
|
|
93
|
+
] as const;
|
|
94
|
+
|
|
95
|
+
/** THE PACK'S DECLARATION — pure data, the only Pinecone-specific thing in the whole budget. */
|
|
96
|
+
export const PINECONE_RATE_BUDGET: RateBudgetDeclaration = {
|
|
97
|
+
windowMs: PINECONE_BUDGET_WINDOW_MS,
|
|
98
|
+
ceiling: PINECONE_BUDGET_CEILING,
|
|
99
|
+
defaultWeight: PINECONE_CALL_WEIGHTS.other,
|
|
100
|
+
maxRetryAfterSeconds: PINECONE_BUDGET_MAX_RETRY_AFTER_S,
|
|
101
|
+
// Ordered: the kernel prices FIRST-MATCH-WINS, so the tightest tier is listed first.
|
|
102
|
+
rules: [
|
|
103
|
+
{ match: "(^|\\.)listVectors$", weight: PINECONE_CALL_WEIGHTS.fanout },
|
|
104
|
+
],
|
|
105
|
+
reason:
|
|
106
|
+
"Pinecone publishes a detailed DATA-plane limits table (per namespace/s: query/upsert/delete/update 100, " +
|
|
107
|
+
"by-metadata mutations only 5; per index/s: List 200, Fetch 100) but NO limit at all for the CONTROL-plane " +
|
|
108
|
+
"listIndexes/describeIndex this connector calls. With no documented figure for the endpoint actually used, " +
|
|
109
|
+
"this ceiling is pinned to the kernel's conservative fallback (60 units / 60s at weight 2 = 30 calls/min) " +
|
|
110
|
+
"rather than borrowed from the looser data-plane rows — no more permissive than an undeclared vendor " +
|
|
111
|
+
"already gets. listVectors costs 4 because it is this pack's own cross-index, per-namespace paginated " +
|
|
112
|
+
"aggregation, not a single vendor endpoint — a judgement call, not a published cost. Pinecone's limits page " +
|
|
113
|
+
"does not document Retry-After; the 429/exhaustion cooldown is the backstop.",
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// Declared at module load, so merely importing this module (which `pinecone-connector.ts` does) is
|
|
117
|
+
// enough to arm the real ceiling. A budget constructed BEFORE this runs falls back to the kernel's
|
|
118
|
+
// DEFAULT_RATE_BUDGET — which is tighter in call COUNT but prices every call at 2, so for a vendor
|
|
119
|
+
// with an expensive endpoint the fallback is CHEAPER there, not safer. `RateBudget` reads its policy
|
|
120
|
+
// LIVE precisely so this declaration takes effect the moment it lands, and constructing through the
|
|
121
|
+
// subclass below (whose module IS this one) makes the ordering a non-issue in practice.
|
|
122
|
+
declareRateBudget(VENDOR, PINECONE_RATE_BUDGET);
|
|
123
|
+
|
|
124
|
+
/** Price one Pinecone call by its client method key (a dotted path, e.g. `listIndexes`). */
|
|
125
|
+
export function pineconeCallWeight(method: string): number {
|
|
126
|
+
return rateBudgetWeight(VENDOR, method);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Where Pinecone's ledger lives. Token-keyed and cwd-independent by default (the vendor limits per
|
|
130
|
+
* credential, so a cwd-scoped ledger would hand the same credential a fresh allowance in every
|
|
131
|
+
* checkout, worktree and CI matrix leg); pass `root` to opt into world-scoped accounting. */
|
|
132
|
+
export function pineconeBudgetPath(opts: { root?: string; token?: string } | string = {}): string {
|
|
133
|
+
const o = typeof opts === 'string' ? { root: opts } : opts;
|
|
134
|
+
// VENDOR spread LAST: a loosely-typed `{ vendor: 'other', … }` slipping through (TypeScript's
|
|
135
|
+
// excess-property check only catches object literals) must not redirect this pack's ledger to
|
|
136
|
+
// another vendor's file.
|
|
137
|
+
return rateBudgetPath({ ...o, vendor: VENDOR });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Construction options for Pinecone's budget. The vendor is fixed; everything else may only TIGHTEN. */
|
|
141
|
+
export type PineconeBudgetOptions = Omit<RateBudgetOptions, 'vendor'>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Pinecone's budget — the shared kernel guard bound to this vendor's declaration. A real subclass,
|
|
145
|
+
* not an alias, so `budget instanceof PineconeBudget` means "a budget that accounts against this
|
|
146
|
+
* vendor's ledger under this vendor's ceiling": another vendor's `RateBudget` (with its own,
|
|
147
|
+
* possibly larger, ceiling) is NOT assignable where one of these is required.
|
|
148
|
+
*/
|
|
149
|
+
export class PineconeBudget extends RateBudget {
|
|
150
|
+
constructor(opts: PineconeBudgetOptions = {}) {
|
|
151
|
+
super({ ...opts, vendor: VENDOR });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export type { RateBudgetErrorKind as PineconeBudgetErrorKind } from '@volter/world-core';
|
|
156
|
+
export { RateBudgetError as PineconeBudgetError } from '@volter/world-core';
|
|
157
|
+
export type PineconeBudgetReservation = RateBudgetReservation;
|
|
158
|
+
export type PineconeBudgetSnapshot = RateBudgetSnapshot;
|
|
159
|
+
|
|
160
|
+
/** What every budgeted connector entrypoint accepts. There is deliberately no option that turns the
|
|
161
|
+
* guard OFF — only ones that say WHICH ledger and clock to account against. */
|
|
162
|
+
export type PineconeBudgetedOptions = {
|
|
163
|
+
/** An existing budget to share across calls. Omit and one is constructed. Cannot be null. */
|
|
164
|
+
budget?: PineconeBudget;
|
|
165
|
+
/** Construction options for the default budget (ledger path, clock). Cannot loosen it. */
|
|
166
|
+
budgetOptions?: PineconeBudgetOptions;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/** Pull the budget wiring out of a caller's opts bag, so an entrypoint can forward it verbatim. */
|
|
170
|
+
export function pineconeBudgetOf(opts: PineconeBudgetedOptions): PineconeBudgetedOptions {
|
|
171
|
+
return {
|
|
172
|
+
...(opts.budget !== undefined ? { budget: opts.budget } : {}),
|
|
173
|
+
...(opts.budgetOptions !== undefined ? { budgetOptions: opts.budgetOptions } : {}),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ── the choke point ─────────────────────────────────────────────────────────────────────────
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Marks a client this module has already wrapped, so guarding twice cannot charge twice.
|
|
181
|
+
*
|
|
182
|
+
* A MODULE-PRIVATE `Symbol()`, deliberately not `Symbol.for()`: a global-registry symbol is
|
|
183
|
+
* reachable BY NAME, so any caller could stamp `client[Symbol.for(…)] = anything` on a RAW client and
|
|
184
|
+
* the guard would hand it straight back UNGUARDED — a one-line bypass of the whole budget. With a
|
|
185
|
+
* private symbol the only way to be branded is to have been wrapped by this function. The cost is
|
|
186
|
+
* that two copies of this module in one dependency tree would each wrap (double-charging a call);
|
|
187
|
+
* that is the SAFE direction, and over-charging is the tradeoff this module takes everywhere else.
|
|
188
|
+
*/
|
|
189
|
+
const GUARDED: unique symbol = Symbol('@volter/twin-pinecone.budget.guarded');
|
|
190
|
+
|
|
191
|
+
type Guarded = { [GUARDED]?: unknown };
|
|
192
|
+
|
|
193
|
+
/** Is this client already behind a budget? Returns the budget it is behind, if so. */
|
|
194
|
+
export function pineconeClientBudget(client: unknown): RateBudget | undefined {
|
|
195
|
+
const mark = (client as Guarded | null)?.[GUARDED];
|
|
196
|
+
// Belt and braces: only a REAL budget counts as "already guarded". A non-RateBudget value here
|
|
197
|
+
// could only come from a forged brand, and the answer to a forgery is to wrap anyway.
|
|
198
|
+
return mark instanceof RateBudget ? mark : undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Charge a call keyed by `method`, invoke it, settle. Returns whatever the call returned. */
|
|
202
|
+
type Charge = (method: string, invoke: (...args: unknown[]) => unknown) => (...args: unknown[]) => unknown;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Wrap an INJECTED Pinecone client so EVERY call it makes is charged against the shared budget
|
|
206
|
+
* BEFORE the request goes out. This pack's connector never constructs the transport itself (the
|
|
207
|
+
* consumer injects a client that satisfies `PineconeLikeClient` structurally), so the guard is a
|
|
208
|
+
* DECORATOR rather than a factory — which is exactly why every connector entrypoint applies it
|
|
209
|
+
* UNCONDITIONALLY instead of trusting the caller to have done it.
|
|
210
|
+
*
|
|
211
|
+
* IDEMPOTENT: wrapping an already-guarded client returns it unchanged, so a caller who forgot is
|
|
212
|
+
* protected and a caller who wrapped deliberately is not double-charged.
|
|
213
|
+
*
|
|
214
|
+
* A method that THROWS is still inspected: an SDK typically RAISES on a 429 rather than returning
|
|
215
|
+
* it, and that error's back-off is exactly the signal that must become a persisted cooldown. Losing
|
|
216
|
+
* it would leave the ledger cheerfully spending into a throttled credential. The original error is
|
|
217
|
+
* always re-raised afterwards — the budget never swallows a vendor failure — EXCEPT when the
|
|
218
|
+
* back-off is beyond the cap, where the budget's own louder "stop calling" error takes precedence.
|
|
219
|
+
*/
|
|
220
|
+
export function guardPineconeClient(client: PineconeLikeClient, opts: PineconeBudgetedOptions = {}): PineconeLikeClient {
|
|
221
|
+
// There is no value a caller can pass to end up with an UNGUARDED client. `null`/`undefined` (or
|
|
222
|
+
// omitting it) build the default budget; anything that is not a REAL `PineconeBudget` is refused
|
|
223
|
+
// loudly rather than trusted — a duck-typed stand-in with a no-op `checkBudget` would otherwise be
|
|
224
|
+
// the one clean way around the guard. Validated BEFORE the already-guarded early return, so
|
|
225
|
+
// `guardPineconeClient(alreadyGuarded, { budget: impostor })` is refused too rather than silently
|
|
226
|
+
// ignoring the impostor.
|
|
227
|
+
if (opts.budget !== undefined && opts.budget !== null && !(opts.budget instanceof PineconeBudget)) {
|
|
228
|
+
throw new Error('guardPineconeClient: `budget` must be a PineconeBudget — refusing to guard a Pinecone client with an unverified rate guard');
|
|
229
|
+
}
|
|
230
|
+
if (pineconeClientBudget(client)) return client;
|
|
231
|
+
const budget = opts.budget instanceof PineconeBudget
|
|
232
|
+
? opts.budget
|
|
233
|
+
: new PineconeBudget({
|
|
234
|
+
// The default ledger is keyed by a hash of the credential — the vendor limits per credential,
|
|
235
|
+
// so a cwd-scoped ledger would hand it a fresh allowance per worktree/CI leg.
|
|
236
|
+
//
|
|
237
|
+
// HONESTLY: this pack does NOT hold the credential — the consumer's injected client does — so
|
|
238
|
+
// `PINECONE_API_KEY` is a BEST-EFFORT stand-in for it, not the real thing. If that env var names a
|
|
239
|
+
// different account than the injected client, spend is booked against the wrong ledger; if it
|
|
240
|
+
// is unset, every unattributed Pinecone credential on the machine shares one (over-tight,
|
|
241
|
+
// which is the safe direction). A caller who knows the credential should say so:
|
|
242
|
+
// `budgetOptions: { token }` overrides this, and does so deliberately last in the spread.
|
|
243
|
+
...(process.env.PINECONE_API_KEY !== undefined ? { token: process.env.PINECONE_API_KEY } : {}),
|
|
244
|
+
...(opts.budgetOptions ?? {}),
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
/** Settle a reservation from whatever the call produced. May THROW (a back-off past the cap). */
|
|
248
|
+
const settle = (weight: number, reservation: RateBudgetReservation, v: unknown): void => {
|
|
249
|
+
const status = responseStatus(v);
|
|
250
|
+
const headers = responseHeaders(v);
|
|
251
|
+
if (status !== undefined || headers !== undefined) budget.recordCall(weight, headers, { status, reservation });
|
|
252
|
+
else budget.recordCall(weight, undefined, { reservation });
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Settle once the call RESOLVED: the vendor has answered. recordCall arms any cooldown before
|
|
257
|
+
* it throws (a back-off beyond the cap), so that refusal is swallowed and the answer kept — a
|
|
258
|
+
* write the vendor accepted is never reported failed and performed again on retry. A resolved
|
|
259
|
+
* answer that carries a non-2xx status still lets the louder refusal win.
|
|
260
|
+
*/
|
|
261
|
+
const settleAnswered = (weight: number, reservation: RateBudgetReservation, v: unknown): void => {
|
|
262
|
+
try {
|
|
263
|
+
settle(weight, reservation, v);
|
|
264
|
+
} catch (e) {
|
|
265
|
+
const status = responseStatus(v);
|
|
266
|
+
if (!(e instanceof RateBudgetError) || (status !== undefined && (status < 200 || status >= 300))) throw e;
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Charge, call, settle — for a MODELED method, whose interface declares it `async`. Refusing
|
|
272
|
+
* REJECTS rather than throwing synchronously, so `client.x().catch(…)` behaves exactly as it does
|
|
273
|
+
* on an unguarded client. `checkBudget` RESERVES under lock, so nothing after its line runs when
|
|
274
|
+
* the budget refuses: the request is never made.
|
|
275
|
+
*/
|
|
276
|
+
const chargeAsync = (method: string, invoke: (...args: unknown[]) => unknown) =>
|
|
277
|
+
async (...args: unknown[]): Promise<unknown> => {
|
|
278
|
+
const weight = budget.weightFor(method);
|
|
279
|
+
const reservation = budget.checkBudget(weight);
|
|
280
|
+
try {
|
|
281
|
+
const res = await invoke(...args);
|
|
282
|
+
settleAnswered(weight, reservation, res);
|
|
283
|
+
return res;
|
|
284
|
+
} catch (e) {
|
|
285
|
+
settle(weight, reservation, e); // may throw its own louder refusal, which wins
|
|
286
|
+
throw e;
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Charge, call, settle — for an UNMODELED member reached through the passthrough Proxy, where the
|
|
292
|
+
* shape is unknown.
|
|
293
|
+
*
|
|
294
|
+
* Deliberately NOT `async`. Some vendor SDKs (twilio-shaped ones) build requests through
|
|
295
|
+
* SYNCHRONOUS chained accessors — `client.a.b('sid').c.create()` — where the intermediate calls
|
|
296
|
+
* return a resource context, not a promise. An `async` wrapper would turn every one of those into
|
|
297
|
+
* a `Promise` and `.c` would come back `undefined`: the guard would BREAK the client instead of
|
|
298
|
+
* guarding it. So a non-thenable return is treated as an accessor — still charged (we cannot know
|
|
299
|
+
* before calling, and over-charging is the safe direction) and wrapped, so the eventual async leaf
|
|
300
|
+
* is charged too rather than escaping the budget. The cost of the sync shape is that a REFUSAL on
|
|
301
|
+
* this path throws synchronously instead of rejecting; that is the honest signal for an accessor,
|
|
302
|
+
* and the modeled surface above (every method this connector actually calls) does not have it.
|
|
303
|
+
*/
|
|
304
|
+
const charge: Charge = (method, invoke) => (...args) => {
|
|
305
|
+
const weight = budget.weightFor(method);
|
|
306
|
+
const reservation = budget.checkBudget(weight);
|
|
307
|
+
let out: unknown;
|
|
308
|
+
try {
|
|
309
|
+
out = invoke(...args);
|
|
310
|
+
} catch (e) {
|
|
311
|
+
settle(weight, reservation, e); // may throw its own louder refusal, which wins
|
|
312
|
+
throw e;
|
|
313
|
+
}
|
|
314
|
+
if (!isThenable(out)) {
|
|
315
|
+
settle(weight, reservation, undefined);
|
|
316
|
+
return out !== null && (typeof out === 'object' || typeof out === 'function')
|
|
317
|
+
? proxyThrough({}, out as object, method, charge)
|
|
318
|
+
: out;
|
|
319
|
+
}
|
|
320
|
+
return out.then(
|
|
321
|
+
(res) => { settleAnswered(weight, reservation, res); return res; },
|
|
322
|
+
(e: unknown) => { settle(weight, reservation, e); throw e; },
|
|
323
|
+
);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// Build the charged surface from PINECONE_BUDGETED_METHODS (see its docstring for why an absent path is
|
|
327
|
+
// SKIPPED rather than stubbed).
|
|
328
|
+
const guarded: Record<string, unknown> & Guarded = {};
|
|
329
|
+
for (const path of PINECONE_BUDGETED_METHODS) {
|
|
330
|
+
const segs = path.split('.');
|
|
331
|
+
const leaf = segs[segs.length - 1]!;
|
|
332
|
+
let owner: Record<string, unknown> | undefined = client as unknown as Record<string, unknown>;
|
|
333
|
+
for (const seg of segs.slice(0, -1)) owner = owner?.[seg] as Record<string, unknown> | undefined;
|
|
334
|
+
if (typeof owner?.[leaf] !== 'function') continue;
|
|
335
|
+
const realOwner = owner;
|
|
336
|
+
let node: Record<string, unknown> = guarded;
|
|
337
|
+
for (const seg of segs.slice(0, -1)) node = (node[seg] ??= {}) as Record<string, unknown>;
|
|
338
|
+
// The method is resolved at CALL time, not here, so a client whose method is swapped later is
|
|
339
|
+
// still charged for whatever it actually runs.
|
|
340
|
+
node[leaf] = chargeAsync(path, (...args) => (realOwner[leaf] as (...a: unknown[]) => unknown).apply(realOwner, args));
|
|
341
|
+
}
|
|
342
|
+
Object.defineProperty(guarded, GUARDED, { value: budget, enumerable: false });
|
|
343
|
+
|
|
344
|
+
// The surface above is what this connector calls. A real client has MORE — and a consumer who
|
|
345
|
+
// needs any of it must not be forced to keep the RAW client alongside, because every call through
|
|
346
|
+
// that would be unbudgeted. So the guarded object is a Proxy: known members come from the map
|
|
347
|
+
// above, anything else is taken from the real client and PRICED at `defaultWeight`.
|
|
348
|
+
return proxyThrough(guarded, client as object, '', charge) as PineconeLikeClient;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Members JavaScript itself asks for. Wrapping any of these turns the object into something that
|
|
353
|
+
* looks thenable / mis-reports its own identity, which breaks `await`, `instanceof` and logging —
|
|
354
|
+
* so they always come from the target untouched, never priced.
|
|
355
|
+
*/
|
|
356
|
+
const NEVER_WRAP = new Set(['then', 'catch', 'finally', 'constructor', 'prototype', 'toJSON', 'toString', 'valueOf', 'inspect']);
|
|
357
|
+
|
|
358
|
+
/** Does this look like a promise? (Only a thenable gets the settle-on-resolution treatment.) */
|
|
359
|
+
function isThenable(v: unknown): v is Promise<unknown> {
|
|
360
|
+
return v !== null && (typeof v === 'object' || typeof v === 'function') && typeof (v as { then?: unknown }).then === 'function';
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Symbols that name an ITERATION PROTOCOL. Reaching for one of these on a vendor object is how a
|
|
365
|
+
* paginator is driven (`for await (const page of client.things.list())`), i.e. it is the doorway to
|
|
366
|
+
* an unbounded sequence of REQUESTS — exactly what this budget exists to bound — so they are charged
|
|
367
|
+
* and their result is kept behind the proxy. Every other symbol (`Symbol.toStringTag`,
|
|
368
|
+
* `nodejs.util.inspect.custom`, …) is metadata rather than a request and passes through untouched.
|
|
369
|
+
*/
|
|
370
|
+
const ITERATOR_SYMBOLS = new Set<symbol>([Symbol.asyncIterator, Symbol.iterator]);
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Serve `known` where it has the member; otherwise price a passthrough to `real`. Applied
|
|
374
|
+
* recursively, so a namespace member this pack never modeled is charged at `defaultWeight` rather
|
|
375
|
+
* than coming back `undefined`.
|
|
376
|
+
*
|
|
377
|
+
* CALLABLE NAMESPACES (§9 finding): a member can be BOTH a function and a namespace — twilio's
|
|
378
|
+
* `client.messages` is called as `client.messages(sid)` to get one message's context AND read as
|
|
379
|
+
* `client.messages.list()`. An earlier version returned the modeled node as a plain object whenever
|
|
380
|
+
* this pack modeled any child of it, which silently DROPPED the call signature and every unmodeled
|
|
381
|
+
* sibling — the module's own docstring calls a dropped member "worse than free, because it is
|
|
382
|
+
* invisible", and that is what it was doing. So when `real` is callable the proxy target is a
|
|
383
|
+
* function and an `apply` trap charges the call, while `known` stays the source of modeled members.
|
|
384
|
+
*/
|
|
385
|
+
function proxyThrough(known: object, real: object, prefix: string, charge: Charge, owner?: object): object {
|
|
386
|
+
const callable = typeof real === 'function';
|
|
387
|
+
// The target must itself be callable for the `apply` trap to exist at all. `known` stays the
|
|
388
|
+
// source of modeled members, read explicitly below rather than through the target.
|
|
389
|
+
const target: object = callable ? function proxied() { /* every call goes through the trap */ } : known;
|
|
390
|
+
return new Proxy(target, {
|
|
391
|
+
apply(_t, _thisArg, args: unknown[]) {
|
|
392
|
+
// Calling the namespace is itself a request-builder hop: charge it, and keep the result
|
|
393
|
+
// behind the proxy (charge() re-proxies a non-thenable) so the eventual leaf is charged too.
|
|
394
|
+
// `owner` is the object the function was read from — dropping it would silently break every
|
|
395
|
+
// method that relies on `this`.
|
|
396
|
+
return charge(prefix || 'call', (...a) => (real as (...x: unknown[]) => unknown).apply(owner, a))(...args);
|
|
397
|
+
},
|
|
398
|
+
get(_t, prop, receiver) {
|
|
399
|
+
if (typeof prop === 'symbol') {
|
|
400
|
+
const ownSym = Reflect.get(known, prop, receiver);
|
|
401
|
+
if (ownSym !== undefined) return ownSym; // the GUARDED brand, and anything we model
|
|
402
|
+
const fromSym = (real as Record<symbol, unknown> | null)?.[prop];
|
|
403
|
+
if (ITERATOR_SYMBOLS.has(prop) && typeof fromSym === 'function') {
|
|
404
|
+
return charge(`${prefix}[${prop.description ?? 'iterator'}]`, (...args) => (fromSym as (...a: unknown[]) => unknown).apply(real, args));
|
|
405
|
+
}
|
|
406
|
+
return fromSym;
|
|
407
|
+
}
|
|
408
|
+
const name = String(prop);
|
|
409
|
+
if (NEVER_WRAP.has(name)) return Reflect.get(known, prop, receiver);
|
|
410
|
+
const key = prefix ? `${prefix}.${name}` : name;
|
|
411
|
+
const own = Reflect.get(known, prop, receiver);
|
|
412
|
+
const from = (real as Record<string, unknown> | null)?.[name];
|
|
413
|
+
// A member we model: the charged wrapper (a function) or a namespace we must keep descending
|
|
414
|
+
// into, so an unmodeled sibling is still priced rather than dropped.
|
|
415
|
+
if (typeof own === 'function') return own;
|
|
416
|
+
if (own && typeof own === 'object') {
|
|
417
|
+
// `from` may be an object OR a CALLABLE namespace — both keep descending, which is what
|
|
418
|
+
// preserves `client.messages(sid)` alongside the modeled `client.messages.list()`.
|
|
419
|
+
return from && (typeof from === 'object' || typeof from === 'function')
|
|
420
|
+
? proxyThrough(own, from, key, charge, real)
|
|
421
|
+
: own;
|
|
422
|
+
}
|
|
423
|
+
if (own !== undefined) return own;
|
|
424
|
+
// A member only the real client has. Functions go through proxyThrough too, so one that also
|
|
425
|
+
// carries members (a callable namespace) keeps both its call signature and its siblings.
|
|
426
|
+
if (typeof from === 'function') return proxyThrough({}, from, key, charge, real);
|
|
427
|
+
if (from && typeof from === 'object') return proxyThrough({}, from, key, charge, real);
|
|
428
|
+
return from;
|
|
429
|
+
},
|
|
430
|
+
has(_t, prop) {
|
|
431
|
+
return Reflect.has(known, prop) || (real !== null && Reflect.has(real, prop));
|
|
432
|
+
},
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** The HTTP status a value carries, if it looks like one (an SDK error, or a raw response). */
|
|
437
|
+
function responseStatus(v: unknown): number | undefined {
|
|
438
|
+
const s = (v as { status?: unknown } | null)?.status;
|
|
439
|
+
return typeof s === 'number' && Number.isFinite(s) ? s : undefined;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Response/error headers as a plain lower-cased record, or `undefined` when there are none. Accepts
|
|
444
|
+
* a `Headers` instance, a `Map`, or a plain object — an SDK's error type changes shape across
|
|
445
|
+
* versions and none of the three is worth depending on.
|
|
446
|
+
*/
|
|
447
|
+
function responseHeaders(v: unknown): Record<string, string> | undefined {
|
|
448
|
+
const h = (v as { headers?: unknown } | null)?.headers;
|
|
449
|
+
if (!h || typeof h !== 'object') return undefined;
|
|
450
|
+
const out: Record<string, string> = {};
|
|
451
|
+
if (typeof (h as Headers).forEach === 'function') {
|
|
452
|
+
(h as Headers).forEach((value: string, key: string) => { out[String(key).toLowerCase()] = String(value); });
|
|
453
|
+
} else {
|
|
454
|
+
for (const [k, value] of Object.entries(h as Record<string, unknown>)) out[k.toLowerCase()] = String(value);
|
|
455
|
+
}
|
|
456
|
+
return Object.keys(out).length > 0 ? out : undefined;
|
|
457
|
+
}
|