deepclause-sdk 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/README.md +134 -2
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +154 -28
  4. package/dist/agent.js.map +1 -1
  5. package/dist/cli/config.d.ts +150 -140
  6. package/dist/cli/config.d.ts.map +1 -1
  7. package/dist/cli/config.js +3 -0
  8. package/dist/cli/config.js.map +1 -1
  9. package/dist/cli/login.d.ts +2 -1
  10. package/dist/cli/login.d.ts.map +1 -1
  11. package/dist/cli/login.js +15 -2
  12. package/dist/cli/login.js.map +1 -1
  13. package/dist/cli/repl.d.ts +54 -1
  14. package/dist/cli/repl.d.ts.map +1 -1
  15. package/dist/cli/repl.js +437 -54
  16. package/dist/cli/repl.js.map +1 -1
  17. package/dist/cli/set-backend.d.ts +2 -2
  18. package/dist/cli/set-backend.d.ts.map +1 -1
  19. package/dist/cli/set-backend.js +54 -8
  20. package/dist/cli/set-backend.js.map +1 -1
  21. package/dist/cli/work.d.ts +16 -0
  22. package/dist/cli/work.d.ts.map +1 -0
  23. package/dist/cli/work.js +45 -0
  24. package/dist/cli/work.js.map +1 -0
  25. package/dist/index.d.ts +8 -6
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +4 -3
  28. package/dist/index.js.map +1 -1
  29. package/dist/prolog-src/deepclause_mi.pl +2426 -253
  30. package/dist/prolog-src/deepclause_strings.pl +36 -3
  31. package/dist/repl/agent.d.ts +5 -0
  32. package/dist/repl/agent.d.ts.map +1 -1
  33. package/dist/repl/agent.js +19 -7
  34. package/dist/repl/agent.js.map +1 -1
  35. package/dist/repl/auth.d.ts +6 -3
  36. package/dist/repl/auth.d.ts.map +1 -1
  37. package/dist/repl/auth.js +55 -5
  38. package/dist/repl/auth.js.map +1 -1
  39. package/dist/repl/backend.d.ts +24 -0
  40. package/dist/repl/backend.d.ts.map +1 -1
  41. package/dist/repl/backend.js +292 -12
  42. package/dist/repl/backend.js.map +1 -1
  43. package/dist/repl/compaction.d.ts +2 -0
  44. package/dist/repl/compaction.d.ts.map +1 -1
  45. package/dist/repl/compaction.js +9 -0
  46. package/dist/repl/compaction.js.map +1 -1
  47. package/dist/repl/event-history.d.ts +4 -0
  48. package/dist/repl/event-history.d.ts.map +1 -0
  49. package/dist/repl/event-history.js +33 -0
  50. package/dist/repl/event-history.js.map +1 -0
  51. package/dist/repl/harness-tools.d.ts +25 -18
  52. package/dist/repl/harness-tools.d.ts.map +1 -1
  53. package/dist/repl/harness-tools.js +103 -34
  54. package/dist/repl/harness-tools.js.map +1 -1
  55. package/dist/repl/history.d.ts +52 -2
  56. package/dist/repl/history.d.ts.map +1 -1
  57. package/dist/repl/history.js +113 -4
  58. package/dist/repl/history.js.map +1 -1
  59. package/dist/repl/index.d.ts +27 -5
  60. package/dist/repl/index.d.ts.map +1 -1
  61. package/dist/repl/index.js +27 -0
  62. package/dist/repl/index.js.map +1 -1
  63. package/dist/repl/reference.d.ts +31 -0
  64. package/dist/repl/reference.d.ts.map +1 -0
  65. package/dist/repl/reference.js +143 -0
  66. package/dist/repl/reference.js.map +1 -0
  67. package/dist/repl/render.d.ts +3 -1
  68. package/dist/repl/render.d.ts.map +1 -1
  69. package/dist/repl/render.js +7 -0
  70. package/dist/repl/render.js.map +1 -1
  71. package/dist/repl/session.d.ts +128 -23
  72. package/dist/repl/session.d.ts.map +1 -1
  73. package/dist/repl/session.js +895 -196
  74. package/dist/repl/session.js.map +1 -1
  75. package/dist/repl/tui/autocomplete.d.ts.map +1 -1
  76. package/dist/repl/tui/autocomplete.js +5 -1
  77. package/dist/repl/tui/autocomplete.js.map +1 -1
  78. package/dist/repl/tui/renderer.d.ts +2 -1
  79. package/dist/repl/tui/renderer.d.ts.map +1 -1
  80. package/dist/repl/tui/renderer.js +20 -3
  81. package/dist/repl/tui/renderer.js.map +1 -1
  82. package/dist/repl/tui/status-bar.d.ts +5 -2
  83. package/dist/repl/tui/status-bar.d.ts.map +1 -1
  84. package/dist/repl/tui/status-bar.js +8 -3
  85. package/dist/repl/tui/status-bar.js.map +1 -1
  86. package/dist/repl/tui/tool-pane.d.ts +1 -1
  87. package/dist/repl/tui/tool-pane.d.ts.map +1 -1
  88. package/dist/repl/tui/tool-pane.js +13 -7
  89. package/dist/repl/tui/tool-pane.js.map +1 -1
  90. package/dist/repl/tui/work-view.d.ts +10 -0
  91. package/dist/repl/tui/work-view.d.ts.map +1 -0
  92. package/dist/repl/tui/work-view.js +28 -0
  93. package/dist/repl/tui/work-view.js.map +1 -0
  94. package/dist/runner.d.ts.map +1 -1
  95. package/dist/runner.js +50 -19
  96. package/dist/runner.js.map +1 -1
  97. package/dist/runtime-identity.json +10 -0
  98. package/dist/system/assets/docs/AGENT_PROMPT.md +265 -3008
  99. package/dist/system/assets/docs/DML_AUTHORING.md +81 -299
  100. package/dist/system/assets/docs/DML_BASICS.md +93 -0
  101. package/dist/system/assets/docs/DML_DEBUGGING.md +73 -0
  102. package/dist/system/assets/docs/DML_FORMS.md +94 -728
  103. package/dist/system/assets/docs/DML_MEDIA.md +63 -0
  104. package/dist/system/assets/docs/DML_PROCESSES.md +100 -0
  105. package/dist/system/assets/docs/DML_PYTHON.md +74 -0
  106. package/dist/system/assets/docs/DML_REFERENCE.md +224 -1971
  107. package/dist/system/assets/docs/DML_SHELL.md +107 -0
  108. package/dist/system/assets/docs/DML_TASKS.md +112 -0
  109. package/dist/system/assets/docs/HARNESS_EXAMPLES.md +255 -179
  110. package/dist/system/assets/docs/HARNESS_PATTERNS.md +65 -538
  111. package/dist/system/assets/docs/LOCAL_AGENT_PROMPT.md +186 -0
  112. package/dist/system/assets/docs/PROLOG_SNIPPETS.md +61 -154
  113. package/dist/system/assets/index.d.ts +1 -1
  114. package/dist/system/assets/index.d.ts.map +1 -1
  115. package/dist/system/assets/index.js +7 -0
  116. package/dist/system/assets/index.js.map +1 -1
  117. package/dist/system/environment.d.ts +1 -1
  118. package/dist/system/environment.d.ts.map +1 -1
  119. package/dist/system/environment.js +10 -6
  120. package/dist/system/environment.js.map +1 -1
  121. package/dist/system/runtime/shell-manager.d.ts +1 -1
  122. package/dist/system/runtime/shell-manager.d.ts.map +1 -1
  123. package/dist/types.d.ts +49 -2
  124. package/dist/types.d.ts.map +1 -1
  125. package/dist/work/index.d.ts +82 -0
  126. package/dist/work/index.d.ts.map +1 -0
  127. package/dist/work/index.js +232 -0
  128. package/dist/work/index.js.map +1 -0
  129. package/package.json +12 -3
  130. package/src/prolog-src/deepclause_mi.pl +2426 -253
  131. package/src/prolog-src/deepclause_strings.pl +36 -3
@@ -1,299 +1,81 @@
1
- # DML authoring guide (REPL)
2
-
3
- How to think about a REPL harness. For code, see `HARNESS_EXAMPLES.md` (idiomatic
4
- harness shapes) and `PROLOG_SNIPPETS.md` (tested building blocks).
5
-
6
- ## 0. What a harness is
7
-
8
- A harness is a **living library of reusable DML skills** for the workspace —
9
- not a single program. A skill is a predicate; name them descriptively and run
10
- them with `execute` (during a turn) or `/goal`. Skills are built from:
11
-
12
- - **helper predicates** (plain Prolog/CLP),
13
- - **semantic judgments** (`choose`/`rate`/`verify`/`probability`),
14
- - **host tools** via `exec/2`, and
15
- - **model calls** via `task/N` — a bounded agentic turn that may itself call
16
- other skills.
17
-
18
- The module is **data = code**: small/medium structured data belongs in it as
19
- facts (`country(ch, "Switzerland").`), inspected and queried exactly like
20
- predicates (`listing`, `/goal`, `execute`). Add data as facts when it is small
21
- enough to reason over; keep large datasets in a file or a linked script rather
22
- than bloating the module.
23
-
24
- A harness may also carry **test predicates** that pin the intended behavior of a
25
- skill — for example `test_<name>`, or a small `check/2` runner over
26
- input/expected pairs. When you change a skill, update or add its test and run it
27
- so the change is verified, not assumed.
28
-
29
- So a harness evolves like a codebase through **composition and reuse**: prefer
30
- an existing predicate over a near-duplicate. Change it **only when the user
31
- asks** — create on a fresh workspace, and afterwards touch only what the request
32
- requires; never refactor or extend it on your own initiative.
33
-
34
- **Generalize, don't specialize.** Prefer a few small, composable primitives over
35
- a predicate per request, and express one-off requests by composing them (or a
36
- direct `/goal`) instead of naming a predicate for every phrasing. A hard-coded
37
- "latest image in `~/Downloads`" is wrong; even `find_latest(Dir, Pattern, Path)`
38
- is fairly narrow — the reusable parts (list a directory, filter, pick a
39
- best/latest element) are shared by many tasks. Ask whether another caller would
40
- plausibly reuse the predicate with different arguments; if only this request
41
- would, do not add it. A harness of many task-shaped helpers is as bad as one
42
- hard-coded script.
43
-
44
- **Declare the shape of structured model output.** `list(object(B))` does not say
45
- what is in the dicts, so every consumer re-derives defaults and tolerates missing
46
- fields by hand. `schema(Name, [field(Field, Type, Option)])` is checked when the
47
- result binds: defaults filled, a missing `required` field or a wrong type reported
48
- against the field name, an undeclared field kept so a mistake stays visible. Name it
49
- in the output — `task(…, list(schema(author, Authors)))` — or apply it by hand with
50
- `schema_fill(author, Raw, Author)`.
51
-
52
- **Know which conversation the model sees.** `task/N` is sent the memory built so
53
- far and writes its reply back, so consecutive `task/N` calls accumulate history —
54
- pass values as explicit arguments instead of relying on the model to remember them.
55
- `prompt/N` is fresh (no memory, no tools) and is the right choice for a classifier,
56
- extractor, or transformation that must not be swayed by the conversation;
57
- `with_fresh_memory(Goal)` does the same for a group of steps, and restores the
58
- caller's memory on exit. A `task/N` inside a `tool/2` body starts empty: give it the
59
- context as an argument.
60
-
61
- **Start small, then improve.** Write the smallest harness that answers the actual
62
- request end to end — often a single predicate — run it on a real input, and show
63
- the user the result *before* extending it. Say what it does, what it does not do
64
- yet, and what you would add next, then let the next request decide. A harness grows
65
- in the direction the user actually takes it.
66
-
67
- Adding capability speculatively is the expensive mistake, and it costs more here
68
- than in a normal codebase: every predicate is part of the harness the next turn has
69
- to reason about, and every helper is one more thing the model can be offered as a
70
- tool. Do not write an error taxonomy before an error has occurred, retry and
71
- fallback policies before something has failed, caching or configuration layers
72
- before there is a reason, or a predicate per case for cases you have not seen.
73
- Prefer the smallest change that makes the failing input work.
74
-
75
- Signs you are overengineering:
76
-
77
- - a predicate with one caller whose arguments are all literals from the request;
78
- - an abstraction (a schema, an options list, a dispatch table) built for a single
79
- shape or a single value;
80
- - a helper "framework" written before the first end-to-end run;
81
- - generalizing across cases that no one has asked for yet;
82
- - a harness that does not run yet, because the parts are still being made tidy.
83
-
84
- A small change is also easier to trust: each commit is one reviewable unit, the
85
- `end_turn` probes report on a small surface, and `rollback` stays cheap. A harness
86
- that is incomplete but working is a better answer than one that is complete and
87
- unverified.
88
-
89
- **Do not parse in DML — shell out or ask the model.** Prolog string handling is
90
- the fiddliest thing here, and a hand-rolled parser (`sub_string/5`,
91
- `split_string/4`, `atom_concat/3`, `format/2`, an accumulator) is the easiest way
92
- to write DML that looks right and is quietly wrong. Split the work by what it is.
93
- Mechanical work — pull a field, change a format, count, filter — belongs in a
94
- **helper script** (`script_file/2` + `exec(bash(argv: [...]))`), which is testable
95
- outside DML and written in a language made for text; have it emit **JSON** and read
96
- it with `json_get/3`/`json_gets/3`/`json_text/2` instead of splitting stdout.
97
- Work that needs *understanding* — find the field named X, classify, summarise —
98
- belongs in a **model call** with a typed output (`task("…", list(string(Items)))`,
99
- `schema/2`, `choose`/`rate`/`verify`). DML sequences and checks; it does not parse.
100
-
101
- **Prefer orchestration over algorithms.** A skill should read as a short
102
- sequence of steps. Describe each step with `task/N` (which sees the conversation
103
- memory) or `prompt/N` (fresh context) and let the model extract, classify,
104
- summarise, or decide; keep the Prolog between the steps to simple guards
105
- (`->`, `;`, `once/1`, `==`, `\==`), `member/2`, `between/3`, `format/2`, and
106
- small arithmetic. Reach for deep recursion, hand-rolled list algorithms, CLP, or
107
- `assert`/`retract` only when the task is genuinely algorithmic — and prefer a
108
- one-shot `execute` goal over committing it as a skill. DML is the orchestration
109
- layer, not the logic library.
110
-
111
- ## 1. Skills vs one-shot work
112
-
113
- A harness is a library of reusable **capabilities**, not a record of what you
114
- did. Most requests mix two things, and you must separate them:
115
-
116
- - **A reusable capability** — worth a named, documented, tested predicate. It
117
- takes its inputs as arguments and works for any caller (`describe_image(Path,
118
- Text)`, `tally(List, Counts)`).
119
- - **A one-off action** — the concrete step for *this* request. Run it now with
120
- an `execute` goal or `bash`; do not commit it as a predicate.
121
-
122
- Example — "find the latest image in `~/Downloads` and tell me what's on it":
123
- run the "find/copy the latest file" step once in the turn, and add only
124
- `describe_image(Path, Text)` as the skill. Finding a latest file, copying files
125
- around, moving data, or any single concrete invocation is a task, not a skill.
126
-
127
- Never bake environment-specific values into a predicate: no host paths
128
- (`/home/...`), machine-specific directories, or fixed filenames — a skill takes
129
- them as arguments. A predicate that exists only to be called once, with literals
130
- frozen in, is a script in disguise.
131
-
132
- ## 2. Only use tools from the catalog
133
-
134
- The HOST TOOL CATALOG above is the complete set of `exec(...)` capability tools.
135
- Do not invent names — there is no `calculator`, `fetch_url`, `web_search`, or
136
- `vm_exec` unless the catalog lists it. By default the only capability tool is
137
- **`bash`**.
138
-
139
- **Prefer the shell for file I/O.** Use `bash` for reading, writing, copying, and
140
- converting files. The DML file predicates (`read_file/2`, `open/3`,
141
- `read_string/3`, `write_file/2`, `make_directory_path/1`, `directory_files/2`, …)
142
- are for when the content must be bound and reasoned over **inside Prolog** — not
143
- for moving bytes around. Reading a file into DML just to shell it out again is a
144
- smell.
145
-
146
- ## 3. Scripts are files, linked from the harness
147
-
148
- Materialize a script by running a DML goal with the `execute` tool — for example
149
- `make_directory_path("scripts"), write_file("scripts/x.py", "…")` — then link it
150
- with `script_file/2` and run it with `bash`. `write_file/2` writes to the
151
- workspace immediately and does **not** create parent directories.
152
- `script_file/2` is just a fact: it tells `export` which files belong to the
153
- harness. Use a literal workspace-relative path and one fact per name. `export harness.zip` bundles the
154
- `.dml` plus every linked file; `export harness.dml` inlines them so the single
155
- file stays portable.
156
-
157
- Untrusted data (URLs, file contents, user text) goes through a file or `stdin`,
158
- never interpolated into the shell command string. See `HARNESS_EXAMPLES.md` §1.
159
-
160
- A linked script is for a genuinely external capability (a library, `ffmpeg`, an
161
- API) — not a substitute for a shell command you run once, and not one script per
162
- request. When a script is the capability, it is a component: parameterize it and
163
- document it with `doc/2`.
164
-
165
- ## 4. Annotate the harness (self-describing)
166
-
167
- Keep the harness self-describing. `module_doc/1` is the harness's **skill
168
- description** — the one line that says what the harness does and when to use it,
169
- the same role a skill's `description` plays for routing. Write it like
170
- frontmatter, not like a title:
171
-
172
- ```prolog
173
- module_doc("Support triage: classify a customer message and route it to the right team. Use when a message needs a team.").
174
-
175
- doc(classify/2, "Route a support message to a team. classify(+Message, -Team). Example: /goal classify(\"I was charged twice\", T).").
176
- doc(tally/2, "Count occurrences of each item. tally(+List, -Counts) yields Key-N pairs. Example: tally([a,b,a], C).").
177
- ```
178
-
179
- - `module_doc(Text)` — the skill description. It is the **first line of the
180
- manifest** (`- skill: ...`), so **read it at the start of the session and
181
- re-read it whenever the harness changes**. A file with only `module_doc/1` and
182
- no predicates is still a valid skill: it advertises the capability while the
183
- body is empty.
184
- - `tool(Head, "desc") :- Body.` — **an annotation, not an implementation**: it
185
- exposes an existing predicate to the nested agent in `task/N`. Define the
186
- predicate normally and delegate
187
- (`tool(count_words(File, Count), "…") :- count_words(File, Count).`), so the
188
- same predicate is callable from `execute` / `/goal` **and** from `task/N`. Logic
189
- placed only inside `tool/2` is unreachable from `execute` (the loader asserts
190
- `tool(Head)`, never `Head`).
191
- - Annotate **capabilities** (file I/O, search, a script) that a nested agent
192
- should use — not the LLM steps themselves. A `tool/2` whose implementation
193
- calls `task/N` and advertises the same job is offered to the agent inside that
194
- `task/N`, which then calls it. Absent the tool call stack, wrapper predicates
195
- are the same job twice; keep the step a skill, or scope the task with
196
- `without_tools([Head], task(…))`.
197
- - `doc(Name/Arity, Text)` — one fact per skill, helper, or tool; terse, with
198
- arguments (`+in` / `-out`) and an example goal.
199
-
200
- They are shown in the manifest (so the next turn sees them), in `listing`, and
201
- survive `export`. When you change a predicate, update its `doc/2`; when the
202
- harness's purpose changes, update `module_doc/1`.
203
-
204
- ## 5. Media (images)
205
-
206
- Images are **workspace files**, referenced by `media(Path, Mime)`; the bytes
207
- never enter Prolog or conversation memory. Read a reference with `read_media/2`
208
- and put it in context with `attach_media/1`; the next `task/N` sends it to the
209
- model as an image block:
210
-
211
- ```prolog
212
- describe_image(Path, Text) :-
213
- read_media(Path, Image),
214
- attach_media(Image),
215
- task("Describe the attached image in one sentence.", Text).
216
- ```
217
-
218
- Only PNG/JPEG/GIF/WebP are accepted. This needs a vision-capable model; keep a
219
- textual description in memory when it does not. Semantic judgments stay
220
- text-only — never attach media to them.
221
-
222
- ## 6. Guard rails
223
-
224
- - Read first: call `listing`.
225
- - **Paths: use `/workspace/...`.** The workspace is the working directory, so
226
- `out/report.txt` and `/workspace/out/report.txt` are the same file — but write
227
- the `/workspace` form, because it means the same thing from DML, from
228
- `exec(bash(...))`, and from the shell tool on every backend. Everything outside
229
- `/workspace` is the session's ephemeral VM filesystem: fine for scratch, never
230
- for a result, because **only files under `/workspace` reach the host**. Scratch
231
- goes in `$TMPDIR` (per-session in every backend), not a bare `/tmp`.
232
- `directory_files/2` returns **bare names**, so build one
233
- (`atom_concat('images/', Name, Path)`). Prefer `bash`/host file tools for file
234
- operations; use the DML file predicates only for content you must reason over
235
- in Prolog.
236
- - **Side-effecting skills must be deterministic.** Any skill that calls
237
- `task/N`, `prompt/N`, `exec/2`, `read_media/2`, `attach_media/1`, or writes a
238
- file must yield exactly one solution with no choicepoint left across the side
239
- effect (`once/1`, `->`, or a cut). Otherwise backtracking re-runs the model
240
- call: slow, costly, and a different answer each time.
241
- - Replace a predicate with `define(head, clauses)`, where the head names the
242
- predicate being replaced and must be one of the predicates the block defines.
243
- Never patch it clause by clause.
244
- - Open the transaction once with `begin_turn`; it is idempotent and returns a
245
- reminder if it is already open. Do not call it again before every edit.
246
- - `assert`/`assert_many` are only for a **new** predicate: they reject a
247
- predicate that already has any clauses. This includes `doc/2` and other
248
- annotation/fact predicates. There is no append operation in the authoring
249
- tools. To change an existing predicate, use `define` with its complete clause
250
- set; to add a `doc/2` fact, include all existing doc facts in that `define`.
251
- - `retract` removes the clauses matching its pattern and says how many; `delete`
252
- removes a whole predicate. `retract` fails when nothing matched — check that
253
- the pattern is the one you meant.
254
- - Clause order matters: base case before a recursive rule; give a whole predicate
255
- to `define`/`assert_many` at once (clauses in the order written).
256
- - Verify after changing a predicate: `listing(Name/Arity)`.
257
- - Bounded iteration only: finite recursion or an explicit bound. `execute` is
258
- gas-limited and the turn has a model-call budget, but a single builtin call
259
- (e.g. an unbounded `label/1`) is not interrupted.
260
- - **CLP is advanced — use it only for a real constraint problem.** Bound every
261
- domain (`X ins 0..N`) before `label/1`, prefer `once/1`, and keep the model
262
- small: an under-specified or unsatisfiable model searches forever, and there
263
- is no per-goal timeout. Never reach for CLP as a shortcut for arithmetic.
264
- - **Know whether you are sandboxed.** The session's environment block says which
265
- backend is in effect. With a sandbox (`bwrap`, AgentVM, `sandbox-exec`),
266
- `bash` is isolated and writes outside the workspace are confined. With
267
- `clean-room` there is **no** sandbox: commands run as the user, writes outside
268
- the workspace are real files that persist, and a `rm` is a real `rm`. Explore
269
- what is available (`python3 --version`, `pip list`, `which jq`) and install a
270
- dependency when a skill genuinely needs one; installs are sandbox/session-local
271
- and do not travel with an exported harness, so prefer the standard library.
272
- - If a harness genuinely requires isolation, the **user** can add
273
- `:- require_sandbox.` (through `/set-require-sandbox`). It makes the load fail
274
- rather than run unsandboxed. Do not add it yourself.
275
- - Do **not** use `agent_main` unless the user asks for a `deepclause run` /
276
- `/dc-run` skill.
277
- - Media: reference images with `media(Path, Mime)` (`read_media/2` +
278
- `attach_media/1`); never inline bytes.
279
-
280
- ## 7. Concurrency and subagents
281
-
282
- Most skills are a short sequence of `task/N` calls in the foreground. Reach for a
283
- **subagent** only when the work must outlive the turn, wait for a message, or run
284
- in parallel.
285
-
286
- - **Fan out over a list** (`swarm/3`, `pmap/3`): the same worker over many items,
287
- each in its own process, results in input order. Bound the list.
288
- - **Background a long goal** (`spawn/2`): report the id and carry on; the user
289
- watches it with `/ps --watch --logs`.
290
- - **A service** (`serve/2` — or `serve/3` for the pid — plus `service_call/3`): a long-lived `receive` loop that
291
- answers requests; also good for a single-writer or rate-limited step.
292
-
293
- A subagent is isolated (private module snapshot) and asynchronous (value
294
- semantics): its `assert`/`retract` do not touch the harness, and a result comes
295
- back by `send/2` or the child's `answer/1`, never by an output argument. Prefer
296
- `swarm` over hand-rolled `spawn`+`receive` unless you need a custom protocol.
297
-
298
- Do not fan out an unbounded list (each item is a process and usually a model
299
- call), and do not spawn for a single model call.
1
+ # Authoring a living harness
2
+
3
+ Start small, then improve. Use the smallest end-to-end design that satisfies the
4
+ request, verify it on real input and show the user. Reuse existing predicates;
5
+ do not build speculative abstractions, duplicate helpers or a predicate per one-off
6
+ request. A harness is reusable capabilities, not a log of everything performed.
7
+
8
+ ## Design and inspect
9
+
10
+ Call `listing(filter: "all")` first, then relevant `Name/Arity` entries.
11
+ Clarify input, output, execution surface and evidence of correctness. If ambiguous,
12
+ ask one question before editing; once settled, do not reopen the same decision.
13
+ Normal conversational agents use `shell/1` plus tools/workflows, not a custom loop.
14
+ Use explicit argument modes and typed model outputs from the first small version.
15
+
16
+ Simple exact logic stays in DML; substantial parsing/file algorithms belong in
17
+ scripts; semantic work uses typed prompt/task calls. Choose by actual need, not
18
+ by a blanket ban on Prolog or a blanket preference for models. Read `patterns`
19
+ for a matching small shape and `examples` for a complete tested one.
20
+
21
+ ## Transactions
22
+
23
+ `begin_turn` opens a snapshot; call it once, not before every define. `define`
24
+ and `delete` are refused without an open transaction. `end_turn` validates and
25
+ commits a version; `rollback` discards the open edit. After committing, open a
26
+ new transaction before editing again. Check per-call results and end_turn warnings.
27
+ `committed: false` or no changed predicates means the intended edit may not have
28
+ landed. Verify `listing` rather than asserting it worked.
29
+
30
+ Change the module only through those tools. Do not consult/load a workspace DML
31
+ file to bypass edits. A system library directive is different from importing a
32
+ user DML file. The user can load a harness as a separate action.
33
+
34
+ ## Define targets
35
+
36
+ `define(head, clauses)` adds a NEW or replaces EXISTING complete predicate block.
37
+ Head is an indicator `"foo/2"` or actual head `"foo(A, B)"`, not a function name
38
+ whose arity you hope the tool infers. The block must define the target and may
39
+ include related predicates/annotations. All clauses end with their own period.
40
+ Preserve ordering; replacing a recursive predicate requires all its clauses.
41
+
42
+ For keyed annotations, use the actual two-argument head:
43
+ `define("doc(foo/2, _)", "doc(foo/2, \"...\").")`.
44
+ `doc(foo/2)` would name doc/1 and is rejected. `doc/2`, `schema/2`, `script_file/2`
45
+ merge by key so other entries survive. Changing foo/3 to foo/2 creates a different
46
+ predicate: delete the old one/doc key and define the new one in one transaction.
47
+
48
+ ## Delete targets
49
+
50
+ - `delete("foo/2")`: whole predicate and its associated nested tool annotation.
51
+ - `delete("tool:foo/2")`: nested tool annotation, keeping delegate.
52
+ - `delete("doc:foo/2")`: one documentation entry.
53
+ - `delete("schema:person")`: one schema key.
54
+ - `delete("script_file:extract_py")`: one script link, not the file.
55
+
56
+ Missing annotation keys are errors. No assertion/retraction tools are offered.
57
+ Use define with the complete remaining clause set to remove one clause.
58
+ Script link names must differ from all user predicate names; prefer `_py` or
59
+ `script_` link names, not the predicate they implement.
60
+
61
+ ## Documentation and scripts
62
+
63
+ `module_doc(Text)` describes purpose/when to use the harness. `doc(Name/Arity, Text)`
64
+ records inputs/outputs and an example goal; count every actual predicate argument.
65
+ `script_file(Name, Path)` links a workspace file for export. Write a script once
66
+ with the authoring write_file tool, then link/call it. Do not repeatedly overwrite
67
+ helper source every time a workflow runs. Unlinked files are not promised portable.
68
+ Do not hardcode user-specific host paths or secrets in code/facts.
69
+
70
+ ## Test and hand over
71
+
72
+ A small pure validation test, actual bounded input, error case and cancellation
73
+ case where relevant are enough for the first version. Side effects must be
74
+ intentional and single-solution. Debug intermediate values with output/1 rather
75
+ than adding more model steps. Foreground form workflows cannot be exercised in
76
+ browser scratch execute; ask for a foreground run and test pure rules separately.
77
+
78
+ End_turn warnings are contract evidence, not a reason to add bogus alias predicates
79
+ to satisfy stale docs. Correct the annotation or arity. Clean up fixtures and test
80
+ processes you created, but never delete unrelated user files. Summarize what was
81
+ observed, what is still unverified and how to run it.
@@ -0,0 +1,93 @@
1
+ # DML basics
2
+
3
+ DML is Prolog with host-interpreted operations for models, tools, forms and
4
+ processes. A harness is a library; `agent_main/N` is optional for positional runs.
5
+ Read these contracts when syntax or failure behavior is uncertain.
6
+
7
+ ## Terms and arguments
8
+
9
+ Variables start uppercase or `_`; lowercase words are atoms, and double quotes
10
+ are strings. `'two words'` is a quoted atom, not a string. `true` and `false`
11
+ are atoms. A bare `_` is a fresh anonymous variable on every occurrence.
12
+ Lists are `[a,b,c]` or `[Head|Tail]`; dicts are `_{name: "Ada", age: 36}` with
13
+ ATOM keys. Read controlled keys with `get_dict(name, D, X)`, varying model keys
14
+ with `dict_field(D, name, X)`.
15
+
16
+ Predicates are relations, not functions. `greet("Ada", Greeting)` supplies an
17
+ input and leaves the output variable to be bound. `greet/2` counts BOTH
18
+ arguments. `greet(+Name, -Text)` is documentation for intended modes.
19
+ Do not call `Text = greet("Ada")` expecting evaluation.
20
+
21
+ ## Clauses and goals
22
+
23
+ A fact states a relation; a rule gives a body that must succeed:
24
+
25
+ ```prolog
26
+ team(billing).
27
+ team(orders).
28
+ greet(Name, Text) :- format(string(Text), "Hello ~w", [Name]).
29
+ ```
30
+
31
+ Every fact/rule ends with a period, including `doc/2`, `schema/2`, `script_file/2`
32
+ and `shell/1`. Newlines alone do not separate clauses. `%` starts a line comment.
33
+ A query such as `greet("Ada", X)` runs the body and returns `X = "Hello Ada"`.
34
+ A predicate may have several clauses; their written order matters.
35
+
36
+ ## Unification and arithmetic
37
+
38
+ `X = Y` unifies terms, binding variables if possible. `X = 1 + 2` binds a TERM,
39
+ not 3. `X is 1 + 2` evaluates arithmetic; inputs must be bound numbers.
40
+ `A =:= B` compares evaluated numbers. `A == B` tests identical terms without
41
+ binding; `A \== B` tests non-identity now. `dif(A, B)` constrains them to remain
42
+ different. A string `"yes"` and atom `yes` are not identical.
43
+
44
+ Use `number_string/2` or `atom_string/2` deliberately at a type boundary; do not
45
+ sprinkle conversions blindly. Model type wrappers bind actual numbers/booleans.
46
+
47
+ ## Control flow
48
+
49
+ `A, B` is conjunction, `A ; B` disjunction. Parenthesize branching:
50
+
51
+ ```prolog
52
+ size_label(Text, Label) :-
53
+ string_length(Text, N),
54
+ ( N > 100 -> Label = long ; Label = short ).
55
+ ```
56
+
57
+ `\+ Goal` means the goal cannot be proved under current bindings. It is not
58
+ semantic truth or a model judgment. `catch(Goal, E, Recovery)` catches EXCEPTIONS,
59
+ not ordinary failure. `throw(Error)` raises an exception.
60
+
61
+ ## Failure and determinism
62
+
63
+ A false guard or missing relation yields zero solutions; it may not throw.
64
+ `findall(X, Goal, Xs)` yields `[]` for no solutions, while `setof`/`bagof` fail.
65
+ `get_dict` fails for a missing key and throws for an invalid key/dict type.
66
+
67
+ Pure relations can enumerate intentionally. `once(Goal)` chooses the first
68
+ solution, `!` discards local alternatives, and `(C -> T ; E)` commits the
69
+ condition. Select a deterministic input BEFORE a model/file/network effect.
70
+ An earlier choicepoint can replay a later effect on backtracking. A trailing
71
+ cut only commits paths that reached it. Do not retry irreversible actions by
72
+ backtracking; use explicit, bounded, safe retry policies.
73
+
74
+ ## Values and output
75
+
76
+ A reusable skill, nested tool or shell workflow binds its output arguments and
77
+ returns. The caller composes those values.
78
+ `output(Text)` emits progress/display without adding model memory.
79
+ `answer(Text)` emits the final one-shot answer and terminates the goal; it is NOT
80
+ `end_turn` and does not assign workflow outputs. Use it at a run entry point,
81
+ not in a helper whose caller must continue.
82
+
83
+ `system/1`/`user/1` add DML harness memory, not authoring conversation.
84
+ See `reference(topic: "tasks", section: "memory")` before relying on context.
85
+
86
+ ## Standard glue
87
+
88
+ Use `maplist/3` for sequential mapping; it passes each item plus an output to a
89
+ predicate. Use `include/3`, `exclude/3`, `memberchk/2`, `findall/3`, `sort/2`,
90
+ `msort/2`, `sum_list/2`, JSON helpers and `format/3` for small exact operations.
91
+ `sort/2` removes duplicates; `msort/2` keeps them. `directory_files/2` gives bare
92
+ entry names. Do not hand-build a large parser/list algorithm when a script fits.
93
+ `reference(topic: "snippets")` has precise short recipes.
@@ -0,0 +1,73 @@
1
+ # Debugging DML
2
+
3
+ Reproduce the smallest failing goal, inspect the actual result, then change the
4
+ smallest relevant predicate. A supported contract is cheaper to read than guesses.
5
+
6
+ ## Zero solutions versus exceptions
7
+
8
+ `solutions: []` means the goal failed; `catch/3` will not intercept ordinary
9
+ failure. Inspect each boundary: input type, missing field, guard, host tool, output
10
+ binding. Use `(Goal -> output("succeeded") ; output("failed here"))` and temporary
11
+ `output/1` calls with intermediate values. Remove noisy diagnostics afterwards.
12
+ Do not turn a failed fetch or cancelled form into invented success data.
13
+ An empty predicate and a handler primitive are different: `current_predicate`
14
+ can report false for built-in DML handlers such as `form/2` or `exec/2`.
15
+
16
+ ## Model calls
17
+
18
+ Read `tasks` for memory/type/tool scope errors. A bare output is a string, not a
19
+ number; use type wrappers. A task sees harness memory, not the authoring request.
20
+ A prompt sees only its own description and no attached memory images.
21
+ Pass exact inputs, include them in the description, and guard empty data before
22
+ a model can fill gaps from prior knowledge. A tool named in prose is not registered.
23
+
24
+ ## Host failures
25
+
26
+ Read the CURRENT schema via `tools` before retrying a parameter error. Inspect
27
+ host failure, `exitCode`, `stderr`, `stdout` separately; a nonzero Python exit may
28
+ still return a dict. `run_script/3` checks exit and parses JSON for you.
29
+ In the browser, Python has no stdin option and DML/Python private tmp directories
30
+ are different. Use workspace argv/files, not source-code interpolation.
31
+ For script-name conflicts, rename the link (`foo_py`) or predicate; use
32
+ `delete("script_file:old_name")` for a stale link.
33
+
34
+ ## Execution contexts
35
+
36
+ | Context | Structured forms | Test isolation |
37
+ |---|---|---|
38
+ | Foreground query/run with UI | yes, via session handler | normal session/run |
39
+ | Work workflow/tool foreground runner | yes, via session handler | not disposable test worker |
40
+ | Browser authoring execute worker | no | disposable engine; host/file effects real |
41
+ | Browser worker-hosted process | no | private code/memory snapshot; live shared workspace |
42
+
43
+ The same goal can be valid but unsupported in a particular host. Interactive form
44
+ flows must be tested in foreground, not by repeatedly trying scratch execute.
45
+ Test pure validation separately; cancellation/unavailable is NOT a submitted record.
46
+ No capability directive is necessary. Close forms on every exit path.
47
+
48
+ ## Edits and annotations
49
+
50
+ Read `authoring` for transaction/target contracts. `define` takes a head matching
51
+ the actual predicate arity. For one doc use `doc(foo/2, _)`, NOT `doc(foo/2)`.
52
+ Arity changes add a different predicate; remove the old predicate/doc key explicitly.
53
+ `delete("doc:foo/2")`, `delete("schema:Name")`, `delete("script_file:Name")`
54
+ remove one entry, never all docs/schemas/scripts. Missing keys are errors.
55
+ Read the end_turn result, not your intent; verify `listing` after changes.
56
+
57
+ ## Process failures
58
+
59
+ Use `ps`, process state/errors, and the process harness version. A service keeps
60
+ its spawn-time snapshot; an edit does not hot-update it. Children cannot bind
61
+ parent outputs. Use messages or wait outcomes, inspect `failed` swarm items, bound
62
+ receives, and stop/reap test processes. Do not equate zero model tokens with a dead
63
+ service. See `processes` for exact signatures.
64
+
65
+ ## Logs and bounds
66
+
67
+ Read only a short tail of `.deepclause/repl/sessions/<id>/events.jsonl` using the
68
+ available host capability when normal tool results are insufficient. Logs can
69
+ contain user data, prompts and arguments. Do not dump them wholesale to the model.
70
+ Use execute's `timeoutMs`, result limits and reduced bindings for scratch tests;
71
+ foreground goals/processes have different cancellation guarantees. Bound finite
72
+ search and builtin inference separately; gas alone cannot preempt every builtin.
73
+ After a couple of failed attempts, report the evidence and remaining uncertainty.