Skip to content
Merged
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
15 changes: 10 additions & 5 deletions nixos/services/openai-cred-watchdog/openai-cred-watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
"""Watchdog: OpenAI credential invariant for the laptop OpenCode store.

Invariant (recurrence guard for developing-today/code#3216):
- provider `openai`: NO credential row with method chatgpt-token-sharing
may exist, and the ACTIVE credential method must be chatgpt-browser
or chatgpt-headless.
- provider `openai.main`: NO credential row with method
chatgpt-token-sharing may exist, and the ACTIVE credential method
must be chatgpt-browser or chatgpt-headless.
- provider `openai.alternate`: the ACTIVE credential method must be
chatgpt-browser or chatgpt-headless.

The main account was renamed `openai` -> `openai.main` in PR #5085;
the retired `openai` identity is deliberately NOT checked (issue
#5250): a store still carrying only `openai` rows reports
`openai.main: no credential rows found` rather than passing.

Read-only. Never prints token values (only credential ids, labels, methods).
Exit: 0 healthy, 1 violation, 2 check error.
Usage: openai-cred-watchdog.py [path-to-opencode.db]
Expand All @@ -21,7 +26,7 @@
DEFAULT_DB = os.path.expanduser("~/.local/share/opencode/opencode.db")
ALLOWED_ACTIVE_METHODS = {"chatgpt-browser", "chatgpt-headless"}
BANNED_METHOD = "chatgpt-token-sharing"
PROVIDERS = ("openai", "openai.alternate")
PROVIDERS = ("openai.main", "openai.alternate")


def method_of(value_json):
Expand Down Expand Up @@ -53,7 +58,7 @@ def check(db_path):
if not creds:
violations.append(f"{provider}: no credential rows found")
continue
if provider == "openai":
if provider == "openai.main":
for cred_id, label, active, method in creds:
if method == BANNED_METHOD:
violations.append(
Expand Down
111 changes: 111 additions & 0 deletions nixos/services/openai-cred-watchdog/openai-cred-watchdog.test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env python3
"""Tests for openai-cred-watchdog.py (issue #5250).

The main OpenAI account was renamed `openai` -> `openai.main` in
PR #5085, but the watchdog kept checking the retired `openai`
provider: it permanently reported `openai: no credential rows found`
and never applied the banned chatgpt-token-sharing check to the
renamed account. These tests drive the REAL watchdog module against
fixture credential stores covering the renamed-provider case:

- a healthy store keyed by `openai.main` passes;
- a banned token-sharing row on `openai.main` is flagged (even when
inactive — the invariant is that no such row may exist);
- a store carrying only the retired `openai` identity does NOT
pass: the watchdog reports `openai.main` (and `openai.alternate`)
as missing instead of being satisfied by the retired rows.

Run: python3 openai-cred-watchdog.test.py
"""

import importlib.util
import json
import os
import sqlite3
import sys
import tempfile
import unittest

SELF_DIR = os.path.dirname(os.path.abspath(__file__))
SPEC = importlib.util.spec_from_file_location(
"openai_cred_watchdog", os.path.join(SELF_DIR, "openai-cred-watchdog.py")
)
watchdog = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(watchdog)


def make_db(rows):
"""rows: list of (integration_id, active, methodID). Returns db path."""
fd, path = tempfile.mkstemp(suffix=".db")
os.close(fd)
con = sqlite3.connect(path)
try:
con.execute(
"CREATE TABLE credential ("
"id INTEGER PRIMARY KEY, integration_id TEXT, label TEXT, "
"active INTEGER, value TEXT)"
)
for i, (provider, active, method) in enumerate(rows, start=1):
con.execute(
"INSERT INTO credential VALUES (?, ?, ?, ?, ?)",
(
i,
provider,
f"{provider}-cred-{i}",
1 if active else 0,
json.dumps({"methodID": method}),
),
)
con.commit()
finally:
con.close()
return path


class WatchdogRenameTests(unittest.TestCase):
def check_rows(self, rows):
path = make_db(rows)
try:
return watchdog.check(path)
finally:
os.unlink(path)

def test_providers_are_the_renamed_identities(self):
self.assertEqual(watchdog.PROVIDERS, ("openai.main", "openai.alternate"))

def test_healthy_renamed_store_passes(self):
violations = self.check_rows(
[
("openai.main", True, "chatgpt-browser"),
("openai.alternate", True, "chatgpt-headless"),
]
)
self.assertEqual(violations, [])

def test_banned_method_on_openai_main_is_flagged(self):
violations = self.check_rows(
[
("openai.main", True, "chatgpt-browser"),
("openai.main", False, "chatgpt-token-sharing"),
("openai.alternate", True, "chatgpt-headless"),
]
)
self.assertTrue(
any(
v.startswith("openai.main:") and "banned" in v for v in violations
),
f"expected a banned-method violation for openai.main, got {violations}",
)

def test_retired_openai_only_store_does_not_pass(self):
violations = self.check_rows(
[
("openai", True, "chatgpt-browser"),
("openai.alternate", True, "chatgpt-headless"),
]
)
self.assertIn("openai.main: no credential rows found", violations)


if __name__ == "__main__":
unittest.main(verbosity=2)