oct
Loading...
Searching...
No Matches
oct.linter.prompt_invariant_rules Namespace Reference

Classes

class  Finding
class  _ModelAssignmentVisitor

Functions

bool is_runtime_path (str|Path path)
list[Finding] check_source (str|Path path, str text)
tuple[bool, str] check_prompt_invariants (str|Path path, str text)
Iterator[Path] iter_runtime_paths (Path root)
list[Finding] check_paths (Iterable[Path] paths, *, Path root)
str _normalize_repo_path (str|Path path)
tuple[str,...] _path_parts (str|Path path)
ast.Module|None _parse_source (str text)
Finding _finding (str rule_id, str path, int line, int col, str message)
list[Finding] _check_raw_tool_schema_dump (str path, ast.Module tree)
Iterator[ast.AST] _iter_scopes (ast.Module tree)
Iterator[ast.AST] _iter_scope_nodes (ast.AST scope)
Iterator[ast.AST] _iter_scope_child_nodes (ast.AST node)
set[str] _tool_schema_aliases (list[ast.AST] nodes)
list[str] _assignment_target_names (ast.AST node)
str|None _assignment_value_name (ast.AST node)
bool _is_json_dumps (ast.AST func)
list[Finding] _check_model_setter_chokepoint (str path, ast.Module tree)
bool _is_model_write_target (ast.AST target)
list[Finding] _check_literal_mode_branch (str path, ast.Module tree)
bool _compare_has_mode_literal (ast.Compare node)
bool _is_mode_expr (ast.AST node)
bool _is_mode_literal (ast.AST node)
list[Finding] _check_retired_composition_symbols (str path, str text, ast.Module|None tree)
set[tuple[int, int]] _allowed_default_system_positions (str path, ast.Module|None tree)
list[Finding] _check_cache_ctx_fields_distinct (str path, ast.Module tree)
bool _is_show_turn_summary_call (ast.Call node)

Variables

tuple _PROMPT_REFS
Mapping RULES
dict _EXCLUDED_PARTS
dict _MODE_LITERALS = {"standard", "option-c"}
dict _RETIRED_NAMES

Detailed Description

Purpose
-------
OC Chat prompt-invariant structural rules, lifted into the OCT linter from
``oc_chat/invariants/prompt_invariant_rules.py`` (Plan 140) at the Plan 151
monorepo migration. Provides local structural enforcement for the Plan 135
prompt invariants. The module exposes portable rule metadata, runtime path
scoping, and source checkers that the OCT strict profile hosts as the
``prompt_invariants`` rule (replacing the OC Chat pytest wrapper).

Responsibilities
----------------
- Define rule metadata and agent-legible remediation text.
- Keep runtime path scope centralized and repository-relative (scoped to
  the OC Chat runtime source tree; a no-op for every other project).
- Check a single source string or a set of filesystem paths for invariant
  violations.
- Keep detector logic independent of OC Chat runtime imports.

Diagnostics
-----------
Domain: OCT-LINTER
Levels:
    L2 - lifecycle events
    L3 - semantic details
    L4 - deep tracing

Contracts
---------
- Public checkers accept repository-relative paths for matching.
- Absolute filesystem paths are normalized only through ``check_paths`` with
  an explicit root.
- Detectors must not edit source or depend on live OC Chat runtime objects.
- Findings include remediation text from the rule registry.

Dependencies
------------
- ast (parses the source and walks it for the structural detectors)
- collections.abc (Iterable, Iterator, Mapping - type hints for paths and the rule table)
- dataclasses (dataclass - the frozen RuleMetadata and Finding records)
- io (StringIO - feeds the source text to tokenize)
- pathlib (Path - filesystem paths for the runtime scope walk)
- tokenize (NAME tokens for the retired composition symbol check)
- types (MappingProxyType - the read-only RULES table)
- oct.core.diagnostics (structured debug logging via _dbg)

Function Documentation

◆ _allowed_default_system_positions()

set[tuple[int, int]] oct.linter.prompt_invariant_rules._allowed_default_system_positions ( str path,
ast.Module | None tree )
protected

Definition at line 549 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _assignment_target_names()

list[str] oct.linter.prompt_invariant_rules._assignment_target_names ( ast.AST node)
protected

Definition at line 363 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _assignment_value_name()

str | None oct.linter.prompt_invariant_rules._assignment_value_name ( ast.AST node)
protected

Definition at line 375 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _check_cache_ctx_fields_distinct()

list[Finding] oct.linter.prompt_invariant_rules._check_cache_ctx_fields_distinct ( str path,
ast.Module tree )
protected

Definition at line 570 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _check_literal_mode_branch()

list[Finding] oct.linter.prompt_invariant_rules._check_literal_mode_branch ( str path,
ast.Module tree )
protected

Definition at line 470 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _check_model_setter_chokepoint()

list[Finding] oct.linter.prompt_invariant_rules._check_model_setter_chokepoint ( str path,
ast.Module tree )
protected

Definition at line 395 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _check_raw_tool_schema_dump()

list[Finding] oct.linter.prompt_invariant_rules._check_raw_tool_schema_dump ( str path,
ast.Module tree )
protected

Definition at line 300 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _check_retired_composition_symbols()

list[Finding] oct.linter.prompt_invariant_rules._check_retired_composition_symbols ( str path,
str text,
ast.Module | None tree )
protected

Definition at line 514 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _compare_has_mode_literal()

bool oct.linter.prompt_invariant_rules._compare_has_mode_literal ( ast.Compare node)
protected

Definition at line 490 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _finding()

Finding oct.linter.prompt_invariant_rules._finding ( str rule_id,
str path,
int line,
int col,
str message )
protected

Definition at line 288 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _is_json_dumps()

bool oct.linter.prompt_invariant_rules._is_json_dumps ( ast.AST func)
protected

Definition at line 386 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _is_mode_expr()

bool oct.linter.prompt_invariant_rules._is_mode_expr ( ast.AST node)
protected

Definition at line 503 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _is_mode_literal()

bool oct.linter.prompt_invariant_rules._is_mode_literal ( ast.AST node)
protected

Definition at line 510 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _is_model_write_target()

bool oct.linter.prompt_invariant_rules._is_model_write_target ( ast.AST target)
protected

Definition at line 461 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _is_show_turn_summary_call()

bool oct.linter.prompt_invariant_rules._is_show_turn_summary_call ( ast.Call node)
protected

Definition at line 601 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _iter_scope_child_nodes()

Iterator[ast.AST] oct.linter.prompt_invariant_rules._iter_scope_child_nodes ( ast.AST node)
protected

Definition at line 339 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _iter_scope_nodes()

Iterator[ast.AST] oct.linter.prompt_invariant_rules._iter_scope_nodes ( ast.AST scope)
protected

Definition at line 333 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _iter_scopes()

Iterator[ast.AST] oct.linter.prompt_invariant_rules._iter_scopes ( ast.Module tree)
protected

Definition at line 326 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _normalize_repo_path()

str oct.linter.prompt_invariant_rules._normalize_repo_path ( str | Path path)
protected

Definition at line 269 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _parse_source()

ast.Module | None oct.linter.prompt_invariant_rules._parse_source ( str text)
protected

Definition at line 281 of file prompt_invariant_rules.py.

Here is the caller graph for this function:

◆ _path_parts()

tuple[str, ...] oct.linter.prompt_invariant_rules._path_parts ( str | Path path)
protected

Definition at line 276 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _tool_schema_aliases()

set[str] oct.linter.prompt_invariant_rules._tool_schema_aliases ( list[ast.AST] nodes)
protected

Definition at line 347 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_paths()

list[Finding] oct.linter.prompt_invariant_rules.check_paths ( Iterable[Path] paths,
* ,
Path root )
Check filesystem paths by converting them to repository-relative paths.

Definition at line 251 of file prompt_invariant_rules.py.

Here is the call graph for this function:

◆ check_prompt_invariants()

tuple[bool, str] oct.linter.prompt_invariant_rules.check_prompt_invariants ( str | Path path,
str text )
OCT linter adapter: collapse :func:`check_source` findings to the
``(passed, message)`` contract the per-file linter dispatch expects.

Returns ``(True, "")`` when the source is clean or out of the OC Chat
runtime scope (a no-op for every non-OC-Chat project), else
``(False, "<one line per finding>")`` naming each violated rule with its
location and remediation-bearing message.

Definition at line 202 of file prompt_invariant_rules.py.

Here is the call graph for this function:

◆ check_source()

list[Finding] oct.linter.prompt_invariant_rules.check_source ( str | Path path,
str text )
Check one repository-relative source path and return invariant findings.

Definition at line 184 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_runtime_path()

bool oct.linter.prompt_invariant_rules.is_runtime_path ( str | Path path)
Return whether *path* is in the root-anchored runtime source scope.

Definition at line 167 of file prompt_invariant_rules.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ iter_runtime_paths()

Iterator[Path] oct.linter.prompt_invariant_rules.iter_runtime_paths ( Path root)
Yield existing Python files in OC Chat's runtime source scope.

Definition at line 220 of file prompt_invariant_rules.py.

Here is the call graph for this function:

Variable Documentation

◆ _EXCLUDED_PARTS

dict oct.linter.prompt_invariant_rules._EXCLUDED_PARTS
protected
Initial value:
1= {
2 "__pycache__",
3 "docs",
4 "plans",
5 "tests",
6}

Definition at line 154 of file prompt_invariant_rules.py.

◆ _MODE_LITERALS

dict oct.linter.prompt_invariant_rules._MODE_LITERALS = {"standard", "option-c"}
protected

Definition at line 160 of file prompt_invariant_rules.py.

◆ _PROMPT_REFS

tuple oct.linter.prompt_invariant_rules._PROMPT_REFS
protected
Initial value:
1= (
2 "oc_chat/plans/135-prompt-redesign-agnostic-core.md section 4.7; "
3 "oc_chat/docs/architecture/inner-loop/inner-loop.md section 8"
4)

Definition at line 90 of file prompt_invariant_rules.py.

◆ _RETIRED_NAMES

dict oct.linter.prompt_invariant_rules._RETIRED_NAMES
protected
Initial value:
1= {
2 "build_system_prompt_with_tools",
3 "ensure_system_includes_tool_schemas",
4}

Definition at line 161 of file prompt_invariant_rules.py.

◆ RULES

Mapping oct.linter.prompt_invariant_rules.RULES

Definition at line 95 of file prompt_invariant_rules.py.