Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ jobs:
- name: Service Registry Tests
run: |
python3 tests/test_pixel_agent_teams_roles_count.py
python3 tests/test_pixel_chat_context_token_limits.py
bash tests/test-service-registry.sh

- name: Switchboard Bypass Inventory
Expand Down
10 changes: 10 additions & 0 deletions ods/extensions/services/dashboard-api/pixel_chat_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,23 @@ class ContextUsage(_Projection):
window: int = Field(ge=1, le=10_000_000)
measuredAt: str = Field(min_length=1, max_length=64, pattern=r"^[0-9TZ: .+\-]+$")

@field_validator("used", "window", mode="before")
@classmethod
def _coerce_integer(cls, value):
return int(value) if isinstance(value, float) and value.is_integer() else value


class ContextModel(_Projection):
id: str = Field(min_length=1, max_length=512, pattern=r"^[^\x00-\x1f\x7f]+$")
provider: str = Field(min_length=1, max_length=128, pattern=r"^[^\x00-\x1f\x7f]+$")
contextWindow: int = Field(ge=1, le=10_000_000)
routeFingerprint: str | None = Field(default=None, min_length=64, max_length=64, pattern=r"^[a-f0-9]{64}$")

@field_validator("contextWindow", mode="before")
@classmethod
def _coerce_window(cls, value):
return int(value) if isinstance(value, float) and value.is_integer() else value


class CompactionState(_Projection):
status: Literal["idle", "running", "completed", "skipped", "failed", "unknown"]
Expand Down
64 changes: 64 additions & 0 deletions ods/tests/test_pixel_chat_context_token_limits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3
"""Regression test: pixel_chat_context coerces integer float token measurements.

The baseline configured _Projection with strict=True, causing Pydantic to reject
exact integer floats (such as 100.0 or 2048.0) commonly produced by JSON deserializers
or arithmetic token counting operations with ValidationError.
"""
import sys
import unittest
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO_ROOT / "extensions/services/dashboard-api"))

from pixel_chat_context import public_context


class PixelChatContextTokenLimitsTests(unittest.TestCase):
def setUp(self):
self.base_context = {
"schemaVersion": 1,
"status": "ready",
"sessionRevision": "rev-1",
"context": {
"used": 100,
"window": 2048,
"measuredAt": "2026-09-19T10:00:00Z",
},
"model": {
"id": "model-1",
"provider": "prov-1",
"contextWindow": 2048,
},
"compaction": {"status": "idle", "count": 0},
"history": {
"status": "ready",
"revision": "a" * 64,
"acknowledgedMessages": 5,
},
}

def test_exact_integer_floats_coerced(self):
# Pass float values for used, window, and contextWindow
data = dict(self.base_context)
data["context"] = dict(data["context"], used=100.0, window=2048.0)
data["model"] = dict(data["model"], contextWindow=2048.0)

result = public_context(data)
self.assertEqual(result["context"]["used"], 100)
self.assertIsInstance(result["context"]["used"], int)
self.assertEqual(result["context"]["window"], 2048)
self.assertIsInstance(result["context"]["window"], int)
self.assertEqual(result["model"]["contextWindow"], 2048)
self.assertIsInstance(result["model"]["contextWindow"], int)

def test_non_integer_floats_still_rejected(self):
data = dict(self.base_context)
data["context"] = dict(data["context"], used=100.5)
with self.assertRaises(Exception):
public_context(data)


if __name__ == "__main__":
unittest.main()
Loading