feat(core): scanner -- structural analysis and anagild snapshot export #107

Open
opened 2026-06-01 08:07:30 +02:00 by michael · 0 comments
Owner

Goal

Add a structural scanner to fragjan core that reads source files
and writes .anagild snapshot files for consumption by anagild.

The scanner is a core component -- not a primitive. Primitives may
use it to gain structural context (e.g. function boundaries for
return analysis). fragjan remains fully independent of anagild;
the .anagild files are optional output.

Background

fragjan already reads all files for rule checking. The scanner
adds structural understanding on top: functions, returns, requires,
variables, and -- anagild: annotations.

Format specification: DAW/anagild docs/anagild-snapshot-format.md
(schema: anagild_snapshot/v1)

Scope

New file: core/scanner/init.lua

Responsibilities:

  • detect language from file extension (lua, c, shell, md, yaml, json)
  • extract functions with range (start/end lines)
  • extract return statements with kind classification
  • extract require() calls with alias and line
  • extract file-level variables
  • extract -- anagild: annotations with target association
  • write .anagild/<path>.anagild (current snapshot)
  • write .anagild/<path>.anagild.history (hash reference list)
  • write .anagild/<path>.anagild.history.<sha256> (snapshot archive)

Out of scope (later):

  • primitives using scanner context (separate issues)
  • DAW/fragjan#93 nil-err semantic check (blocked by this issue)
  • anagild reading and indexing the files

Output format

See DAW/anagild docs/anagild-snapshot-format.md for the full schema.

Key fields per snapshot:

  • schema: anagild_snapshot/v1
  • lang: detected language
  • hash: sha256 of source file (algorithm + value)
  • functions[]: name, range, params, returns with kind
  • requires[]: module, alias, line
  • variables[]: name, scope, line, kind
  • annotations[]: line, target_kind, target, text, confidence

return.kind values

  • success_return -- recognized success path
  • error_return -- recognized error path
  • bare_nil -- single nil without second return value
  • unknown -- return recognized, meaning not determined (+ optional confidence)

Language support (v1)

Initial implementation covers lua only.
Scanner interface must accept a lang parameter to allow future
extension (c, shell, markdown, yaml, json) without API changes.

Acceptance criteria

  • core/scanner/init.lua exists and is loadable
  • scanner detects Lua functions with correct start/end range
  • return statements classified as success_return, error_return, bare_nil, unknown
  • require() calls extracted with alias and line
  • file-level variables extracted
  • -- anagild: annotations extracted and associated to nearest element
  • .anagild/ directory structure mirrors repo tree
  • history entry written only when source file hash changes
  • output validates against anagild_snapshot/v1 schema
  • lang parameter present in scanner API (extensibility)
  • fragjan runs normally without .anagild/ being writable (optional output)

Notes

The scanner must not affect fragjan's existing pass/fail behavior.
If .anagild/ is not writable, fragjan logs a warning and continues.
No new rule packs or primitives in this issue -- scanner only.

Enables: DAW/fragjan#93 (nil-err semantic check)

## Goal Add a structural scanner to fragjan core that reads source files and writes `.anagild` snapshot files for consumption by anagild. The scanner is a core component -- not a primitive. Primitives may use it to gain structural context (e.g. function boundaries for return analysis). fragjan remains fully independent of anagild; the `.anagild` files are optional output. ## Background fragjan already reads all files for rule checking. The scanner adds structural understanding on top: functions, returns, requires, variables, and `-- anagild:` annotations. Format specification: DAW/anagild docs/anagild-snapshot-format.md (schema: anagild_snapshot/v1) ## Scope New file: `core/scanner/init.lua` Responsibilities: - detect language from file extension (lua, c, shell, md, yaml, json) - extract functions with range (start/end lines) - extract return statements with kind classification - extract require() calls with alias and line - extract file-level variables - extract `-- anagild:` annotations with target association - write `.anagild/<path>.anagild` (current snapshot) - write `.anagild/<path>.anagild.history` (hash reference list) - write `.anagild/<path>.anagild.history.<sha256>` (snapshot archive) Out of scope (later): - primitives using scanner context (separate issues) - DAW/fragjan#93 nil-err semantic check (blocked by this issue) - anagild reading and indexing the files ## Output format See DAW/anagild docs/anagild-snapshot-format.md for the full schema. Key fields per snapshot: - `schema`: `anagild_snapshot/v1` - `lang`: detected language - `hash`: sha256 of source file (algorithm + value) - `functions[]`: name, range, params, returns with kind - `requires[]`: module, alias, line - `variables[]`: name, scope, line, kind - `annotations[]`: line, target_kind, target, text, confidence ## return.kind values - `success_return` -- recognized success path - `error_return` -- recognized error path - `bare_nil` -- single nil without second return value - `unknown` -- return recognized, meaning not determined (+ optional confidence) ## Language support (v1) Initial implementation covers `lua` only. Scanner interface must accept a `lang` parameter to allow future extension (c, shell, markdown, yaml, json) without API changes. ## Acceptance criteria - [ ] `core/scanner/init.lua` exists and is loadable - [ ] scanner detects Lua functions with correct start/end range - [ ] return statements classified as success_return, error_return, bare_nil, unknown - [ ] require() calls extracted with alias and line - [ ] file-level variables extracted - [ ] `-- anagild:` annotations extracted and associated to nearest element - [ ] `.anagild/` directory structure mirrors repo tree - [ ] history entry written only when source file hash changes - [ ] output validates against anagild_snapshot/v1 schema - [ ] lang parameter present in scanner API (extensibility) - [ ] fragjan runs normally without `.anagild/` being writable (optional output) ## Notes The scanner must not affect fragjan's existing pass/fail behavior. If `.anagild/` is not writable, fragjan logs a warning and continues. No new rule packs or primitives in this issue -- scanner only. Enables: DAW/fragjan#93 (nil-err semantic check)
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
DAW/fragjan#107
No description provided.