Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@ DISCORD_BOT_TOKEN=
# Leave unset (or false) during normal development to avoid rate limits.
LANG2SQL_SYNC_COMMANDS=

# OpenAI API key. Optional: when set, the agent uses gpt-4.1-mini. When unset,
# it falls back to the offline FakeLLM (deterministic canned tool cycles — fine
# for a smoke run, not for real answers).
OPENAI_API_KEY=

# ── Local LLM (vLLM / Ollama) ────────────────────────────────────────────
# When LANG2SQL_LLM_BASE_URL is set it takes priority over OPENAI_API_KEY.
# Point to the base URL of any OpenAI-compatible server.
# vLLM: http://localhost:8000
# Ollama: http://localhost:11434
LANG2SQL_LLM_BASE_URL=
# Model name as the server expects it (e.g. Qwen/Qwen3-14B-AWQ for vLLM).
# All application inference uses authenticated Codex CLI (run `codex login`).
# Optional Codex model ID; unset uses the CLI default. No provider fallback.
LANG2SQL_LLM_MODEL=
# Per-Codex-call deadline in seconds. Must be positive.
LANG2SQL_LLM_TIMEOUT=180
# OPENAI_API_KEY and LANG2SQL_LLM_BASE_URL no longer select an application backend.

# Fernet key used to encrypt stored secrets (DSNs / API keys) at rest. Optional:
# if unset, a key is auto-generated and persisted in the SQLite kv table. Set it
Expand All @@ -39,6 +32,8 @@ LANG2SQL_SECRET_KEY=
# snowflake://user:pass@account/db/schema?warehouse=wh
# mysql+pymysql://user:pass@host/dbname
# duckdb:////absolute/path/to/file.duckdb
# oracle+oracledb://readonly_user:pass@localhost:1521/?service_name=FREEPDB1
# Oracle driver: uv sync --extra oracle (Thin mode, no Instant Client needed).
LANG2SQL_DB_URL=
# Optional default schema for the SQLAlchemy explorer.
LANG2SQL_DB_SCHEMA=
Expand All @@ -49,3 +44,12 @@ CLOUDFLARE_D1_ACCOUNT_ID=
CLOUDFLARE_D1_DATABASE_ID=
# API token with D1 read access (Account → API Tokens). Required for D1 queries.
CLOUDFLARE_API_TOKEN=

# Codex/MCP identity is fixed at startup (all three required).
# These are trusted local inputs, not an authentication or user-mapping system.
LANG2SQL_USER_ID=
LANG2SQL_ORG_ID=
LANG2SQL_TEAM_ID=
# Shared semantic definitions/audit database; use an absolute path for Codex.
# Defaults to :memory: if unset. Existing stored definitions are reused.
LANG2SQL_DATA_PATH=
19 changes: 19 additions & 0 deletions .github/workflows/codex-mcp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Codex application tests
on:
pull_request:
workflow_dispatch:
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
python: ['3.10', '3.13']
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v6
with:
python-version: ${{ matrix.python }}
- run: uv sync --extra codex --extra oracle --locked
- run: uv run --extra codex --extra oracle pytest -q
- run: uv run --with black black --check src tests bench/codex_oracle_demo.py
- run: uv run --with ruff ruff check --isolated --select E4,E7,E9,F src/lang2sql/adapters/llm/codex.py src/lang2sql/adapters/llm/codex_process.py src/lang2sql/frontends/codex src/lang2sql/frontends/mcp src/lang2sql/tenancy/mcp_runtime.py bench/codex_oracle_demo.py tests/test_codex_mcp.py tests/test_codex_backend.py
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Lang2SQL

## Codex × Oracle 발표·실행

CLI·Discord·문서 추출·스키마 강화·조직 용어 추출이 모두 Codex를 기본 추론 엔진으로 사용합니다. 기존 하네스가 도구 실행·권한·세션을 관리하며 MCP 실행은 선택 가능한 별도 진입점입니다. [설치·실행·검증 안내](docs/codex_oracle_demo.md)와 [새 발표 장표](docs/presentations/Oracle_Lang2SQL_Codex_v2.pptx)를 참고하세요. 기존 CLI/Discord 진입점도 유지합니다.

<div align="center">
<a href="https://pseudo-lab.com"><img src="https://img.shields.io/badge/PseudoLab-S10-3776AB" alt="PseudoLab"/></a>
<a href="https://discord.gg/EPurkHVtp2"><img src="https://img.shields.io/badge/Discord-BF40BF" alt="Discord Community"/></a>
Expand Down Expand Up @@ -69,9 +73,10 @@ Requires Python ≥ 3.10 and [uv](https://docs.astral.sh/uv/).
uv sync # create .venv and install deps
```

### 1. Run the offline demo (no token, no database)
### 1. Authenticate Codex and run the demo

```bash
codex login
.venv/bin/python bench/ecommerce_demo.py
```

Expand All @@ -85,14 +90,15 @@ the safety gate (DROP/INSERT blocked, SELECT passes). See [`bench/README.md`](be
```

The CLI assembles a real `HarnessContext` and runs one turn through the agent
loop. With `OPENAI_API_KEY` set it calls `gpt-4.1-mini`; otherwise it uses the
offline `FakeLLM`.
loop. Every inference call uses Codex CLI structured output. Tool requests are
executed by the existing harness, preserving identity, safety and session history.
Codex must be installed and authenticated; failures propagate without a fake/provider fallback.

### 3. Run the Discord bot

```bash
export DISCORD_BOT_TOKEN=... # required
export OPENAI_API_KEY=... # optional; offline FakeLLM if unset
codex login # required on the service account
export LANG2SQL_SECRET_KEY=... # optional; Fernet key for secret encryption
.venv/bin/lang2sql-bot
```
Expand All @@ -117,10 +123,11 @@ The bot exits loudly if `DISCORD_BOT_TOKEN` is unset. Full setup and hosting:
- Encrypted-at-rest secrets (Fernet) and SQLite-backed persistence.

**Does NOT yet:**
- **Execute against a real database.** `PostgresExplorer` is a **V1 stub** with
canned `orders`/`users` schema and sample rows; real psycopg execution is v1.5.
- **Reason without a key.** Without `OPENAI_API_KEY`, the `FakeLLM` returns
deterministic canned tool cycles — useful for wiring tests, not for answers.
- Without a configured database, `PostgresExplorer` remains an offline stub.
With `LANG2SQL_DB_URL`, SQLAlchemy executes against a real database, including Oracle.
The Codex path requires an explicit DB URL and never falls back to the stub.
- All default application inference requires authenticated Codex CLI.
`FakeLLM` is only an explicitly injected test double; no automatic fallback.
- AST-precise SQL validation, function blocklists, cost gating,
`/semantic diff` / `/semantic promote`, keyword/vector recall,
automatic fact extraction, URL/Notion ingestion — all scoped to v1.5+.
Expand Down
18 changes: 9 additions & 9 deletions bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@

A self-contained walkthrough of Lang2SQL V1's headline value, with **no Discord
token and no live database**. It runs the real shipped modules against the
canned Postgres stub and the offline `FakeLLM`.
canned Postgres stub and authenticated Codex CLI.

## Run

```bash
.venv/bin/python bench/ecommerce_demo.py
```

No environment variables are needed. (If `OPENAI_API_KEY` happens to be set,
Section 0 will route through OpenAI instead of the FakeLLM — everything else is
offline.)
Install Codex CLI and run `codex login`. Section 0 uses real Codex inference;
other sections exercise local federation and safety code.

## What each section proves

| Section | What it shows | Maps to |
|---|---|---|
| **0 — assembled harness** | `ContextConcierge` wires the FakeLLM + canned `PostgresExplorer` + six tools into a `HarnessContext`, and `agent_loop` drives one full LLM→tool→LLM cycle. A *wiring* proof, not an intelligence proof. | harness + concierge |
| **0 — assembled harness** | `ContextConcierge` wires CodexLLM + canned `PostgresExplorer` + nine tools into a `HarnessContext`, and `agent_loop` drives one full LLM→tool→LLM cycle. A *wiring* proof, not an intelligence proof. | harness + concierge |
| **1 — define metrics** | Three e-commerce metrics (`total_revenue`, `aov`, `paid_orders`) are written to the current channel's scope and read back from the effective layer. | ★① context learning |
| **2 — federation (the money shot)** | `active_user` is defined as *"30-day login"* in `#marketing` and *"paid subscriber"* in `#finance`. Each channel resolves its **own** definition by walking its scope chain — same term, two live meanings, **zero conflict**. | **★④ semantic federation** |
| **3 — safety gate** | `DROP`, `INSERT`, and a CTE-hidden `INSERT` are all **blocked fail-closed**; a plain `SELECT ... GROUP BY` **passes**. The read-only guarantee is enforced before any SQL would reach a database. | **★① safety pipeline** |
Expand All @@ -31,9 +30,10 @@ The two headline pillars demonstrated are **★① safety** (Sections 0 + 3) and
- The `PostgresExplorer` is a **V1 stub**: it returns canned `orders`/`users`
schema and sample rows and does not connect to a real database. Real psycopg
execution lands in v1.5.
- The `FakeLLM` is a deterministic stub for offline runs; it does not reason. It
blindly calls the first available tool, which is why Section 0's turn ends at
the safety gate rather than counting orders. With a real key it would call
`gpt-4.1-mini`.
- Codex performs inference while the database remains a canned fixture. Tests explicitly inject FakeLLM at the composition seam to run offline.
- Federation state in Sections 1–2 lives in an in-memory `SemanticStore`; the
SQLite-backed persistent store is wired through the concierge for the bot.

## Oracle 데모

실제 Oracle의 부서별 매출·되묻기·권한 차단 시연: [재현 절차](../docs/oracle_demo.md).
11 changes: 11 additions & 0 deletions bench/codex/results/extraction.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"runtime": "CodexLLM through default ContextConcierge ingestion",
"passed": true,
"candidates": [
{
"name": "매출",
"definition": "orders 테이블의 gross_amount 합계",
"kind": "metric"
}
]
}
Loading
Loading