@trillboards/edge-sdk 0.2.196 → 0.2.200

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.
Files changed (60) hide show
  1. package/deploy/detector/pyproject.toml +1 -1
  2. package/deploy/detector/tests/test_colors_v2.py +79 -0
  3. package/deploy/detector/tests/test_detection_colors_wire.py +25 -0
  4. package/deploy/detector/tests/test_held_phone_customer_packaging.py +102 -0
  5. package/deploy/detector/trillboards_detector/_version.py +2 -2
  6. package/deploy/detector/trillboards_detector/colors.py +30 -11
  7. package/deploy/detector/trillboards_detector/server.py +1 -1
  8. package/deploy/litertlm/test_attention_token_budget.py +57 -0
  9. package/deploy/litertlm/trillboards_litertlm_shim.py +96 -1
  10. package/deploy/macos/build-macos-pkg.sh +14 -2
  11. package/deploy/windows/build-windows.ps1 +12 -2
  12. package/deploy/windows/ci/test-published-install.ps1 +167 -8
  13. package/deploy/windows/setup-detector.ps1 +4 -1
  14. package/dist/DetectionColors.d.ts +11 -4
  15. package/dist/DetectionColors.d.ts.map +1 -1
  16. package/dist/DetectionColors.js +5 -2
  17. package/dist/DetectionColors.js.map +1 -1
  18. package/dist/DetectorFields.d.ts +138 -4
  19. package/dist/DetectorFields.d.ts.map +1 -1
  20. package/dist/DetectorFields.js +101 -11
  21. package/dist/DetectorFields.js.map +1 -1
  22. package/dist/EdgeAgent.js +1 -1
  23. package/dist/LiteRtVlmProvider.d.ts.map +1 -1
  24. package/dist/LiteRtVlmProvider.js +10 -1
  25. package/dist/LiteRtVlmProvider.js.map +1 -1
  26. package/dist/PhoneEmbeddingClassifier.d.ts +28 -0
  27. package/dist/PhoneEmbeddingClassifier.d.ts.map +1 -0
  28. package/dist/PhoneEmbeddingClassifier.js +40 -0
  29. package/dist/PhoneEmbeddingClassifier.js.map +1 -0
  30. package/dist/RetainedFieldContract.d.ts +9 -1
  31. package/dist/RetainedFieldContract.d.ts.map +1 -1
  32. package/dist/RetainedFieldContract.js +19 -3
  33. package/dist/RetainedFieldContract.js.map +1 -1
  34. package/dist/ScopedVlmReview.d.ts +6 -2
  35. package/dist/ScopedVlmReview.d.ts.map +1 -1
  36. package/dist/ScopedVlmReview.js +55 -5
  37. package/dist/ScopedVlmReview.js.map +1 -1
  38. package/dist/SensingProfileClient.d.ts +6 -0
  39. package/dist/SensingProfileClient.d.ts.map +1 -1
  40. package/dist/SensingProfileClient.js +7 -0
  41. package/dist/SensingProfileClient.js.map +1 -1
  42. package/dist/diagnose.d.ts +1 -1
  43. package/dist/diagnose.js +1 -1
  44. package/dist/localSensing.d.ts +5 -2
  45. package/dist/localSensing.d.ts.map +1 -1
  46. package/dist/localSensing.js +56 -8
  47. package/dist/localSensing.js.map +1 -1
  48. package/package.json +3 -3
  49. package/retained/ProfileAuthorityClient.cjs +1 -1
  50. package/retained/RetainedCaptureClient.cjs +2 -1
  51. package/retained/WebsiteAdmissionCaptureClient.cjs +478 -0
  52. package/retained/WebsiteAdmissionState.cjs +75 -0
  53. package/retained/__tests__/actor-perception.test.cjs +1 -1
  54. package/retained/__tests__/actor-window.test.cjs +1 -1
  55. package/retained/__tests__/archive-lifecycle.test.cjs +1 -1
  56. package/retained/__tests__/runtime.test.cjs +30 -2
  57. package/retained/__tests__/website-admission-realtime.test.cjs +503 -0
  58. package/retained/__tests__/website-admission.test.cjs +806 -0
  59. package/retained/manager.cjs +4 -1
  60. package/retained/pilot-profile-client.cjs +2 -1
@@ -10,7 +10,7 @@ name = "trillboards-detector"
10
10
  # constant, a `labels.py`-only change survives `uv tool install` AND
11
11
  # `uv tool install --force`; move the version and a bare `uv tool install`
12
12
  # reinstalls. See `stamp_version.py`.
13
- version = "0.2.0+src.a8173c092f27"
13
+ version = "0.2.0+src.42da00861e2b"
14
14
  description = "On-device object-detector service for the Trillboards BYO-CCTV connector (loopback HTTP, ONNX Runtime)."
15
15
  requires-python = ">=3.10"
16
16
 
@@ -0,0 +1,79 @@
1
+ """Bounded colour contract; synthetic fixtures establish arithmetic, never field accuracy."""
2
+ import importlib.util
3
+ import unittest
4
+
5
+ from trillboards_detector.colors import validate_color_request, build_color_response
6
+
7
+ V1 = {"version": 1, "minScore": .25, "maxBoxes": 32, "hue": [[0, 10], [240, 255]],
8
+ "minSaturation": 100, "minValue": 60}
9
+ V2 = {"version": 2, "minScore": .25, "maxBoxes": 32, "hue": [[0, 255]],
10
+ "minSaturation": 0, "maxSaturation": 40, "minValue": 60, "maxValue": 199}
11
+
12
+
13
+ class BoundedColourContractTest(unittest.TestCase):
14
+ def test_v1_request_and_response_are_unchanged(self):
15
+ self.assertEqual(validate_color_request(V1), V1)
16
+ self.assertEqual(build_color_response(V1, [2], [.08], "a" * 64, (160, 120)),
17
+ {"version": 1, "imageSha256": "a" * 64, "frameSize": {"width": 160, "height": 120},
18
+ "request": V1, "people": [{"detectionIndex": 2, "fraction": .08}]})
19
+ with self.assertRaises(ValueError):
20
+ validate_color_request({**V1, "maxValue": 255})
21
+
22
+ def test_v2_accepts_bounds_and_echoes_its_own_version(self):
23
+ self.assertEqual(validate_color_request(V2), V2)
24
+ result = build_color_response(V2, [2, 1], [.08, 0.0], "b" * 64, (160, 120))
25
+ self.assertEqual(result["version"], 2)
26
+ self.assertEqual(result["request"], V2)
27
+ self.assertEqual(result["people"], [{"detectionIndex": 2, "fraction": .08},
28
+ {"detectionIndex": 1, "fraction": 0.0}])
29
+
30
+ def test_v2_accepts_equal_inclusive_endpoints(self):
31
+ request = {**V2, "minSaturation": 40, "maxSaturation": 40, "minValue": 199, "maxValue": 199}
32
+ self.assertEqual(validate_color_request(request), request)
33
+
34
+ def test_v2_refuses_missing_unknown_or_invalid_bounds(self):
35
+ bad = [{**V2, "version": 3}, {**V2, "version": True},
36
+ {k: v for k, v in V2.items() if k != "maxValue"},
37
+ {k: v for k, v in V2.items() if k != "maxSaturation"},
38
+ {**V2, "minSaturation": 41}, {**V2, "minValue": 200},
39
+ {**V2, "maxSaturation": 256}, {**V2, "maxValue": -1},
40
+ {**V2, "maxValue": True}, {**V2, "maxValue": 199.5},
41
+ {**V2, "hue": [[10, 0]]}, {**V2, "maxBoxes": 33}, {**V2, "extra": 1}]
42
+ for request in bad:
43
+ with self.subTest(request=request), self.assertRaises(ValueError):
44
+ validate_color_request(request)
45
+
46
+ def test_v2_empty_selection_remains_an_explicit_empty_result(self):
47
+ result = build_color_response(V2, [], [], "c" * 64, (160, 120))
48
+ self.assertEqual(result["version"], 2)
49
+ self.assertEqual(result["people"], [])
50
+
51
+
52
+ @unittest.skipUnless(importlib.util.find_spec("numpy"), "NumPy unavailable for actual HSV arithmetic")
53
+ class BoundedColourArithmeticTest(unittest.TestCase):
54
+ def test_upper_and_lower_bounds_and_red_hue_wrap_are_inclusive(self):
55
+ import numpy as np
56
+ from trillboards_detector.colors import color_pixel_mask
57
+ hsv = np.asarray([[[0, 100, 60], [10, 200, 199], [240, 200, 199], [255, 200, 199],
58
+ [11, 100, 60], [239, 100, 60], [0, 99, 60], [0, 201, 60],
59
+ [0, 100, 59], [0, 100, 200]]], dtype=np.int16)
60
+ request = {**V2, "hue": [[0, 10], [240, 255]], "minSaturation": 100, "maxSaturation": 200}
61
+ self.assertEqual(color_pixel_mask(hsv, request).tolist(),
62
+ [[True, True, True, True, False, False, False, False, False, False]])
63
+ self.assertEqual(color_pixel_mask(hsv, V1).tolist(),
64
+ [[True, True, True, True, False, False, False, True, False, True]])
65
+
66
+ def test_black_white_and_grey_are_distinguished_by_value_and_saturation(self):
67
+ import numpy as np
68
+ from trillboards_detector.colors import color_pixel_mask
69
+ hsv = np.asarray([[[0, 0, 0], [200, 255, 59], [0, 0, 60], [255, 40, 199],
70
+ [0, 0, 200], [255, 40, 255], [0, 41, 200]]], dtype=np.int16)
71
+ black = {**V2, "maxSaturation": 255, "minValue": 0, "maxValue": 59}
72
+ white = {**V2, "minValue": 200, "maxValue": 255}
73
+ self.assertEqual(color_pixel_mask(hsv, black).tolist(), [[True, True, False, False, False, False, False]])
74
+ self.assertEqual(color_pixel_mask(hsv, V2).tolist(), [[False, False, True, True, False, False, False]])
75
+ self.assertEqual(color_pixel_mask(hsv, white).tolist(), [[False, False, False, False, True, True, False]])
76
+
77
+
78
+ if __name__ == "__main__":
79
+ unittest.main()
@@ -123,6 +123,31 @@ class DetectionColorsWireTest(unittest.TestCase):
123
123
  self.assertNotIn("detections", body)
124
124
  self.assertEqual(self.source.calls, 0)
125
125
 
126
+ def test_v2_bounds_select_grey_without_matching_the_saturated_red_half(self):
127
+ self.start([Detection("person", .9, 10, 10, 40, 80), Detection("person", .8, 100, 10, 40, 80)])
128
+ request = {"version": 2, "minScore": .25, "maxBoxes": 32, "hue": [[0, 255]],
129
+ "minSaturation": 0, "maxSaturation": 40, "minValue": 60, "maxValue": 199}
130
+ status, body = self.post(detection_colors=request)
131
+ self.assertEqual(status, 200)
132
+ colors = body["detection_colors"]
133
+ self.assertEqual(colors["version"], 2)
134
+ self.assertEqual(colors["request"], request)
135
+ self.assertEqual(colors["imageSha256"], hashlib.sha256(self.jpeg).hexdigest())
136
+ self.assertEqual(colors["frameSize"], {"width": 160, "height": 120})
137
+ self.assertEqual(colors["people"], [{"detectionIndex": 0, "fraction": 1.0},
138
+ {"detectionIndex": 1, "fraction": 0.0}])
139
+
140
+ def test_v2_invalid_or_incomplete_bounds_are_refused_before_detection(self):
141
+ self.start([Detection("person", .9, 100, 10, 40, 80)])
142
+ request = {**UNIFORM, "version": 2, "maxSaturation": 255, "maxValue": 255}
143
+ for change in [{"maxSaturation": 99}, {"maxValue": 59}, {"maxValue": True},
144
+ {"maxValue": 256}, {"extra": 1}]:
145
+ status, _ = self.post(detection_colors={**request, **change})
146
+ self.assertEqual(status, 400, change)
147
+ status, _ = self.post(detection_colors={key: value for key, value in request.items() if key != "maxValue"})
148
+ self.assertEqual(status, 400)
149
+ self.assertEqual(self.source.calls, 0)
150
+
126
151
  def test_an_unrequested_colour_task_is_absent(self):
127
152
  self.start([Detection("person", .9, 100, 10, 40, 80)])
128
153
  status, body = self.post(detection_colors=None)
@@ -0,0 +1,102 @@
1
+ """Customer manifest and tiny source-staging fixtures; no installer/model run."""
2
+ import base64
3
+ import json
4
+ import os
5
+ from pathlib import Path
6
+ import re
7
+ import shutil
8
+ import subprocess
9
+ import tempfile
10
+ import unittest
11
+
12
+ SDK = Path(__file__).resolve().parents[3]
13
+ DEPLOY = SDK / "deploy"
14
+
15
+
16
+ class HeldPhoneCustomerPackaging(unittest.TestCase):
17
+ def test_npm_manifest_excludes_frozen_phone_but_keeps_other_retained_and_rf_pose(self):
18
+ result = subprocess.run(["npm", "pack", "--dry-run", "--json", "--ignore-scripts"],
19
+ cwd=SDK, capture_output=True, text=True, timeout=40)
20
+ self.assertEqual(result.returncode, 0, result.stderr)
21
+ if os.environ.get("HELD_PHONE_NPM_MANIFEST"):
22
+ Path(os.environ["HELD_PHONE_NPM_MANIFEST"]).write_text(result.stdout)
23
+ files = {row["path"] for row in json.loads(result.stdout)[0]["files"]}
24
+ self.assertFalse(sorted(p for p in files if p.startswith("retained/phone-embedding/")))
25
+ tracked = subprocess.check_output(["git", "ls-files", "--", "retained"], cwd=SDK, text=True).splitlines()
26
+ expected = {p for p in tracked if not p.startswith("retained/phone-embedding/")
27
+ and Path(p).name not in (".npmignore", ".gitignore", ".gitattributes")}
28
+ self.assertTrue(expected)
29
+ self.assertEqual(sorted(expected-files), [])
30
+ for relative in ["deploy/detector/pyproject.toml", "deploy/detector/trillboards_detector/loader.py",
31
+ "deploy/detector/trillboards_detector/pose.py", "deploy/detector/trillboards_detector/session.py"]:
32
+ self.assertIn(relative, files)
33
+ self.assertNotIn("ai-edge-litert", (DEPLOY / "detector/pyproject.toml").read_text())
34
+
35
+ def test_actual_windows_copy_omits_only_phone_and_preserves_original(self):
36
+ powershell = shutil.which("powershell.exe")
37
+ if not powershell:
38
+ self.skipTest("Windows PowerShell is required for the Windows copy fixture")
39
+ source = DEPLOY / "windows/build-windows.ps1"
40
+ source_text = source.read_text()
41
+ self.assertIn('Copy-RetainedRuntime -Source "$PkgRoot\\retained"', source_text)
42
+ self.assertIn('Copy-RetainedRuntime -Source "$src\\retained"', source_text)
43
+ source_path = str(source) if os.name == "nt" else subprocess.check_output(["wslpath", "-w", str(source)], text=True).strip()
44
+ script = r"""
45
+ $ErrorActionPreference='Stop';$ProgressPreference='SilentlyContinue'
46
+ $tokens=$null;$errors=$null
47
+ $tree=[Management.Automation.Language.Parser]::ParseInput([IO.File]::ReadAllText('%s',[Text.Encoding]::UTF8),[ref]$tokens,[ref]$errors)
48
+ if($errors){throw 'Staging source parse failed'}
49
+ $definition=$tree.Find({param($node)$node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Copy-RetainedRuntime'},$true)
50
+ if(-not $definition){throw 'Missing actual staging function'}
51
+ Invoke-Expression $definition.Extent.Text
52
+ $base=[IO.Path]::GetFullPath([IO.Path]::GetTempPath())
53
+ $root=Join-Path $base ('trillboards-held-phone-copy-'+[Guid]::NewGuid().ToString('N'))
54
+ try {
55
+ $source=Join-Path $root 'source'
56
+ New-Item -ItemType Directory -Force -Path (Join-Path $source 'phone-embedding\source'),(Join-Path $source 'other')|Out-Null
57
+ foreach($name in @('manager.cjs','.evidence','other\keep.json','phone-embedding\source\model.tflite')) {
58
+ Set-Content -LiteralPath (Join-Path $source $name) -Value 'non-model fixture'
59
+ }
60
+ $results=@()
61
+ foreach($relative in @('lib\edge-sdk\retained','lib\node_modules\@trillboards\edge-sdk\retained')){
62
+ $dest=Join-Path $root $relative
63
+ Copy-RetainedRuntime -Source $source -Destination $dest
64
+ $results+=,@(Get-ChildItem -LiteralPath $dest -Recurse -Force -File|ForEach-Object{$_.FullName.Substring($dest.Length+1)}|Sort-Object)
65
+ }
66
+ [pscustomobject]@{results=@($results);originalPreserved=(Test-Path -LiteralPath (Join-Path $source 'phone-embedding\source\model.tflite'))}|ConvertTo-Json -Compress -Depth 5
67
+ }finally{
68
+ $resolved=[IO.Path]::GetFullPath($root)
69
+ if(-not $resolved.StartsWith($base,[StringComparison]::OrdinalIgnoreCase) -or [IO.Path]::GetFileName($resolved) -notlike 'trillboards-held-phone-copy-*'){throw 'Fixture cleanup escaped temp root'}
70
+ Remove-Item -LiteralPath $resolved -Recurse -Force -ErrorAction SilentlyContinue
71
+ }
72
+ """ % source_path.replace("'", "''")
73
+ result = subprocess.run([powershell, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-EncodedCommand",
74
+ base64.b64encode(script.encode("utf-16le")).decode()],
75
+ capture_output=True, text=True, timeout=15)
76
+ self.assertEqual(result.returncode, 0, result.stdout+result.stderr)
77
+ self.assertEqual(json.loads(result.stdout.strip().splitlines()[-1]),
78
+ {"results": [[".evidence", "manager.cjs", "other\\keep.json"]]*2, "originalPreserved": True})
79
+
80
+ def test_actual_macos_copy_omits_only_phone_and_preserves_original(self):
81
+ source = (DEPLOY / "macos/build-macos-pkg.sh").read_text()
82
+ match = re.search(r"^copy_retained_runtime\(\) \{.*?^\}", source, re.M | re.S)
83
+ self.assertIsNotNone(match)
84
+ self.assertIn('copy_retained_runtime "$PKG_ROOT/retained"', source)
85
+ self.assertIn('copy_retained_runtime "$src/retained"', source)
86
+ with tempfile.TemporaryDirectory(prefix="held-phone-copy-") as temp:
87
+ root=Path(temp)
88
+ for name in ["manager.cjs", ".evidence", "other/keep.json", "phone-embedding/source/model.tflite"]:
89
+ file=root/"source"/name;file.parent.mkdir(parents=True,exist_ok=True);file.write_text("non-model fixture")
90
+ for relative in ["lib/edge-sdk/retained", "lib/node_modules/@trillboards/edge-sdk/retained"]:
91
+ dest=root/relative
92
+ result=subprocess.run(["bash","-c",match.group()+'\ncopy_retained_runtime "$1" "$2"',
93
+ "held-phone-copy-fixture",str(root/"source"),str(dest)],
94
+ capture_output=True,text=True,timeout=5)
95
+ self.assertEqual(result.returncode,0,result.stderr)
96
+ self.assertEqual(sorted(p.relative_to(dest).as_posix() for p in dest.rglob("*") if p.is_file()),
97
+ [".evidence","manager.cjs","other/keep.json"])
98
+ self.assertTrue((root/"source/phone-embedding/source/model.tflite").is_file())
99
+
100
+
101
+ if __name__ == "__main__":
102
+ unittest.main()
@@ -13,5 +13,5 @@ human sets.
13
13
  """
14
14
 
15
15
  BASE_VERSION = "0.2.0"
16
- SOURCE_FINGERPRINT = "a8173c092f27"
17
- VERSION = "0.2.0+src.a8173c092f27"
16
+ SOURCE_FINGERPRINT = "42da00861e2b"
17
+ VERSION = "0.2.0+src.42da00861e2b"
@@ -4,7 +4,9 @@ A uniform is the cheapest staff cue a ceiling camera sees: the Texas paint
4
4
  counter's staff wear the store's red vest or shirt, its customers almost never
5
5
  do. The connector decides "is a staff member here" from the share of a
6
6
  person's box that is uniform-coloured; this module measures that share and
7
- nothing else. It runs no model and makes no decision.
7
+ nothing else. It runs no model and makes no decision. Version 2 also supports
8
+ upper saturation/value bounds for selected achromatic and bounded colours;
9
+ neither version identifies garment type.
8
10
 
9
11
  The arithmetic is the one the 2026-11-04 measurement used
10
12
  (docs/runbooks/cctv-detector-tier.md section 5b): Pillow decodes the original
@@ -26,20 +28,28 @@ MAX_HUE_RANGES = 4
26
28
 
27
29
  def validate_color_request(value):
28
30
  """`detection_colors` -> the same dict, or ValueError."""
31
+ bounded = isinstance(value, dict) and value.get("version") == 2
32
+ fields = {"version", "minScore", "maxBoxes", "hue", "minSaturation", "minValue"}
33
+ if bounded:
34
+ fields |= {"maxSaturation", "maxValue"}
29
35
  if (not isinstance(value, dict)
30
- or set(value) != {"version", "minScore", "maxBoxes", "hue", "minSaturation", "minValue"}
31
- or type(value["version"]) is not int or value["version"] != 1
36
+ or set(value) != fields
37
+ or type(value["version"]) is not int or value["version"] not in (1, 2)
32
38
  or isinstance(value["minScore"], bool) or not isinstance(value["minScore"], (int, float))
33
39
  or not math.isfinite(value["minScore"]) or not 0 < value["minScore"] <= 1
34
40
  or type(value["maxBoxes"]) is not int or not 1 <= value["maxBoxes"] <= MAX_COLOR_BOXES
35
41
  or type(value["minSaturation"]) is not int or not 0 <= value["minSaturation"] <= 255
36
42
  or type(value["minValue"]) is not int or not 0 <= value["minValue"] <= 255
43
+ or bounded and (type(value["maxSaturation"]) is not int
44
+ or not value["minSaturation"] <= value["maxSaturation"] <= 255
45
+ or type(value["maxValue"]) is not int
46
+ or not value["minValue"] <= value["maxValue"] <= 255)
37
47
  or not isinstance(value["hue"], list) or not 1 <= len(value["hue"]) <= MAX_HUE_RANGES
38
48
  or any(not isinstance(r, list) or len(r) != 2 or any(type(v) is not int for v in r)
39
49
  or not 0 <= r[0] <= r[1] <= 255 for r in value["hue"])):
40
50
  raise ValueError(
41
- "detection_colors needs version 1, minScore in (0, 1], maxBoxes 1..%d, "
42
- "1..%d hue ranges of whole 0..255 values, and whole saturation/value floors"
51
+ "detection_colors needs version 1 floors or version 2 saturation/value bounds, "
52
+ "minScore in (0, 1], maxBoxes 1..%d, and 1..%d hue ranges of whole 0..255 values"
43
53
  % (MAX_COLOR_BOXES, MAX_HUE_RANGES))
44
54
  return {**value, "hue": [list(r) for r in value["hue"]]}
45
55
 
@@ -52,6 +62,19 @@ def color_box_indices(detections, request):
52
62
  return [index for index, _ in people[:request["maxBoxes"]]]
53
63
 
54
64
 
65
+ def color_pixel_mask(hsv, request):
66
+ """Inclusive Pillow 8-bit HSV predicate; version 1 retains its measured floors."""
67
+ import numpy as np
68
+ hue, sat, val = hsv[..., 0], hsv[..., 1], hsv[..., 2]
69
+ in_hue = np.zeros(hue.shape, dtype=bool)
70
+ for low, high in request["hue"]:
71
+ in_hue |= (hue >= low) & (hue <= high)
72
+ match = in_hue & (sat >= request["minSaturation"]) & (val >= request["minValue"])
73
+ if request["version"] == 2:
74
+ match &= (sat <= request["maxSaturation"]) & (val <= request["maxValue"])
75
+ return match
76
+
77
+
55
78
  def detection_color_fractions(jpeg, boxes, request):
56
79
  """Fraction of each [x, y, width, height] box whose pixels match the request."""
57
80
  import numpy as np
@@ -61,11 +84,7 @@ def detection_color_fractions(jpeg, boxes, request):
61
84
  raise ValueError("colour fractions need a bounded original JPEG")
62
85
  hsv = np.asarray(image.convert("HSV")).astype(np.int16)
63
86
  height, width = hsv.shape[0], hsv.shape[1]
64
- hue, sat, val = hsv[..., 0], hsv[..., 1], hsv[..., 2]
65
- in_hue = np.zeros(hue.shape, dtype=bool)
66
- for low, high in request["hue"]:
67
- in_hue |= (hue >= low) & (hue <= high)
68
- match = in_hue & (sat >= request["minSaturation"]) & (val >= request["minValue"])
87
+ match = color_pixel_mask(hsv, request)
69
88
  fractions = []
70
89
  for x, y, w, h in boxes:
71
90
  x0, x1 = int(max(0, x)), int(min(width, x + w))
@@ -79,7 +98,7 @@ def build_color_response(request, indices, fractions, image_sha256, frame_size):
79
98
  if len(indices) != len(fractions) or any(
80
99
  isinstance(f, bool) or not isinstance(f, float) or not 0 <= f <= 1 for f in fractions):
81
100
  raise ValueError("colour fractions differ from the requested person boxes")
82
- return {"version": 1, "imageSha256": image_sha256,
101
+ return {"version": request["version"], "imageSha256": image_sha256,
83
102
  "frameSize": {"width": frame_size[0], "height": frame_size[1]},
84
103
  "request": request,
85
104
  "people": [{"detectionIndex": i, "fraction": f} for i, f in zip(indices, fractions)]}
@@ -276,7 +276,7 @@ def parse_detection_pose(raw: bytes):
276
276
 
277
277
 
278
278
  def parse_detection_colors(raw: bytes):
279
- """`detection_colors`: uniform-colour share of each confident person box."""
279
+ """`detection_colors`: v1 floors or v2 bounded colour share of each confident person box."""
280
280
  body = json.loads(raw.decode("utf-8"))
281
281
  if "detection_colors" not in body:
282
282
  return None
@@ -0,0 +1,57 @@
1
+ """Native conversation rendering and token accounting, without model inference."""
2
+ import sys
3
+ import unittest
4
+ from pathlib import Path
5
+ from types import SimpleNamespace
6
+ sys.path.insert(0, str(Path(__file__).parent))
7
+ import trillboards_litertlm_shim as shim
8
+
9
+ class TokenBudgetTest(unittest.TestCase):
10
+ def conversation(self, n=20, capacity=100, previous=3):
11
+ calls = []
12
+ def tokenize(text):
13
+ calls.append(text)
14
+ return list(range(n))
15
+ c = SimpleNamespace(_engine=SimpleNamespace(tokenize=tokenize, max_num_tokens=capacity, _trillboards_initial_prefix_tokens=1),
16
+ token_count=previous, render_message_to_string=lambda message: 'native chat template')
17
+ return c, calls
18
+
19
+ def test_uses_the_native_render_and_loaded_engine_tokenizer(self):
20
+ self.assertTrue(callable(getattr(shim, '_native_text_budget', None)))
21
+ c, calls = self.conversation()
22
+ result = shim._native_text_budget(c, {'role':'user','content':[{'type':'text','text':'x'*5000}]}, 10)
23
+ self.assertEqual(calls, ['native chat template'])
24
+ self.assertEqual(result, {'version':1,'contextTokens':100,'inputTokens':23,'reservedOutputTokens':10})
25
+
26
+ def test_output_capacity_is_reserved_before_generation(self):
27
+ self.assertTrue(callable(getattr(shim, '_native_text_budget', None)))
28
+ c, _ = self.conversation(n=90)
29
+ with self.assertRaisesRegex(ValueError, 'context|budget'):
30
+ shim._native_text_budget(c, {'role':'user','content':[]}, 10)
31
+
32
+ def test_missing_capacity_and_image_token_accounting_are_refused(self):
33
+ self.assertTrue(callable(getattr(shim, '_native_text_budget', None)))
34
+ c, _ = self.conversation(capacity=None)
35
+ with self.assertRaises(ValueError):
36
+ shim._native_text_budget(c, {'role':'user','content':[]}, 10)
37
+ c, _ = self.conversation()
38
+ with self.assertRaises(ValueError):
39
+ shim._native_text_budget(c, {'role':'user','content':[{'type':'image','blob':'x'}]}, 10)
40
+
41
+ def test_native_initial_prefix_is_counted_only_on_an_empty_conversation(self):
42
+ c, _ = self.conversation(previous=0)
43
+ self.assertEqual(shim._native_text_budget(c, {'role':'user','content':[]}, 10)['inputTokens'],21)
44
+ c.token_count = 8
45
+ self.assertEqual(shim._native_text_budget(c, {'role':'user','content':[]}, 10)['inputTokens'],28)
46
+
47
+ def test_unmeasured_prefix_cannot_pass_a_budget(self):
48
+ c, _ = self.conversation(previous=0)
49
+ del c._engine._trillboards_initial_prefix_tokens
50
+ with self.assertRaisesRegex(ValueError, 'prefix'):
51
+ shim._native_text_budget(c, {'role':'user','content':[]}, 10)
52
+
53
+ def test_prefix_measurement_is_a_native_engine_operation(self):
54
+ self.assertTrue(callable(getattr(shim, '_measure_native_token_prefix', None)))
55
+
56
+ if __name__ == '__main__':
57
+ unittest.main()
@@ -317,6 +317,63 @@ def _apply_native_images(module):
317
317
  conversation.send_message_async = send
318
318
 
319
319
 
320
+ class _ContextBudgetExceeded(ValueError):
321
+ def __init__(self, budget):
322
+ super().__init__('local context budget exceeded before generation')
323
+ self.budget = budget
324
+
325
+
326
+ def _measure_native_token_prefix(engine):
327
+ """Measure the loaded template's initial prefix once, before allocating the request conversation.
328
+
329
+ Native rendering excludes the runtime's initial special tokens. Measure that
330
+ difference using two distinct native prefills, then verify each request's
331
+ observed usage against its budget. No model-family guesses or byte ratios.
332
+ """
333
+ if hasattr(engine, '_trillboards_initial_prefix_tokens'):
334
+ return
335
+ offsets = []
336
+ for text in ('x', 'Native token accounting: café 123.'):
337
+ message = {'role': 'user', 'content': [{'type': 'text', 'text': text}]}
338
+ with engine.create_conversation(messages=[]) as probe:
339
+ rendered_tokens = len(engine.tokenize(probe.render_message_to_string(message)))
340
+ probe.send_message(message, max_output_tokens=1)
341
+ observed = probe.get_benchmark_info().last_prefill_token_count
342
+ if isinstance(observed, bool) or not isinstance(observed, int):
343
+ raise ValueError('native prefix prefill count required')
344
+ offsets.append(observed - rendered_tokens)
345
+ if offsets[0] != offsets[1] or not 0 <= offsets[0] <= 64:
346
+ raise ValueError('native initial prefix is not stable for this loaded template')
347
+ engine._trillboards_initial_prefix_tokens = offsets[0]
348
+
349
+
350
+ def _native_text_budget(conversation, message, reserve):
351
+ """Count the exact native chat rendering on its already loaded tokenizer."""
352
+ engine = getattr(conversation, '_engine', None)
353
+ capacity = getattr(engine, 'max_num_tokens', None)
354
+ if (isinstance(capacity, bool) or not isinstance(capacity, int) or capacity < 1
355
+ or isinstance(reserve, bool) or not isinstance(reserve, int) or reserve < 1):
356
+ raise ValueError('native context capacity and output budget required')
357
+ parts = message.get('content', []) if isinstance(message, dict) else []
358
+ if any(not isinstance(p, dict) or p.get('type') != 'text' for p in parts):
359
+ raise ValueError('native text token budget cannot account for image tokens')
360
+ rendered = conversation.render_message_to_string(message)
361
+ if not isinstance(rendered, str) or not rendered:
362
+ raise ValueError('native chat rendering required for token budget')
363
+ previous = conversation.token_count
364
+ if isinstance(previous, bool) or not isinstance(previous, int) or previous < 0:
365
+ raise ValueError('native conversation token count required')
366
+ prefix = getattr(engine, '_trillboards_initial_prefix_tokens', None)
367
+ if isinstance(prefix, bool) or not isinstance(prefix, int) or prefix < 0:
368
+ raise ValueError('measured native initial prefix required')
369
+ budget = {'version': 1, 'contextTokens': capacity,
370
+ 'inputTokens': previous + len(engine.tokenize(rendered)) + (prefix if previous == 0 else 0),
371
+ 'reservedOutputTokens': reserve}
372
+ if budget['inputTokens'] + reserve > capacity:
373
+ raise _ContextBudgetExceeded(budget)
374
+ return budget
375
+
376
+
320
377
  def apply_runtime_handler(module):
321
378
  """Bind proof to the engine used by this HTTP request, outside model text."""
322
379
  import contextvars
@@ -326,12 +383,17 @@ def apply_runtime_handler(module):
326
383
  return
327
384
  active = contextvars.ContextVar('trillboards_runtime_identity', default=None)
328
385
  original_post, original_engine, original_dump = handler.do_POST, handler._get_engine, module._dump_json
386
+ original_chat = getattr(handler, '_handle_chat_completions', None)
329
387
 
330
388
  def do_post(self):
331
389
  expected = self.headers.get('X-Trillboards-Model-Sha256')
332
390
  if expected is not None and not re.fullmatch(r'[a-f0-9]{64}', expected):
333
391
  self.send_error(400, 'invalid selected artifact hash')
334
392
  return
393
+ budget_header = self.headers.get('X-Trillboards-Token-Budget')
394
+ if budget_header not in (None, 'required') or budget_header is not None and expected is None:
395
+ self.send_error(400, 'pinned native token budget request required')
396
+ return
335
397
  raw_images = self.headers.get('X-Trillboards-Image-Sha256')
336
398
  images = raw_images.split(',') if raw_images is not None else None
337
399
  if images is not None and (expected is None or not 1 <= len(images) <= 5
@@ -339,7 +401,7 @@ def apply_runtime_handler(module):
339
401
  self.send_error(400, 'invalid pinned native image request')
340
402
  return
341
403
  image_token = _image_request.set({'expected': images, 'consumed': None} if images is not None else None)
342
- token = active.set({'expected': expected, 'proof': None})
404
+ token = active.set({'expected': expected, 'proof': None, 'budgetRequired': budget_header == 'required', 'budget': None})
343
405
  try:
344
406
  return original_post(self)
345
407
  finally:
@@ -376,15 +438,46 @@ def apply_runtime_handler(module):
376
438
  runtime_id = kwargs.get('model_id', args[0] if args else None)
377
439
  if backend is not None:
378
440
  runtime_id = f'{runtime_id},{actual_backend}'
441
+ if state.get('budgetRequired'):
442
+ try:
443
+ _measure_native_token_prefix(engine)
444
+ except Exception as error:
445
+ self.send_error(422, f'native token prefix measurement failed: {type(error).__name__}')
446
+ return None
379
447
  if sha is not None:
380
448
  state['proof'] = {'version': 1, 'sha256': sha, 'format': 'litert-lm',
381
449
  'runtimeModelId': runtime_id}
382
450
  return engine
383
451
 
452
+ def handle_chat(self, conv, prompt, *args, **kwargs):
453
+ state = active.get()
454
+ if state and state.get('budgetRequired'):
455
+ try:
456
+ state['budget'] = _native_text_budget(conv, prompt, kwargs.get('max_completion_tokens'))
457
+ except _ContextBudgetExceeded as error:
458
+ self._headers_sent = True
459
+ self.send_response(413)
460
+ self.send_header('Content-Type', 'application/json')
461
+ self.end_headers()
462
+ self.wfile.write(original_dump({'error': 'local_context_budget_exceeded',
463
+ 'tokenBudget': error.budget, 'runtimeModel': state['proof']}).encode('utf-8'))
464
+ return
465
+ except ValueError as error:
466
+ self.send_error(422, str(error))
467
+ return
468
+ return original_chat(self, conv, prompt, *args, **kwargs)
469
+
384
470
  def dump_json(data, **kwargs):
385
471
  state = active.get()
386
472
  if isinstance(data, dict) and data.get('object') == 'chat.completion' and state and state['proof']:
387
473
  data = {**data, 'runtimeModel': dict(state['proof'])}
474
+ budget = state.get('budget')
475
+ if budget is not None:
476
+ data = {**data, 'tokenBudget': budget}
477
+ usage = data.get('usage') or {}
478
+ if (usage.get('completion_tokens', 0) >= budget['reservedOutputTokens']
479
+ or usage.get('total_tokens', 0) >= budget['contextTokens']):
480
+ data = {**data, 'choices': [{**c, 'finish_reason': 'length'} for c in data.get('choices', [])]}
388
481
  images = _image_request.get()
389
482
  if isinstance(data, dict) and data.get('object') == 'chat.completion' and images is not None:
390
483
  if images['consumed'] != images['expected']:
@@ -393,6 +486,8 @@ def apply_runtime_handler(module):
393
486
  return original_dump(data, **kwargs)
394
487
 
395
488
  handler.do_POST, handler._get_engine, module._dump_json = do_post, get_engine, dump_json
489
+ if original_chat is not None:
490
+ handler._handle_chat_completions = handle_chat
396
491
  handler._trillboards_runtime_identity = True
397
492
 
398
493
 
@@ -206,7 +206,19 @@ log "Building all edge workspaces (tsc, dependency order)…"
206
206
  log "Staging edge-sdk dist…"
207
207
  mkdir -p "$PAYLOAD/lib/edge-sdk"
208
208
  cp -R "$PKG_ROOT/dist" "$PAYLOAD/lib/edge-sdk/dist"
209
- cp -R "$PKG_ROOT/retained" "$PAYLOAD/lib/edge-sdk/retained"
209
+ # Frozen experimental phone code/models remain repository evidence. Released
210
+ # phone sensing uses the normal RF-DETR/pose service; omit the obsolete helper.
211
+ copy_retained_runtime() {
212
+ local source="$1" destination="$2" item
213
+ mkdir -p "$destination"
214
+ for item in "$source"/* "$source"/.[!.]* "$source"/..?*; do
215
+ [ -e "$item" ] || [ -L "$item" ] || continue
216
+ [ "${item##*/}" != 'phone-embedding' ] || continue
217
+ cp -R "$item" "$destination/"
218
+ done
219
+ }
220
+
221
+ copy_retained_runtime "$PKG_ROOT/retained" "$PAYLOAD/lib/edge-sdk/retained"
210
222
  cp "$PKG_ROOT/package.json" "$PAYLOAD/lib/edge-sdk/package.json"
211
223
 
212
224
  # ─── 3. Lay down the runtime node_modules subset (NO heavy native stack) ──────
@@ -242,7 +254,7 @@ for p in edge-core edge-sensing edge-ads edge-federated edge-cloud \
242
254
  rm -rf "$dest"
243
255
  mkdir -p "$dest"
244
256
  [ -d "$src/dist" ] && cp -R "$src/dist" "$dest/dist"
245
- [ "$p" != "edge-sdk" ] || cp -R "$src/retained" "$dest/retained"
257
+ [ "$p" != "edge-sdk" ] || copy_retained_runtime "$src/retained" "$dest/retained"
246
258
  cp "$src/package.json" "$dest/package.json"
247
259
  done
248
260
 
@@ -93,6 +93,16 @@ if (Test-Path $Stage) { Remove-Item -Recurse -Force $Stage }
93
93
  if (Test-Path $Scratch) { Remove-Item -Recurse -Force $Scratch }
94
94
  New-Item -ItemType Directory -Force -Path "$Payload\bin","$Payload\lib","$Scratch" | Out-Null
95
95
 
96
+ # The frozen experimental phone helper remains repository evidence. Released
97
+ # phone sensing uses the normal RF-DETR/pose service; omit the obsolete helper.
98
+ function Copy-RetainedRuntime([string]$Source, [string]$Destination) {
99
+ New-Item -ItemType Directory -Force -Path $Destination | Out-Null
100
+ foreach ($item in (Get-ChildItem -LiteralPath $Source -Force)) {
101
+ if ($item.Name -eq 'phone-embedding') { continue }
102
+ Copy-Item -LiteralPath $item.FullName -Destination $Destination -Recurse -Force
103
+ }
104
+ }
105
+
96
106
  # ─── 1. Build ALL edge workspaces (tsc, dependency order) ──────────────────────
97
107
  # edge-sdk's tsc imports edge-core, edge-platform-rtsp/linux/windows, edge-sensing,
98
108
  # edge-ads, edge-federated AND edge-cloud — every workspace's dist must exist before
@@ -107,7 +117,7 @@ Push-Location $WorkspaceRoot; npm run build; if ($LASTEXITCODE -ne 0) { Die "edg
107
117
  Log "Staging edge-sdk dist…"
108
118
  New-Item -ItemType Directory -Force -Path "$Payload\lib\edge-sdk" | Out-Null
109
119
  Copy-Item -Recurse -Force "$PkgRoot\dist" "$Payload\lib\edge-sdk\dist"
110
- Copy-Item -Recurse -Force "$PkgRoot\retained" "$Payload\lib\edge-sdk\retained"
120
+ Copy-RetainedRuntime -Source "$PkgRoot\retained" -Destination "$Payload\lib\edge-sdk\retained"
111
121
  Copy-Item -Force "$PkgRoot\package.json" "$Payload\lib\edge-sdk\package.json"
112
122
 
113
123
  # ─── 3. Runtime node_modules subset (NO heavy native stack) ────────────────────
@@ -144,7 +154,7 @@ foreach ($p in @("edge-core","edge-sensing","edge-ads","edge-federated","edge-cl
144
154
  if (Test-Path $dest) { Remove-Item -Recurse -Force $dest }
145
155
  New-Item -ItemType Directory -Force -Path $dest | Out-Null
146
156
  if (Test-Path "$src\dist") { Copy-Item -Recurse -Force "$src\dist" "$dest\dist" }
147
- if ($p -eq 'edge-sdk') { Copy-Item -Recurse -Force "$src\retained" "$dest\retained" }
157
+ if ($p -eq 'edge-sdk') { Copy-RetainedRuntime -Source "$src\retained" -Destination "$dest\retained" }
148
158
  Copy-Item -Force "$src\package.json" "$dest\package.json"
149
159
  }
150
160