Skip to content

Commit bb2a87f

Browse files
authored
Merge pull request #148 from ndycode/audit/current-structure-cleanup-20260425
Audit current structure cleanup
2 parents 3331324 + 1667c0a commit bb2a87f

71 files changed

Lines changed: 1405 additions & 14051 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ jobs:
5353
- name: Build
5454
run: npm run build
5555

56-
audit-prod:
57-
name: Production dependency audit
56+
audit-ci:
57+
name: Dependency audit
5858
runs-on: ubuntu-latest
5959
steps:
6060
- name: Checkout
@@ -71,5 +71,5 @@ jobs:
7171
- name: Install dependencies
7272
run: npm ci
7373

74-
- name: Audit production dependencies (high+)
75-
run: npm run audit:prod
74+
- name: Audit dependencies and dev allowlist
75+
run: npm run audit:ci

AGENTS.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PROJECT KNOWLEDGE BASE
22

3-
Generated: 2026-02-03
4-
Commit: 461ea2b
3+
Generated: 2026-04-25
4+
Source baseline: 3331324cb14d2b80dd8dfb424619870a88476706
55
Branch: main
66

77
## OVERVIEW
@@ -10,7 +10,7 @@ OpenCode plugin: intercepts OpenAI SDK calls, routes through ChatGPT Codex backe
1010
## STRUCTURE
1111
```
1212
./
13-
├── index.ts # plugin entry (7-step fetch pipeline, tool registration)
13+
├── index.ts # plugin entry (context wiring + 5-stage fetch pipeline)
1414
├── lib/ # core logic (see lib/AGENTS.md)
1515
├── test/ # vitest suites (see test/AGENTS.md)
1616
├── scripts/ # install + build helpers
@@ -23,11 +23,12 @@ OpenCode plugin: intercepts OpenAI SDK calls, routes through ChatGPT Codex backe
2323
## WHERE TO LOOK
2424
| Task | Location | Notes |
2525
| --- | --- | --- |
26-
| Plugin orchestration | `index.ts` | 7-step request pipeline, tool registration |
26+
| Plugin orchestration | `index.ts` | 5-stage request pipeline: URL rewrite, body transform, OAuth headers, SSE conversion, error handling |
27+
| Tool registry | `lib/tools/index.ts` + `lib/tools/codex-*.ts` | 21 registered `codex-*` tools |
2728
| OAuth flow + PKCE | `lib/auth/auth.ts` | token refresh, JWT decode |
2829
| OAuth callback server | `lib/auth/server.ts` | binds port 1455 |
2930
| Multi-account rotation | `lib/accounts.ts` | health scoring, cooldown, selection |
30-
| Account storage | `lib/storage.ts` | V3 format, per-project/global paths |
31+
| Account storage | `lib/storage.ts` + `lib/storage/` | V3 facade, per-project/global paths, keychain, import/export |
3132
| Request transformation | `lib/request/request-transformer.ts` | model normalization, prompt injection |
3233
| Headers + rate limits | `lib/request/fetch-helpers.ts` | Codex headers, error mapping |
3334
| SSE to JSON | `lib/request/response-handler.ts` | stream parsing |
@@ -37,13 +38,13 @@ OpenCode plugin: intercepts OpenAI SDK calls, routes through ChatGPT Codex backe
3738
| Graceful shutdown | `lib/shutdown.ts` | cleanup on process exit |
3839
| Health monitoring | `lib/health.ts` | account health status |
3940
| Circuit breaker | `lib/circuit-breaker.ts` | failure isolation |
40-
| Tests | `test/` | vitest globals, 80% coverage threshold |
41+
| Tests | `test/` | vitest globals, coverage threshold gate |
4142

4243
## CONVENTIONS
4344
- Source: root `index.ts` + `lib/`; `dist/` is generated output.
4445
- ESLint flat config: `no-explicit-any` enforced, unused args prefixed `_`.
4546
- Tests relax lint rules (see `eslint.config.js`).
46-
- Build emits `dist/lib/oauth-success.html` from the TypeScript source via `scripts/copy-oauth-success.js`.
47+
- Build cleans `dist/`, then emits `dist/lib/oauth-success.html` from the TypeScript source via `scripts/copy-oauth-success.js`.
4748
- ESM only (`"type": "module"`), Node >= 18.
4849

4950
## ANTI-PATTERNS (THIS PROJECT)
@@ -54,9 +55,11 @@ OpenCode plugin: intercepts OpenAI SDK calls, routes through ChatGPT Codex backe
5455

5556
## COMMANDS
5657
```bash
57-
npm run build # tsc + copy oauth-success.html
58+
npm run build # clean dist + tsc + copy oauth-success.html
5859
npm run typecheck # type checking only
5960
npm test # vitest once
61+
npm run test:coverage # vitest coverage
62+
npm run audit:ci # prod audit + dev allowlist
6063
npm run test:watch # vitest watch mode
6164
npm run lint # eslint
6265
```
@@ -82,7 +85,7 @@ Skills to load when delegating tasks in this codebase.
8285
|-------|---------------|
8386
| `typescript-senior` | Strict mode, template literal types (`QuotaKey`), discriminated unions (`TokenResult`), Zod inference |
8487
| `node-backend` | ESM-first, `node:crypto`, Buffer API, async patterns |
85-
| `testing-js` | Vitest with 80% coverage threshold, `vi.mock`, `vi.useFakeTimers` |
88+
| `testing-js` | Vitest coverage gates, `vi.mock`, `vi.useFakeTimers` |
8689
| `mcp-builder` | Uses `@opencode-ai/plugin/tool` pattern for tool registration |
8790

8891
### Domain-Specific Skills (load when touching these areas)

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Contract tests in `test/contracts/` pin external API response shapes (OAuth toke
188188
**Never commit real tokens, real account ids, real organization ids, real JWT payloads, or any PII in fixtures.** The fixtures live in version control; treat them as public.
189189

190190
### Debug OAuth flow locally
191-
Set `CODEX_DEBUG_AUTH=1` in your shell before running. See `docs/development/AUTH_FLOW.md` (if present) for protocol details.
191+
Set `CODEX_DEBUG_AUTH=1` in your shell before running. See `docs/development/ARCHITECTURE.md#oauth-flow` for protocol details.
192192

193193
### Testing the OS-keychain backend
194194

config/minimal-opencode.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"provider": {
66
"openai": {
77
"options": {
8-
"store": false
8+
"store": false,
9+
"include": ["reasoning.encrypted_content"]
910
}
1011
}
1112
},

docs/DOCUMENTATION.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,27 @@ This file describes how docs are organized in this repository.
1414

1515
```text
1616
docs/
17-
index.md # documentation landing page
18-
README.md # docs portal / navigation
19-
getting-started.md # install + first-run guide
20-
configuration.md # full config reference
21-
troubleshooting.md # operational debugging guide
22-
privacy.md # data handling notes
23-
development/
24-
ARCHITECTURE.md # technical design
25-
CONFIG_FLOW.md # config resolution internals
26-
CONFIG_FIELDS.md # config field semantics
27-
TESTING.md # testing strategy and commands
28-
TUI_PARITY_CHECKLIST.md # auth dashboard UI parity checks
17+
├── _config.yml # docs site config
18+
├── DOCUMENTATION.md # this repository documentation map
19+
├── README.md # docs portal / navigation
20+
├── index.md # documentation landing page
21+
├── getting-started.md # install + first-run guide
22+
├── configuration.md # full config reference
23+
├── troubleshooting.md # operational debugging guide
24+
├── faq.md # short common answers
25+
├── privacy.md # data handling notes
26+
├── OPENCODE_PR_PROPOSAL.md # upstream OpenCode proposal notes
27+
├── development/
28+
│ ├── ARCHITECTURE.md # technical design and current module/docs layout
29+
│ ├── CONFIG_FIELDS.md # config field semantics
30+
│ ├── CONFIG_FLOW.md # config resolution internals
31+
│ ├── TESTING.md # testing strategy and commands
32+
│ └── TUI_PARITY_CHECKLIST.md # auth dashboard UI parity checks
33+
└── audits/
34+
├── INDEX.md
35+
├── 01-executive-summary.md ... 16-verdict.md
36+
├── _findings/ # T01 through T16 detailed findings
37+
└── _meta/ # audit rubric, ledger, environment, verification
2938
```
3039

3140
## config/ (copy-paste templates)
Lines changed: 16 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,22 @@
1-
> **Audit SHA**: d92a8eedad906fcda94cd45f9b75a6244fd9ef51 | **Generated**: 2026-04-17T09:28:39Z | **Task**: T18 Synthesis | **Source**: docs/audits/_meta/findings-ledger.csv
1+
> Audit source: 3331324cb14d2b80dd8dfb424619870a88476706 | Generated: 2026-04-25T12:45:33+08:00 | Scope: current-structure refresh
22
3-
# 01 — Executive Summary
3+
# Executive Summary
44

5-
**Repository**: `oc-codex-multi-auth` — OpenCode plugin intercepting OpenAI SDK calls and routing through a ChatGPT Codex OAuth backend with multi-account rotation. Version: `v6.0.0` (per `CHANGELOG.md`). Languages: TypeScript (strict), ESM-only Node ≥ 18.
5+
The audit corpus has been regenerated against the current codebase. The old audit line anchors for the pre-split tool monolith and storage monolith are no longer valid. Current structure evidence:
66

7-
**Audit effort**: 16 task audits (T01–T16) producing 10,841 lines across `docs/audits/_findings/`. Verification layer T17 extracted 320 findings; after Layer-2 citation check and Layer-3 dedup/severity reclassification, **149 unique PASSED findings** survive: 1 CRITICAL, 15 HIGH, 40 MEDIUM, 93 LOW. See `_meta/verification-report.md`, `_meta/dedup-report.md`, `_meta/severity-reclassifications.md` for the trail.
7+
- `index.ts`: 3694 lines, plugin context wiring and request pipeline.
8+
- `lib/tools/`: 21 per-tool `codex-*` modules wired by `createToolRegistry`.
9+
- `lib/storage.ts`: 79-line facade over focused `lib/storage/*` modules.
10+
- `lib/accounts.ts`: 366-line orchestrator over focused `lib/accounts/*` modules.
11+
- Tests: 83 Vitest files, including extracted-tool, chaos, property, contract, and doc-parity suites.
812

9-
---
13+
Current severity posture after this refresh:
1014

11-
## Maturity assessment
15+
| Severity | Current active | Notes |
16+
| --- | ---: | --- |
17+
| Critical | 0 | No active Critical finding remains. |
18+
| High | 0 | Former High items for inline tools, storage monolith, remove confirmation, export overwrite, and minimal config drift are resolved or superseded. |
19+
| Medium | 4 | Remaining items are maintainability or hardening work, mainly large request modules and release/process polish. |
20+
| Low | 5 | Backlog items are docs hygiene, coverage granularity, and developer-experience polish. |
1221

13-
**Overall**: **mid-to-late maturity for a single-maintainer plugin**, early for commercial/enterprise use.
14-
15-
- The codebase shows TypeScript-senior hygiene: strict mode, `noImplicitAny`, discriminated unions, Zod schema source-of-truth in `lib/schemas.ts`. Anti-pattern surface in production code is near-zero (`no-explicit-any` enforced; `as any` banned).
16-
- Package hygiene is deliberate: aggressive dependency overrides, per-project + global storage, `files:` allowlist, `.npmignore` present.
17-
- Documentation and public-policy surface exist: `README.md`, `CHANGELOG.md`, `SECURITY.md`, `CONTRIBUTING.md`, `CODE_OF_CONDUCT.md`, issue templates, `docs/` with architecture/getting-started/configuration/faq/privacy/troubleshooting.
18-
- Test coverage claims 80% at the line level (`vitest.config.ts`). 60+ test files exist covering the common auth + rotation + storage paths.
19-
20-
**Counter-signals**:
21-
- No CI runs typecheck, lint, test, audit, or build on pull requests (`pr-quality.yml` is markdown-only) — HIGH `290`.
22-
- Two advertised features (`lib/audit.ts`, `lib/auth-rate-limit.ts`) are 100% dead production code (HIGH `302`, `303`).
23-
- 5975-line `index.ts` and 1296-line `lib/storage.ts` block safe incremental change.
24-
- One package in the credential path (`@openauthjs/openauth@0.4.3`) ships without a declared license.
25-
26-
---
27-
28-
## Biggest strengths
29-
30-
- **Strict TypeScript + schema source-of-truth**. Zod schemas drive `lib/types.ts` and most boundary parsing; discriminated unions (e.g., `TokenResult`) are a first-class pattern. Type-safety findings ([§05-medium.md](05-medium.md)) are *polish*, not *risk*.
31-
- **Zero `as any`, zero `@ts-ignore`, zero `@ts-expect-error`** in production code.
32-
- **Dependency discipline**: aggressive overrides block known-bad transitives (ledger id `276`); Node engines constrained; ESM-only.
33-
- **SECURITY.md present with triage channel** (LOW `295` only asks for a dedicated email).
34-
- **Prompt-template ETag caching** (`lib/prompts/codex.ts`) is a model of cache-correctness: SWR + If-None-Match + bundled fallback.
35-
- **Proactive-refresh + refresh-queue** is well-structured — the defects in it are persistence races (HIGH `14`), not architectural mistakes.
36-
37-
---
38-
39-
## Biggest risks
40-
41-
1. **Credential handling has five latent defects** that are defensible-in-depth but one accident away from CRITICAL (plaintext persistence, unsigned cross-process injection, credential resurrection on merge, unverified JWT identity, silent token loss on refresh). See [§09-security-trust.md](09-security-trust.md).
42-
2. **One active CRITICAL race** in `lib/accounts.ts:728-733` (auth-failure counter across org-variant accounts). See [§03-critical-issues.md](03-critical-issues.md).
43-
3. **Silent-failure chains** across storage, recovery, and logger — swallowed errors, `warn`-level on session-breaking conditions, unbounded log files. See [§05-medium.md](05-medium.md), [§09-security-trust.md](09-security-trust.md).
44-
4. **Two monolithic modules** (`index.ts`, `lib/storage.ts`) prevent safe incremental change and concentrate blast radius. See [§04-high-priority.md](04-high-priority.md), [§07-refactoring-plan.md](07-refactoring-plan.md).
45-
5. **Dead code shipping as if live**`lib/audit.ts` and `lib/auth-rate-limit.ts`. See [§04-high-priority.md](04-high-priority.md).
46-
6. **No CI validation on PRs** — regressions reach main; coverage, type, and lint signals are maintainer-local only. See [§11-dx-cli-docs.md](11-dx-cli-docs.md).
47-
7. **Destructive defaults**`importAccounts(backupMode:"none")`, `exportAccounts(force:true)`, `codex-remove` without confirmation. See [§06-low-priority.md](06-low-priority.md) and [§12-quick-wins.md](12-quick-wins.md).
48-
8. **Storage-format forward-compat gap** — V4+ is silently discarded; V2 has no migrator. See [§04-high-priority.md](04-high-priority.md).
49-
9. **Testing**: documented 80% coverage masks zero direct tests for `lib/accounts/rate-limits.ts`, no fault-injection despite a 537-line "chaos" file, and no contract tests for external API shapes. See [§10-testing-gaps.md](10-testing-gaps.md).
50-
10. **Supply chain**: `@openauthjs/openauth@0.4.3` ships with no declared license. See [§04-high-priority.md](04-high-priority.md), [§09-security-trust.md](09-security-trust.md).
51-
52-
---
53-
54-
## Top 5 priorities (next 3-5 days)
55-
56-
1. **Ship the CRITICAL fix** — per-refresh-token promise chain in `lib/accounts.ts:728-733` + paired test (§10 #1, §03 entry). Effort: Short.
57-
2. **Flush debounced save on shutdown** — RC-10 closes a correlated defect class. Effort: Short. See [§07-refactoring-plan.md](07-refactoring-plan.md).
58-
3. **Turn on CI (typecheck/lint/test/build) on PRs** — F9 unblocks everything else. Effort: Short. See [§11-dx-cli-docs.md](11-dx-cli-docs.md).
59-
4. **Swap destructive defaults**`importAccounts(backupMode:"timestamped")`, `exportAccounts(force:false)`, `codex-remove(confirm:true)`. Effort: Quick. See [§12-quick-wins.md](12-quick-wins.md).
60-
5. **Decide `lib/audit.ts` + `lib/auth-rate-limit.ts`** via a single RFC — either wire in (F1/F10) or retire (RC-5). Effort: Short. See [§07-refactoring-plan.md](07-refactoring-plan.md), [§08-feature-recommendations.md](08-feature-recommendations.md).
61-
62-
The full 20-item prioritised list lives in [§14-top20.md](14-top20.md); adversarial verification outcomes in [`_meta/oracle-review.md`](_meta/oracle-review.md). For calendar sequencing see [§13-phased-roadmap.md](13-phased-roadmap.md).
63-
64-
---
65-
66-
## Per-chapter anchor pointers
67-
68-
Every domain chapter contributes to this summary:
69-
70-
- **[§02-system-map.md](02-system-map.md)** — architecture + trust boundaries backing the "biggest risks" list.
71-
- **[§03-critical-issues.md](03-critical-issues.md)** — the single CRITICAL race.
72-
- **[§04-high-priority.md](04-high-priority.md)** — 15 HIGHs spanning security, reliability, storage, CLI, CI.
73-
- **[§05-medium.md](05-medium.md)** — 40 MEDIUMs including the silent-failure cluster.
74-
- **[§06-low-priority.md](06-low-priority.md)** — 93 LOW items as a batched backlog.
75-
- **[§07-refactoring-plan.md](07-refactoring-plan.md)** — RC-1..RC-10 unblocking the HIGH/MEDIUM backlog.
76-
- **[§08-feature-recommendations.md](08-feature-recommendations.md)** — 10 user-pain-anchored features.
77-
- **[§09-security-trust.md](09-security-trust.md)** — credential lifecycle + hardening steps.
78-
- **[§10-testing-gaps.md](10-testing-gaps.md)** — race-window + contract + property tests to add.
79-
- **[§11-dx-cli-docs.md](11-dx-cli-docs.md)** — OSS-readiness scorecard.
80-
- **[§12-quick-wins.md](12-quick-wins.md)** — 20 ≤ 2-hour changes, high ROI.
81-
- **[§13-phased-roadmap.md](13-phased-roadmap.md)** — four phases with rollback posture.
82-
- **[§14-top20.md](14-top20.md)** — final ranked action list.
83-
- **[§15-file-by-file.md](15-file-by-file.md)** — disposition per module.
84-
- **[§16-verdict.md](16-verdict.md)** — scorecard + ship/don't-ship.
85-
86-
---
87-
88-
## Bottom line
89-
90-
The project is **structurally healthy** — mature TS/Node posture, explicit schemas, decent docs — but **operationally exposed** through one active race, destructive defaults, dead-code promises in privacy features, and no CI gating. The remediation cost is modest: **Phase 1 (safety + CI) is 3-5 days for a single maintainer**, after which the HIGH backlog halves and every subsequent change lands under test + type + lint protection.
22+
This PR also adds current-structure parity tests so future stale audit claims fail locally.

0 commit comments

Comments
 (0)