Skip to content

feat: add rogue gateway tokenomics routing spike package#210

Open
bearmug wants to merge 4 commits into
mainfrom
feature/issue-207-rogue-gateway-spike
Open

feat: add rogue gateway tokenomics routing spike package#210
bearmug wants to merge 4 commits into
mainfrom
feature/issue-207-rogue-gateway-spike

Conversation

@bearmug

@bearmug bearmug commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Prototype spike for standalone Rogue Gateway (issue #207).

What this adds

  • New workspace package: packages/rogue-gateway
  • Tokenomics-aware route planning (quoteRoute) with deterministic score/guard logic
  • Substrate abstraction with mock + OpenAI-compatible adapters
  • Decision event ledger for route/provenance observability
  • Spike HTTP endpoints:
    • POST /rogue/economics/quote
    • /health
  • Planner/server tests for happy-path routing and malformed payload handling

Validation

  • npm run check --workspace @fiale-plus/pi-rogue-gateway
  • npm run test --workspace @fiale-plus/pi-rogue-gateway
  • npm run check --workspaces --if-present
  • npm run test --workspaces --if-present

Closes #207

@bearmug
bearmug force-pushed the feature/issue-207-rogue-gateway-spike branch from 6eeb1ef to f0b3508 Compare June 23, 2026 21:29

@bearmug bearmug left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #210 looks good and checks are passing.

I reviewed against issue #207 requirements and current diff:

  • Added standalone (planner/substrates/events/server/tests) ✅
  • Workspace checks/tests pass locally and in CI ✅
  • Lockfile updated for npm ci parity ✅

Non-blocking follow-up: consider wrapping JSON parse in request handling in the try/catch so malformed JSON returns 400 instead of throwing out of handler.
Otherwise this is good to merge.

@bearmug bearmug left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #210 looks good and checks are passing.

I reviewed against issue #207 requirements and current diff:

  • Added standalone packages/rogue-gateway (planner/substrates/events/server/tests) ✅
  • Workspace checks/tests pass locally and in CI ✅
  • Lockfile updated for npm ci parity ✅

Non-blocking follow-up: consider wrapping JSON parse in startGatewayServer request handling in the try/catch so malformed JSON returns 400 instead of throwing out of handler.
Otherwise this is good to merge.

@bearmug
bearmug force-pushed the feature/issue-207-rogue-gateway-spike branch from f0b3508 to c5e1f80 Compare June 23, 2026 21:34
@bearmug

bearmug commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

Codex-style review pass: fixed a blocker in server request handling.

Fixes made:

  • Wrapped JSON parsing in /rogue/economics/quote with safe parse handling inside the request handler.
  • Malformed JSON now returns 400 { error: "invalid json" } instead of throwing and dropping connection handling.
  • Added regression coverage in server.test.ts for invalid JSON body.

Validation after fix:

  • npm run check --workspace @fiale-plus/pi-rogue-gateway
  • npm run test --workspace @fiale-plus/pi-rogue-gateway
  • npm run check --workspaces --if-present
  • npm run test --workspaces --if-present
  • PR checks are passing.

@bearmug
bearmug force-pushed the feature/issue-207-rogue-gateway-spike branch from c5e1f80 to 751b001 Compare June 23, 2026 21:47
@bearmug

bearmug commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

Added the missing explicit Commodity Gateway Substrate Check report in this PR (same branch), including direct answers to the required 1..6 questions.

Files added/updated:

  • packages/rogue-gateway/COMMODITY_GATEWAY_SUBSTRATE_CHECK.md
  • packages/rogue-gateway/README.md (added section + link)

Summary in PR:

  1. Duplicated gateway pieces: auth/keying, retries/failover, quotas/budgets, provider protocol compatibility, cost telemetry/ops stack.
  2. Outsourced to LiteLLM/Portkey: those commodity runtime pieces.
  3. Rogue in front of generic OpenAI-compatible: yes (OpenAICompatibleSubstrate, /models and /chat/completions).
  4. Substrate-specific tokenomics/context logic: no (planner is metadata-driven).
  5. Minimal interface: listModels, callChat, optional estimateCost, optional getUsage.
  6. Painful to outsource: local-first/tokenomics policy + deterministic route explanations + local-first value accounting.

Validation still passes (npm run check/test --workspace ... and workspaces wide).

@bearmug

bearmug commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Rogue Gateway spike outcome

Status: validated spike / pilot setup, not a target-system architecture recommendation yet.

What is now in PR #210:

  • deterministic tokenomics routing package for the Rogue Gateway spike
  • commodity gateway substrate check documentation
  • OpenAI-compatible substrate with /models + /v1/models and /chat/completions + /v1/chat/completions fallback support
  • env-driven Portkey/OpenAI-compatible adapter with no bundled credentials
  • Pi Rogue config model routing: pi-dedicated resolves to the configured upstream GPT/model target
  • token measurement harness for:
    • raw_forward
    • typed_lens
    • lookup_compress

Local validation performed:

  • npm run check --workspace @fiale-plus/pi-rogue-gateway --silent
  • npm run test --workspace @fiale-plus/pi-rogue-gateway --silent
  • npm run check --workspaces --if-present --silent
  • npm run test --workspaces --if-present --silent

Manual Portkey experiment setup is prepared, but a live Portkey run was not executed because no Portkey endpoint/key is available in this environment.

Expected manual env setup:

PORTKEY_BASE_URL=...
PORTKEY_API_KEY=...
PORTKEY_AUTH_HEADER=x-portkey-api-key # optional; defaults are supported
PI_ROGUE_CONFIG_PATH=...
PI_ROGUE_ROUTER_CONFIG_PATH=...

Fugu-ultra review was attempted but not obtained: Sakana returned 429 Subscription window is exhausted. Current strategic advice available in-session came from the configured advisor model, not fugu-ultra.

Conclusion: this is good evidence for the pilot/spike. Before treating it as the target system direction, follow-ups should harden config schema validation, failure-mode tests, credential redaction, and observability.

@bearmug

bearmug commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Final explicit spike status / manual experiment readiness

Outcome: PR #210 is a validated Rogue Gateway spike / pilot setup, not a recommendation to promote this as the target gateway architecture yet.

What is in the PR now:

  • deterministic tokenomics route planner and event-led observability
  • commodity gateway substrate check documentation
  • OpenAI-compatible substrate with /models + /v1/models and /chat/completions + /v1/chat/completions fallback support
  • env-driven Portkey/OpenAI-compatible adapter with no bundled credentials
  • Pi Rogue config model routing that maps pi-dedicated to the configured upstream model target
  • token-measurement harness for:
    • raw_forward
    • typed_lens
    • lookup_compress

Verification completed locally and in CI:

  • PR CI is green.
  • Local workspace validation passed:
    • npm run check --workspace @fiale-plus/pi-rogue-gateway --silent
    • npm run test --workspace @fiale-plus/pi-rogue-gateway --silent
    • npm run check --workspaces --if-present --silent
    • npm run test --workspaces --if-present --silent

Manual Portkey experiment readiness: prepared, but not live-run here because this environment does not have a real Portkey/OpenAI-compatible endpoint + key.

Required env:

export PORTKEY_BASE_URL="https://..."          # Portkey/OpenAI-compatible base URL
export PORTKEY_API_KEY="..."                  # secret; not stored in repo
export PORTKEY_AUTH_HEADER="x-portkey-api-key" # optional; default Authorization/Bearer also supported
export PI_ROGUE_CONFIG_PATH="$HOME/.pi/agent/pi-rogue/config.json"
export PI_ROGUE_ROUTER_CONFIG_PATH="$HOME/.pi/agent/pi-rogue/router/config.json"

One-shot manual measurement command from repo root:

cat > /tmp/rogue-portkey-measure.ts <<'TS'
import { measurePiDedicatedModes } from "./packages/rogue-gateway/src/measurement.js";

const report = await measurePiDedicatedModes({
  profile: process.env.PI_ROGUE_PROFILE ?? "local-smart",
  role: process.env.PI_ROGUE_ROLE ?? "smart",
  request: {
    profile: "local-first-economy",
    taskKind: "manual_portkey_measurement",
    rawInputTokensApprox: Number(process.env.RAW_INPUT_TOKENS ?? 82000),
    forwardedInputTokensApprox: Number(process.env.FORWARDED_INPUT_TOKENS ?? 2400),
    expectedOutputTokensApprox: Number(process.env.EXPECTED_OUTPUT_TOKENS ?? 900),
    contextPolicy: "typed_lens",
    candidateAssets: ["local.qwen35", "remote.cheap", "remote.premium", "subscription.smart"],
  },
});

console.log(JSON.stringify({
  profile: report.profile,
  role: report.role,
  requestedModel: report.requestedModel,
  upstreamModel: report.upstreamModel,
  modes: report.modes.map((mode) => ({
    mode: mode.mode,
    forwardedInputTokensApprox: mode.request.forwardedInputTokensApprox,
    selectedRoute: mode.quote.selected.route,
    selectedAsset: mode.quote.selected.asset,
    ok: mode.chat.ok,
    status: mode.chat.status,
    providerUsage: mode.chat.usage ?? null,
  })),
}, null, 2));
TS

# Run from repo root so the relative package import resolves:
npx tsx /tmp/rogue-portkey-measure.ts

Expected manual output: JSON showing pi-dedicated resolved to the configured upstream model and per-mode route/usage values for raw_forward, typed_lens, and lookup_compress.

Still missing for a live external result: real PORTKEY_BASE_URL + PORTKEY_API_KEY. Without those, only mock OpenAI-compatible tests can be validated in CI/local automation.

Fugu-ultra note: attempted but not obtained; Sakana returned 429 Subscription window is exhausted. Available strategic advice in-session was not fugu-ultra.

@bearmug

bearmug commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Added a repo-local manual experiment artifact for the Portkey self-host path:

  • packages/rogue-gateway/PORTKEY_SELF_HOST_EXPERIMENT.md
  • packages/rogue-gateway/src/measure.ts
  • npm run measure:portkey --workspace @fiale-plus/pi-rogue-gateway

This keeps the spike scope intact: the repo still cannot launch Portkey itself, but the exact env-driven manual run is now drafted and runnable once a real Portkey/OpenAI-compatible endpoint + API key are provided in the runtime environment.

@bearmug

bearmug commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Drafted the tiny self-host Portkey experiment setup requested:

  • packages/rogue-gateway/docker-compose.yml
  • packages/rogue-gateway/.env.example
  • updated packages/rogue-gateway/PORTKEY_SELF_HOST_EXPERIMENT.md
  • added npm run measure:portkey --workspace @fiale-plus/pi-rogue-gateway

This keeps the experiment repo-local and env-driven. It does not launch Portkey from within this repo; it expects a real Portkey/OpenAI-compatible runtime to be started externally via Docker/infra and then pointed to with PORTKEY_BASE_URL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prototype Spike: Rogue Gateway substrate + tokenomics router

1 participant