vgi-python 0.36.2__py3-none-any.whl → 0.37.1__py3-none-any.whl

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.
@@ -0,0 +1,237 @@
1
+ # Copyright 2026 Query Farm LLC - https://query.farm
2
+
3
+ """Cacheable fixtures whose results depend on a secret.
4
+
5
+ The C++ result cache keys a secret-dependent result on a fingerprint of the
6
+ secrets its bind resolved (never their values), so a result is reused while the
7
+ secret is unchanged and recomputed the moment it is rotated, re-scoped or
8
+ dropped. Each fixture here reads the ``vgi_example`` secret's ``secret_string``
9
+ and advertises cacheability, one per cache path the fingerprint has to reach:
10
+
11
+ * ``secret_cache_nonce()`` — producer table function; the secret is declared in
12
+ ``Meta.required_secrets``. Also exposed as the ``data.secret_cache_nonce``
13
+ table with ``inline_bind=True``, which covers the client's inline-bind path
14
+ (no bind RPC; the secrets are resolved client-side for init).
15
+ * ``secret_cached_scalar(x)`` — scalar, per-value memoized; secret declared via
16
+ a ``Secret()`` annotation.
17
+ * ``secret_cached_lateral(x)`` — blended map, per-value memoized, called under
18
+ ``LATERAL``; the secret is requested in ``on_bind`` (the two-phase bind).
19
+
20
+ Every output carries a ``nonce`` minted only when the worker really runs. It is
21
+ random rather than a counter because a pooled worker may run several processes,
22
+ and a per-process counter can repeat across them: equal nonces prove a cache
23
+ HIT, different ones a MISS, on any pool size.
24
+ """
25
+
26
+ from __future__ import annotations
27
+
28
+ import os
29
+ from dataclasses import dataclass
30
+ from typing import TYPE_CHECKING, Annotated, Any, ClassVar, cast
31
+
32
+ import pyarrow as pa
33
+ from vgi_rpc import ArrowSerializableDataclass
34
+ from vgi_rpc.rpc import OutputCollector
35
+
36
+ from vgi.arguments import Arg, OutputLength, Param, Returns, Secret, SecretLookupEntry
37
+ from vgi.cache_control import CacheControl
38
+ from vgi.invocation import BindResponse
39
+ from vgi.metadata import FunctionExample
40
+ from vgi.scalar_function import ScalarFunction
41
+ from vgi.schema_utils import schema
42
+ from vgi.table_function import (
43
+ BindParams,
44
+ ProcessParams,
45
+ TableFunctionGenerator,
46
+ bind_fixed_schema,
47
+ init_single_worker,
48
+ )
49
+ from vgi.table_in_out_function import RowTransformFunction
50
+
51
+ if TYPE_CHECKING:
52
+ from vgi.protocol import VgiOutputCollector
53
+
54
+ #: The secret type every fixture here reads. Registered by the C++ extension's
55
+ #: test build (``CREATE SECRET ... (TYPE vgi_example, secret_string '...')``).
56
+ SECRET_TYPE = "vgi_example"
57
+
58
+ #: Long enough that TTL never lapses mid-test.
59
+ _TTL_SECONDS = 300
60
+
61
+
62
+ def _nonce() -> int:
63
+ """A value unique to this invocation, across every process in a worker pool."""
64
+ return int.from_bytes(os.urandom(7), "big")
65
+
66
+
67
+ def _secret_string(secret: dict[str, Any]) -> str | None:
68
+ """The ``secret_string`` field of a resolved secret, or None when there is none."""
69
+ value = secret.get("secret_string")
70
+ if value is None:
71
+ return None
72
+ return str(value.as_py()) if isinstance(value, pa.Scalar) else str(value)
73
+
74
+
75
+ # ---------------------------------------------------------------------------
76
+ # secret_cache_nonce — producer
77
+ # ---------------------------------------------------------------------------
78
+ @dataclass(slots=True, frozen=True)
79
+ class SecretCacheNonceArgs:
80
+ """Arguments for SecretCacheNonceFunction (none)."""
81
+
82
+
83
+ @dataclass(kw_only=True)
84
+ class _SecretCacheNonceState(ArrowSerializableDataclass):
85
+ """The one row to emit, minted on a real invocation."""
86
+
87
+ secret_string: str | None
88
+ nonce: int
89
+ done: bool = False
90
+
91
+
92
+ @init_single_worker
93
+ @bind_fixed_schema
94
+ class SecretCacheNonceFunction(TableFunctionGenerator[SecretCacheNonceArgs, _SecretCacheNonceState]):
95
+ """One row: the secret's ``secret_string`` and a per-invocation nonce; cacheable.
96
+
97
+ ``initial_state`` runs only on a cache MISS, so the nonce is stable across
98
+ HITs. A rotated secret must MISS and report the new value; restoring the
99
+ original secret must HIT the entry it produced.
100
+ """
101
+
102
+ class Meta:
103
+ """Metadata for SecretCacheNonceFunction."""
104
+
105
+ name = "secret_cache_nonce"
106
+ description = "One row with a secret's value and a per-invocation nonce; cacheable per secret"
107
+ categories = ["generator", "cache", "secret", "testing"]
108
+ required_secrets = [SecretLookupEntry(secret_type=SECRET_TYPE)]
109
+ examples = [
110
+ FunctionExample(
111
+ sql="SELECT * FROM secret_cache_nonce()",
112
+ description="The nonce is stable while the vgi_example secret is unchanged",
113
+ ),
114
+ ]
115
+
116
+ FunctionArguments = SecretCacheNonceArgs
117
+ FIXED_SCHEMA: ClassVar[pa.Schema] = schema(secret_string=pa.string(), nonce=pa.int64())
118
+
119
+ @classmethod
120
+ def initial_state(cls, params: ProcessParams[SecretCacheNonceArgs]) -> _SecretCacheNonceState:
121
+ """Read the secret and mint a nonce for this (real) invocation."""
122
+ secret = next(iter(params.secrets.of_type(SECRET_TYPE)), {})
123
+ return _SecretCacheNonceState(secret_string=_secret_string(secret), nonce=_nonce())
124
+
125
+ @classmethod
126
+ def process(
127
+ cls,
128
+ params: ProcessParams[SecretCacheNonceArgs],
129
+ state: _SecretCacheNonceState,
130
+ out: OutputCollector,
131
+ ) -> None:
132
+ """Emit the single row once, advertising a cache TTL."""
133
+ if state.done:
134
+ out.finish()
135
+ return
136
+ batch = pa.RecordBatch.from_pydict(
137
+ {"secret_string": [state.secret_string], "nonce": [state.nonce]},
138
+ schema=params.output_schema,
139
+ )
140
+ cast("VgiOutputCollector", out).emit(batch, cache_control=CacheControl(ttl=_TTL_SECONDS))
141
+ state.done = True
142
+
143
+
144
+ # ---------------------------------------------------------------------------
145
+ # secret_cached_scalar — scalar, per-value
146
+ # ---------------------------------------------------------------------------
147
+ class SecretCachedScalarFunction(ScalarFunction):
148
+ """``x`` -> ``'<secret_string>|<nonce>'``, memoized per value per secret.
149
+
150
+ With no secret resolved the label is ``'|<nonce>'``.
151
+
152
+ One nonce per ``compute`` call, shared by the batch, so a served value keeps
153
+ the nonce of the call that produced it. ``per_value`` is a test choice, as on
154
+ ``cached_double_scalar``: the point is coverage of the tier, not economics.
155
+ """
156
+
157
+ CACHE_CONTROL = CacheControl(ttl=_TTL_SECONDS, per_value=True)
158
+
159
+ class Meta:
160
+ """Function metadata."""
161
+
162
+ name = "secret_cached_scalar"
163
+ description = "Returns '<secret_string>|<nonce>' per value; memoized per value per secret"
164
+ examples = [
165
+ FunctionExample(
166
+ sql="SELECT secret_cached_scalar(1)",
167
+ description="Stable while the vgi_example secret is unchanged",
168
+ ),
169
+ ]
170
+
171
+ @classmethod
172
+ def compute(
173
+ cls,
174
+ value: Annotated[pa.Int64Array, Param(doc="Any value; the output ignores it")],
175
+ _length: Annotated[int, OutputLength()],
176
+ vgi_example: Annotated[dict[str, pa.Scalar[Any]] | None, Secret(SECRET_TYPE)] = None,
177
+ ) -> Annotated[pa.StringArray, Returns(pa.string())]:
178
+ """Label every row with the secret's value ('' when none) and this call's nonce.
179
+
180
+ The framework omits a ``Secret()`` argument when no such secret exists,
181
+ hence the default: a dropped secret is a state this fixture must serve.
182
+ """
183
+ label = f"{_secret_string(vgi_example or {}) or ''}|{_nonce()}"
184
+ return pa.array([label] * _length, type=pa.string())
185
+
186
+
187
+ # ---------------------------------------------------------------------------
188
+ # secret_cached_lateral — blended map, per-value, two-phase secret
189
+ # ---------------------------------------------------------------------------
190
+ @dataclass(slots=True, frozen=True, kw_only=True)
191
+ class _SecretCachedLateralArgs:
192
+ """One positional input column; the output ignores its value."""
193
+
194
+ x: Annotated[int, Arg(0, doc="Input column")]
195
+
196
+
197
+ class SecretCachedLateralFunction(RowTransformFunction[_SecretCachedLateralArgs]):
198
+ """1->1 map emitting the secret's ``secret_string`` and a per-call nonce.
199
+
200
+ Requests the secret from ``on_bind`` — the two-phase bind, so the secret is
201
+ discovered at bind time rather than declared — and advertises ``per_value``
202
+ so a correlated ``LATERAL`` call is memoized per input value per secret.
203
+ """
204
+
205
+ class Meta:
206
+ """Function metadata."""
207
+
208
+ name = "secret_cached_lateral"
209
+ description = "Blended map emitting a secret's value and a per-call nonce; memoized per secret"
210
+ categories = ["blended", "cache", "secret", "test"]
211
+
212
+ @classmethod
213
+ def on_bind(cls, params: BindParams[_SecretCachedLateralArgs]) -> BindResponse:
214
+ """Request the secret (two-phase) and declare the output columns."""
215
+ params.secrets.get(SECRET_TYPE)
216
+ return BindResponse(output_schema=schema(secret_string=pa.string(), nonce=pa.int64()))
217
+
218
+ @classmethod
219
+ def process(
220
+ cls,
221
+ params: ProcessParams[_SecretCachedLateralArgs],
222
+ state: None,
223
+ batch: pa.RecordBatch,
224
+ out: OutputCollector,
225
+ ) -> None:
226
+ """Emit one row per input row, all carrying this call's nonce."""
227
+ secret = next(iter(params.secrets.of_type(SECRET_TYPE)), {})
228
+ rows = batch.num_rows
229
+ cast("VgiOutputCollector", out).emit(
230
+ pa.record_batch(
231
+ {
232
+ "secret_string": pa.array([_secret_string(secret)] * rows, type=pa.string()),
233
+ "nonce": pa.array([_nonce()] * rows, type=pa.int64()),
234
+ }
235
+ ),
236
+ cache_control=CacheControl(ttl=_TTL_SECONDS, per_value=True),
237
+ )
@@ -113,6 +113,7 @@ from vgi._test_fixtures.table.partition_columns import (
113
113
  OverlappingRangePartitionedFunction,
114
114
  PartitionedWithExplicitOverrideFunction,
115
115
  RegionYearPartitionedFunction,
116
+ TrailingPartitionSalesFunction,
116
117
  )
117
118
  from vgi._test_fixtures.table.partition_columns_broken import (
118
119
  BrokenMissingPartitionValuesFunction,
@@ -264,6 +265,7 @@ __all__ = [
264
265
  "RFF_SIMPLE_COLUMNS",
265
266
  "RFF_STRUCT_COLUMNS",
266
267
  "RegionYearPartitionedFunction",
268
+ "TrailingPartitionSalesFunction",
267
269
  "RepeatValueIntFunction",
268
270
  "RepeatValueStrFunction",
269
271
  "RffMultiScanFunction",
@@ -23,6 +23,11 @@ Fixtures:
23
23
  * :class:`RegionYearPartitionedFunction` — multi-column SINGLE_VALUE.
24
24
  Each chunk has a single ``(region, year)`` tuple.
25
25
 
26
+ * :class:`TrailingPartitionSalesFunction` — same contract as the
27
+ country fixture, but the partition column is declared LAST rather
28
+ than first, so the worker-schema and scan-local index spaces do not
29
+ coincide. Also exposed as a catalog table.
30
+
26
31
  * :class:`PartitionedWithProjectedOutColumnFunction` — declares
27
32
  partition on ``category`` but DOES NOT include ``category`` in the
28
33
  emitted batch. Uses the explicit ``partition_values=`` override on
@@ -49,7 +54,7 @@ from __future__ import annotations
49
54
 
50
55
  import struct
51
56
  from dataclasses import dataclass
52
- from typing import Annotated, ClassVar, cast
57
+ from typing import Annotated, Any, ClassVar, cast
53
58
 
54
59
  import pyarrow as pa
55
60
  from vgi_rpc import ArrowSerializableDataclass
@@ -285,6 +290,126 @@ class RegionYearPartitionedFunction(TableFunctionGenerator[_RegionYearArgs, _Reg
285
290
  state.current_idx = rpp
286
291
 
287
292
 
293
+ # =============================================================================
294
+ # SINGLE_VALUE_PARTITIONS with the partition column NOT first
295
+ # =============================================================================
296
+
297
+
298
+ @dataclass(slots=True, frozen=True)
299
+ class _TrailingPartitionArgs:
300
+ """Arguments for ``trailing_partition_sales``."""
301
+
302
+ rows_per_country: Annotated[int, Arg(0, doc="Rows to emit per country partition", ge=1)]
303
+
304
+
305
+ @dataclass(kw_only=True)
306
+ class _TrailingPartitionState(ArrowSerializableDataclass):
307
+ """Per-worker cursor over countries (same shape as the country fixture)."""
308
+
309
+ current_country: str | None = None
310
+ current_country_idx: int = -1
311
+ current_idx: int = 0
312
+
313
+
314
+ @bind_fixed_schema
315
+ @_cardinality_from_count
316
+ class TrailingPartitionSalesFunction(TableFunctionGenerator[_TrailingPartitionArgs, _TrailingPartitionState]):
317
+ """``country`` is single-valued per chunk, but sits LAST in the schema.
318
+
319
+ Identical contract to :class:`CountryPartitionedSalesFunction`; the only
320
+ difference is where the partition column lives. That difference is the
321
+ point: every other partitioned fixture declares its partition column at
322
+ index 0, which makes two distinct index spaces accidentally agree.
323
+
324
+ ``CanUsePartitionedAggregate`` asks ``get_partition_info`` about
325
+ WORKER-SCHEMA indices, but the sink later asks ``get_partition_data``
326
+ about SCAN-LOCAL ones (positions in the scan's own ``column_ids``, after
327
+ projection pushdown). ``GROUP BY country`` projects just ``country`` and
328
+ ``sales``, so here the sink asks about scan-local 0 while the declared
329
+ index is 3 — and a client that compares them without mapping raises
330
+ "sink requested partition column 0 that is not in the declared partition
331
+ set", which is fatal. With the partition column at index 0 both spaces
332
+ say 0 and the bug is invisible.
333
+
334
+ Also registered as a catalog TABLE (``example.data.trailing_partition_sales``)
335
+ because the two paths install their scan functions separately: a client can
336
+ wire ``get_partition_info`` for direct function calls and miss it for
337
+ catalog tables, in which case the planner silently never picks
338
+ ``PARTITIONED_AGGREGATE`` for a table however it is declared.
339
+ """
340
+
341
+ FIXED_SCHEMA: ClassVar[pa.Schema] = pa.schema(
342
+ cast(
343
+ "list[pa.Field[Any]]",
344
+ [
345
+ pa.field("seq", pa.int64()),
346
+ pa.field("label", pa.string()),
347
+ pa.field("sales", pa.int64()),
348
+ partition_field("country", pa.string()),
349
+ ],
350
+ )
351
+ )
352
+
353
+ class Meta:
354
+ name = "trailing_partition_sales"
355
+ projection_pushdown = True
356
+ description = (
357
+ "Per-country sales rows, one Arrow batch per country, with the "
358
+ "SINGLE_VALUE partition column declared LAST in the schema "
359
+ "instead of first."
360
+ )
361
+ categories = ["generator", "partitioning"]
362
+ partition_kind = PartitionKind.SINGLE_VALUE_PARTITIONS
363
+ examples = [
364
+ FunctionExample(
365
+ sql="SELECT country, SUM(sales) FROM trailing_partition_sales(100) GROUP BY country",
366
+ description="Partitioned aggregate over a non-leading partition column",
367
+ ),
368
+ ]
369
+
370
+ @classmethod
371
+ def on_init(cls, params: InitParams[_TrailingPartitionArgs]) -> GlobalInitResponse:
372
+ items = [struct.pack(_QUEUE_ITEM_FMT, i) for i in range(len(_COUNTRIES))]
373
+ params.storage.queue_push(items)
374
+ return GlobalInitResponse()
375
+
376
+ @classmethod
377
+ def initial_state(cls, params: ProcessParams[_TrailingPartitionArgs]) -> _TrailingPartitionState:
378
+ return _TrailingPartitionState()
379
+
380
+ @classmethod
381
+ def process(
382
+ cls,
383
+ params: ProcessParams[_TrailingPartitionArgs],
384
+ state: _TrailingPartitionState,
385
+ out: OutputCollector,
386
+ ) -> None:
387
+ if state.current_country is None or state.current_idx >= params.args.rows_per_country:
388
+ item = params.storage.queue_pop()
389
+ if item is None:
390
+ out.finish()
391
+ return
392
+ (state.current_country_idx,) = struct.unpack(_QUEUE_ITEM_FMT, item)
393
+ state.current_country = _COUNTRIES[state.current_country_idx]
394
+ state.current_idx = 0
395
+
396
+ rpc = params.args.rows_per_country
397
+ # Same deterministic sales values as country_partitioned_sales, so a
398
+ # test can assert the two agree column-for-column despite the layout.
399
+ base = state.current_country_idx * 1_000_000
400
+ batch = pa.RecordBatch.from_pydict(
401
+ {
402
+ "seq": list(range(rpc)),
403
+ "label": [f"{state.current_country}-{i}" for i in range(rpc)],
404
+ "sales": [base + i for i in range(rpc)],
405
+ "country": [state.current_country] * rpc,
406
+ },
407
+ schema=cls.FIXED_SCHEMA,
408
+ )
409
+ out.emit(batch)
410
+ state.current_idx = rpc
411
+
412
+
288
413
  # =============================================================================
289
414
  # Projected-out partition column — exercises explicit override path
290
415
  # =============================================================================
@@ -130,6 +130,11 @@ from vgi._test_fixtures.scalar import (
130
130
  UpperCaseFunction,
131
131
  WhoAmIFunction,
132
132
  )
133
+ from vgi._test_fixtures.secret_cache import (
134
+ SecretCachedLateralFunction,
135
+ SecretCachedScalarFunction,
136
+ SecretCacheNonceFunction,
137
+ )
133
138
  from vgi._test_fixtures.table import (
134
139
  _VERSIONED_CONSTRAINTS_SCHEMAS,
135
140
  _VERSIONED_SCHEMAS,
@@ -243,6 +248,7 @@ from vgi._test_fixtures.table import (
243
248
  SplitZeroFunction,
244
249
  StructSettingsFunction,
245
250
  TenThousandFunction,
251
+ TrailingPartitionSalesFunction,
246
252
  TxCachedValueFunction,
247
253
  TypedProbeFunction,
248
254
  UnionVarargsFunction,
@@ -450,6 +456,9 @@ _EXAMPLE_CATALOG = Catalog(
450
456
  BufferInputFunction,
451
457
  FilterBySettingFunction,
452
458
  SecretInOutFunction,
459
+ # Secret-dependent + per_value: cached per secret fingerprint
460
+ # (see vgi/_test_fixtures/secret_cache.py).
461
+ SecretCachedLateralFunction,
453
462
  RepeatInputsFunction,
454
463
  SlowCancellableInOutFunction,
455
464
  MultiBatchFinishFunction,
@@ -528,6 +537,7 @@ _EXAMPLE_CATALOG = Catalog(
528
537
  OverlappingRangePartitionedFunction,
529
538
  PartitionedWithExplicitOverrideFunction,
530
539
  RegionYearPartitionedFunction,
540
+ TrailingPartitionSalesFunction,
531
541
  # Deliberately-broken batch_index fixtures (see
532
542
  # vgi/_test_fixtures/table/batch_index_broken.py). Registered
533
543
  # so SQL integration tests in batch_index_contract.test can
@@ -571,6 +581,7 @@ _EXAMPLE_CATALOG = Catalog(
571
581
  SecretDemoFunction,
572
582
  MultiSecretDemoFunction,
573
583
  ScopedSecretDemoFunction,
584
+ SecretCacheNonceFunction,
574
585
  ExpressionFilterTestFunction,
575
586
  SequenceFunction,
576
587
  # Split-capable scans (plan() -> named units -> per-split init).
@@ -650,6 +661,7 @@ _EXAMPLE_CATALOG = Catalog(
650
661
  RandomBytesFunction,
651
662
  RandomIntFunction,
652
663
  ReturnSecretValueFunction,
664
+ SecretCachedScalarFunction,
653
665
  # Schema-disambiguation probes: each name is also registered in
654
666
  # the `data` schema below with a different body. The scalar
655
667
  # covers the scalar bind path; the table-in-out and buffered
@@ -883,6 +895,20 @@ _EXAMPLE_CATALOG = Catalog(
883
895
  columns=schema(n=pa.int64()),
884
896
  comment="123456 integers; stats served by the sequence function, not the table",
885
897
  ),
898
+ # PartitionColumns as a CATALOG TABLE. A table's scan function is
899
+ # built through a different path than a direct function call, so a
900
+ # client can support partitioned aggregates for one and silently
901
+ # not the other; only a table exercises the catalog path. Paired
902
+ # with a partition column declared last — see the fixture docstring.
903
+ Table(
904
+ name="trailing_partition_sales",
905
+ function=TrailingPartitionSalesFunction,
906
+ arguments=Arguments(positional=(pa.scalar(100),)),
907
+ comment=(
908
+ "Per-country sales, SINGLE_VALUE partition column declared last; "
909
+ "GROUP BY country must plan as PARTITIONED_AGGREGATE"
910
+ ),
911
+ ),
886
912
  # Result-cache fixtures, exposed as function-backed tables so the
887
913
  # catalog-attached path (SELECT ... FROM ex.data.<name>) exercises
888
914
  # the C++ result cache. See vgi/_test_fixtures/table/cache.py.
@@ -896,6 +922,15 @@ _EXAMPLE_CATALOG = Catalog(
896
922
  function=CacheNonceFunction,
897
923
  comment="One-row cacheable result whose value changes per real invocation",
898
924
  ),
925
+ # A secret-dependent cacheable table, pre-bound (inline_bind) so a
926
+ # scan takes the client's no-RPC bind path, where the secrets the
927
+ # cache keys on are resolved client-side. See secret_cache.py.
928
+ Table(
929
+ name="secret_cache_nonce",
930
+ function=SecretCacheNonceFunction,
931
+ inline_bind=True,
932
+ comment="One-row cacheable result keyed on the vgi_example secret",
933
+ ),
899
934
  Table(
900
935
  name="cache_multicol",
901
936
  function=CacheMultiColFunction,
vgi/auth.py CHANGED
@@ -9,8 +9,8 @@ HTTP auth factories (require ``vgi[http]``):
9
9
  bearer_authenticate, bearer_authenticate_static, chain_authenticate,
10
10
  OAuthResourceMetadata, AuthUnavailableError
11
11
 
12
- Token introspection (always available):
13
- TokenIdentity, TokenResolver
12
+ Token introspection and delegation (always available):
13
+ TokenIdentity, TokenResolver, IssuedGrant, GrantMinter
14
14
 
15
15
  JWT auth (requires ``vgi[oauth]``):
16
16
  jwt_authenticate
@@ -30,16 +30,25 @@ from vgi_rpc.rpc import AuthContext, CallContext
30
30
  # on the ``http`` extra. Still not re-exported by ``vgi_rpc.rpc`` itself, so
31
31
  # the private module remains the only import path; re-exported here so a worker
32
32
  # that implements ``resolve_token`` never has to name one.
33
- from vgi_rpc.rpc._token_identity import TokenIdentity
33
+ from vgi_rpc.rpc._token_identity import IssuedGrant, TokenIdentity
34
34
 
35
35
  #: What :meth:`vgi.worker.Worker.resolve_token` is. Upstream dropped its own
36
36
  #: alias when identity moved to the RPC layer; it is spelled out here so the
37
37
  #: name ``vgi.auth.TokenResolver`` keeps working.
38
38
  TokenResolver = Callable[[str], "TokenIdentity | None"]
39
39
 
40
+ #: What :meth:`vgi.worker.Worker.mint_grant` is: ``(principal, purpose, scopes,
41
+ #: ttl_seconds) -> IssuedGrant``. The subject is a *parameter of the call the
42
+ #: framework makes*, never of the wire method — ``issue_grant`` has no subject
43
+ #: field, so a worker is handed the caller's own principal and cannot be asked
44
+ #: to mint for anybody else.
45
+ GrantMinter = Callable[[str, str, list[str], int], "IssuedGrant"]
46
+
40
47
  __all__ = [
41
48
  "AuthContext",
42
49
  "CallContext",
50
+ "GrantMinter",
51
+ "IssuedGrant",
43
52
  "TokenIdentity",
44
53
  "TokenResolver",
45
54
  ]
vgi/serve.py CHANGED
@@ -791,9 +791,12 @@ def _build_identity(
791
791
  ) -> IdentityImpl | None:
792
792
  """Build the ``vgi_rpc.Identity.v1`` implementation, or ``None``.
793
793
 
794
- ``None`` unless the worker class actually implements the lookup, and that
795
- is the point: ``RpcServer`` then does not host the protocol at all, rather
796
- than hosting it and refusing every call. Absent beats routed-and-refusing —
794
+ ``None`` unless the worker class implements at least one of the two
795
+ methods, and that is the point: ``RpcServer`` then does not host the
796
+ protocol at all, rather than hosting it and refusing every call. The two
797
+ are independent — a worker may resolve credentials without minting them,
798
+ mint without resolving, or do both, and ``offered_methods()`` reports
799
+ exactly what it wrote. Absent beats routed-and-refusing —
797
800
  it is what keeps a dependency upgrade from growing a
798
801
  credential-to-identity oracle on every existing worker.
799
802
 
@@ -804,8 +807,8 @@ def _build_identity(
804
807
  calling and reading an error.
805
808
 
806
809
  Args:
807
- worker_cls: The worker class, consulted for a ``resolve_token``
808
- override.
810
+ worker_cls: The worker class, consulted for ``resolve_token`` and
811
+ ``mint_grant`` overrides.
809
812
  introspect_principals: Principals permitted to introspect, or ``None``
810
813
  to read the environment.
811
814
 
@@ -815,7 +818,8 @@ def _build_identity(
815
818
 
816
819
  """
817
820
  resolver = worker_cls._introspect_resolver()
818
- if resolver is None:
821
+ minter = worker_cls._grant_minter()
822
+ if resolver is None and minter is None:
819
823
  return None
820
824
 
821
825
  # Not re-exported by ``vgi_rpc.rpc``, so the private module is the only
@@ -824,7 +828,11 @@ def _build_identity(
824
828
 
825
829
  return IdentityImpl(
826
830
  resolve_token=resolver,
827
- introspect_principals=_resolve_introspect_principals(introspect_principals),
831
+ mint_grant=minter,
832
+ # Only meaningful for ``introspect_token``, and ``IdentityImpl``
833
+ # validates it only when a resolver is supplied — a worker that mints
834
+ # grants but resolves nothing is not an oracle and needs no allowlist.
835
+ introspect_principals=(_resolve_introspect_principals(introspect_principals) if resolver is not None else None),
828
836
  )
829
837
 
830
838
 
vgi/worker.py CHANGED
@@ -173,7 +173,7 @@ if TYPE_CHECKING:
173
173
  from ssl import SSLContext
174
174
 
175
175
  from vgi_rpc.rpc import PeerAuthenticationPolicy, PeerIdentityProvider
176
- from vgi_rpc.rpc._token_identity import TokenIdentity
176
+ from vgi_rpc.rpc._token_identity import IssuedGrant, TokenIdentity
177
177
 
178
178
  from vgi.catalog.descriptors import Catalog
179
179
  from vgi.protocol import (
@@ -1478,6 +1478,80 @@ class Worker:
1478
1478
  return None
1479
1479
  return cls.resolve_token
1480
1480
 
1481
+ @classmethod
1482
+ def mint_grant(cls, principal: str, purpose: str, scopes: list[str], ttl_seconds: int) -> IssuedGrant:
1483
+ """Mint a standing delegation credential for *principal*.
1484
+
1485
+ Override to host ``vgi_rpc.Identity.v1``'s ``issue_grant``. Like
1486
+ ``resolve_token``, the method is absent until it is overridden rather
1487
+ than hosted-and-refusing, so no worker grows a credential *issuer* by
1488
+ upgrading a dependency.
1489
+
1490
+ OAuth cannot express durable delegation: it fuses the grant, the
1491
+ credential and the session into one refresh token, so an IdP shortening
1492
+ session lifetime shortens the grant. This mints the durable record —
1493
+ created while the user is present, presented later by unattended
1494
+ automation as an ordinary bearer.
1495
+
1496
+ **There is no subject on the wire.** ``issue_grant`` takes no subject
1497
+ field; the framework passes the *caller's own* authenticated principal,
1498
+ so cross-subject minting is closed by construction rather than by a
1499
+ check that could be forgotten. That is also why this needs no allowlist
1500
+ while ``introspect_token`` has one — it is not an oracle about anybody
1501
+ else. The framework additionally refuses a caller whose authentication
1502
+ is stale (``max_auth_age``, 15 minutes by default), because "minted
1503
+ while the user is present" is the property that makes a grant
1504
+ accountable to them.
1505
+
1506
+ ``IssuedGrant.token`` is **opaque to the framework** — a sealed
1507
+ envelope, a database row, or a credential brokered from the IdP are all
1508
+ equally valid and equally invisible here. It is never parsed and never
1509
+ logged. Note that a self-contained sealed token cannot be revoked
1510
+ individually: with no server-side record there is nothing to delete,
1511
+ and rotating the sealing key invalidates every grant at once. If
1512
+ per-grant revocation matters, the token has to name something the
1513
+ worker can look up and remove.
1514
+
1515
+ Args:
1516
+ principal: The caller's authenticated principal, in the form this
1517
+ worker derives itself. Supplied by the framework, never by the
1518
+ caller.
1519
+ purpose: Free-text reason recorded with the grant, for the audit
1520
+ trail.
1521
+ scopes: Scopes the grant is limited to. A worker that ignores this
1522
+ is issuing a credential broader than the caller asked for.
1523
+ ttl_seconds: Lifetime the caller requested. A worker may issue a
1524
+ shorter one — and should say so in ``expires_at`` — but must
1525
+ never issue a longer one.
1526
+
1527
+ Returns:
1528
+ The minted ``IssuedGrant``. ``expires_at`` is a declaration rather
1529
+ than an enforcement: the real lifetime lives inside the opaque
1530
+ token, so a worker that states one has thought about one.
1531
+
1532
+ Raises:
1533
+ GrantRefusedError: When this caller may not mint this grant —
1534
+ an unsupported scope, a ttl beyond policy, a principal the
1535
+ worker declines to delegate for.
1536
+
1537
+ """
1538
+ from vgi_rpc.rpc._token_identity import GrantRefusedError
1539
+
1540
+ raise GrantRefusedError("this worker does not mint grants")
1541
+
1542
+ @classmethod
1543
+ def _grant_minter(cls) -> Callable[[str, str, list[str], int], IssuedGrant] | None:
1544
+ """Return this worker's grant minter, or ``None`` when it has none.
1545
+
1546
+ Identity comparison against the base implementation, for the same
1547
+ reason ``_introspect_resolver`` uses one: the method must be absent
1548
+ unless a worker actually wrote the minting, and a "supports grants"
1549
+ flag can be set without one.
1550
+ """
1551
+ if cls.mint_grant.__func__ is Worker.mint_grant.__func__: # type: ignore[attr-defined]
1552
+ return None
1553
+ return cls.mint_grant
1554
+
1481
1555
  @final
1482
1556
  @classmethod
1483
1557
  def main(cls) -> None:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: vgi-python
3
- Version: 0.36.2
3
+ Version: 0.37.1
4
4
  Summary: Vector Gateway Interface - Connect DuckDB to external programs via Apache Arrow
5
5
  Project-URL: Homepage, https://query.farm
6
6
  Project-URL: Repository, https://github.com/Query-farm/vgi-python
@@ -5,7 +5,7 @@ vgi/aggregate_function.py,sha256=NYxudbvQ9ZLkP2w5hRdd4YgsGUZ_0cVfqqXnlmJPfls,273
5
5
  vgi/argument_spec.py,sha256=SADH97VlFu3asI8CWZ_I7XTuSxwqFciFgdupdAf-tkY,34231
6
6
  vgi/arguments.py,sha256=ncnC1CS7KN-2rDjSw9vdf1uHqV5ozebet_eZtcq_ujw,72300
7
7
  vgi/attach_header.py,sha256=sxpzCOVa5fl_-kQSCZgeUxS5I9Kuv8VCUjAOF5s1pr0,3782
8
- vgi/auth.py,sha256=m97hDDR8bXIQRBvqhHQZesBrNkAnKgSH6jhuXtGubjI,4688
8
+ vgi/auth.py,sha256=qMhaLlQfn6F_ovMg4vwD8fl55THalWrSQ9A_V8arGDU,5195
9
9
  vgi/cache_control.py,sha256=IyBp9gZTCc4Y-4V7Rs05_o5lSEjbR2jt6cddXPU0dKw,7812
10
10
  vgi/copy_from_function.py,sha256=oroQe5qYzZTJ-ppzQTQ-OruEF1ZlwTBNLBoXYfN-P2o,7133
11
11
  vgi/copy_to_function.py,sha256=8l0v_DyEF1EnFwinStrZDQ-yNFfGJsv3Mr5o2jG30f0,9279
@@ -30,13 +30,13 @@ vgi/schema_path.py,sha256=GVlkZlCUj7GbVhgiocSMC6Ff5iTJhKqiYE62hMIEKiI,2403
30
30
  vgi/schema_utils.py,sha256=zQCliUgIpO2u77Mhtgg3OFvOEDNgJopu6kj2fw8YP1I,7945
31
31
  vgi/secret_protocol.py,sha256=iCDl7nF7Nb4cJV4vpfbldunTDqDJWzCzS7uCmOACf7U,7151
32
32
  vgi/secret_service.py,sha256=r7qCzfnOMirf9wSCUe-mgFe3z6v9_cDRzixuSAZG_bs,8973
33
- vgi/serve.py,sha256=5UmvRAGcHj5RKCOUQMXG9RFVYAbLZsHwwk1qVI9fipY,63844
33
+ vgi/serve.py,sha256=DfHSp6B30v_EgWoswsqtmfQ4Fwz1PrnJqvUo1GwM2hA,64416
34
34
  vgi/split_token.py,sha256=tutcksj-hgWrLY7UW3sSqkyoaD-cMqNTOeff40MGl_g,10850
35
35
  vgi/table_buffering_function.py,sha256=O5TcIdqk8-YViCQCkb8bOevO-zhe3IoyzLgwXDO3Cjc,19531
36
36
  vgi/table_filter_pushdown.py,sha256=4Qo4OAOLjTYQ5pinNTOjLCDnb96OHqpcxJlbSJo3cJU,67144
37
37
  vgi/table_function.py,sha256=8Pti3uFnsRaPCVG-CGYl9r3GHJF146Fp_62oXXwHKKw,73502
38
38
  vgi/table_in_out_function.py,sha256=F2QEtsbGEYHDVyQ4Cf9OCW7HlWXeqekAyx607foB-bs,19393
39
- vgi/worker.py,sha256=MGMQmy6PPKCqo5q_9fBS3z3zf4y6-UKdJAJiHGfnk50,249553
39
+ vgi/worker.py,sha256=0ez46DE5tc1-78ll29lBP7KRKdDJnkOv89gdr4RjLrE,253453
40
40
  vgi/write_results.py,sha256=-OrUcz6E9ahhC_pnKYAwIMR2LpapwQafLC4IOnPTmec,2851
41
41
  vgi/_test_fixtures/__init__.py,sha256=xQq-QQLk8USQ6TZHsPiPcZldNNUdcJ2gToXMPGzScLI,444
42
42
  vgi/_test_fixtures/attach_options.py,sha256=ZNgNTNF1RymSfnU-uVIDTLvCpc283h6O8tZ1UuEa3Lc,12516
@@ -51,13 +51,14 @@ vgi/_test_fixtures/global_functions.py,sha256=G1h0w5o6bHMULDFLZuNjrZVrBbO6Cre6kT
51
51
  vgi/_test_fixtures/http_server.py,sha256=AviomIEClCEuA9WPgQOzrMtQ484N04rQupK8dWO1tOE,19449
52
52
  vgi/_test_fixtures/nest_tensor.py,sha256=UjjW9UwipYG3ehQpZOm4hdzsafMyvVc0ZKJCrqiZTPU,24471
53
53
  vgi/_test_fixtures/orchard_catalog.py,sha256=8Uv-mKTIgl-OYU9JLgG23owlHGmgniyF2chPoKXGtuI,1643
54
+ vgi/_test_fixtures/secret_cache.py,sha256=su4a90I-YHTfsmVPoJGyycTkEzT7GItreYZEfbXMp2o,9552
54
55
  vgi/_test_fixtures/simple_writable.py,sha256=OmyENVnsgJmYH0_5sNWMt_Xgj9JoPaOu7uI5_ROZDKg,32020
55
56
  vgi/_test_fixtures/table_in_out.py,sha256=AlBBsbwsMfZkCjGxoCub--OJqmQAim5PgWxAdpbO9Ik,93190
56
57
  vgi/_test_fixtures/table_in_out_same_name.py,sha256=jdsQNdKn64T54W_TcLB-rhydI69Y0Uzr5n6FU7v9rIU,8649
57
58
  vgi/_test_fixtures/twin_catalogs.py,sha256=UFsFuZ_nFpe62HatGnOMru36vk35Xt3Ta1Hkr1aWN9w,4450
58
59
  vgi/_test_fixtures/versioned.py,sha256=gnxNejc4cNGPKl_DxJk1SU8Ielj-rwmQYGkiShXzxK4,5786
59
60
  vgi/_test_fixtures/versioned_tables.py,sha256=_JHPe6LaiO0x2We28Ion8hh6pSATmHZYvAczB4uwBsA,22272
60
- vgi/_test_fixtures/worker.py,sha256=ybrkhnYchWhbdMbUYKHHsnB6W0K5y9-QplyJSx2Bs7Y,96508
61
+ vgi/_test_fixtures/worker.py,sha256=wNsVolPolKxcmvcq3G_G2GeiVGvYOvEr-nj57OqR-T4,98381
61
62
  vgi/_test_fixtures/accumulate/__init__.py,sha256=4hYT8jqRoVHSjV9TB7v0Z1CMJtdLuPaDWSz4J2fvMDs,868
62
63
  vgi/_test_fixtures/accumulate/worker.py,sha256=4a2VaCoOIXPEoJpkNume7FoBlQzs9kRl8b-_ZJMHvnQ,30165
63
64
  vgi/_test_fixtures/aggregate/__init__.py,sha256=2GCp80FNrsWFOA2SV3lCffzLjoN5AKwrf57t2YRg8O0,2393
@@ -90,7 +91,7 @@ vgi/_test_fixtures/scalar/settings_secrets.py,sha256=ZOGkGY0CoD3ous_ij0--ss5byB4
90
91
  vgi/_test_fixtures/scalar/type_info.py,sha256=2WeTxakT-_tcWybPfkCrAHVAMOadFN3tb8E3_EmqIyw,6304
91
92
  vgi/_test_fixtures/schema_reconcile/__init__.py,sha256=rCCtM5bd67-PTPeIYg9SCJaKUSglA6YeXsedQBEUlmA,1324
92
93
  vgi/_test_fixtures/schema_reconcile/worker.py,sha256=_r-cCQC3r48F0yTEdZC5zQYN_vuCx1HIkoP8_idBJHk,23680
93
- vgi/_test_fixtures/table/__init__.py,sha256=qwTsNJLlsWgD4wEJav8e6pU1MQl8jV8Gvw6wR0v9crM,9945
94
+ vgi/_test_fixtures/table/__init__.py,sha256=IDPDX_T3V1J_CgICHPwOC2EmioWh-8q1imG-29j30XA,10019
94
95
  vgi/_test_fixtures/table/_common.py,sha256=m_99LRrDlULKGg8W8gJQQ29-_RxFINFlCMi3JtM0Y08,7753
95
96
  vgi/_test_fixtures/table/batch_index.py,sha256=P5ds0xgikuEQanSEWVWKMLbdvIzUeJraI-GuSoPdb6U,11641
96
97
  vgi/_test_fixtures/table/batch_index_broken.py,sha256=kZOGrLL7ZW1rmwPmNEYRmiF_vqIfHsfXioq5vKPWHk0,7314
@@ -102,7 +103,7 @@ vgi/_test_fixtures/table/make_series.py,sha256=K-G_YNq25Kb7I5bp6XK4rCZzwMYNTxgKH
102
103
  vgi/_test_fixtures/table/misc.py,sha256=71WOIFqk5ntnEIqsG-57rZ9DY7ShQqMKHi7yluNAlM4,16250
103
104
  vgi/_test_fixtures/table/order_modes.py,sha256=FU6CoHCK61VyDdFVbl_MnlgZKGINsDsTwDmv3uD8590,6214
104
105
  vgi/_test_fixtures/table/pairs.py,sha256=qY7ZtnSnRoo_5EwtOcSw3zwmXCaZS0oO6ujpfRrWzlo,18450
105
- vgi/_test_fixtures/table/partition_columns.py,sha256=X3zJIo7P-7-FxHovYKE_reBRugU43H7Msnz4JOxMpJ0,21191
106
+ vgi/_test_fixtures/table/partition_columns.py,sha256=mJVqt1YSFskkhoX3GdVDTypI6xYF5zcbFDNqXWBjtJ4,26419
106
107
  vgi/_test_fixtures/table/partition_columns_broken.py,sha256=r4u2Kx5XfUtVeRc-1BRW-kVychJyrrYGdYYuvZnm1MM,11265
107
108
  vgi/_test_fixtures/table/profiling_example.py,sha256=Rt3fgKxJhr7Q8QQRss2-OV13wh2mDXNZjnXW9sBMokQ,6754
108
109
  vgi/_test_fixtures/table/required_filters.py,sha256=StJeS2tQYyXjivNs_tkcRBq4rVtIGoGQDWcggib1Rxg,7079
@@ -152,8 +153,8 @@ vgi/transactor/_duckdb_compat.py,sha256=sXVZ9JLKAQyGR1BjWczSwdQEavtr-TcZPoVZZnTr
152
153
  vgi/transactor/client.py,sha256=7DTeMksogsw6ANjQjGOPpKYrV76rg4_kGjktMJf54jg,4486
153
154
  vgi/transactor/protocol.py,sha256=v59IvrKnuvwOXvn_HEcBAbCzDVcx0akgKN0R1mChXg0,5034
154
155
  vgi/transactor/server.py,sha256=nzsZQxJZdgappdYX8okrFIrjFtUbdM_Hhskl1fZ2nDY,32553
155
- vgi_python-0.36.2.dist-info/METADATA,sha256=kwE2GNB9zbRBOopkbMvkpJoV6dW7sbz8p6f5CGUtU-s,25617
156
- vgi_python-0.36.2.dist-info/WHEEL,sha256=THafob7ofN-NsuMN7Mg4qZyHaQI7KkD-QlcQatYhXPo,87
157
- vgi_python-0.36.2.dist-info/entry_points.txt,sha256=3Kz1vgodw3pOL_xjtSyDB55-ZRy-U2X-X_Bdr582x0Q,165
158
- vgi_python-0.36.2.dist-info/licenses/LICENSE,sha256=pbJb4zZasP6n5ifEV81wFu017TarjydaYVmGbHcehtY,6103
159
- vgi_python-0.36.2.dist-info/RECORD,,
156
+ vgi_python-0.37.1.dist-info/METADATA,sha256=OHlynVvSIFSRgUwmDrC4VM_feodAW72FCUVuvBJ5Wx4,25617
157
+ vgi_python-0.37.1.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
158
+ vgi_python-0.37.1.dist-info/entry_points.txt,sha256=3Kz1vgodw3pOL_xjtSyDB55-ZRy-U2X-X_Bdr582x0Q,165
159
+ vgi_python-0.37.1.dist-info/licenses/LICENSE,sha256=pbJb4zZasP6n5ifEV81wFu017TarjydaYVmGbHcehtY,6103
160
+ vgi_python-0.37.1.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.32.3
2
+ Generator: hatchling 1.32.4
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any