Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Living Code: docstrings, introspection, self-modification, evolve#30

Draft
HelgeSverre wants to merge 7 commits into
mainfrom
feature/living-code
Draft

Living Code: docstrings, introspection, self-modification, evolve#30
HelgeSverre wants to merge 7 commits into
mainfrom
feature/living-code

Conversation

@HelgeSverre

Copy link
Copy Markdown
Owner

Summary

  • Implements Living Code phases 1-4: docstrings, doctests, introspection (become!/rollback!), and self-modification
  • Adds evolve special form for LLM-driven genetic programming
  • Documentation: Living Code guide and metaprogramming reference pages
  • Makes defn the canonical function definition form

Test plan

  • Verify docstrings attach correctly to functions
  • Test doctests run and validate
  • Test become!/rollback! recursive doctest validation and source metadata sync
  • Test evolve with an LLM provider configured

HelgeSverre and others added 7 commits April 12, 2026 09:14
…elf-modification

Phase 1 (Layer 0+1): Docstrings & Doctests
- Docstring support in defn/define/defmacro (optional string before params)
- (meta sym) returns map with :name/:doc/:type/:arity/:params/:variadic?/:source/:file
- (doc sym) pretty-prints documentation to stderr
- (doc/search query) searches names and docstrings
- (doctest sym) runs >>> examples from docstrings
- Doctest parser/runner in crates/sema-eval/src/doctest.rs
- sema test CLI command

Phase 2 (Layer 2): Self-Reading
- (source-of sym) returns source text of definitions
- ;;@key value directive comment parser in sema-reader
- (read-source path) parses .sema files into structured definition maps
- Glob support for read-source

Phase 3 (Layer 3+4): LLM-Powered Introspection
- (ask target question) LLM-powered code analysis
- (ask/code target instruction) returns parsed Sema code from LLM
- (heal! sym) auto-repair loop using doctests as spec
- sema test --heal CLI flag

Phase 4 (Layer 6): Runtime Self-Modification
- (observe! sym n callback) wraps function with call logger
- (become! sym new-def) replaces definition with doctest validation
- (history sym) returns version history list
- (rollback! sym version) restores previous version
- (freeze! sym) permanently prevents mutation
- (bench thunk n) structured benchmarking with timing stats

Architecture: metadata stored on Env via parallel SpurMap alongside
bindings. Introspection forms are special forms (need calling Env).
LLM calls use call_llm_complete() helper via env binding lookup.

Amp-Thread-ID: https://ampcode.com/threads/T-019c9195-39f2-76a9-9f61-e37b65785c4f
Co-authored-by: Amp <[email protected]>
…ta sync

- become!: temporarily install candidate in env during doctest validation
  so recursive calls resolve to the new definition, not the old one.
  Restores old definition if doctests fail.
- become!: update :source metadata after successful replacement.
- rollback!: save current :source to history entry before restoring.
- rollback!: restore :source metadata from the history entry.

Amp-Thread-ID: https://ampcode.com/threads/T-019c9195-39f2-76a9-9f61-e37b65785c4f
Co-authored-by: Amp <[email protected]>
Phase 5 of Living Code: (evolve :name ... :spec ... :fitness fn :seed-prompt ...)
generates candidate function implementations via LLM, evaluates them against a
spec (inline or doctest-based), computes fitness via user callback, and iteratively
breeds/mutates the best candidates using crossover and mutation.

Components:
- evolve.rs: config parsing, spec runner, fitness computation, types
- special_forms.rs: eval_evolve (evolution loop), generate_candidate, log_generation
- 9 integration tests covering all config validation error paths

Co-authored-by: Amp <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-019c91b8-b2f4-70d3-ac9c-f7b1c3e12dd6
…ation, evolve

Comprehensive documentation page covering all 5 phases of Living Code:
- Docstrings & Doctests (defn, doc, doc/search, doctest, meta, CLI)
- Source Introspection (source-of, reader directives, read-source)
- Conversational Introspection (ask, ask/code, heal!)
- Runtime Self-Modification (observe!, become!, history, rollback!, freeze!)
- Genetic Programming (evolve with spec modes, fitness functions)

Includes 3 end-to-end scenarios (self-healing parser, adaptive memoization,
evolving for performance) and best practices. Added to sidebar and LLM index.

Co-authored-by: Amp <[email protected]>
Amp-Thread-ID: https://ampcode.com/threads/T-019c91b8-b2f4-70d3-ac9c-f7b1c3e12dd6
Update all documentation to prefer (defn name (params) body) over
(define (name params) body) for function definitions. define remains
the canonical form for value bindings (define x 42).

- special-forms.md: defn is now the primary heading, define section
  notes function shorthand is legacy, added preferred-style tip
- All code examples across 18 doc files updated
- defun references changed to defn (packages.md)
- Prose references to define-as-function kept where explaining compatibility

Amp-Thread-ID: https://ampcode.com/threads/T-019c91b8-b2f4-70d3-ac9c-f7b1c3e12dd6
Co-authored-by: Amp <[email protected]>
Metaprogramming (language/metaprogramming.md) covers introspection and
runtime modification features that work without an LLM:
- Docstrings, doctests, doc, doc/search, meta
- source-of, reader directives, read-source
- observe!, become!, history, rollback!, freeze!
- bench
- Scenarios: hot-swapping, profiling, doc generation

Living Code (llm/living-code.md) now focuses on LLM-powered features:
- ask, ask/code, heal!, evolve
- References metaprogramming page for prerequisites

Added Metaprogramming to Language Reference sidebar section.

Amp-Thread-ID: https://ampcode.com/threads/T-019c91b8-b2f4-70d3-ac9c-f7b1c3e12dd6
Co-authored-by: Amp <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant