OCT Reference#
Version: 0.12.0 Entry point:
oct = oct.cli:cli(Click-based CLI) Python: 3.10+ Dependencies: Click 8.1+, pytest 7.0+
OCT (Option C Tools) is a development-time CLI toolkit that enforces, generates, and audits code written to the Option C coding standard. It has no runtime dependency on oc_diagnostics — it merely ensures that projects it manages are correctly wired to use it.
Table of Contents#
1. Installation & Entry Point#
OCT is installed as a local editable package:
pip install -e .
This registers the oct console script globally. The entry point is defined in pyproject.toml:
[project.scripts]
oct = "oct.cli:cli"
The version is read dynamically from package metadata via importlib.metadata.version("oct") in oct/__init__.py.
Source: oct/__init__.py, pyproject.toml
2. Project Root Detection#
OCT locates the project root by walking upward from the current working directory. The algorithm, implemented in find_project_root(), is:
Check for
.octrc.jsonroot marker — If a.octrc.jsonfile exists at the current level and contains"root_marker": true, that directory is the project root.Check for required directories — A directory is the project root if it contains all three of:
docs/tests/oc_diagnostics/(or legacydiagnostics/)
Legacy fallback — If
diagnostics/is found butoc_diagnostics/is not, a deprecation warning is emitted.oc_diagnostics/takes priority when both exist.--root-diroverride — The global--root-dir DIRflag bypasses auto-detection entirely, allowing OCT to operate on any directory.
The get_project_root() helper checks for the --root-dir Click context override first, then falls back to find_project_root().
Source: oct/core/project_root.py
3. CLI Commands — Complete Reference#
All commands are defined in oct/cli.py using Click. Global options (--root-dir) must appear before the subcommand name.
3.1 oct lint#
Static compliance analysis of Python source files.
oct lint [--fix-headers] [--dry-run] [--json] [--jobs N] [--changed] [PATH ...]
Flags#
Flag |
Description |
|---|---|
|
Rewrite malformed shebang, encoding, and file identity headers. Originals archived to |
|
Preview changes without writing. When combined with |
|
Emit structured JSON to stdout instead of colorized terminal output. |
|
Parallel workers (default: 1). Forced to 1 when |
|
Lint only git-modified |
|
Files or directories to lint. Defaults to entire project. |
Checks Performed#
Check |
Function |
Description |
|---|---|---|
|
|
Lines 1–4: shebang, encoding, file identity, blank line. |
|
|
Module docstring with Purpose, Responsibilities, Diagnostics, Contracts sections. |
|
|
Diagnostics section has Domain, L2, L3, L4 definitions. Scoped to module docstring. |
|
|
Non-trivial files (>20 lines) must call |
|
|
Non-trivial files must have |
|
|
Functions calling |
|
|
Detects invalid escape sequences and other syntax warnings. |
|
|
AST-based detection of string literals assigned to secret-like variable names, dict literals with secret keys, and function defaults with secret parameters (§6b). Blocking at |
|
|
Heuristic: |
Project-level checks (always run): presence of docs/ARCHITECTURE.md, tests/run_tests.py, tests/Tests_README.md, oc_diagnostics/debug_config.json.
Excluded Directories#
Defined in oct/core/exclusions.py:
Exact match (linter):
.git, __pycache__, Old, old, .linter_archive, copied_python_files, data, data_raw, raw_data, flow_charts, dot_files, dev-tools, Code Reviews
Wildcard (substring) match:
cache, _source, _skeleton, logs, _exports, _build
Additionally, OCT’s own package directory is always excluded.
JSON Output Schema#
{
"project": "project-name",
"total_files": 42,
"fully_compliant": 38,
"compliance_pct": 90.5,
"fixed_files": 0,
"checks": {
"header": {"pass": 40, "total": 42, "pct": 95.2},
"docstring": {"pass": 39, "total": 42, "pct": 92.9},
...
},
"files": [
{
"path": "src/module.py",
"checks": {
"header": {"pass": true, "message": "OK"},
"docstring": {"pass": false, "message": "Missing sections: Contracts"},
...
},
"compliant": false
}
]
}
Rule Profiles#
Configured via .octrc.json (see Section 4.1).
Profile |
header |
docstring |
diagnostics_profile |
dbg_usage |
dbg_import |
func_pattern |
tests_exist |
|---|---|---|---|---|---|---|---|
|
yes |
yes |
yes |
yes |
yes |
yes |
yes |
|
yes |
yes |
yes |
yes |
yes |
yes |
yes |
|
yes |
yes |
no |
no |
no |
no |
no |
Per-rule overrides in .octrc.json take priority over profile defaults.
Log Files#
Every lint run writes a timestamped log to logs/oct_lint_output-<YYYYMMDD-HHMMSS>.txt. Old logs are rotated automatically (max 10 files kept).
Source: oct/linter/oct_lint.py
3.2 oct format#
Auto-fix Option C compliance violations while preserving code semantics.
oct format [--fix] [--dry-run] [--json] [--verbose]
Flags#
Flag |
Description |
|---|---|
|
Apply formatting changes. Without this, only reports what would change (dry-run is default). |
|
Report what would change without modifying files. Default behavior without |
|
Machine-readable JSON output. |
|
Per-file details in addition to summary. |
Fix Pipeline#
Fixes are applied in this order:
Step |
Function |
What it fixes |
|---|---|---|
1 |
|
Rewrites malformed shebang, encoding, file identity, blank line. |
2 |
|
Inserts missing docstring skeleton or appends missing sections to existing docstrings. |
3 |
|
Adds |
4 |
|
Inserts |
Safety & Archival#
Originals archived to
.formatter_archive/<name>.<timestamp>.bakbefore modification.All code below headers is preserved exactly.
Each fixer is idempotent (running twice produces the same result as once).
Files are never modified unless
--fixis explicitly passed.Trivial files (<20 lines) skip
dbg_importandfunc_patternfixes.
Source: oct/formatter/oct_format.py
3.3 oct new#
Generate a new Option C-compliant Python file.
oct new PATH [--force] [--test] [--dry-run] [--verbose]
Arguments & Flags#
Argument/Flag |
Description |
|---|---|
|
Target file path (relative or absolute). Parent directories created automatically. Must be |
|
Overwrite existing file. |
|
Also generate companion test file at |
|
Show what would be created without writing. |
|
Show the generated file content. Works with or without |
Generated Module File#
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# <relative/path/to/file.py>
from oc_diagnostics import _dbg
"""
Purpose
-------
Describe the purpose of this module.
Responsibilities
----------------
- List the responsibilities of this module.
Diagnostics
-----------
Domain: <DOMAIN>
Levels:
L2 — lifecycle
L3 — semantic details
L4 — deep tracing
Contracts
---------
- State the contracts and guarantees of this module.
"""
Generated Test File (with --test)#
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# tests/test_<name>.py
"""
Purpose
-------
Unit tests for <name>.
...
"""
from <dotted.import.path> import *
def test_<name>_placeholder():
"""Placeholder test — replace with real tests."""
assert True
Package Markers#
When creating a file in a new subdirectory, __init__.py files are automatically created in all intermediate directories up to the project root.
Source: oct/generator/new_python_file.py
3.4 oct scaffold#
Create a complete Option C project skeleton.
oct scaffold NAME [--dry-run]
Arguments & Flags#
Argument/Flag |
Description |
|---|---|
|
Project directory name (created in current working directory). |
|
Show what would be created without writing. |
Created Structure#
NAME/
├── docs/
│ ├── ARCHITECTURE.md
│ └── doxygen_work_dir/
│ └── Doxyfile # Copied from OCT template, PROJECT_NAME set
├── tests/
│ ├── run_tests.py # Executable test runner stub
│ └── Tests_README.md
├── oc_diagnostics/
│ └── debug_config.json # Pre-configured oc_diagnostics template
├── logs/
└── docs/doxygen_output/
The debug_config.json template contains:
Schema version
"1.5"One domain:
GENERALat level 2All global settings with sensible defaults
A Doxyfile template is copied from oct/templates/Doxyfile (or fallback from OCT’s own docs/doxygen_work_dir/Doxyfile) with PROJECT_NAME set to the scaffold name.
An oc_diagnostics compatibility check runs at scaffold time (warning-only).
Source: oct/cli.py (scaffold command)
3.5 oct docs#
Validate, generate, or clean project documentation.
oct docs [--sphinx] [--doxygen] [--all] [--clean] [--dry-run]
Flags#
Flag |
Description |
|---|---|
|
Generate Sphinx HTML documentation. Discovers markdown in |
|
Generate Doxygen HTML documentation. Locates |
|
Run both |
|
Remove generated documentation artifacts from |
|
Preview what |
Without any flags, validates presence of required documentation files:
File |
Location |
|---|---|
Architecture document |
|
Test documentation |
|
Debug configuration |
|
Sphinx Pipeline Details#
Check Sphinx dependencies (
sphinx,sphinx_book_theme,myst_parser,sphinxcontrib.mermaid,sphinx_copybutton).Set up
docs/_sphinx/conf.pyfrom template (one-time, only if absent).Discover markdown files in
docs/, ordered by_PRIORITY_DOCS.Generate API reference via
sphinx-apidocfor discovered Python packages.Generate dependency graph via
oct deps(Mermaid).Write
index.rstwith all discovered content.Run
sphinx-build -M html.
Doxygen Pipeline Details#
Check
doxygenis installed viashutil.which().Locate
docs/doxygen_work_dir/Doxyfile.Create temp copy with
INPUTpatched to project root (forward slashes for cross-platform compatibility).Run
doxygenon the temp copy.Clean up temp Doxyfile.
Source: oct/docs/oct_docs.py, oct/cli.py
3.6 oct test#
Run the project’s test suite via pytest.
oct test [ARGS ...]
All arguments are forwarded to pytest. Exits with pytest’s return code.
Single-project mode#
When the project root contains a tests/ directory or a pytest configuration file (pytest.ini, pyproject.toml, setup.cfg, tox.ini), runs python -m pytest from that root.
Meta-project mode#
When the project root has no test suite of its own (no tests/ directory and no pytest config), oct test scans immediate sub-directories for those that have both a tests/ directory and a pytest config file. Each qualifying sub-project’s tests are run independently in the correct working directory, avoiding import collisions that would occur from running pytest across the entire tree. Results are aggregated; exits non-zero if any sub-project fails.
A sub-directory must have both tests/ and a pytest config to qualify — this avoids false positives from directories that happen to contain a tests/ folder but are not proper test suites (e.g. scaffold templates).
Source: oct/cli.py (_run_pytest, test command)
3.7 oct export-source#
Consolidate source files into readable text files for review, archiving, or analysis.
oct export-source [PATH] [--verbose] [--clean] [--single-dir]
Arguments & Flags#
Argument/Flag |
Description |
|---|---|
|
Root directory to export from. Defaults to auto-detected project root (see §2). |
|
Per-file line/word/byte statistics. |
|
Deprecated (use |
|
Write all output into one root-level directory instead of per-directory folders. |
Included File Types#
Organized by profile:
Profile |
Extensions |
|---|---|
Python |
|
Web |
|
JVM |
|
Systems |
|
Config |
|
Docs |
|
Scripts |
|
Misc |
|
Excluded Directories (exporter)#
Same common set as linter, plus: dataset_library, doxygen_work_dir, doxygen_output, logs, cache, _backup, old_backup, .venv, .claude, .git.
Excluded Files (§6b Secret Hygiene)#
Defined in oct/core/exclusions.py as NEVER_EXPORT_FILE_PATTERNS:
.env, .env.*, *.pem, *.key, credentials.json, secrets.json, secrets.yaml, secrets.yml
These files are excluded regardless of extension profile. See §6b of the Master Coding Style Specification.
Output Structure#
Per-directory _source_code-<YYYYMMDD-HHMM>/ folders containing:
_source.<dotpath>.txt— files from each directory_full_source.<root>.txt— all files combined
I/O Error Handling#
Never crashes on recoverable I/O errors (permissions, path too long).
Skips problematic directories/files with warnings.
Windows long-path: truncates dotpath and appends 8-character MD5 hash when path exceeds 240 characters.
Source: oct/tools/source_exporter.py
3.8 oct export-skeleton#
Export structural skeletons of source files for AI inspection or code review.
oct export-skeleton [PATH] [--verbose] [--single-dir] [--no-diagnostics] [--clean]
Arguments & Flags#
Argument/Flag |
Description |
|---|---|
|
Root directory to export from. Defaults to auto-detected project root (see §2). |
|
Per-file line/word/byte statistics. |
|
Write all output into one root-level directory. |
|
Omit the Diagnostics section from module docstrings. |
|
Remove all |
Skeleton content (Python files)#
Lines 1-3 (header block)
Module docstring (filtered if
--no-diagnostics)Class definitions with bases, decorators, class docstrings
Function/method signatures with type hints, decorators, docstrings
Module-level constants (
ALL_CAPSnames) asNAME = ......as body placeholder — no implementation bodies
Skeleton content (non-Python files)#
One-line placeholder: # [filename.ext] N lines, X.X KB
Output Structure#
Per-directory _skeleton_code-<YYYYMMDD-HHMM>/ folders containing:
_skeleton.<dotpath>.txt— skeletons from each directory_full_skeleton.<root>.txt— all skeletons combined
Uses the same config, exclusions, path safety, and I/O error handling as oct export-source.
Source: oct/tools/skeleton_exporter.py
3.9 oct clean#
Remove build artifacts.
oct clean [PATH] [--dry-run]
Argument/Flag |
Description |
|---|---|
|
Directory to clean. Defaults to current working directory. |
|
Show what would be deleted without deleting. |
Removes recursively:
_source_code-*export directories_skeleton_code-*skeleton directories__pycache__directories
No Option C project structure required — works on any directory.
Source: oct/tools/source_exporter.py, oct/tools/skeleton_exporter.py
3.10 oct health#
Project compliance dashboard.
oct health [--json] [--verbose]
Flags#
Flag |
Description |
|---|---|
|
Machine-readable JSON output. |
|
Per-file lint breakdown. |
Sections#
Section |
What it checks |
|---|---|
Lint Compliance |
Runs all linter checks, reports per-check pass rates and overall compliance %. |
Fixable Violations |
Counts violations |
Documentation |
Checks for |
Tests |
Runs pytest subprocess (120s timeout), captures pass/fail/skip counts and error details. |
oc_diagnostics |
Checks installation and version compatibility (range: >=1.5.0, <2.0.0). |
JSON Output Schema#
{
"project": "project-name",
"oct_version": "0.9.1",
"timestamp": "2026-03-08T12:00:00",
"lint": {
"total_files": 42,
"fully_compliant": 38,
"compliance_pct": 90.5,
"checks": {
"header": {"pass": 40, "total": 42, "pct": 95.2},
...
},
"files": [...]
},
"fixable": {
"header": 2, "docstring": 1, "dbg_import": 0, "func_pattern": 1, "total": 4
},
"docs": {
"total_required": 4,
"total_present": 4,
"files": [{"name": "ARCHITECTURE.md", "present": true}, ...]
},
"tests": {
"status": "pass|fail|timeout|error|not_run",
"exit_code": 0,
"summary": "42 passed in 1.23s",
"details": []
},
"compat": {
"installed": true,
"version": "1.5.0",
"compatible": true,
"message": ""
}
}
Source: oct/health/oct_health.py
3.11 oct diag#
oc_diagnostics configuration management tools. Operates purely on the JSON config file — does not import oc_diagnostics at runtime.
oct diag <subcommand>
oct diag validate-config#
Validates debug_config.json against expected schema. Reports:
Check |
Severity |
Condition |
|---|---|---|
File existence |
ERROR |
Config file not found |
Valid JSON |
ERROR |
JSON parse failure |
Root type |
ERROR |
Root must be a JSON object |
|
WARNING |
Key missing |
|
WARNING/ERROR |
Missing key, or not a dict, or entries with invalid level |
Domain levels |
ERROR |
Level not an integer 0–4 |
|
WARNING/ERROR |
Missing key, or not a dict |
|
WARNING |
Must be |
|
WARNING |
Must be |
oct diag list-domains#
Lists all configured domains in a formatted table:
Domain Level Color
--------------------------------------------
GENERAL 2 default
API 3 cyan
oct diag set-level DOMAIN LEVEL#
Updates a domain’s debug level in-place:
Validates level is 0–4
Validates domain exists in config
Writes back with
json.dumps(indent=2)+ trailing newline
Config File Location#
_find_config() checks:
<project_root>/oc_diagnostics/debug_config.json(preferred)<project_root>/diagnostics/debug_config.json(legacy fallback)
Source: oct/diag/oct_diag.py
3.12 oct git status#
Show the current git repository state with Option C compliance annotations.
oct git status [--json] [--verbose]
Flag |
Description |
|---|---|
|
Output structured JSON instead of colorized terminal report |
|
Show per-file lint status for staged files |
Displays branch name, clean/dirty state, staged/unstaged file counts with OCT lint status annotations. Includes hook installation state and protected-branch warnings.
Source: oct/git/oct_git.py
3.13 oct git check#
Run the unified quality gate on staged files or the entire project.
oct git check [--staged-only] [--all] [--profile PROFILE] [--fix] [--include-tests] [--json]
Flag |
Description |
|---|---|
|
Check only git-staged files (default when in a git repo) |
|
Check all Python files in the project |
|
Override lint profile: |
|
Apply auto-fixes for format violations |
|
Also run the test suite as part of the gate |
|
Output structured JSON with counts and exit code |
Exit codes#
Code |
Meaning |
|---|---|
0 |
All checks passed |
1 |
Lint violations |
2 |
Format violations |
3 |
Lint + format violations |
4 |
Test failures |
5 |
Secrets detected |
Source: oct/git/oct_git.py, oct/git/quality_gate.py
3.14 oct git init#
Initialise or enhance a git repository with Option C conventions.
oct git init [--force] [--skip-workflow] [--json]
Flag |
Description |
|---|---|
|
Overwrite existing configuration files |
|
Do not generate GitHub Actions workflow |
|
Output structured JSON summary |
Actions performed:
Creates or merges
.gitignorewith Option C patterns (bounded by# --- Option C START ---/# --- Option C END ---markers)Creates
.gitattributeswith sane defaults (line endings, binary detection, diff drivers)Generates
.github/workflows/oct-ci.ymlGitHub Actions workflow (unless--skip-workflow)
Source: oct/git/oct_git.py
3.15 oct git commit#
Commit staged changes with quality gate enforcement and Conventional Commits validation.
oct git commit -m MESSAGE [--force] [--no-verify] [--profile PROFILE] [--json]
Flag |
Description |
|---|---|
|
Required. Commit message in Conventional Commits format |
|
Skip lint/format checks. Secrets are still checked (non-bypassable). |
|
Forward |
|
Override lint profile: |
|
Output structured JSON with exit code, message, branch, errors |
Exit codes#
Code |
Meaning |
|---|---|
0 |
Commit succeeded |
1 |
Quality check violations (aborted) or protected-branch blocked |
2 |
Secrets detected (aborted, non-bypassable) |
3 |
Commit message validation failed |
4 |
Nothing to commit (no staged files) or git commit failed |
Pre-flight checks (in order)#
Message validation — Validates Conventional Commits format (type, scope, subject rules)
Staged file check — Verifies at least one file is staged
Protected-branch guard — On
main/master, forces strict profile; blocks direct commits in strict modeQuality gate — Runs lint + format + secrets on staged files
Secrets enforcement (G-D4) — Secrets findings always exit 2, regardless of
--forceor--no-verifyLarge-file advisory — Files > 10 MB trigger a warning (non-blocking)
Advisory checks — Warns if ARCHITECTURE.md or CHANGELOG.md not staged when relevant
Conventional Commits format#
<type>(<scope>): <subject>
Valid types: feat, fix, docs, style, refactor, perf, test, chore, sec
Rules:
Subject must start with lowercase letter
Subject must not end with period
First line must be <= 72 characters
!after scope indicates breaking change (e.g.,feat!: remove old API)
Source: oct/git/oct_git.py, oct/git/conventional.py
3.16 oct git hooks#
Manage Option C pre-commit hooks. Subcommands: install, status, remove.
oct git hooks install#
oct git hooks install [--force]
Flag |
Description |
|---|---|
|
Overwrite existing |
Generates .pre-commit-config.yaml with four hooks:
Hook ID |
Stage |
Description |
|---|---|---|
|
pre-commit |
Runs |
|
pre-commit |
Runs secrets detection ( |
|
commit-msg |
Validates Conventional Commits format |
|
pre-push |
Runs full quality gate including tests |
oct git hooks status#
oct git hooks status
Reports [INSTALLED] or [MISSING] for each of the four hook types.
oct git hooks remove#
oct git hooks remove
Removes OCT hooks from .pre-commit-config.yaml. Preserves any non-OCT hooks. Deletes the file entirely if only OCT hooks were present.
Deprecation note: The legacy oct install-hooks command still works but prints a deprecation notice and delegates to oct git hooks install.
Source: oct/git/oct_git.py
4. Configuration#
4.1 .octrc.json — Linter Configuration#
Place a .octrc.json file in the project root to customize linter behavior.
Full Schema#
{
"root_marker": false,
"linter": {
"profile": "default",
"exclude_dirs_add": [],
"exclude_dirs_remove": [],
"wildcard_exclude_add": [],
"rules": {
"header": true,
"docstring": true,
"diagnostics_profile": true,
"dbg_usage": true,
"dbg_import": true,
"func_pattern": true,
"tests_exist": true
}
}
}
Key |
Type |
Description |
|---|---|---|
|
bool |
If |
|
string |
Rule profile: |
|
list[str] |
Add directory names to exact-match exclusion list. |
|
list[str] |
Remove directory names from default exclusion list. |
|
list[str] |
Add substring patterns to wildcard exclusion list. |
|
dict[str, bool] |
Per-rule overrides. Takes priority over profile. |
Available Rule Names#
header, docstring, diagnostics_profile, dbg_usage, dbg_import, func_pattern, tests_exist
Profiles#
default/strict: All rules enabled.minimal: Onlyheaderanddocstringenabled.
If the file contains malformed JSON, a warning is printed to stderr and defaults are used.
4.2 .octrc.json — Git Configuration#
The .octrc.json file also supports git-related settings:
{
"git": {
"protected_branches": ["main", "master", "release/*"],
"large_file_threshold_mb": 10
}
}
Key |
Default |
Description |
|---|---|---|
|
|
Branch patterns where strict profile is enforced and direct commits are blocked |
|
|
Size threshold (MB) for large-file advisory warnings during |
4.3 _oct_exporter_config.json — Exporter Configuration#
Place a _oct_exporter_config.json file in the project root to customize the source exporter. Supports add/remove/replace semantics for:
include_extensionsexclude_dirswildcard_exclude_dirs
Config Resolution Order#
The exporter searches for _oct_exporter_config.json in three tiers:
Project root — A config file in the auto-detected project root (or explicit
PATHargument) takes highest priority.OCT bundled default — If no project-level config is found, the exporter falls back to oct’s own
_oct_exporter_config.json(located in the oct package root). A note is printed to stderr when this fallback is used.Built-in defaults — If neither file exists (e.g. oct is installed as a standalone package without the bundled config), hardcoded defaults from
oct/core/exclusions.pyare used. A warning is printed to stderr.
This ensures that oct’s comprehensive exclusion list (.venv, .claude, .git, AI tool directories, etc.) is always applied even when a project does not ship its own config file.
Source: oct/tools/source_exporter.py
5. Option C Compliance Rules#
Every Python file in an Option C project must satisfy these rules, enforced by oct lint and auto-fixable by oct format.
5.1 Header Block (3-line + blank)#
Lines 1–4 of every .py file:
#!/usr/bin/env python3 # Line 1: shebang
# -*- coding: utf-8 -*- # Line 2: encoding declaration
# relative/path/to/file.py # Line 3: file identity (relative to project root)
# Line 4: mandatory blank line
The file identity path is computed by expected_path_header(): the file’s path relative to the project root, in POSIX format.
5.2 Module Docstring (4 sections)#
A top-level """...""" docstring with four named sections:
Purpose — What this module does and why it exists.
Responsibilities — Bullet list of what this module owns.
Diagnostics — Domain name and L2/L3/L4 level definitions.
Contracts — Inputs, outputs, preconditions, postconditions.
5.3 Diagnostics Profile#
Within the module docstring’s Diagnostics section:
Diagnostics
-----------
Domain: DOMAIN_NAME
Levels:
L2 — summary-level events
L3 — detailed workflow events
L4 — verbose trace events
Must contain the keywords: Diagnostics, Domain:, L2, L3, L4.
5.4 _dbg Import and Usage#
Non-trivial files (>20 lines) must:
Import:
from oc_diagnostics import _dbg(canonical form, no aliases)Usage: At least one
_dbg()call (verified via AST)
5.5 func = "..." Pattern#
Every function that calls _dbg() must define func = "function_name" as its first non-docstring statement:
def process_data(items):
func = "process_data"
_dbg("L2", "Processing started", domain="CORE", func=func)
Checked per-function body; nested functions are checked independently.
5.6 Syntax Warnings#
Detects Python syntax warnings (e.g., invalid escape sequences like '\.' instead of r'\.'). These will become SyntaxError in a future Python version.
5.7 Regression Tests Heuristic#
Two-stage check:
Naming convention —
test_<stem>.pyexists anywhere undertests/.Import statement — Any
.pyfile intests/contains a whole-word import of the module stem.
5.8 Project-Level Files#
Required files checked at project level:
docs/ARCHITECTURE.mdtests/run_tests.pytests/Tests_README.mdoc_diagnostics/debug_config.json(ordiagnostics/debug_config.json)
6. Linter Internals#
6.1 LinterContext#
@dataclass
class LinterContext:
project_root: Path # Resolved project root
project_name: str # project_root.name
diagnostics_dir: Path # oc_diagnostics/ or diagnostics/
tests_dir: Path # <root>/tests
docs_dir: Path # <root>/docs
log_handle: Any = None # Open log file handle
log_path: Path | None # Path to current log file
log_lock: Lock # Thread lock for parallel logging
6.2 Check Functions#
All check functions return (ok: bool, message: str) tuples.
Function |
What it validates |
|---|---|
|
4-line header block correctness |
|
Module docstring with 4 required sections |
|
Domain + L2/L3/L4 in module docstring |
|
At least one |
|
Canonical |
|
|
|
Python syntax warnings |
|
String literals assigned to secret-like variable names, dict keys, or function defaults (§6b) |
|
Regression test presence (naming + import) |
6.3 Header Auto-Fix#
fix_header_block(path, text, ctx):
Archives original to
.linter_archive/<name>.<YYYYMMDD-HHMMSS>.bakComputes header boundary via
_header_boundary()(scans shebang, encoding, identity, blank line in strict order)Writes corrected 4-line header + remaining content
preview_header_fix(path, ctx) returns the corrected header without writing.
6.4 File Discovery#
find_python_files(root, exclude_dirs, wildcard_exclude_dirs):
Yields all
.pyfiles under root viarglob("*.py")Excludes OCT’s own package directory
Excludes directories matching exact or wildcard patterns
is_excluded_dir(path, exclude_dirs, wildcard_exclude_dirs):
Returns
Trueif directory name is in exact-match set OR contains any wildcard substring.
_git_changed_files(root):
Uses
git diff --name-only --diff-filter=ACMR HEADfor unstaged changesUses
git diff --name-only --diff-filter=ACMR --cachedfor staged changesFalls back to
git status --porcelain -uallfor fresh repos with no HEADReturns combined, deduplicated list of
.pypathsReturns empty list on timeout (10s) or if git is not available
6.5 Output Formats#
Terminal: Colorized output with ANSI codes (auto-detected on Windows). Per-check pass rates as percentages, per-file results.
JSON: Structured output with project name, total files, per-check statistics, and per-file check results.
Log file: Plain text written to logs/oct_lint_output-<YYYYMMDD-HHMMSS>.txt. Max 10 log files retained.
6.6 Parallel Processing#
--jobs N uses concurrent.futures.ThreadPoolExecutor. Each file is processed by _lint_single_file() which is thread-safe (uses ctx.log_lock for log writes). Forced to sequential (jobs=1) when --fix-headers is active.
Source: oct/linter/oct_lint.py
7. Formatter Internals#
7.1 FormatterContext#
@dataclass
class FormatterContext:
project_root: Path
project_name: str
diagnostics_dir: Path
tests_dir: Path
docs_dir: Path
fix_mode: bool # Actually apply changes
dry_run_mode: bool # Report-only mode
json_mode: bool # JSON output
verbose: bool # Per-file details
7.2 Fix Functions#
All fix functions return (changed: bool, new_text: str, messages: list[str]).
Function |
Action |
|---|---|
|
Reconstructs correct 4-line header, preserving content below. |
|
Inserts complete skeleton if missing; appends missing sections if incomplete. |
|
Adds canonical import after header. Removes incorrect/aliased imports. |
|
Inserts |
7.3 Processing Pipeline#
For each
.pyfile, apply fixers in sequence: header → docstring → import → func_pattern.If any fixer changed the text and
fix_modeis True, archive original and write new text.Collect per-file results for summary/JSON output.
7.4 Template Constants#
SHEBANG = "#!/usr/bin/env python3"
ENCODING = "# -*- coding: utf-8 -*-"
IMPORT_BLOCK = "from oc_diagnostics import _dbg\n"
DOCSTRING_SKELETON = '"""\nPurpose\n-------\n...\n"""'
Missing section templates: MISSING_DOCSTRING_SECTIONS dict with text for Purpose, Responsibilities, Diagnostics, Contracts.
Source: oct/formatter/oct_format.py
8. Generator Internals#
8.1 Template Constants#
SHEBANG = "#!/usr/bin/env python3"
ENCODING = "# -*- coding: utf-8 -*-"
IMPORT_BLOCK = "from oc_diagnostics import _dbg\n\n"
DOCSTRING_TEMPLATE — Standard module docstring with Purpose, Responsibilities, Diagnostics (Domain + L2/L3/L4), Contracts.
TEST_DOCSTRING_TEMPLATE — Test module docstring with Domain: TESTS.
TEST_BODY_TEMPLATE — Import line + placeholder test function.
8.2 Helper Functions#
_make_header(rel_posix): Builds the 4-line header block from a relative POSIX path.
_derive_import_path(rel_posix): Converts path/to/module.py → path.to.module for import statements.
8.3 File Generation Logic#
create_new_file(project_root, path, force, dry_run, test):
Resolves target path (absolute or relative to project root).
Validates
.pyextension and target is inside project root.Checks for existing files (refuses without
--force).Creates parent directories and
__init__.pypackage markers.Writes header + import block + docstring template.
If
test=True: generates companion test file attests/test_<stem>.pywith header, docstring, import, and placeholder test.
Source: oct/generator/new_python_file.py
9. Health Dashboard Internals#
9.1 Check Functions#
Function |
Returns |
|---|---|
|
Dict with total_files, fully_compliant, compliance_pct, per-check counters, optional file details |
|
Dict with fixable counts per category (header, docstring, dbg_import, func_pattern, total) |
|
Dict with total_required, total_present, per-file status |
|
Dict with status, exit_code, summary, details. Runs pytest subprocess with 120s timeout |
|
Dict with installed, version, compatible, message |
9.2 oc_diagnostics Compatibility#
Defined in oct/core/compat.py:
_OC_DIAGNOSTICS_COMPAT = {"min": "1.5.0", "max": "2.0.0"}
Uses importlib.metadata.version("oc_diagnostics") to check installed version. Tuple-based comparison (no packaging dependency required).
Source: oct/health/oct_health.py, oct/core/compat.py
10. Diagnostics Config Tools Internals#
10.1 Constants#
VALID_LEVELS = {0, 1, 2, 3, 4}
EXPECTED_GLOBAL_KEYS = {
"silent_mode", "enable_timestamps", "enable_colors", "include_filename",
"output_mode", "instrumentation_enabled", "max_log_files", "max_log_size_mb",
"instrumentation_allow_all", "mode", "http_redact_headers",
"ui_event_properties", "structural_id_prefixes", "structural_id_exact",
}
10.2 Config Location#
_find_config(project_root):
Check
<root>/oc_diagnostics/debug_config.jsonFall back to
<root>/diagnostics/debug_config.jsonReturn oc_diagnostics path as default (for error messages)
10.3 Validation Rules#
validate_config() checks:
File exists and is readable
Valid JSON
Root is a dict
_schema_versionkey presentdomainsis a dict, each entry is a dict with integer level 0–4global_settingsis a dictoutput_modeis one of:terminal,file,both,jsonmodeis one of:dev,prod
10.4 set-level Behavior#
set_level(project_root, domain, level):
Validates level 0–4
Verifies domain exists in config
Updates level in-place
Writes back with
json.dumps(config, indent=2) + "\n"
Source: oct/diag/oct_diag.py
11. Source Exporter Internals#
11.1 Export Algorithm#
Walk directory tree, collect source files matching included extensions.
Group files by directory.
For each directory, create
_source_code-<timestamp>/and write_source.<dotpath>.txt.Write
_full_source.<root>.txtcombining all directories.Print summary with per-directory statistics.
11.2 Clean Functions#
clean_source_dirs(target, dry_run): Removes all _source_code-* directories recursively.
clean_pycache_dirs(target, dry_run): Removes all __pycache__ directories recursively.
11.3 Excluded Directories (exporter)#
Defined in oct/core/exclusions.py as EXPORTER_EXCLUDE_DIRS:
Common set plus: Code Reviews, dataset_library, doxygen_work_dir, doxygen_output, logs, cache, _backup, old_backup.
11.4 Excluded Files (§6b Secret Hygiene)#
Defined in oct/core/exclusions.py as NEVER_EXPORT_FILE_PATTERNS:
.env, .env.*, *.pem, *.key, credentials.json, secrets.json, secrets.yaml, secrets.yml
These file patterns are checked via fnmatch against every filename before inclusion. This is Layer 2 of the three-layer secret defence (DD-05). .env was also removed from the config extension profile to provide defence-in-depth at the extension level.
Source: oct/tools/source_exporter.py, oct/core/exclusions.py
12. oc_diagnostics Integration Points#
OCT integrates with oc_diagnostics at multiple levels while maintaining a strict dev-time/runtime boundary — OCT never imports oc_diagnostics at runtime.
OCT Command |
Integration |
|---|---|
|
Generates |
|
Adds |
|
Checks for |
|
Auto-fixes missing |
|
Checks |
|
Validates, inspects, and updates |
oc_diagnostics Version Compatibility#
OCT validates the installed oc_diagnostics version at lint and scaffold time:
Minimum: 1.5.0
Maximum (exclusive): 2.0.0
If oc_diagnostics is not installed, an informational message is shown (not an error — OCT itself does not require it).