YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
Fix Luna pipe tool routing
Louis Beaumont33d5637dda01
diff --git a/crates/screenpipe-core/src/agents/pi.rs b/crates/screenpipe-core/src/agents/pi.rsindex 748960032..eb4d8f8db 100644--- a/crates/screenpipe-core/src/agents/pi.rs+++ b/crates/screenpipe-core/src/agents/pi.rs@@ -115,8 +115,20 @@ async fn fetch_models_from_gateway(let body: serde_json::Value = resp.json().await.ok()?;let data = body.get("data")?.as_array()?;- let models: Vec<serde_json::Value> = data+ let models = gateway_models_to_pi_models(data);++ info!("fetched {} models from gateway", models.len());+ Some(json!(models))+}++/// Turn the gateway catalog into Pi's provider catalog. The gateway retains+/// locked models for UI upgrade prompts, while Pi treats every listed model as+/// selectable. Omit locked entries here so a pipe never appears to select a+/// model only for the gateway to silently rewrite it to `auto`.+fn gateway_models_to_pi_models(data: &[serde_json::Value]) -> Vec<serde_json::Value> {+ data.iter()+ .filter(|m| !m.get("locked").and_then(|v| v.as_bool()).unwrap_or(false)).map(|m| {let id = m.get("id").and_then(|v| v.as_str()).unwrap_or("");let name = m.get("name").and_then(|v| v.as_str()).unwrap_or(id);@@ -140,10 +152,7 @@ async fn fetch_models_from_gateway("maxTokens": 32000,})})- .collect();-- info!("fetched {} models from gateway", models.len());- Some(json!(models))+ .collect()}/// Minimal fallback when the gateway is unreachable.@@ -3392,6 +3401,38 @@ mod tests {]));}+ #[test]+ fn gateway_catalog_omits_locked_models_from_pi() {+ let models = gateway_models_to_pi_models(&[+ json!({+ "id": "auto",+ "name": "Auto",+ "context_window": 128000,+ "intelligence": "standard",+ }),+ json!({+ "id": "gpt-5.6-terra",+ "name": "GPT-5.6 Terra",+ "locked": true,+ "context_window": 128000,+ "intelligence": "highest",+ }),+ json!({+ "id": "gpt-5.6-luna",+ "name": "GPT-5.6 Luna",+ "locked": null,+ "context_window": 128000,+ "intelligence": "high",+ }),+ ]);++ let ids: Vec<&str> = models+ .iter()+ .filter_map(|model| model.get("id").and_then(|id| id.as_str()))+ .collect();+ assert_eq!(ids, vec!["auto", "gpt-5.6-luna"]);+ }+#[test]fn test_is_rate_limit_error() {assert!(is_rate_limit_error("HTTP 429 Too Many Requests"));diff --git a/packages/ai-gateway/src/handlers/chat.ts b/packages/ai-gateway/src/handlers/chat.tsindex bce07dc87..d8ba3b9e7 100644--- a/packages/ai-gateway/src/handlers/chat.ts+++ b/packages/ai-gateway/src/handlers/chat.ts@@ -43,12 +43,14 @@ export const AUTO_WATERFALL_VISION = [// lacks). Measured (6/19): gpt-5.4 here cost $590/day vs $177 on flex for the SAME// traffic — 3.3x more, on the small OpenAI pool, for a marginal IQ gain (54 vs ~50-55)// that pipes don't need; reverted (#4285→). glm-5 + gemini-3-flash are standard-tier-// fallbacks for when flex is throttled. Keep Luna out of this tool-heavy lane:-// GPT-5.6 Luna can serve ordinary background chat, but tool-enabled Pi pipe-// requests currently fall through to GLM. Retrying it here would add latency-// without actually serving the pipe.+// fallbacks for when flex is throttled. Luna is a paid, high-volume fallback+// immediately after the flex primary: it improves resilience without replacing+// the proven cache-efficient default for every scheduled run. Pi's streaming+// tool policy is forwarded by the OpenAI adapter, so this lane can use Luna for+// tool-enabled pipes as intended.export const AUTO_WATERFALL_BACKGROUND = ['gemini-3.5-flash', // flex tier — cheapest on the cache-heavy background mix+ 'gpt-5.6-luna', // paid fallback for complex / tool-enabled pipe work'glm-5', // free Vertex MaaS fallback, standard tier'gemini-3-flash', // near-free safety net];diff --git a/packages/ai-gateway/src/providers/openai.ts b/packages/ai-gateway/src/providers/openai.tsindex 6af031855..af1df3789 100644--- a/packages/ai-gateway/src/providers/openai.ts+++ b/packages/ai-gateway/src/providers/openai.ts@@ -218,6 +218,11 @@ export class OpenAIProvider implements AIProvider {stream_options: { include_usage: true },response_format: this.formatResponseFormat(body.response_format),tools: body.tools as ChatCompletionCreateParams['tools'],+ // Keep the streaming path semantically identical to non-streaming+ // requests. Pi streams every pipe run; dropping tool_choice here meant+ // a caller's explicit tool policy (notably "required") never reached+ // GPT-5.6 Luna even though the model supports function calling.+ tool_choice: body.tool_choice as ChatCompletionCreateParams['tool_choice'],};this.applyGenerationOptions(params, body);diff --git a/packages/ai-gateway/src/test/auto-cost-attribution.unit.test.ts b/packages/ai-gateway/src/test/auto-cost-attribution.unit.test.tsindex caddf35cf..95c68ef68 100644--- a/packages/ai-gateway/src/test/auto-cost-attribution.unit.test.ts+++ b/packages/ai-gateway/src/test/auto-cost-attribution.unit.test.ts@@ -73,10 +73,10 @@ describe('routing chains stay priceable (no silent $0.01 fallback rows)', () =>expect(getModelCost('gemini-3-flash', 1_000_000, 0)).toBeCloseTo(0.10, 10);});- it('places Luna behind the current chat primaries but keeps tool-heavy pipes on proven models', () => {+ it('places Luna behind the current auto primaries for text, vision, and pipes', () => {const luna = 'gpt-5.6-luna';expect(AUTO_WATERFALL.indexOf('glm-5')).toBeLessThan(AUTO_WATERFALL.indexOf(luna));expect(AUTO_WATERFALL_VISION.indexOf('gemini-3.5-flash')).toBeLessThan(AUTO_WATERFALL_VISION.indexOf(luna));- expect(AUTO_WATERFALL_BACKGROUND).not.toContain(luna);+ expect(AUTO_WATERFALL_BACKGROUND.indexOf('gemini-3.5-flash')).toBeLessThan(AUTO_WATERFALL_BACKGROUND.indexOf(luna));});});diff --git a/packages/ai-gateway/src/test/provider-usage.unit.test.ts b/packages/ai-gateway/src/test/provider-usage.unit.test.tsindex 18c877e2a..424cd0bef 100644--- a/packages/ai-gateway/src/test/provider-usage.unit.test.ts+++ b/packages/ai-gateway/src/test/provider-usage.unit.test.ts@@ -78,6 +78,42 @@ describe('OpenAIProvider usage reporting', () => {expect(text.trim().endsWith('data: [DONE]')).toBe(true);});+ it('forwards Pi-shaped tools and required tool_choice on streams', async () => {+ async function* stream() {+ yield { choices: [{ delta: { tool_calls: [{ index: 0, id: 'call_1', type: 'function', function: { name: 'add_one', arguments: '{"a":1}' } }] }, finish_reason: 'tool_calls' }] };+ }+ const { provider, calls } = makeOpenAIProvider(async () => {+ const s: any = stream();+ s.controller = { abort: () => {} };+ return s;+ });+ const tools: any = [{+ type: 'function',+ function: {+ name: 'add_one',+ description: 'Adds one to a number.',+ strict: false,+ parameters: {+ type: 'object',+ properties: { a: { type: 'integer' } },+ required: ['a'],+ additionalProperties: false,+ },+ },+ }];++ await new Response(await provider.createStreamingCompletion({+ ...body,+ model: 'gpt-5.6-luna',+ stream: true,+ tools,+ tool_choice: 'required',+ })).text();++ expect(calls[0].tools).toEqual(tools);+ expect(calls[0].tool_choice).toBe('required');+ });+it('drops stream_options and retries when an OpenAI-compatible server rejects it', async () => {let attempt = 0;async function* stream() {diff --git a/packages/ai-gateway/src/utils/auth.ts b/packages/ai-gateway/src/utils/auth.tsindex 360b32104..055e225ce 100644--- a/packages/ai-gateway/src/utils/auth.ts+++ b/packages/ai-gateway/src/utils/auth.ts@@ -12,14 +12,15 @@ import { activeSubscriptionFilter } from './subscription';* @returns Promise resolving to boolean indicating if token is valid*/export async function verifyClerkToken(env: Env, token: string): Promise<{ valid: boolean; userId?: string }> {- console.log('verifying clerk token', token);try {const payload = await verifyToken(token, {secretKey: env.CLERK_SECRET_KEY,});return { valid: payload.sub !== null, userId: payload.sub ?? undefined };- } catch (error) {- console.error('clerk verification failed:', error);+ } catch {+ // Never log the JWT or upstream verification error verbatim: worker logs+ // are broadly accessible operational data and may retain request context.+ console.error('clerk verification failed');return { valid: false };}}@@ -208,10 +209,10 @@ async function validateSubscriptionWithId(env: Env, token: string): Promise<{ isconst subs = await subsRes.json() as Array<{ id: string }>;hasSub = subs.length > 0;} else {- console.error('Supabase subscription check error:', await subsRes.text());+ console.error('Supabase subscription check failed', subsRes.status);}- } catch (error) {- console.error('Error in UUID auth path:', error);+ } catch {+ console.error('UUID subscription check failed');}// Always return resolvedUserId (clerk_id when available, UUID otherwise)// so the non-subscribed UUID branch upstream also keys on the same id.@@ -220,7 +221,6 @@ async function validateSubscriptionWithId(env: Env, token: string): Promise<{ is// Clerk user IDs - resolve to UUID first, then check subscriptionif (CLERK_USER_ID_REGEX.test(token)) {- console.log('Clerk user ID detected, resolving to UUID:', token);try {// Resolve clerk_id to Supabase UUID (has_active_cloud_subscription expects uuid)const userResponse = await fetch(@@ -255,8 +255,8 @@ async function validateSubscriptionWithId(env: Env, token: string): Promise<{ is}}}- } catch (error) {- console.error('Error checking Clerk user subscription:', error);+ } catch {+ console.error('Clerk user subscription check failed');}// Not subscribed - don't auto-grant, return false so it falls throughreturn { isValid: false };@@ -292,7 +292,6 @@ async function validateScreenpipeToken(token: string): Promise<{ isValid: booleaif (response.ok) {const data = await response.json() as { success?: boolean; user?: ScreenpipeUserData };const userData = data.user;- console.log('Valid screenpipe user token, user:', userData?.email);return {isValid: true,userId: userData?.clerk_id || userData?.id || userData?.email,@@ -302,8 +301,8 @@ async function validateScreenpipeToken(token: string): Promise<{ isValid: booleaconsole.log('Invalid screenpipe user token');return { isValid: false };}- } catch (error) {- console.error('Error validating screenpipe token:', error);+ } catch {+ console.error('screenpipe token validation failed');return { isValid: false };}}