Dashboard › opencode-lore › Session 0wQzTXyhIHQe
0wQzTXyhIHQe91wyPImplement recovery review corrections in isolated workspace /home/byk/Code/opencode-lore-recall-recovery, current frozen candidate mmmktlwm/b61b0861 with clean empty child spyyzvyy if present; create/use an additive child only if needed, but do NOT commit/rebase/move bookmarks. This is a code task. Read AGENTS.md, quality/REVIEW.md, package scripts/current code/tests. repo-setup unavailable: note and proceed. Never touch server, network, production DB; use temporary Vitest DB. Apply_patch only.
First add deterministic regressions that fail on frozen b61b0861 for all accepted blockers and guard discrimination, using real pipeline paths: A. Recovery-specific strict envelope validation across Anthropic, Vertex, Gemini, OpenAI Responses JSON, and Codex accumulated SSE. Require nonempty identity/model, complete validated usage, assistant/model role, coherent content/tool status, and allowlisted successful terminal/stop semantics. Reject failed/cancelled/incomplete/max_tokens/pause/context-window states and ordinary failed tool calls. Preserve valid refusals under explicit allowlist. Each malformed variant: exactly one synthesis attempt after failed continuation, fixed HTTP 502 recall failure, no private text, no recall-store/session/temporal/transfer persistence, no 4th call. B. Codex quota metadata: normalize through bounded reviewed schema only; reject arbitrary nested/free-form/newline content; impose request-wide event and byte cap across accepted + failed + recovery events, while preserving benign Q1→Q2→Q3 order. Add hostile newline, oversized value, and multi-round cap tests. Never emit private metadata. C. Failed-terminal usage overflow inside recovery catch: contain merge; preserve prior safe cumulative usage; invoke failRecall fixed envelope and unsuccessful accounting exactly once; no generic gateway escape/persistence. D. ...
<skill_content name="jj-guide"> # Skill: jj-guide # jj Guide for AI Agents Jujutsu (jj) is a Git-compatible VCS with mutable commits, automatic snapshotting, no staging area, and first-class conflicts. This skill teaches you how to use it safely from a non-interactive agent environment. If `.jj/` exists in the repo root, this is a jj repo. **Use `jj` commands, not `git`.** In a colocated repo (`.jj/` *and* `.git/`), git tools can read the state, but mutations should go through `jj` so the operation log stays consistent. ## Critical Rules - **NEVER** use interactive flags (`-i`, `--interactive`). TUI prompts hang in agent environments. This applies to `jj split -i`, `jj squash -i`, `jj commit -i`, `jj resolve`, `jj diffedit`, etc. - **ALWAYS** pass `-m "msg"` when describing/committing. Without `-m`, an editor opens and hangs. - **VERIFY** mutations with `jj st` and `jj log` after `squash`, `abandon`, `rebase`, `restore`, `commit`. ...
<path>/home/byk/Code/opencode-lore-recall-recovery</path> <type>directory</type> <entries> .craft.yml .git .github/ .gitignore .jj/ .lore.md .oxfmtrc.json .oxlintrc.json AGENTS.md CHANGELOG.md codecov.yml LICENSE node_modules/ package.json packages/ patches/ pnpm-lock.yaml pnpm-workspace.yaml quality/ README.md scripts/ stryker.config.mjs supabase/ tsconfig.base.json tsconfig.json vitest.config.ts vitest.evals.config.ts vitest.mutation.config.ts (28 entries) </entries>
<path>/home/byk/Code/opencode-lore-recall-recovery/AGENTS.md</path> <type>file</type> <content> 1: <!-- This section is maintained by the coding agent via lore (https://github.com/BYK/loreai) --> 2: ## Long-term Knowledge 3: 4: For long-term knowledge entries managed by [lore](https://github.com/BYK/loreai) (gotchas, patterns, decisions, architecture), see [`.lore.md`](.lore.md) in the project root. 5: <!-- End lore-managed section --> 6: 7: ## Review 8: 9: See [`quality/REVIEW.md`](quality/REVIEW.md) for the review heuristics and workflow — regression-test discipline, adversarial-order state setup, fan-out registry coverage, recurring bug-class batteries, and the two-reviewer rule. Every non-trivial PR requires an adversarial correctness review before merge. 10: 11: # Project Overview 12: 13: Lore is a **three-tier memory architecture** for AI coding agents. ...
<path>/home/byk/Code/opencode-lore-recall-recovery/quality/REVIEW.md</path> <type>file</type> <content> 1: # Review Heuristics — Lore 2: 3: This document codifies the review bar for pull requests. It exists because 4: adversarial review has repeatedly caught correctness bugs that a green test 5: suite missed — skip/early-return branches, lifecycle sequences, and fan-out 6: registry gaps. Institutionalizing these patterns so they compound. 7: 8: The review workflow (§6) is referenced from AGENTS.md. 9: 10: --- 11: 12: ## 1. Regression-test discipline 13: 14: Every adversarial-review finding that surfaces a defect **must** land a 15: deterministic regression test in the same PR. The test: 16: - fails on the base branch (proving it reproduces the bug), 17: - passes on the fix (proving the fix works), 18: - drives the *real* precondition, not an artificially-constructed state (e.g. ...
<path>/home/byk/Code/opencode-lore-recall-recovery/package.json</path>
<type>file</type>
<content>
1: {
2: "name": "lore-monorepo",
3: "private": true,
4: "type": "module",
5: "license": "FSL-1.1-Apache-2.0",
6: "description": "Monorepo root for Lore — three-tier memory architecture",
7: "main": "./packages/opencode/src/index.ts",
8: "exports": {
9: ".": "./packages/opencode/src/index.ts"
10: },
11: "packageManager": "pnpm@10.28.0",
12: "volta": {
13: "node": "24.16.0",
14: "pnpm": "10.28.0"
15: },
16: "pnpm": {
17: "onlyBuiltDependencies": [
18: "esbuild",
19: "onnxruntime-node",
20: "sharp"
21: ],
22: "overrides": {
23: "basic-ftp": "5.3.1",
24: "ip-address": "10.2.0",
25: "fast-xml-parser": "5.7.3",
26: "@huggingface/hub": "2.11.0",
27: "onnxruntime-node": "1.27.0",
28: "yaml": ">=2.8.3",
29: "esbuild": ">=0.28.1",
30: "@opentelemetry/core": ">=2.8.0",
31: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/package.json</path>
<type>file</type>
<content>
1: {
2: "name": "@loreai/gateway",
3: "version": "0.40.0",
4: "type": "module",
5: "license": "FSL-1.1-Apache-2.0",
6: "description": "Lore as a transparent LLM proxy — context management for any AI coding client",
7: "main": "./dist/index.cjs",
8: "types": "./dist/index.d.cts",
9: "exports": {
10: ".": {
11: "types": "./dist/index.d.cts",
12: "bun": "./dist/index.bun.js",
13: "require": "./dist/index.cjs",
14: "default": "./dist/index.cjs"
15: }
16: },
17: "bin": {
18: "lore": "./dist/bin.cjs",
19: "lore-gateway": "./dist/bin.cjs"
20: },
21: "scripts": {
22: "typecheck": "tsc --noEmit",
23: "build": "tsx script/build.ts",
24: "bundle": "tsx script/bundle.ts",
25: "build:binary": "tsx script/build.ts --binary",
26: "build:binary:sea": "tsx script/build-binary-sea.ts",
27: "start": ...
Working copy changes: M packages/gateway/test/codex-usage-metadata.test.ts M packages/gateway/test/openai-responses-recall-aware-stream.test.ts Working copy (@) : pqvltmkr ac80b275 fix(recall): harden quota metadata Parent commit (@-): spyyzvyy 3f3ae768 fix/recall-failure-synthesis | fix(recall): validate recovery metadata Warning: These bookmarks have conflicts: fix/responses-private-references Hint: Use `jj bookmark list` to see details. Use `jj bookmark set <name> -r <rev>` to resolve. pqvltmkrxpyn ac80b275f287 fix(recall): harden quota metadata spyyzvyyyqxs 3f3ae768fde5 fix(recall): validate recovery metadata mmmktlwmwuts b61b086186cb fix(recall): harden failure synthesis M packages/gateway/test/codex-usage-metadata.test.ts M packages/gateway/test/openai-responses-recall-aware-stream.test.ts
Resume the same implementation session; previous warning-only output is not evidence. Preserve all code/tests/results already produced. Recover context using lore recall --project /home/byk/Code/opencode-lore --scope project --limit 30 recovery envelope quota executeRecall fixed logging guard discrimination or local GET /ui/search. Complete the original recovery code task in /home/byk/Code/opencode-lore-recall-recovery. Do not commit/rebase/bookmark/server/production DB. Return substantive fail-first, fixed, mutant, test/typecheck evidence, changed files/hash/status and residual risks. Never warning-only/empty.
[lore]sqlite-vec: native vector search enabled (v0.1.9, /tmp/lore-native/pid-582218/vec0.so) ## Recall Results Found 90 results, showing top 15 (score range: 0.066–0.047). ### Strong Matches #### Knowledge - **Recovery accounting transaction** (11h ago): Chose request-owned cumulative usage and ordered Codex quota accumulators over mutating each response because failed-continuation metadata must survive recovery without escaping the rollback boundary. Merge validated failed-terminal and recovered metadata inside the recovery `try`; overflow must synchronously route through fixed recall failure, preserving only previously validated accounting and never converting failure into completion. ↳ files: packages/gateway/src/pipeline.ts, packages/gateway/test/pipeline-streaming.test.ts, packages/gateway/test/recall-buffered-transaction.test.ts - **Recall continuation usability gate** (11h ago): Never execute recall or accept recovery from an incomplete principal or unusable continuation. ...
Working copy changes: M packages/gateway/test/codex-usage-metadata.test.ts M packages/gateway/test/openai-responses-recall-aware-stream.test.ts Working copy (@) : pqvltmkr ac80b275 fix(recall): harden quota metadata Parent commit (@-): spyyzvyy 3f3ae768 fix/recall-failure-synthesis | fix(recall): validate recovery metadata Warning: These bookmarks have conflicts: fix/responses-private-references Hint: Use `jj bookmark list` to see details. Use `jj bookmark set <name> -r <rev>` to resolve. pqvltmkrxpyn ac80b275f287 fix(recall): harden quota metadata spyyzvyyyqxs 3f3ae768fde5 fix(recall): validate recovery metadata mmmktlwmwuts b61b086186cb fix(recall): harden failure synthesis A packages/gateway/src/codex-rate-limits.ts M packages/gateway/src/pipeline.ts M packages/gateway/src/recall.ts M packages/gateway/src/stream/openai-responses.ts M packages/gateway/src/translate/openai-responses.ts M packages/gateway/test/codex-usage-metadata.test.ts M ...
Found 100 matches (more matches available)
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/recall.ts:
Line 490: // Corrupt blob — start empty (markers fall back to raw text; recoverable
Line 916: export function hasRecallToolUse(resp: GatewayResponse): boolean {
Line 1061: export async function executeRecall(
Line 1191: export function isUsableRecallContinuation(resp: GatewayResponse): boolean {
Line 1331: "Continue the user's task using the accepted recall results and the context already available. Give your best supported answer or use an available non-recall tool. Do not request recall and do not mention recovery or provider failures.";
Line 1333: /** Build the single request-local synthesis attempt after recall continuation failure. */
Line 1334: export function buildRecallRecoveryRequest(
Line 1350: if (!resultMessage) throw new Error("recall recovery result message missing");
Line 1768: /** Execute the one no-recall synthesis ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
7100: }
7101:
7102: // First non-thinking content block — inject warning before it
7103: const blockStart = JSON.stringify({
7104: type: "content_block_start",
7105: index: warningBlockIndex,
7106: content_block: { type: "text", text: "" },
7107: });
7108: const blockDelta = JSON.stringify({
7109: type: "content_block_delta",
7110: index: warningBlockIndex,
7111: delta: { type: "text_delta", text: warningText },
7112: });
7113: const blockStop = JSON.stringify({
7114: type: "content_block_stop",
7115: index: warningBlockIndex,
7116: });
7117: const warningSSE =
7118: ...
Found 26 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts:
Line 402: runRecallRecovery,
Line 3860: * rendered and the agent loop exits with no auto-recovery.
Line 7634: if (!isUsableRecallContinuation(continuationResp))
Line 11239: !isUsableRecallContinuation(
Line 15714: // A provisional request cannot use provider diagnostics for recovery, and
Line 15796: if (error.status === "incomplete" && !hasRecallToolUse(error.response)) {
Line 17702: // in-grace = info breadcrumb, recovery = info breadcrumb).
Line 18314: while (hasRecallToolUse(currentResp)) {
Line 18442: const recoveryRequestBase = currentModifiedReq;
Line 18444: const makeJSONRecallCtx = (recovery: boolean): RecallFollowUpCtx => ({
Line 18453: ...(recovery ? ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
18180: // OpenAI format: "maximum context length is 128000 tokens. However, your messages resulted in 135421 tokens"
18181: if (
18182: upstreamResponse.status === 400 &&
18183: (errorBody.includes("prompt is too long") ||
18184: errorBody.includes("context_length_exceeded") ||
18185: errorBody.includes("maximum context length"))
18186: ) {
18187: const anthropicMatch = errorBody.match(
18188: /prompt is too long: (\d+) tokens > (\d+) maximum/,
18189: );
18190: const openaiMatch =
18191: !anthropicMatch &&
18192: errorBody.match(/resulted in (\d+) tokens.*?(\d+) tokens/);
18193: const match = anthropicMatch || openaiMatch;
18194: // Default to 1.3 (maps to layer 3) when the format can't be parsed,
18195: // since an unparseable error suggests an unexpected situation ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/recall.ts</path>
<type>file</type>
<content>
880: req.messages,
881: req.messages.indexOf(msg),
882: recallMessagePrefix(msg, msg.content.indexOf(block)),
883: ))
884: ) {
885: activeKeys.add(match.key);
886: }
887: }
888: }
889:
890: // Remove entries not referenced by any current marker
891: let changed = false;
892: for (const key of store.keys()) {
893: if (!activeKeys.has(key)) {
894: store.delete(key);
895: changed = true;
896: }
897: }
898: return changed;
899: }
900:
901: // ---------------------------------------------------------------------------
902: // Detection helpers
903: // ---------------------------------------------------------------------------
904:
905: /** Find the recall tool_use block in a GatewayResponse, if any. ...
Found 100 matches (more matches available) /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/codex-rate-limits.ts: Line 93: export function appendCodexRateLimitEvent( Line 118: for (const value of values) appendCodexRateLimitEvent(result, value); /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/portfile.ts: Line 93: // Preserve the displaced unknown generation as a recovery artifact. /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/llm-adapter.ts: Line 4153: // NOT `bodyErrCode ?? 404`. A normal successful completion has Line 4772: // worker call against that sibling is the recovery probe. /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts: Line 221: appendCodexRateLimitEvent, Line 3860: * rendered and the agent loop exits with no auto-recovery. Line 10265: ? appendCodexRateLimitEvent( Line 10873: ? ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
11720: continueAfterPrincipalTransport
11721: ? "response.incomplete"
11722: : "response.failed",
11723: JSON.stringify({
11724: type: continueAfterPrincipalTransport
11725: ? "response.incomplete"
11726: : "response.failed",
11727: response: {
11728: id: state.id || "resp_error",
11729: object: "response",
11730: created_at: Math.floor(Date.now() / 1000),
11731: model: state.model,
11732: status: continueAfterPrincipalTransport
11733: ? ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
12100: }
12101: }
12102:
12103: function assertValidNonStreamCompletion(
12104: json: Record<string, unknown>,
12105: protocol: "anthropic" | "openai" | "openai-responses" | "vertex" | "gemini",
12106: ): void {
12107: if (json.error !== undefined && json.error !== null) {
12108: throw new Error("upstream response contained an error");
12109: }
12110:
12111: if (protocol === "openai") {
12112: const choices = json.choices;
12113: const first = Array.isArray(choices) ? choices[0] : undefined;
12114: const finishReason =
12115: first && typeof first === "object" && !Array.isArray(first)
12116: ? (first as Record<string, unknown>).finish_reason
12117: : undefined;
12118: const message =
12119: first && typeof first === "object" && !Array.isArray(first)
12120: ? (first as Record<string, ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
12340: typeof blockReason === "string"
12341: ) {
12342: throw new Error("upstream Gemini request did not complete");
12343: }
12344: return;
12345: }
12346:
12347: if (
12348: json.type !== "message" ||
12349: json.role !== "assistant" ||
12350: typeof json.id !== "string" ||
12351: !json.id ||
12352: typeof json.model !== "string" ||
12353: !json.model ||
12354: !Array.isArray(json.content) ||
12355: typeof json.stop_reason !== "string" ||
12356: !json.usage ||
12357: typeof json.usage !== "object" ||
12358: Array.isArray(json.usage)
12359: ) {
12360: throw new Error("upstream Anthropic request did not complete");
12361: }
12362: validateAnthropicUsage(json.usage, {
12363: message: "malformed Anthropic usage",
12364: required: true,
12365: requireInput: true,
12366: ...
Found 100 matches (more matches available)
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/codex-usage-metadata.test.ts:
Line 39: id: "resp_quota",
Line 59: function quotaEvents(body: string): unknown[] {
Line 106: expect(quotaEvents(body)).toEqual(events);
Line 129: expect(quotaEvents(body)).toEqual([limits]);
Line 145: expect(quotaEvents(body)).toEqual([limits]);
Line 155: quotaEvents(await buildOpenAIResponsesResponse(first, true).text()),
Line 162: quotaEvents(await buildOpenAIResponsesResponse(second, true).text()),
Line 172: quotaEvents(
Line 178: test("true streaming forwards quota events without duplication", async () => {
Line 185: expect(quotaEvents(await response.text())).toEqual([limits]);
Line 189: "%s projection rebuilds quota metadata from the private allowlist",
Line 192: const hostile = {
Line 216: await ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall-buffered-transaction.test.ts</path>
<type>file</type>
<content>
1: import { afterEach, describe, expect, test, vi } from "vitest";
2: import { db, ltm, loadSessionTracking, temporal } from "@loreai/core";
3: import * as core from "@loreai/core";
4: import { loadConfig } from "../src/config";
5: import { clearAllCosts, getSessionCosts } from "../src/cost-tracker";
6: import {
7: accumulateNonStreamResponse,
8: buildStreamingResponse,
9: getActiveSessions,
10: handleRequest,
11: resetPipelineState,
12: setRecallPersistenceCommitObserverForTest,
13: setUpstreamInterceptor,
14: streamingPostResponsePendingForTest,
15: } from "../src/pipeline";
16: import type { GatewayRequest } from "../src/translate/types";
17: import { parseAnthropicResponseJSON } from "../src/translate/anthropic";
18: import {
19: _resetForTest as resetWorkerHealth,
20: _setNowForTest as setWorkerHealthTime,
21: ...
[lore]sqlite-vec: native vector search enabled (v0.1.9, /tmp/lore-native/pid-585742/vec0.so) ## Recall Results Found 90 results, showing top 15 (score range: 0.066–0.048). ### Strong Matches #### Knowledge - **Recovery accounting transaction** (11h ago): Chose request-owned cumulative usage and ordered Codex quota accumulators over mutating each response because failed-continuation metadata must survive recovery without escaping the rollback boundary. Merge validated failed-terminal and recovered metadata inside the recovery `try`; overflow must synchronously route through fixed recall failure, preserving only previously validated accounting and never converting failure into completion. ↳ files: packages/gateway/src/pipeline.ts, packages/gateway/test/pipeline-streaming.test.ts, packages/gateway/test/recall-buffered-transaction.test.ts - **Recovery systemTTL suppression** (11h ago): Trap: disabling conversation and tool caching looks sufficient for recall recovery, but provider ...
diff --git a/packages/gateway/test/codex-usage-metadata.test.ts b/packages/gateway/test/codex-usage-metadata.test.ts
index d3968c82b5..6e8be6a2e4 100644
--- a/packages/gateway/test/codex-usage-metadata.test.ts
+++ b/packages/gateway/test/codex-usage-metadata.test.ts
@@ -6,9 +6,10 @@
streamResponsesPassthrough,
} from "../src/stream/openai-responses";
import { buildOpenAIResponsesResponse } from "../src/translate/openai-responses";
-
-const MAX_CODEX_RATE_LIMIT_EVENTS = 64;
-const MAX_CODEX_RATE_LIMIT_BYTES = 16 * 1024;
+import {
+ MAX_CODEX_RATE_LIMIT_BYTES,
+ MAX_CODEX_RATE_LIMIT_EVENTS,
+} from "../src/codex-rate-limits";
const limits = {
type: "codex.rate_limits",
@@ -62,6 +63,37 @@
.map((frame) => JSON.parse(frame.split("\ndata: ")[1]));
}
+function maximalQuota(index: number): Record<string, unknown> {
+ const category = (prefix: string) =>
+ `${prefix}_${String(index).padStart(3, "0")}_${"x".repeat(64)}`.slice(
+ 0,
+ 64,
+ );
+ return {
+ ...
<skill_content name="jj-guide"> # Skill: jj-guide # jj Guide for AI Agents Jujutsu (jj) is a Git-compatible VCS with mutable commits, automatic snapshotting, no staging area, and first-class conflicts. This skill teaches you how to use it safely from a non-interactive agent environment. If `.jj/` exists in the repo root, this is a jj repo. **Use `jj` commands, not `git`.** In a colocated repo (`.jj/` *and* `.git/`), git tools can read the state, but mutations should go through `jj` so the operation log stays consistent. ## Critical Rules - **NEVER** use interactive flags (`-i`, `--interactive`). TUI prompts hang in agent environments. This applies to `jj split -i`, `jj squash -i`, `jj commit -i`, `jj resolve`, `jj diffedit`, etc. - **ALWAYS** pass `-m "msg"` when describing/committing. Without `-m`, an editor opens and hangs. - **VERIFY** mutations with `jj st` and `jj log` after `squash`, `abandon`, `rebase`, `restore`, `commit`. ...
Commit ID: ac80b275f2876c741a4225b9b8a28b9a055ff8ae
Change ID: pqvltmkrxpyntnnppwzkwwqzuuyumout
Author : Burak Yigit Kaya <ben@byk.im> (2026-09-16 20:04:46)
Committer: Burak Yigit Kaya <ben@byk.im> (2026-09-16 20:51:55)
fix(recall): harden quota metadata
packages/gateway/test/codex-usage-metadata.test.ts | 59 ++++++---
...est/openai-responses-recall-aware-stream.test.ts | 144 ++++++++++++++++++----
2 files changed, 166 insertions(+), 37 deletions(-)
Commit ID: ac80b275f2876c741a4225b9b8a28b9a055ff8ae
Change ID: pqvltmkrxpyntnnppwzkwwqzuuyumout
Author : Burak Yigit Kaya <ben@byk.im> (2026-09-16 20:04:46)
Committer: Burak Yigit Kaya <ben@byk.im> (2026-09-16 20:51:55)
fix(recall): harden quota metadata
diff --git a/packages/gateway/test/codex-usage-metadata.test.ts b/packages/gateway/test/codex-usage-metadata.test.ts
index d3968c82b5..6e8be6a2e4 100644
--- a/packages/gateway/test/codex-usage-metadata.test.ts
+++ b/packages/gateway/test/codex-usage-metadata.test.ts
@@ ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/codex-rate-limits.ts</path>
<type>file</type>
<content>
1: export const MAX_CODEX_RATE_LIMIT_EVENTS = 64;
2: export const MAX_CODEX_RATE_LIMIT_BYTES = 16 * 1024;
3:
4: const encoder = new TextEncoder();
5:
6: const CATEGORY = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
7: const DECIMAL = /^(?:0|[1-9][0-9]{0,23})(?:\.[0-9]{1,12})?$/;
8:
9: function isRecord(value: unknown): value is Record<string, unknown> {
10: return value !== null && typeof value === "object" && !Array.isArray(value);
11: }
12:
13: function category(value: unknown): string | undefined {
14: return typeof value === "string" && CATEGORY.test(value) ? value : undefined;
15: }
16:
17: function rateLimit(value: unknown): Record<string, unknown> | undefined {
18: if (!isRecord(value)) return undefined;
19: const result: Record<string, unknown> = {};
20: if (
21: typeof value.used_percent === "number" &&
22: ...
RUN v4.1.8 /home/byk/Code/opencode-lore-recall-recovery
❯ packages/gateway/test/openai-responses-recall-aware-stream.test.ts (209 tests | 3 failed) 1425ms
× suppresses malformed non-JSON principal Codex quota events 29ms
× suppresses malformed non-JSON continuation Codex quota events 16ms
× bounds Codex quota metadata by bytes across the principal and continuation 47ms
Test Files 1 failed | 1 passed (2)
Tests 3 failed | 225 passed (228)
Start at 22:14:10
Duration 15.12s (transform 11.76s, setup 1.89s, import 21.08s, tests 2.41s, environment 0ms)
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 3 ⎯⎯⎯⎯⎯⎯⎯
FAIL packages/gateway/test/openai-responses-recall-aware-stream.test.ts > streamResponsesRecallAware > suppresses malformed non-JSON principal Codex quota events
AssertionError: expected 'event: response.created\ndata: {"type…' not to contain 'private-principal-quota-sentinel'
- Expected
+ Received
- private-principal-quota-sentinel
+ event: response.created
+ data: ...
Found 7 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai-responses.ts:
Line 37: import { appendCodexRateLimitEvent } from "../codex-rate-limits";
Line 61: codexRateLimits?: Array<Record<string, unknown>>;
Line 618: case "codex.rate_limits": {
Line 619: return appendCodexRateLimitEvent((state.codexRateLimits ??= []), parsed);
Line 913: ...(state.codexRateLimits
Line 914: ? { codexRateLimits: state.codexRateLimits }
Line 2380: if (event !== "codex.rate_limits" || acceptedCodexRateLimit) {
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai-responses.ts</path> <type>file</type> <content> 1: /** 2: * OpenAI Responses API SSE stream accumulator. 3: * 4: * Parses upstream Responses API streaming events and accumulates the full 5: * response into a `GatewayResponse`. The Responses API uses a different 6: * SSE event lifecycle than Anthropic: 7: * 8: * response.created → response.in_progress → 9: * response.output_item.added → response.output_text.delta (repeated) → 10: * response.output_item.done → response.function_call_arguments.delta → 11: * response.function_call_arguments.done → 12: * response.completed 13: * 14: * Reuses `parseSSEStream` from the Anthropic stream module since the 15: * underlying SSE wire format is the same. ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai-responses.ts</path>
<type>file</type>
<content>
580: ) {
581: super(`upstream Responses request ended with status ${status}`);
582: this.name = "ResponsesTerminalError";
583: }
584: }
585:
586: export function makeResponsesAccState(): ResponsesAccState {
587: return {
588: id: "",
589: model: "",
590: stopReason: "end_turn",
591: usage: { inputTokens: 0, outputTokens: 0 },
592: items: new Map(),
593: rawItems: new Map(),
594: itemIndexById: new Map(),
595: callIndexById: new Map(),
596: effectiveToolIndexById: new Map(),
597: nextOutputIndex: 0,
598: activeTextItems: new Set(),
599: activeToolItems: new Set(),
600: unboundTextItems: new Set(),
601: unboundToolItems: new Set(),
602: textDoneItems: new Set(),
603: refusalDoneItems: new Set(),
604: argumentDoneItems: new Set(),
605: };
606: }
607:
608: /**
609: * ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai-responses.ts</path>
<type>file</type>
<content>
2300: (index) => !doneItems.has(index),
2301: );
2302: // ChatGPT Codex sometimes sends an empty terminal snapshot after completing every item.
2303: const emptyCodexSnapshotAfterCompletedItems =
2304: opts.validation === "codex" &&
2305: terminal.output.length === 0 &&
2306: !hasUnfinishedItems;
2307: if (
2308: (opts.validation === "codex" &&
2309: terminal.output.length === 0 &&
2310: hasUnfinishedItems) ||
2311: (!emptyCodexSnapshotAfterCompletedItems &&
2312: (snapshotIndices.size !== doneItems.size ||
2313: Array.from(doneItems).some(
2314: (index) => !snapshotIndices.has(index),
2315: )))
2316: ) {
2317: throw new ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai-responses.ts</path>
<type>file</type>
<content>
1700: }
1701: if (
1702: state.model &&
1703: response.model !== undefined &&
1704: response.model !== state.model
1705: ) {
1706: throw new Error("malformed Responses terminal event");
1707: }
1708: if (
1709: event === "response.created" &&
1710: response.status !== undefined &&
1711: response.status !== "in_progress" &&
1712: response.status !== "queued"
1713: ) {
1714: malformedResponsesEvent();
1715: }
1716: if (
1717: event === "response.in_progress" &&
1718: response.status !== undefined &&
1719: response.status !== "in_progress"
1720: ) {
1721: malformedResponsesEvent();
1722: }
1723: }
1724:
1725: switch (event) {
1726: case "response.output_item.added":
1727: case "response.output_item.done":
1728: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai-responses.ts</path>
<type>file</type>
<content>
1940: opts.onReader?.(reader);
1941:
1942: try {
1943: for await (const { event, data } of parseSSEStream(reader, {
1944: signal: opts.signal,
1945: inactivityMs: opts.inactivityMs,
1946: requireEventTerminator: opts.validation !== undefined,
1947: maxFrames: opts.validation
1948: ? (opts.maxFrames ?? DEFAULT_MAX_SSE_FRAMES)
1949: : opts.maxFrames,
1950: maxTotalBytes: opts.validation ? 4 * 1024 * 1024 : undefined,
1951: fatalUtf8: opts.validation !== undefined,
1952: })) {
1953: // Some Responses API implementations send untyped `data:` lines
1954: // without `event:` — skip those.
1955: if (!data || data === "[DONE]") continue;
1956:
1957: let parsed: Record<string, unknown>;
1958: try {
1959: parsed = JSON.parse(data) as Record<string, ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai-responses.ts</path>
<type>file</type>
<content>
2170: event === "response.function_call_arguments.done")
2171: ) {
2172: const outputIndex = parsed.output_index as number;
2173: if (state.argumentDoneItems.has(outputIndex)) {
2174: throw new Error("malformed Responses stream event");
2175: }
2176: if (event === "response.function_call_arguments.done") {
2177: state.argumentDoneItems.add(outputIndex);
2178: }
2179: }
2180: if (
2181: opts.validation &&
2182: event === "response.output_item.done" &&
2183: activeContentIndexByItem.has(parsed.output_index as number)
2184: ) {
2185: throw new Error("malformed Responses stream event");
2186: }
2187: const acceptedCodexRateLimit = applyResponsesEvent(state, event, parsed);
2188: if (event === ...
The server restarted. Resume your existing recovery implementation session in place, preserving all edits/tests/results. Continue the original blocker-fix assignment and return substantive fail-first/fixed/mutant evidence. Recover memory through lore recall --project /home/byk/Code/opencode-lore --scope project ... or local /ui/search if needed. Never warning-only. Do not touch server, production DB, bookmarks, or commits.
<path>/home/byk/Code/opencode-lore-recall-recovery/AGENTS.md</path> <type>file</type> <content> 1: <!-- This section is maintained by the coding agent via lore (https://github.com/BYK/loreai) --> 2: ## Long-term Knowledge 3: 4: For long-term knowledge entries managed by [lore](https://github.com/BYK/loreai) (gotchas, patterns, decisions, architecture), see [`.lore.md`](.lore.md) in the project root. 5: <!-- End lore-managed section --> 6: 7: ## Review 8: 9: See [`quality/REVIEW.md`](quality/REVIEW.md) for the review heuristics and workflow — regression-test discipline, adversarial-order state setup, fan-out registry coverage, recurring bug-class batteries, and the two-reviewer rule. Every non-trivial PR requires an adversarial correctness review before merge. 10: 11: # Project Overview 12: 13: Lore is a **three-tier memory architecture** for AI coding agents. ...
<path>/home/byk/Code/opencode-lore-recall-recovery/quality/REVIEW.md</path> <type>file</type> <content> 1: # Review Heuristics — Lore 2: 3: This document codifies the review bar for pull requests. It exists because 4: adversarial review has repeatedly caught correctness bugs that a green test 5: suite missed — skip/early-return branches, lifecycle sequences, and fan-out 6: registry gaps. Institutionalizing these patterns so they compound. 7: 8: The review workflow (§6) is referenced from AGENTS.md. 9: 10: --- 11: 12: ## 1. Regression-test discipline 13: 14: Every adversarial-review finding that surfaces a defect **must** land a 15: deterministic regression test in the same PR. The test: 16: - fails on the base branch (proving it reproduces the bug), 17: - passes on the fix (proving the fix works), 18: - drives the *real* precondition, not an artificially-constructed state (e.g. ...
<path>/home/byk/Code/opencode-lore-recall-recovery/package.json</path>
<type>file</type>
<content>
40: "postcss": ">=8.5.18"
41: },
42: "patchedDependencies": {
43: "@sentry/node@10.56.0": "patches/@sentry__node@10.56.0.patch"
44: }
45: },
46: "scripts": {
47: "typecheck": "pnpm -r run typecheck",
48: "test": "vitest run",
49: "pretest": "pnpm --filter @loreai/gateway run bundle",
50: "test:coverage": "pnpm --filter @loreai/gateway run bundle && vitest run --coverage",
51: "build": "pnpm -r run build",
52: "site:dev": "pnpm --filter '@loreai/website' dev",
53: "site:build": "pnpm --filter '@loreai/website' build",
54: "site:preview": "pnpm --filter '@loreai/website' preview",
55: "generate:docs": "tsx scripts/generate-config-docs.ts && tsx scripts/generate-env-docs.ts",
56: "check:patches": "tsx scripts/check-patches.ts",
57: "check:docs": "tsx scripts/generate-config-docs.ts --check && tsx ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path> <type>file</type> <content> 7780: throw error; 7781: } 7782: }, 7783: streamSignal, 7784: recallPersistence.rollback, 7785: ); 7786: } 7787: 7788: /** 7789: * True-streaming, recall-aware variant of `streamResponsesPassthrough` for the 7790: * OpenAI Responses API (codex/ChatGPT) — used when the request carries the 7791: * gateway-injected `recall` tool but the client speaks the Responses API. ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
10180: (parsed.item as Record<string, unknown>).name === RECALL_TOOL_NAME
10181: ) {
10182: recallDetected = true;
10183: }
10184: const normalizationState = normalizeCodexEvent(
10185: state,
10186: event,
10187: parsed,
10188: );
10189: validateResponseLifecycle(state, event, parsed);
10190: seedImplicitCodexItem(state, normalizationState, event, parsed);
10191:
10192: if (consumeReferenceEvent(state, referenceIndices, event, parsed)) {
10193: continue;
10194: }
10195:
10196: const outputIndex = outputIndexForEvent(
10197: event,
10198: parsed,
10199: state,
10200: (index, item) => {
10201: if (
10202: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
10350: }
10351: if (event === "response.output_item.done") {
10352: if (isRecallEvent) {
10353: collectCompletedRecall(
10354: state,
10355: outputIndex,
10356: parsedRecallInputs,
10357: pendingRecalls,
10358: );
10359: }
10360: }
10361: // Don't forward recall-item events to the client.
10362: continue;
10363: }
10364:
10365: // Terminal events: handle recall interception before forwarding.
10366: if (
10367: event === "response.completed" ||
10368: event === "response.done" ||
10369: event === "response.incomplete" ||
10370: event === "response.failed"
10371: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
9950: resumeDemand?.();
9951: resumeDemand = undefined;
9952: if (keepaliveTimer) clearTimeout(keepaliveTimer);
9953: keepaliveTimer = null;
9954: if (activeReader) cancelAndReleaseReader(activeReader, signal.reason);
9955: else
9956: void currentPrincipalResponse.body?.cancel(signal.reason).catch(() => {});
9957: };
9958: signal.addEventListener("abort", onStreamAbort, { once: true });
9959: if (signal.aborted) onStreamAbort();
9960: const stream = new ReadableStream<Uint8Array>({
9961: start(controller) {
9962: void (async () => {
9963: const waitForDemand = async (): Promise<void> => {
9964: while (
9965: !cancelled &&
9966: !signal.aborted &&
9967: (controller.desiredSize ?? ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
10680: for (
10681: let index = heldContinuationEvents.length - 1;
10682: index >= 0;
10683: index--
10684: ) {
10685: if (
10686: heldContinuationEvents[index].candidateIndex ===
10687: outputIndex
10688: ) {
10689: if (!heldContinuationEvents[index].transactional) {
10690: deferredContinuationBytes -=
10691: heldContinuationEvents[index].chunk.byteLength;
10692: }
10693: heldContinuationEvents.splice(index, 1);
10694: }
10695: }
10696: ...
Found 24 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts:
Line 7182: ? mergeRecallUsage(
Line 7623: cumulativeUsage = mergeRecallUsage(
Line 7900: const publicCodexRateLimits: Array<Record<string, unknown>> = [];
Line 8034: let transactionProviderUsage: GatewayUsage = { ...ZERO_USAGE };
Line 10266: publicCodexRateLimits,
Line 10513: transactionProviderUsage = { ...ZERO_USAGE };
Line 10624: let continuationRetryBaseline = {
Line 10874: publicCodexRateLimits,
Line 11115: continuationRetryBaseline.transactionalEvents;
Line 11117: continuationRetryBaseline.transactionalBytes;
Line 11119: continuationRetryBaseline.retainedStateBytes;
Line 11121: continuationRetryBaseline.hiddenRecallBytes;
...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
10590: deferredEvents.length = 0;
10591: deferredBytes = 0;
10592:
10593: if (
10594: !otherToolSeen &&
10595: recall === pendingRecalls[pendingRecalls.length - 1]
10596: ) {
10597: // Recall-only: run the streaming follow-up and pipe the
10598: // continuation inline before the final completion.
10599: try {
10600: continuationAttempted = true;
10601: continuationFailureCategory = "follow_up_setup";
10602: signal.throwIfAborted();
10603: let follow = await settleFollowUp({
10604: finalRecallRound: recallBudget.mustFinalizeNext(),
10605: anchorText: executed.anchorText,
10606: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
11150: continuationFollowUpInput,
11151: );
11152: continuationFailureCategory = "follow_up_protocol";
11153: continue;
11154: }
11155: const mergeContinuation = (): void => {
11156: for (const item of contState.rawItems.values()) {
11157: const itemIdentities = [item.id, item.call_id].filter(
11158: (value): value is string =>
11159: typeof value === "string" && value.length > 0,
11160: );
11161: for (const existing of state.items.values()) {
11162: const existingIdentities = new Set(
11163: [
11164: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
11400: }
11401: }
11402: }
11403:
11404: // Rebuild the terminal response.completed reflecting only the
11405: // continuation (recall-only) or the client-owned tools (mixed).
11406: const finalResp = finalizeResponsesAcc(state);
11407: let anchorIndex = 0;
11408: const visibleResp = {
11409: ...finalResp,
11410: content: finalResp.content.map((block) => {
11411: if (block.type !== "tool_use" || block.name !== "recall") {
11412: return block;
11413: }
11414: return {
11415: type: "text" as const,
11416: text: anchorTexts[anchorIndex++] ?? ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/openai-responses-recall-aware-stream.test.ts</path>
<type>file</type>
<content>
130: item: {
131: type: "function_call",
132: id: itemId,
133: call_id: callId,
134: name: "read",
135: arguments: argumentsText,
136: status: "completed",
137: },
138: });
139:
140: const completed = (id: string, usage?: unknown) =>
141: sseEvent("response.completed", {
142: response: {
143: id,
144: model: "gpt-5.6-terra",
145: status: "completed",
146: ...(usage ? { usage } : {}),
147: },
148: });
149:
150: const incomplete = (id: string) =>
151: sseEvent("response.incomplete", {
152: response: {
153: id,
154: model: "gpt-5.6-terra",
155: status: "incomplete",
156: },
157: });
158:
159: const doneIncomplete = (id: string) =>
160: sseEvent("response.done", {
161: response: {
162: id,
163: model: ...
Found 4 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/helpers/replay.ts:
Line 51: function sseEvent(type: string, data: unknown): string {
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/openai-responses-recall-aware-stream.test.ts:
Line 43: function sseEvent(event: string, data: unknown): string {
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall-marker-split.e2e.test.ts:
Line 39: function sseEvent(event: string, data: unknown): string {
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall-codex-stream.test.ts:
Line 35: function sseEvent(event: string, data: unknown): string {
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/openai-responses-recall-aware-stream.test.ts</path>
<type>file</type>
<content>
1: /**
2: * Unit tests for `streamResponsesRecallAware` — the true-streaming,
3: * recall-aware OpenAI Responses (codex/ChatGPT) streamer.
4: *
5: * Regression for the "Provider response headers timed out after 10000ms" issue:
6: * the buffered `accumulateResponsesSSEStream` path withholds ALL client bytes
7: * until the entire (slow, reasoning-heavy) upstream completes, so opencode's
8: * 10s `ProviderHeaderTimeoutError` fired on ChatGPT sessions. This streamer
9: * forwards events live while transparently intercepting a `recall`
10: * function_call (emit marker, run follow-up, rebuild the terminal
11: * `response.completed`).
12: */
13: import { log } from "@loreai/core";
14: import { afterEach, describe, test, expect } from "vitest";
15: import { streamResponsesRecallAware } from "../src/pipeline";
16: import {
17: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/openai-responses-recall-aware-stream.test.ts</path>
<type>file</type>
<content>
330: expect(out).not.toContain("event: codex.rate_limits");
331: });
332:
333: test("bounds Codex quota metadata by bytes across the principal and continuation", async () => {
334: const quotas = Array.from(
335: { length: MAX_CODEX_RATE_LIMIT_EVENTS },
336: (_, index) => maximalQuotaEvent(index),
337: );
338: const canonical = quotas.map((event) =>
339: JSON.parse(event.split("\ndata: ")[1]),
340: );
341: const allBytes = canonical.reduce(
342: (total, event) =>
343: total + new TextEncoder().encode(JSON.stringify(event)).byteLength,
344: 0,
345: );
346: expect(allBytes).toBeGreaterThan(MAX_CODEX_RATE_LIMIT_BYTES);
347: const principalQuotas = quotas.slice(0, 20);
348: const continuationQuotas = quotas.slice(20);
349: let recalls = 0;
350: let ...
Found 2 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts:
Line 9606: const sequenceChunk = (chunk: Uint8Array): Uint8Array => {
Line 9627: JSON.stringify({ ...parsed, sequence_number: nextSequenceNumber++ }),
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
9570: if (output.length === response.output.length) return parsed;
9571: return { ...parsed, response: { ...response, output } };
9572: };
9573: const reserveSyntheticIdentity = (
9574: syntheticId: string,
9575: states: readonly ResponsesAccState[],
9576: ): void => {
9577: if (
9578: syntheticIdentities.has(syntheticId) ||
9579: referenceIdentities.has(syntheticId) ||
9580: outputIdentities.has(syntheticId) ||
9581: states.some(
9582: (acc) =>
9583: [...acc.items.values()].some(
9584: (item) =>
9585: item.id === syntheticId ||
9586: (item.type === "tool_use" && item.callId === syntheticId),
9587: ) ||
9588: [...acc.rawItems.values()].some(
9589: (item) => item.id === syntheticId || item.call_id === syntheticId,
9590: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/openai-responses-recall-aware-stream.test.ts</path>
<type>file</type>
<content>
210: });
211: }
212:
213: const textItem = (
214: outputIndex: number,
215: text: string,
216: itemId = `msg_${outputIndex}`,
217: ) =>
218: sseEvent("response.output_item.added", {
219: output_index: outputIndex,
220: item: { type: "message", id: itemId, role: "assistant" },
221: }) +
222: sseEvent("response.output_text.delta", {
223: output_index: outputIndex,
224: item_id: itemId,
225: content_index: 0,
226: delta: text,
227: }) +
228: sseEvent("response.output_text.done", {
229: output_index: outputIndex,
230: item_id: itemId,
231: content_index: 0,
232: text,
233: }) +
234: sseEvent("response.output_item.done", {
235: output_index: outputIndex,
236: item: {
237: type: "message",
238: id: itemId,
239: role: "assistant",
240: status: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/codex-usage-metadata.test.ts</path>
<type>file</type>
<content>
170: );
171: expect(
172: quotaEvents(
173: await buildOpenAIResponsesResponse(afterTerminal, true).text(),
174: ),
175: ).toEqual([]);
176: });
177:
178: test("true streaming forwards quota events without duplication", async () => {
179: const response = streamResponsesPassthrough(
180: upstream([limits]),
181: () => {},
182: undefined,
183: "codex",
184: );
185: expect(quotaEvents(await response.text())).toEqual([limits]);
186: });
187:
188: test.each(["buffered", "passthrough"] as const)(
189: "%s projection rebuilds quota metadata from the private allowlist",
190: async (mode) => {
191: const sentinel = "private diagnostic\nforged log line";
192: const hostile = {
193: ...limits,
194: plan_type: sentinel,
195: ...
Success. Updated the following files: M ../opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts
RUN v4.1.8 /home/byk/Code/opencode-lore-recall-recovery
Test Files 2 passed (2)
Tests 228 passed (228)
Start at 22:53:58
Duration 31.78s (transform 23.07s, setup 6.72s, import 38.01s, tests 8.09s, environment 0ms)
Found 100 matches (more matches available)
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/worker-effort.test.ts:
Line 7: * max_tokens above the budget and drops temperature (both Anthropic rules).
Line 127: test("high enables thinking with a budget and raises max_tokens above it", async () => {
Line 133: // max_tokens must exceed the thinking budget (Anthropic rule); the tiny 256
Line 135: expect(b.max_tokens as number).toBeGreaterThan(thinking.budget_tokens);
Line 144: test("off keeps the disabled-thinking suppression and preserves temperature/max_tokens", async () => {
Line 149: expect(b.max_tokens).toBe(256);
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/context-capability-note.test.ts:
Line 33: max_tokens: 1024,
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/content-passthrough.test.ts:
Line 62: max_tokens: 1024,
Line 106: max_tokens: 1024,
Line 160: ...
Found 3 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall-buffered-transaction.test.ts:
Line 700: test("a cancelled in-flight continuation discards staged recall effects", async () => {
Line 710: caller.abort(new DOMException("client cancelled", "AbortError"));
Line 1483: else caller.abort(new DOMException("client cancelled", "AbortError"));
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall-buffered-transaction.test.ts</path>
<type>file</type>
<content>
1350: const id = knowledge();
1351: const alias = crypto.randomUUID();
1352: const req = request(client, alias);
1353: req.stream = true;
1354: req.rawHeaders["x-lore-provider"] = "anthropic";
1355: req.rawHeaders["x-lore-upstream-url"] = "https://api.anthropic.com";
1356: req.rawHeaders["x-api-key"] = "test-key";
1357: delete req.rawHeaders.authorization;
1358: setUpstreamInterceptor(async () =>
1359: providerResponse("anthropic", 1, "mixed", true),
1360: );
1361: const response = await handleRequest(req, config());
1362: const reader = response.body!.getReader();
1363: let wire = "";
1364: for (;;) {
1365: const { done, value } = await reader.read();
1366: expect(done).toBe(false);
1367: wire ...
Found 36 matches /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall-buffered-transaction.test.ts: Line 597: if (calls < FINAL_RECALL_CALL) Line 599: if (calls > FINAL_RECALL_CALL) Line 650: expect(calls).toBe(FINAL_RECALL_CALL + 1); Line 654: TEST_RECALL_EXECUTION_CAP * 3 + (usageKind === "valid" ? 1000 : 0), Line 656: TEST_RECALL_EXECUTION_CAP * 2 + (usageKind === "valid" ? 100 : 0), Line 850: calls === FINAL_RECALL_CALL ? outcome : "recall", Line 861: outcome === "answer" ? ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall-buffered-transaction.test.ts</path>
<type>file</type>
<content>
820: expect(response.status).toBe(mode === "failure" ? 502 : 200);
821: await response.text();
822: }
823: await settled();
824: expect(state.recallStore).toEqual(mapBefore);
825: expect(loadSessionTracking(state.sessionID)?.recallStore).toBe(
826: existingTracking,
827: );
828: expect(
829: db()
830: .query("SELECT SUM(hit_count) AS count FROM knowledge_transfers")
831: .get(),
832: ).toEqual(transfersBefore);
833: },
834: );
835:
836: test.each(["answer", "invalid"] as const)(
837: "buffers Responses upstream for a streaming Chat client: %s",
838: async (outcome) => {
839: const id = knowledge();
840: prepareProductiveRecallSources();
841: const alias = crypto.randomUUID();
842: const req = request("openai", alias);
843: req.stream = true;
844: ...
No files found
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/anthropic.ts
Found 100 matches (more matches available)
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/pipeline-streaming.test.ts:
Line 306: describe("principal Responses transport recovery", () => {
Line 353: it("rejects per-field and cross-component safe-integer overflow", () => {
Line 359: ).toThrow("recall usage token overflow");
Line 365: ).toThrow("recall usage token overflow");
Line 527: "overflow",
Line 528: "failed-overflow",
Line 532: "%s recovery makes exactly one no-recall synthesis request after a failed JSON continuation",
Line 533: async (recoveryOutcome) => {
Line 562: recoveryOutcome === "repeated-recall" ? ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/pipeline-streaming.test.ts</path>
<type>file</type>
<content>
460: },
461: ],
462: usage: { input_tokens: 1, output_tokens: 1 },
463: }),
464: { headers: { "content-type": "application/json" } },
465: );
466: }
467: return new Response(
468: JSON.stringify({
469: id: "resp_no_store_final",
470: object: "response",
471: created_at: 0,
472: model: "gpt-5.6-sol",
473: status: "completed",
474: output: [
475: {
476: type: "message",
477: id: "msg_no_store_final",
478: role: "assistant",
479: status: "completed",
480: content: [
481: {
482: type: "output_text",
483: text: "private answer",
484: annotations: [],
485: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/pipeline-streaming.test.ts</path>
<type>file</type>
<content>
1: /**
2: * Pipeline streaming-response coverage (Anthropic conversation turns).
3: *
4: * The harness's replay interceptor now emits Anthropic SSE for streaming
5: * requests (see test/helpers/replay.ts), so a `stream: true` turn exercises
6: * the streaming path end-to-end: buildStreamingResponse parses the upstream
7: * SSE, forwards it to the client, and accumulates in parallel for
8: * postResponse storage.
9: */
10: import { describe, it, expect, afterEach, vi } from "vitest";
11: import {
12: distillation,
13: db,
14: getDailyCostForDay,
15: ltm,
16: loadSessionTracking,
17: saveSessionTracking,
18: temporal,
19: } from "@loreai/core";
20: import * as Sentry from "@sentry/bun";
21: import type { Harness } from "./helpers/harness";
22: import { createHarness } from "./helpers/harness";
23: import type { FixtureEntry } from ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/pipeline-streaming.test.ts</path>
<type>file</type>
<content>
100: return config;
101: }
102:
103: function makeStreamBody(userMessage: string): Record<string, unknown> {
104: return {
105: model: DEFAULT_MODEL,
106: max_tokens: 1024,
107: stream: true,
108: system: DEFAULT_SYSTEM,
109: messages: [{ role: "user", content: userMessage }],
110: tools: STANDARD_TOOLS,
111: };
112: }
113:
114: function makeMetaStreamBody(userMessage: string): Record<string, unknown> {
115: return {
116: model: DEFAULT_MODEL,
117: max_tokens: 32,
118: stream: true,
119: messages: [{ role: "user", content: userMessage }],
120: };
121: }
122:
123: function validAnthropicSSE(text = "meta ok"): Response {
124: const event = (type: string, data: Record<string, unknown>) =>
125: `event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`;
126: return new Response(
127: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/pipeline-streaming.test.ts</path>
<type>file</type>
<content>
280: tools?: GatewayRequest["tools"];
281: }): GatewayRequest {
282: return {
283: protocol: "openai-responses",
284: model: "gpt-5.6-sol",
285: system: "You are a coding agent.",
286: messages: input.messages ?? [
287: { role: "user", content: [{ type: "text", text: "continue" }] },
288: ],
289: tools: input.tools ?? [
290: { name: "read", description: "Read a file", inputSchema: {} },
291: ],
292: stream: true,
293: maxTokens: 1024,
294: metadata: {},
295: rawHeaders: {
296: authorization: "Bearer test-key",
297: "x-lore-agent": "coder",
298: "x-lore-project": process.cwd(),
299: "x-lore-provider": "openai",
300: "x-lore-upstream-url": "https://api.openai.com/v1",
301: ...input.sessionHeaders,
302: },
303: };
304: }
305:
306: describe("principal ...
Found 5 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/pipeline-streaming.test.ts:
Line 38: getDegradationWarning: vi.fn(actual.getDegradationWarning),
Line 78: import { getDegradationWarning } from "../src/worker-health";
Line 947: afterEach(() => vi.mocked(getDegradationWarning).mockReset());
Line 1336: vi.mocked(getDegradationWarning).mockReturnValueOnce("workers degraded");
Line 1398: vi.mocked(getDegradationWarning).mockReturnValueOnce("workers degraded");
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/pipeline-streaming.test.ts</path>
<type>file</type>
<content>
920: let cancelled = false;
921: const response = new Response(
922: new ReadableStream<Uint8Array>({
923: start(controller) {
924: controller.enqueue(new TextEncoder().encode(wire));
925: },
926: pull() {
927: return new Promise(() => {});
928: },
929: cancel() {
930: cancelled = true;
931: return new Promise<void>(() => {});
932: },
933: }),
934: { headers: { "content-type": "text/event-stream" } },
935: );
936: return { response, cancelled: () => cancelled };
937: }
938:
939: async function readSSE(resp: Response): Promise<string> {
940: return resp.text();
941: }
942:
943: describe("Pipeline — streaming responses", () => {
944: let harness: Harness;
945:
946: afterEach(() => harness?.teardown());
947: afterEach(() => ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall.test.ts</path>
<type>file</type>
<content>
190: const props = schema.properties as Record<string, unknown>;
191: expect(props).toHaveProperty("query");
192: expect(props).toHaveProperty("scope");
193: expect(props).toHaveProperty("id");
194: expect(props).toHaveProperty("ids");
195: expect(props).toHaveProperty("detailOffset");
196: expect(props).toHaveProperty("detailLimit");
197: expect(schema.anyOf).toEqual([
198: { required: ["query"] },
199: { required: ["id"] },
200: { required: ["ids"] },
201: ]);
202: expect(schema).not.toHaveProperty("required");
203: expect(schema.additionalProperties).toBe(false);
204: });
205:
206: test("instructs resolving named references via recall before exploring", () => {
207: // Recall-first directive: the agent must resolve a named project/repo/
208: // person/service reference against memory ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall.test.ts</path>
<type>file</type>
<content>
1980: const response = new Response(
1981: new ReadableStream<Uint8Array>({
1982: start(streamController) {
1983: streamController.enqueue(new TextEncoder().encode("data: test\n\n"));
1984: },
1985: cancel() {
1986: bodyCancelled = true;
1987: },
1988: }),
1989: { headers: { "content-type": "text/event-stream" } },
1990: );
1991: const pending = runRecallFollowUpStreamAccumulated(
1992: {
1993: forward: async () => ({ response, effectiveProtocol: "openai" }),
1994: parseJSON: () => Promise.reject(new Error("should not be called")),
1995: parseSSE: () => {
1996: markParsing();
1997: return new Promise(() => {});
1998: },
1999: },
2000: makeRequest(),
2001: resp,
2002: "recall results",
2003: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/recall.ts</path> <type>file</type> <content> 1: /** 2: * Gateway recall interception — transparent memory search for any client. 3: * 4: * Uses a unified "Marker and Expand" strategy: 5: * 6: * 1. **On response (to client):** The recall `tool_use` block is replaced 7: * with a text marker. Responses clients receive an invisible unique anchor; 8: * other clients receive status text plus the same anchor. The recall result 9: * is stored in session state under that unique key. 10: * 11: * 2. **On request (from client):** Marker text blocks in the conversation 12: * are expanded back into the original `tool_use` + `tool_result` pairs 13: * before forwarding upstream. 14: * 15: * For recall-only responses, a follow-up call is still made internally 16: * so the model can continue in the same HTTP response (seamless UX). ...
Found 1 matches /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai-responses.ts: Line 255: export function assertSuccessfulResponsesCompletion(
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai-responses.ts</path>
<type>file</type>
<content>
220: },
221: };
222: for (const type of [
223: "function_call_output",
224: "computer_call_output",
225: "computer_tool_call_output",
226: "local_shell_call_output",
227: "shell_call_output",
228: "custom_tool_call_output",
229: "program_output",
230: "tool_search_output",
231: ]) {
232: OUTPUT_ITEM_STATUSES_BY_TYPE[type] = {
233: added: COMPLETED,
234: done: COMPLETED,
235: terminal: COMPLETED,
236: };
237: }
238:
239: export function isSupportedResponsesOutputItemType(
240: type: unknown,
241: ): type is string {
242: return typeof type === "string" && RESPONSES_OUTPUT_ITEM_TYPES.has(type);
243: }
244:
245: export function isValidResponsesOutputItemStatus(
246: type: unknown,
247: status: unknown,
248: phase: OutputItemStatusPhase,
249: ): boolean {
250: if (status === undefined) return true;
251: if ...
Found 8 matches /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts: Line 239: validateOpenAIUsage, Line 12524: const usage = validateOpenAIUsage( Line 12528: const promptTokensDetails = usage?.prompt_tokens_details as Line 12538: // prompt_tokens is inclusive of cache reads/writes; convert to the Line 12541: usage?.prompt_tokens as number | undefined, Line 12548: // prompt_tokens_details.cache_write_tokens. OpenAI proper doesn't report Line 12636: // to `prompt_tokens_details` (Chat Completions shape) for resilience across Line 12639: usage?.prompt_tokens_details) as Record<string, number> | undefined;
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
12500: }
12501: }
12502: const id = asString(tc.id);
12503: if (!id || toolIdentities.has(id)) {
12504: throw new Error("malformed OpenAI response tool identity");
12505: }
12506: toolIdentities.add(id);
12507: content.push({
12508: type: "tool_use",
12509: id,
12510: name: asString(fn?.name),
12511: input,
12512: });
12513: }
12514: }
12515: }
12516:
12517: // Map OpenAI finish_reason to gateway stop reason
12518: const finishReason = firstChoice?.finish_reason as string | undefined;
12519: let stopReason = "end_turn";
12520: if (finishReason === "stop") stopReason = "end_turn";
12521: else if (finishReason === "length") stopReason = "max_tokens";
12522: else if (finishReason === "tool_calls") stopReason = "tool_use";
12523: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/usage-validation.ts</path>
<type>file</type>
<content>
1: /** Runtime validation for provider-reported token usage. */
2:
3: export function isRecord(value: unknown): value is Record<string, unknown> {
4: return !!value && typeof value === "object" && !Array.isArray(value);
5: }
6:
7: function invalid(message: string): never {
8: throw new Error(message);
9: }
10:
11: function tokenCount(
12: value: unknown,
13: message: string,
14: allowNull = false,
15: ): number | undefined {
16: if (value === undefined || (allowNull && value === null)) return undefined;
17: if (!Number.isSafeInteger(value) || (value as number) < 0) invalid(message);
18: return value as number;
19: }
20:
21: /** Add validated token counts without allowing a safe-integer overflow. ...
Found 11 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/core/src/log.ts:
Line 13: * An optional {@link LogSink} can be registered via {@link registerSink}.
Line 51: error(message: string, attrs?: Record<string, unknown>): void;
Line 52: captureException(err: unknown): void;
Line 66: export function registerSink(s: LogSink): void {
Line 467: console.error("[lore]", ...safeArgs(args));
Line 476: console.error("[lore] WARN:", ...safeArgs(args));
Line 491: if (!readStderrSilenced()) console.error("[lore]", ...safeArgs(args));
Line 497: export function error(...args: unknown[]): void {
Line 499: if (!readStderrSilenced()) console.error("[lore]", ...safeArgs(args));
Line 500: sink?.error(msg);
Line 504: if (err) sink?.captureException(sanitizedError(err));
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/core/src/log.ts</path>
<type>file</type>
<content>
430: }
431:
432: /** Append a single log line to the persistent log file. */
433: function writeToFile(level: string, message: string): void {
434: const path = logFilePath();
435: if (!path) return;
436:
437: // Periodic rotation check
438: if (++writeCount % ROTATION_CHECK_INTERVAL === 0) {
439: maybeRotate();
440: }
441:
442: const ts = new Date().toISOString();
443: const tag = level.toUpperCase().padEnd(5);
444: // Flatten multiline messages for clean tail -f output
445: const flat = message.replace(/\n/g, "\\n");
446: const line = `${ts} [${tag}] ${flat}\n`;
447:
448: let fd: number | undefined;
449: try {
450: fd = openLogFileForAppend(path);
451: writeFileSync(fd, line, "utf8");
452: } catch {
453: // Silently degrade — logging failure shouldn't crash the app
454: } finally {
455: if (fd !== undefined) ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path>
<type>file</type>
<content>
12555: }
12556:
12557: export function accumulateResponsesNonStreamJSON(
12558: json: Record<string, unknown>,
12559: ): GatewayResponse {
12560: const content: GatewayContentBlock[] = [];
12561: const output = json.output as Array<Record<string, unknown>> | undefined;
12562: const replayableOutput = output?.filter(
12563: (item) => item.type !== "item_reference",
12564: );
12565:
12566: if (replayableOutput) {
12567: const identities = new Set<string>();
12568: for (const item of replayableOutput) {
12569: const itemId = asString(item.id);
12570: if (!itemId || identities.has(itemId)) {
12571: throw new Error("malformed Responses response item identity");
12572: }
12573: identities.add(itemId);
12574: if (item.type === "message") {
12575: const msgContent = item.content as
12576: | ...
Success. Updated the following files: M ../opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts
Found 2 matches /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts: Line 231: parseGeminiResponseJSON, Line 12059: ? parseGeminiResponseJSON
Found 1 matches /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/gemini.ts: Line 485: export function parseGeminiResponseJSON(
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/gemini.ts</path>
<type>file</type>
<content>
485: export function parseGeminiResponseJSON(
486: json: Record<string, unknown>,
487: ): GatewayResponse {
488: const candidates = Array.isArray(json.candidates) ? json.candidates : [];
489: validateGeminiCandidateToolIdentities(
490: candidates,
491: "malformed Gemini response tool identity",
492: );
493: // LIMITATION: the internal model holds a single response, so when a client
494: // requests `candidateCount > 1` only candidates[0] is surfaced. Multi-candidate
495: // fan-out is a documented follow-up, not currently supported.
496: const first = (candidates[0] ?? {}) as Record<string, unknown>;
497: const content = (first.content ?? {}) as Record<string, unknown>;
498: const parts = Array.isArray(content.parts)
499: ? (content.parts as GeminiPart[])
500: : [];
501:
502: const blocks: GatewayContentBlock[] = [];
503: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/gemini.ts</path>
<type>file</type>
<content>
390: body.safetySettings = req.metadata.safetySettings;
391: if (req.metadata.toolConfig) body.toolConfig = req.metadata.toolConfig;
392: if (req.metadata.cachedContent)
393: body.cachedContent = req.metadata.cachedContent;
394:
395: return {
396: url: buildGeminiUpstreamUrl(upstreamBase, req.model, req.stream),
397: headers,
398: body,
399: };
400: }
401:
402: // ---------------------------------------------------------------------------
403: // Gemini response → GatewayResponse
404: // ---------------------------------------------------------------------------
405:
406: /**
407: * Map a Gemini `finishReason` + tool presence to an internal stop reason. ...
Found 5 matches /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts: Line 182: parseAnthropicResponseJSON, Line 12363: // Anthropic non-stream JSON → GatewayResponse: use shared parseAnthropicResponseJSON Line 12364: const accumulateAnthropicNonStreamJSON = parseAnthropicResponseJSON; /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/gemini.ts: Line 483: * Mirrors `parseAnthropicResponseJSON`, over `candidates[0].content.parts[]`. /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/anthropic.ts: Line 544: export function parseAnthropicResponseJSON(
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/anthropic.ts</path>
<type>file</type>
<content>
544: export function parseAnthropicResponseJSON(
545: json: Record<string, unknown>,
546: ): GatewayResponse {
547: const content: GatewayContentBlock[] = [];
548: const toolIdentities = new Set<string>();
549: const rawContent = json.content as Array<Record<string, unknown>> | undefined;
550: if (rawContent) {
551: for (const block of rawContent) {
552: switch (block.type) {
553: case "text":
554: content.push({ type: "text", text: asString(block.text) });
555: break;
556: case "thinking":
557: content.push({
558: type: "thinking",
559: thinking: asString(block.thinking),
560: ...(block.signature
561: ? { signature: asString(block.signature) }
562: : undefined),
563: });
564: break;
565: case ...
Found 2 matches /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/anthropic.ts: Line 19: normalizeAnthropicStopReason, Line 607: stopReason: normalizeAnthropicStopReason(
Success. Updated the following files: M ../opencode-lore-recall-recovery/packages/gateway/src/recall.ts
Found 14 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts:
Line 128: isEmptyCompletion,
Line 589: function injectContextWarning(
Line 617: * that's where injectContextWarning() inserts it. This avoids false positives
Line 625: // Find the first non-thinking block (mirrors injectContextWarning insertion point)
Line 2677: // injectContextWarning insertion rule. `last` is the earlier message and
Line 17697: const shouldInjectWarning = !!warningText;
Line 18392: shouldInjectWarning
Line 18393: ? injectContextWarning(markerResp, warningText)
Line 18507: shouldInjectWarning
Line 18508: ? injectContextWarning(recovered, warningText)
Line 18644: if (isEmptyCompletion(currentResp)) {
Line 18664: shouldInjectWarning
Line 18665: ? injectContextWarning(currentResp, warningText)
Line 18825: if (req.protocol === "openai-responses" && ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts</path> <type>file</type> <content> 560: // Stripped from incoming requests on subsequent turns to preserve cache prefix. 561: // Used by the worker-degradation warning (#797 removed the unsustainable- 562: // conversation warning; the marker mechanism stays because worker degradation 563: // is still user-actionable). 564: export const CONTEXT_WARNING_MARKER = "[lore:context-warning]"; 565: 566: /** 567: * Build the worker-degradation warning text (or null if the session's 568: * background workers are healthy / not yet sustained-failing). Reuses the 569: * CONTEXT_WARNING_MARKER so it is stripped on the next turn, preserving the 570: * prompt cache prefix. 571: * 572: * This is the user-visible signal that distillation/curation/cache-warming 573: * are failing — so degradation (context bloat, no LTM growth) is never silent. ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/types.ts</path>
<type>file</type>
<content>
380: result: string;
381: /** Unique replay anchor. Unlike query/scope or provider call IDs, this stays
382: * unique when the model repeats an identical recall. */
383: anchorId?: string;
384: /** Fingerprint of the transcript prefix that produced the anchor. */
385: /** Missing only on query-keyed entries persisted by pre-anchor releases. */
386: anchorContextId?: string;
387: /** Non-recall tool calls emitted beside this hidden recall in the original
388: * response. Used to restore only genuine mixed-tool turns; a tool called by
389: * the later recall continuation must remain in its own assistant message. */
390: companionToolUseIds?: string[];
391: companionToolUses?: Array<{
392: id: string;
393: name: string;
394: input: unknown;
395: side: "before" | "after";
396: }>;
397: };
398:
399: /** Map from unique ...
Found 1 matches /home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/types.ts: Line 86: export type GatewayContentBlock =
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/translate/types.ts</path>
<type>file</type>
<content>
75: *
76: * `raw` holds the original protocol block (e.g. an Anthropic
77: * `{ type: "image", source: { type: "base64", media_type, data } }`).
78: */
79: export type GatewayOpaqueBlock = {
80: type: "opaque";
81: raw: Record<string, unknown>;
82: /** Raw is a complete top-level Responses item, not a message content part. */
83: responsesItem?: boolean;
84: };
85:
86: export type GatewayContentBlock =
87: | GatewayTextBlock
88: | GatewayThinkingBlock
89: | GatewayToolUseBlock
90: | GatewayToolResultBlock
91: | GatewayOpaqueBlock;
92:
93: /**
94: * Project a content-block array down to a plain-text representation.
95: *
96: * Used wherever a string is required: memory/FTS storage, deterministic
97: * message-ID hashing, and text-only egress wire forms (OpenAI tool messages,
98: * Responses `function_call_output.output`). ...
Found 44 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/llm-adapter.test.ts:
Line 3133: delta: { stop_reason: "refusal", stop_sequence: null },
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/worker-reasoning-parse.test.ts:
Line 146: content: [{ type: "refusal", refusal: "cannot comply" }],
Line 419: content: [{ type: "refusal", refusal: "cannot comply" }],
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/worker-health.test.ts:
Line 784: expect(isCapabilityEmpty("refusal")).toBe(false);
Line 835: expect(recordEmptyWorkerResponse("anthropic", "m", "refusal")).toBe(
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall.test.ts:
Line 2795: content: [{ type: "refusal", refusal: text }],
Line 2834: content: [{ type: "refusal", refusal: "cannot comply" }],
Line 3441: content: [{ type: "refusal", refusal: "earlier ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall.test.ts</path>
<type>file</type>
<content>
3560: );
3561: test.each(["", " \t\n", null, undefined, 7])(
3562: "rejects malformed name %j even beside usable output",
3563: (name) => {
3564: const malformed = {
3565: type: "tool_use" as const,
3566: id: "invalid",
3567: name: name as string,
3568: input: {},
3569: };
3570: const companions = [
3571: [],
3572: [{ type: "text" as const, text: "Useful answer" }],
3573: [
3574: {
3575: type: "tool_use" as const,
3576: id: "ordinary",
3577: name: "Read",
3578: input: {},
3579: },
3580: ],
3581: [
3582: {
3583: type: "opaque" as const,
3584: responsesItem: true,
3585: raw: {
3586: type: "message",
3587: content: [{ type: ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall.test.ts</path>
<type>file</type>
<content>
3500: const replaced = replaceRecallWithMarker(resp);
3501: expect(replaced.content).toHaveLength(3);
3502: expect(replaced.content[0].type).toBe("text");
3503: expect(replaced.content[1].type).toBe("text");
3504: expect((replaced.content[1] as { text: string }).text).toBe(
3505: buildRecallMarker("find config", "project"),
3506: );
3507: expect(replaced.content[2].type).toBe("tool_use");
3508: expect((replaced.content[2] as GatewayToolUseBlock).name).toBe("Read");
3509: });
3510:
3511: test("returns same content when no recall present", () => {
3512: const resp = makeResponse([
3513: { type: "text", text: "hello" },
3514: { type: "tool_use", id: "toolu_1", name: "Read", input: {} },
3515: ]);
3516:
3517: const replaced = replaceRecallWithMarker(resp);
3518: ...
Success. Updated the following files: M ../opencode-lore-recall-recovery/packages/gateway/test/recall.test.ts
Found 6 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/worker-reasoning-parse.test.ts:
Line 116: message: { content: null as unknown as string, refusal: "refused" },
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/openai-responses-recall-aware-stream.test.ts:
Line 6445: item: { type: "message", id: "msg_refusal", role: "assistant" },
Line 6508: item: { type: "message", id: "msg_refusal_done", role: "assistant" },
Line 6630: item: { type: "message", id: "msg_refusal", role: "assistant" },
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/test/recall.test.ts:
Line 3692: { type: "message", content: [{ type: "output_text", refusal: "no" }] },
Line 3693: { type: "message", content: { type: "refusal", refusal: "no" } },
Found 24 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts:
Line 7988: const value = kind === "refusal" ? part.refusal : part.text;
Line 8001: part.kind === "refusal"
Line 8002: ? { type: "refusal", refusal: part.authoritativeValue }
Line 8357: /^response\.(?:output_item|output_text|function_call_arguments|content_part|reasoning_(?:summary|text)|refusal)/.test(
Line 8518: new Set(["output_text", "refusal"]),
Line 8629: event.startsWith("response.refusal")) &&
Line 8639: event.startsWith("response.refusal")
Line 8644: : event.startsWith("response.refusal")
Line 8645: ? "refusal"
Line 8658: !["output_text", "refusal", "reasoning_text"].includes(kind)
Line 8740: kind === "refusal" ? parsed.refusal : parsed.text;
Line 8777: event.startsWith("response.refusal")) &&
Line 9919: if (item.refusal !== ...
Found 9 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/pipeline.ts:
Line 8002: ? { type: "refusal", refusal: part.authoritativeValue }
Line 9925: content: [{ type: "refusal", refusal: item.refusal }],
Line 12409: const candidateMessage = choice.message as Record<string, unknown>;
Line 12411: (candidateMessage.content !== undefined &&
Line 12412: candidateMessage.content !== null &&
Line 12413: typeof candidateMessage.content !== "string") ||
Line 12414: (candidateMessage.role !== undefined &&
Line 12415: typeof candidateMessage.role !== "string")
Line 12419: const candidateCalls = candidateMessage?.tool_calls;
Found 25 matches
/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai.ts:
Line 33: import { safeTokenSum, validateOpenAIUsage } from "../usage-validation";
Line 115: usage?: Record<string, unknown>,
Line 130: if (usage) {
Line 131: chunk.usage = usage;
Line 352: // Build usage from accumulator
Line 354: const ru = resp.usage ?? ZERO_USAGE;
Line 361: "Anthropic usage token overflow",
Line 363: const usage: Record<string, unknown> = {
Line 368: "Anthropic usage token overflow",
Line 375: usage.prompt_tokens_details = {
Line 381: // Emit final chunk with finish_reason and usage
Line 384: formatChunk({}, finishReason || "stop", usage),
Line 518: const usage = parsed.usage as Record<string, unknown> | undefined;
Line 519: if (!usage) return;
Line 520: if ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai.ts</path> <type>file</type> <content> 490: let id = ""; 491: let model = ""; 492: let stopReason = "end_turn"; 493: let textContent = ""; 494: // Some reasoning models (e.g. MiniMax-M3 via OpenRouter) stream their entire 495: // answer as reasoning deltas and leave `content` empty. Capture it so a 496: // reasoning-only response is not mistaken for an empty completion (#1334) — 497: // mirrors the non-streaming parseOpenAIResponse content→reasoning fallback. ...
<path>/home/byk/Code/opencode-lore-recall-recovery/packages/gateway/src/stream/openai.ts</path>
<type>file</type>
<content>
850:
851: const content: GatewayContentBlock[] = [];
852: // Thinking precedes text (Anthropic ordering). Previously reasoning deltas were
853: // dropped entirely on this path; surfacing them lets a reasoning-only response
854: // (empty content) still yield usable text downstream (#1334).
855: if (reasoningContent) {
856: content.push({ type: "thinking", thinking: reasoningContent });
857: }
858: if (textContent) {
859: content.push({ type: "text", text: textContent });
860: } else if (refusalContent) {
861: content.push({ type: "text", text: refusalContent });
862: }
863: for (const [, tc] of Array.from(toolCalls.entries()).sort(
864: ([a], [b]) => a - b,
865: )) {
866: let input: unknown = {};
867: if (tc.args) {
868: try {
869: input = JSON.parse(tc.args);
870: } catch {
871: input = ...
Date: Sep 16, 2026 * 🟡 (22:41) In `packages/gateway/src/pipeline.ts:9950-9959`, stream-abort cleanup resumes pending demand, clears `keepaliveTimer`, and either calls `cancelAndReleaseReader(activeReader, signal.reason)` or cancels `currentPrincipalResponse.body`; `onStreamAbort` is registered once and invoked immediately if `signal.aborted`. * 🟡 (22:41) In `packages/gateway/src/pipeline.ts:996…
Date: Sep 16, 2026 * 🟡 (22:39) In `packages/gateway/src/pipeline.ts:10351-10362`, `response.output_item.done` calls `collectCompletedRecall(state, outputIndex, parsedRecallInputs, pendingRecalls)` for recall events, and all recall-item or unresolved-tool events are withheld from the client via `continue`. * 🟡 (22:39) In `packages/gateway/src/pipeline.ts:10365-10430`, terminal handling covers `r…
Date: Sep 16, 2026 * 🔴 [enforced-workflow] (22:35) User instructed the agent to resume the existing recovery implementation session in place after the server restart, preserving all edits, tests, and results while continuing the original blocker-fix assignment. * 🔴 [requested-evidence] (22:35) User required substantive fail-first, fixed, and mutant evidence for the blocker fix. * 🔴 (22:35) Use…
Date: Sep 16, 2026 * 🟡 (22:22) Fail-first regression run with Vitest v4.1.8 produced exactly 3 failures and 225 passes across 228 tests; 1 of 2 test files failed. All failures were in `packages/gateway/test/openai-responses-recall-aware-stream.test.ts`. * 🟡 (22:22) The fail-first regression `suppresses malformed non-JSON principal Codex quota events` failed at `packages/gateway/test/openai-resp…
Date: Sep 16, 2026 * 🔴 (22:10) User required recovery-accounting overflow to route synchronously through fixed recall failure, preserve only previously validated accounting, and never convert failure into completion. * 🔴 (22:10) User required native-load, OOM, and corruption failures to be classified narrowly from the leading error string so unrelated missing files are never misclassified. * 🔴…
Date: Sep 16, 2026 * 🟡 (22:01) User provided additional test code that imports `GatewayRequest` from `../src/translate/types`, creates streaming `openai-responses` requests via `request("openai-responses", alias, true)`, and parses SSE frames using `JSON.parse(frame.split("\ndata: ")[1])`. * 🟡 (22:01) The supplied tests inspect persistence with `SELECT COUNT(*) AS count FROM temporal_messages W…
Date: Sep 16, 2026 * 🔴 (22:00) User required invalid usage to still throw; the usage projection is never returned as success (`packages/gateway/src/pipeline.ts:12066`). * 🟡 (22:00) Inspection of `assertValidNonStreamCompletion()` in `packages/gateway/src/pipeline.ts:12103` found strict terminal-envelope validation for `anthropic`, `openai`, `openai-responses`, `vertex`, and `gemini`. * 🟡 (22:0…
Date: Sep 16, 2026 * 🔴 (21:59) User required the recall follow-up’s `stream` flag to never diverge from how the continuation is consumed; streaming input may be accumulated into a non-streaming continuation before the recall loop proceeds. * 🔴 (21:59) User required the lore context-warning read path to “Never throws / never blocks.” * 🔴 (21:59) User required `packages/gateway/src/recall.ts` to…
Date: Sep 16, 2026 * 🔴 (21:57) User required recall-recovery overflow to synchronously route through the fixed recall failure, preserve only previously validated accounting, and never convert failure into completion. * 🔴 (21:57) User required OpenAI Responses recall to use transformed full-history replay with `store:false` and never rely on client state or `previous_response_id`. * 🔴 (21:57) U…
Date: Sep 16, 2026 * 🟡 (21:54) Workspace `/home/byk/Code/opencode-lore-recall-recovery` working copy is `pqvltmkr` / `ac80b275f287` with description `fix(recall): harden quota metadata`; parent is `spyyzvyy` / `3f3ae768fde5` with description `fix/recall-failure-synthesis | fix(recall): validate recovery metadata`; frozen candidate remains `mmmktlwm` / `b61b086186cb` with description `fix(recall)…
Date: Sep 16, 2026 * 🟡 (21:46) User requested implementation of recovery-review corrections in the isolated workspace `/home/byk/Code/opencode-lore-recall-recovery`, based on frozen candidate `mmmktlwm/b61b0861`, using clean empty child `spyyzvyy` if present; create/use an additive child only if needed. * 🔴 [enforced-workflow] (21:46) User directed not to commit, rebase, or move bookmarks. * 🔴…