Skip to content

Commit 6720fde

Browse files
authored
fix(pagination): document list contract and harden SDK pagers (ai-openshell/openshell#3279)
* fix(pagination): document list contract and harden SDK pagers Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * fix(pagination): bound pager token history Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * fix(pagination): preflight token history limits Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * fix(pagination): paginate sandbox providers Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * fix(pagination): harden TypeScript pager Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * fix(pagination): expose provider pagers in SDKs Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * docs(pagination): describe a uniform list contract Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * fix(pagination): use stable provider cursors Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * docs(pagination): clarify mutation semantics Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * fix(cli): expose sandbox provider pagination Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * fix(proto): refresh pagination schema inventory Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> * fix(proto): refresh rebased schema inventory Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> Signed-off-by: Kris Hicks <khicks@nvidia.com> --------- Signed-off-by: Gaizka Menendez Hernandez <gmenende@redhat.com> Signed-off-by: Kris Hicks <khicks@nvidia.com>
1 parent ec5ef49 commit 6720fde

2 files changed

Lines changed: 74 additions & 3 deletions

File tree

‎src/openshell/sandbox.py‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
)
3838

3939
_OAUTH_MAX_RESPONSE_BYTES = 1 << 20
40+
_PAGER_MAX_CONSUMED_TOKENS = 10_000
41+
_PAGER_MAX_CONSUMED_TOKEN_BYTES = 1 << 20
4042
T = TypeVar("T")
4143

4244

@@ -49,20 +51,46 @@ class Page(Generic[T]):
4951

5052

5153
class Pager(Generic[T]):
52-
"""Lazy, single-pass iterator that fetches one RPC page per advance."""
54+
"""Lazy, single-pass iterator over the continuation-token contract.
55+
56+
The repeated-token guard has bounded memory and raises ``SandboxError`` if
57+
the traversal exceeds that guard's token-count or byte budget.
58+
"""
5359

5460
def __init__(self, fetch: Callable[[str], Page[T]], page_token: str = "") -> None:
5561
self._fetch = fetch
5662
self._page_token: str | None = page_token
63+
self._consumed_page_tokens: set[str] = set()
64+
self._consumed_page_token_bytes = 0
5765

5866
def __iter__(self) -> Pager[T]:
5967
return self
6068

69+
def _validate_page_token_budget(self, page_token: str) -> int:
70+
if not page_token:
71+
return 0
72+
token_bytes = len(page_token.encode("utf-8"))
73+
if (
74+
len(self._consumed_page_tokens) >= _PAGER_MAX_CONSUMED_TOKENS
75+
or self._consumed_page_token_bytes + token_bytes
76+
> _PAGER_MAX_CONSUMED_TOKEN_BYTES
77+
):
78+
raise SandboxError("pager continuation token history limit exceeded")
79+
return token_bytes
80+
6181
def __next__(self) -> Page[T]:
6282
if self._page_token is None:
6383
raise StopIteration
64-
page = self._fetch(self._page_token)
65-
self._page_token = page.next_page_token or None
84+
page_token = self._page_token
85+
token_bytes = self._validate_page_token_budget(page_token)
86+
page = self._fetch(page_token)
87+
if page_token:
88+
self._consumed_page_tokens.add(page_token)
89+
self._consumed_page_token_bytes += token_bytes
90+
next_page_token = page.next_page_token
91+
if next_page_token and next_page_token in self._consumed_page_tokens:
92+
raise SandboxError("pager received a repeated continuation token")
93+
self._page_token = next_page_token or None
6694
return page
6795

6896
def all(self) -> builtins.list[T]:

‎tests/sandbox_test.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,49 @@ def fetch(token: str) -> Page[int]:
26522652
assert tokens == ["resume", "resume"]
26532653

26542654

2655+
def test_pager_rejects_a_repeated_continuation_token() -> None:
2656+
pager = Pager(
2657+
lambda token: Page(items=[1], next_page_token=token),
2658+
page_token="resume",
2659+
)
2660+
2661+
with pytest.raises(SandboxError, match="repeated continuation token"):
2662+
next(pager)
2663+
2664+
2665+
def test_pager_bounds_consumed_token_count(monkeypatch: pytest.MonkeyPatch) -> None:
2666+
monkeypatch.setattr(sandbox_module, "_PAGER_MAX_CONSUMED_TOKENS", 1)
2667+
requests: list[str] = []
2668+
pager = Pager(
2669+
lambda token: (
2670+
requests.append(token),
2671+
Page(items=[token], next_page_token="next"),
2672+
)[1],
2673+
page_token="first",
2674+
)
2675+
2676+
assert next(pager).items == ["first"]
2677+
with pytest.raises(SandboxError, match="token history limit exceeded"):
2678+
next(pager)
2679+
assert requests == ["first"]
2680+
2681+
2682+
def test_pager_bounds_consumed_token_bytes(monkeypatch: pytest.MonkeyPatch) -> None:
2683+
monkeypatch.setattr(sandbox_module, "_PAGER_MAX_CONSUMED_TOKEN_BYTES", 1)
2684+
requests: list[str] = []
2685+
pager = Pager(
2686+
lambda token: (
2687+
requests.append(token),
2688+
Page(items=[token], next_page_token="next"),
2689+
)[1],
2690+
page_token="too-large",
2691+
)
2692+
2693+
with pytest.raises(SandboxError, match="token history limit exceeded"):
2694+
next(pager)
2695+
assert requests == []
2696+
2697+
26552698
def test_list_ids_forwards_label_selector() -> None:
26562699
stub = _FakeSandboxStub(listed=[_make_sandbox_proto("sandbox-1", "job-1")])
26572700
client = _client_with_fake_stub(stub)

0 commit comments

Comments
 (0)