Merged
Conversation
There was a problem hiding this comment.
6 issues found across 5 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/l0/guardrails.py">
<violation number="1" location="src/l0/guardrails.py:1563">
P2: The cached `is_json_content` flag is never safely invalidated for reused/new streams, so `json_rule()` can keep the previous stream's JSON/non-JSON classification and skip or mis-run validation.</violation>
<violation number="2" location="src/l0/guardrails.py:1889">
P1: Guard against an empty `patterns` list before compiling the combined regex; otherwise this rule matches the empty string everywhere and floods the stream with false violations.</violation>
<violation number="3" location="src/l0/guardrails.py:1904">
P2: This delta-only scan offset can miss earlier bad patterns during the later full-content check, because the same rule is first run against `context.delta` and then reused for the whole response.</violation>
</file>
<file name="src/l0/drift.py">
<violation number="1" location="src/l0/drift.py:409">
P2: `get_history()["last_content"]` now returns only the sliding window, which silently truncates long outputs behind the existing API name.</violation>
</file>
<file name="src/l0/state.py">
<violation number="1" location="src/l0/state.py:35">
P1: Buffering tokens here leaves `state.content` stale on abort paths, so post-abort reads and abort telemetry can drop the most recent tokens.</violation>
</file>
<file name="src/l0/runtime.py">
<violation number="1" location="src/l0/runtime.py:733">
P2: Flushing the full content string inside periodic checks makes long streams O(n²) again.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
| state.first_token_at = now | ||
| state.last_token_at = now | ||
| state.content += token | ||
| state._content_buffer.append(token) |
There was a problem hiding this comment.
P1: Buffering tokens here leaves state.content stale on abort paths, so post-abort reads and abort telemetry can drop the most recent tokens.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/l0/state.py, line 35:
<comment>Buffering tokens here leaves `state.content` stale on abort paths, so post-abort reads and abort telemetry can drop the most recent tokens.</comment>
<file context>
@@ -12,23 +12,33 @@ def create_state() -> State:
state.first_token_at = now
state.last_token_at = now
- state.content += token
+ state._content_buffer.append(token)
state.token_count += 1
</file context>
| "last_content": self._history.last_content, | ||
| "entropy": list(self._history.entropy), | ||
| "tokens": list(self._history.tokens), | ||
| "last_content": self._history.last_window, |
There was a problem hiding this comment.
P2: get_history()["last_content"] now returns only the sliding window, which silently truncates long outputs behind the existing API name.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/l0/drift.py, line 409:
<comment>`get_history()["last_content"]` now returns only the sliding window, which silently truncates long outputs behind the existing API name.</comment>
<file context>
@@ -396,14 +396,17 @@ def _detect_excessive_hedging(self, content: str) -> bool:
- "last_content": self._history.last_content,
+ "entropy": list(self._history.entropy),
+ "tokens": list(self._history.tokens),
+ "last_content": self._history.last_window,
}
</file context>
src/l0/runtime.py
Outdated
| phase="post", | ||
| ruleCount=len(guardrails), | ||
| ) | ||
| flush_content(state) |
There was a problem hiding this comment.
P2: Flushing the full content string inside periodic checks makes long streams O(n²) again.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/l0/runtime.py, line 733:
<comment>Flushing the full content string inside periodic checks makes long streams O(n²) again.</comment>
<file context>
@@ -718,82 +719,94 @@ async def emit_buffered_tool_calls() -> AsyncIterator[Event]:
- phase="post",
- ruleCount=len(guardrails),
- )
+ flush_content(state)
+ _has_obs = event_bus._handler is not None
</file context>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by cubic
Speeds up the streaming hot path and reduces memory churn by buffering tokens, lazy concat via a descriptor, delta-only scans, and trimmed guardrail/observability overhead. Benchmarks now show 596K tokens/s for core and 114.9K tokens/s for the full stack, with lower latency under load.
Refactors
State: add_content_buffer; lazy concat via_ContentDescriptor;append_tokenbuffers tokens.json_rulecaches JSON detection and resets on empty content or shrink;markdown_ruleskips analysis untilcompleted;pattern_ruleprecompiles a combined regex, scans only deltas with small overlap, and runs a full scan at completion.deques sized byentropy_window; store only the last analysis window;resethonors window size;get_historyreturns lists.Bug Fixes
_content_bufferon invalid checkpoints and fresh retries to avoid stale content.pattern_rule/drift/json_rulehandle content replacement/shrinks correctly (no stale caches, no unbounded growth, no full re-scan during streaming).Written for commit 57e0c93. Summary will update on new commits.