0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
123 lines
4.6 KiB
Python
123 lines
4.6 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from headroom import utils
|
|
|
|
|
|
class FakeProvider:
|
|
def __init__(self, result: str | None) -> None:
|
|
self.result = result
|
|
self.calls: list[tuple[int, int, str, int]] = []
|
|
|
|
def estimate_cost(
|
|
self, input_tokens: int, output_tokens: int, model: str, cached_tokens: int = 0
|
|
) -> str | None:
|
|
self.calls.append((input_tokens, output_tokens, model, cached_tokens))
|
|
return self.result
|
|
|
|
|
|
def test_hash_helpers_and_request_id() -> None:
|
|
request_id = utils.generate_request_id()
|
|
assert len(request_id) == 36
|
|
assert request_id.count("-") == 4
|
|
|
|
assert (
|
|
utils.compute_hash("hello")
|
|
== "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
|
|
)
|
|
assert utils.compute_hash("hi\ud800") == utils.compute_hash(
|
|
"hi\ud800".encode("utf-8", "surrogatepass")
|
|
)
|
|
assert utils.compute_short_hash("hello", length=8) == "2cf24dba"
|
|
assert utils.fast_hash("hello", length=8) == "5d41402a"
|
|
|
|
|
|
def test_extract_user_query_and_message_hashes() -> None:
|
|
messages = [
|
|
{"role": "system", "content": "rules"},
|
|
{"role": "user", "content": " "},
|
|
{"role": "assistant", "content": "skip"},
|
|
{
|
|
"role": "user",
|
|
"content": [{"type": "image"}, {"type": "text", "text": " latest question "}],
|
|
},
|
|
]
|
|
assert utils.extract_user_query(messages) == "latest question"
|
|
assert utils.extract_user_query([{"role": "assistant", "content": "skip"}]) == ""
|
|
|
|
hash_one = utils.compute_messages_hash(messages)
|
|
hash_two = utils.compute_messages_hash(list(messages))
|
|
assert hash_one == hash_two
|
|
assert len(hash_one) == 16
|
|
|
|
prefix_default = utils.compute_prefix_hash(
|
|
[
|
|
{"role": "system", "content": "a"},
|
|
{"role": "system", "content": "b"},
|
|
{"role": "user", "content": "c"},
|
|
{"role": "assistant", "content": "d"},
|
|
]
|
|
)
|
|
prefix_explicit = utils.compute_prefix_hash(
|
|
[
|
|
{"role": "system", "content": "a"},
|
|
{"role": "system", "content": "b"},
|
|
{"role": "user", "content": "c"},
|
|
{"role": "assistant", "content": "d"},
|
|
],
|
|
prefix_count=3,
|
|
)
|
|
assert prefix_default == prefix_explicit
|
|
assert utils.compute_prefix_hash([]) == utils.compute_short_hash("")
|
|
|
|
|
|
def test_timestamp_marker_and_json_helpers() -> None:
|
|
ts = utils.format_timestamp(datetime(2026, 4, 23, 6, 0, 0))
|
|
assert ts == "2026-04-23T06:00:00Z"
|
|
assert utils.parse_timestamp(ts) == datetime(2026, 4, 23, 6, 0, 0)
|
|
assert utils.parse_timestamp("2026-04-23T06:00:00") == datetime(2026, 4, 23, 6, 0, 0)
|
|
|
|
marker = utils.create_marker("tool_digest", sha256="abc", count="2")
|
|
assert marker == '<headroom:tool_digest sha256="abc" count="2">'
|
|
assert utils.create_tool_digest_marker("abc") == '<headroom:tool_digest sha256="abc">'
|
|
assert utils.create_dropped_context_marker("budget") == (
|
|
'<headroom:dropped_context reason="budget">'
|
|
)
|
|
assert utils.create_dropped_context_marker("budget", count=4) == (
|
|
'<headroom:dropped_context reason="budget" count="4">'
|
|
)
|
|
assert utils.create_truncated_marker(100, 25) == (
|
|
'<headroom:truncated original="100" truncated_to="25">'
|
|
)
|
|
|
|
extracted = utils.extract_markers(
|
|
'x <headroom:tool_digest sha256="abc"> y <headroom:dropped_context reason="budget" count="2">'
|
|
)
|
|
assert extracted == [
|
|
{"type": "tool_digest", "attributes": {"sha256": "abc"}},
|
|
{"type": "dropped_context", "attributes": {"reason": "budget", "count": "2"}},
|
|
]
|
|
|
|
assert utils.safe_json_loads('{"ok": true}') == ({"ok": True}, True)
|
|
assert utils.safe_json_loads("{bad") == (None, False)
|
|
assert utils.safe_json_dumps({"emoji": "café"}) == '{"emoji":"café"}'
|
|
|
|
|
|
def test_cost_formatting_and_deep_copy() -> None:
|
|
provider = FakeProvider("1.25")
|
|
assert utils.estimate_cost(100, 50, "gpt-4o", cached_tokens=10, provider=provider) == 1.25
|
|
assert provider.calls == [(100, 50, "gpt-4o", 10)]
|
|
assert utils.estimate_cost(1, 1, "gpt-4o", provider=None) is None
|
|
|
|
none_provider = FakeProvider(None)
|
|
assert utils.estimate_cost(1, 1, "gpt-4o", provider=none_provider) is None
|
|
|
|
assert utils.format_cost(0.0099) == "$0.0099"
|
|
assert utils.format_cost(1.234) == "$1.23"
|
|
|
|
messages = [{"role": "user", "content": {"nested": ["a"]}}]
|
|
copied = utils.deep_copy_messages(messages)
|
|
copied[0]["content"]["nested"].append("b")
|
|
assert messages == [{"role": "user", "content": {"nested": ["a"]}}]
|