From e918cfd7a1510920eee7d5ab05ea66881afe5736 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 21:10:41 -0600 Subject: [PATCH] fix(openai-cred-watchdog): check openai.main instead of retired openai provider (#5250) --- .../openai-cred-watchdog.py | 15 ++- .../openai-cred-watchdog.test.py | 111 ++++++++++++++++++ 2 files changed, 121 insertions(+), 5 deletions(-) create mode 100755 nixos/services/openai-cred-watchdog/openai-cred-watchdog.test.py diff --git a/nixos/services/openai-cred-watchdog/openai-cred-watchdog.py b/nixos/services/openai-cred-watchdog/openai-cred-watchdog.py index e71ab1efa6..d8e8e43f80 100755 --- a/nixos/services/openai-cred-watchdog/openai-cred-watchdog.py +++ b/nixos/services/openai-cred-watchdog/openai-cred-watchdog.py @@ -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] @@ -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): @@ -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( diff --git a/nixos/services/openai-cred-watchdog/openai-cred-watchdog.test.py b/nixos/services/openai-cred-watchdog/openai-cred-watchdog.test.py new file mode 100755 index 0000000000..48f460c412 --- /dev/null +++ b/nixos/services/openai-cred-watchdog/openai-cred-watchdog.test.py @@ -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)