deepclause-sdk 0.3.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 (135) hide show
  1. package/README.md +148 -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 +467 -53
  16. package/dist/cli/repl.js.map +1 -1
  17. package/dist/cli/run.d.ts.map +1 -1
  18. package/dist/cli/run.js +4 -0
  19. package/dist/cli/run.js.map +1 -1
  20. package/dist/cli/set-backend.d.ts +2 -2
  21. package/dist/cli/set-backend.d.ts.map +1 -1
  22. package/dist/cli/set-backend.js +54 -8
  23. package/dist/cli/set-backend.js.map +1 -1
  24. package/dist/cli/work.d.ts +16 -0
  25. package/dist/cli/work.d.ts.map +1 -0
  26. package/dist/cli/work.js +45 -0
  27. package/dist/cli/work.js.map +1 -0
  28. package/dist/index.d.ts +8 -6
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +4 -3
  31. package/dist/index.js.map +1 -1
  32. package/dist/prolog-src/deepclause_mi.pl +2596 -268
  33. package/dist/prolog-src/deepclause_strings.pl +36 -3
  34. package/dist/repl/agent.d.ts +12 -0
  35. package/dist/repl/agent.d.ts.map +1 -1
  36. package/dist/repl/agent.js +40 -16
  37. package/dist/repl/agent.js.map +1 -1
  38. package/dist/repl/auth.d.ts +6 -3
  39. package/dist/repl/auth.d.ts.map +1 -1
  40. package/dist/repl/auth.js +55 -5
  41. package/dist/repl/auth.js.map +1 -1
  42. package/dist/repl/backend.d.ts +24 -0
  43. package/dist/repl/backend.d.ts.map +1 -1
  44. package/dist/repl/backend.js +292 -12
  45. package/dist/repl/backend.js.map +1 -1
  46. package/dist/repl/compaction.d.ts +2 -0
  47. package/dist/repl/compaction.d.ts.map +1 -1
  48. package/dist/repl/compaction.js +9 -0
  49. package/dist/repl/compaction.js.map +1 -1
  50. package/dist/repl/event-history.d.ts +4 -0
  51. package/dist/repl/event-history.d.ts.map +1 -0
  52. package/dist/repl/event-history.js +33 -0
  53. package/dist/repl/event-history.js.map +1 -0
  54. package/dist/repl/harness-tools.d.ts +26 -18
  55. package/dist/repl/harness-tools.d.ts.map +1 -1
  56. package/dist/repl/harness-tools.js +103 -34
  57. package/dist/repl/harness-tools.js.map +1 -1
  58. package/dist/repl/history.d.ts +75 -3
  59. package/dist/repl/history.d.ts.map +1 -1
  60. package/dist/repl/history.js +155 -4
  61. package/dist/repl/history.js.map +1 -1
  62. package/dist/repl/index.d.ts +33 -5
  63. package/dist/repl/index.d.ts.map +1 -1
  64. package/dist/repl/index.js +30 -0
  65. package/dist/repl/index.js.map +1 -1
  66. package/dist/repl/reference.d.ts +31 -0
  67. package/dist/repl/reference.d.ts.map +1 -0
  68. package/dist/repl/reference.js +143 -0
  69. package/dist/repl/reference.js.map +1 -0
  70. package/dist/repl/render.d.ts +3 -1
  71. package/dist/repl/render.d.ts.map +1 -1
  72. package/dist/repl/render.js +17 -0
  73. package/dist/repl/render.js.map +1 -1
  74. package/dist/repl/session.d.ts +166 -23
  75. package/dist/repl/session.d.ts.map +1 -1
  76. package/dist/repl/session.js +980 -188
  77. package/dist/repl/session.js.map +1 -1
  78. package/dist/repl/tui/autocomplete.d.ts.map +1 -1
  79. package/dist/repl/tui/autocomplete.js +5 -1
  80. package/dist/repl/tui/autocomplete.js.map +1 -1
  81. package/dist/repl/tui/renderer.d.ts +2 -1
  82. package/dist/repl/tui/renderer.d.ts.map +1 -1
  83. package/dist/repl/tui/renderer.js +20 -3
  84. package/dist/repl/tui/renderer.js.map +1 -1
  85. package/dist/repl/tui/status-bar.d.ts +5 -2
  86. package/dist/repl/tui/status-bar.d.ts.map +1 -1
  87. package/dist/repl/tui/status-bar.js +8 -3
  88. package/dist/repl/tui/status-bar.js.map +1 -1
  89. package/dist/repl/tui/tool-pane.d.ts +1 -1
  90. package/dist/repl/tui/tool-pane.d.ts.map +1 -1
  91. package/dist/repl/tui/tool-pane.js +13 -7
  92. package/dist/repl/tui/tool-pane.js.map +1 -1
  93. package/dist/repl/tui/work-view.d.ts +10 -0
  94. package/dist/repl/tui/work-view.d.ts.map +1 -0
  95. package/dist/repl/tui/work-view.js +28 -0
  96. package/dist/repl/tui/work-view.js.map +1 -0
  97. package/dist/runner.d.ts +7 -1
  98. package/dist/runner.d.ts.map +1 -1
  99. package/dist/runner.js +81 -23
  100. package/dist/runner.js.map +1 -1
  101. package/dist/runtime-identity.json +10 -0
  102. package/dist/system/assets/docs/AGENT_PROMPT.md +265 -2943
  103. package/dist/system/assets/docs/DML_AUTHORING.md +81 -295
  104. package/dist/system/assets/docs/DML_BASICS.md +93 -0
  105. package/dist/system/assets/docs/DML_DEBUGGING.md +73 -0
  106. package/dist/system/assets/docs/DML_FORMS.md +94 -728
  107. package/dist/system/assets/docs/DML_MEDIA.md +63 -0
  108. package/dist/system/assets/docs/DML_PROCESSES.md +100 -0
  109. package/dist/system/assets/docs/DML_PYTHON.md +74 -0
  110. package/dist/system/assets/docs/DML_REFERENCE.md +224 -1912
  111. package/dist/system/assets/docs/DML_SHELL.md +107 -0
  112. package/dist/system/assets/docs/DML_TASKS.md +112 -0
  113. package/dist/system/assets/docs/HARNESS_EXAMPLES.md +255 -179
  114. package/dist/system/assets/docs/HARNESS_PATTERNS.md +65 -538
  115. package/dist/system/assets/docs/LOCAL_AGENT_PROMPT.md +186 -0
  116. package/dist/system/assets/docs/PROLOG_SNIPPETS.md +61 -154
  117. package/dist/system/assets/index.d.ts +1 -1
  118. package/dist/system/assets/index.d.ts.map +1 -1
  119. package/dist/system/assets/index.js +7 -0
  120. package/dist/system/assets/index.js.map +1 -1
  121. package/dist/system/environment.d.ts +1 -1
  122. package/dist/system/environment.d.ts.map +1 -1
  123. package/dist/system/environment.js +10 -6
  124. package/dist/system/environment.js.map +1 -1
  125. package/dist/system/runtime/shell-manager.d.ts +1 -1
  126. package/dist/system/runtime/shell-manager.d.ts.map +1 -1
  127. package/dist/types.d.ts +62 -2
  128. package/dist/types.d.ts.map +1 -1
  129. package/dist/work/index.d.ts +82 -0
  130. package/dist/work/index.d.ts.map +1 -0
  131. package/dist/work/index.js +232 -0
  132. package/dist/work/index.js.map +1 -0
  133. package/package.json +12 -3
  134. package/src/prolog-src/deepclause_mi.pl +2596 -268
  135. package/src/prolog-src/deepclause_strings.pl +36 -3
@@ -1,266 +1,342 @@
1
- # Idiomatic harness examples
1
+ # Runnable canonical examples
2
2
 
3
- Copy-paste shapes for REPL harnesses. Keep each skill small and compose them.
4
- (The snippets are tested against the bundled SWI-Prolog.)
3
+ Generated from scripts/canonical-examples.mjs; do not hand-copy into the system prompt.
4
+ Pick ONE matching section. Each skill binds its outputs; agent_main is only used
5
+ for the explicitly custom conversation. Host-dependent examples list prerequisites.
5
6
 
6
- Most skills should look like §2–§3: a short sequence of `task/N`/`prompt/N` calls
7
- with thin guards between them. §4–§7 (facts, pure logic, judgments, CLP) are the
8
- exception — reach for them only when the task is genuinely data- or
9
- logic-shaped, not as the default style. DML is the orchestration layer over model
10
- calls, not the logic library (`DML_AUTHORING.md` §0). For file work, shell out
11
- (`exec(bash(...))`) instead of reading files into DML (§2).
7
+ ## work-agent
12
8
 
13
- Every skill carries a `doc(Name/Arity, Text)` annotation and the harness a
14
- `module_doc(Text)` line (`DML_AUTHORING.md` §4). `module_doc/1` is the harness's
15
- **skill description** — what it does and when to use it; it is the first line of
16
- the manifest, which the agent reads at session start and whenever it changes. A
17
- file with only `module_doc/1` is still a valid skill (an advertised capability
18
- with an empty body). Keep both terse.
9
+ **Default Work agent with a tool and workflow**
10
+
11
+ The default conversation is supplied by shell/1. A custom persona does not need a DML chat loop. Inputs precede outputs; tools are not directly run from the sidebar.
12
+
13
+ Requires: Work host; title workflow requires a text model. The character-count test is pure DML.
19
14
 
20
15
  ```prolog
21
- module_doc("Example harness: a few small skills used as patterns. Use when you need a copy-paste starting point.").
16
+ length_of(Text, N) :- string_length(Text, N).
17
+ title(Text, Label) :- prompt("Give a short title for: {Text}", string(Label)).
18
+ shell(_{
19
+ kind: agent, system: "You are a concise writing assistant.", host_tools: [],
20
+ tools: [_{name: "text_length", goal: "length_of", description: "Count characters.",
21
+ inputs: [_{name: "Text", type: string}], outputs: [_{name: "N", type: integer}]}],
22
+ workflows: [_{name: "Create title", goal: "title",
23
+ inputs: [_{name: "Text", type: string}], outputs: [_{name: "Label", type: string}]}],
24
+ welcome: "Ask about text, or create a title."
25
+ }).
22
26
  ```
23
27
 
24
- ## 1. Wrap a workspace script as a tool
28
+ Test goal: `length_of("Ada", N), write(N)`. Expected: `3`.
29
+
30
+ Failure/cancel check: Do not replace output binding with answer/1 or add a custom conversation loop.
31
+
32
+ ## basic-relation
33
+
34
+ **Arguments, arithmetic and a deterministic branch**
35
+
36
+ N is an input; Label is an output. is/2 evaluates arithmetic, = binds a returned value. A failed input guard is not an exception.
25
37
 
26
- Materialize the program as a workspace file (run a DML goal with
27
- `make_directory_path("scripts"), write_file("scripts/count_words.py", "…")`),
28
- link it with `script_file/2`, and define a predicate for it. `tool/2` is only an
29
- annotation that exposes an existing predicate to `task/N`; keep it a one-line
30
- delegation so the predicate is also callable from `execute` / `/goal`:
38
+ Requires: No host tools or model.
31
39
 
32
40
  ```prolog
33
- script_file(count_words, ".dc-repl/scripts/count_words.py").
34
-
35
- % The capability: callable from `execute` / `/goal` and from `task/N`.
36
- count_words(File, Count) :-
37
- script_file(count_words, Script),
38
- format(atom(Cmd), "python3 '~w' '~w'", [Script, File]),
39
- exec(bash(command: Cmd), Result),
40
- get_dict(stdout, Result, Count).
41
-
42
- % The annotation exposing it to the nested agent (delegation only).
43
- tool(count_words(File, Count), "Count words in a workspace file.") :-
44
- count_words(File, Count).
41
+ size_label(N, Label) :-
42
+ integer(N), N >= 0,
43
+ Twice is N * 2,
44
+ ( Twice > 10 -> Label = large ; Label = small ).
45
45
  ```
46
46
 
47
- Untrusted data goes through files/stdin, never interpolated into the shell
48
- command. `script_file/2` is just a fact (the exporter bundles the file); the
49
- write lands on disk immediately, so `bash` can run the script in the same turn.
47
+ Test goal: `size_label(2, Label), write(Label)`. Expected: `small`.
50
48
 
51
- ## 2. A skill that calls the model
49
+ Failure/cancel check: size_label(-1, _) has zero solutions; catch/3 does not catch a false guard.
52
50
 
53
- `task/N` runs a bounded agentic turn. Name each output variable in the
54
- description so the model fills it:
51
+ ## typed-pipeline
52
+
53
+ **Fresh typed transformation and explicit data flow**
54
+
55
+ Each prompt includes its input. No tool or previous-memory dependency. Reject empty intermediate data before the next semantic step.
56
+
57
+ Requires: Text model; tests supply deterministic typed completions.
55
58
 
56
59
  ```prolog
57
- doc(classify/2, "Route a support message. classify(+Message, -Team). Example: /goal classify(\"I was charged twice\", T).").
60
+ brief(Text, Report) :-
61
+ prompt("Extract the central claim from: {Text}", string(Claim)),
62
+ Claim \== "",
63
+ prompt("Explain this claim using only the source {Text}: {Claim}", string(Report)).
64
+ ```
65
+
66
+ Test goal: `brief("A source document", Report), write(Report)`. Expected: `A grounded summary.`.
58
67
 
59
- classify(Message, Team) :-
60
- task("Route this support message to exactly one of billing, orders, or account. Message: {Message}. Store the choice in Team.", Team).
68
+ Failure/cancel check: An empty Claim fails before generating a report.
61
69
 
62
- demo :-
63
- classify("I was charged twice for the same order", Team),
64
- format(string(Out), "Routed to ~w", [Team]),
65
- answer(Out).
70
+ ## task-tools
71
+
72
+ **A scoped task calls one declared capability**
73
+
74
+ Host tools, nested tool/N annotations and shell tools are different surfaces. This task may only call length_of; the real predicate remains callable directly.
75
+
76
+ Requires: Text model with tool calling; test checks the capability really executes.
77
+
78
+ ```prolog
79
+ length_of(Text, N) :- string_length(Text, N).
80
+ tool(length_of(Text, N), "Count characters.", [string]) :- length_of(Text, N).
81
+ inspect(Text, Reply) :-
82
+ with_tools([length_of], task("Call length_of for {Text}; report the observed count.", string(Reply))).
66
83
  ```
67
84
 
68
- ## 3. Multi-step pipeline: task -> task -> answer
85
+ Test goal: `inspect("Ada", Reply), write(Reply)`. Expected: `3 characters`.
86
+
87
+ Failure/cancel check: Naming length_of in prose without registering its annotation would not expose it.
88
+
89
+ ## task-memory
90
+
91
+ **Task memory accumulates; a fresh prompt does not inherit it**
92
+
93
+ Task memory is DML harness memory, not the authoring chat. The tasks share a fresh group, then a separate prompt receives only explicitly supplied data.
94
+
95
+ Requires: Text model; tests inspect actual request memory, not only returned strings.
69
96
 
70
97
  ```prolog
71
- report(Topic, Result) :-
72
- task("List three risks of using an LLM for arithmetic. Store them in Risks.", Risks),
73
- format(string(Desc), "Rank these risks and explain the top one: ~w", [Risks]),
74
- task(Desc, Top),
75
- format(string(Result), "Top risk: ~w", [Top]).
98
+ memory_demo(Out) :-
99
+ with_fresh_memory(with_tools([], (
100
+ task("Return marker alpha.", string(First)),
101
+ task("Report the earlier marker. Explicit value: {First}", string(Second))
102
+ ))),
103
+ prompt("Report only this explicit result: {Second}", string(Out)).
76
104
  ```
77
105
 
78
- Compose the steps and let the model do the thinking; keep only thin guards
79
- between them — an if-then-else for a branch, `once/1` to commit the first
80
- answer, or a simple check that fails a step:
106
+ Test goal: `memory_demo(Out), write(Out)`. Expected: `Memory scopes checked.`.
107
+
108
+ Failure/cancel check: Do not assume a prompt sees a prior task or a nested tool inherits the Work transcript.
109
+
110
+ ## judgment-routing
111
+
112
+ **A constrained semantic choice with explicit evidence**
113
+
114
+ Choose returns one option atom; deterministic branching follows it. A semantic judgment is not proof that an external action was performed.
115
+
116
+ Requires: Configured judgment backend (the test uses the session model with a deterministic judge response).
81
117
 
82
118
  ```prolog
83
- summarise(Doc, Summary) :-
84
- task("Extract the document's main claim. Store it in Claim.", Claim),
85
- ( Claim == ""
86
- -> Summary = "no claim found"
87
- ; task("Summarise this claim in one sentence: {Claim}", Summary)
119
+ route(Message, Team) :-
120
+ choose(Message, "Which team handles this?", [billing, orders, account], Choice),
121
+ ( Choice == billing -> Team = billing
122
+ ; Choice == orders -> Team = orders
123
+ ; Team = account
88
124
  ).
89
125
  ```
90
126
 
91
- ## 4. Data as facts (data = code)
127
+ Test goal: `route("I was charged twice", Team), write(Team)`. Expected: `billing`.
92
128
 
93
- Small/medium data belongs in the module and is queried exactly like code:
129
+ Failure/cancel check: Unknown evidence is not a reason to claim that a real refund happened.
130
+
131
+ ## schema-output
132
+
133
+ **A declared schema rather than guessed dict keys**
134
+
135
+ The schema is an output contract, not merely a suggested JSON shape. Missing required fields and invalid types fail validation.
136
+
137
+ Requires: Text model; deterministic schema-aware test output.
94
138
 
95
139
  ```prolog
96
- parent(alice, bob).
97
- parent(alice, carol).
98
- parent(bob, dave).
99
- parent(carol, eve).
140
+ schema(person, [field(name, string, required), field(age, integer, default(0))]).
141
+ extract_person(Text, Person) :-
142
+ prompt("Extract a person from: {Text}", schema(person, Person)).
143
+ ```
100
144
 
101
- doc(ancestor/2, "Transitive parent. ancestor(+A, -B) enumerates descendants. Example: /goal ancestor(alice, X).").
145
+ Test goal: `extract_person("Ada, age 36", Person), get_dict(name, Person, Name), write(Name)`. Expected: `Ada`.
102
146
 
103
- ancestor(A, B) :- parent(A, B).
104
- ancestor(A, B) :- parent(A, C), ancestor(C, B).
147
+ Failure/cancel check: Missing name must not be silently converted to an empty string.
148
+
149
+ ## python-files
150
+
151
+ **Python, files and argv**
152
+
153
+ Write/link the helper during authoring, not on each run. Script link normalise_py is distinct from the normalise/2 predicate. run_script checks exit status and parses JSON.
154
+
155
+ Requires: Python host capability (or a compatible script execution embedding). Files below are fixtures to create before calling the skill.
156
+
157
+ ```prolog
158
+ script_file(normalise_py, "scripts/normalise.py").
159
+ normalise(Path, Text) :-
160
+ run_script(normalise_py, [Path], Data),
161
+ get_dict(text, Data, Text).
105
162
  ```
106
163
 
164
+ Test goal: `normalise("canonical/input.txt", Text), write(Text)`. Expected: `HELLO`.
165
+
166
+ Failure/cancel check: A missing input file produces a script_failed error; no model fills the missing text.
167
+
168
+ Fixture `canonical/input.txt` (create before run):
169
+
107
170
  ```text
108
- /goal ancestor(alice, X).
109
- X = bob ; X = carol ; X = dave ; X = eve
171
+ hello
110
172
  ```
111
173
 
112
- ## 5. Pure logic + a test predicate
174
+ Fixture `scripts/normalise.py` (create before run):
113
175
 
114
- ```prolog
115
- doc(tally/2, "Count occurrences of each item. tally(+List, -Counts) yields Key-N pairs. Example: /goal tally([a,b,a,c,a], C).").
176
+ ```python
177
+ import json, sys
178
+ from pathlib import Path
179
+ print(json.dumps({'text': Path(sys.argv[1]).read_text().upper()}))
180
+ ```
116
181
 
117
- tally(List, Counts) :-
118
- list_to_set(List, Items),
119
- findall(Item-N, (member(Item, Items), aggregate_all(count, member(Item, List), N)), Counts).
182
+ ## swarm
120
183
 
121
- test_tally :-
122
- tally([a,b,a,c,a], C),
123
- C == [a-3, b-1, c-1].
124
- ```
184
+ **Bounded parallel mapping with visible failures**
125
185
 
126
- `/goal test_tally.` succeeds when the behavior is correct. When a skill changes,
127
- update or add its test and run it (`execute` during a turn, or `/goal`).
186
+ Results preserve input order. Every item has its own process; failed items remain visible. Use pmap only if any failure should fail the whole batch.
128
187
 
129
- ## 6. Semantic judgments (the model decides, Prolog enforces)
188
+ Requires: Process-capable session; no model.
130
189
 
131
190
  ```prolog
132
- route(Message, Team) :-
133
- choose(Message, "Which team should handle this?", [billing, orders, account], Team).
134
-
135
- urgent(Message) :-
136
- holds(Message, "Is this urgent?").
191
+ double(N, R) :- R is N * 2.
192
+ parallel_values(Items, Results) :- swarm(double, Items, Results, 2).
137
193
  ```
138
194
 
139
- `choose/4` always returns one of your options; `verify/3` is three-valued
140
- (`yes`/`no`/`unknown`); `holds/2` is the semidet wrapper.
195
+ Test goal: `parallel_values([1,2,3], Results), write(Results)`. Expected: `[ok(2),ok(4),ok(6)]`.
196
+
197
+ Failure/cancel check: Inspect failed items rather than assuming all results succeeded.
198
+
199
+ ## spawn-python
141
200
 
142
- ## 7. Constraints with CLP(FD)
201
+ **Spawn a worker and return by message**
202
+
203
+ Create required files before spawn. Child output variables do not bind the parent; send a result, receive it with a bound, then reap the worker.
204
+
205
+ Requires: Python and process host. Test fixture canonical/input.txt exists before spawn.
143
206
 
144
207
  ```prolog
145
- :- use_module(library(clpfd)).
208
+ read_in_worker(Path, Out) :-
209
+ self(Me), spawn(file_worker(Path, Me), Pid),
210
+ receive(done(Out), 30000), wait(Pid, _).
211
+ file_worker(Path, Parent) :-
212
+ exec(python(code: "from pathlib import Path\nimport sys\nprint(Path(sys.argv[1]).read_text().upper())", argv: [Path]), R),
213
+ get_dict(exitCode, R, 0), get_dict(stdout, R, Out),
214
+ send(Parent, done(Out)).
215
+ ```
216
+
217
+ Test goal: `read_in_worker("canonical/input.txt", Out), write(Out)`. Expected: `HELLO`.
218
+
219
+ Failure/cancel check: A failed worker may never send; the receive is bounded. Inspect the worker and stop/reap it after a timeout.
146
220
 
147
- doc(solve/3, "X+Y=Total with X<Y over 0..100. solve(+Total, -X, -Y). Example: /goal solve(10, X, Y).").
221
+ Fixture `canonical/input.txt` (create before run):
148
222
 
149
- solve(Total, X, Y) :-
150
- [X, Y] ins 0..100,
151
- X + Y #= Total,
152
- X #< Y,
153
- label([X, Y]).
223
+ ```text
224
+ hello
154
225
  ```
155
226
 
156
- The `:- use_module(...)` directive must be **top-level** (an inline
157
- `use_module` inside a body is not run at read time, so `#=`/`ins` are unknown).
227
+ ## service
228
+
229
+ **A stateful service with correlated replies**
230
+
231
+ A service is for message-driven state, not a single model call. Registered names survive across turns while the process is alive. Stop/reap test services.
158
232
 
159
- ## 8. Vision: describe a workspace image
233
+ Requires: Process-capable session.
160
234
 
161
235
  ```prolog
162
- doc(describe_image/2, "Describe an image file. describe_image(+Path, -Text). Example: /goal describe_image(\"photos/fox.png\", T).").
236
+ service_demo(Text) :-
237
+ serve(counter, counter_loop(0), Pid),
238
+ service_call(counter, bump, First, 5000),
239
+ service_call(counter, bump, Second, 5000),
240
+ service_call(counter, read, Final, 5000),
241
+ service_cast(counter, stop), wait(Pid, _),
242
+ format(string(Text), "~w ~w ~w", [First, Second, Final]).
243
+ counter_loop(Count) :-
244
+ receive(Msg),
245
+ ( Msg = request(From, Ref, read)
246
+ -> send(From, reply(Ref, Count)), counter_loop(Count)
247
+ ; Msg = request(From, Ref, bump)
248
+ -> Next is Count + 1, send(From, reply(Ref, Next)), counter_loop(Next)
249
+ ; Msg = stop -> true
250
+ ; counter_loop(Count)
251
+ ).
252
+ ```
253
+
254
+ Test goal: `service_demo(Text), write(Text)`. Expected: `1 2 2`.
255
+
256
+ Failure/cancel check: Duplicate service names are rejected; callers use bounded service_call timeouts.
257
+
258
+ ## vision
259
+
260
+ **Vision input is not display or generation**
261
+
262
+ Attach a workspace reference, then use a vision task. A fresh prompt does not read the attached image. Display is a separate output operation.
163
263
 
264
+ Requires: Vision-capable model and supported image file; tests provide a tiny PNG and assert media is actually in the model request.
265
+
266
+ ```prolog
164
267
  describe_image(Path, Text) :-
165
- read_media(Path, Image), % media(Path, Mime) — no bytes
166
- attach_media(Image), % reference-only; sent by the next task/N
167
- task("Describe the attached image in one sentence.", Text).
268
+ read_media(Path, Image), attach_media(Image),
269
+ with_tools([], task("Describe only the attached image.", string(Text))).
270
+ show_image_file(Path) :- output(media(Path)).
168
271
  ```
169
272
 
170
- Images are files; `media(Path, Mime)` is the reference. The bytes are read from
171
- the workspace at call time, only PNG/JPEG/GIF/WebP are accepted, and they never
172
- enter clauses or memory. Judgments stay text-only.
273
+ Test goal: `describe_image("canonical/pixel.png", Text), write(Text)`. Expected: `An observed image.`.
173
274
 
174
- ## 9. Forms: structured input from the user
275
+ Failure/cancel check: A text-only model cannot analyse pixels. Image generation additionally needs a configured image backend.
175
276
 
176
- Use a form when the program needs **typed, validated** input rather than free
177
- text. Forms are always available — no directive, nothing to import.
277
+ Binary fixture: provide a valid supported image before running (test suite includes one).
178
278
 
179
- **Let the model design and drive the form** (the usual shape). Give `task/N` the
180
- form tools and describe what you need:
279
+ ## fixed-form
181
280
 
182
- ```prolog
183
- doc(collect_claim/0, "Collect an expense claim and report it. Example: /goal collect_claim.").
281
+ **Fixed foreground form with a cancellation branch**
282
+
283
+ Known fields need no model. The host enforces hard validation; a refused submit is not delivered. Never save on cancel or unavailable.
284
+
285
+ Requires: Foreground form host. Browser disposable execute/process workers do not render forms.
184
286
 
185
- collect_claim(Summary) :-
186
- system("You collect a new expense claim. Ask for the vendor, the amount and a note."),
187
- with_tools([render_form, wait_for_form_change, close_form, set_result, finish],
188
- task("Ask for the three fields, then report exactly what the user entered.",
189
- string(Summary))).
287
+ ```prolog
288
+ claim_form(Values) :-
289
+ form(_{title: "Quick check-in", fields: [_{name: name, type: string, required: true}]}, F),
290
+ await_form(F, Change), close_form(F),
291
+ ( Change.action == submit -> Change.values = Values ; Values = _{} ).
190
292
  ```
191
293
 
192
- The model's loop is three calls: `render_form(spec)` → a form id,
193
- `wait_for_form_change(formId)` → `{action, values}`, `close_form(formId)`.
294
+ Test goal: `claim_form(V), get_dict(name, V, Name), format(string(S), "Hello ~w", [Name]), write(S)`. Expected: `Hello Ada`.
194
295
 
195
- **Or drive it from DML**, with no model:
296
+ Failure/cancel check: Cancel/unavailable returns an empty dict; a caller must not create a record from it.
196
297
 
197
- ```prolog
198
- doc(claim_form/1, "Collect an expense claim. claim_form(-Values) returns the entered values.").
298
+ ## dynamic-form
199
299
 
200
- claim_form(Values) :-
201
- form(_{ title: "New expense claim",
202
- fields: [ _{name: vendor, type: string, required: true},
203
- _{name: amount, type: integer, required: true, minimum: 0},
204
- _{name: notes, widget: textarea} ] }, F),
205
- await_form(F, Change),
206
- close_form(F),
207
- ( Change.action == submit % an atom: submit, cancel
208
- -> Change.values = Values
209
- ; Values = _{}
210
- ).
300
+ **Context-dependent fields designed by a scoped task**
211
301
 
212
- %% Domain rules on top of the spec's own constraints; both block a submit, and a
213
- %% refused submit is never returned — the host shows the errors and waits again.
214
- form_validate(Values, Errors) :-
215
- findall(error(amount, "the amount looks implausible"),
216
- ( get_dict(amount, Values, A), A > 100000 ),
217
- Errors).
302
+ The nested model designs the form, waits for actual values and closes it. Result/finish machinery is internal, not a user tool annotation.
303
+
304
+ Requires: Foreground form host and text model with tools.
305
+
306
+ ```prolog
307
+ collect_details(Context, Summary) :-
308
+ with_tools([render_form, wait_for_form_change, close_form],
309
+ task("Collect {Context} using a form, wait for the user action, close the form, and report only what they entered.", string(Summary))).
218
310
  ```
219
311
 
220
- Note what is *not* here: no `version`, no loop for rejected submits, no error
221
- branch. A field's `type` defaults to `string`, and the host owns validation.
222
- `Change.action` is an **atom** (`submit`, `cancel`) — compare with `==`, never
223
- against `"submit"`. `current_predicate(form/2)` reports `no` even when forms work
224
- (it is a host handler, like `exec/2`). See `reference("forms")` and
225
- `sdk-examples/forms.dml`.
312
+ Test goal: `collect_details("name and favorite color", Summary), write(Summary)`. Expected: `Ada likes blue.`.
226
313
 
227
- ## 10. A conversational agent
314
+ Failure/cancel check: A form rendered without a real submission is not evidence that intake completed.
228
315
 
229
- A conversation talks with the person over many turns and acts between them. Two
230
- primitives: `input/1` (the next message, no prompt) and `chat/1` (one traditional
231
- agent turn — tools, no `finish`, no nudging; it ends when the model stops calling
232
- tools). Run it as the foreground goal, not inside `execute`.
316
+ ## custom-intake
233
317
 
234
- ```prolog
235
- doc(agent_main/0, "Chat assistant that can add. Run with /goal agent_main.").
318
+ **Custom staged conversation (advanced, not the default)**
236
319
 
237
- agent_main :-
238
- system("You are a concise assistant. Use add/3 when asked for arithmetic."),
239
- loop.
320
+ Only use a custom loop for a required protocol. This example validates project intake BEFORE allowing conversation. A normal assistant with a persona/tools uses shell/1 instead.
240
321
 
241
- loop :-
242
- input(A), % the person's next message; fails when there is none
243
- chat(A), % one turn: tools, then it replies and stops
244
- loop.
322
+ Requires: Foreground conversation/input host and text model; file fixture exists. First input is the required project name, followed by chat messages.
245
323
 
246
- tool(add(A, B, Result), "Add two numbers.", [number, number]) :-
247
- Result is A + B.
324
+ ```prolog
325
+ agent_main :-
326
+ input("Project name?", Project), Project \== "",
327
+ system("You are a concise assistant for project {Project}."),
328
+ conversation.
329
+ conversation :- input(Message), chat(Message), conversation.
330
+ read_text(Path, Text) :- read_file(Path, Text).
331
+ tool(read_text(Path, Text), "Read a workspace text file.", [string]) :- read_text(Path, Text).
248
332
  ```
249
333
 
250
- `chat(Message, Reply)` binds the assistant's last message if the harness wants to
251
- post-process it, and a failed turn is reported without ending the conversation.
252
- Memory carries the conversation, so each turn sees what was said. A turn may open
253
- a **form** (§9), which is how structured input fits inside a conversation.
334
+ Test goal: `agent_main`. Expected: `I remember the earlier turn.`.
254
335
 
255
- `next/1` reads the same inbox for **events**: replace `input(A), chat(A)` with a
256
- `next/1` dispatch, and a worker's message wakes the conversation:
336
+ Failure/cancel check: An empty project fails before chat starts; no custom loop should be added for ordinary conversation.
257
337
 
258
- ```prolog
259
- loop(Me) :- next(Item), handle(Me, Item), loop(Me).
338
+ Fixture `canonical/input.txt` (create before run):
260
339
 
261
- handle(_, user(Text)) :- chat(Text). % the person
262
- handle(_, message(mail(M))) :- system("New email."), chat("Summarise: {M}").
340
+ ```text
341
+ hello
263
342
  ```
264
-
265
- The `message(Term)` comes from `send(Me, mail(M))` in a spawned worker. See
266
- [`docs/DML_CONVERSATION.md`](../../../../docs/DML_CONVERSATION.md) §5.