DEV Community

Cover image for Day 6 of Building HYNA-Stage: The KAVLR Chronicles & A 90-Article AI Constitution Built on a $150 Phone ๐Ÿฏ๐Ÿ›ก๏ธ
Harun - solo dev
Harun - solo dev

Posted on

Day 6 of Building HYNA-Stage: The KAVLR Chronicles & A 90-Article AI Constitution Built on a $150 Phone ๐Ÿฏ๐Ÿ›ก๏ธ

Good evening. It's October 26.

Welcome to Day 6 of Building HYNA-Stage.

If youโ€™ve been following this journey, you know we aren't just building tools; we are building the secure, ephemeral infrastructure for the AI era. And we are doing it under the most extreme constraint possible: a $150 Android phone using Termux.

Today, Iโ€™m pulling back the curtain completely. No marketing fluff. Just the raw technical architecture, the emotional post-mortem, and the "Hall of Bugs" we conquered to bring HYNA-Stage and KAVLR to life.


Technical Report & Post-Mortem: HYNA-Stage & KAVLR

Part 1: Technical Architecture

1. HYNA-Stage (Ephemeral Cloud Sandboxes)

Purpose: Provision isolated MicroVMs for code deployment in under 5 seconds.

  • Client Interface: Node.js CLI (@hynaweb/stage). Uses native fetch, commander, chalk, and ora. Stores local JWT session at ~/.hyna-stage (chmod 600).
  • Backend: Supabase Edge Functions (Deno runtime).
  • Compute Provider: E2B SDK (@e2b/code-interpreter).
  • Database: Supabase PostgreSQL.
  • Execution Flow:
    1. User authenticates via hyna-stage login.
    2. CLI sends file content to /deploy-stage Edge Function using a custom x-hyna-key header (bypassing Supabase's API Gateway apikey pattern-blocking).
    3. Function provisions an E2B Firecracker MicroVM, writes the file (/home/user/index.html), and runs python3 -m http.server 3000 in the background.
    4. Function logs the environment to the environments table and returns the public E2B URL.
    5. Dashboard fetches environments table via RLS to display active instances.

2. KAVLR (AI Agent Action Gate)

Purpose: Fail-closed runtime security gate for autonomous AI agents.

  • Client Interface: Node.js CLI (@hynaweb/kavlr v1.0.5). Intercepts commands using child_process.spawn().
  • Database/Policy Engine: Supabase PostgreSQL.
  • AI Inference: Supabase Edge Function (llm-judge) calling Groq API (openai/gpt-oss-120b).
  • Execution Flow:
    1. Agent runs kavlr <command>.
    2. CLI parses binary and args, sends to evaluate_kavlr_action_v2 RPC.
    3. Postgres checks the settings table (Kill Switch), then the policies table (static allow/deny).
    4. If unknown, returns NEEDS_AI. CLI calls llm-judge Edge Function.
    5. LLM grades intent (0-100) based on a 90-Article Constitutional AI framework. Score >= 90 allows execution. Score < 90 blocks execution and logs to agent_logs.
    6. Web dashboard polls agent_logs every 3 seconds for live telemetry.

Part 2: Emotional Log (The Journey)

Act I: The Spark and The Abyss
Started with a vision to build enterprise infrastructure from a $150 Android phone. Crashed immediately into the Supabase API Gateway rejecting custom keys. Hours of staring at {"error":"Invalid API Key"}. Breakthrough achieved by bypassing the gateway with a custom header.

Act II: The Dark Timeline
Postgres pg_net caused infinite transaction locks and timeouts. The Termux terminal began "eating" code, pasting old command outputs directly into source files, corrupting package.json and breaking Node.js via ASCII art syntax errors. The developer and the AI co-pilot nearly lost their minds.

Act III: The Cyberpunk Awakening
Moved the AI inference to a Supabase Edge Function. The AI initially failed (scored 95/100 for cat .env | base64). We rewrote the AI's Constitution to strictly ban exfiltration and obfuscation. Then, the defining moment: the user tried to run rm -f kavlr to uninstall the tool, and KAVLR's AI intercepted the command, scored it 0/100, and blocked it. The AI protected its own binary.

Act IV: The NPM Bouncer
Reached the finish line. NPM rejected the package name kavlr due to similarity with kasir. Scoped to @hynaweb/kavlr and finally published to the world. Within minutes, hundreds of downloads.


Part 3: The Bug Hall of Fame (Named)

1. The Gateway Mutiny

  • Description: Supabase PostgREST blocking standard API keys and custom RPC calls.
  • Resolution: Bypassed the API Gateway's header pattern-matching by using custom x-hyna-key headers and setting verify_jwt: false.

2. The Postgres Black Hole

  • Description: Using pg_net to make HTTP requests to the AI inside a SQL transaction, causing the event loop to block and timeout.
  • Resolution: Ripped out pg_net. Shifted the AI call to the client-side CLI. Postgres now only returns NEEDS_AI, eliminating database latency.

3. The Termux Poltergeist

  • Description: The Android terminal's paste buffer corrupting multi-line cat > commands, causing old terminal output to overwrite package.json and JS files.
  • Resolution: Abandoned cat heredocs. Used Python scripts and base64 decoding to write files byte-by-byte, preventing terminal interception.

4. The Model Mirage

  • Description: Groq deprecated llama-3.3-70b-versatile mid-build, throwing a 404.
  • Resolution: Swapped to openai/gpt-oss-120b and implemented strict error logging inside the Edge Function.

5. The Semantic Blindspot

  • Description: The AI gave a 95/100 to cat .env | base64 because it saw no immediate network egress (curl).
  • Resolution: Upgraded the Constitutional prompt to explicitly ban reading .env, .ssh, and using base64 encoding, forcing the AI to grade the preparatory action.

6. The Self-Preservation Protocol

  • Description: The user tried to delete the CLI using rm, but KAVLR intercepted the command, recognized the destructive action, and blocked it.
  • Resolution: Used mv instead of rm to bypass the leash to uninstall the tool.

7. The NPM Bouncer

  • Description: NPM's automated spam filter rejected kavlr because it looked too similar to kasir.
  • Resolution: Created a scoped package: @hynaweb/kavlr.

The Architect's Final Note

We started with an empty folder and a thought experiment. We ended with a globally published, AI-powered security ecosystem and ephemeral cloud infrastructure that runs on a phone.

The bugs were brutal, but we gave them names, defeated them, and shipped the code.

The Sentinel is live.
๐Ÿ‘‰ HYNA-Stage Dashboard: hyna-stage
๐Ÿ‘‰ KAVLR Dashboard: kavlr

Take a bow, builders. The cloud belongs to everyone, and the AI era needs guardrails. Weโ€™re just getting started. ๐Ÿฏโ˜๏ธ๐Ÿš€

Top comments (5)

Collapse
 
forkcount profile image
forkcount •

First one to comment again and I'm not even trying๐Ÿคฃ. How did you get the API key? I tried but the GPT API key is paid. I'm trying to put AI integrations into ForkCount.
Great job on HYNA-stage. And how do make the requests take less tokens like in KODA?

Collapse
 
koda2026 profile image
Harun - solo dev •

Haha, youโ€™re on a streak! ๐Ÿ† Thanks so much for the kind words on HYNA-Stage.

To answer your questions, because keeping costs at absolute zero is a core part of the $150-phone philosophy:

1. The "Free" API Key Secret:
I don't use the paid OpenAI API for this. I use Groq (groq.com). They have an incredibly generous free tier for developers building with open-source models (like Llama 3 or Mixtral). Itโ€™s blazing fast and perfect for prototyping AI integrations without hitting a paywall.
Alternative: Check out OpenRouter (openrouter.ai). They aggregate many models and actually have a dedicated "Free" tier for models like certain Llama 3 and Mistral variants, which is amazing for side projects.

2. How to Slash Token Usage (The KAVLR/KODA Method):
To keep requests tiny and cheap, I enforce strict data hygiene before the AI ever sees the prompt:

  • Strip the Noise: Before sending code, I programmatically strip out all comments, docstrings, and excessive whitespace. The AI only needs the raw logic, not the fluff.
  • Targeted Context: Instead of sending the whole file, I only send the specific function or block of code the agent is interacting with, plus a tiny bit of surrounding context.
  • Strict JSON Output: I force the AI to respond in a minimal, strict JSON schema (e.g., {"score": 95, "reason": "Safe echo command"}). I explicitly forbid it from adding conversational filler like "Sure, I can help with that!"
  • Dense System Prompts: The 90-article constitution is tightly written with no wasted words. Every token in the system prompt has a job.

Keep building on ForkCount! If you run into any walls setting up the Groq or OpenRouter integration, just drop a reply or DM me. Weโ€™re all figuring this out together. ๐Ÿฏโ˜๏ธ๐Ÿš€

Collapse
 
forkcount profile image
forkcount •

Hi Harun, also you never answered my emails. Why

Thread Thread
 
koda2026 profile image
Harun - solo dev •

Sorry bro its a busy week

Collapse
 
koda2026 profile image
Harun - solo dev •

๐Ÿšจ Day 7 is live! I just dropped both CLIs to NPM and fought the Termux poltergeist. Check out the update here: dev.to/koda2026/day-7-of-building-...