pythia-plsql 0.13.0__tar.gz → 0.14.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. {pythia_plsql-0.13.0/scripts/pythia_plsql.egg-info → pythia_plsql-0.14.0}/PKG-INFO +1 -1
  2. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/pyproject.toml +1 -1
  3. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/scripts/pythia.py +250 -2
  4. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0/scripts/pythia_plsql.egg-info}/PKG-INFO +1 -1
  5. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/scripts/pythia_plsql.egg-info/SOURCES.txt +1 -0
  6. pythia_plsql-0.14.0/tests/test_mcp.py +309 -0
  7. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/LICENSE +0 -0
  8. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/README.md +0 -0
  9. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/compile-errors.sql +0 -0
  10. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/current-schema.sql +0 -0
  11. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/dependencies.sql +0 -0
  12. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/impact.sql +0 -0
  13. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/invalid-objects.sql +0 -0
  14. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/name-occupants.sql +0 -0
  15. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/object-names.sql +0 -0
  16. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/object-source.sql +0 -0
  17. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/plscope-enabled.sql +0 -0
  18. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/plscope-statements.sql +0 -0
  19. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/plscope-usages.sql +0 -0
  20. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/session-privileges.sql +0 -0
  21. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/similar-candidates.sql +0 -0
  22. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/queries/source.sql +0 -0
  23. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/scripts/pythia_plsql.egg-info/dependency_links.txt +0 -0
  24. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/scripts/pythia_plsql.egg-info/entry_points.txt +0 -0
  25. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/scripts/pythia_plsql.egg-info/requires.txt +0 -0
  26. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/scripts/pythia_plsql.egg-info/top_level.txt +0 -0
  27. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/setup.cfg +0 -0
  28. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-apply/SKILL.md +0 -0
  29. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-comment/SKILL.md +0 -0
  30. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-conventions/SKILL.md +0 -0
  31. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-explore/SKILL.md +0 -0
  32. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-explore/reference/data-dictionary.md +0 -0
  33. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-impact/SKILL.md +0 -0
  34. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-review/SKILL.md +0 -0
  35. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-review/reference/antipatterns.md +0 -0
  36. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-setup/SKILL.md +0 -0
  37. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-skill-author/SKILL.md +0 -0
  38. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-spec/SKILL.md +0 -0
  39. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-write/SKILL.md +0 -0
  40. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/pythia-write/reference/patterns.md +0 -0
  41. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/skills/using-pythia/SKILL.md +0 -0
  42. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/tests/test_install.py +0 -0
  43. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/tests/test_phase1.py +0 -0
  44. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/tests/test_phase2.py +0 -0
  45. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/tests/test_phase3.py +0 -0
  46. {pythia_plsql-0.13.0 → pythia_plsql-0.14.0}/tests/test_phase5.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pythia-plsql
3
- Version: 0.13.0
3
+ Version: 0.14.0
4
4
  Summary: PL/SQL development for AI agents on Oracle Database - expert data-dictionary queries, impact analysis, and a snapshot-verified write path with honest rollback.
5
5
  License: MIT
6
6
  Project-URL: Homepage, https://github.com/thaildhe172591/pythia
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "pythia-plsql"
7
- version = "0.13.0"
7
+ version = "0.14.0"
8
8
  description = "PL/SQL development for AI agents on Oracle Database - expert data-dictionary queries, impact analysis, and a snapshot-verified write path with honest rollback."
9
9
  readme = "README.md"
10
10
  license = { text = "MIT" }
@@ -2921,6 +2921,10 @@ apply -> verify -> report.
2921
2921
  AND to the developer's approval
2922
2922
  journal restore undo, through the same six steps and the same gate
2923
2923
  unistr exact non-ASCII literals for what you are writing
2924
+ mcp Codex only: a write-free MCP approver. The agent
2925
+ calls its pythia_approve tool, Codex shows the card
2926
+ as a select prompt, and the developer's Approve mints
2927
+ the grant. It never writes; apply still verifies it.
2924
2928
  install · agent-user · guide setting the harness itself up
2925
2929
 
2926
2930
  The CLI enforces the gates: headless --yes is refused, policy cannot be
@@ -3392,6 +3396,221 @@ def merge_codex_hooks(path, events=None):
3392
3396
  return path, added
3393
3397
 
3394
3398
 
3399
+ # The MCP approver: a write-free stdio server whose one tool, pythia_approve,
3400
+ # mints the same grant `approve` mints — gated by an MCP elicitation the
3401
+ # developer answers in Codex. Writes never go through here; apply still
3402
+ # verifies the grant. Hand-rolled JSON-RPC (stdlib only).
3403
+ MCP_TOOL = "pythia_approve"
3404
+ MCP_PROTOCOL = "2025-06-18"
3405
+ MCP_ELICIT_METHOD = "elicitation/create" # the one spelling to adjust if a
3406
+ # live Codex disagrees (isolated)
3407
+ _MCP_TOOL_DEF = {
3408
+ "name": MCP_TOOL,
3409
+ "description": ("Ask the developer to approve a pythia apply preview by its "
3410
+ "token. Shows pythia's approval card and mints the one-time "
3411
+ "grant only if the developer picks Approve. Call it after "
3412
+ "`pythia apply <file>` prints a token; then run "
3413
+ "`pythia apply <file> --confirm <token>`."),
3414
+ "inputSchema": {"type": "object",
3415
+ "properties": {"token": {"type": "string"}},
3416
+ "required": ["token"]},
3417
+ }
3418
+
3419
+
3420
+ def _rpc_result(mid, result):
3421
+ return {"jsonrpc": "2.0", "id": mid, "result": result}
3422
+
3423
+
3424
+ def _rpc_error(mid, code, message):
3425
+ return {"jsonrpc": "2.0", "id": mid, "error": {"code": code, "message": message}}
3426
+
3427
+
3428
+ def mcp_handle(msg, state):
3429
+ """Dispatch one incoming JSON-RPC message that is NOT a tools/call (those
3430
+ need the elicitation round-trip and are handled in the loop). Returns
3431
+ (outgoing_messages, done). A notification (no id) is answered with []."""
3432
+ method = msg.get("method")
3433
+ mid = msg.get("id")
3434
+ if method == "initialize":
3435
+ proto = (msg.get("params") or {}).get("protocolVersion") or MCP_PROTOCOL
3436
+ return [_rpc_result(mid, {
3437
+ "protocolVersion": proto,
3438
+ "capabilities": {"tools": {}},
3439
+ "serverInfo": {"name": "pythia",
3440
+ "version": getattr(sys.modules[__name__],
3441
+ "__version__", "0")}})], False
3442
+ if method == "notifications/initialized":
3443
+ return [], False
3444
+ if method == "tools/list":
3445
+ return [_rpc_result(mid, {"tools": [_MCP_TOOL_DEF]})], False
3446
+ if mid is None:
3447
+ return [], False # any other notification
3448
+ return [_rpc_error(mid, -32601, f"method not found: {method}")], False
3449
+
3450
+
3451
+ def _mcp_text(text, is_error=False):
3452
+ return {"content": [{"type": "text", "text": text}], "isError": is_error}
3453
+
3454
+
3455
+ def mcp_approve_decision(root, token, elicit):
3456
+ """Mint on a developer's Approve, delivered through an MCP elicitation.
3457
+
3458
+ Transport-free: `elicit(message, schema) -> (action, content)` performs the
3459
+ round-trip (a fake in tests). The card is pythia's own, built from the
3460
+ journal — so there is nothing for the agent to paraphrase and no verbatim
3461
+ check to run. Fail-closed everywhere: the only path that mints is an
3462
+ explicit accept whose decision is exactly Approve."""
3463
+ token = str(token or "").strip().lower()
3464
+ try:
3465
+ _, meta, body = approval_card(root, token) # refuses unknown/applied
3466
+ except SystemExit as e:
3467
+ return _mcp_text(str(e), is_error=True)
3468
+ message = f"{invocation()} approve {token}\n" + "\n".join(body)
3469
+ schema = {"type": "object",
3470
+ "properties": {"decision": {"type": "string",
3471
+ "enum": ["Approve", "Reject"]}},
3472
+ "required": ["decision"]}
3473
+ try:
3474
+ action, content = elicit(message, schema)
3475
+ except Exception as e: # noqa: BLE001 — fail closed
3476
+ return _mcp_text(f"No grant minted: the approval prompt could not be "
3477
+ f"completed ({e}). Approve in a terminal instead: "
3478
+ f"{invocation()} approve {token}", is_error=True)
3479
+ if action != "accept" or (content or {}).get("decision") != "Approve":
3480
+ return _mcp_text(f"No grant minted for {token}: the developer did not "
3481
+ "approve (answered "
3482
+ f"{(content or {}).get('decision') or action!r}). "
3483
+ "Do not apply; ask what should change.", is_error=True)
3484
+ prune_expired_grants(root)
3485
+ mint_grant(root, token, meta.get("connection") or "",
3486
+ revalidate=fingerprint_text(meta.get("row_set")), approver="mcp")
3487
+ return _mcp_text(f"Approved — grant minted for {token}, single use, expires "
3488
+ f"in {GRANT_TTL_MINUTES} minutes. Now run: "
3489
+ f"{invocation()} apply <file> --confirm {token}")
3490
+
3491
+
3492
+ def mcp_serve(reader, writer, root):
3493
+ """The stdio loop. Newline-delimited JSON-RPC in and out. A tools/call for
3494
+ pythia_approve runs the elicitation round-trip inline: send
3495
+ elicitation/create, then read until the matching-id response. One call in
3496
+ flight at a time (Codex calls tools serially in a turn)."""
3497
+ counter = [1000]
3498
+
3499
+ def send(msg):
3500
+ writer.write(json.dumps(msg) + "\n")
3501
+ writer.flush()
3502
+
3503
+ def read_msg():
3504
+ line = reader.readline()
3505
+ if not line:
3506
+ return None
3507
+ try:
3508
+ return json.loads(line)
3509
+ except ValueError:
3510
+ return {} # skip a malformed line
3511
+
3512
+ def elicit(message, schema):
3513
+ counter[0] += 1
3514
+ eid = counter[0]
3515
+ send({"jsonrpc": "2.0", "id": eid, "method": MCP_ELICIT_METHOD,
3516
+ "params": {"message": message, "requestedSchema": schema}})
3517
+ while True:
3518
+ m = read_msg()
3519
+ if m is None:
3520
+ raise RuntimeError("client closed before answering the elicitation")
3521
+ if m.get("id") == eid and ("result" in m or "error" in m):
3522
+ if "error" in m:
3523
+ raise RuntimeError(str(m["error"]))
3524
+ r = m["result"] or {}
3525
+ return r.get("action"), r.get("content") or {}
3526
+ # ignore anything else that arrives mid-elicitation
3527
+
3528
+ while True:
3529
+ msg = read_msg()
3530
+ if msg is None:
3531
+ return
3532
+ if msg.get("method") == "tools/call":
3533
+ mid = msg.get("id")
3534
+ params = msg.get("params") or {}
3535
+ if params.get("name") != MCP_TOOL:
3536
+ send(_rpc_error(mid, -32601, f"no such tool: {params.get('name')}"))
3537
+ continue
3538
+ token = (params.get("arguments") or {}).get("token", "")
3539
+ result = mcp_approve_decision(root, token, elicit)
3540
+ send(_rpc_result(mid, result))
3541
+ continue
3542
+ out, _ = mcp_handle(msg, {})
3543
+ for m in out:
3544
+ send(m)
3545
+
3546
+
3547
+ def cmd_mcp(conn, schema, ns):
3548
+ """Run the MCP approver on stdio. Launched by Codex per its
3549
+ [mcp_servers.pythia] config; lives for the session. Touches no database."""
3550
+ mcp_serve(sys.stdin, sys.stdout, ns.project_root)
3551
+
3552
+
3553
+ # Codex config is TOML, and the stdlib cannot parse or write it on 3.9
3554
+ # (tomllib is read-only, 3.11+). So these merges never parse TOML: they append
3555
+ # a marker-delimited block when the target tables are absent, and refuse
3556
+ # (writing nothing) when a table already exists — the installer then prints
3557
+ # what to add. ponytail: literal-text collision check, not a TOML engine; a
3558
+ # table header inside a string/comment is not worth a parser here.
3559
+ CODEX_MCP_BEGIN = "# pythia:begin (managed by `pythia install`)"
3560
+ CODEX_MCP_END = "# pythia:end"
3561
+ CODEX_MCP_BLOCK = (f"{CODEX_MCP_BEGIN}\n"
3562
+ "[mcp_servers.pythia]\n"
3563
+ 'command = "python"\n'
3564
+ 'args = ["-m", "pythia", "mcp"]\n\n'
3565
+ "[approval_policy.granular]\n"
3566
+ "mcp_elicitations = true\n"
3567
+ f"{CODEX_MCP_END}")
3568
+ # requirements.toml is a separate admin-enforced file; forbidding these two is
3569
+ # what closes the danger-full-access elicitation auto-approve hole.
3570
+ CODEX_REQUIREMENTS = (f"{CODEX_MCP_BEGIN}\n"
3571
+ "# pythia needs elicitations to reach a human, so the two\n"
3572
+ "# modes that would auto-answer or skip them are disallowed.\n"
3573
+ 'allowed_approval_policies = ["untrusted", "on-request", "on-failure"]\n'
3574
+ 'allowed_sandbox_modes = ["read-only", "workspace-write"]\n'
3575
+ f"{CODEX_MCP_END}")
3576
+
3577
+
3578
+ def merge_codex_mcp_config(path):
3579
+ """Append the MCP block to <project>/.codex/config.toml. Returns
3580
+ (path, action): 'created' | 'appended' | 'present' | 'conflict'. Refuses
3581
+ (writes nothing) if either table already exists without our marker."""
3582
+ path = pathlib.Path(path)
3583
+ old = path.read_text(encoding="utf-8") if path.is_file() else None
3584
+ if old is not None and CODEX_MCP_BEGIN in old:
3585
+ return path, "present"
3586
+ if old is not None and ("[mcp_servers.pythia]" in old
3587
+ or "[approval_policy.granular]" in old):
3588
+ return path, "conflict"
3589
+ if old is None:
3590
+ new, action = CODEX_MCP_BLOCK + "\n", "created"
3591
+ else:
3592
+ tail = "" if old.endswith("\n\n") else "\n" if old.endswith("\n") else "\n\n"
3593
+ new, action = old + tail + CODEX_MCP_BLOCK + "\n", "appended"
3594
+ path.parent.mkdir(parents=True, exist_ok=True)
3595
+ path.write_text(new, encoding="utf-8")
3596
+ return path, action
3597
+
3598
+
3599
+ def merge_codex_requirements(path):
3600
+ """Write <project>/.codex/requirements.toml forbidding danger-full-access
3601
+ and never-approval. Returns (path, action): 'created' | 'present' |
3602
+ 'conflict'. An existing file without our marker is left untouched."""
3603
+ path = pathlib.Path(path)
3604
+ old = path.read_text(encoding="utf-8") if path.is_file() else None
3605
+ if old is not None and CODEX_MCP_BEGIN in old:
3606
+ return path, "present"
3607
+ if old is not None:
3608
+ return path, "conflict"
3609
+ path.parent.mkdir(parents=True, exist_ok=True)
3610
+ path.write_text(CODEX_REQUIREMENTS + "\n", encoding="utf-8")
3611
+ return path, "created"
3612
+
3613
+
3395
3614
  def install_claude_hooks(base_dir, events=None):
3396
3615
  """Merge the two hooks and the deny rule into <base>/.claude/settings.json
3397
3616
  — the project's, or the home directory's for -g. The file is Claude
@@ -3473,6 +3692,31 @@ def report_codex_hooks(project_root, ns):
3473
3692
  print(f"\nCodex session-start hook already in {path}.")
3474
3693
 
3475
3694
 
3695
+ def report_codex_mcp(project_root, ns):
3696
+ """Project scope: register the write-free MCP approver in Codex's config and
3697
+ pin the sandbox so elicitations always reach a human. --no-hooks skips it,
3698
+ as with the other Codex wiring. Needs Codex >= v0.120; older Codex ignores
3699
+ the server and the console approve path still works."""
3700
+ if getattr(ns, "no_hooks", False):
3701
+ return
3702
+ base = pathlib.Path(project_root) / ".codex"
3703
+ cfg_path, cfg = merge_codex_mcp_config(base / "config.toml")
3704
+ req_path, req = merge_codex_requirements(base / "requirements.toml")
3705
+ if cfg in ("created", "appended"):
3706
+ print(f"\nRegistered the pythia MCP approver in {cfg_path} "
3707
+ "(Codex >= v0.120; run /mcp to trust it).")
3708
+ elif cfg == "conflict":
3709
+ print(f"\n! {cfg_path} already defines [mcp_servers.pythia] or "
3710
+ "[approval_policy.granular]; add this yourself:\n"
3711
+ + CODEX_MCP_BLOCK)
3712
+ if req == "created":
3713
+ print(f"Pinned the sandbox in {req_path} (no danger-full-access, so "
3714
+ "elicitations always ask a human).")
3715
+ elif req == "conflict":
3716
+ print(f"\n! {req_path} already exists; add these constraints to it:\n"
3717
+ + CODEX_REQUIREMENTS)
3718
+
3719
+
3476
3720
  def cmd_install(conn, schema, ns):
3477
3721
  import shutil
3478
3722
  en = getattr(ns, "color", False)
@@ -3516,6 +3760,7 @@ def cmd_install(conn, schema, ns):
3516
3760
  report_claude_hooks(ns.project_root, ns)
3517
3761
  report_codex_agents_md(ns.project_root)
3518
3762
  report_codex_hooks(ns.project_root, ns)
3763
+ report_codex_mcp(ns.project_root, ns)
3519
3764
  print(f"\nNext: fill in {path}")
3520
3765
  print(f"Then: {invocation()} check")
3521
3766
  scripts_dir = installed_scripts_dir()
@@ -3536,10 +3781,10 @@ COMMANDS = {"check": cmd_check, "ls": cmd_ls, "src": cmd_src, "args": cmd_args,
3536
3781
  "approve": cmd_approve,
3537
3782
  "conventions": cmd_conventions, "guide": cmd_guide, "connections": cmd_connections, "install": cmd_install,
3538
3783
  "unistr": cmd_unistr, "agent-user": cmd_agent_user,
3539
- "history": cmd_history}
3784
+ "history": cmd_history, "mcp": cmd_mcp}
3540
3785
 
3541
3786
  NO_DB_COMMANDS = {"policy", "journal", "install", "unistr", "guide", "connections",
3542
- "history", "approve"}
3787
+ "history", "approve", "mcp"}
3543
3788
 
3544
3789
 
3545
3790
  # --- CLI ---------------------------------------------------------------------
@@ -3683,6 +3928,9 @@ def build_parser():
3683
3928
  s.add_argument("text", nargs="*", help="text; omit to read stdin")
3684
3929
  s.add_argument("--loi", action="store_true",
3685
3930
  help="wrap as 'loi:'||unistr(...)||':loi'")
3931
+ sub.add_parser("mcp", parents=[common()],
3932
+ help="run the MCP approver for Codex (stdio; Codex launches "
3933
+ "it — the developer approves via an elicitation)")
3686
3934
  s = sub.add_parser("install", parents=[common()],
3687
3935
  help="install the skill pack, scaffold .pythia/ config, wire the Claude Code hooks")
3688
3936
  s.add_argument("-g", "--global", dest="glob", action="store_true",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pythia-plsql
3
- Version: 0.13.0
3
+ Version: 0.14.0
4
4
  Summary: PL/SQL development for AI agents on Oracle Database - expert data-dictionary queries, impact analysis, and a snapshot-verified write path with honest rollback.
5
5
  License: MIT
6
6
  Project-URL: Homepage, https://github.com/thaildhe172591/pythia
@@ -37,6 +37,7 @@ skills/pythia-write/SKILL.md
37
37
  skills/pythia-write/reference/patterns.md
38
38
  skills/using-pythia/SKILL.md
39
39
  tests/test_install.py
40
+ tests/test_mcp.py
40
41
  tests/test_phase1.py
41
42
  tests/test_phase2.py
42
43
  tests/test_phase3.py
@@ -0,0 +1,309 @@
1
+ #!/usr/bin/env python3
2
+ """MCP approver self-checks — no database, no network, no live Codex.
3
+
4
+ Run: python tests/test_mcp.py
5
+ """
6
+ import io
7
+ import json
8
+ import os
9
+ import pathlib
10
+ import sys
11
+ import tempfile
12
+
13
+ os.environ.setdefault("PYTHIA_CI", "1")
14
+ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent / "scripts"))
15
+ import pythia # noqa: E402
16
+
17
+
18
+ # --- Task 1: transport handshake + tools/list -------------------------------
19
+
20
+ def test_initialize_echoes_protocol_and_declares_tools():
21
+ out, done = pythia.mcp_handle(
22
+ {"jsonrpc": "2.0", "id": 1, "method": "initialize",
23
+ "params": {"protocolVersion": "2025-06-18", "capabilities": {},
24
+ "clientInfo": {"name": "codex", "version": "0.120.0"}}},
25
+ {})
26
+ assert done is False
27
+ assert len(out) == 1
28
+ r = out[0]
29
+ assert r["id"] == 1 and r["jsonrpc"] == "2.0"
30
+ assert r["result"]["protocolVersion"] == "2025-06-18" # echo client's
31
+ assert "tools" in r["result"]["capabilities"]
32
+ assert r["result"]["serverInfo"]["name"] == "pythia"
33
+
34
+
35
+ def test_initialized_notification_is_silent():
36
+ out, done = pythia.mcp_handle(
37
+ {"jsonrpc": "2.0", "method": "notifications/initialized"}, {})
38
+ assert out == [] and done is False
39
+
40
+
41
+ def test_tools_list_returns_only_pythia_approve():
42
+ out, _ = pythia.mcp_handle(
43
+ {"jsonrpc": "2.0", "id": 2, "method": "tools/list"}, {})
44
+ tools = out[0]["result"]["tools"]
45
+ assert [t["name"] for t in tools] == ["pythia_approve"]
46
+ schema = tools[0]["inputSchema"]
47
+ assert schema["properties"]["token"]["type"] == "string"
48
+ assert schema["required"] == ["token"]
49
+
50
+
51
+ def test_unknown_method_with_id_is_an_error_not_a_crash():
52
+ out, _ = pythia.mcp_handle(
53
+ {"jsonrpc": "2.0", "id": 3, "method": "resources/list"}, {})
54
+ assert out[0]["error"]["code"] == -32601 # method not found
55
+ assert out[0]["id"] == 3
56
+
57
+
58
+ # --- Task 2: the elicitation round-trip and the mint (fail-closed) ----------
59
+
60
+ def _preview(root, token="a1b2c3"):
61
+ """A pending plsql_source preview in the journal, the way apply writes one."""
62
+ pythia.write_journal_entry(
63
+ root, "PACKAGE BODY", "PKG_ORDER", "old\n",
64
+ "CREATE OR REPLACE PACKAGE BODY pkg_order AS\n new;\nEND;\n",
65
+ {"token": token, "connection": "DEV", "schema": "APP",
66
+ "group": "plsql_source", "applied": False})
67
+ return token
68
+
69
+
70
+ def test_mint_only_on_accept_with_approve():
71
+ with tempfile.TemporaryDirectory() as td:
72
+ token = _preview(td)
73
+ seen = {}
74
+
75
+ def elicit(message, schema):
76
+ seen["message"] = message
77
+ seen["schema"] = schema
78
+ return "accept", {"decision": "Approve"}
79
+
80
+ res = pythia.mcp_approve_decision(td, token, elicit)
81
+ assert res["isError"] is False
82
+ assert token in res["content"][0]["text"]
83
+ assert "PKG_ORDER" in seen["message"] # the card was shown
84
+ assert seen["schema"]["properties"]["decision"]["enum"] == ["Approve", "Reject"]
85
+ g = pythia.read_grant(td, token)
86
+ assert g and g["approver"] == "mcp" and g["used_at"] is None
87
+
88
+
89
+ def test_no_mint_on_reject_decline_or_cancel():
90
+ for action, content in (("accept", {"decision": "Reject"}),
91
+ ("decline", {}), ("cancel", {})):
92
+ with tempfile.TemporaryDirectory() as td:
93
+ token = _preview(td)
94
+ res = pythia.mcp_approve_decision(td, token, lambda m, s: (action, content))
95
+ assert res["isError"] is True
96
+ assert pythia.read_grant(td, token) is None # nothing minted
97
+
98
+
99
+ def test_no_mint_for_unknown_token_and_elicit_is_not_even_called():
100
+ with tempfile.TemporaryDirectory() as td:
101
+ called = []
102
+ res = pythia.mcp_approve_decision(
103
+ td, "nope99",
104
+ lambda m, s: called.append(1) or ("accept", {"decision": "Approve"}))
105
+ assert res["isError"] is True
106
+ assert called == [] # refused before asking
107
+ assert pythia.read_grant(td, "nope99") is None
108
+
109
+
110
+ def test_elicit_error_fails_closed():
111
+ with tempfile.TemporaryDirectory() as td:
112
+ token = _preview(td)
113
+
114
+ def boom(message, schema):
115
+ raise RuntimeError("transport died")
116
+
117
+ res = pythia.mcp_approve_decision(td, token, boom)
118
+ assert res["isError"] is True
119
+ assert pythia.read_grant(td, token) is None
120
+
121
+
122
+ # --- Task 3: the server loop, the mcp subcommand, and the guide -------------
123
+
124
+ class ScriptedClient:
125
+ """Feeds the server initialize -> tools/call, then answers the server's
126
+ elicitation/create with a fixed (action, decision). Acts as both reader
127
+ and writer for mcp_serve."""
128
+ def __init__(self, token, action="accept", decision="Approve"):
129
+ self.out = io.StringIO()
130
+ self.answered = False
131
+ self.action, self.decision = action, decision
132
+ self._pending = [
133
+ {"jsonrpc": "2.0", "id": 1, "method": "initialize",
134
+ "params": {"protocolVersion": "2025-06-18", "capabilities": {}}},
135
+ {"jsonrpc": "2.0", "id": 2, "method": "tools/call",
136
+ "params": {"name": "pythia_approve", "arguments": {"token": token}}},
137
+ ]
138
+
139
+ def readline(self):
140
+ if self._pending:
141
+ return json.dumps(self._pending.pop(0)) + "\n"
142
+ if not self.answered:
143
+ for line in reversed(self.out.getvalue().splitlines()):
144
+ m = json.loads(line)
145
+ if m.get("method") == pythia.MCP_ELICIT_METHOD:
146
+ self.answered = True
147
+ return json.dumps({"jsonrpc": "2.0", "id": m["id"],
148
+ "result": {"action": self.action,
149
+ "content": {"decision": self.decision}}}) + "\n"
150
+ return "" # EOF -> server loop ends
151
+
152
+ def write(self, s):
153
+ self.out.write(s)
154
+
155
+ def flush(self):
156
+ pass
157
+
158
+ def messages(self):
159
+ return [json.loads(l) for l in self.out.getvalue().splitlines() if l.strip()]
160
+
161
+
162
+ def test_serve_end_to_end_mints_on_scripted_approve():
163
+ with tempfile.TemporaryDirectory() as td:
164
+ token = _preview(td)
165
+ client = ScriptedClient(token)
166
+ pythia.mcp_serve(client, client, td)
167
+ msgs = client.messages()
168
+ assert any(m.get("result", {}).get("serverInfo", {}).get("name") == "pythia"
169
+ for m in msgs)
170
+ assert any(m.get("method") == pythia.MCP_ELICIT_METHOD for m in msgs)
171
+ call_result = [m for m in msgs if m.get("id") == 2 and "result" in m][-1]
172
+ assert call_result["result"]["isError"] is False
173
+ assert pythia.read_grant(td, token)["approver"] == "mcp"
174
+
175
+
176
+ def test_serve_fails_closed_on_scripted_decline():
177
+ with tempfile.TemporaryDirectory() as td:
178
+ token = _preview(td)
179
+ client = ScriptedClient(token, action="decline", decision="")
180
+ pythia.mcp_serve(client, client, td)
181
+ call_result = [m for m in client.messages()
182
+ if m.get("id") == 2 and "result" in m][-1]
183
+ assert call_result["result"]["isError"] is True
184
+ assert pythia.read_grant(td, token) is None
185
+
186
+
187
+ def test_mcp_is_a_no_db_command_in_the_guide():
188
+ assert "mcp" in pythia.COMMANDS
189
+ assert "mcp" in pythia.NO_DB_COMMANDS
190
+ assert "mcp" in pythia.OPERATING_GUIDE
191
+
192
+
193
+ # --- Task 4: config.toml and requirements.toml merges (TOML-safe) -----------
194
+
195
+ def test_merge_codex_mcp_config_appends_once_when_clean():
196
+ with tempfile.TemporaryDirectory() as td:
197
+ path = pathlib.Path(td) / ".codex" / "config.toml"
198
+ p, action = pythia.merge_codex_mcp_config(path)
199
+ assert p == path and action == "created"
200
+ body = path.read_text(encoding="utf-8")
201
+ assert "[mcp_servers.pythia]" in body
202
+ assert 'args = ["-m", "pythia", "mcp"]' in body
203
+ assert "mcp_elicitations = true" in body
204
+ assert pythia.merge_codex_mcp_config(path)[1] == "present" # idempotent
205
+
206
+
207
+ def test_merge_codex_mcp_config_preserves_other_content():
208
+ with tempfile.TemporaryDirectory() as td:
209
+ path = pathlib.Path(td) / ".codex" / "config.toml"
210
+ path.parent.mkdir(parents=True)
211
+ path.write_text('model = "gpt-5"\n', encoding="utf-8")
212
+ _, action = pythia.merge_codex_mcp_config(path)
213
+ assert action == "appended"
214
+ body = path.read_text(encoding="utf-8")
215
+ assert body.startswith('model = "gpt-5"\n') # kept
216
+ assert "[mcp_servers.pythia]" in body
217
+
218
+
219
+ def test_merge_codex_mcp_config_refuses_on_table_collision():
220
+ with tempfile.TemporaryDirectory() as td:
221
+ path = pathlib.Path(td) / ".codex" / "config.toml"
222
+ path.parent.mkdir(parents=True)
223
+ original = "[approval_policy.granular]\nmcp_elicitations = false\n"
224
+ path.write_text(original, encoding="utf-8")
225
+ p, action = pythia.merge_codex_mcp_config(path)
226
+ assert action == "conflict"
227
+ assert path.read_text(encoding="utf-8") == original # untouched
228
+
229
+
230
+ def test_merge_codex_requirements_creates_and_is_idempotent():
231
+ with tempfile.TemporaryDirectory() as td:
232
+ path = pathlib.Path(td) / ".codex" / "requirements.toml"
233
+ p, action = pythia.merge_codex_requirements(path)
234
+ assert action == "created"
235
+ body = path.read_text(encoding="utf-8")
236
+ assert "allowed_approval_policies" in body and "allowed_sandbox_modes" in body
237
+ assert "danger-full-access" not in body # the forbidden mode is absent
238
+ assert '"never"' not in body # the forbidden policy is absent
239
+ assert pythia.merge_codex_requirements(path)[1] == "present"
240
+
241
+
242
+ def test_merge_codex_requirements_refuses_an_existing_file():
243
+ with tempfile.TemporaryDirectory() as td:
244
+ path = pathlib.Path(td) / ".codex" / "requirements.toml"
245
+ path.parent.mkdir(parents=True)
246
+ original = "# org policy\nallowed_sandbox_modes = []\n"
247
+ path.write_text(original, encoding="utf-8")
248
+ p, action = pythia.merge_codex_requirements(path)
249
+ assert action == "conflict"
250
+ assert path.read_text(encoding="utf-8") == original # untouched
251
+
252
+
253
+ # --- Task 5: wire the MCP install into cmd_install (project scope) ----------
254
+
255
+ def test_cmd_install_registers_the_mcp_server_and_pins_the_sandbox():
256
+ import argparse
257
+ import contextlib
258
+ old = os.environ.get("PATH")
259
+ os.environ["PATH"] = ""
260
+ try:
261
+ with tempfile.TemporaryDirectory() as td:
262
+ ns = argparse.Namespace(project_root=td, glob=False, source=None,
263
+ color=False, json=False, no_hooks=False)
264
+ with contextlib.redirect_stdout(io.StringIO()):
265
+ pythia.cmd_install(None, None, ns)
266
+ cfg = pathlib.Path(td) / ".codex" / "config.toml"
267
+ req = pathlib.Path(td) / ".codex" / "requirements.toml"
268
+ assert "[mcp_servers.pythia]" in cfg.read_text(encoding="utf-8")
269
+ assert "mcp_elicitations = true" in cfg.read_text(encoding="utf-8")
270
+ assert "allowed_sandbox_modes" in req.read_text(encoding="utf-8")
271
+ finally:
272
+ if old is not None:
273
+ os.environ["PATH"] = old
274
+
275
+
276
+ def test_cmd_install_no_hooks_skips_the_mcp_files():
277
+ import argparse
278
+ import contextlib
279
+ old = os.environ.get("PATH")
280
+ os.environ["PATH"] = ""
281
+ try:
282
+ with tempfile.TemporaryDirectory() as td:
283
+ ns = argparse.Namespace(project_root=td, glob=False, source=None,
284
+ color=False, json=False, no_hooks=True)
285
+ with contextlib.redirect_stdout(io.StringIO()):
286
+ pythia.cmd_install(None, None, ns)
287
+ assert not (pathlib.Path(td) / ".codex" / "config.toml").exists()
288
+ finally:
289
+ if old is not None:
290
+ os.environ["PATH"] = old
291
+
292
+
293
+ def main():
294
+ failed = 0
295
+ for name, fn in sorted(globals().items()):
296
+ if name.startswith("test_") and callable(fn):
297
+ try:
298
+ fn()
299
+ print(f"PASS {name}")
300
+ except (Exception, SystemExit) as e: # noqa: BLE001 — keep going
301
+ failed += 1
302
+ print(f"FAIL {name}: {e!r}")
303
+ if failed:
304
+ sys.exit(f"{failed} test(s) failed")
305
+ print("OK")
306
+
307
+
308
+ if __name__ == "__main__":
309
+ main()
File without changes
File without changes
File without changes