Files
ss-tools/semantic_protocol.md
2025-12-19 22:40:28 +03:00

125 lines
3.4 KiB
Markdown

# SYSTEM STANDARD: CODE GENERATION PROTOCOL
**OBJECTIVE:** Generate Python code that strictly adheres to the Semantic Coherence standards defined below. All output must be machine-readable, fractal-structured, and optimized for Sparse Attention navigation.
## I. CORE REQUIREMENTS
1. **Causal Validity:** Semantic definitions (Contracts) must ALWAYS precede implementation code.
2. **Immutability:** Once defined, architectural decisions in the Module Header are treated as immutable constraints.
3. **Format Compliance:** Output must strictly follow the `[DEF]` / `[/DEF]` anchor syntax.
---
## II. SYNTAX SPECIFICATION
Code must be wrapped in semantic anchors using square brackets to minimize token interference.
### 1. Entity Anchors (The "Container")
* **Start:** `# [DEF:identifier:Type]`
* **End:** `# [/DEF:identifier]` (MANDATORY for semantic accumulation)
* **Types:** `Module`, `Class`, `Function`, `DataClass`, `Enum`.
### 2. Metadata Tags (The "Content")
* **Syntax:** `# @KEY: Value`
* **Location:** Inside the `[DEF]` block, before any code.
### 3. Graph Relations (The "Map")
* **Syntax:** `# @RELATION: TYPE -> TARGET_ID`
* **Types:** `DEPENDS_ON`, `CALLS`, `INHERITS_FROM`, `IMPLEMENTS`, `WRITES_TO`, `READS_FROM`.
---
## III. FILE STRUCTURE STANDARD
### 1. Python Module Header
Every `.py` file starts with a Module definition.
```python
# [DEF:module_name:Module]
#
# @SEMANTICS: [keywords for vector search]
# @PURPOSE: [Primary responsibility of the module]
# @LAYER: [Architecture layer: Domain/Infra/UI]
# @RELATION: [Dependencies]
#
# @INVARIANT: [Global immutable rule for this file]
# @CONSTRAINT: [Hard restriction, e.g., "No SQL here"]
# @PUBLIC_API: [Exported symbols]
# [SECTION: IMPORTS]
...
# [/SECTION]
# ... IMPLEMENTATION ...
# [/DEF:module_name]
```
### 2. Svelte Component Header
Every `.svelte` file starts with a Component definition inside an HTML comment.
```html
<!--
[DEF:ComponentName:Component]
@SEMANTICS: [keywords]
@PURPOSE: [Primary responsibility]
@LAYER: [UI/State/Layout]
@RELATION: [Child components, Stores, API]
@PROPS:
- name: type - description
@EVENTS:
- name: payload_type - description
@INVARIANT: [Immutable UI rule]
-->
<script>
// ...
</script>
<!-- [/DEF:ComponentName] -->
```
---
## IV. FUNCTION & CLASS CONTRACTS (DbC)
Contracts are the **Source of Truth**.
**Required Template:**
```python
# [DEF:func_name:Function]
# @PURPOSE: [Description]
# @SPEC_LINK: [Requirement ID]
#
# @PRE: [Condition required before execution]
# @POST: [Condition guaranteed after execution]
# @PARAM: [name] ([type]) - [desc]
# @RETURN: [type] - [desc]
# @THROW: [Exception] - [Reason]
#
# @RELATION: [Graph connections]
def func_name(...):
# 1. Runtime check of @PRE
# 2. Logic implementation
# 3. Runtime check of @POST
pass
# [/DEF:func_name]
```
---
## V. LOGGING STANDARD (BELIEF STATE)
Logs define the agent's internal state for debugging and coherence checks.
**Format:** `logger.level(f"[{ANCHOR_ID}][{STATE}] {MESSAGE} context={...}")`
**States:** `Entry`, `Validation`, `Action`, `Coherence:OK`, `Coherence:Failed`, `Exit`.
---
## VI. GENERATION WORKFLOW
1. **Analyze Request:** Identify target module and graph position.
2. **Define Structure:** Generate `[DEF]` anchors and Contracts FIRST.
3. **Implement Logic:** Write code satisfying Contracts.
4. **Validate:** If logic conflicts with Contract -> Stop -> Report Error.