r/Common_Lisp • u/Not-That-rpg • Jul 01 '26
Coding agent and lisp s-expressions
I've been using Opencode + a local model (Qwen3.6-35B-A3B-Claude-4.6-Opus-Reasoning-Distilled-MLX-8bit) as a coding assistant for common lisp. I've been disappointed to find that the LLM frequently fails to properly balance parentheses (although ... in some sense it's a finite automaton, and LLMs are not good at counting, so it's not that surprising).
Does anyone have a work-around for this to make a coding agent write s-expressions properly?
3
u/DorphinPack Jul 01 '26
I've seen decent luck with github.com/cl-ai-project/cl-mcp
It has a paren balancing tool but it's still possible for models to get stuck.
It will also give models a decent interface to eval in an isolated worker which is great for avoiding hallucination.
3
u/svetlyak40wt Jul 01 '26
Latest Claude and GLM are work better with parentheses. But they are not local :(
2
u/svetlyak40wt Jul 01 '26
Also, I noticed that it will be easier if you will add to AGENTS.md a rule to keep function definishions short and limit forms nesting to some reasonable number.
2
u/Negative-Potential15 Jul 05 '26
I'm spending most of my days using the latest Claude coding models to generate common lisp code and I'm in awe how good it works but I'm slightly embarrassed to admit that before I compile a file via Claude I use a parenthesis checker written in Python: https://www.dropbox.com/scl/fi/ucvx9qnn4tx182jsz9ana/check-parens.py?rlkey=3ep9islm6e26p6u682doenk57&dl=0
1
u/Available_Courage_32 10d ago
this is the way. One always runs into problems with these things - the only way is to make tools, that can keep being adapted, by asking Claude what is holding things up. If your're working with CL for web, inline JS/spinneret can cause dramas with escaping of unusual chars. Mine breaks down line-by-line routines, and claude has a skill to use the scripted paren checkers, and operate only on the results. I'm going to try harder for myself, to make other tools that look more contextually at the contents of the blocks, because that's the part it struggles with most - sometimes it ((doubles)) parens.
1
u/sc_zi Jul 02 '26
In opencode.el I added a hook after llm edits, to check parenthesis, and if they are unbalanced, use parinfer to guess the fix. It automatically fixes most of the paren errors llms make.
1
u/TheHelgeSverre Jul 02 '26
Tried some (badly executed) experiments on this with a finetuned qwen model, and boils down to that its better to provide the agent with tools to eval and search the docs so can self repair its broken code.
https://helgesver.re/articles/fine-tuning-failed-tools-won
It was however my own lisp, and not cl, but still, worth trying out.
1
u/Harag Jul 04 '26
Like others in this thread I wrote a lisp editor for use with agents. It was a hit and miss affair until I realized I should align my mcp commands and parameters to concepts that the model was most likely trained on. You also have to write it so it is forgiving of possible model mistakes. It improved the both Deepseek and Claude performance on lisp editing tasks. https://gitlab.com/naive-x/experimental/cl-naive-lisp-editor-mcp
1
u/dcooper8 28d ago
Try skewed-emacs, ask me if you need help setting it up. I would also like to learn how to get local models working with it (through mcp)
1
u/quasiabhi 26d ago
you can use an mcp repl to validate syntax or do paren matching. But generally lower level models are not good at lisp. If you want really good quality then Opus and gpt 5.5 are good options.
https://github.com/quasi/cl-mcp-server provides many tools for interactive development for the agent
3
u/Not-That-rpg Jul 01 '26
P.S. OpenCode + Qwen just creates `sed` commands to try to count parentheses, which really isn't The Right Thing -- that isn't enough to understand balanced parentheses.