deepclause-sdk 0.4.0 → 0.7.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.
- package/README.md +191 -2
- package/dist/agent.d.ts +2 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +159 -28
- package/dist/agent.js.map +1 -1
- package/dist/cli/agent-inbox.d.ts +25 -0
- package/dist/cli/agent-inbox.d.ts.map +1 -0
- package/dist/cli/agent-inbox.js +46 -0
- package/dist/cli/agent-inbox.js.map +1 -0
- package/dist/cli/config.d.ts +166 -156
- package/dist/cli/config.d.ts.map +1 -1
- package/dist/cli/config.js +3 -0
- package/dist/cli/config.js.map +1 -1
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +7 -2
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/login.d.ts +2 -1
- package/dist/cli/login.d.ts.map +1 -1
- package/dist/cli/login.js +15 -2
- package/dist/cli/login.js.map +1 -1
- package/dist/cli/repl.d.ts +61 -1
- package/dist/cli/repl.d.ts.map +1 -1
- package/dist/cli/repl.js +618 -67
- package/dist/cli/repl.js.map +1 -1
- package/dist/cli/run.d.ts +1 -0
- package/dist/cli/run.d.ts.map +1 -1
- package/dist/cli/run.js +3 -1
- package/dist/cli/run.js.map +1 -1
- package/dist/cli/serve.d.ts +3 -0
- package/dist/cli/serve.d.ts.map +1 -0
- package/dist/cli/serve.js +131 -0
- package/dist/cli/serve.js.map +1 -0
- package/dist/cli/set-backend.d.ts +2 -2
- package/dist/cli/set-backend.d.ts.map +1 -1
- package/dist/cli/set-backend.js +54 -8
- package/dist/cli/set-backend.js.map +1 -1
- package/dist/cli/work.d.ts +16 -0
- package/dist/cli/work.d.ts.map +1 -0
- package/dist/cli/work.js +45 -0
- package/dist/cli/work.js.map +1 -0
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/judge/llm.d.ts.map +1 -1
- package/dist/judge/llm.js +9 -4
- package/dist/judge/llm.js.map +1 -1
- package/dist/judge/types.d.ts +10 -0
- package/dist/judge/types.d.ts.map +1 -1
- package/dist/judge/types.js.map +1 -1
- package/dist/prolog-src/deepclause_mi.pl +2791 -302
- package/dist/prolog-src/deepclause_strings.pl +36 -3
- package/dist/repl/agent-inbox.d.ts +73 -0
- package/dist/repl/agent-inbox.d.ts.map +1 -0
- package/dist/repl/agent-inbox.js +168 -0
- package/dist/repl/agent-inbox.js.map +1 -0
- package/dist/repl/agent.d.ts +19 -1
- package/dist/repl/agent.d.ts.map +1 -1
- package/dist/repl/agent.js +44 -13
- package/dist/repl/agent.js.map +1 -1
- package/dist/repl/auth.d.ts +6 -3
- package/dist/repl/auth.d.ts.map +1 -1
- package/dist/repl/auth.js +55 -5
- package/dist/repl/auth.js.map +1 -1
- package/dist/repl/backend.d.ts +30 -0
- package/dist/repl/backend.d.ts.map +1 -1
- package/dist/repl/backend.js +309 -14
- package/dist/repl/backend.js.map +1 -1
- package/dist/repl/compaction.d.ts +2 -0
- package/dist/repl/compaction.d.ts.map +1 -1
- package/dist/repl/compaction.js +9 -0
- package/dist/repl/compaction.js.map +1 -1
- package/dist/repl/event-history.d.ts +4 -0
- package/dist/repl/event-history.d.ts.map +1 -0
- package/dist/repl/event-history.js +36 -0
- package/dist/repl/event-history.js.map +1 -0
- package/dist/repl/harness-tools.d.ts +32 -18
- package/dist/repl/harness-tools.d.ts.map +1 -1
- package/dist/repl/harness-tools.js +127 -34
- package/dist/repl/harness-tools.js.map +1 -1
- package/dist/repl/history.d.ts +66 -5
- package/dist/repl/history.d.ts.map +1 -1
- package/dist/repl/history.js +174 -8
- package/dist/repl/history.js.map +1 -1
- package/dist/repl/index.d.ts +38 -5
- package/dist/repl/index.d.ts.map +1 -1
- package/dist/repl/index.js +32 -1
- package/dist/repl/index.js.map +1 -1
- package/dist/repl/process-backend.d.ts +1 -0
- package/dist/repl/process-backend.d.ts.map +1 -1
- package/dist/repl/process-executor.d.ts +4 -0
- package/dist/repl/process-executor.d.ts.map +1 -1
- package/dist/repl/process-executor.js +36 -1
- package/dist/repl/process-executor.js.map +1 -1
- package/dist/repl/process-worker.d.ts.map +1 -1
- package/dist/repl/process-worker.js +33 -8
- package/dist/repl/process-worker.js.map +1 -1
- package/dist/repl/processes.d.ts +14 -1
- package/dist/repl/processes.d.ts.map +1 -1
- package/dist/repl/processes.js +8 -2
- package/dist/repl/processes.js.map +1 -1
- package/dist/repl/reference.d.ts +31 -0
- package/dist/repl/reference.d.ts.map +1 -0
- package/dist/repl/reference.js +144 -0
- package/dist/repl/reference.js.map +1 -0
- package/dist/repl/render.d.ts +3 -1
- package/dist/repl/render.d.ts.map +1 -1
- package/dist/repl/render.js +20 -0
- package/dist/repl/render.js.map +1 -1
- package/dist/repl/session.d.ts +199 -27
- package/dist/repl/session.d.ts.map +1 -1
- package/dist/repl/session.js +1471 -219
- package/dist/repl/session.js.map +1 -1
- package/dist/repl/toolset.d.ts +8 -0
- package/dist/repl/toolset.d.ts.map +1 -1
- package/dist/repl/toolset.js +53 -4
- package/dist/repl/toolset.js.map +1 -1
- package/dist/repl/tui/autocomplete.d.ts.map +1 -1
- package/dist/repl/tui/autocomplete.js +5 -1
- package/dist/repl/tui/autocomplete.js.map +1 -1
- package/dist/repl/tui/renderer.d.ts +2 -1
- package/dist/repl/tui/renderer.d.ts.map +1 -1
- package/dist/repl/tui/renderer.js +23 -4
- package/dist/repl/tui/renderer.js.map +1 -1
- package/dist/repl/tui/status-bar.d.ts +5 -2
- package/dist/repl/tui/status-bar.d.ts.map +1 -1
- package/dist/repl/tui/status-bar.js +8 -3
- package/dist/repl/tui/status-bar.js.map +1 -1
- package/dist/repl/tui/tool-pane.d.ts +1 -1
- package/dist/repl/tui/tool-pane.d.ts.map +1 -1
- package/dist/repl/tui/tool-pane.js +13 -7
- package/dist/repl/tui/tool-pane.js.map +1 -1
- package/dist/repl/tui/work-view.d.ts +10 -0
- package/dist/repl/tui/work-view.d.ts.map +1 -0
- package/dist/repl/tui/work-view.js +28 -0
- package/dist/repl/tui/work-view.js.map +1 -0
- package/dist/repl/work-hooks.d.ts +29 -0
- package/dist/repl/work-hooks.d.ts.map +1 -0
- package/dist/repl/work-hooks.js +117 -0
- package/dist/repl/work-hooks.js.map +1 -0
- package/dist/repl/worker-host.d.ts +2 -0
- package/dist/repl/worker-host.d.ts.map +1 -1
- package/dist/repl/worker-host.js +48 -4
- package/dist/repl/worker-host.js.map +1 -1
- package/dist/repl/worker-protocol.d.ts +23 -2
- package/dist/repl/worker-protocol.d.ts.map +1 -1
- package/dist/repl/worker-protocol.js +1 -1
- package/dist/runner.d.ts +2 -2
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +69 -22
- package/dist/runner.js.map +1 -1
- package/dist/runtime-identity.json +10 -0
- package/dist/serve/agent-broadcast.d.ts +29 -0
- package/dist/serve/agent-broadcast.d.ts.map +1 -0
- package/dist/serve/agent-broadcast.js +28 -0
- package/dist/serve/agent-broadcast.js.map +1 -0
- package/dist/serve/agent-preferences.d.ts +16 -0
- package/dist/serve/agent-preferences.d.ts.map +1 -0
- package/dist/serve/agent-preferences.js +42 -0
- package/dist/serve/agent-preferences.js.map +1 -0
- package/dist/serve/api-docs.d.ts +61 -0
- package/dist/serve/api-docs.d.ts.map +1 -0
- package/dist/serve/api-docs.js +122 -0
- package/dist/serve/api-docs.js.map +1 -0
- package/dist/serve/backend.d.ts +12 -0
- package/dist/serve/backend.d.ts.map +1 -0
- package/dist/serve/backend.js +52 -0
- package/dist/serve/backend.js.map +1 -0
- package/dist/serve/capabilities.d.ts +6 -0
- package/dist/serve/capabilities.d.ts.map +1 -0
- package/dist/serve/capabilities.js +21 -0
- package/dist/serve/capabilities.js.map +1 -0
- package/dist/serve/example-agent.d.ts +5 -0
- package/dist/serve/example-agent.d.ts.map +1 -0
- package/dist/serve/example-agent.js +5 -0
- package/dist/serve/example-agent.js.map +1 -0
- package/dist/serve/files.d.ts +55 -0
- package/dist/serve/files.d.ts.map +1 -0
- package/dist/serve/files.js +249 -0
- package/dist/serve/files.js.map +1 -0
- package/dist/serve/harness.d.ts +3 -0
- package/dist/serve/harness.d.ts.map +1 -0
- package/dist/serve/harness.js +53 -0
- package/dist/serve/harness.js.map +1 -0
- package/dist/serve/previews.d.ts +40 -0
- package/dist/serve/previews.d.ts.map +1 -0
- package/dist/serve/previews.js +240 -0
- package/dist/serve/previews.js.map +1 -0
- package/dist/serve/protocol.d.ts +228 -0
- package/dist/serve/protocol.d.ts.map +1 -0
- package/dist/serve/protocol.js +2 -0
- package/dist/serve/protocol.js.map +1 -0
- package/dist/serve/schedules.d.ts +85 -0
- package/dist/serve/schedules.d.ts.map +1 -0
- package/dist/serve/schedules.js +249 -0
- package/dist/serve/schedules.js.map +1 -0
- package/dist/serve/server.d.ts +50 -0
- package/dist/serve/server.d.ts.map +1 -0
- package/dist/serve/server.js +1696 -0
- package/dist/serve/server.js.map +1 -0
- package/dist/serve/session-worker.d.ts +2 -0
- package/dist/serve/session-worker.d.ts.map +1 -0
- package/dist/serve/session-worker.js +291 -0
- package/dist/serve/session-worker.js.map +1 -0
- package/dist/serve/sessions.d.ts +178 -0
- package/dist/serve/sessions.d.ts.map +1 -0
- package/dist/serve/sessions.js +905 -0
- package/dist/serve/sessions.js.map +1 -0
- package/dist/serve/storage.d.ts +41 -0
- package/dist/serve/storage.d.ts.map +1 -0
- package/dist/serve/storage.js +211 -0
- package/dist/serve/storage.js.map +1 -0
- package/dist/serve/ui/api-docs.html +17 -0
- package/dist/serve/ui/api-docs.js +36 -0
- package/dist/serve/ui/app.js +958 -0
- package/dist/serve/ui/authoring.js +92 -0
- package/dist/serve/ui/deepclause-logo.png +0 -0
- package/dist/serve/ui/deepclause-mark.svg +1 -0
- package/dist/serve/ui/diff.js +16 -0
- package/dist/serve/ui/dompurify-LICENSE.txt +202 -0
- package/dist/serve/ui/features.css +15 -0
- package/dist/serve/ui/files.js +13 -0
- package/dist/serve/ui/ibm-plex-mono-latin.woff2 +0 -0
- package/dist/serve/ui/ibmplexmono-OFL.txt +93 -0
- package/dist/serve/ui/icons.js +30 -0
- package/dist/serve/ui/index.html +68 -0
- package/dist/serve/ui/manrope-OFL.txt +93 -0
- package/dist/serve/ui/manrope-latin.woff2 +0 -0
- package/dist/serve/ui/markdown.js +38 -0
- package/dist/serve/ui/marked-LICENSE.txt +44 -0
- package/dist/serve/ui/marked.js +88 -0
- package/dist/serve/ui/purify.js +2454 -0
- package/dist/serve/ui/style.css +249 -0
- package/dist/serve/ui/web-work.css +247 -0
- package/dist/serve/ui/web.css +1848 -0
- package/dist/serve/workflow-bundles.d.ts +8 -0
- package/dist/serve/workflow-bundles.d.ts.map +1 -0
- package/dist/serve/workflow-bundles.js +78 -0
- package/dist/serve/workflow-bundles.js.map +1 -0
- package/dist/serve/workflow-calls.d.ts +6 -0
- package/dist/serve/workflow-calls.d.ts.map +1 -0
- package/dist/serve/workflow-calls.js +13 -0
- package/dist/serve/workflow-calls.js.map +1 -0
- package/dist/serve/workflows.d.ts +93 -0
- package/dist/serve/workflows.d.ts.map +1 -0
- package/dist/serve/workflows.js +219 -0
- package/dist/serve/workflows.js.map +1 -0
- package/dist/system/assets/docs/AGENT_PROMPT.md +265 -3008
- package/dist/system/assets/docs/DML_AUTHORING.md +151 -291
- package/dist/system/assets/docs/DML_BASICS.md +93 -0
- package/dist/system/assets/docs/DML_DEBUGGING.md +73 -0
- package/dist/system/assets/docs/DML_FORMS.md +94 -728
- package/dist/system/assets/docs/DML_MEDIA.md +63 -0
- package/dist/system/assets/docs/DML_PROCESSES.md +124 -0
- package/dist/system/assets/docs/DML_PYTHON.md +74 -0
- package/dist/system/assets/docs/DML_REFERENCE.md +353 -2012
- package/dist/system/assets/docs/DML_SHELL.md +249 -0
- package/dist/system/assets/docs/DML_TASKS.md +119 -0
- package/dist/system/assets/docs/HARNESS_EXAMPLES.md +255 -179
- package/dist/system/assets/docs/HARNESS_PATTERNS.md +65 -538
- package/dist/system/assets/docs/LOCAL_AGENT_PROMPT.md +186 -0
- package/dist/system/assets/docs/PROLOG_SNIPPETS.md +61 -154
- package/dist/system/assets/index.d.ts +1 -1
- package/dist/system/assets/index.d.ts.map +1 -1
- package/dist/system/assets/index.js +7 -0
- package/dist/system/assets/index.js.map +1 -1
- package/dist/system/environment.d.ts +5 -1
- package/dist/system/environment.d.ts.map +1 -1
- package/dist/system/environment.js +20 -10
- package/dist/system/environment.js.map +1 -1
- package/dist/system/runtime/agentvm-manager.d.ts +12 -1
- package/dist/system/runtime/agentvm-manager.d.ts.map +1 -1
- package/dist/system/runtime/agentvm-manager.js +136 -20
- package/dist/system/runtime/agentvm-manager.js.map +1 -1
- package/dist/system/runtime/shell-manager.d.ts +4 -1
- package/dist/system/runtime/shell-manager.d.ts.map +1 -1
- package/dist/system/runtime/shell-manager.js +1 -1
- package/dist/system/runtime/shell-manager.js.map +1 -1
- package/dist/system/timezone.d.ts +20 -0
- package/dist/system/timezone.d.ts.map +1 -0
- package/dist/system/timezone.js +37 -0
- package/dist/system/timezone.js.map +1 -0
- package/dist/types.d.ts +85 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/work/index.d.ts +92 -0
- package/dist/work/index.d.ts.map +1 -0
- package/dist/work/index.js +297 -0
- package/dist/work/index.js.map +1 -0
- package/docs/DEEP_RESEARCH_EXAMPLE.md +127 -0
- package/docs/SERVE.md +664 -0
- package/docs/WORK_AGENT_HOOKS_PLAN.md +416 -0
- package/docs/WORK_AGENT_INBOX.md +140 -0
- package/docs/WORK_AGENT_INBOX_PLAN.md +91 -0
- package/docs/WORK_MODE.md +269 -0
- package/package.json +35 -4
- package/sdk-examples/deep-research.dml +144 -0
- package/sdk-examples/deep-research.py +502 -0
- package/sdk-examples/hooked-agent.dml +49 -0
- package/sdk-examples/subagent-inbox.dml +66 -0
- package/src/prolog-src/deepclause_mi.pl +2791 -302
- package/src/prolog-src/deepclause_strings.pl +36 -3
|
@@ -58,11 +58,29 @@
|
|
|
58
58
|
repl_retract/3,
|
|
59
59
|
repl_define/4,
|
|
60
60
|
repl_delete_predicate/4,
|
|
61
|
+
repl_delete_doc/4,
|
|
62
|
+
repl_delete_schema/3,
|
|
63
|
+
repl_delete_script/3,
|
|
64
|
+
repl_delete_annotation_entry/4,
|
|
65
|
+
repl_trace_json/2,
|
|
66
|
+
repl_trace_clear/1,
|
|
67
|
+
repl_add_harness_import/5,
|
|
68
|
+
repl_set_harness_exports/3,
|
|
69
|
+
repl_trace_tool_start/4,
|
|
70
|
+
repl_trace_tool_end/3,
|
|
71
|
+
repl_load_harness_module/6,
|
|
72
|
+
repl_hs_register_exports/2,
|
|
73
|
+
repl_hs_register_module_exports/2,
|
|
74
|
+
repl_hs_register_imports/5,
|
|
75
|
+
parse_harness_module_string/3,
|
|
76
|
+
process_harness_term/3,
|
|
77
|
+
repl_harness_module_info_json/2,
|
|
61
78
|
repl_edit_touched/2,
|
|
62
79
|
repl_edit_touched_text/2,
|
|
63
80
|
repl_edit_touched_clear/1,
|
|
64
81
|
repl_validate_tools/2,
|
|
65
82
|
repl_validate_docs/2,
|
|
83
|
+
repl_validate_complexity/2,
|
|
66
84
|
schema_fill_fields/3,
|
|
67
85
|
schema_fields_of/3,
|
|
68
86
|
repl_unhandled_directives/2,
|
|
@@ -71,6 +89,8 @@
|
|
|
71
89
|
repl_set_dml_reference/2,
|
|
72
90
|
repl_set_authoring_examples/2,
|
|
73
91
|
repl_manifest/2,
|
|
92
|
+
repl_display_harness/2,
|
|
93
|
+
repl_shell_json/2,
|
|
74
94
|
repl_history_load/2,
|
|
75
95
|
repl_history_record/2,
|
|
76
96
|
repl_history_summary/2,
|
|
@@ -128,9 +148,18 @@
|
|
|
128
148
|
%% `process_engine_result/4`, which is intentional.
|
|
129
149
|
:- discontiguous process_engine_result/4.
|
|
130
150
|
:- discontiguous process_directive/2.
|
|
151
|
+
%% `repl_register_tool/6` sits between the tool clauses of process_clause/2.
|
|
152
|
+
:- discontiguous process_clause/2.
|
|
153
|
+
%% Tool/workflow conversion helpers sit between the workflow_from/2 clauses.
|
|
154
|
+
:- discontiguous workflow_from/2.
|
|
155
|
+
%% Harness-local dispatch sits near mi_call/3; the fallbacks live much later.
|
|
156
|
+
:- discontiguous mi_call_dispatch/3.
|
|
131
157
|
|
|
132
158
|
%% Dynamic predicates for session state (engine management only, not memory!)
|
|
133
159
|
:- dynamic session_engine/2. % session_engine(SessionId, Engine) - legacy default engine handle
|
|
160
|
+
:- dynamic mi_cut_class/3. % mi_cut_class(SessionId, Name/Arity, native|interpreted)
|
|
161
|
+
:- dynamic mi_purity/3. % mi_purity(SessionId, Name/Arity, pure|impure|in_progress)
|
|
162
|
+
:- dynamic mi_cut_done/1. % mi_cut_done(CallId) - non-backtrackable clause commit
|
|
134
163
|
:- dynamic session_frame/4. % session_frame(FrameId, SessionId, Kind, Depth)
|
|
135
164
|
:- dynamic session_frame_engine/2. % session_frame_engine(FrameId, Engine)
|
|
136
165
|
:- dynamic session_default_frame/2. % session_default_frame(SessionId, FrameId)
|
|
@@ -153,13 +182,23 @@
|
|
|
153
182
|
:- dynamic session_params/2. % session_params(SessionId, ParamsDict)
|
|
154
183
|
:- dynamic session_user_tools/2. % session_user_tools(SessionId, ToolName)
|
|
155
184
|
:- dynamic session_user_tool_schema/3. % session_user_tool_schema(SessionId, ToolName, Schema)
|
|
185
|
+
:- dynamic session_source_view/3. % session_source_view(SessionId, Name/Arity, SourceText)
|
|
156
186
|
:- dynamic session_pending_input/2. % session_pending_input(SessionId, Input)
|
|
157
187
|
:- dynamic session_agent_result/2. % session_agent_result(SessionId, Result)
|
|
158
188
|
:- dynamic session_exec_result/2. % session_exec_result(SessionId, Result)
|
|
159
189
|
:- dynamic session_sample_token_result/2. % session_sample_token_result(SessionId, Result)
|
|
160
190
|
:- dynamic session_llm_result/2. % session_llm_result(SessionId, Result)
|
|
161
191
|
:- dynamic session_judgment_result/2. % session_judgment_result(SessionId, Result)
|
|
162
|
-
:- dynamic
|
|
192
|
+
:- dynamic session_trace_entry/3. % session_trace_entry(SessionId, Index, Entry) - bounded trace
|
|
193
|
+
:- dynamic session_trace_scope/2. % session_trace_scope(SessionId, [ToolId|...]) - nested tool scope stack
|
|
194
|
+
:- dynamic session_harness_module/5. % SessionId, Name, Version, Hash, ModuleId
|
|
195
|
+
:- dynamic harness_module_owner/4. % ModuleId, Name, Version, Hash
|
|
196
|
+
:- dynamic harness_module_export/3. % ModuleId, Name, Arity
|
|
197
|
+
:- dynamic harness_module_import/4. % ModuleId, Name/Arity, TargetModule, TargetName/Arity
|
|
198
|
+
:- dynamic session_harness_export/3. % SessionId, Name, Arity
|
|
199
|
+
:- dynamic session_harness_import/4. % SessionId, Name/Arity, TargetModule, TargetName/Arity
|
|
200
|
+
:- dynamic session_trace_count/2. % session_trace_count(SessionId, Count)
|
|
201
|
+
:- dynamic session_trace_log/2. % legacy list; retained for compatibility
|
|
163
202
|
:- dynamic session_trace_enabled/2. % session_trace_enabled(SessionId, true/false)
|
|
164
203
|
:- dynamic session_pending_signal/2. % session_pending_signal(SessionId, Signal) - fallback for engine_fetch
|
|
165
204
|
:- dynamic session_proc_result/3. % session_proc_result(SessionId, Key, Value) - process primitive results
|
|
@@ -202,12 +241,35 @@ assert_params(SessionId, Params) :-
|
|
|
202
241
|
; true
|
|
203
242
|
).
|
|
204
243
|
|
|
244
|
+
%% Validate that every exported predicate actually exists. Export directives can
|
|
245
|
+
%% appear before the definitions, so this runs after the whole source is loaded.
|
|
246
|
+
repl_validate_session_exports(SessionId) :-
|
|
247
|
+
findall(Name/Arity, session_harness_export(SessionId, Name, Arity), Exports),
|
|
248
|
+
repl_validate_export_list(Exports, SessionId, session).
|
|
249
|
+
|
|
250
|
+
repl_validate_module_exports(ModuleId) :-
|
|
251
|
+
findall(Name/Arity, harness_module_export(ModuleId, Name, Arity), Exports),
|
|
252
|
+
repl_validate_export_list(Exports, ModuleId, module).
|
|
253
|
+
|
|
254
|
+
repl_validate_export_list([], _, _).
|
|
255
|
+
repl_validate_export_list([Name/Arity|Rest], Owner, Kind) :-
|
|
256
|
+
( module_local_predicate(Owner, Name, Arity)
|
|
257
|
+
-> true
|
|
258
|
+
; throw(error(existence_error(export, Name/Arity),
|
|
259
|
+
context(export/1, 'exported predicate is not defined in this harness')))
|
|
260
|
+
),
|
|
261
|
+
repl_validate_export_list(Rest, Owner, Kind).
|
|
262
|
+
|
|
205
263
|
%% parse_dml_string(+Code, +SessionId)
|
|
206
264
|
%% Parse DML code from a string
|
|
207
265
|
parse_dml_string(Code, SessionId) :-
|
|
266
|
+
retractall(session_harness_export(SessionId, _, _)),
|
|
267
|
+
retractall(session_harness_import(SessionId, _, _, _)),
|
|
208
268
|
open_string(Code, Stream),
|
|
209
269
|
parse_clauses(Stream, SessionId),
|
|
210
|
-
close(Stream)
|
|
270
|
+
close(Stream),
|
|
271
|
+
repl_source_views(SessionId, Code, Views),
|
|
272
|
+
repl_store_source_views(SessionId, Views, append).
|
|
211
273
|
|
|
212
274
|
%% parse_clauses(+Stream, +SessionId)
|
|
213
275
|
%% Read and process all clauses from a stream
|
|
@@ -215,7 +277,8 @@ parse_dml_string(Code, SessionId) :-
|
|
|
215
277
|
parse_clauses(Stream, SessionId) :-
|
|
216
278
|
read_term(Stream, Term, [module(SessionId), variable_names(Bindings)]),
|
|
217
279
|
( Term == end_of_file
|
|
218
|
-
-> install_builtin_tools(SessionId)
|
|
280
|
+
-> install_builtin_tools(SessionId),
|
|
281
|
+
repl_validate_session_exports(SessionId)
|
|
219
282
|
; preprocess_loaded_term(Term, Bindings, PreparedTerms),
|
|
220
283
|
forall(member(PreparedTerm, PreparedTerms), process_clause(PreparedTerm, SessionId)),
|
|
221
284
|
parse_clauses(Stream, SessionId)
|
|
@@ -229,7 +292,29 @@ parse_clauses(Stream, SessionId) :-
|
|
|
229
292
|
preprocess_loaded_term(Term, Bindings, PreparedTerms) :-
|
|
230
293
|
transform_task_calls(Term, Bindings, TransformedTerm),
|
|
231
294
|
expand_interpolations(TransformedTerm, Bindings, InterpolatedTerm),
|
|
232
|
-
expand_loaded_term(InterpolatedTerm,
|
|
295
|
+
expand_loaded_term(InterpolatedTerm, ExpandedTerms),
|
|
296
|
+
annotate_tool_names_list(ExpandedTerms, Bindings, PreparedTerms).
|
|
297
|
+
|
|
298
|
+
%% annotate_tool_names(+Term, +Bindings, -Annotated)
|
|
299
|
+
%% A `tool(...)` clause is wrapped with the source names of its head variables.
|
|
300
|
+
%% Asserting the clause loses them (the listing prints A, B, C), and the schema
|
|
301
|
+
%% is what the nested agent reads: `arg1` says nothing, `Path` does.
|
|
302
|
+
annotate_tool_names(Term, Bindings, '$dc_tool'(Term, Names)) :-
|
|
303
|
+
tool_clause_head(Term, Head),
|
|
304
|
+
!,
|
|
305
|
+
term_variables(Head, HeadVars),
|
|
306
|
+
findall(Name, (member(Var, HeadVars), member(Name=Var, Bindings)), Names0),
|
|
307
|
+
list_to_set(Names0, Names).
|
|
308
|
+
annotate_tool_names(Term, _Bindings, Term).
|
|
309
|
+
|
|
310
|
+
annotate_tool_names_list([], _, []).
|
|
311
|
+
annotate_tool_names_list([Term|Terms], Bindings, [Annotated|Annotateds]) :-
|
|
312
|
+
annotate_tool_names(Term, Bindings, Annotated),
|
|
313
|
+
annotate_tool_names_list(Terms, Bindings, Annotateds).
|
|
314
|
+
|
|
315
|
+
tool_clause_head((tool(Head, _) :- _), Head).
|
|
316
|
+
tool_clause_head((tool(Head, _, _) :- _), Head).
|
|
317
|
+
tool_clause_head((tool(Head) :- _), Head).
|
|
233
318
|
|
|
234
319
|
%% expand_loaded_term(+Term, -ExpandedTerms)
|
|
235
320
|
%% Runs SWI term expansion and normalizes the result to a list of terms.
|
|
@@ -397,12 +482,44 @@ wrapper_type(dict, object).
|
|
|
397
482
|
%% clean_clause_body(+Body, -CleanBody)
|
|
398
483
|
clean_clause_body(Body, Clean) :-
|
|
399
484
|
conj_list(Body, Goals),
|
|
400
|
-
|
|
485
|
+
% `{Var}` interpolation is rewritten to a render goal plus a `~s` format; put
|
|
486
|
+
% the original variables back so the source reads `task("…{X}…")` again.
|
|
487
|
+
fold_interp_renders(Goals, FoldedGoals),
|
|
488
|
+
clean_goal_list(FoldedGoals, CleanGoals),
|
|
401
489
|
( CleanGoals = [Single]
|
|
402
490
|
-> Clean = Single
|
|
403
491
|
; list_to_conjunction(CleanGoals, Clean)
|
|
404
492
|
).
|
|
405
493
|
|
|
494
|
+
%% fold_interp_renders(+Goals, -Folded)
|
|
495
|
+
%% Drop `dc_interp_value(Original, Rendered)` goals and put `Original` back into
|
|
496
|
+
%% the argument list of the `format/3` they feed.
|
|
497
|
+
fold_interp_renders(Goals, Folded) :-
|
|
498
|
+
fold_interp_renders(Goals, [], Folded).
|
|
499
|
+
|
|
500
|
+
fold_interp_renders([], _, []).
|
|
501
|
+
fold_interp_renders([Goal|Rest], Map, Out) :-
|
|
502
|
+
( interp_render_goal(Goal, Original, Rendered)
|
|
503
|
+
-> fold_interp_renders(Rest, [Rendered-Original|Map], Out)
|
|
504
|
+
; ( Goal = format(string(TextVar), Template, Args),
|
|
505
|
+
is_list(Args)
|
|
506
|
+
-> swap_interp_vars(Args, Map, Args2),
|
|
507
|
+
Out = [format(string(TextVar), Template, Args2)|Out2]
|
|
508
|
+
; Out = [Goal|Out2]
|
|
509
|
+
),
|
|
510
|
+
fold_interp_renders(Rest, Map, Out2)
|
|
511
|
+
).
|
|
512
|
+
|
|
513
|
+
interp_render_goal(deepclause_strings:dc_interp_value(Original, Rendered), Original, Rendered).
|
|
514
|
+
|
|
515
|
+
swap_interp_vars([], _, []).
|
|
516
|
+
swap_interp_vars([Arg|Args], Map, [Swapped|SwappedArgs]) :-
|
|
517
|
+
( member(Arg-Original, Map)
|
|
518
|
+
-> Swapped = Original
|
|
519
|
+
; Swapped = Arg
|
|
520
|
+
),
|
|
521
|
+
swap_interp_vars(Args, Map, SwappedArgs).
|
|
522
|
+
|
|
406
523
|
conj_list((A, B), List) :-
|
|
407
524
|
!,
|
|
408
525
|
conj_list(A, LA),
|
|
@@ -525,14 +642,25 @@ typed_arg_type(string, Var, _, Var) :- !.
|
|
|
525
642
|
typed_arg_type(integer, Var, _, integer(Var)) :- !.
|
|
526
643
|
typed_arg_type(number, Var, _, number(Var)) :- !.
|
|
527
644
|
typed_arg_type(boolean, Var, _, boolean(Var)) :- !.
|
|
528
|
-
typed_arg_type(object, Var,
|
|
645
|
+
typed_arg_type(object, Var, Info, Wrapped) :-
|
|
646
|
+
!,
|
|
647
|
+
% `schema(name, X)` is an object with a declared shape. Dropping the name
|
|
648
|
+
% here made listing and export show `object(X)`, so an edit made from the
|
|
649
|
+
% listing silently lost the schema.
|
|
650
|
+
( get_dict(schema, Info, SchemaName)
|
|
651
|
+
-> Wrapped = schema(SchemaName, Var)
|
|
652
|
+
; Wrapped = object(Var)
|
|
653
|
+
).
|
|
529
654
|
typed_arg_type(array, Var, Info, list(Wrapped)) :-
|
|
530
655
|
!,
|
|
531
656
|
( get_dict(itemType, Info, ItemType)
|
|
532
657
|
-> true
|
|
533
658
|
; ItemType = string
|
|
534
659
|
),
|
|
535
|
-
|
|
660
|
+
( get_dict(schema, Info, SchemaName)
|
|
661
|
+
-> Wrapped = schema(SchemaName, Var)
|
|
662
|
+
; Wrapped =.. [ItemType, Var]
|
|
663
|
+
).
|
|
536
664
|
typed_arg_type(_, Var, _, Var).
|
|
537
665
|
|
|
538
666
|
%% interpolate_template(+Template, +Args, -Description)
|
|
@@ -547,7 +675,8 @@ interpolate_template(Template, Args, Description) :-
|
|
|
547
675
|
interpolate_codes([], [], []) :- !.
|
|
548
676
|
%% A {Var} placeholder consumes one argument; require Args to be exhausted
|
|
549
677
|
%% exactly (the []/[] base case above fails if either side has leftovers).
|
|
550
|
-
interpolate_codes([0'~,
|
|
678
|
+
interpolate_codes([0'~, Directive | Rest], [Arg | Args], Out) :-
|
|
679
|
+
( Directive =:= 0'w ; Directive =:= 0's ),
|
|
551
680
|
!,
|
|
552
681
|
named_var(Arg),
|
|
553
682
|
term_string(Arg, Name, [numbervars(true)]),
|
|
@@ -573,6 +702,57 @@ var_to_name(Bindings, Var, Name) :-
|
|
|
573
702
|
; Name = 'Var' % Fallback if not found
|
|
574
703
|
).
|
|
575
704
|
|
|
705
|
+
repl_script_predicate_conflict(SessionId, Name) :-
|
|
706
|
+
current_predicate(SessionId:script_file/2),
|
|
707
|
+
SessionId:script_file(Name, _),
|
|
708
|
+
current_predicate(SessionId:Name/Arity),
|
|
709
|
+
repl_user_predicate(SessionId, Name, Arity).
|
|
710
|
+
|
|
711
|
+
repl_reject_existing_predicate_for_script(SessionId, Name) :-
|
|
712
|
+
( current_predicate(SessionId:Name/Arity),
|
|
713
|
+
repl_user_predicate(SessionId, Name, Arity)
|
|
714
|
+
-> format(atom(Message),
|
|
715
|
+
'~w is already a predicate. Rename the predicate or use a distinct script link name (for example ~w_py); script link names and predicate names must not overlap.',
|
|
716
|
+
[Name, Name]),
|
|
717
|
+
throw(error(permission_error(modify, script_predicate_collision, Name),
|
|
718
|
+
context(script_file/2, Message)))
|
|
719
|
+
; true
|
|
720
|
+
).
|
|
721
|
+
|
|
722
|
+
repl_reject_script_predicate_collision(SessionId, Name, Where) :-
|
|
723
|
+
( repl_script_predicate_conflict(SessionId, Name)
|
|
724
|
+
-> format(atom(Message),
|
|
725
|
+
'~w is already a script_file/2 link name. Rename the link (for example ~w_py) or rename the predicate; script link names and predicate names must not overlap.',
|
|
726
|
+
[Name, Name]),
|
|
727
|
+
throw(error(permission_error(modify, script_predicate_collision, Name),
|
|
728
|
+
context(Where, Message)))
|
|
729
|
+
; true
|
|
730
|
+
).
|
|
731
|
+
|
|
732
|
+
repl_check_script_name_for_head(SessionId, Head) :-
|
|
733
|
+
( catch(repl_head_of(Head, H), _, fail),
|
|
734
|
+
callable(H),
|
|
735
|
+
functor(H, Name, Arity)
|
|
736
|
+
-> ( session_harness_import(SessionId, Name/Arity, TargetModule, _)
|
|
737
|
+
-> format(atom(Message),
|
|
738
|
+
'~w/~w is imported read-only from ~w. Define a different local wrapper instead of redefining it.',
|
|
739
|
+
[Name, Arity, TargetModule]),
|
|
740
|
+
throw(error(permission_error(modify, imported_predicate, Name/Arity),
|
|
741
|
+
context(process_clause/2, Message)))
|
|
742
|
+
; true
|
|
743
|
+
),
|
|
744
|
+
( current_predicate(SessionId:script_file/2),
|
|
745
|
+
SessionId:script_file(Name, _)
|
|
746
|
+
-> format(atom(Message),
|
|
747
|
+
'~w is already a script_file/2 link name. Rename the link (for example ~w_py) or rename the predicate; script link names and predicate names must not overlap.',
|
|
748
|
+
[Name, Name]),
|
|
749
|
+
throw(error(permission_error(modify, script_predicate_collision, Name),
|
|
750
|
+
context(process_clause/2, Message)))
|
|
751
|
+
; true
|
|
752
|
+
)
|
|
753
|
+
; true
|
|
754
|
+
).
|
|
755
|
+
|
|
576
756
|
%% process_clause(+Term, +SessionId)
|
|
577
757
|
%% Process a single clause and add to session.
|
|
578
758
|
%% Reject reserved-name definitions (third place of the reserved guard, so
|
|
@@ -592,43 +772,88 @@ process_clause((:- Directive), SessionId) :-
|
|
|
592
772
|
%% The leading args are inputs (typed from the list); the last arg is the output.
|
|
593
773
|
process_clause((tool(ToolHead, Description, Types) :- Body), SessionId) :-
|
|
594
774
|
!,
|
|
595
|
-
(
|
|
596
|
-
extract_tool_schema(ToolHead, Description, Types, ToolName, Schema),
|
|
597
|
-
assertz(session_user_tools(SessionId, ToolName)),
|
|
598
|
-
assertz(session_user_tool_schema(SessionId, ToolName, Schema)),
|
|
599
|
-
format_tool_source(ToolHead, Description, Types, Body, SourceCode),
|
|
600
|
-
assertz(SessionId:tool_source(ToolName, SourceCode)),
|
|
601
|
-
assertz(SessionId:(tool(ToolHead) :- ExpandedBody)).
|
|
775
|
+
repl_register_tool(SessionId, ToolHead, Description, Types, Body, []).
|
|
602
776
|
|
|
603
777
|
%% Handle tool/2 with description: tool(Head, Description) :- Body
|
|
604
778
|
process_clause((tool(ToolHead, Description) :- Body), SessionId) :-
|
|
605
779
|
!,
|
|
606
|
-
(
|
|
607
|
-
extract_tool_schema(ToolHead, Description, ToolName, Schema),
|
|
608
|
-
assertz(session_user_tools(SessionId, ToolName)),
|
|
609
|
-
assertz(session_user_tool_schema(SessionId, ToolName, Schema)),
|
|
610
|
-
% Store the source code for the tool description. Reverses the load-time
|
|
611
|
-
% task/prompt rewrites and normalizes variables for deterministic export.
|
|
612
|
-
format_tool_source(ToolHead, Description, Body, SourceCode),
|
|
613
|
-
assertz(SessionId:tool_source(ToolName, SourceCode)),
|
|
614
|
-
% Assert the tool implementation (use just ToolHead for execution)
|
|
615
|
-
assertz(SessionId:(tool(ToolHead) :- ExpandedBody)).
|
|
780
|
+
repl_register_tool(SessionId, ToolHead, Description, none, Body, []).
|
|
616
781
|
|
|
617
782
|
%% Handle tool/1 without description: tool(Head) :- Body
|
|
618
783
|
process_clause((tool(ToolHead) :- Body), SessionId) :-
|
|
619
784
|
!,
|
|
785
|
+
repl_register_tool(SessionId, ToolHead, none, none, Body, []).
|
|
786
|
+
|
|
787
|
+
%% A tool clause annotated with the source variable names of its head, so the
|
|
788
|
+
%% schema tells the model `Path` rather than `arg1`. `preprocess_loaded_term/3`
|
|
789
|
+
%% wraps tool clauses because the names are lost once the clause is asserted.
|
|
790
|
+
process_clause('$dc_tool'(Term, Names), SessionId) :-
|
|
791
|
+
!,
|
|
792
|
+
( Term = (tool(Head, Description, Types) :- Body)
|
|
793
|
+
-> repl_register_tool(SessionId, Head, Description, Types, Body, Names)
|
|
794
|
+
; Term = (tool(Head, Description) :- Body)
|
|
795
|
+
-> repl_register_tool(SessionId, Head, Description, none, Body, Names)
|
|
796
|
+
; Term = (tool(Head) :- Body)
|
|
797
|
+
-> repl_register_tool(SessionId, Head, none, none, Body, Names)
|
|
798
|
+
; true
|
|
799
|
+
).
|
|
800
|
+
|
|
801
|
+
%% A tool written as a *fact* is not an annotation. It used to be asserted as a
|
|
802
|
+
%% plain `tool/2` (or `tool/3`) fact that nothing reads: the model got no tool,
|
|
803
|
+
%% no error at edit time, and "Tool not found" only when a nested agent called
|
|
804
|
+
%% it. Refuse it and name the shape that works.
|
|
805
|
+
process_clause(tool(_, _), _SessionId) :-
|
|
806
|
+
!,
|
|
807
|
+
throw(error(domain_error(tool_clause, tool/2),
|
|
808
|
+
context(process_clause/2,
|
|
809
|
+
'a tool needs a body: tool(Head, "what it does") :- Delegate.'))).
|
|
810
|
+
process_clause(tool(_, _, _), _SessionId) :-
|
|
811
|
+
!,
|
|
812
|
+
throw(error(domain_error(tool_clause, tool/3),
|
|
813
|
+
context(process_clause/2,
|
|
814
|
+
'a tool needs a body: tool(Head, "what it does", [InputTypes]) :- Delegate.'))).
|
|
815
|
+
|
|
816
|
+
%% repl_register_tool(+SessionId, +Head, +Description, +TypesOrNone, +Body, +Names)
|
|
817
|
+
%%
|
|
818
|
+
%% One tool per name. A second registration used to be accepted silently, so the
|
|
819
|
+
%% same tool appeared twice to the model and an old implementation could stay
|
|
820
|
+
%% live while the export carried the new one. Replacing is `define`'s job, whose
|
|
821
|
+
%% block may carry the tool clause next to the predicate.
|
|
822
|
+
repl_register_tool(SessionId, ToolHead, Description, Types, Body, Names) :-
|
|
823
|
+
repl_check_script_name_for_head(SessionId, ToolHead),
|
|
824
|
+
ToolHead =.. [ToolName|ToolArgs],
|
|
825
|
+
length(ToolArgs, Arity),
|
|
826
|
+
( session_user_tools(SessionId, ToolName)
|
|
827
|
+
-> format(atom(Message),
|
|
828
|
+
'tool ~w is already registered. Replace it with define, putting the predicate and its tool(...) clause in the same block.',
|
|
829
|
+
[ToolName]),
|
|
830
|
+
throw(error(permission_error(modify, existing_tool, ToolName),
|
|
831
|
+
context(process_clause/2, Message)))
|
|
832
|
+
; true
|
|
833
|
+
),
|
|
620
834
|
(catch(expand_dict_dots(Body, ExpandedBody), _, fail) -> true ; ExpandedBody = Body),
|
|
621
|
-
extract_tool_schema(ToolHead,
|
|
835
|
+
extract_tool_schema(ToolHead, Description, Types, Names, ToolName, Schema),
|
|
622
836
|
assertz(session_user_tools(SessionId, ToolName)),
|
|
623
837
|
assertz(session_user_tool_schema(SessionId, ToolName, Schema)),
|
|
624
|
-
|
|
625
|
-
|
|
838
|
+
assertz(session_user_tool_arity(SessionId, ToolName, Arity)),
|
|
839
|
+
assertz(session_user_tool_head(SessionId, ToolName, ToolHead)),
|
|
840
|
+
assertz(session_user_tool_desc(SessionId, ToolName, Description)),
|
|
841
|
+
assertz(session_user_tool_names(SessionId, ToolName, Names)),
|
|
842
|
+
( is_list(Types) -> assertz(session_user_tool_types(SessionId, ToolName, Types)) ; true ),
|
|
843
|
+
assertz(session_user_tool_body(SessionId, ToolName, ExpandedBody)),
|
|
844
|
+
% The export source reverses the load-time task/prompt rewrites and
|
|
845
|
+
% normalizes variables, so an export round-trips deterministically.
|
|
846
|
+
( Types == none
|
|
847
|
+
-> format_tool_source(ToolHead, Description, Body, SourceCode)
|
|
848
|
+
; format_tool_source(ToolHead, Description, Types, Body, SourceCode)
|
|
849
|
+
),
|
|
626
850
|
assertz(SessionId:tool_source(ToolName, SourceCode)),
|
|
627
|
-
% Assert the tool implementation
|
|
851
|
+
% Assert the tool implementation (just the head; the body is the delegate).
|
|
628
852
|
assertz(SessionId:(tool(ToolHead) :- ExpandedBody)).
|
|
629
853
|
|
|
630
854
|
process_clause((Head :- Body), SessionId) :-
|
|
631
855
|
!,
|
|
856
|
+
repl_check_script_name_for_head(SessionId, Head),
|
|
632
857
|
% Expand dict dot-notation (Dict.Key → get_dict), then assert
|
|
633
858
|
( catch(expand_dict_dots(Body, ExpandedBody), _, fail)
|
|
634
859
|
-> true
|
|
@@ -636,6 +861,18 @@ process_clause((Head :- Body), SessionId) :-
|
|
|
636
861
|
),
|
|
637
862
|
assertz(SessionId:(Head :- ExpandedBody)).
|
|
638
863
|
|
|
864
|
+
%% A script link is a keyed annotation, not a predicate. Refuse a name that is
|
|
865
|
+
%% already a user predicate; a later predicate definition checks the reverse in
|
|
866
|
+
%% process_clause and repl_register_tool.
|
|
867
|
+
process_clause(script_file(Name, Path), SessionId) :-
|
|
868
|
+
!,
|
|
869
|
+
( atom(Name)
|
|
870
|
+
-> true
|
|
871
|
+
; throw(error(type_error(atom, Name), context(script_file/2, 'the link name must be an atom')))
|
|
872
|
+
),
|
|
873
|
+
repl_reject_existing_predicate_for_script(SessionId, Name),
|
|
874
|
+
assertz(SessionId:script_file(Name, Path)).
|
|
875
|
+
|
|
639
876
|
%% repl_delete/1 is a load-time directive that removes a predicate. It must
|
|
640
877
|
%% be handled before the generic fact clause so it is never asserted itself.
|
|
641
878
|
process_clause(repl_delete(Name/Arity), SessionId) :-
|
|
@@ -647,6 +884,7 @@ process_clause(repl_delete(Name/Arity), SessionId) :-
|
|
|
647
884
|
|
|
648
885
|
process_clause(Fact, SessionId) :-
|
|
649
886
|
% Simple fact
|
|
887
|
+
repl_check_script_name_for_head(SessionId, Fact),
|
|
650
888
|
assertz(SessionId:Fact).
|
|
651
889
|
|
|
652
890
|
%% ============================================================
|
|
@@ -720,64 +958,64 @@ expand_dict_expr(Expr, Expr).
|
|
|
720
958
|
|
|
721
959
|
%% extract_tool_schema(+ToolHead, +Description, -ToolName, -Schema)
|
|
722
960
|
extract_tool_schema(ToolHead, Description, ToolName, Schema) :-
|
|
723
|
-
extract_tool_schema(ToolHead, Description, [], ToolName, Schema).
|
|
961
|
+
extract_tool_schema(ToolHead, Description, [], [], ToolName, Schema).
|
|
724
962
|
|
|
725
|
-
%% extract_tool_schema(+Head, +Description, +Types, -ToolName, -Schema)
|
|
963
|
+
%% extract_tool_schema(+Head, +Description, +Types, +Names, -ToolName, -Schema)
|
|
726
964
|
%% `Types` is an optional list giving the type of each input (the leading args,
|
|
727
965
|
%% in order); a missing entry defaults to `string`. This is what lets a tool take
|
|
728
|
-
%% an object, an array or a number rather than only a string.
|
|
729
|
-
|
|
966
|
+
%% an object, an array or a number rather than only a string. `Names` are the
|
|
967
|
+
%% head's source variable names (`Path`, `Question`), so the model sees what an
|
|
968
|
+
%% argument means instead of `arg1`.
|
|
969
|
+
extract_tool_schema(ToolHead, Description, Types, Names, ToolName, Schema) :-
|
|
730
970
|
ToolHead =.. [ToolName|Args],
|
|
731
971
|
length(Args, Arity),
|
|
732
|
-
|
|
733
|
-
extract_params(Args, 1, Arity, Types, Inputs, Outputs),
|
|
972
|
+
extract_params(Args, Names, 1, Arity, Types, Inputs, Outputs),
|
|
734
973
|
(Description == none -> Desc = "" ; Desc = Description),
|
|
735
974
|
Schema = schema{
|
|
736
975
|
name: ToolName,
|
|
976
|
+
arity: Arity,
|
|
737
977
|
description: Desc,
|
|
738
978
|
inputs: Inputs,
|
|
739
979
|
outputs: Outputs
|
|
740
980
|
}.
|
|
741
981
|
|
|
742
|
-
%% extract_params(+Args, +Index, +Arity, +Types, -Inputs, -Outputs)
|
|
982
|
+
%% extract_params(+Args, +Names, +Index, +Arity, +Types, -Inputs, -Outputs)
|
|
743
983
|
%% The last argument is the output; the rest are inputs, typed from `Types`.
|
|
744
|
-
extract_params([], _, _, _, [], []) :- !.
|
|
745
|
-
extract_params([
|
|
746
|
-
|
|
984
|
+
extract_params([], _, _, _, _, [], []) :- !.
|
|
985
|
+
extract_params([Arg|Rest], Names, Index, Arity, Types, Inputs, Outputs) :-
|
|
986
|
+
param_name(Arg, Names, Index, Name, Label),
|
|
747
987
|
NextIndex is Index + 1,
|
|
748
|
-
extract_params(Rest, NextIndex, Arity, Types, RestInputs, RestOutputs),
|
|
988
|
+
extract_params(Rest, Names, NextIndex, Arity, Types, RestInputs, RestOutputs),
|
|
749
989
|
( Index == Arity
|
|
750
990
|
-> IsOutput = true, Type = string
|
|
751
991
|
; IsOutput = false, input_type(Index, Types, Type)
|
|
752
992
|
),
|
|
753
|
-
Param = param{name: Name, type: Type},
|
|
993
|
+
Param = param{name: Name, type: Type, description: Label},
|
|
754
994
|
( IsOutput == true
|
|
755
995
|
-> Inputs = RestInputs, Outputs = [Param|RestOutputs]
|
|
756
996
|
; Inputs = [Param|RestInputs], Outputs = RestOutputs
|
|
757
997
|
).
|
|
758
998
|
|
|
999
|
+
%% param_name(+HeadArg, +Names, +Index, -Name, -Label)
|
|
1000
|
+
%% The wire name stays `argN` (a model bridge and existing harnesses use it);
|
|
1001
|
+
%% the head's variable name becomes the parameter description, so the model
|
|
1002
|
+
%% reads `Path` and `Question` instead of an unexplained `arg1`.
|
|
1003
|
+
param_name(_Arg, Names, Index, Name, Label) :-
|
|
1004
|
+
format(atom(Name), 'arg~w', [Index]),
|
|
1005
|
+
( nth1(Index, Names, Candidate),
|
|
1006
|
+
atom(Candidate)
|
|
1007
|
+
-> Label = Candidate
|
|
1008
|
+
; Label = Name
|
|
1009
|
+
).
|
|
1010
|
+
|
|
759
1011
|
%% input_type(+Index, +Types, -Type): the Index-th input's declared type.
|
|
1012
|
+
input_type(_, Types, string) :-
|
|
1013
|
+
\+ is_list(Types),
|
|
1014
|
+
!.
|
|
760
1015
|
input_type(1, [T|_], T) :- !.
|
|
761
1016
|
input_type(N, [_|Rest], T) :- N > 1, N1 is N - 1, input_type(N1, Rest, T).
|
|
762
1017
|
input_type(_, [], string).
|
|
763
1018
|
|
|
764
|
-
%% tool_type_warning(+Name, +Arity, +Types)
|
|
765
|
-
%% The last argument of a tool head is the output, so a type list should label
|
|
766
|
-
%% the other (Arity - 1) inputs. When it does not, the author has almost
|
|
767
|
-
%% certainly written a tool whose second input was taken for its output — the
|
|
768
|
-
%% `tool(fs_write(Path, Content), …)` shape, which silently writes nothing.
|
|
769
|
-
tool_type_warning(ToolName, Arity, Types) :-
|
|
770
|
-
is_list(Types),
|
|
771
|
-
Types \== [],
|
|
772
|
-
length(Types, Declared),
|
|
773
|
-
Inputs is Arity - 1,
|
|
774
|
-
Declared =\= Inputs,
|
|
775
|
-
!,
|
|
776
|
-
format(user_error,
|
|
777
|
-
'Warning: tool ~w/~w declares ~w input type(s) but has ~w input argument(s). The last argument of a tool head is the output; a tool with another input needs a result argument (e.g. fs_write(Path, Content, ok)).~n',
|
|
778
|
-
[ToolName, Arity, Declared, Inputs]).
|
|
779
|
-
tool_type_warning(_, _, _).
|
|
780
|
-
|
|
781
1019
|
module_reference_string(Module, ModuleStr) :-
|
|
782
1020
|
atom(Module),
|
|
783
1021
|
!,
|
|
@@ -817,6 +1055,295 @@ import_library_into_mi(library(Name)) :-
|
|
|
817
1055
|
catch(deepclause_mi:use_module(library(Name)), _, true).
|
|
818
1056
|
import_library_into_mi(_).
|
|
819
1057
|
|
|
1058
|
+
%% ============================================================
|
|
1059
|
+
%% Harness modules (export / use_harness)
|
|
1060
|
+
%% ============================================================
|
|
1061
|
+
|
|
1062
|
+
harness_indicator_list(Spec, List) :-
|
|
1063
|
+
( is_list(Spec)
|
|
1064
|
+
-> List = Spec
|
|
1065
|
+
; Spec = (A, B)
|
|
1066
|
+
-> harness_indicator_list(A, LA),
|
|
1067
|
+
harness_indicator_list(B, LB),
|
|
1068
|
+
append(LA, LB, List)
|
|
1069
|
+
; List = [Spec]
|
|
1070
|
+
).
|
|
1071
|
+
|
|
1072
|
+
valid_harness_indicator(Name/Arity) :-
|
|
1073
|
+
atom(Name), integer(Arity), Arity >= 0,
|
|
1074
|
+
\+ repl_reserved_name(Name).
|
|
1075
|
+
|
|
1076
|
+
repl_hs_register_exports(SessionId, Spec) :-
|
|
1077
|
+
harness_indicator_list(Spec, Exports),
|
|
1078
|
+
repl_hs_export_list(Exports, SessionId).
|
|
1079
|
+
|
|
1080
|
+
repl_hs_export_list([], _SessionId).
|
|
1081
|
+
repl_hs_export_list([Indicator|Rest], SessionId) :-
|
|
1082
|
+
( valid_harness_indicator(Indicator)
|
|
1083
|
+
-> Indicator = Name/Arity,
|
|
1084
|
+
assertz(session_harness_export(SessionId, Name, Arity))
|
|
1085
|
+
; throw(error(domain_error(predicate_indicator, Indicator),
|
|
1086
|
+
context(export/1, 'export expects Name/Arity')))
|
|
1087
|
+
),
|
|
1088
|
+
repl_hs_export_list(Rest, SessionId).
|
|
1089
|
+
|
|
1090
|
+
repl_hs_register_module_exports(ModuleId, Spec) :-
|
|
1091
|
+
harness_indicator_list(Spec, Exports),
|
|
1092
|
+
repl_hs_module_export_list(Exports, ModuleId).
|
|
1093
|
+
|
|
1094
|
+
repl_hs_module_export_list([], _ModuleId).
|
|
1095
|
+
repl_hs_module_export_list([Indicator|Rest], ModuleId) :-
|
|
1096
|
+
( valid_harness_indicator(Indicator)
|
|
1097
|
+
-> Indicator = Name/Arity,
|
|
1098
|
+
assertz(harness_module_export(ModuleId, Name, Arity))
|
|
1099
|
+
; throw(error(domain_error(predicate_indicator, Indicator),
|
|
1100
|
+
context(export/1, 'export expects Name/Arity')))
|
|
1101
|
+
),
|
|
1102
|
+
repl_hs_module_export_list(Rest, ModuleId).
|
|
1103
|
+
|
|
1104
|
+
%% repl_hs_register_imports(+Owner, +SessionId, +Name, +VersionOpt, +Spec)
|
|
1105
|
+
%% Owner is a harness module id, or the session id for the caller.
|
|
1106
|
+
repl_hs_register_imports(Owner, SessionId, Name, VersionOpt, Spec) :-
|
|
1107
|
+
harness_indicator_list(Spec, Exports),
|
|
1108
|
+
repl_hs_find_target(SessionId, Name, VersionOpt, TargetModule),
|
|
1109
|
+
repl_hs_import_list(Exports, Owner, SessionId, TargetModule).
|
|
1110
|
+
|
|
1111
|
+
repl_hs_find_target(SessionId, Name, none, TargetModule) :-
|
|
1112
|
+
findall(ModuleId,
|
|
1113
|
+
session_harness_module(SessionId, Name, _, _, ModuleId),
|
|
1114
|
+
Modules0),
|
|
1115
|
+
sort(Modules0, Modules),
|
|
1116
|
+
( Modules = [TargetModule|_]
|
|
1117
|
+
-> true
|
|
1118
|
+
; throw(error(existence_error(harness_module, Name),
|
|
1119
|
+
context(use_harness/2, 'harness module is not available in this session')))
|
|
1120
|
+
).
|
|
1121
|
+
repl_hs_find_target(SessionId, Name, version(Version0), TargetModule) :-
|
|
1122
|
+
normalize_harness_version(Version0, Version),
|
|
1123
|
+
findall(ModuleId,
|
|
1124
|
+
session_harness_module(SessionId, Name, Version, _, ModuleId),
|
|
1125
|
+
Modules0),
|
|
1126
|
+
sort(Modules0, Modules),
|
|
1127
|
+
( Modules = [TargetModule|_]
|
|
1128
|
+
-> true
|
|
1129
|
+
; throw(error(existence_error(harness_module, Name),
|
|
1130
|
+
context(use_harness/2, 'harness module is not available in this session')))
|
|
1131
|
+
).
|
|
1132
|
+
|
|
1133
|
+
normalize_harness_version(Value, Normalized) :-
|
|
1134
|
+
( number(Value)
|
|
1135
|
+
-> number_string(Value, Normalized)
|
|
1136
|
+
; atom(Value)
|
|
1137
|
+
-> atom_string(Value, Normalized)
|
|
1138
|
+
; string(Value)
|
|
1139
|
+
-> Normalized = Value
|
|
1140
|
+
; term_string(Value, Normalized)
|
|
1141
|
+
).
|
|
1142
|
+
|
|
1143
|
+
harness_import_conflicts(Owner, SessionId, Indicator) :-
|
|
1144
|
+
( Owner == SessionId
|
|
1145
|
+
-> ( session_harness_import(SessionId, Indicator, _, _)
|
|
1146
|
+
; session_harness_export(SessionId, Name, Arity),
|
|
1147
|
+
Indicator = Name/Arity
|
|
1148
|
+
)
|
|
1149
|
+
; harness_module_import(Owner, Indicator, _, _)
|
|
1150
|
+
; Indicator = Name/Arity,
|
|
1151
|
+
module_local_predicate(Owner, Name, Arity)
|
|
1152
|
+
).
|
|
1153
|
+
|
|
1154
|
+
repl_hs_import_list([], _Owner, _SessionId, _TargetModule).
|
|
1155
|
+
repl_hs_import_list([Indicator|Rest], Owner, SessionId, TargetModule) :-
|
|
1156
|
+
( Indicator = ExportName/ExportArity
|
|
1157
|
+
-> true
|
|
1158
|
+
; throw(error(domain_error(predicate_indicator, Indicator),
|
|
1159
|
+
context(use_harness/2, 'imports must be Name/Arity')))
|
|
1160
|
+
),
|
|
1161
|
+
( harness_module_export(TargetModule, ExportName, ExportArity)
|
|
1162
|
+
-> true
|
|
1163
|
+
; throw(error(existence_error(export, Indicator),
|
|
1164
|
+
context(use_harness/2, 'the imported harness does not export this predicate')))
|
|
1165
|
+
),
|
|
1166
|
+
( harness_import_conflicts(Owner, SessionId, Indicator)
|
|
1167
|
+
-> throw(error(permission_error(import, duplicate_predicate, Indicator),
|
|
1168
|
+
context(use_harness/2, 'name is already present in the importing harness')))
|
|
1169
|
+
; true
|
|
1170
|
+
),
|
|
1171
|
+
( Owner == SessionId
|
|
1172
|
+
-> assertz(session_harness_import(SessionId, Indicator, TargetModule, Indicator))
|
|
1173
|
+
; assertz(harness_module_import(Owner, Indicator, TargetModule, Indicator))
|
|
1174
|
+
),
|
|
1175
|
+
repl_hs_import_list(Rest, Owner, SessionId, TargetModule).
|
|
1176
|
+
|
|
1177
|
+
%% repl_add_harness_import(+SessionId, +Name, +VersionOpt, +Predicates, -Result)
|
|
1178
|
+
%% Adds a use_harness directive to the living module inside the current edit
|
|
1179
|
+
%% transaction. The caller must have preloaded the target module.
|
|
1180
|
+
repl_add_harness_import(SessionId, Name, VersionOpt, Predicates, Result) :-
|
|
1181
|
+
catch(
|
|
1182
|
+
( guard_not_frozen(SessionId),
|
|
1183
|
+
( VersionOpt == none
|
|
1184
|
+
-> Directive = use_harness(Name, Predicates)
|
|
1185
|
+
; Directive = use_harness(Name, VersionOpt, Predicates)
|
|
1186
|
+
),
|
|
1187
|
+
process_directive(Directive, SessionId),
|
|
1188
|
+
Result = ok
|
|
1189
|
+
),
|
|
1190
|
+
ImportError,
|
|
1191
|
+
format(atom(Result), 'error(~w)', [ImportError])
|
|
1192
|
+
).
|
|
1193
|
+
|
|
1194
|
+
%% process_directive(export(...), +SessionId)
|
|
1195
|
+
process_directive(export(Spec), SessionId) :-
|
|
1196
|
+
!,
|
|
1197
|
+
repl_hs_register_exports(SessionId, Spec),
|
|
1198
|
+
record_directive(SessionId, export(Spec)).
|
|
1199
|
+
|
|
1200
|
+
%% process_directive(use_harness(Name, Imports), +SessionId)
|
|
1201
|
+
process_directive(use_harness(Name, Imports), SessionId) :-
|
|
1202
|
+
!,
|
|
1203
|
+
repl_hs_register_imports(SessionId, SessionId, Name, none, Imports),
|
|
1204
|
+
record_directive(SessionId, use_harness(Name, Imports)).
|
|
1205
|
+
process_directive(use_harness(Name, version(Version), Imports), SessionId) :-
|
|
1206
|
+
!,
|
|
1207
|
+
repl_hs_register_imports(SessionId, SessionId, Name, version(Version), Imports),
|
|
1208
|
+
record_directive(SessionId, use_harness(Name, version(Version), Imports)).
|
|
1209
|
+
|
|
1210
|
+
%% repl_harness_module_info_json(+SessionId, -Json)
|
|
1211
|
+
%% Provenance for the UI: imported modules, exported predicates and the local
|
|
1212
|
+
%% caller's exports. Never exposes private predicates.
|
|
1213
|
+
repl_harness_module_info_json(SessionId, Json) :-
|
|
1214
|
+
findall(ModuleDict,
|
|
1215
|
+
( session_harness_module(SessionId, Name, Version, Hash, ModuleId),
|
|
1216
|
+
findall(ExportName/ExportArity,
|
|
1217
|
+
harness_module_export(ModuleId, ExportName, ExportArity),
|
|
1218
|
+
ExportIndicators),
|
|
1219
|
+
maplist(indicator_text, ExportIndicators, Exports),
|
|
1220
|
+
findall(ImportDict,
|
|
1221
|
+
( harness_module_import(ModuleId, FromName/FromArity, Target, TargetName/TargetArity),
|
|
1222
|
+
indicator_text(FromName/FromArity, From),
|
|
1223
|
+
indicator_text(TargetName/TargetArity, As),
|
|
1224
|
+
ImportDict = _{from: From, as: As, module: Target}
|
|
1225
|
+
),
|
|
1226
|
+
Imports),
|
|
1227
|
+
ModuleDict = _{name: Name, version: Version, hash: Hash, module: ModuleId,
|
|
1228
|
+
exports: Exports, imports: Imports}
|
|
1229
|
+
),
|
|
1230
|
+
Modules),
|
|
1231
|
+
findall(IndicatorText,
|
|
1232
|
+
( session_harness_export(SessionId, Name, Arity),
|
|
1233
|
+
indicator_text(Name/Arity, IndicatorText)
|
|
1234
|
+
),
|
|
1235
|
+
LocalExports),
|
|
1236
|
+
Json =_{modules: Modules, exports: LocalExports}.
|
|
1237
|
+
|
|
1238
|
+
indicator_text(Name/Arity, Text) :-
|
|
1239
|
+
format(atom(Text), '~w/~w', [Name, Arity]).
|
|
1240
|
+
|
|
1241
|
+
%% repl_set_harness_exports(+SessionId, +Predicates, -Result)
|
|
1242
|
+
%% Replace the current export contract within the open edit transaction.
|
|
1243
|
+
repl_set_harness_exports(SessionId, Predicates, Result) :-
|
|
1244
|
+
catch(
|
|
1245
|
+
( guard_not_frozen(SessionId),
|
|
1246
|
+
repl_validate_export_selection(SessionId, Predicates),
|
|
1247
|
+
retractall(session_directive(SessionId, export(_))),
|
|
1248
|
+
retractall(session_harness_export(SessionId, _, _)),
|
|
1249
|
+
( Predicates == []
|
|
1250
|
+
-> true
|
|
1251
|
+
; process_directive(export(Predicates), SessionId)
|
|
1252
|
+
),
|
|
1253
|
+
Result = ok
|
|
1254
|
+
),
|
|
1255
|
+
ExportError,
|
|
1256
|
+
format(atom(Result), 'error(~w)', [ExportError])
|
|
1257
|
+
).
|
|
1258
|
+
|
|
1259
|
+
repl_validate_export_selection(_SessionId, []).
|
|
1260
|
+
repl_validate_export_selection(SessionId, [Name/Arity|Rest]) :-
|
|
1261
|
+
( atom(Name), integer(Arity), Arity >= 0, module_local_predicate(SessionId, Name, Arity)
|
|
1262
|
+
-> repl_validate_export_selection(SessionId, Rest)
|
|
1263
|
+
; throw(error(existence_error(export, Name/Arity),
|
|
1264
|
+
context(export/1, 'can only export predicates defined by this harness')))
|
|
1265
|
+
).
|
|
1266
|
+
|
|
1267
|
+
%% ============================================================
|
|
1268
|
+
%% Module loading (preloaded by the embedding before the main source)
|
|
1269
|
+
%% ============================================================
|
|
1270
|
+
|
|
1271
|
+
ensure_harness_module(ModuleId) :-
|
|
1272
|
+
( current_module(ModuleId)
|
|
1273
|
+
-> true
|
|
1274
|
+
; assertz(ModuleId:harness_module_marker)
|
|
1275
|
+
).
|
|
1276
|
+
|
|
1277
|
+
%% repl_load_harness_module(+SessionId, +ModuleId, +Name, +Version, +Hash, +Code)
|
|
1278
|
+
%% Loads one dependency into its own SWI module. The module is immutable at the
|
|
1279
|
+
%% public boundary: only exported predicates are registered as imports.
|
|
1280
|
+
repl_load_harness_module(SessionId, ModuleId, Name, Version0, Hash, Code) :-
|
|
1281
|
+
atom(ModuleId),
|
|
1282
|
+
atom(Name),
|
|
1283
|
+
normalize_harness_version(Version0, Version),
|
|
1284
|
+
( session_harness_module(SessionId, Name, Version, Hash, _Instance)
|
|
1285
|
+
-> true
|
|
1286
|
+
; format(atom(Instance), '~w_~w', [ModuleId, SessionId]),
|
|
1287
|
+
( harness_module_owner(Instance, Name, Version, Hash)
|
|
1288
|
+
-> true
|
|
1289
|
+
; assertz(harness_module_owner(Instance, Name, Version, Hash)),
|
|
1290
|
+
ensure_harness_module(Instance),
|
|
1291
|
+
parse_harness_module_string(Code, Instance, SessionId)
|
|
1292
|
+
),
|
|
1293
|
+
assertz(session_harness_module(SessionId, Name, Version, Hash, Instance))
|
|
1294
|
+
).
|
|
1295
|
+
|
|
1296
|
+
parse_harness_module_string(Code, ModuleId, SessionId) :-
|
|
1297
|
+
open_string(Code, Stream),
|
|
1298
|
+
parse_harness_module_clauses(Stream, ModuleId, SessionId),
|
|
1299
|
+
close(Stream),
|
|
1300
|
+
repl_validate_module_exports(ModuleId).
|
|
1301
|
+
|
|
1302
|
+
parse_harness_module_clauses(Stream, ModuleId, SessionId) :-
|
|
1303
|
+
read_term(Stream, RawTerm, [module(ModuleId), variable_names(Bindings)]),
|
|
1304
|
+
( RawTerm == end_of_file
|
|
1305
|
+
-> true
|
|
1306
|
+
; preprocess_loaded_term(RawTerm, Bindings, PreparedTerms),
|
|
1307
|
+
forall(member(Term, PreparedTerms),
|
|
1308
|
+
process_harness_term(Term, ModuleId, SessionId)),
|
|
1309
|
+
parse_harness_module_clauses(Stream, ModuleId, SessionId)
|
|
1310
|
+
).
|
|
1311
|
+
|
|
1312
|
+
process_harness_term((:- export(Spec)), ModuleId, _SessionId) :-
|
|
1313
|
+
!, repl_hs_register_module_exports(ModuleId, Spec).
|
|
1314
|
+
process_harness_term((:- use_harness(Name, Imports)), ModuleId, SessionId) :-
|
|
1315
|
+
!, repl_hs_register_imports(ModuleId, SessionId, Name, none, Imports).
|
|
1316
|
+
process_harness_term((:- use_harness(Name, version(Version), Imports)), ModuleId, SessionId) :-
|
|
1317
|
+
!, repl_hs_register_imports(ModuleId, SessionId, Name, version(Version), Imports).
|
|
1318
|
+
process_harness_term((:- use_module(Module)), ModuleId, _SessionId) :-
|
|
1319
|
+
!, catch(ModuleId:use_module(Module), _, true).
|
|
1320
|
+
process_harness_term((:- ModuleSpec), ModuleId, _SessionId) :-
|
|
1321
|
+
!,
|
|
1322
|
+
( ModuleSpec = use_module(Module, _)
|
|
1323
|
+
-> catch(ModuleId:use_module(Module), _, true)
|
|
1324
|
+
; true
|
|
1325
|
+
).
|
|
1326
|
+
process_harness_term('$dc_tool'(_, _), _ModuleId, _SessionId) :- !.
|
|
1327
|
+
process_harness_term((tool(_, _) :- _), _ModuleId, _SessionId) :- !.
|
|
1328
|
+
process_harness_term((tool(_, _, _) :- _), _ModuleId, _SessionId) :- !.
|
|
1329
|
+
process_harness_term((tool(_) :- _), _ModuleId, _SessionId) :- !.
|
|
1330
|
+
process_harness_term((Head :- Body), ModuleId, _SessionId) :-
|
|
1331
|
+
!,
|
|
1332
|
+
( catch(expand_dict_dots(Body, ExpandedBody), _, fail)
|
|
1333
|
+
-> true
|
|
1334
|
+
; ExpandedBody = Body
|
|
1335
|
+
),
|
|
1336
|
+
assertz(ModuleId:(Head :- ExpandedBody)).
|
|
1337
|
+
process_harness_term(Fact, ModuleId, _SessionId) :-
|
|
1338
|
+
( Fact = script_file(_, _)
|
|
1339
|
+
-> true
|
|
1340
|
+
; Fact = tool(_, _)
|
|
1341
|
+
-> true
|
|
1342
|
+
; Fact = tool(_, _, _)
|
|
1343
|
+
-> true
|
|
1344
|
+
; assertz(ModuleId:Fact)
|
|
1345
|
+
).
|
|
1346
|
+
|
|
820
1347
|
%% process_directive(+Directive, +SessionId)
|
|
821
1348
|
process_directive(param(Key, Desc), SessionId) :-
|
|
822
1349
|
!,
|
|
@@ -1056,7 +1583,13 @@ create_engine(SessionId, _MemoryId, Args, Params, InitialMemory, Engine) :-
|
|
|
1056
1583
|
% Check if tracing is enabled and store in session state
|
|
1057
1584
|
(get_dict(trace, Params, true) -> TraceEnabled = true ; TraceEnabled = false),
|
|
1058
1585
|
assertz(session_trace_enabled(SessionId, TraceEnabled)),
|
|
1059
|
-
|
|
1586
|
+
% Keep the session trace across turns: create_engine runs once per turn,
|
|
1587
|
+
% and a Work session can execute several workflows. Only create_session/
|
|
1588
|
+
% explicit clear resets the store.
|
|
1589
|
+
( session_trace_count(SessionId, _)
|
|
1590
|
+
-> true
|
|
1591
|
+
; assertz(session_trace_count(SessionId, 0))
|
|
1592
|
+
),
|
|
1060
1593
|
% Check for gas limit in params
|
|
1061
1594
|
(get_dict(gas_limit, Params, GasLimit) -> true ; GasLimit = -1),
|
|
1062
1595
|
% Create initial state with provided memory, params, context stack, trace depth and gas
|
|
@@ -1128,9 +1661,9 @@ step_engine_handle(SessionId, Engine, Status, Content, Payload) :-
|
|
|
1128
1661
|
; % Engine finished - return trace if enabled
|
|
1129
1662
|
Status = finished,
|
|
1130
1663
|
Content = '',
|
|
1131
|
-
( session_trace_enabled(SessionId, true)
|
|
1132
|
-
|
|
1133
|
-
|
|
1664
|
+
( session_trace_enabled(SessionId, true)
|
|
1665
|
+
-> session_trace_list(SessionId, TraceLog),
|
|
1666
|
+
Payload = payload{trace: TraceLog}
|
|
1134
1667
|
; Payload = none
|
|
1135
1668
|
)
|
|
1136
1669
|
),
|
|
@@ -1142,9 +1675,9 @@ step_engine_handle(SessionId, Engine, Status, Content, Payload) :-
|
|
|
1142
1675
|
; format(atom(ErrMsg), 'Engine error: ~w', [Error]),
|
|
1143
1676
|
Status = error,
|
|
1144
1677
|
Content = ErrMsg,
|
|
1145
|
-
( session_trace_enabled(SessionId, true)
|
|
1146
|
-
|
|
1147
|
-
|
|
1678
|
+
( session_trace_enabled(SessionId, true)
|
|
1679
|
+
-> session_trace_list(SessionId, TraceLog),
|
|
1680
|
+
Payload = payload{trace: TraceLog}
|
|
1148
1681
|
; Payload = none
|
|
1149
1682
|
)
|
|
1150
1683
|
)
|
|
@@ -1184,8 +1717,7 @@ create_session(SessionId, Params, HarnessMemory, MetaMemory) :-
|
|
|
1184
1717
|
),
|
|
1185
1718
|
retractall(session_trace_enabled(SessionId, _)),
|
|
1186
1719
|
assertz(session_trace_enabled(SessionId, TraceEnabled)),
|
|
1187
|
-
|
|
1188
|
-
assertz(session_trace_log(SessionId, [])),
|
|
1720
|
+
reset_trace_store(SessionId),
|
|
1189
1721
|
retractall(session_repl_memory(SessionId, _)),
|
|
1190
1722
|
assertz(session_repl_memory(SessionId, HarnessMemory)),
|
|
1191
1723
|
retractall(session_meta_memory(SessionId, _)),
|
|
@@ -1244,7 +1776,10 @@ clear_session(SessionId) :-
|
|
|
1244
1776
|
retractall(repl_snapshot(SessionId, _)),
|
|
1245
1777
|
retractall(session_user_tools(SessionId, _)),
|
|
1246
1778
|
retractall(session_user_tool_schema(SessionId, _, _)),
|
|
1779
|
+
retractall(session_source_view(SessionId, _, _)),
|
|
1247
1780
|
retractall(session_builtin_tool(SessionId, _)),
|
|
1781
|
+
retractall(session_harness_export(SessionId, _, _)),
|
|
1782
|
+
retractall(session_harness_import(SessionId, _, _, _)),
|
|
1248
1783
|
repl_abolish_user_predicates(SessionId),
|
|
1249
1784
|
install_builtin_tools(SessionId).
|
|
1250
1785
|
|
|
@@ -1269,14 +1804,17 @@ destroy_session(SessionId) :-
|
|
|
1269
1804
|
retractall(repl_snapshot(SessionId, _)),
|
|
1270
1805
|
retractall(session_params(SessionId, _)),
|
|
1271
1806
|
retractall(session_trace_enabled(SessionId, _)),
|
|
1272
|
-
|
|
1807
|
+
reset_trace_store(SessionId).
|
|
1273
1808
|
|
|
1274
1809
|
%% load_code(+SessionId, +Code, -Error)
|
|
1275
1810
|
%% Parses DML source into the session module, running the normal source
|
|
1276
1811
|
%% preprocessing (task/prompt rewrite, interpolation, expand_term).
|
|
1277
1812
|
load_code(SessionId, Code, Error) :-
|
|
1278
1813
|
catch(
|
|
1279
|
-
(
|
|
1814
|
+
( mi_clear_cut_classes(SessionId),
|
|
1815
|
+
guard_not_frozen(SessionId),
|
|
1816
|
+
retractall(session_harness_export(SessionId, _, _)),
|
|
1817
|
+
retractall(session_harness_import(SessionId, _, _, _)),
|
|
1280
1818
|
parse_dml_string(Code, SessionId),
|
|
1281
1819
|
Error = none
|
|
1282
1820
|
),
|
|
@@ -1375,6 +1913,14 @@ trim_trailing_ws(Str, Len0, Len) :-
|
|
|
1375
1913
|
%% Names is the read_term variable_names list (Name=Var), used to render
|
|
1376
1914
|
%% bindings for solution events.
|
|
1377
1915
|
read_repl_goal(SessionId, Text, Goal, Names) :-
|
|
1916
|
+
catch(read_repl_goal_once(SessionId, Text, Goal, Names), ReadError,
|
|
1917
|
+
( repl_clp_operator_error(ReadError),
|
|
1918
|
+
repl_install_clp_operators(SessionId)
|
|
1919
|
+
-> read_repl_goal_once(SessionId, Text, Goal, Names)
|
|
1920
|
+
; throw(ReadError)
|
|
1921
|
+
)).
|
|
1922
|
+
|
|
1923
|
+
read_repl_goal_once(SessionId, Text, Goal, Names) :-
|
|
1378
1924
|
% read_term/3 requires a full stop; REPL callers may omit it (`-g "p(X)"`).
|
|
1379
1925
|
ensure_full_stop(Text, Text1),
|
|
1380
1926
|
open_string(Text1, Stream),
|
|
@@ -1392,9 +1938,33 @@ read_repl_goal(SessionId, Text, Goal, Names) :-
|
|
|
1392
1938
|
% hoisted into a goal *before* the goal that uses it. expand_term_interp
|
|
1393
1939
|
% embeds the interpolation term in the argument position instead, which is
|
|
1394
1940
|
% wrong for a raw goal such as verify(X, "Is {X} > 0?", V).
|
|
1395
|
-
expand_body_interp(Transformed, Names0,
|
|
1941
|
+
expand_body_interp(Transformed, Names0, Interpolated),
|
|
1942
|
+
% `Dict.key` is expanded when a clause is asserted, but an `execute` goal is
|
|
1943
|
+
% read and called directly, so it used to reach `'.'/2` and silently return
|
|
1944
|
+
% the term `_{a:1}.a` instead of 1.
|
|
1945
|
+
( catch(expand_dict_dots(Interpolated, Expanded), _, fail)
|
|
1946
|
+
-> Goal = Expanded
|
|
1947
|
+
; Goal = Interpolated
|
|
1948
|
+
),
|
|
1396
1949
|
Names = Names0.
|
|
1397
1950
|
|
|
1951
|
+
%% repl_clp_operator_error(+Error)
|
|
1952
|
+
%% `X #> 3` in a goal is a *syntax* error until the CLP operators are declared
|
|
1953
|
+
%% in the session module: the docs tell the agent to run CLP with `execute`, and
|
|
1954
|
+
%% without this it got `syntax_error(operator_expected)` and no hint.
|
|
1955
|
+
repl_clp_operator_error(error(syntax_error(operator_expected), _)).
|
|
1956
|
+
|
|
1957
|
+
repl_install_clp_operators(SessionId) :-
|
|
1958
|
+
% Import predicates *and* operators, into the session module and the
|
|
1959
|
+
% meta-interpreter's module: the goal is read against the session and run
|
|
1960
|
+
% through `deepclause_mi`.
|
|
1961
|
+
forall(member(Library, [clpfd, clpr, clpq]),
|
|
1962
|
+
catch(repl_load_clp_library(SessionId, Library), _, true)).
|
|
1963
|
+
|
|
1964
|
+
repl_load_clp_library(SessionId, Library) :-
|
|
1965
|
+
catch(SessionId:use_module(library(Library)), _, true),
|
|
1966
|
+
catch(import_library_into_mi(library(Library)), _, true).
|
|
1967
|
+
|
|
1398
1968
|
%% repl_solve(+Goal, +Names, +SessionId, +State)
|
|
1399
1969
|
%% Enumerating goal-mode solver. Yields one solution(Bindings, Deterministic)
|
|
1400
1970
|
%% per answer and fails so engine_next/2 backtracks for the next solution.
|
|
@@ -1417,7 +1987,13 @@ repl_solve(Goal, Names, SessionId, State) :-
|
|
|
1417
1987
|
).
|
|
1418
1988
|
|
|
1419
1989
|
repl_solve_loop(Goal, Names, SessionId, State) :-
|
|
1420
|
-
|
|
1990
|
+
( mi_native_query(SessionId, Goal)
|
|
1991
|
+
-> % A cut in a query commits the query itself. A pure query has no
|
|
1992
|
+
% state to thread, so it can run natively and get real cut semantics.
|
|
1993
|
+
call(SessionId:Goal),
|
|
1994
|
+
StateOut = State
|
|
1995
|
+
; mi_call(Goal, State, StateOut)
|
|
1996
|
+
),
|
|
1421
1997
|
collect_bindings(Names, Bindings),
|
|
1422
1998
|
store_memory_for_state(SessionId, StateOut),
|
|
1423
1999
|
engine_yield(solution(Bindings, false)),
|
|
@@ -1464,12 +2040,11 @@ repl_listing(Filter, Text) :-
|
|
|
1464
2040
|
|
|
1465
2041
|
repl_listing_output(SessionId, Filter) :-
|
|
1466
2042
|
( Filter == tools
|
|
1467
|
-
->
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
)
|
|
2043
|
+
-> repl_listing_tools(SessionId, false)
|
|
2044
|
+
; Filter == 'tools:all'
|
|
2045
|
+
-> repl_listing_tools(SessionId, true)
|
|
2046
|
+
; Filter == scripts
|
|
2047
|
+
-> repl_listing_scripts(SessionId)
|
|
1473
2048
|
; Filter == history
|
|
1474
2049
|
-> repl_history_listing(SessionId)
|
|
1475
2050
|
; ( current_predicate(SessionId:module_doc/1),
|
|
@@ -1484,18 +2059,116 @@ repl_listing_output(SessionId, Filter) :-
|
|
|
1484
2059
|
Preds0),
|
|
1485
2060
|
sort(Preds0, Preds),
|
|
1486
2061
|
forall(member(Name/Arity, Preds),
|
|
1487
|
-
( repl_listing_match(Filter, Name, Arity)
|
|
2062
|
+
( repl_listing_match(Filter, Name, Arity),
|
|
2063
|
+
\+ repl_annotation_predicate(Name, Arity)
|
|
1488
2064
|
-> format("~w/~w~n", [Name, Arity]),
|
|
1489
2065
|
( current_predicate(SessionId:doc/2),
|
|
1490
2066
|
SessionId:doc(Name/Arity, DocText)
|
|
1491
2067
|
-> format(" % ~w~n", [DocText])
|
|
1492
2068
|
; true
|
|
1493
2069
|
),
|
|
1494
|
-
|
|
1495
|
-
|
|
2070
|
+
repl_listing_tool_line(SessionId, Name, Arity),
|
|
2071
|
+
( session_source_view(SessionId, Name/Arity, SourceView)
|
|
2072
|
+
-> format("~w", [SourceView])
|
|
2073
|
+
; functor(Head, Name, Arity),
|
|
2074
|
+
forall(clause(SessionId:Head, ClauseBody), print_user_clause(Head, ClauseBody))
|
|
2075
|
+
)
|
|
1496
2076
|
; true
|
|
1497
2077
|
)
|
|
2078
|
+
),
|
|
2079
|
+
findall(Name/Arity-TargetModule,
|
|
2080
|
+
( session_harness_import(SessionId, Name/Arity, TargetModule, _),
|
|
2081
|
+
repl_listing_match(Filter, Name, Arity)
|
|
2082
|
+
),
|
|
2083
|
+
Imports0),
|
|
2084
|
+
sort(Imports0, Imports),
|
|
2085
|
+
forall(member(Name/Arity-TargetModule, Imports),
|
|
2086
|
+
format("~w/~w [imported from ~w, read-only]~n", [Name, Arity, TargetModule])
|
|
2087
|
+
),
|
|
2088
|
+
repl_listing_tool_summary(SessionId)
|
|
2089
|
+
).
|
|
2090
|
+
|
|
2091
|
+
%% repl_listing_scripts(+SessionId): the linked script files, which are
|
|
2092
|
+
%% annotations rather than predicates.
|
|
2093
|
+
repl_listing_scripts(SessionId) :-
|
|
2094
|
+
( current_predicate(SessionId:script_file/2)
|
|
2095
|
+
-> findall(Name-Path,
|
|
2096
|
+
( SessionId:script_file(Name, Path),
|
|
2097
|
+
( atom(Path) ; string(Path) )
|
|
2098
|
+
),
|
|
2099
|
+
Links0),
|
|
2100
|
+
sort(Links0, Links),
|
|
2101
|
+
( Links == []
|
|
2102
|
+
-> format("no script_file/2 links.~n")
|
|
2103
|
+
; forall(member(Name-Path, Links), format("~w -> ~w~n", [Name, Path]))
|
|
1498
2104
|
)
|
|
2105
|
+
; format("no script_file/2 links.~n")
|
|
2106
|
+
).
|
|
2107
|
+
|
|
2108
|
+
%% repl_listing_tool_line(+SessionId, +Name, +Arity)
|
|
2109
|
+
%% A tool annotation is stored outside the clause database, so `listing` used to
|
|
2110
|
+
%% show a predicate with no hint that a nested agent can call it — an agent that
|
|
2111
|
+
%% had just added a tool could not confirm it from the listing, and added it
|
|
2112
|
+
%% again.
|
|
2113
|
+
repl_listing_tool_line(SessionId, Name, Arity) :-
|
|
2114
|
+
session_user_tools(SessionId, Name),
|
|
2115
|
+
session_user_tool_arity(SessionId, Name, Arity),
|
|
2116
|
+
!,
|
|
2117
|
+
session_user_tool_head(SessionId, Name, Head),
|
|
2118
|
+
( session_user_tool_types(SessionId, Name, Types) -> true ; Types = none ),
|
|
2119
|
+
( session_user_tool_desc(SessionId, Name, Desc), Desc \== none -> true ; Desc = "" ),
|
|
2120
|
+
( Types == none
|
|
2121
|
+
-> format(" % tool ~q — ~w~n", [Head, Desc])
|
|
2122
|
+
; format(" % tool ~q ~w — ~w~n", [Head, Types, Desc])
|
|
2123
|
+
).
|
|
2124
|
+
repl_listing_tool_line(_, _, _).
|
|
2125
|
+
|
|
2126
|
+
%% repl_listing_tool_summary(+SessionId): one line at the end of `all`.
|
|
2127
|
+
repl_listing_tool_summary(SessionId) :-
|
|
2128
|
+
findall(ToolName/Arity,
|
|
2129
|
+
( session_user_tools(SessionId, ToolName),
|
|
2130
|
+
\+ session_builtin_tool(SessionId, ToolName),
|
|
2131
|
+
session_user_tool_arity(SessionId, ToolName, Arity)
|
|
2132
|
+
),
|
|
2133
|
+
Tools0),
|
|
2134
|
+
sort(Tools0, Tools),
|
|
2135
|
+
( Tools == []
|
|
2136
|
+
-> true
|
|
2137
|
+
; format("~ntools: ~w~n", [Tools])
|
|
2138
|
+
).
|
|
2139
|
+
|
|
2140
|
+
%% repl_listing_tools(+SessionId, +IncludeBuiltins)
|
|
2141
|
+
%% User tools first and compact; the built-in form tools only on request. The
|
|
2142
|
+
%% old listing led with seven built-ins as raw schema dicts, which buried the
|
|
2143
|
+
%% harness's own tools.
|
|
2144
|
+
repl_listing_tools(SessionId, IncludeBuiltins) :-
|
|
2145
|
+
findall(ToolName/Arity,
|
|
2146
|
+
( session_user_tools(SessionId, ToolName),
|
|
2147
|
+
session_user_tool_arity(SessionId, ToolName, Arity),
|
|
2148
|
+
( IncludeBuiltins == true
|
|
2149
|
+
-> true
|
|
2150
|
+
; \+ session_builtin_tool(SessionId, ToolName)
|
|
2151
|
+
)
|
|
2152
|
+
),
|
|
2153
|
+
Tools0),
|
|
2154
|
+
sort(Tools0, Tools),
|
|
2155
|
+
( Tools == []
|
|
2156
|
+
-> format("no tools registered. A tool is tool(Head, \"description\", [InputTypes]) :- Delegate.~n")
|
|
2157
|
+
; forall(member(ToolName/Arity, Tools),
|
|
2158
|
+
( session_user_tool_head(SessionId, ToolName, Head),
|
|
2159
|
+
( session_user_tool_types(SessionId, ToolName, Types) -> true ; Types = none ),
|
|
2160
|
+
( session_user_tool_desc(SessionId, ToolName, Desc), Desc \== none -> true ; Desc = "" ),
|
|
2161
|
+
format("~w/~w~n % tool ~q", [ToolName, Arity, Head]),
|
|
2162
|
+
( Types == none
|
|
2163
|
+
-> true
|
|
2164
|
+
; format(" ~w", [Types])
|
|
2165
|
+
),
|
|
2166
|
+
format(" — ~w~n", [Desc]),
|
|
2167
|
+
( SessionId:tool_source(ToolName, Source)
|
|
2168
|
+
-> format("~w~n", [Source])
|
|
2169
|
+
; true
|
|
2170
|
+
)
|
|
2171
|
+
))
|
|
1499
2172
|
).
|
|
1500
2173
|
|
|
1501
2174
|
%% A filter either names a predicate (`name/arity`), names a prefix, or is `all`.
|
|
@@ -1687,6 +2360,244 @@ record_directive(SessionId, Directive) :-
|
|
|
1687
2360
|
export_harness(SessionId, Header, Text) :-
|
|
1688
2361
|
with_output_to(string(Text), export_harness_output(SessionId, Header)).
|
|
1689
2362
|
|
|
2363
|
+
%% repl_shell_json(+SessionId, -Json)
|
|
2364
|
+
%%
|
|
2365
|
+
%% How the harness wants to be presented in Work mode. Two shapes are accepted:
|
|
2366
|
+
%%
|
|
2367
|
+
%% shell(agent([system("…"), host_tools(show_image, execute)]),
|
|
2368
|
+
%% workflows([name("Check mail"), predicate([goal(check_mail), args([(string:"Account")])])]),
|
|
2369
|
+
%% welcome("## …")).
|
|
2370
|
+
%%
|
|
2371
|
+
%% shell(workflow([goal(entry_point), args([(string:"Param 1")])])).
|
|
2372
|
+
%%
|
|
2373
|
+
%% A `shell(_{...})` dict is the third form. Everything is normalized to one
|
|
2374
|
+
%% JSON object; a harness without a shell is `null` and Work keeps its old view.
|
|
2375
|
+
repl_shell_json(SessionId, Json) :-
|
|
2376
|
+
( repl_shell_spec(SessionId, Spec)
|
|
2377
|
+
-> atom_json_dict(Json, Spec, [])
|
|
2378
|
+
; Json = "null"
|
|
2379
|
+
).
|
|
2380
|
+
|
|
2381
|
+
repl_shell_spec(SessionId, Spec) :-
|
|
2382
|
+
( clause(SessionId:shell(One), true)
|
|
2383
|
+
-> shell_from_one(One, Spec)
|
|
2384
|
+
; clause(SessionId:shell(Agent, Workflows, Welcome), true)
|
|
2385
|
+
-> shell_from_parts(Agent, Workflows, Welcome, Spec)
|
|
2386
|
+
; current_predicate(SessionId:workflow/1),
|
|
2387
|
+
findall(Entry, clause(SessionId:workflow(Entry),true), [Declared]),
|
|
2388
|
+
repl_workflow_literal(Declared),workflow_from(Declared,Workflow),
|
|
2389
|
+
Spec=shell_spec{kind:workflow,system:"Execute the declared workflow.",hostTools:[],tools:[],workflows:[Workflow],welcome:""}
|
|
2390
|
+
).
|
|
2391
|
+
|
|
2392
|
+
shell_from_one(workflow(Items), Spec) :-
|
|
2393
|
+
!,
|
|
2394
|
+
workflow_from(workflow(Items), Workflow),
|
|
2395
|
+
Spec = shell_spec{kind: workflow, system: "", hostTools: [], tools: [], workflows: [Workflow], welcome: ""}.
|
|
2396
|
+
shell_from_one(Dict, Spec) :-
|
|
2397
|
+
is_dict(Dict),
|
|
2398
|
+
!,
|
|
2399
|
+
shell_from_dict(Dict, Spec).
|
|
2400
|
+
shell_from_one(agent(Items), Spec) :-
|
|
2401
|
+
!,
|
|
2402
|
+
agent_items(Items, System, Tools),
|
|
2403
|
+
Spec = shell_spec{kind: agent, system: System, hostTools: Tools, tools: [], workflows: [], welcome: ""}.
|
|
2404
|
+
|
|
2405
|
+
shell_from_parts(AgentTerm, WorkflowsTerm, WelcomeTerm, Spec) :-
|
|
2406
|
+
agent_term(AgentTerm, System, Tools),
|
|
2407
|
+
workflows_term(WorkflowsTerm, Workflows),
|
|
2408
|
+
( WelcomeTerm = welcome(Welcome0)
|
|
2409
|
+
-> text_value(Welcome0, Welcome)
|
|
2410
|
+
; text_value(WelcomeTerm, Welcome)
|
|
2411
|
+
),
|
|
2412
|
+
Spec = shell_spec{kind: agent, system: System, hostTools: Tools, tools: [], workflows: Workflows, welcome: Welcome}.
|
|
2413
|
+
|
|
2414
|
+
shell_from_dict(Dict, Spec) :-
|
|
2415
|
+
( get_dict(kind, Dict, Kind0)
|
|
2416
|
+
-> text_value(Kind0, Kind)
|
|
2417
|
+
; shell_infer_kind(Dict, Kind)
|
|
2418
|
+
),
|
|
2419
|
+
( get_dict(system, Dict, Sys0) -> text_value(Sys0, System) ; System = "" ),
|
|
2420
|
+
( get_dict(host_tools, Dict, Tools0)
|
|
2421
|
+
-> host_tools_value(Tools0, Tools)
|
|
2422
|
+
; get_dict(hostTools, Dict, Tools1)
|
|
2423
|
+
-> host_tools_value(Tools1, Tools)
|
|
2424
|
+
; Tools = ["show_image", "execute"]
|
|
2425
|
+
),
|
|
2426
|
+
( get_dict(tools, Dict, ToolsList0) -> tools_term(ToolsList0, ToolSpecs) ; ToolSpecs = [] ),
|
|
2427
|
+
( get_dict(workflows, Dict, Wfs0) -> workflows_term(Wfs0, Workflows) ; Workflows = [] ),
|
|
2428
|
+
( get_dict(welcome, Dict, Welcome0) -> text_value(Welcome0, Welcome) ; Welcome = "" ),
|
|
2429
|
+
Base = shell_spec{kind: Kind, system: System, hostTools: Tools, tools: ToolSpecs, workflows: Workflows, welcome: Welcome},
|
|
2430
|
+
( get_dict(hooks, Dict, Hooks0)
|
|
2431
|
+
-> is_dict(Hooks0), dict_pairs(Hooks0, _, Pairs),
|
|
2432
|
+
maplist(shell_hook_pair, Pairs, HookPairs),
|
|
2433
|
+
dict_pairs(Hooks, hooks, HookPairs), put_dict(hooks, Base, Hooks, WithHooks)
|
|
2434
|
+
; WithHooks = Base
|
|
2435
|
+
),
|
|
2436
|
+
( get_dict(inbox, Dict, Inbox) -> put_dict(inbox, WithHooks, Inbox, Spec) ; Spec = WithHooks ).
|
|
2437
|
+
|
|
2438
|
+
shell_hook_pair(Key-Value, Key-Text) :- text_value(Value, Text).
|
|
2439
|
+
|
|
2440
|
+
shell_infer_kind(Dict, Kind) :-
|
|
2441
|
+
( get_dict(system, Dict, _)
|
|
2442
|
+
; get_dict(host_tools, Dict, _)
|
|
2443
|
+
; get_dict(hostTools, Dict, _)
|
|
2444
|
+
),
|
|
2445
|
+
!,
|
|
2446
|
+
Kind = agent.
|
|
2447
|
+
shell_infer_kind(_, workflow).
|
|
2448
|
+
|
|
2449
|
+
agent_term(agent(Items), System, Tools) :- !, agent_items(Items, System, Tools).
|
|
2450
|
+
agent_term(Items, System, Tools) :- is_list(Items), !, agent_items(Items, System, Tools).
|
|
2451
|
+
agent_term(_, "", ["show_image", "execute"]).
|
|
2452
|
+
|
|
2453
|
+
agent_items(Items, System, Tools) :-
|
|
2454
|
+
( member(system(S), Items) -> text_value(S, System) ; System = "" ),
|
|
2455
|
+
( member(Term, Items),
|
|
2456
|
+
compound(Term),
|
|
2457
|
+
( compound_name_arity(Term, host_tools, _) ; compound_name_arity(Term, hostTools, _) )
|
|
2458
|
+
-> Term =.. [_|Args],
|
|
2459
|
+
host_tools_args(Args, Tools)
|
|
2460
|
+
; Tools = ["show_image", "execute"]
|
|
2461
|
+
).
|
|
2462
|
+
|
|
2463
|
+
host_tools_args([Single], Tools) :-
|
|
2464
|
+
is_list(Single),
|
|
2465
|
+
!,
|
|
2466
|
+
host_tools_value(Single, Tools).
|
|
2467
|
+
host_tools_args(Args, Tools) :-
|
|
2468
|
+
maplist(text_value, Args, Tools).
|
|
2469
|
+
|
|
2470
|
+
host_tools_value(Tools, Normalized) :-
|
|
2471
|
+
( is_list(Tools)
|
|
2472
|
+
-> maplist(text_value, Tools, Normalized)
|
|
2473
|
+
; Normalized = []
|
|
2474
|
+
).
|
|
2475
|
+
|
|
2476
|
+
workflows_term(workflows(Items), Workflows) :- !, workflows_list(Items, Workflows).
|
|
2477
|
+
workflows_term(Items, Workflows) :- is_list(Items), !, workflows_list(Items, Workflows).
|
|
2478
|
+
workflows_term(Item, [Workflow]) :- workflow_from(Item, Workflow).
|
|
2479
|
+
|
|
2480
|
+
workflows_list([], []).
|
|
2481
|
+
workflows_list([name(Name), predicate(Pred)|Rest], [Workflow|Workflows]) :-
|
|
2482
|
+
!,
|
|
2483
|
+
text_value(Name, N),
|
|
2484
|
+
workflow_from_parts(N, Pred, Workflow),
|
|
2485
|
+
workflows_list(Rest, Workflows).
|
|
2486
|
+
workflows_list([name(Name), workflow(Pred)|Rest], [Workflow|Workflows]) :-
|
|
2487
|
+
!,
|
|
2488
|
+
text_value(Name, N),
|
|
2489
|
+
workflow_from_parts(N, Pred, Workflow),
|
|
2490
|
+
workflows_list(Rest, Workflows).
|
|
2491
|
+
workflows_list([Item|Rest], [Workflow|Workflows]) :-
|
|
2492
|
+
workflow_from(Item, Workflow),
|
|
2493
|
+
workflows_list(Rest, Workflows).
|
|
2494
|
+
|
|
2495
|
+
workflow_from(Dict, Workflow) :-
|
|
2496
|
+
is_dict(Dict),
|
|
2497
|
+
!,
|
|
2498
|
+
workflow_dict_parts(Dict, Name, Goal, Inputs, Outputs),
|
|
2499
|
+
Base = shell_workflow{name: Name, goal: Goal, inputs: Inputs, outputs: Outputs},
|
|
2500
|
+
(get_dict(description,Dict,Desc0) -> text_value(Desc0,Description),put_dict(description,Base,Description,Workflow) ; Workflow=Base).
|
|
2501
|
+
|
|
2502
|
+
workflow_dict_parts(Dict, Name, Goal, Inputs, Outputs) :-
|
|
2503
|
+
( get_dict(name, Dict, Name0) -> text_value(Name0, Name) ; Name = "" ),
|
|
2504
|
+
( get_dict(goal, Dict, Goal0) -> text_value(Goal0, Goal) ; Goal = "" ),
|
|
2505
|
+
( get_dict(inputs, Dict, In0) -> workflow_args(In0, Inputs)
|
|
2506
|
+
; get_dict(args, Dict, Args0) -> workflow_args(Args0, Inputs)
|
|
2507
|
+
; Inputs = []
|
|
2508
|
+
),
|
|
2509
|
+
( get_dict(outputs, Dict, Out0) -> workflow_args(Out0, Outputs) ; Outputs = [] ).
|
|
2510
|
+
|
|
2511
|
+
tool_from(Dict, Tool) :-
|
|
2512
|
+
is_dict(Dict),
|
|
2513
|
+
!,
|
|
2514
|
+
workflow_dict_parts(Dict, Name, Goal, Inputs, Outputs),
|
|
2515
|
+
( get_dict(description, Dict, Desc0) -> text_value(Desc0, Description) ; Description = "" ),
|
|
2516
|
+
Tool = shell_tool{name: Name, goal: Goal, inputs: Inputs, outputs: Outputs, description: Description}.
|
|
2517
|
+
|
|
2518
|
+
tools_term(Tools, ToolsOut) :-
|
|
2519
|
+
( is_list(Tools) -> maplist(tool_from, Tools, ToolsOut) ; ToolsOut = [] ).
|
|
2520
|
+
workflow_from(workflow(Name, Pred), Workflow) :- !, text_value(Name, N), workflow_from_parts(N, Pred, Workflow).
|
|
2521
|
+
workflow_from(workflow(Pred), Workflow) :- !, workflow_from_parts("", Pred, Workflow).
|
|
2522
|
+
workflow_from(predicate(Pred), Workflow) :- !, workflow_from_parts("", Pred, Workflow).
|
|
2523
|
+
|
|
2524
|
+
workflow_from_parts(Name, Pred, Workflow) :-
|
|
2525
|
+
( Pred = predicate(Items) -> true ; Items = Pred ),
|
|
2526
|
+
( is_list(Items)
|
|
2527
|
+
-> ( member(goal(Goal0), Items) -> text_value(Goal0, Goal) ; Goal = "" ),
|
|
2528
|
+
( member(inputs(Inputs0), Items) -> workflow_args(Inputs0, Inputs)
|
|
2529
|
+
; member(args(Args0), Items) -> workflow_args(Args0, Inputs)
|
|
2530
|
+
; Inputs = []
|
|
2531
|
+
),
|
|
2532
|
+
( member(outputs(Outputs0), Items) -> workflow_args(Outputs0, Outputs) ; Outputs = [] )
|
|
2533
|
+
; text_value(Pred, Goal), Inputs = [], Outputs = []
|
|
2534
|
+
),
|
|
2535
|
+
( Name == "" -> WorkflowName = Goal ; WorkflowName = Name ),
|
|
2536
|
+
Workflow = shell_workflow{name: WorkflowName, goal: Goal, inputs: Inputs, outputs: Outputs}.
|
|
2537
|
+
|
|
2538
|
+
workflow_args(Args, Normalized) :-
|
|
2539
|
+
( Args = args(Inner) -> true ; Inner = Args ),
|
|
2540
|
+
( is_list(Inner) -> maplist(workflow_arg, Inner, Normalized) ; Normalized = [] ).
|
|
2541
|
+
|
|
2542
|
+
workflow_arg(Type:Label, shell_arg{name: Name, type: TypeText}) :-
|
|
2543
|
+
!,
|
|
2544
|
+
text_value(Label, Name),
|
|
2545
|
+
text_value(Type, TypeText).
|
|
2546
|
+
workflow_arg(Dict, Arg) :-
|
|
2547
|
+
is_dict(Dict),
|
|
2548
|
+
!,
|
|
2549
|
+
( get_dict(name, Dict, N0) -> text_value(N0, Name) ; Name = "" ),
|
|
2550
|
+
( get_dict(type, Dict, T0) -> text_value(T0, TypeText) ; TypeText = "string" ),
|
|
2551
|
+
workflow_arg_metadata([description,required,default], Dict, shell_arg{name:Name,type:TypeText}, Arg).
|
|
2552
|
+
workflow_arg(arg(Name0, Type0), shell_arg{name: Name, type: TypeText}) :-
|
|
2553
|
+
!,
|
|
2554
|
+
text_value(Name0, Name),
|
|
2555
|
+
text_value(Type0, TypeText).
|
|
2556
|
+
workflow_arg(Value, shell_arg{name: Value, type: "string"}) :- atom(Value), !.
|
|
2557
|
+
workflow_arg(Value, shell_arg{name: Value, type: "string"}) :- string(Value), !.
|
|
2558
|
+
workflow_arg(_, shell_arg{name: "", type: "string"}).
|
|
2559
|
+
|
|
2560
|
+
workflow_arg_metadata([], _, Arg, Arg).
|
|
2561
|
+
workflow_arg_metadata([Key|Keys], Dict, In, Out) :-
|
|
2562
|
+
( get_dict(Key, Dict, Value) -> Next=In.put(Key,Value) ; Next=In ),
|
|
2563
|
+
workflow_arg_metadata(Keys, Dict, Next, Out).
|
|
2564
|
+
|
|
2565
|
+
text_value(Value, Text) :-
|
|
2566
|
+
( string(Value) -> Text = Value
|
|
2567
|
+
; atom(Value) -> atom_string(Value, Text)
|
|
2568
|
+
; number(Value) -> number_string(Value, Text)
|
|
2569
|
+
; Text = ""
|
|
2570
|
+
).
|
|
2571
|
+
|
|
2572
|
+
%% repl_display_harness(+SessionId, -Text)
|
|
2573
|
+
%% The same module as `export_harness/3`, but for humans: clauses come from the
|
|
2574
|
+
%% stored source view, so variable names, `{Var}` and `schema(...)` are the ones
|
|
2575
|
+
%% the author wrote. Export keeps its deterministic A/B numbering for hashing and
|
|
2576
|
+
%% byte-stable round-trips.
|
|
2577
|
+
repl_display_harness(SessionId, Text) :-
|
|
2578
|
+
with_output_to(string(Text), repl_display_output(SessionId)).
|
|
2579
|
+
|
|
2580
|
+
repl_display_output(SessionId) :-
|
|
2581
|
+
export_directives(SessionId),
|
|
2582
|
+
export_tools(SessionId),
|
|
2583
|
+
repl_display_clauses(SessionId).
|
|
2584
|
+
|
|
2585
|
+
repl_display_clauses(SessionId) :- repl_display_clauses(SessionId, []).
|
|
2586
|
+
repl_display_clauses(SessionId, Excluded) :-
|
|
2587
|
+
findall(Name/Arity,
|
|
2588
|
+
( current_predicate(SessionId:Name/Arity),
|
|
2589
|
+
export_user_predicate(SessionId, Name, Arity),
|
|
2590
|
+
\+ memberchk(Name/Arity, Excluded)
|
|
2591
|
+
),
|
|
2592
|
+
Preds0),
|
|
2593
|
+
sort(Preds0, Preds),
|
|
2594
|
+
forall(member(Name/Arity, Preds),
|
|
2595
|
+
( session_source_view(SessionId, Name/Arity, SourceView)
|
|
2596
|
+
-> format("~w~n", [SourceView])
|
|
2597
|
+
; functor(Head, Name, Arity),
|
|
2598
|
+
forall(clause(SessionId:Head, Body), export_clause(Head, Body))
|
|
2599
|
+
)).
|
|
2600
|
+
|
|
1690
2601
|
export_harness_output(SessionId, Header) :-
|
|
1691
2602
|
( Header == none
|
|
1692
2603
|
-> true
|
|
@@ -1949,6 +2860,8 @@ repl_assert(SessionId, ClauseText, Result) :-
|
|
|
1949
2860
|
repl_require_new_predicate(SessionId, PreparedTerm)),
|
|
1950
2861
|
forall(member(PreparedTerm, PreparedTerms),
|
|
1951
2862
|
repl_assert_clause(PreparedTerm, SessionId)),
|
|
2863
|
+
repl_source_views(SessionId, ClauseText, Views),
|
|
2864
|
+
repl_store_source_views(SessionId, Views, append),
|
|
1952
2865
|
Result = ok
|
|
1953
2866
|
),
|
|
1954
2867
|
AssertError,
|
|
@@ -1987,13 +2900,121 @@ repl_set_prolog_snippets(SessionId, Text) :-
|
|
|
1987
2900
|
retractall(session_prolog_snippets(SessionId, _)),
|
|
1988
2901
|
assertz(session_prolog_snippets(SessionId, Text)).
|
|
1989
2902
|
|
|
2903
|
+
%% Host-scoped workflow authoring: inspect before any edit/load. Only trusted
|
|
2904
|
+
%% standard-library operator imports run during parsing; never arbitrary directives.
|
|
2905
|
+
%% Partial blocks are allowed for define; complete files require one literal interface.
|
|
2906
|
+
repl_validate_workflow_source(SessionId, Code, Complete, Result) :-
|
|
2907
|
+
catch((setup_call_cleanup(open_string(Code,Stream),repl_workflow_read_terms(Stream,SessionId,Terms),close(Stream)),
|
|
2908
|
+
forall(member(Term,Terms),repl_workflow_owned_term(SessionId,Term)),
|
|
2909
|
+
( Complete==true -> findall(Entry,member(workflow(Entry),Terms),[Entry]),repl_workflow_literal(Entry),is_dict(Entry),
|
|
2910
|
+
dict_pairs(Entry,_,Pairs),forall(member(Key-_,Pairs),memberchk(Key,[name,goal,inputs,outputs,description])) ; true ),
|
|
2911
|
+
Result=ok),Error,format(atom(Result),'error(~w)',[Error])).
|
|
2912
|
+
|
|
2913
|
+
repl_workflow_read_terms(Stream,SessionId,Terms) :-
|
|
2914
|
+
read_term(Stream,Term,[module(SessionId)]),
|
|
2915
|
+
( Term==end_of_file -> Terms=[]
|
|
2916
|
+
; (nonvar(Term) -> true ; throw(error(instantiation_error,workflow))),
|
|
2917
|
+
( Term=(:- use_module(library(Library))),ground(Library),memberchk(Library,[clpfd,clpq,clpr,lists,apply,dcg/basics]) -> catch(SessionId:use_module(library(Library)),_,true) ; true ),
|
|
2918
|
+
Terms=[Term|Rest],repl_workflow_read_terms(Stream,SessionId,Rest) ).
|
|
2919
|
+
|
|
2920
|
+
repl_workflow_literal(Value) :-
|
|
2921
|
+
nonvar(Value),
|
|
2922
|
+
( is_dict(Value) -> dict_pairs(Value,_,Pairs),forall(member(_-Item,Pairs),repl_workflow_literal(Item))
|
|
2923
|
+
; is_list(Value) -> maplist(repl_workflow_literal,Value)
|
|
2924
|
+
; atomic(Value) ).
|
|
2925
|
+
|
|
2926
|
+
repl_workflow_owned_term(SessionId, (:- Directive)) :- !,
|
|
2927
|
+
( ground(Directive),Directive=use_module(library(Library)),memberchk(Library,[clpfd,clpq,clpr,lists,apply,dcg/basics]) -> true
|
|
2928
|
+
; ground(Directive),Directive=use_harness(Name,Imports),session_harness_module(SessionId,Name,_,_,_),is_list(Imports) -> true
|
|
2929
|
+
; ground(Directive),Directive=use_harness(Name,version(Version),Imports),normalize_harness_version(Version,V),session_harness_module(SessionId,Name,V,_,_),is_list(Imports) -> true
|
|
2930
|
+
; throw(error(permission_error(modify,workflow_dependency,Directive),workflow)) ).
|
|
2931
|
+
repl_workflow_owned_term(SessionId, (Head --> Body)) :- !,repl_workflow_owned_term(SessionId,(Head :- Body)).
|
|
2932
|
+
repl_workflow_owned_term(SessionId, '$dc_tool'(Term,_)) :- !,repl_workflow_owned_term(SessionId,Term).
|
|
2933
|
+
repl_workflow_owned_term(SessionId, (tool(Head,_) :- Body)) :- !,repl_workflow_owned_term(SessionId,(Head :- Body)).
|
|
2934
|
+
repl_workflow_owned_term(SessionId, (tool(Head,_,_) :- Body)) :- !,repl_workflow_owned_term(SessionId,(Head :- Body)).
|
|
2935
|
+
repl_workflow_owned_term(SessionId, Term) :-
|
|
2936
|
+
repl_head_of(Term,Head),
|
|
2937
|
+
( Head=workflow(Entry) -> (Term=workflow(Entry),repl_workflow_literal(Entry),is_dict(Entry) -> true ; throw(error(domain_error(workflow_interface,Term),workflow)))
|
|
2938
|
+
; callable(Head),Head\=(_: _),functor(Head,Name,_),Name\==shell,Name\==agent_main,Name\==term_expansion,Name\==goal_expansion,\+sub_atom(Name,0,3,_,bp_),\+sub_atom(Name,0,3,_,dc_),repl_require_committable(SessionId,Term) -> true
|
|
2939
|
+
; throw(error(permission_error(modify,workflow_owned_source,Head),workflow)) ).
|
|
2940
|
+
|
|
1990
2941
|
%% repl_parse_clauses(+SessionId, +ClausesText, -Terms)
|
|
1991
2942
|
%% Parse and preprocess a block of DML clauses without asserting them.
|
|
1992
2943
|
repl_parse_clauses(SessionId, ClausesText, Terms) :-
|
|
2944
|
+
repl_parse_clauses(SessionId, ClausesText, Terms, _Views).
|
|
2945
|
+
|
|
2946
|
+
%% repl_parse_clauses/4 also returns the source view: the block as written, with
|
|
2947
|
+
%% the author's variable names, before task/N rewriting. Asserting a clause loses
|
|
2948
|
+
%% those names (listing used to show A, B, C), so the view is what listing prints.
|
|
2949
|
+
repl_parse_clauses(SessionId, ClausesText, Terms, Views) :-
|
|
1993
2950
|
ensure_full_stop(ClausesText, Text1),
|
|
1994
2951
|
open_string(Text1, Stream),
|
|
1995
2952
|
repl_read_prepared_terms(Stream, SessionId, Terms),
|
|
1996
|
-
close(Stream)
|
|
2953
|
+
close(Stream),
|
|
2954
|
+
repl_source_views(SessionId, ClausesText, Views).
|
|
2955
|
+
|
|
2956
|
+
%% repl_source_views(+Text, -Views): Indicator-Source pairs, one per clause.
|
|
2957
|
+
%% A source view is best-effort: it only improves `listing`. Read the text as it
|
|
2958
|
+
%% is first — a comment-only harness (the export header) and an empty string are
|
|
2959
|
+
%% both valid, and `ensure_full_stop` would append a lone `.` after the comment,
|
|
2960
|
+
%% which the reader rejects with `end_of_clause`. Only when the final clause
|
|
2961
|
+
%% lacks a full stop does a retry with one make sense.
|
|
2962
|
+
repl_source_views(SessionId, Text, Views) :-
|
|
2963
|
+
( catch(repl_source_views_raw(SessionId, Text, Views0), _, fail)
|
|
2964
|
+
-> Views = Views0
|
|
2965
|
+
; catch(( ensure_full_stop(Text, Text1),
|
|
2966
|
+
repl_source_views_raw(SessionId, Text1, Views)
|
|
2967
|
+
), _, Views = [])
|
|
2968
|
+
).
|
|
2969
|
+
|
|
2970
|
+
repl_source_views_raw(SessionId, Text, Views) :-
|
|
2971
|
+
open_string(Text, Stream),
|
|
2972
|
+
call_cleanup(repl_source_views_loop(Stream, SessionId, Views), close(Stream)).
|
|
2973
|
+
|
|
2974
|
+
repl_source_views_loop(Stream, SessionId, Views) :-
|
|
2975
|
+
read_term(Stream, Term, [module(SessionId), variable_names(Bindings)]),
|
|
2976
|
+
( Term == end_of_file
|
|
2977
|
+
-> Views = []
|
|
2978
|
+
; ( repl_term_indicator(Term, Indicator)
|
|
2979
|
+
-> with_output_to(string(Body),
|
|
2980
|
+
write_term(Term, [variable_names(Bindings), quoted(true)])),
|
|
2981
|
+
format(string(Source), "~w.~n", [Body]),
|
|
2982
|
+
Views = [Indicator-Source|Rest]
|
|
2983
|
+
; Views = Rest
|
|
2984
|
+
),
|
|
2985
|
+
repl_source_views_loop(Stream, SessionId, Rest)
|
|
2986
|
+
).
|
|
2987
|
+
|
|
2988
|
+
%% repl_store_source_views(+SessionId, +Views, +Mode)
|
|
2989
|
+
%% Mode is `replace` (define) or `append` (assert).
|
|
2990
|
+
repl_store_source_views(SessionId, Views, Mode) :-
|
|
2991
|
+
keysort(Views, Sorted),
|
|
2992
|
+
repl_store_view_groups(SessionId, Sorted, Mode).
|
|
2993
|
+
|
|
2994
|
+
repl_store_view_groups(_, [], _).
|
|
2995
|
+
repl_store_view_groups(SessionId, [Indicator-Source|Rest], Mode) :-
|
|
2996
|
+
% keysort/2 is stable: retain that order within each predicate. These
|
|
2997
|
+
% views are reloaded as executable snapshots, so rotating even one clause
|
|
2998
|
+
% changes cuts, fallback handlers, and coordinator lookup-before-spawn.
|
|
2999
|
+
repl_same_indicator(Indicator, Rest, Sources, Remaining),
|
|
3000
|
+
atomic_list_concat([Source|Sources], '', NewText),
|
|
3001
|
+
( Mode == append,
|
|
3002
|
+
session_source_view(SessionId, Indicator, Existing)
|
|
3003
|
+
-> atomic_list_concat([Existing, NewText], '', Text)
|
|
3004
|
+
; Text = NewText
|
|
3005
|
+
),
|
|
3006
|
+
retractall(session_source_view(SessionId, Indicator, _)),
|
|
3007
|
+
assertz(session_source_view(SessionId, Indicator, Text)),
|
|
3008
|
+
repl_store_view_groups(SessionId, Remaining, Mode).
|
|
3009
|
+
|
|
3010
|
+
repl_same_indicator(Indicator, [Indicator-Source|Rest], [Source|Same], Remaining) :-
|
|
3011
|
+
!,
|
|
3012
|
+
repl_same_indicator(Indicator, Rest, Same, Remaining).
|
|
3013
|
+
repl_same_indicator(_, Rest, [], Rest).
|
|
3014
|
+
|
|
3015
|
+
%% repl_drop_source_view(+SessionId, +Indicator)
|
|
3016
|
+
repl_drop_source_view(SessionId, Indicator) :-
|
|
3017
|
+
retractall(session_source_view(SessionId, Indicator, _)).
|
|
1997
3018
|
|
|
1998
3019
|
repl_read_prepared_terms(Stream, SessionId, Terms) :-
|
|
1999
3020
|
read_term(Stream, Term, [module(SessionId), variable_names(Bindings)]),
|
|
@@ -2042,9 +3063,21 @@ repl_commit_terms(SessionId, Terms) :-
|
|
|
2042
3063
|
( repl_protected_clause(PreparedTerm)
|
|
2043
3064
|
-> throw(error(permission_error(modify, reserved_name, PreparedTerm),
|
|
2044
3065
|
context(repl_assert_many/3, 'cannot define a reserved predicate')))
|
|
3066
|
+
; repl_replace_annotation(SessionId, PreparedTerm)
|
|
3067
|
+
-> process_clause(PreparedTerm, SessionId)
|
|
2045
3068
|
; process_clause(PreparedTerm, SessionId)
|
|
2046
3069
|
)).
|
|
2047
3070
|
|
|
3071
|
+
%% repl_replace_annotation(+SessionId, +Term)
|
|
3072
|
+
%% Remove the existing entry with the same key before asserting the new one.
|
|
3073
|
+
repl_replace_annotation(SessionId, Term) :-
|
|
3074
|
+
repl_annotation_key(Term, AnnotationName, Key),
|
|
3075
|
+
current_predicate(SessionId:AnnotationName/2),
|
|
3076
|
+
functor(Template, AnnotationName, 2),
|
|
3077
|
+
arg(1, Template, Key),
|
|
3078
|
+
retractall(SessionId:Template),
|
|
3079
|
+
!.
|
|
3080
|
+
|
|
2048
3081
|
%% repl_assert_many(+SessionId, +ClausesText, -Result)
|
|
2049
3082
|
%% Parses a block of clauses and asserts them in the order written. Prefer this
|
|
2050
3083
|
%% for defining a predicate: clause order matters for backtracking, so asserting
|
|
@@ -2052,12 +3085,13 @@ repl_commit_terms(SessionId, Terms) :-
|
|
|
2052
3085
|
repl_assert_many(SessionId, ClausesText, Result) :-
|
|
2053
3086
|
catch(
|
|
2054
3087
|
( guard_not_frozen(SessionId),
|
|
2055
|
-
repl_parse_clauses(SessionId, ClausesText, Terms),
|
|
3088
|
+
repl_parse_clauses(SessionId, ClausesText, Terms, SourceViews),
|
|
2056
3089
|
% assert appends; against an existing predicate that leaves both
|
|
2057
3090
|
% versions live with their clauses interleaved.
|
|
2058
3091
|
forall(member(Term, Terms), repl_require_new_predicate(SessionId, Term)),
|
|
2059
3092
|
repl_commit_terms(SessionId, Terms),
|
|
2060
3093
|
repl_note_touched(SessionId, Terms),
|
|
3094
|
+
repl_store_source_views(SessionId, SourceViews, append),
|
|
2061
3095
|
Result = ok
|
|
2062
3096
|
),
|
|
2063
3097
|
AssertError,
|
|
@@ -2068,6 +3102,7 @@ repl_assert_many(SessionId, ClausesText, Result) :-
|
|
|
2068
3102
|
%% The plain predicate a parsed clause defines. Directives and tool/2 wrappers
|
|
2069
3103
|
%% have no plain indicator here (process_clause owns them).
|
|
2070
3104
|
repl_term_indicator((:- _), _) :- !, fail.
|
|
3105
|
+
repl_term_indicator('$dc_tool'(_, _), _) :- !, fail.
|
|
2071
3106
|
repl_term_indicator((tool(_, _) :- _), _) :- !, fail.
|
|
2072
3107
|
repl_term_indicator((tool(_, _, _) :- _), _) :- !, fail.
|
|
2073
3108
|
repl_term_indicator((tool(_) :- _), _) :- !, fail.
|
|
@@ -2093,80 +3128,129 @@ repl_note_indicator(SessionId, Name/Arity) :-
|
|
|
2093
3128
|
%% that mixes the old and the new version — both stay live, clause order
|
|
2094
3129
|
%% interleaves them, and nothing says so. Refuse instead, and name the remedy.
|
|
2095
3130
|
repl_require_new_predicate(SessionId, Term) :-
|
|
2096
|
-
|
|
2097
|
-
|
|
3131
|
+
% Annotation facts (`doc/2`, `schema/2`, `script_file/2`) are keyed records,
|
|
3132
|
+
% not appendable clauses: a new key is fine, an existing key must be replaced.
|
|
3133
|
+
( repl_annotation_key(Term, AnnotationName, Key)
|
|
3134
|
+
-> ( functor(AnnotationTemplate, AnnotationName, 2),
|
|
3135
|
+
clause(SessionId:AnnotationTemplate, Body),
|
|
3136
|
+
repl_annotation_clause_key(AnnotationName, Body, Key)
|
|
3137
|
+
-> format(atom(AnnotationMessage),
|
|
3138
|
+
'~w(~w, …) already exists. Use define("~w", "…") to replace that entry (other entries are kept).',
|
|
3139
|
+
[AnnotationName, Key, AnnotationName]),
|
|
3140
|
+
throw(error(permission_error(modify, existing_annotation, AnnotationName),
|
|
3141
|
+
context(repl_require_new_predicate/2, AnnotationMessage)))
|
|
3142
|
+
; true
|
|
3143
|
+
)
|
|
3144
|
+
; repl_term_indicator(Term, Name/Arity),
|
|
3145
|
+
findall(x,
|
|
2098
3146
|
( functor(Head, Name, Arity),
|
|
2099
3147
|
clause(SessionId:Head, _)
|
|
2100
3148
|
),
|
|
2101
3149
|
Existing),
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
3150
|
+
Existing \== [],
|
|
3151
|
+
!,
|
|
3152
|
+
length(Existing, Count),
|
|
3153
|
+
format(atom(Message),
|
|
3154
|
+
'~w/~w already has ~w clause(s). assert appends, so the old and the new clauses would both be present and clause order would interleave them. Use define("~w/~w", "...") to replace the predicate, or delete("~w/~w") first.',
|
|
3155
|
+
[Name, Arity, Count, Name, Arity, Name, Arity]),
|
|
3156
|
+
throw(error(permission_error(modify, existing_predicate, Name/Arity),
|
|
3157
|
+
context(repl_require_new_predicate/2, Message)))
|
|
3158
|
+
).
|
|
2110
3159
|
repl_require_new_predicate(_SessionId, _Term).
|
|
2111
3160
|
|
|
3161
|
+
%% repl_annotation_key(+Term, -Name, -Key)
|
|
3162
|
+
%% The annotation predicates whose entries are keyed: one per documented
|
|
3163
|
+
%% predicate, schema or linked script. Defining one entry must not delete the
|
|
3164
|
+
%% others, which is exactly what "replace the whole predicate" used to do.
|
|
3165
|
+
repl_annotation_key(doc(Key, _), doc, Key) :- !.
|
|
3166
|
+
repl_annotation_key(schema(Key, _), schema, Key) :- !.
|
|
3167
|
+
repl_annotation_key(script_file(Key, _), script_file, Key).
|
|
3168
|
+
repl_annotation_key((Head :- _), Name, Key) :- !, repl_annotation_key(Head, Name, Key).
|
|
3169
|
+
|
|
3170
|
+
repl_annotation_clause_key(doc, doc(Key, _), Key) :- !.
|
|
3171
|
+
repl_annotation_clause_key(schema, schema(Key, _), Key) :- !.
|
|
3172
|
+
repl_annotation_clause_key(script_file, script_file(Key, _), Key).
|
|
3173
|
+
|
|
3174
|
+
repl_annotation_predicate(doc, 2).
|
|
3175
|
+
repl_annotation_predicate(schema, 2).
|
|
3176
|
+
repl_annotation_predicate(script_file, 2).
|
|
3177
|
+
|
|
2112
3178
|
%% repl_forget_tool(+SessionId, +Name)
|
|
2113
|
-
%% Drop the tool annotation that belonged to a predicate being removed
|
|
3179
|
+
%% Drop the tool annotation that belonged to a predicate being removed, and the
|
|
3180
|
+
%% `tool/1` implementation clause that carried it. Leaving the implementation
|
|
3181
|
+
%% behind meant a later `task/N` still offered the old body while the export
|
|
3182
|
+
%% carried the new one, so the live session and the exported harness disagreed.
|
|
2114
3183
|
repl_forget_tool(SessionId, Name) :-
|
|
2115
3184
|
( session_user_tools(SessionId, Name)
|
|
2116
3185
|
-> retractall(session_user_tools(SessionId, Name)),
|
|
2117
3186
|
retractall(session_user_tool_schema(SessionId, Name, _)),
|
|
3187
|
+
retractall(session_user_tool_arity(SessionId, Name, _)),
|
|
3188
|
+
retractall(session_user_tool_head(SessionId, Name, _)),
|
|
3189
|
+
retractall(session_user_tool_desc(SessionId, Name, _)),
|
|
3190
|
+
retractall(session_user_tool_names(SessionId, Name, _)),
|
|
3191
|
+
retractall(session_user_tool_types(SessionId, Name, _)),
|
|
3192
|
+
retractall(session_user_tool_body(SessionId, Name, _)),
|
|
2118
3193
|
catch(retractall(SessionId:tool_source(Name, _)), _, true)
|
|
2119
3194
|
; true
|
|
2120
|
-
)
|
|
3195
|
+
),
|
|
3196
|
+
repl_forget_tool_clauses(SessionId, Name).
|
|
3197
|
+
|
|
3198
|
+
%% repl_forget_tool_clauses(+SessionId, +Name)
|
|
3199
|
+
%% Every `tool(Head) :- Body` whose head is this tool. The clause lives in the
|
|
3200
|
+
%% module's `tool/1` predicate, so it cannot be abolished by name/arity.
|
|
3201
|
+
repl_forget_tool_clauses(SessionId, Name) :-
|
|
3202
|
+
forall(( clause(SessionId:tool(Head), Body), compound(Head), functor(Head, Name, _) ),
|
|
3203
|
+
( Body == true
|
|
3204
|
+
-> retract(SessionId:tool(Head))
|
|
3205
|
+
; retract(SessionId:(tool(Head) :- Body))
|
|
3206
|
+
)).
|
|
2121
3207
|
|
|
2122
3208
|
%% repl_capture_predicates(+SessionId, +Indicators, -Captured)
|
|
2123
3209
|
%% Copy the clauses and tool bookkeeping a replacement is about to destroy, so
|
|
2124
3210
|
%% a failure part-way through can put them back.
|
|
2125
3211
|
repl_capture_predicates(SessionId, Indicators, Captured) :-
|
|
2126
|
-
findall(indicator(Name, Arity, Clauses,
|
|
3212
|
+
findall(indicator(Name, Arity, Clauses, ToolInfo),
|
|
2127
3213
|
( member(Name/Arity, Indicators),
|
|
2128
|
-
findall(Stored,
|
|
2129
|
-
|
|
2130
|
-
clause(SessionId:Head, Body),
|
|
2131
|
-
( Body == true
|
|
2132
|
-
-> Stored = Head
|
|
2133
|
-
; Stored = (Head :- Body)
|
|
2134
|
-
)
|
|
2135
|
-
),
|
|
2136
|
-
Clauses),
|
|
2137
|
-
( session_user_tools(SessionId, Name)
|
|
2138
|
-
-> IsTool = true
|
|
2139
|
-
; IsTool = false
|
|
2140
|
-
),
|
|
2141
|
-
( session_user_tool_schema(SessionId, Name, Found)
|
|
2142
|
-
-> Schema = Found
|
|
2143
|
-
; Schema = none
|
|
2144
|
-
),
|
|
2145
|
-
( current_predicate(SessionId:tool_source/2),
|
|
2146
|
-
SessionId:tool_source(Name, FoundSource)
|
|
2147
|
-
-> Source = FoundSource
|
|
2148
|
-
; Source = none
|
|
2149
|
-
)
|
|
3214
|
+
findall(Stored, capture_clause(SessionId, Name, Arity, Stored), Clauses),
|
|
3215
|
+
capture_tool_info(SessionId, Name, ToolInfo)
|
|
2150
3216
|
),
|
|
2151
3217
|
Captured).
|
|
2152
3218
|
|
|
3219
|
+
capture_clause(SessionId, Name, Arity, Stored) :-
|
|
3220
|
+
functor(Head, Name, Arity),
|
|
3221
|
+
clause(SessionId:Head, Body),
|
|
3222
|
+
( Body == true
|
|
3223
|
+
-> Stored = Head
|
|
3224
|
+
; Stored = (Head :- Body)
|
|
3225
|
+
).
|
|
3226
|
+
|
|
3227
|
+
capture_tool_info(SessionId, Name, ToolInfo) :-
|
|
3228
|
+
( session_user_tools(SessionId, Name),
|
|
3229
|
+
\+ session_builtin_tool(SessionId, Name),
|
|
3230
|
+
% Head and Body must come from the *same* clause: reading the stored head
|
|
3231
|
+
% and body separately gives two copies with unrelated variables, and the
|
|
3232
|
+
% restored tool then delegates to an unbound goal.
|
|
3233
|
+
clause(SessionId:tool(Head), Body),
|
|
3234
|
+
compound(Head),
|
|
3235
|
+
functor(Head, Name, _),
|
|
3236
|
+
session_user_tool_desc(SessionId, Name, Desc)
|
|
3237
|
+
-> ( session_user_tool_names(SessionId, Name, Names) -> true ; Names = [] ),
|
|
3238
|
+
( session_user_tool_types(SessionId, Name, Types) -> true ; Types = none ),
|
|
3239
|
+
ToolInfo = tool(true, tool_meta(Head, Desc, Names, Types, Body))
|
|
3240
|
+
; ToolInfo = none
|
|
3241
|
+
).
|
|
3242
|
+
|
|
2153
3243
|
%% repl_restore_captured(+SessionId, +Captured)
|
|
2154
3244
|
repl_restore_captured(SessionId, Captured) :-
|
|
2155
|
-
forall(member(indicator(Name, Arity, Clauses,
|
|
2156
|
-
(
|
|
3245
|
+
forall(member(indicator(Name, Arity, Clauses, ToolInfo), Captured),
|
|
3246
|
+
( % Only the arity this transaction touched: other arities of the same
|
|
3247
|
+
% name were never part of it.
|
|
3248
|
+
catch(abolish(SessionId:Name/Arity), _, true),
|
|
2157
3249
|
repl_forget_tool(SessionId, Name),
|
|
2158
3250
|
forall(member(Clause, Clauses), assertz(SessionId:Clause)),
|
|
2159
|
-
(
|
|
2160
|
-
->
|
|
3251
|
+
( ToolInfo = tool(true, tool_meta(Head, Desc, Names, Types, Body))
|
|
3252
|
+
-> catch(repl_register_tool(SessionId, Head, Desc, Types, Body, Names), _, true)
|
|
2161
3253
|
; true
|
|
2162
|
-
),
|
|
2163
|
-
( Schema == none
|
|
2164
|
-
-> true
|
|
2165
|
-
; assertz(session_user_tool_schema(SessionId, Name, Schema))
|
|
2166
|
-
),
|
|
2167
|
-
( Source == none
|
|
2168
|
-
-> true
|
|
2169
|
-
; assertz(SessionId:tool_source(Name, Source))
|
|
2170
3254
|
)
|
|
2171
3255
|
)).
|
|
2172
3256
|
|
|
@@ -2174,7 +3258,12 @@ repl_restore_captured(SessionId, Captured) :-
|
|
|
2174
3258
|
%% The destructive half of define: abolish the targets, then assert the block.
|
|
2175
3259
|
repl_replace_predicates(SessionId, Indicators, Terms) :-
|
|
2176
3260
|
forall(member(Name/Arity, Indicators),
|
|
2177
|
-
(
|
|
3261
|
+
( % Annotation predicates merge per key (repl_commit_terms replaces the
|
|
3262
|
+
% entry the block defines); abolishing them deleted every other
|
|
3263
|
+
% predicate's doc/schema/script link.
|
|
3264
|
+
repl_annotation_predicate(Name, Arity)
|
|
3265
|
+
-> true
|
|
3266
|
+
; catch(abolish(SessionId:Name/Arity), _, true),
|
|
2178
3267
|
repl_forget_tool(SessionId, Name)
|
|
2179
3268
|
)),
|
|
2180
3269
|
repl_commit_terms(SessionId, Terms).
|
|
@@ -2220,58 +3309,110 @@ repl_retract(SessionId, ClauseText, Result) :-
|
|
|
2220
3309
|
catch(
|
|
2221
3310
|
( guard_not_frozen(SessionId),
|
|
2222
3311
|
repl_read_clause(SessionId, ClauseText, Pattern),
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
; true
|
|
2229
|
-
),
|
|
2230
|
-
findall(x, retract(SessionId:Pattern), Removed),
|
|
2231
|
-
length(Removed, RemovedCount),
|
|
2232
|
-
( RemovedCount =:= 0
|
|
2233
|
-
-> format(atom(Message),
|
|
2234
|
-
'no clause of ~w/~w matched ~w, so nothing was removed',
|
|
2235
|
-
[Name, Arity, Clause]),
|
|
2236
|
-
throw(error(existence_error(clause, Clause),
|
|
2237
|
-
context(repl_retract/3, Message)))
|
|
2238
|
-
; true
|
|
2239
|
-
),
|
|
2240
|
-
% Only drop the tool annotation when the predicate is really gone;
|
|
2241
|
-
% other clauses of it may still be live.
|
|
2242
|
-
( functor(Head, Name, Arity),
|
|
2243
|
-
clause(SessionId:Head, _)
|
|
2244
|
-
-> true
|
|
2245
|
-
; repl_forget_tool(SessionId, Name)
|
|
2246
|
-
),
|
|
2247
|
-
repl_note_indicator(SessionId, Name/Arity),
|
|
2248
|
-
( RemovedCount =:= 1
|
|
2249
|
-
-> Suffix = ''
|
|
2250
|
-
; Suffix = 's'
|
|
2251
|
-
),
|
|
2252
|
-
format(atom(Result), 'ok(removed ~w clause~w)', [RemovedCount, Suffix])
|
|
3312
|
+
( repl_tool_pattern_name(Pattern, ToolName)
|
|
3313
|
+
-> repl_forget_tool(SessionId, ToolName),
|
|
3314
|
+
format(atom(Result), 'ok(removed tool ~w)', [ToolName])
|
|
3315
|
+
; repl_retract_predicate_clause(SessionId, Pattern, Result)
|
|
3316
|
+
)
|
|
2253
3317
|
),
|
|
2254
3318
|
RetractError,
|
|
2255
3319
|
format(atom(Result), 'error(~w)', [RetractError])
|
|
2256
3320
|
).
|
|
2257
3321
|
|
|
3322
|
+
%% A `tool(...)` pattern names an annotation, not a module clause. The
|
|
3323
|
+
%% implementation lives in `tool/1` and the metadata outside the module, so the
|
|
3324
|
+
%% generic retract found nothing and reported "no clause of tool/2 matched" —
|
|
3325
|
+
%% while the tool stayed registered and the agent had no way to remove it.
|
|
3326
|
+
repl_tool_pattern_name(Pattern, Name) :-
|
|
3327
|
+
repl_tool_pattern_head(Pattern, Head),
|
|
3328
|
+
compound(Head),
|
|
3329
|
+
functor(Head, Name, _),
|
|
3330
|
+
\+ Name == tool_source.
|
|
3331
|
+
|
|
3332
|
+
repl_tool_pattern_head((tool(Head) :- _), Head) :- !.
|
|
3333
|
+
repl_tool_pattern_head((tool(Head, _) :- _), Head) :- !.
|
|
3334
|
+
repl_tool_pattern_head((tool(Head, _, _) :- _), Head) :- !.
|
|
3335
|
+
repl_tool_pattern_head(tool(Head), Head) :- !.
|
|
3336
|
+
repl_tool_pattern_head(tool(Head, _), Head) :- !.
|
|
3337
|
+
repl_tool_pattern_head(tool(Head, _, _), Head).
|
|
3338
|
+
|
|
3339
|
+
repl_retract_predicate_clause(SessionId, Pattern, Result) :-
|
|
3340
|
+
repl_head_of(Pattern, Clause),
|
|
3341
|
+
repl_callable_indicator(Clause, Name, Arity),
|
|
3342
|
+
( session_harness_import(SessionId, Name/Arity, TargetModule, _)
|
|
3343
|
+
-> format(atom(ImportMessage),
|
|
3344
|
+
'~w/~w is imported read-only from ~w and cannot be retracted here.',
|
|
3345
|
+
[Name, Arity, TargetModule]),
|
|
3346
|
+
throw(error(permission_error(modify, imported_predicate, Name/Arity),
|
|
3347
|
+
context(repl_retract/3, ImportMessage)))
|
|
3348
|
+
; true
|
|
3349
|
+
),
|
|
3350
|
+
( repl_protected_clause(Clause)
|
|
3351
|
+
-> throw(error(permission_error(modify, reserved_name, Clause),
|
|
3352
|
+
context(repl_retract/3, 'cannot retract a reserved predicate')))
|
|
3353
|
+
; true
|
|
3354
|
+
),
|
|
3355
|
+
findall(x, retract(SessionId:Pattern), Removed),
|
|
3356
|
+
length(Removed, RemovedCount),
|
|
3357
|
+
( RemovedCount =:= 0
|
|
3358
|
+
-> format(atom(Message),
|
|
3359
|
+
'no clause of ~w/~w matched ~w, so nothing was removed',
|
|
3360
|
+
[Name, Arity, Clause]),
|
|
3361
|
+
throw(error(existence_error(clause, Clause),
|
|
3362
|
+
context(repl_retract/3, Message)))
|
|
3363
|
+
; true
|
|
3364
|
+
),
|
|
3365
|
+
% Only drop the tool annotation when the predicate is really gone;
|
|
3366
|
+
% other clauses of it may still be live.
|
|
3367
|
+
( functor(Head, Name, Arity),
|
|
3368
|
+
clause(SessionId:Head, _)
|
|
3369
|
+
-> true
|
|
3370
|
+
; repl_forget_tool(SessionId, Name)
|
|
3371
|
+
),
|
|
3372
|
+
repl_note_indicator(SessionId, Name/Arity),
|
|
3373
|
+
( RemovedCount =:= 1
|
|
3374
|
+
-> Suffix = ''
|
|
3375
|
+
; Suffix = 's'
|
|
3376
|
+
),
|
|
3377
|
+
format(atom(Result), 'ok(removed ~w clause~w)', [RemovedCount, Suffix]).
|
|
3378
|
+
|
|
2258
3379
|
%% repl_delete_predicate(+SessionId, +Name, +Arity, -Result)
|
|
2259
3380
|
%% Fully remove a predicate. Unlike retract (which only removes matching
|
|
2260
3381
|
%% clauses and can leave an empty predicate behind), this abolishes it.
|
|
2261
3382
|
repl_delete_predicate(SessionId, Name, Arity, Result) :-
|
|
2262
3383
|
catch(
|
|
2263
|
-
(
|
|
3384
|
+
( mi_clear_cut_classes(SessionId),
|
|
3385
|
+
guard_not_frozen(SessionId),
|
|
2264
3386
|
atom(Name), integer(Arity), Arity >= 0,
|
|
3387
|
+
( session_harness_import(SessionId, Name/Arity, TargetModule, _)
|
|
3388
|
+
-> format(atom(ImportMessage),
|
|
3389
|
+
'~w/~w is imported read-only from ~w and cannot be deleted here.',
|
|
3390
|
+
[Name, Arity, TargetModule]),
|
|
3391
|
+
throw(error(permission_error(modify, imported_predicate, Name/Arity),
|
|
3392
|
+
context(repl_delete_predicate/4, ImportMessage)))
|
|
3393
|
+
; true
|
|
3394
|
+
),
|
|
2265
3395
|
( repl_protected_name(Name)
|
|
2266
3396
|
-> throw(error(permission_error(modify, reserved_name, Name/Arity),
|
|
2267
3397
|
context(repl_delete_predicate/4, 'cannot delete a reserved predicate')))
|
|
2268
3398
|
; true
|
|
2269
3399
|
),
|
|
3400
|
+
( repl_annotation_predicate(Name, Arity)
|
|
3401
|
+
-> throw(error(permission_error(modify, annotation_predicate, Name/Arity),
|
|
3402
|
+
context(repl_delete_predicate/4,
|
|
3403
|
+
'doc/2, schema/2 and script_file/2 are keyed annotations, not skills. Remove one entry with retract("doc(name/arity, _)") / retract("schema(name, _)") / retract("script_file(name, _)"), or replace it with define.')))
|
|
3404
|
+
; true
|
|
3405
|
+
),
|
|
3406
|
+
( Name == tool
|
|
3407
|
+
-> throw(error(domain_error(delete_target, tool/Arity),
|
|
3408
|
+
context(repl_delete_predicate/4,
|
|
3409
|
+
'tool/2 and tool/1 are the annotation mechanism, not a predicate to delete. To remove one tool use delete("tool:Name/Arity") or retract("tool(Head, _)").')))
|
|
3410
|
+
; true
|
|
3411
|
+
),
|
|
2270
3412
|
catch(abolish(SessionId:Name/Arity), _, true),
|
|
3413
|
+
repl_drop_source_view(SessionId, Name/Arity),
|
|
2271
3414
|
( session_user_tools(SessionId, Name)
|
|
2272
|
-
->
|
|
2273
|
-
retractall(session_user_tool_schema(SessionId, Name, _)),
|
|
2274
|
-
retractall(SessionId:tool_source(Name, _))
|
|
3415
|
+
-> repl_forget_tool(SessionId, Name)
|
|
2275
3416
|
; true
|
|
2276
3417
|
),
|
|
2277
3418
|
( session_edit_touched(SessionId, Name/Arity)
|
|
@@ -2284,6 +3425,76 @@ repl_delete_predicate(SessionId, Name, Arity, Result) :-
|
|
|
2284
3425
|
format(atom(Result), 'error(~w)', [DeleteError])
|
|
2285
3426
|
).
|
|
2286
3427
|
|
|
3428
|
+
%% repl_delete_doc(+SessionId, +Name, +Arity, -Result)
|
|
3429
|
+
%% Remove one keyed doc/2 entry, leaving every other doc entry alone.
|
|
3430
|
+
repl_delete_doc(SessionId, Name, Arity, Result) :-
|
|
3431
|
+
catch(
|
|
3432
|
+
( guard_not_frozen(SessionId),
|
|
3433
|
+
integer(Arity),
|
|
3434
|
+
Key = Name/Arity,
|
|
3435
|
+
repl_delete_annotation_entry(SessionId, doc, Key, Result)
|
|
3436
|
+
),
|
|
3437
|
+
DeleteError,
|
|
3438
|
+
format(atom(Result), 'error(~w)', [DeleteError])
|
|
3439
|
+
).
|
|
3440
|
+
|
|
3441
|
+
%% repl_delete_schema(+SessionId, +Key, -Result)
|
|
3442
|
+
%% Remove one keyed schema/2 entry.
|
|
3443
|
+
repl_delete_schema(SessionId, Key, Result) :-
|
|
3444
|
+
catch(
|
|
3445
|
+
( guard_not_frozen(SessionId),
|
|
3446
|
+
atom(Key),
|
|
3447
|
+
repl_delete_annotation_entry(SessionId, schema, Key, Result)
|
|
3448
|
+
),
|
|
3449
|
+
DeleteError,
|
|
3450
|
+
format(atom(Result), 'error(~w)', [DeleteError])
|
|
3451
|
+
).
|
|
3452
|
+
|
|
3453
|
+
%% repl_delete_script(+SessionId, +Key, -Result)
|
|
3454
|
+
%% Remove one keyed script_file/2 link.
|
|
3455
|
+
repl_delete_script(SessionId, Key, Result) :-
|
|
3456
|
+
catch(
|
|
3457
|
+
( guard_not_frozen(SessionId),
|
|
3458
|
+
atom(Key),
|
|
3459
|
+
repl_delete_annotation_entry(SessionId, script_file, Key, Result)
|
|
3460
|
+
),
|
|
3461
|
+
DeleteError,
|
|
3462
|
+
format(atom(Result), 'error(~w)', [DeleteError])
|
|
3463
|
+
).
|
|
3464
|
+
|
|
3465
|
+
repl_delete_annotation_entry(SessionId, Kind, Key, Result) :-
|
|
3466
|
+
functor(Template, Kind, 2),
|
|
3467
|
+
arg(1, Template, Key),
|
|
3468
|
+
( current_predicate(SessionId:Kind/2),
|
|
3469
|
+
clause(SessionId:Template, _)
|
|
3470
|
+
-> retractall(SessionId:Template),
|
|
3471
|
+
format(atom(Result), 'ok(removed ~w(~w))', [Kind, Key])
|
|
3472
|
+
; format(atom(Annotation), '~w(~w)', [Kind, Key]),
|
|
3473
|
+
throw(error(existence_error(annotation, Annotation),
|
|
3474
|
+
context(repl_delete_annotation_entry/4,
|
|
3475
|
+
'no such keyed annotation entry; check the key exactly')))
|
|
3476
|
+
).
|
|
3477
|
+
|
|
3478
|
+
%% repl_delete_tool(+SessionId, +ToolName, -Result)
|
|
3479
|
+
%% `delete("tool:name/arity")`. Removes the annotation and its implementation,
|
|
3480
|
+
%% and leaves the delegate predicate alone — that is a skill in its own right.
|
|
3481
|
+
repl_delete_tool(SessionId, ToolName, Result) :-
|
|
3482
|
+
catch(
|
|
3483
|
+
( guard_not_frozen(SessionId),
|
|
3484
|
+
atom(ToolName),
|
|
3485
|
+
( session_user_tools(SessionId, ToolName)
|
|
3486
|
+
-> repl_forget_tool(SessionId, ToolName),
|
|
3487
|
+
Result = ok
|
|
3488
|
+
; format(atom(Message),
|
|
3489
|
+
'no tool named ~w is registered, so nothing was removed', [ToolName]),
|
|
3490
|
+
throw(error(existence_error(tool, ToolName),
|
|
3491
|
+
context(repl_delete_tool/3, Message)))
|
|
3492
|
+
)
|
|
3493
|
+
),
|
|
3494
|
+
DeleteToolError,
|
|
3495
|
+
format(atom(Result), 'error(~w)', [DeleteToolError])
|
|
3496
|
+
).
|
|
3497
|
+
|
|
2287
3498
|
%% repl_define(+SessionId, +HeadPatternText, +ClausesText, -Result)
|
|
2288
3499
|
%% Replace every predicate the clause block defines, all or nothing.
|
|
2289
3500
|
%%
|
|
@@ -2295,13 +3506,16 @@ repl_delete_predicate(SessionId, Name, Arity, Result) :-
|
|
|
2295
3506
|
%% clauses being replaced are copied first so a failure can still put them back.
|
|
2296
3507
|
repl_define(SessionId, HeadPatternText, ClausesText, Result) :-
|
|
2297
3508
|
catch(
|
|
2298
|
-
(
|
|
3509
|
+
( mi_clear_cut_classes(SessionId),
|
|
3510
|
+
guard_not_frozen(SessionId),
|
|
2299
3511
|
repl_define_target(SessionId, HeadPatternText, HeadName, HeadArity),
|
|
2300
|
-
repl_parse_clauses(SessionId, ClausesText, Terms),
|
|
3512
|
+
repl_parse_clauses(SessionId, ClausesText, Terms, SourceViews),
|
|
2301
3513
|
findall(Name/Arity,
|
|
2302
3514
|
( member(Term, Terms), repl_term_indicator(Term, Name/Arity) ),
|
|
2303
3515
|
BlockIndicators0),
|
|
2304
3516
|
sort(BlockIndicators0, BlockIndicators),
|
|
3517
|
+
forall(member(Term, Terms), repl_check_script_name_for_head(SessionId, Term)),
|
|
3518
|
+
repl_reject_script_predicate_collision(SessionId, HeadName, repl_define/4),
|
|
2305
3519
|
% The head names the predicate being replaced. If it is not one of
|
|
2306
3520
|
% them the target is almost certainly a typo, and silently replacing
|
|
2307
3521
|
% a *different* predicate is worse than refusing.
|
|
@@ -2325,12 +3539,34 @@ repl_define(SessionId, HeadPatternText, ClausesText, Result) :-
|
|
|
2325
3539
|
))
|
|
2326
3540
|
),
|
|
2327
3541
|
repl_note_touched(SessionId, Terms),
|
|
3542
|
+
repl_restore_kept_tools(SessionId, Captured),
|
|
3543
|
+
repl_store_source_views(SessionId, SourceViews, replace),
|
|
2328
3544
|
Result = ok
|
|
2329
3545
|
),
|
|
2330
3546
|
DefineError,
|
|
2331
3547
|
format(atom(Result), 'error(~w)', [DefineError])
|
|
2332
3548
|
).
|
|
2333
3549
|
|
|
3550
|
+
%% repl_restore_kept_tools(+SessionId, +Captured)
|
|
3551
|
+
%%
|
|
3552
|
+
%% `define` replaces a predicate; the tool annotation is separate bookkeeping.
|
|
3553
|
+
%% It used to be dropped whenever the new block did not repeat the tool clause,
|
|
3554
|
+
%% so a perfectly good capability silently disappeared from the next `task/N`.
|
|
3555
|
+
%% Keep it when the delegate still exists at the same arity; a block that
|
|
3556
|
+
%% re-declares the tool has already registered it.
|
|
3557
|
+
repl_restore_kept_tools(SessionId, Captured) :-
|
|
3558
|
+
forall(member(indicator(Name, _Arity, _Clauses, ToolInfo), Captured),
|
|
3559
|
+
( ToolInfo = tool(true, tool_meta(Head, Desc, Names, Types, Body)),
|
|
3560
|
+
\+ session_user_tools(SessionId, Name)
|
|
3561
|
+
-> Head =.. [Name|Args],
|
|
3562
|
+
length(Args, HeadArity),
|
|
3563
|
+
( current_predicate(SessionId:Name/HeadArity)
|
|
3564
|
+
-> catch(repl_register_tool(SessionId, Head, Desc, Types, Body, Names), _, true)
|
|
3565
|
+
; true
|
|
3566
|
+
)
|
|
3567
|
+
; true
|
|
3568
|
+
)).
|
|
3569
|
+
|
|
2334
3570
|
%% repl_define_target(+SessionId, +HeadText, -Name, -Arity)
|
|
2335
3571
|
%% The first argument of `define` names the predicate being replaced, written as
|
|
2336
3572
|
%% an indicator (`f/1`) or as a clause head (`f(_)`).
|
|
@@ -2349,22 +3585,47 @@ repl_define_target(SessionId, HeadText, Name, Arity) :-
|
|
|
2349
3585
|
Arity = IndicatorArity
|
|
2350
3586
|
; repl_head_of(Pattern, HeadClause),
|
|
2351
3587
|
repl_callable_indicator(HeadClause, Name, Arity)
|
|
3588
|
+
),
|
|
3589
|
+
( Name == tool
|
|
3590
|
+
-> throw(error(domain_error(define_target, tool/Arity),
|
|
3591
|
+
context(repl_define/4,
|
|
3592
|
+
'define the predicate the tool delegates to, and put the tool(...) clause in the same block: define("up/2", "up(X, Y) :- ... . tool(up(X, Y), \"…\") :- up(X, Y).")')))
|
|
3593
|
+
; true
|
|
2352
3594
|
).
|
|
2353
3595
|
|
|
2354
3596
|
%% repl_require_committable(+SessionId, +Term)
|
|
2355
|
-
repl_require_committable(
|
|
3597
|
+
repl_require_committable(SessionId, Term) :-
|
|
2356
3598
|
( repl_protected_clause(Term)
|
|
2357
3599
|
-> throw(error(permission_error(modify, reserved_name, Term),
|
|
2358
3600
|
context(repl_define/4, 'cannot define a reserved predicate')))
|
|
2359
3601
|
; true
|
|
3602
|
+
),
|
|
3603
|
+
( catch(repl_head_of(Term, Head), _, fail),
|
|
3604
|
+
callable(Head),
|
|
3605
|
+
functor(Head, Name, Arity),
|
|
3606
|
+
session_harness_import(SessionId, Name/Arity, TargetModule, _)
|
|
3607
|
+
-> format(atom(ImportMessage),
|
|
3608
|
+
'~w/~w is imported read-only from ~w and cannot be redefined here.',
|
|
3609
|
+
[Name, Arity, TargetModule]),
|
|
3610
|
+
throw(error(permission_error(modify, imported_predicate, Name/Arity),
|
|
3611
|
+
context(repl_define/4, ImportMessage)))
|
|
3612
|
+
; true
|
|
2360
3613
|
).
|
|
2361
3614
|
|
|
2362
3615
|
%% repl_require_not_reserved(+SessionId, +Indicator)
|
|
2363
|
-
repl_require_not_reserved(
|
|
3616
|
+
repl_require_not_reserved(SessionId, Name/Arity) :-
|
|
2364
3617
|
( repl_protected_name(Name)
|
|
2365
3618
|
-> throw(error(permission_error(modify, reserved_name, Name/Arity),
|
|
2366
3619
|
context(repl_define/4, 'cannot define a reserved predicate')))
|
|
2367
3620
|
; true
|
|
3621
|
+
),
|
|
3622
|
+
( session_harness_import(SessionId, Name/Arity, TargetModule, _)
|
|
3623
|
+
-> format(atom(ImportMessage),
|
|
3624
|
+
'~w/~w is imported read-only from ~w and cannot be redefined here.',
|
|
3625
|
+
[Name, Arity, TargetModule]),
|
|
3626
|
+
throw(error(permission_error(modify, imported_predicate, Name/Arity),
|
|
3627
|
+
context(repl_define/4, ImportMessage)))
|
|
3628
|
+
; true
|
|
2368
3629
|
).
|
|
2369
3630
|
|
|
2370
3631
|
%% repl_extract_code(+Text, -Source)
|
|
@@ -2398,6 +3659,10 @@ repl_manifest(SessionId, Manifest) :-
|
|
|
2398
3659
|
sort(Tools0, Tools),
|
|
2399
3660
|
findall(Directive, session_directive(SessionId, Directive), Directives0),
|
|
2400
3661
|
sort(Directives0, Directives),
|
|
3662
|
+
findall(Name/Arity-TargetModule,
|
|
3663
|
+
session_harness_import(SessionId, Name/Arity, TargetModule, _),
|
|
3664
|
+
Imports0),
|
|
3665
|
+
sort(Imports0, Imports),
|
|
2401
3666
|
( current_predicate(SessionId:module_doc/1),
|
|
2402
3667
|
SessionId:module_doc(Summary0)
|
|
2403
3668
|
-> Summary = Summary0
|
|
@@ -2415,6 +3680,7 @@ repl_manifest(SessionId, Manifest) :-
|
|
|
2415
3680
|
),
|
|
2416
3681
|
format("- predicates: ~w~n", [Preds]),
|
|
2417
3682
|
format("- tools: ~w~n", [Tools]),
|
|
3683
|
+
format("- imports: ~w~n", [Imports]),
|
|
2418
3684
|
format("- directives: ~w~n", [Directives]),
|
|
2419
3685
|
( Docs0 == []
|
|
2420
3686
|
-> format("- docs: (none; add doc(Name/Arity, Text) facts)~n")
|
|
@@ -2523,8 +3789,10 @@ repl_history_assert_term(Term, SessionId) :-
|
|
|
2523
3789
|
%% memory is preserved.
|
|
2524
3790
|
repl_rollback(SessionId, Source, Result) :-
|
|
2525
3791
|
catch(
|
|
2526
|
-
(
|
|
3792
|
+
( mi_clear_cut_classes(SessionId),
|
|
3793
|
+
guard_not_frozen(SessionId),
|
|
2527
3794
|
repl_abolish_user_predicates(SessionId),
|
|
3795
|
+
retractall(session_source_view(SessionId, _, _)),
|
|
2528
3796
|
retractall(session_directive(SessionId, _)),
|
|
2529
3797
|
retractall(session_user_tools(SessionId, _)),
|
|
2530
3798
|
retractall(session_user_tool_schema(SessionId, _, _)),
|
|
@@ -2562,7 +3830,13 @@ repl_harness_view(SessionId, Rows) :-
|
|
|
2562
3830
|
),
|
|
2563
3831
|
format(atom(Row), "~w/~w|~w|~w", [Name, Arity, Clauses, FlagText])
|
|
2564
3832
|
),
|
|
2565
|
-
|
|
3833
|
+
OwnRows),
|
|
3834
|
+
findall(Row,
|
|
3835
|
+
( session_harness_import(SessionId, Name/Arity, TargetModule, _),
|
|
3836
|
+
format(atom(Row), "~w/~w|0|imported,read-only,~w", [Name, Arity, TargetModule])
|
|
3837
|
+
),
|
|
3838
|
+
ImportedRows),
|
|
3839
|
+
append(OwnRows, ImportedRows, Rows).
|
|
2566
3840
|
|
|
2567
3841
|
repl_predicate_clause_count(SessionId, Name, Arity, Count) :-
|
|
2568
3842
|
functor(Head, Name, Arity),
|
|
@@ -2598,9 +3872,15 @@ repl_user_predicates(SessionId, Preds) :-
|
|
|
2598
3872
|
%% repl_user_predicates_text(+SessionId, -Text)
|
|
2599
3873
|
%% Rendered "name/arity, name/arity" for change diffing on the host side.
|
|
2600
3874
|
repl_user_predicates_text(SessionId, Text) :-
|
|
2601
|
-
repl_user_predicates(SessionId,
|
|
3875
|
+
repl_user_predicates(SessionId, Preds0),
|
|
3876
|
+
% doc/2, schema/2 and script_file/2 are annotations. Reporting them as
|
|
3877
|
+
% changed predicates invited `delete(doc/2)`, which wipes every doc at once.
|
|
3878
|
+
exclude(repl_annotation_indicator, Preds0, Preds),
|
|
2602
3879
|
with_output_to(string(Text), print_pred_list(Preds)).
|
|
2603
3880
|
|
|
3881
|
+
repl_annotation_indicator(Name/Arity) :-
|
|
3882
|
+
repl_annotation_predicate(Name, Arity).
|
|
3883
|
+
|
|
2604
3884
|
%% repl_predicate_arities(+SessionId, +Name, -Arities)
|
|
2605
3885
|
%% Sorted arities of a user predicate, e.g. agent_main -> [0,1,2]. Used by
|
|
2606
3886
|
%% `deepclause run` to pick the agent_main/N to call for the given arguments.
|
|
@@ -2619,47 +3899,80 @@ repl_predicate_arities(SessionId, Name, Arities) :-
|
|
|
2619
3899
|
%% After a commit, the host may probe each changed pure predicate: if calling it
|
|
2620
3900
|
%% with fresh variables yields more than a threshold of solutions, the predicate
|
|
2621
3901
|
%% is probably missing a commit (`once/1` / `!`) and would flood an `execute`.
|
|
2622
|
-
%%
|
|
2623
|
-
%%
|
|
3902
|
+
%% Only a conservative subset of pure logic is eligible. Unknown/native host
|
|
3903
|
+
%% capabilities and exception handlers are skipped, not assumed pure. A catch/3
|
|
3904
|
+
%% retry loop can swallow inference-limit exceptions, hanging this synchronous
|
|
3905
|
+
%% advisory and the entire Node event loop. JavaScript timers cannot preempt it.
|
|
2624
3906
|
%% ============================================================
|
|
2625
3907
|
|
|
2626
|
-
%% Functors with side effects: model calls, host tools, file IO, DB mutation.
|
|
2627
|
-
repl_impure_name(task). repl_impure_name(prompt). repl_impure_name(exec).
|
|
2628
|
-
repl_impure_name(system). repl_impure_name(user). repl_impure_name(answer).
|
|
2629
|
-
repl_impure_name(input). repl_impure_name(output). repl_impure_name(read_media).
|
|
2630
|
-
repl_impure_name(attach_media). repl_impure_name(save_image). repl_impure_name(gen_image).
|
|
2631
|
-
repl_impure_name(judge). repl_impure_name(choose). repl_impure_name(rate).
|
|
2632
|
-
repl_impure_name(verify). repl_impure_name(probability).
|
|
2633
|
-
repl_impure_name(write_file). repl_impure_name(append_file). repl_impure_name(delete_file).
|
|
2634
|
-
repl_impure_name(make_directory_path). repl_impure_name(open). repl_impure_name(close).
|
|
2635
|
-
repl_impure_name(assert). repl_impure_name(assertz). repl_impure_name(asserta).
|
|
2636
|
-
repl_impure_name(retract). repl_impure_name(retractall).
|
|
2637
|
-
repl_impure_name(consult). repl_impure_name(use_module). repl_impure_name(load_files).
|
|
2638
|
-
|
|
2639
|
-
%% repl_body_impure(+Goal) — true when the goal tree calls a side-effecting name.
|
|
2640
|
-
repl_body_impure(Goal) :-
|
|
2641
|
-
callable(Goal),
|
|
2642
|
-
functor(Goal, Name, _),
|
|
2643
|
-
repl_impure_name(Name),
|
|
2644
|
-
!.
|
|
2645
|
-
repl_body_impure((A, _)) :- repl_body_impure(A).
|
|
2646
|
-
repl_body_impure((_, B)) :- repl_body_impure(B).
|
|
2647
|
-
repl_body_impure((A ; B)) :- ( repl_body_impure(A) ; repl_body_impure(B) ).
|
|
2648
|
-
repl_body_impure((A -> B)) :- ( repl_body_impure(A) ; repl_body_impure(B) ).
|
|
2649
|
-
repl_body_impure(\+ Goal) :- repl_body_impure(Goal).
|
|
2650
|
-
repl_body_impure(once(Goal)) :- repl_body_impure(Goal).
|
|
2651
|
-
repl_body_impure(ignore(Goal)) :- repl_body_impure(Goal).
|
|
2652
|
-
repl_body_impure(forall(Gen, Action)) :- ( repl_body_impure(Gen) ; repl_body_impure(Action) ).
|
|
2653
|
-
repl_body_impure(catch(Goal, _, Recovery)) :- ( repl_body_impure(Goal) ; repl_body_impure(Recovery) ).
|
|
2654
|
-
repl_body_impure(findall(_, Goal, _)) :- repl_body_impure(Goal).
|
|
2655
|
-
repl_body_impure(findall(_, Goal, _, _)) :- repl_body_impure(Goal).
|
|
2656
|
-
repl_body_impure(call_with_inference_limit(Goal, _, _)) :- repl_body_impure(Goal).
|
|
2657
|
-
repl_body_impure(with_output_to(_, Goal)) :- repl_body_impure(Goal).
|
|
2658
|
-
repl_body_impure(_:Goal) :- repl_body_impure(Goal).
|
|
2659
|
-
|
|
2660
3908
|
repl_predicate_pure(SessionId, Name, Arity) :-
|
|
2661
|
-
|
|
2662
|
-
|
|
3909
|
+
% Bound the static call-graph walk too (shared branches can grow exponentially).
|
|
3910
|
+
catch(call_with_inference_limit(
|
|
3911
|
+
once(repl_probe_safe_predicate(SessionId, Name, Arity, [])), 10000, Status),
|
|
3912
|
+
_, fail),
|
|
3913
|
+
memberchk(Status, [true, !]).
|
|
3914
|
+
|
|
3915
|
+
repl_probe_safe_predicate(SessionId, Name, Arity, Seen) :-
|
|
3916
|
+
( memberchk(Name/Arity, Seen)
|
|
3917
|
+
-> true
|
|
3918
|
+
; repl_user_predicate(SessionId, Name, Arity),
|
|
3919
|
+
functor(Head, Name, Arity),
|
|
3920
|
+
forall(clause(SessionId:Head, Body),
|
|
3921
|
+
repl_probe_safe_goal(SessionId, Body, [Name/Arity|Seen]))
|
|
3922
|
+
).
|
|
3923
|
+
|
|
3924
|
+
%% Inspect data, never execute the inspected body. Recurse through *all* local
|
|
3925
|
+
%% delegates, so wrapping receive/2 or assertz/1 in a helper does not evade this.
|
|
3926
|
+
%% catch/3, call/N, budget overrides, module qualification and opaque libraries
|
|
3927
|
+
%% deliberately have no safe case. Skipping a warning is better than running a
|
|
3928
|
+
%% service, changing the DB, doing IO, or losing the probe's own stopping rule.
|
|
3929
|
+
repl_probe_safe_goal(_, Goal, _) :- var(Goal), !, fail.
|
|
3930
|
+
repl_probe_safe_goal(S, (A, B), Seen) :- !,
|
|
3931
|
+
repl_probe_safe_goal(S, A, Seen), repl_probe_safe_goal(S, B, Seen).
|
|
3932
|
+
repl_probe_safe_goal(S, (A ; B), Seen) :- !,
|
|
3933
|
+
repl_probe_safe_goal(S, A, Seen), repl_probe_safe_goal(S, B, Seen).
|
|
3934
|
+
repl_probe_safe_goal(S, (A -> B), Seen) :- !,
|
|
3935
|
+
repl_probe_safe_goal(S, A, Seen), repl_probe_safe_goal(S, B, Seen).
|
|
3936
|
+
repl_probe_safe_goal(S, \+ A, Seen) :- !, repl_probe_safe_goal(S, A, Seen).
|
|
3937
|
+
repl_probe_safe_goal(S, once(A), Seen) :- !, repl_probe_safe_goal(S, A, Seen).
|
|
3938
|
+
repl_probe_safe_goal(S, ignore(A), Seen) :- !, repl_probe_safe_goal(S, A, Seen).
|
|
3939
|
+
repl_probe_safe_goal(S, forall(A, B), Seen) :- !,
|
|
3940
|
+
repl_probe_safe_goal(S, A, Seen), repl_probe_safe_goal(S, B, Seen).
|
|
3941
|
+
repl_probe_safe_goal(S, findall(_, A, _), Seen) :- !, repl_probe_safe_goal(S, A, Seen).
|
|
3942
|
+
repl_probe_safe_goal(S, findall(_, A, _, _), Seen) :- !, repl_probe_safe_goal(S, A, Seen).
|
|
3943
|
+
repl_probe_safe_goal(S, bagof(_, A, _), Seen) :- !, repl_probe_safe_goal(S, A, Seen).
|
|
3944
|
+
repl_probe_safe_goal(S, setof(_, A, _), Seen) :- !, repl_probe_safe_goal(S, A, Seen).
|
|
3945
|
+
repl_probe_safe_goal(S, Goal, Seen) :-
|
|
3946
|
+
callable(Goal), functor(Goal, Name, Arity),
|
|
3947
|
+
( repl_user_predicate(S, Name, Arity)
|
|
3948
|
+
-> repl_probe_safe_predicate(S, Name, Arity, Seen)
|
|
3949
|
+
; repl_probe_safe_builtin(Name/Arity),
|
|
3950
|
+
( predicate_property(S:Goal, built_in)
|
|
3951
|
+
; predicate_property(S:Goal, imported_from(Module)),
|
|
3952
|
+
memberchk(Module, [system, lists])
|
|
3953
|
+
)
|
|
3954
|
+
).
|
|
3955
|
+
|
|
3956
|
+
%% Closed, non-meta leaves only. In particular, do not allow arbitrary built_in
|
|
3957
|
+
%% predicates: sleep/1, halt/0, assertz/1, IO and flag mutation are built-ins too.
|
|
3958
|
+
repl_probe_safe_builtin(Indicator) :-
|
|
3959
|
+
memberchk(Indicator, [
|
|
3960
|
+
true/0, fail/0, false/0, (!)/0,
|
|
3961
|
+
(=)/2, (\=)/2, (==)/2, (\==)/2, (=@=)/2, (\=@=)/2,
|
|
3962
|
+
is/2, (=:=)/2, (=\=)/2, (<)/2, (>)/2, (=<)/2, (>=)/2,
|
|
3963
|
+
var/1, nonvar/1, atom/1, string/1, integer/1, float/1, number/1,
|
|
3964
|
+
atomic/1, compound/1, callable/1, ground/1, is_list/1, acyclic_term/1,
|
|
3965
|
+
between/3, succ/2, plus/3, compare/3,
|
|
3966
|
+
functor/3, arg/3, (=..)/2, compound_name_arity/3, compound_name_arguments/3,
|
|
3967
|
+
term_variables/2, copy_term/2, sort/2, msort/2, keysort/2,
|
|
3968
|
+
member/2, memberchk/2, append/3, length/2, reverse/2, last/2,
|
|
3969
|
+
nth0/3, nth1/3, select/3, numlist/3,
|
|
3970
|
+
is_dict/1, is_dict/2, get_dict/3, put_dict/3, put_dict/4, dict_pairs/3,
|
|
3971
|
+
atom_length/2, string_length/2, atom_string/2, atom_chars/2, string_chars/2,
|
|
3972
|
+
atom_number/2, number_string/2, atom_concat/3, string_concat/3,
|
|
3973
|
+
sub_atom/5, sub_string/5, downcase_atom/2, upcase_atom/2,
|
|
3974
|
+
string_lower/2, string_upper/2, atomic_list_concat/2, atomic_list_concat/3
|
|
3975
|
+
]).
|
|
2663
3976
|
|
|
2664
3977
|
%% ============================================================
|
|
2665
3978
|
%% Tool contract validation (end_turn advisory)
|
|
@@ -2676,16 +3989,40 @@ repl_predicate_pure(SessionId, Name, Arity) :-
|
|
|
2676
3989
|
|
|
2677
3990
|
%% repl_validate_tools(+SessionId, -Warnings)
|
|
2678
3991
|
repl_validate_tools(SessionId, Warnings) :-
|
|
2679
|
-
findall(W,
|
|
3992
|
+
findall(W,
|
|
3993
|
+
( tool_contract_violation(SessionId, W)
|
|
3994
|
+
; tool_shape_violation(SessionId, W)
|
|
3995
|
+
),
|
|
3996
|
+
Raw),
|
|
2680
3997
|
list_to_set(Raw, Warnings).
|
|
2681
3998
|
|
|
3999
|
+
%% tool_shape_violation(+SessionId, -Warning)
|
|
4000
|
+
%%
|
|
4001
|
+
%% A tool head has exactly one output, and it is the last argument. The type
|
|
4002
|
+
%% list labels only the inputs, so a mismatch means the author's `Content` is
|
|
4003
|
+
%% being offered to the model as an output — the `fs_write(Path, Content)` shape,
|
|
4004
|
+
%% which a nested agent cannot fill in. This used to be printed to stderr at
|
|
4005
|
+
%% registration time, where the agent never saw it.
|
|
4006
|
+
tool_shape_violation(SessionId, Warning) :-
|
|
4007
|
+
session_user_tools(SessionId, ToolName),
|
|
4008
|
+
\+ session_builtin_tool(SessionId, ToolName),
|
|
4009
|
+
session_user_tool_arity(SessionId, ToolName, Arity),
|
|
4010
|
+
session_user_tool_types(SessionId, ToolName, Types),
|
|
4011
|
+
is_list(Types),
|
|
4012
|
+
length(Types, Declared),
|
|
4013
|
+
Inputs is Arity - 1,
|
|
4014
|
+
Declared =\= Inputs,
|
|
4015
|
+
!,
|
|
4016
|
+
format(string(Warning),
|
|
4017
|
+
'tool ~w/~w declares ~w input type(s) but has ~w input argument(s). The last argument of a tool head is its output; put several values in one dict or JSON string, or add an explicit result argument.',
|
|
4018
|
+
[ToolName, Arity, Declared, Inputs]).
|
|
4019
|
+
|
|
2682
4020
|
%% repl_validate_docs(+SessionId, -Warnings)
|
|
2683
4021
|
%%
|
|
2684
|
-
%% `doc(Name/Arity, Text)` is an annotation: it documents a predicate, it does
|
|
2685
|
-
%% create one. A `doc/2` whose predicate has no clauses
|
|
2686
|
-
%% fails silently when called
|
|
2687
|
-
%%
|
|
2688
|
-
%% Reported at `end_turn`, where the author can still see what they just wrote.
|
|
4022
|
+
%% `doc(Name/Arity, Text)` is an annotation: it documents a predicate, it does
|
|
4023
|
+
%% not create one. A `doc/2` whose predicate has no clauses names a goal that
|
|
4024
|
+
%% fails silently when called, and the name still appears in the manifest, so the
|
|
4025
|
+
%% listing looks healthy. Reported at `end_turn`.
|
|
2689
4026
|
repl_validate_docs(SessionId, Warnings) :-
|
|
2690
4027
|
findall(W, doc_without_clauses(SessionId, W), Raw),
|
|
2691
4028
|
list_to_set(Raw, Warnings).
|
|
@@ -2704,6 +4041,9 @@ doc_without_clauses(SessionId, Warning) :-
|
|
|
2704
4041
|
'doc(~w/~w) documents a predicate with no clauses, so calling it fails silently and yields an empty result. Define ~w/~w, or remove the doc/2.',
|
|
2705
4042
|
[Name, Arity, Name, Arity]).
|
|
2706
4043
|
|
|
4044
|
+
%% tool_contract_violation(+SessionId, -Warning)
|
|
4045
|
+
%% A nested agent calls the tool body, so it must reach real logic with the same
|
|
4046
|
+
%% name and arity as its head.
|
|
2707
4047
|
tool_contract_violation(SessionId, Warning) :-
|
|
2708
4048
|
clause(SessionId:tool(Head), Body),
|
|
2709
4049
|
compound(Head),
|
|
@@ -2717,8 +4057,8 @@ tool_contract_violation(SessionId, Warning) :-
|
|
|
2717
4057
|
length(Args, CalleeArity),
|
|
2718
4058
|
( Callee == Name, CalleeArity == Arity, \+ repl_user_predicate(SessionId, Name, Arity)
|
|
2719
4059
|
-> format(string(Warning),
|
|
2720
|
-
'tool ~w/~w calls
|
|
2721
|
-
[Name, Arity,
|
|
4060
|
+
'tool ~w/~w calls ~w/~w, which does not exist, so a nested agent gets "Tool not found". Define ~w/~w and delegate to it: tool(~w(A, B), "…") :- ~w(A, B).',
|
|
4061
|
+
[Name, Arity, Callee, CalleeArity, Name, Arity, Name, Name])
|
|
2722
4062
|
; Callee == Name, CalleeArity \== Arity
|
|
2723
4063
|
-> format(string(Warning),
|
|
2724
4064
|
'tool ~w/~w declares a different arity than its body calls (~w/~w). A tool head and its delegate must match, with the output argument last.',
|
|
@@ -2740,17 +4080,86 @@ tool_host_predicate(Name, Arity) :-
|
|
|
2740
4080
|
functor(Goal, Name, Arity),
|
|
2741
4081
|
is_mi_special_predicate(Goal).
|
|
2742
4082
|
|
|
2743
|
-
%% tool_body_goal(+Body, -FirstCall): a tool body must reach a call, not be empty.
|
|
2744
|
-
tool_body_goal(Body, Call) :-
|
|
2745
|
-
Body \== true,
|
|
2746
|
-
tool_first_goal(Body, Call),
|
|
2747
|
-
compound(Call),
|
|
2748
|
-
functor(Call, F, _),
|
|
2749
|
-
F \== (',').
|
|
4083
|
+
%% tool_body_goal(+Body, -FirstCall): a tool body must reach a call, not be empty.
|
|
4084
|
+
tool_body_goal(Body, Call) :-
|
|
4085
|
+
Body \== true,
|
|
4086
|
+
tool_first_goal(Body, Call),
|
|
4087
|
+
compound(Call),
|
|
4088
|
+
functor(Call, F, _),
|
|
4089
|
+
F \== (',').
|
|
4090
|
+
|
|
4091
|
+
%% tool_first_goal(+Body, -Call): the first call in a conjunction body.
|
|
4092
|
+
%% tool_first_goal((A, _), Call) :- !, tool_first_goal(A, Call).
|
|
4093
|
+
tool_first_goal(Call, Call).
|
|
4094
|
+
|
|
4095
|
+
%% ============================================================
|
|
4096
|
+
%% Complexity advisories
|
|
4097
|
+
%% ============================================================
|
|
4098
|
+
%%
|
|
4099
|
+
%% The goal is many small composable skills, not one clever predicate. These
|
|
4100
|
+
%% checks catch the shapes a model falls into when it reaches for Prolog instead
|
|
4101
|
+
%% of composition: a long clause, inline Python long enough to belong in a
|
|
4102
|
+
%% script, and an `agent_main` that returns a value instead of answering.
|
|
4103
|
+
repl_validate_complexity(SessionId, Warnings) :-
|
|
4104
|
+
findall(W, complexity_violation(SessionId, W), Raw),
|
|
4105
|
+
list_to_set(Raw, Warnings).
|
|
4106
|
+
|
|
4107
|
+
complexity_violation(SessionId, Warning) :-
|
|
4108
|
+
current_predicate(SessionId:Name/Arity),
|
|
4109
|
+
integer(Arity),
|
|
4110
|
+
repl_user_predicate(SessionId, Name, Arity),
|
|
4111
|
+
functor(Head, Name, Arity),
|
|
4112
|
+
clause(SessionId:Head, Body),
|
|
4113
|
+
Body \== true,
|
|
4114
|
+
( Name == agent_main,
|
|
4115
|
+
Arity > 0,
|
|
4116
|
+
\+ body_calls_predicate(Body, answer, 1)
|
|
4117
|
+
-> format(string(Warning),
|
|
4118
|
+
'agent_main/~w has arguments but never calls answer/1. `agent_main/N` takes the run inputs; return the result with answer/1, not as an extra output argument.',
|
|
4119
|
+
[Arity])
|
|
4120
|
+
; body_goal_count(Body, Count),
|
|
4121
|
+
Count > 12
|
|
4122
|
+
-> format(string(Warning),
|
|
4123
|
+
'~w/~w has ~w goals in one clause. Split it into named skills and compose them (A, B) or with maplist/3; a clause this long is where a harness stops being reviewable.',
|
|
4124
|
+
[Name, Arity, Count])
|
|
4125
|
+
; body_inline_python_lines(Body, Lines),
|
|
4126
|
+
Lines > 3
|
|
4127
|
+
-> format(string(Warning),
|
|
4128
|
+
'~w/~w embeds ~w lines of Python in a DML string. Put the script in a workspace file, link it with script_file/2, and call run_script(Name, Args, Value).',
|
|
4129
|
+
[Name, Arity, Lines])
|
|
4130
|
+
; fail
|
|
4131
|
+
).
|
|
4132
|
+
|
|
4133
|
+
body_calls_predicate(Body, Name, Arity) :-
|
|
4134
|
+
sub_term(Sub, Body),
|
|
4135
|
+
compound(Sub),
|
|
4136
|
+
compound_name_arity(Sub, Name, Arity).
|
|
2750
4137
|
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
4138
|
+
body_goal_count(Body, Count) :-
|
|
4139
|
+
conj_list(Body, Goals),
|
|
4140
|
+
length(Goals, Count).
|
|
4141
|
+
|
|
4142
|
+
body_inline_python_lines(Body, Lines) :-
|
|
4143
|
+
sub_term(Sub, Body),
|
|
4144
|
+
compound(Sub),
|
|
4145
|
+
compound_name_arity(Sub, python, _Arity),
|
|
4146
|
+
Sub =.. [_|Options],
|
|
4147
|
+
python_option(code, Options, Code),
|
|
4148
|
+
string(Code),
|
|
4149
|
+
split_string(Code, "\n", "", Lines0),
|
|
4150
|
+
length(Lines0, Lines).
|
|
4151
|
+
|
|
4152
|
+
%% python_option(+Key, +Options, -Value)
|
|
4153
|
+
%% `exec(python(code: "…"))` reaches Prolog as `python(:(code, "…"))`; a dict is
|
|
4154
|
+
%% also accepted. Both spell the same option.
|
|
4155
|
+
python_option(Key, Options, Value) :-
|
|
4156
|
+
member(Option, Options),
|
|
4157
|
+
( is_dict(Option)
|
|
4158
|
+
-> get_dict(Key, Option, Value)
|
|
4159
|
+
; compound_name_arity(Option, :, 2),
|
|
4160
|
+
arg(1, Option, Key),
|
|
4161
|
+
arg(2, Option, Value)
|
|
4162
|
+
).
|
|
2754
4163
|
|
|
2755
4164
|
%% True when the predicate has at least one clause with a real body (not a fact).
|
|
2756
4165
|
repl_predicate_has_rule(SessionId, Name, Arity) :-
|
|
@@ -3012,6 +4421,8 @@ repl_snapshot_save(SessionId) :-
|
|
|
3012
4421
|
; ToolSources = []
|
|
3013
4422
|
),
|
|
3014
4423
|
findall(Directive, session_directive(SessionId, Directive), Directives),
|
|
4424
|
+
findall(Name/Arity, session_harness_export(SessionId, Name, Arity), Exports),
|
|
4425
|
+
findall(import(Import, Target, TargetImport), session_harness_import(SessionId, Import, Target, TargetImport), Import0),
|
|
3015
4426
|
( session_repl_memory(SessionId, Memory) -> true ; Memory = [] ),
|
|
3016
4427
|
assertz(repl_snapshot(SessionId, snapshot{
|
|
3017
4428
|
clauses: Clauses,
|
|
@@ -3019,11 +4430,14 @@ repl_snapshot_save(SessionId) :-
|
|
|
3019
4430
|
schemas: Schemas,
|
|
3020
4431
|
tool_sources: ToolSources,
|
|
3021
4432
|
directives: Directives,
|
|
4433
|
+
exports: Exports,
|
|
4434
|
+
imports: Import0,
|
|
3022
4435
|
memory: Memory
|
|
3023
4436
|
})).
|
|
3024
4437
|
|
|
3025
4438
|
%% repl_snapshot_restore(+SessionId)
|
|
3026
4439
|
repl_snapshot_restore(SessionId) :-
|
|
4440
|
+
mi_clear_cut_classes(SessionId),
|
|
3027
4441
|
( repl_snapshot(SessionId, Snapshot)
|
|
3028
4442
|
-> retract(repl_snapshot(SessionId, Snapshot)),
|
|
3029
4443
|
findall(Name/Arity,
|
|
@@ -3048,6 +4462,19 @@ repl_snapshot_restore(SessionId) :-
|
|
|
3048
4462
|
retractall(session_directive(SessionId, _)),
|
|
3049
4463
|
forall(member(Directive, Snapshot.directives),
|
|
3050
4464
|
assertz(session_directive(SessionId, Directive))),
|
|
4465
|
+
retractall(session_harness_export(SessionId, _, _)),
|
|
4466
|
+
( get_dict(exports, Snapshot, SnapshotExports)
|
|
4467
|
+
-> forall(member(ExportName/ExportArity, SnapshotExports),
|
|
4468
|
+
assertz(session_harness_export(SessionId, ExportName, ExportArity)))
|
|
4469
|
+
; true
|
|
4470
|
+
),
|
|
4471
|
+
retractall(session_harness_import(SessionId, _, _, _)),
|
|
4472
|
+
( get_dict(imports, Snapshot, SnapshotImports)
|
|
4473
|
+
-> true
|
|
4474
|
+
; SnapshotImports = []
|
|
4475
|
+
),
|
|
4476
|
+
forall(member(import(Import, Target, TargetImport), SnapshotImports),
|
|
4477
|
+
assertz(session_harness_import(SessionId, Import, Target, TargetImport))),
|
|
3051
4478
|
retractall(session_repl_memory(SessionId, _)),
|
|
3052
4479
|
assertz(session_repl_memory(SessionId, Snapshot.memory))
|
|
3053
4480
|
; true
|
|
@@ -3171,6 +4598,80 @@ repl_execute_frame(SessionId, GoalText, FrameId, Error) :-
|
|
|
3171
4598
|
format(atom(Error), '~w', [ExecuteError])
|
|
3172
4599
|
).
|
|
3173
4600
|
|
|
4601
|
+
%% A hook uses an owned frame: interpret its body once, then validate outputs
|
|
4602
|
+
%% natively rather than exposing reserved host helpers to interpreted DML.
|
|
4603
|
+
repl_hook_frame(SessionId, GoalText, FrameId, Error) :-
|
|
4604
|
+
catch((
|
|
4605
|
+
read_repl_goal(SessionId, GoalText, Goal, Names),
|
|
4606
|
+
memberchk('HookState'=HookState, Names), memberchk('HookDecision'=Decision, Names),
|
|
4607
|
+
repl_initial_state(SessionId, nested, State),
|
|
4608
|
+
create_frame_raw(SessionId, nested, 1,
|
|
4609
|
+
deepclause_mi:repl_hook_solve(SessionId, Goal, State, HookState, Decision), FrameId),
|
|
4610
|
+
Error = none
|
|
4611
|
+
), E, format(atom(Error), '~w', [E])).
|
|
4612
|
+
|
|
4613
|
+
repl_hook_solve(SessionId, Goal, State, HookState, Decision) :-
|
|
4614
|
+
nb_setval(current_session_id, SessionId),
|
|
4615
|
+
once(mi_call(Goal, State, StateOut)),
|
|
4616
|
+
repl_hook_result(HookState, Decision, Json),
|
|
4617
|
+
store_memory_for_state(SessionId, StateOut),
|
|
4618
|
+
render_binding(Json, Text), engine_yield(solution(['HookJson'-Text], true)).
|
|
4619
|
+
|
|
4620
|
+
%% Work hook outputs are data, never serialized executable continuations.
|
|
4621
|
+
repl_hook_state(Text, Canonical) :-
|
|
4622
|
+
string_length(Text, Length), Length =< 65536,
|
|
4623
|
+
read_term_from_atom(Text, State, [syntax_errors(error)]),
|
|
4624
|
+
repl_hook_state_value(State),
|
|
4625
|
+
with_output_to(string(Canonical), write_term(State, [quoted(true), ignore_ops(true), max_depth(0)])).
|
|
4626
|
+
|
|
4627
|
+
repl_hook_state_value(State) :-
|
|
4628
|
+
( ground(State), acyclic_term(State), hook_data(State, 32)
|
|
4629
|
+
-> true
|
|
4630
|
+
; throw(error(domain_error(hook_state, 'expected bounded ground data'), _))
|
|
4631
|
+
).
|
|
4632
|
+
|
|
4633
|
+
hook_data(Value, Depth) :-
|
|
4634
|
+
Depth > 0,
|
|
4635
|
+
( atom(Value) -> true
|
|
4636
|
+
; string(Value) -> true
|
|
4637
|
+
; integer(Value) -> abs(Value) =< 9007199254740991
|
|
4638
|
+
; float(Value) -> float_class(Value, Class), memberchk(Class, [normal, subnormal, zero])
|
|
4639
|
+
; Next is Depth - 1,
|
|
4640
|
+
( is_list(Value) -> maplist(hook_data_at(Next), Value)
|
|
4641
|
+
; is_dict(Value), dict_pairs(Value, Tag, Pairs), atom(Tag),
|
|
4642
|
+
maplist(hook_pair_at(Next), Pairs)
|
|
4643
|
+
)
|
|
4644
|
+
).
|
|
4645
|
+
hook_data_at(Depth, Value) :- hook_data(Value, Depth).
|
|
4646
|
+
hook_pair_at(Depth, _-Value) :- hook_data(Value, Depth).
|
|
4647
|
+
|
|
4648
|
+
repl_hook_result(State, Decision, Json) :-
|
|
4649
|
+
repl_hook_state_value(State),
|
|
4650
|
+
( ground(Decision),
|
|
4651
|
+
( Decision == continue -> Action = continue, Text = ""
|
|
4652
|
+
; Decision == consume -> Action = consume, Text = ""
|
|
4653
|
+
; Decision = reply(Text), string(Text) -> Action = reply
|
|
4654
|
+
; Decision = reject(Text), string(Text) -> Action = reject
|
|
4655
|
+
)
|
|
4656
|
+
-> true
|
|
4657
|
+
; throw(error(domain_error(hook_decision, 'expected continue, reply(Text), or reject(Reason)'), _))
|
|
4658
|
+
),
|
|
4659
|
+
with_output_to(string(StateText), write_term(State, [quoted(true), ignore_ops(true), max_depth(0)])),
|
|
4660
|
+
string_length(StateText, Size),
|
|
4661
|
+
( Size =< 65536 -> true ; throw(error(resource_error(hook_state_size), _)) ),
|
|
4662
|
+
atom_json_dict(Atom, _{state:StateText, action:Action, text:Text}, []), atom_string(Atom, Json).
|
|
4663
|
+
|
|
4664
|
+
repl_hook_memory(SessionId, Json) :-
|
|
4665
|
+
repl_get_memory(SessionId, harness, Memory),
|
|
4666
|
+
maplist(hook_memory_entry, Memory, Entries),
|
|
4667
|
+
atom_json_dict(Atom, Entries, []), atom_string(Atom, Json).
|
|
4668
|
+
hook_memory_entry(Message, Entry) :-
|
|
4669
|
+
get_dict(role, Message, Role), memberchk(Role, [system, user, assistant]),
|
|
4670
|
+
get_dict(content, Message, Content0), text_value(Content0, Content),
|
|
4671
|
+
Base = _{role:Role, content:Content},
|
|
4672
|
+
( get_dict(hook_ref, Message, Ref) -> put_dict(ref, Base, Ref, WithRef) ; WithRef = Base ),
|
|
4673
|
+
( get_dict(media, Message, Media) -> put_dict(media, WithRef, Media, Entry) ; Entry = WithRef ).
|
|
4674
|
+
|
|
3174
4675
|
%% determine_agent_goal(+SessionId, +Args, -Goal)
|
|
3175
4676
|
%% Args is a list of positional arguments - passed directly to agent_main
|
|
3176
4677
|
%% Prefers arity that matches the number of arguments provided
|
|
@@ -3225,6 +4726,7 @@ step_engine(SessionId, Status, Content, Payload) :-
|
|
|
3225
4726
|
process_engine_result(output(Text), output, Text, none) :- !.
|
|
3226
4727
|
process_engine_result(media(Payload), media, '', Payload) :- is_dict(Payload), !.
|
|
3227
4728
|
process_engine_result(html(Payload), html, '', Payload) :- is_dict(Payload), !.
|
|
4729
|
+
process_engine_result(download(Payload), download, '', Payload) :- is_dict(Payload), !.
|
|
3228
4730
|
process_engine_result(log(Text), log, Text, none) :- !.
|
|
3229
4731
|
process_engine_result(suspend(Continuation, Memory), suspend, '', payload{continuation: Continuation, memory: Memory}) :- !.
|
|
3230
4732
|
process_engine_result(answer(Text), answer, Text, none) :- !.
|
|
@@ -3268,6 +4770,8 @@ process_engine_result(wait_form(FormId, Since, Timeout), wait_form, '',
|
|
|
3268
4770
|
payload{formId: FormId, since: Since, timeout: Timeout}) :- !.
|
|
3269
4771
|
|
|
3270
4772
|
%% proc_request_payload(+Request, -Payload)
|
|
4773
|
+
proc_request_payload(agent_self, payload{op: agent_self}) :- !.
|
|
4774
|
+
proc_request_payload(agent_post(To, Message), payload{op: agent_post, to: To, message: Message}) :- !.
|
|
3271
4775
|
proc_request_payload(self, payload{op: self}) :- !.
|
|
3272
4776
|
proc_request_payload(spawn(Goal), payload{op: spawn, goal: Goal}) :- !.
|
|
3273
4777
|
proc_request_payload(spawn(Goal, Options), payload{op: spawn, goal: Goal, options: Options}) :- !.
|
|
@@ -3322,6 +4826,7 @@ destroy_engine(SessionId) :-
|
|
|
3322
4826
|
retractall(session_params(SessionId, _)),
|
|
3323
4827
|
retractall(session_user_tools(SessionId, _)),
|
|
3324
4828
|
retractall(session_user_tool_schema(SessionId, _, _)),
|
|
4829
|
+
retractall(session_source_view(SessionId, _, _)),
|
|
3325
4830
|
retractall(session_pending_input(SessionId, _)),
|
|
3326
4831
|
retractall(session_input_cancelled(SessionId)),
|
|
3327
4832
|
retractall(session_agent_result(SessionId, _)),
|
|
@@ -3329,7 +4834,7 @@ destroy_engine(SessionId) :-
|
|
|
3329
4834
|
retractall(session_sample_token_result(SessionId, _)),
|
|
3330
4835
|
retractall(session_llm_result(SessionId, _)),
|
|
3331
4836
|
retractall(session_judgment_result(SessionId, _)),
|
|
3332
|
-
|
|
4837
|
+
reset_trace_store(SessionId),
|
|
3333
4838
|
retractall(session_trace_enabled(SessionId, _)),
|
|
3334
4839
|
( current_module(SessionId)
|
|
3335
4840
|
-> catch(
|
|
@@ -3619,34 +5124,255 @@ get_depth(State, Depth) :-
|
|
|
3619
5124
|
set_depth(StateIn, Depth, StateOut) :-
|
|
3620
5125
|
StateOut = StateIn.put(depth, Depth).
|
|
3621
5126
|
|
|
5127
|
+
%% get_harness_context(+State, -Module)
|
|
5128
|
+
%% The module whose local predicates are currently being resolved. `session`
|
|
5129
|
+
%% means the caller's live harness module.
|
|
5130
|
+
get_harness_context(State, Module) :-
|
|
5131
|
+
( is_dict(State), get_dict(harness_module, State, Module)
|
|
5132
|
+
-> true
|
|
5133
|
+
; get_session_id(SessionId),
|
|
5134
|
+
Module = SessionId
|
|
5135
|
+
).
|
|
5136
|
+
|
|
5137
|
+
set_harness_context(StateIn, Module, StateOut) :-
|
|
5138
|
+
StateOut = StateIn.put(harness_module, Module).
|
|
5139
|
+
|
|
5140
|
+
harness_module_loaded(Module) :-
|
|
5141
|
+
harness_module_owner(Module, _, _, _).
|
|
5142
|
+
|
|
5143
|
+
import_target(Owner, Name, Arity, TargetModule, TargetName) :-
|
|
5144
|
+
( harness_module_loaded(Owner)
|
|
5145
|
+
-> harness_module_import(Owner, Name/Arity, TargetModule, TargetName/Arity)
|
|
5146
|
+
; session_harness_import(Owner, Name/Arity, TargetModule, TargetName/Arity)
|
|
5147
|
+
).
|
|
5148
|
+
|
|
5149
|
+
module_local_predicate(Module, Name, Arity) :-
|
|
5150
|
+
functor(Head, Name, Arity),
|
|
5151
|
+
predicate_property(Module:Head, defined),
|
|
5152
|
+
\+ predicate_property(Module:Head, imported_from(_)),
|
|
5153
|
+
\+ predicate_property(Module:Head, built_in),
|
|
5154
|
+
\+ predicate_property(Module:Head, foreign).
|
|
5155
|
+
|
|
5156
|
+
is_harness_module(Module) :-
|
|
5157
|
+
harness_module_owner(Module, _, _, _).
|
|
5158
|
+
|
|
5159
|
+
retarget_goal(Goal, Name, TargetGoal) :-
|
|
5160
|
+
Goal =.. [_|Args],
|
|
5161
|
+
TargetGoal =.. [Name|Args].
|
|
5162
|
+
|
|
5163
|
+
mi_call_harness_goal(Module, Goal, StateIn, StateOut) :-
|
|
5164
|
+
get_session_id(SessionId),
|
|
5165
|
+
get_harness_context(StateIn, Previous),
|
|
5166
|
+
( Previous == Module
|
|
5167
|
+
-> true
|
|
5168
|
+
; functor(Goal, Name, Arity),
|
|
5169
|
+
( harness_module_export(Module, Name, Arity)
|
|
5170
|
+
-> true
|
|
5171
|
+
; throw(error(permission_error(access, private_predicate, Module:Goal),
|
|
5172
|
+
context(use_harness/2, 'only exported predicates are callable from another harness')))
|
|
5173
|
+
)
|
|
5174
|
+
),
|
|
5175
|
+
get_depth(StateIn, Depth),
|
|
5176
|
+
NextDepth is Depth + 1,
|
|
5177
|
+
State1 = StateIn.put(depth, NextDepth),
|
|
5178
|
+
set_harness_context(State1, Module, State2),
|
|
5179
|
+
trace_goal_call(SessionId, Goal, Depth),
|
|
5180
|
+
( clause(Module:Goal, Body)
|
|
5181
|
+
*-> ( Body == true
|
|
5182
|
+
-> trace_goal_exit(SessionId, Goal, Depth),
|
|
5183
|
+
State3 = State2
|
|
5184
|
+
; mi_call(Body, State2, State3),
|
|
5185
|
+
trace_goal_exit(SessionId, Goal, Depth)
|
|
5186
|
+
),
|
|
5187
|
+
State4 = State3.put(depth, Depth),
|
|
5188
|
+
set_harness_context(State4, Previous, StateOut)
|
|
5189
|
+
; trace_goal_fail(SessionId, Goal, Depth),
|
|
5190
|
+
fail
|
|
5191
|
+
).
|
|
5192
|
+
|
|
5193
|
+
%% mi_call_dispatch for an unqualified goal inside a harness module. Local
|
|
5194
|
+
%% predicates win; otherwise resolve that module's imports. Never fall through
|
|
5195
|
+
%% to the caller's session module, which would leak private caller predicates.
|
|
5196
|
+
mi_call_dispatch(Goal, StateIn, StateOut) :-
|
|
5197
|
+
get_session_id(SessionId),
|
|
5198
|
+
get_harness_context(StateIn, Current),
|
|
5199
|
+
Current \== SessionId,
|
|
5200
|
+
callable(Goal),
|
|
5201
|
+
\+ predicate_property(Goal, built_in),
|
|
5202
|
+
functor(Goal, Name, Arity),
|
|
5203
|
+
!,
|
|
5204
|
+
( module_local_predicate(Current, Name, Arity)
|
|
5205
|
+
-> mi_call_harness_goal(Current, Goal, StateIn, StateOut)
|
|
5206
|
+
; import_target(Current, Name, Arity, TargetModule, TargetName)
|
|
5207
|
+
-> retarget_goal(Goal, TargetName, TargetGoal),
|
|
5208
|
+
mi_call_harness_goal(TargetModule, TargetGoal, StateIn, StateOut)
|
|
5209
|
+
; predicate_property(Current:Goal, defined)
|
|
5210
|
+
-> get_depth(StateIn, Depth),
|
|
5211
|
+
trace_goal_call(SessionId, Goal, Depth),
|
|
5212
|
+
( catch(call(Current:Goal), _, fail)
|
|
5213
|
+
-> trace_goal_exit(SessionId, Goal, Depth), StateOut = StateIn
|
|
5214
|
+
; trace_goal_fail(SessionId, Goal, Depth), fail
|
|
5215
|
+
)
|
|
5216
|
+
; fail
|
|
5217
|
+
).
|
|
5218
|
+
|
|
3622
5219
|
%% add_trace_entry(+SessionId, +Type, +Predicate, +Args, +Depth)
|
|
3623
5220
|
%% Add a trace entry if tracing is enabled
|
|
3624
5221
|
add_trace_entry(SessionId, Type, Predicate, Args, Depth) :-
|
|
5222
|
+
add_trace_entry_result(SessionId, Type, Predicate, Args, none, Depth).
|
|
5223
|
+
|
|
5224
|
+
%% add_trace_entry_result(+SessionId, +Type, +Predicate, +Args, +ResultOrNone, +Depth)
|
|
5225
|
+
%% Indexed, constant-time append with a hard cap. The list-backed log was
|
|
5226
|
+
%% quadratic for long loops; this keeps a bounded store and can still be
|
|
5227
|
+
%% serialized in order.
|
|
5228
|
+
add_trace_entry_result(SessionId, Type, Predicate, Args, ResultOpt, Depth) :-
|
|
3625
5229
|
( session_trace_enabled(SessionId, true)
|
|
3626
5230
|
-> get_time(Now),
|
|
3627
|
-
Timestamp is round(Now * 1000),
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
5231
|
+
Timestamp is round(Now * 1000),
|
|
5232
|
+
( ResultOpt = none
|
|
5233
|
+
-> Entry0 = trace{timestamp: Timestamp, type: Type, predicate: Predicate, args: Args, depth: Depth}
|
|
5234
|
+
; ResultOpt = some(Result),
|
|
5235
|
+
Entry0 = trace{timestamp: Timestamp, type: Type, predicate: Predicate, args: Args, result: Result, depth: Depth}
|
|
5236
|
+
),
|
|
5237
|
+
add_trace_scope(Entry0, Entry),
|
|
5238
|
+
append_trace_entry(SessionId, Entry)
|
|
3633
5239
|
; true
|
|
3634
5240
|
).
|
|
3635
5241
|
|
|
3636
|
-
%%
|
|
3637
|
-
%%
|
|
3638
|
-
|
|
5242
|
+
%% add_trace_scope(+EntryIn, -EntryOut)
|
|
5243
|
+
%% Attach the current tool/workflow scope, so the UI can nest MI calls under
|
|
5244
|
+
%% the host tool invocation that produced them.
|
|
5245
|
+
add_trace_scope(Entry, EntryOut) :-
|
|
5246
|
+
get_session_id(SessionId),
|
|
5247
|
+
( session_trace_scope(SessionId, [Scope|_])
|
|
5248
|
+
-> put_dict(scope, Entry, Scope, EntryOut)
|
|
5249
|
+
; EntryOut = Entry
|
|
5250
|
+
).
|
|
5251
|
+
|
|
5252
|
+
%% append_trace_entry(+SessionId, +Entry)
|
|
5253
|
+
%% Bounded, indexed append used by normal MI entries and host tool roots.
|
|
5254
|
+
append_trace_entry(SessionId, Entry) :-
|
|
5255
|
+
( session_trace_count(SessionId, Count)
|
|
5256
|
+
-> true
|
|
5257
|
+
; Count = 0,
|
|
5258
|
+
assertz(session_trace_count(SessionId, 0))
|
|
5259
|
+
),
|
|
5260
|
+
Max = 50000,
|
|
5261
|
+
( Count < Max
|
|
5262
|
+
-> assertz(session_trace_entry(SessionId, Count, Entry)),
|
|
5263
|
+
Next is Count + 1,
|
|
5264
|
+
retractall(session_trace_count(SessionId, _)),
|
|
5265
|
+
assertz(session_trace_count(SessionId, Next))
|
|
5266
|
+
; true
|
|
5267
|
+
).
|
|
5268
|
+
|
|
5269
|
+
%% repl_trace_tool_start(+SessionId, +Id, +Name, +ArgsJson)
|
|
5270
|
+
repl_trace_tool_start(SessionId, Id, Name, ArgsJson) :-
|
|
3639
5271
|
( session_trace_enabled(SessionId, true)
|
|
3640
5272
|
-> get_time(Now),
|
|
3641
5273
|
Timestamp is round(Now * 1000),
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
|
|
5274
|
+
( session_trace_scope(SessionId, [Parent|_])
|
|
5275
|
+
-> put_dict(scope, trace{timestamp: Timestamp, type: tool, predicate: Name,
|
|
5276
|
+
args: [ArgsJson], depth: 0, id: Id, scope: Parent}, Entry)
|
|
5277
|
+
; Entry = trace{timestamp: Timestamp, type: tool, predicate: Name,
|
|
5278
|
+
args: [ArgsJson], depth: 0, id: Id}
|
|
5279
|
+
),
|
|
5280
|
+
append_trace_entry(SessionId, Entry),
|
|
5281
|
+
( session_trace_scope(SessionId, Stack)
|
|
5282
|
+
-> retractall(session_trace_scope(SessionId, _)),
|
|
5283
|
+
assertz(session_trace_scope(SessionId, [Id|Stack]))
|
|
5284
|
+
; assertz(session_trace_scope(SessionId, [Id]))
|
|
5285
|
+
)
|
|
5286
|
+
; true
|
|
5287
|
+
).
|
|
5288
|
+
|
|
5289
|
+
%% repl_trace_tool_end(+SessionId, +Id, +ResultJson)
|
|
5290
|
+
repl_trace_tool_end(SessionId, Id, ResultJson) :-
|
|
5291
|
+
( session_trace_enabled(SessionId, true)
|
|
5292
|
+
-> ( session_trace_scope(SessionId, Stack),
|
|
5293
|
+
select(Id, Stack, Rest)
|
|
5294
|
+
-> retractall(session_trace_scope(SessionId, _)),
|
|
5295
|
+
( Rest = [] -> true ; assertz(session_trace_scope(SessionId, Rest)) )
|
|
5296
|
+
; true
|
|
5297
|
+
),
|
|
5298
|
+
get_time(Now),
|
|
5299
|
+
Timestamp is round(Now * 1000),
|
|
5300
|
+
Entry = trace{timestamp: Timestamp, type: exit, predicate: tool,
|
|
5301
|
+
args: [], result: ResultJson, depth: 0, scope: Id},
|
|
5302
|
+
append_trace_entry(SessionId, Entry)
|
|
3647
5303
|
; true
|
|
3648
5304
|
).
|
|
3649
5305
|
|
|
5306
|
+
%% reset_trace_store(+SessionId)
|
|
5307
|
+
reset_trace_store(SessionId) :-
|
|
5308
|
+
retractall(session_trace_entry(SessionId, _, _)),
|
|
5309
|
+
retractall(session_trace_count(SessionId, _)),
|
|
5310
|
+
retractall(session_trace_scope(SessionId, _)),
|
|
5311
|
+
retractall(session_trace_log(SessionId, _)).
|
|
5312
|
+
|
|
5313
|
+
%% session_trace_list(+SessionId, -Entries)
|
|
5314
|
+
session_trace_list(SessionId, Entries) :-
|
|
5315
|
+
findall(Entry, session_trace_entry(SessionId, _, Entry), Entries).
|
|
5316
|
+
|
|
5317
|
+
%% add_trace_with_result(+SessionId, +Type, +Predicate, +Args, +Result, +Depth)
|
|
5318
|
+
%% Add a trace entry with a result field
|
|
5319
|
+
add_trace_with_result(SessionId, Type, Predicate, Args, Result, Depth) :-
|
|
5320
|
+
add_trace_entry_result(SessionId, Type, Predicate, Args, some(Result), Depth).
|
|
5321
|
+
|
|
5322
|
+
%% goal_trace_parts(+Goal, -Predicate, -Args)
|
|
5323
|
+
goal_trace_parts(Goal, Predicate, Args) :-
|
|
5324
|
+
( compound(Goal)
|
|
5325
|
+
-> Goal =.. [Predicate|Args]
|
|
5326
|
+
; Predicate = Goal, Args = []
|
|
5327
|
+
).
|
|
5328
|
+
|
|
5329
|
+
trace_goal_call(SessionId, Goal, Depth) :-
|
|
5330
|
+
goal_trace_parts(Goal, Predicate, Args),
|
|
5331
|
+
add_trace_entry(SessionId, call, Predicate, Args, Depth).
|
|
5332
|
+
|
|
5333
|
+
trace_goal_exit(SessionId, Goal, Depth) :-
|
|
5334
|
+
goal_trace_parts(Goal, Predicate, Args),
|
|
5335
|
+
copy_term(Goal, GoalOut),
|
|
5336
|
+
add_trace_with_result(SessionId, exit, Predicate, Args, GoalOut, Depth).
|
|
5337
|
+
|
|
5338
|
+
trace_goal_fail(SessionId, Goal, Depth) :-
|
|
5339
|
+
goal_trace_parts(Goal, Predicate, Args),
|
|
5340
|
+
add_trace_entry(SessionId, fail, Predicate, Args, Depth).
|
|
5341
|
+
|
|
5342
|
+
%% repl_trace_clear(+SessionId)
|
|
5343
|
+
repl_trace_clear(SessionId) :-
|
|
5344
|
+
reset_trace_store(SessionId).
|
|
5345
|
+
|
|
5346
|
+
%% JSON-safe trace. Terms are rendered as text; arbitrary DML terms/variables
|
|
5347
|
+
%% are not JSON values.
|
|
5348
|
+
repl_trace_json(SessionId, Json) :-
|
|
5349
|
+
session_trace_list(SessionId, Log),
|
|
5350
|
+
maplist(trace_entry_json, Log, Entries),
|
|
5351
|
+
atom_json_dict(Json, Entries, []).
|
|
5352
|
+
|
|
5353
|
+
trace_entry_json(Trace, Dict) :-
|
|
5354
|
+
is_dict(Trace),
|
|
5355
|
+
get_dict(timestamp, Trace, Timestamp),
|
|
5356
|
+
get_dict(type, Trace, Type),
|
|
5357
|
+
get_dict(predicate, Trace, Predicate),
|
|
5358
|
+
get_dict(args, Trace, Args),
|
|
5359
|
+
get_dict(depth, Trace, Depth),
|
|
5360
|
+
term_string(Args, ArgsText),
|
|
5361
|
+
Base = _{timestamp: Timestamp, type: Type, predicate: Predicate, depth: Depth, argsText: ArgsText},
|
|
5362
|
+
( get_dict(result, Trace, Result)
|
|
5363
|
+
-> term_string(Result, ResultText),
|
|
5364
|
+
put_dict(resultText, Base, ResultText, WithResult)
|
|
5365
|
+
; WithResult = Base
|
|
5366
|
+
),
|
|
5367
|
+
( get_dict(id, Trace, Id0)
|
|
5368
|
+
-> put_dict(id, WithResult, Id0, WithId)
|
|
5369
|
+
; WithId = WithResult
|
|
5370
|
+
),
|
|
5371
|
+
( get_dict(scope, Trace, Scope)
|
|
5372
|
+
-> put_dict(scope, WithId, Scope, Dict)
|
|
5373
|
+
; Dict = WithId
|
|
5374
|
+
).
|
|
5375
|
+
|
|
3650
5376
|
%% ============================================================
|
|
3651
5377
|
%% Agent Signal Loop - SIMPLIFIED
|
|
3652
5378
|
%% ============================================================
|
|
@@ -3726,8 +5452,9 @@ call_tool_inline(SessionId, ToolName, Args, StateIn, StateOut, Result) :-
|
|
|
3726
5452
|
ToolHead = ToolPattern,
|
|
3727
5453
|
% Execute the tool body through MI
|
|
3728
5454
|
% Wrap in catch to handle answer() commits - answer() yields and then throws
|
|
5455
|
+
mi_lower_cut_body(Body, LoweredBody),
|
|
3729
5456
|
catch(
|
|
3730
|
-
( mi_call(
|
|
5457
|
+
( mi_call(LoweredBody, StateIn, StateOut)
|
|
3731
5458
|
-> % Collect result from result/1 facts or output args
|
|
3732
5459
|
% Use catch to handle case where result/2 doesn't exist
|
|
3733
5460
|
( catch(SessionId:result(ToolNameAtom, R), _, fail)
|
|
@@ -3784,10 +5511,13 @@ create_tool_engine(SessionId, ToolName, Args, ToolEngineId) :-
|
|
|
3784
5511
|
(session_params(SessionId, Params) -> true ; Params = params{}),
|
|
3785
5512
|
InitialState = state{memory: [], params: Params, context_stack: [], depth: 0},
|
|
3786
5513
|
% Create goal that will yield the result
|
|
5514
|
+
% A tool body is interpreted (it may call exec/exec-free host code); lower
|
|
5515
|
+
% any cut so it commits the body's prefix instead of doing nothing.
|
|
5516
|
+
mi_lower_cut_body(Body, LoweredBody),
|
|
3787
5517
|
Goal = (
|
|
3788
5518
|
nb_setval(current_session_id, SessionId),
|
|
3789
5519
|
catch(
|
|
3790
|
-
( mi_call(
|
|
5520
|
+
( mi_call(LoweredBody, InitialState, _FinalState),
|
|
3791
5521
|
% A tool that declares an output but leaves it unbound has no
|
|
3792
5522
|
% result. `extract_tool_result_simple/4` maps that to `true`, which
|
|
3793
5523
|
% is how a `tool(fs_write(Path, Content), …)` (content taken for the
|
|
@@ -3984,6 +5714,7 @@ mi_call(task(Desc), StateIn, StateOut) :-
|
|
|
3984
5714
|
interpolate_desc(Desc, Params, InterpDesc),
|
|
3985
5715
|
get_session_id(SessionId),
|
|
3986
5716
|
get_depth(StateIn, Depth),
|
|
5717
|
+
add_trace_entry(SessionId, call, task, [InterpDesc], Depth),
|
|
3987
5718
|
add_trace_entry(SessionId, llm_call, task, [InterpDesc], Depth),
|
|
3988
5719
|
collect_user_tools(SessionId, UserTools),
|
|
3989
5720
|
get_memory(StateIn, Memory),
|
|
@@ -4004,6 +5735,7 @@ mi_call(task(Desc), StateIn, StateOut) :-
|
|
|
4004
5735
|
-> format(atom(WarnMsg), 'Warning: task/1 failed: ~w', [ErrorMsg])
|
|
4005
5736
|
; format(atom(WarnMsg), 'Warning: task/1 failed (success=false)', [])
|
|
4006
5737
|
),
|
|
5738
|
+
add_trace_entry(SessionId, fail, task, [InterpDesc], Depth),
|
|
4007
5739
|
engine_yield(output(WarnMsg)),
|
|
4008
5740
|
fail
|
|
4009
5741
|
),
|
|
@@ -4060,6 +5792,7 @@ mi_call(prompt(Desc), StateIn, StateOut) :-
|
|
|
4060
5792
|
interpolate_desc(Desc, Params, InterpDesc),
|
|
4061
5793
|
get_session_id(SessionId),
|
|
4062
5794
|
get_depth(StateIn, Depth),
|
|
5795
|
+
add_trace_entry(SessionId, call, prompt, [InterpDesc], Depth),
|
|
4063
5796
|
add_trace_entry(SessionId, llm_call, prompt, [InterpDesc], Depth),
|
|
4064
5797
|
% prompt/N is a simple LLM call - no tools, no tool scope
|
|
4065
5798
|
get_model_spec(StateIn, ModelSpec),
|
|
@@ -4145,6 +5878,7 @@ mi_call(chat(Message, Reply), StateIn, StateOut) :-
|
|
|
4145
5878
|
interpolate_desc(Message, Params, InterpMessage),
|
|
4146
5879
|
get_session_id(SessionId),
|
|
4147
5880
|
get_depth(StateIn, Depth),
|
|
5881
|
+
add_trace_entry(SessionId, call, chat, [InterpMessage], Depth),
|
|
4148
5882
|
add_trace_entry(SessionId, llm_call, chat, [InterpMessage], Depth),
|
|
4149
5883
|
collect_user_tools(SessionId, UserTools),
|
|
4150
5884
|
% The message is NOT added to memory here: the loop appends it as the turn's
|
|
@@ -4339,11 +6073,13 @@ mi_call_judgment(State, Specs, StateIn, StateOut) :-
|
|
|
4339
6073
|
add_trace_entry(SessionId, llm_call, judgment, [Count], Depth),
|
|
4340
6074
|
get_judge_backend(State1, Backend),
|
|
4341
6075
|
get_required_judgments(State1, Requires),
|
|
6076
|
+
get_model_spec(State1, ModelSpec),
|
|
4342
6077
|
engine_yield(request_judgment(payload{
|
|
4343
6078
|
state: State,
|
|
4344
6079
|
questions: Questions,
|
|
4345
6080
|
backend: Backend,
|
|
4346
|
-
requires: Requires
|
|
6081
|
+
requires: Requires,
|
|
6082
|
+
modelSpec: ModelSpec
|
|
4347
6083
|
})),
|
|
4348
6084
|
wait_judgment_signal(SessionId),
|
|
4349
6085
|
( session_judgment_result(SessionId, Result)
|
|
@@ -4480,6 +6216,7 @@ mi_call_task_n(Desc, Vars, VarNames, StateIn, StateOut) :-
|
|
|
4480
6216
|
interpolate_desc(Desc, Params, InterpDesc),
|
|
4481
6217
|
get_session_id(SessionId),
|
|
4482
6218
|
get_depth(StateIn, Depth),
|
|
6219
|
+
add_trace_entry(SessionId, call, task, [InterpDesc], Depth),
|
|
4483
6220
|
add_trace_entry(SessionId, llm_call, task, [InterpDesc], Depth),
|
|
4484
6221
|
collect_user_tools(SessionId, UserTools),
|
|
4485
6222
|
get_memory(StateIn, Memory),
|
|
@@ -4502,6 +6239,7 @@ mi_call_task_n(Desc, Vars, VarNames, StateIn, StateOut) :-
|
|
|
4502
6239
|
-> format(atom(WarnMsg), 'Warning: task/~w failed: ~w', [ActualArity, ErrorMsg])
|
|
4503
6240
|
; format(atom(WarnMsg), 'Warning: task/~w failed (success=false)', [ActualArity])
|
|
4504
6241
|
),
|
|
6242
|
+
add_trace_entry(SessionId, fail, task, [InterpDesc], Depth),
|
|
4505
6243
|
engine_yield(output(WarnMsg)),
|
|
4506
6244
|
fail
|
|
4507
6245
|
),
|
|
@@ -4546,12 +6284,22 @@ mi_call_task_n(Desc, Vars, VarNames, StateIn, StateOut) :-
|
|
|
4546
6284
|
%%
|
|
4547
6285
|
%% task("From the front matter extract every author: {Text}",
|
|
4548
6286
|
%% list(schema(author, Authors))).
|
|
4549
|
-
%% schema_fill(+Fields, +Value, -Filled): the pure part
|
|
4550
|
-
%% usable without a session. The session predicate
|
|
4551
|
-
%% up from `schema/2`.
|
|
6287
|
+
%% schema_fill(+Fields, +Value, -Filled): the pure part (primitive field types),
|
|
6288
|
+
%% so it is testable and usable without a session. The session predicate
|
|
6289
|
+
%% schema_fill/3 looks the fields up from `schema/2`.
|
|
4552
6290
|
schema_fill_fields(Fields, Value, Filled) :-
|
|
6291
|
+
schema_fill_fields(none, Fields, Value, Filled).
|
|
6292
|
+
|
|
6293
|
+
%% schema_fill_fields(+SessionOrNone, +Fields, +Value, -Filled)
|
|
6294
|
+
%%
|
|
6295
|
+
%% Field types: a primitive (`string`, `integer`, `number`, `boolean`, `object`,
|
|
6296
|
+
%% `array`), `list(Type)`, or `schema(Name)` / `schema(Name, _)` for a nested
|
|
6297
|
+
%% object. Nested schemas are filled and validated recursively; before this,
|
|
6298
|
+
%% `list(schema(part, _))` was an unknown type and every value was rejected —
|
|
6299
|
+
%% including `[]`.
|
|
6300
|
+
schema_fill_fields(Session, Fields, Value, Filled) :-
|
|
4553
6301
|
is_dict(Value),
|
|
4554
|
-
( member(field(Field,
|
|
6302
|
+
( member(field(Field, _Type, required), Fields),
|
|
4555
6303
|
\+ dict_field(Value, Field, _),
|
|
4556
6304
|
!,
|
|
4557
6305
|
format(atom(Msg), 'schema: required field "~w" is missing', [Field]),
|
|
@@ -4560,7 +6308,7 @@ schema_fill_fields(Fields, Value, Filled) :-
|
|
|
4560
6308
|
),
|
|
4561
6309
|
( member(field(Field, Type, _), Fields),
|
|
4562
6310
|
dict_field(Value, Field, Found),
|
|
4563
|
-
\+ schema_value_ok(Type, Found),
|
|
6311
|
+
\+ schema_value_ok(Session, Type, Found),
|
|
4564
6312
|
!,
|
|
4565
6313
|
format(atom(Msg), 'schema: field "~w" is not ~w (~w)', [Field, Type, Found]),
|
|
4566
6314
|
throw(error(schema_error(Msg), schema_fill/3))
|
|
@@ -4569,10 +6317,9 @@ schema_fill_fields(Fields, Value, Filled) :-
|
|
|
4569
6317
|
dict_pairs(Value, _, Pairs),
|
|
4570
6318
|
findall(Key-NewValue,
|
|
4571
6319
|
( member(Key-Old, Pairs),
|
|
4572
|
-
( member(field(Key,
|
|
4573
|
-
-> ( Option = default(Default), Old == "" ->
|
|
4574
|
-
|
|
4575
|
-
)
|
|
6320
|
+
( member(field(Key, Type, Option), Fields)
|
|
6321
|
+
-> ( Option = default(Default), Old == "" -> Base = Default ; Base = Old ),
|
|
6322
|
+
schema_fill_value(Session, Type, Base, NewValue)
|
|
4576
6323
|
; NewValue = Old % undeclared: kept, so it stays visible
|
|
4577
6324
|
)
|
|
4578
6325
|
),
|
|
@@ -4585,15 +6332,57 @@ schema_fill_fields(Fields, Value, Filled) :-
|
|
|
4585
6332
|
append(Updated, Missing, All),
|
|
4586
6333
|
dict_pairs(Filled, _, All).
|
|
4587
6334
|
|
|
6335
|
+
%% schema_fill_value(+SessionOrNone, +Type, +Value, -Filled)
|
|
6336
|
+
schema_fill_value(Session, list(Inner), Value, Filled) :-
|
|
6337
|
+
!,
|
|
6338
|
+
( is_list(Value)
|
|
6339
|
+
-> maplist(schema_fill_value(Session, Inner), Value, Filled)
|
|
6340
|
+
; Filled = Value
|
|
6341
|
+
).
|
|
6342
|
+
schema_fill_value(Session, schema(Name), Value, Filled) :-
|
|
6343
|
+
!,
|
|
6344
|
+
schema_fill_nested(Session, Name, Value, Filled).
|
|
6345
|
+
schema_fill_value(Session, schema(Name, _), Value, Filled) :-
|
|
6346
|
+
!,
|
|
6347
|
+
schema_fill_nested(Session, Name, Value, Filled).
|
|
6348
|
+
schema_fill_value(_, _, Value, Value).
|
|
6349
|
+
|
|
6350
|
+
schema_fill_nested(none, _, Value, Value) :- !.
|
|
6351
|
+
schema_fill_nested(Session, Name, Value, Filled) :-
|
|
6352
|
+
( is_dict(Value)
|
|
6353
|
+
-> schema_fields_of(Session, Name, Fields),
|
|
6354
|
+
schema_fill_fields(Session, Fields, Value, Filled)
|
|
6355
|
+
; Filled = Value
|
|
6356
|
+
).
|
|
6357
|
+
|
|
4588
6358
|
%% A declared type accepts its own shape; anything coarser (a number for a string
|
|
4589
6359
|
%% field, for instance) fails, because silently coercing hides the model's error.
|
|
4590
|
-
schema_value_ok(string, V) :- ( string(V) ; atom(V) ), !.
|
|
4591
|
-
schema_value_ok(integer, V) :- integer(V), !.
|
|
4592
|
-
schema_value_ok(number, V) :- number(V), !.
|
|
4593
|
-
schema_value_ok(boolean, V) :- ( V == true ; V == false ; V == yes ; V == no ), !.
|
|
4594
|
-
schema_value_ok(object, V) :- is_dict(V), !.
|
|
4595
|
-
schema_value_ok(array, V) :- is_list(V), !.
|
|
4596
|
-
schema_value_ok(
|
|
6360
|
+
schema_value_ok(_, string, V) :- ( string(V) ; atom(V) ), !.
|
|
6361
|
+
schema_value_ok(_, integer, V) :- integer(V), !.
|
|
6362
|
+
schema_value_ok(_, number, V) :- number(V), !.
|
|
6363
|
+
schema_value_ok(_, boolean, V) :- ( V == true ; V == false ; V == yes ; V == no ), !.
|
|
6364
|
+
schema_value_ok(_, object, V) :- is_dict(V), !.
|
|
6365
|
+
schema_value_ok(_, array, V) :- is_list(V), !.
|
|
6366
|
+
schema_value_ok(Session, list(Inner), V) :-
|
|
6367
|
+
!,
|
|
6368
|
+
is_list(V),
|
|
6369
|
+
maplist(schema_value_ok(Session, Inner), V).
|
|
6370
|
+
schema_value_ok(Session, schema(Name), V) :-
|
|
6371
|
+
!,
|
|
6372
|
+
schema_value_ok_nested(Session, Name, V).
|
|
6373
|
+
schema_value_ok(Session, schema(Name, _), V) :-
|
|
6374
|
+
!,
|
|
6375
|
+
schema_value_ok_nested(Session, Name, V).
|
|
6376
|
+
schema_value_ok(none, list(_), V) :- is_list(V), !.
|
|
6377
|
+
schema_value_ok(none, schema(_), V) :- is_dict(V), !.
|
|
6378
|
+
schema_value_ok(none, schema(_, _), V) :- is_dict(V), !.
|
|
6379
|
+
schema_value_ok(_, _, _) :- fail.
|
|
6380
|
+
|
|
6381
|
+
schema_value_ok_nested(none, _, V) :- is_dict(V), !.
|
|
6382
|
+
schema_value_ok_nested(Session, Name, V) :-
|
|
6383
|
+
is_dict(V),
|
|
6384
|
+
schema_fields_of(Session, Name, Fields),
|
|
6385
|
+
catch((schema_fill_fields(Session, Fields, V, _), true), _, fail).
|
|
4597
6386
|
|
|
4598
6387
|
%% bind_task_variables(+VarsDict, +Names, -Values)
|
|
4599
6388
|
bind_task_variables(_, _, [], []) :- !.
|
|
@@ -4609,8 +6398,8 @@ bind_task_variables(SessionId, VarsDict, [N|Names], [Value|Values]) :-
|
|
|
4609
6398
|
( is_dict(N), get_dict(schema, N, SchemaName), nonvar(Raw)
|
|
4610
6399
|
-> schema_fields_of(SessionId, SchemaName, Fields),
|
|
4611
6400
|
( is_list(Raw)
|
|
4612
|
-
->
|
|
4613
|
-
; schema_fill_fields(Fields, Raw, Value)
|
|
6401
|
+
-> schema_fill_list(SessionId, Fields, Raw, Value)
|
|
6402
|
+
; schema_fill_fields(SessionId, Fields, Raw, Value)
|
|
4614
6403
|
)
|
|
4615
6404
|
; Value = Raw
|
|
4616
6405
|
),
|
|
@@ -4643,18 +6432,36 @@ resolve_schema_field(SessionId, VarName, Resolved) :-
|
|
|
4643
6432
|
|
|
4644
6433
|
%% Field descriptors as dicts, so they cross the bridge without a decoder.
|
|
4645
6434
|
%% `required` is 1/0 rather than an atom: the atom `false` arrives as the string
|
|
4646
|
-
%% "false", which is truthy in JavaScript.
|
|
6435
|
+
%% "false", which is truthy in JavaScript. A nested schema carries its own
|
|
6436
|
+
%% `fields`, so the description the nested agent reads includes them.
|
|
4647
6437
|
schema_field_dicts(SessionId, SchemaName, Dicts) :-
|
|
4648
6438
|
catch(clause(SessionId:schema(SchemaName, Fields), true), _, fail),
|
|
4649
|
-
findall(x{name: Field, type: Type,
|
|
4650
|
-
|
|
6439
|
+
findall(x{name: Field, type: Type, itemType: ItemType, required: Required,
|
|
6440
|
+
default: Default, fields: Nested},
|
|
6441
|
+
( member(field(Field, TypeTerm, Option), Fields),
|
|
6442
|
+
field_type_dict(SessionId, TypeTerm, Type, ItemType, Nested),
|
|
4651
6443
|
( Option == required -> Required = 1 ; Required = 0 ),
|
|
4652
6444
|
( Option = default(D) -> Default = D ; Default = no_default )
|
|
4653
6445
|
),
|
|
4654
6446
|
Dicts).
|
|
4655
6447
|
|
|
4656
|
-
|
|
4657
|
-
|
|
6448
|
+
%% field_type_dict(+Session, +TypeTerm, -Type, -ItemType, -Fields)
|
|
6449
|
+
field_type_dict(Session, list(Inner), array, ItemType, Nested) :-
|
|
6450
|
+
!,
|
|
6451
|
+
field_type_dict(Session, Inner, ItemType, _, Nested).
|
|
6452
|
+
field_type_dict(Session, schema(Name), object, none, Nested) :-
|
|
6453
|
+
!,
|
|
6454
|
+
schema_field_dicts(Session, Name, Nested).
|
|
6455
|
+
field_type_dict(Session, schema(Name, _), object, none, Nested) :-
|
|
6456
|
+
!,
|
|
6457
|
+
schema_field_dicts(Session, Name, Nested).
|
|
6458
|
+
field_type_dict(_, Type, Type, none, []).
|
|
6459
|
+
|
|
6460
|
+
%% schema_fill_list(+Session, +Fields, +Values, -Filled)
|
|
6461
|
+
schema_fill_list(_, _, [], []).
|
|
6462
|
+
schema_fill_list(Session, Fields, [Raw|Raws], [Value|Values]) :-
|
|
6463
|
+
schema_fill_fields(Session, Fields, Raw, Value),
|
|
6464
|
+
schema_fill_list(Session, Fields, Raws, Values).
|
|
4658
6465
|
|
|
4659
6466
|
%% ============================================================
|
|
4660
6467
|
%% Exec Handling
|
|
@@ -4678,7 +6485,92 @@ mi_call(exec(ToolCall, Output), StateIn, StateOut) :-
|
|
|
4678
6485
|
add_trace_with_result(SessionId, exit, ToolName, Args, Output, Depth),
|
|
4679
6486
|
StateOut = State0
|
|
4680
6487
|
; add_trace_entry(SessionId, fail, ToolName, Args, Depth),
|
|
4681
|
-
|
|
6488
|
+
% A failed host call used to be a plain failure. `exec(bash(...))` in the
|
|
6489
|
+
% browser therefore produced no bindings and no message at all: the goal
|
|
6490
|
+
% just failed, which reads like "the predicate did not match" instead of
|
|
6491
|
+
% "there is no such host tool". Surface the host's reason as an error.
|
|
6492
|
+
% post_exec_result/3 stores the reason under `result`, not `error`.
|
|
6493
|
+
% Only a *missing* tool becomes an error: that is the case that used to
|
|
6494
|
+
% fail with no explanation at all (`exec(bash(...))` in the browser).
|
|
6495
|
+
% A tool that ran and failed stays a plain failure, so `catch/3` and
|
|
6496
|
+
% `\+` around `exec` keep working.
|
|
6497
|
+
( catch(get_dict(result, Result, Reason0), _, fail),
|
|
6498
|
+
format(string(Reason), '~w', [Reason0]),
|
|
6499
|
+
( memberchk(ToolName, [agent_send,agent_recipients,agent_broadcast,run_workflow]) ; sub_string(Reason, _, _, _, "Tool not found") )
|
|
6500
|
+
-> throw(error(tool_error(ToolName, Reason), context(exec/2, Reason)))
|
|
6501
|
+
; fail
|
|
6502
|
+
)
|
|
6503
|
+
).
|
|
6504
|
+
|
|
6505
|
+
%% ============================================================
|
|
6506
|
+
%% Scripts: one helper instead of five goals of glue
|
|
6507
|
+
%% ============================================================
|
|
6508
|
+
%%
|
|
6509
|
+
%% run_script(+ScriptName, +Args, -Value)
|
|
6510
|
+
%%
|
|
6511
|
+
%% Resolve a `script_file(Name, Path)` link, convert the arguments to the strings
|
|
6512
|
+
%% `argv` takes, run it, check `exitCode`, and parse stdout as JSON when it is
|
|
6513
|
+
%% JSON. Every Python-backed skill otherwise repeats `script_file`, a numeric
|
|
6514
|
+
%% conversion, `exec`, `get_dict(exitCode)`, `get_dict(stdout)` and
|
|
6515
|
+
%% `atom_json_dict` — and the version that forgets the exit-code check is the one
|
|
6516
|
+
%% that reports fabricated results.
|
|
6517
|
+
mi_call(run_script(Name, Args, Value), StateIn, StateOut) :-
|
|
6518
|
+
!,
|
|
6519
|
+
get_session_id(SessionId),
|
|
6520
|
+
( atom(Name),
|
|
6521
|
+
current_predicate(SessionId:script_file/2),
|
|
6522
|
+
SessionId:script_file(Name, Path)
|
|
6523
|
+
-> true
|
|
6524
|
+
; atom(Name)
|
|
6525
|
+
-> throw(error(existence_error(script, Name),
|
|
6526
|
+
context(run_script/3,
|
|
6527
|
+
'no script_file(Name, Path) links that name; declare script_file/2 and create the file first')))
|
|
6528
|
+
; throw(error(type_error(atom, Name), context(run_script/3, 'the script name must be an atom')))
|
|
6529
|
+
),
|
|
6530
|
+
( is_list(Args)
|
|
6531
|
+
-> maplist(script_arg_string, Args, Argv)
|
|
6532
|
+
; throw(error(type_error(list, Args),
|
|
6533
|
+
context(run_script/3, 'pass the script arguments as a list')))
|
|
6534
|
+
),
|
|
6535
|
+
script_exec(Path, Argv, Result, StateIn, State1),
|
|
6536
|
+
( catch(get_dict(exitCode, Result, 0), _, fail)
|
|
6537
|
+
-> get_dict(stdout, Result, Out),
|
|
6538
|
+
( catch(atom_json_dict(Out, Parsed, []), _, fail)
|
|
6539
|
+
-> Value = Parsed
|
|
6540
|
+
; Value = Out
|
|
6541
|
+
),
|
|
6542
|
+
StateOut = State1
|
|
6543
|
+
; ( catch(get_dict(stderr, Result, Err), _, fail) -> true ; Err = "script failed" ),
|
|
6544
|
+
throw(error(script_failed(Name, Err), context(run_script/3, Err)))
|
|
6545
|
+
).
|
|
6546
|
+
|
|
6547
|
+
%% script_exec(+Path, +Argv, -Result, +StateIn, -StateOut)
|
|
6548
|
+
%% The capability tool differs by embedding: the browser registers `python`, the
|
|
6549
|
+
%% CLI has a shell. Try Python first and fall back to running the file, so one
|
|
6550
|
+
%% `run_script/3` works in both places. A missing tool is the catchable
|
|
6551
|
+
%% `tool_error` raised by `exec/2`; anything else is the script's own failure.
|
|
6552
|
+
script_exec(Path, Argv, Result, StateIn, State1) :-
|
|
6553
|
+
( catch(mi_call(exec(python(script: Path, argv: Argv), Result), StateIn, State1),
|
|
6554
|
+
error(tool_error(_, _), _), fail)
|
|
6555
|
+
-> true
|
|
6556
|
+
; script_shell_command(Path, Argv, Command),
|
|
6557
|
+
mi_call(exec(bash(argv: Command), Result), StateIn, State1)
|
|
6558
|
+
).
|
|
6559
|
+
|
|
6560
|
+
%% script_shell_command(+Path, +Argv, -Command): execute a .py with python3,
|
|
6561
|
+
%% anything else directly (it needs a shebang or to be a shell script).
|
|
6562
|
+
script_shell_command(Path, Argv, [python3, Path|Argv]) :-
|
|
6563
|
+
sub_atom(Path, _, _, 0, '.py'),
|
|
6564
|
+
!.
|
|
6565
|
+
script_shell_command(Path, Argv, [Path|Argv]).
|
|
6566
|
+
|
|
6567
|
+
%% script_arg_string(+Value, -String): argv entries are strings.
|
|
6568
|
+
script_arg_string(Value, String) :-
|
|
6569
|
+
( string(Value) -> String = Value
|
|
6570
|
+
; atom(Value) -> atom_string(Value, String)
|
|
6571
|
+
; number(Value) -> number_string(Value, String)
|
|
6572
|
+
; throw(error(type_error(atomic, Value),
|
|
6573
|
+
context(run_script/3, 'script arguments must be atoms, strings or numbers')))
|
|
4682
6574
|
).
|
|
4683
6575
|
|
|
4684
6576
|
%% ============================================================
|
|
@@ -4689,6 +6581,36 @@ mi_call(exec(ToolCall, Output), StateIn, StateOut) :-
|
|
|
4689
6581
|
%% Goals and messages cross the boundary as canonical term strings, so a
|
|
4690
6582
|
%% process never shares a variable with its spawner (invariant A).
|
|
4691
6583
|
|
|
6584
|
+
%% Work conversation references are distinct from process pids/mailboxes.
|
|
6585
|
+
mi_call(agent_self(Agent), StateIn, StateIn) :-
|
|
6586
|
+
!, get_session_id(SessionId), engine_yield(request_proc(agent_self)),
|
|
6587
|
+
get_next_signal(SessionId, _),
|
|
6588
|
+
retract(session_proc_result(SessionId, agent_self, Result)),
|
|
6589
|
+
( Result = ok(Agent) -> true ; throw(error(agent_inbox(Result), agent_self/1)) ).
|
|
6590
|
+
|
|
6591
|
+
mi_call(agent_post(Agent, Message, Id), StateIn, StateIn) :-
|
|
6592
|
+
!,
|
|
6593
|
+
( nonvar(Agent), Agent = agent_ref(Token), string(Token),
|
|
6594
|
+
acyclic_term(Message), is_dict(Message), repl_inbox_data(Message, 32)
|
|
6595
|
+
-> true ; throw(error(domain_error(agent_message, 'expected agent reference and data dict'), agent_post/3)) ),
|
|
6596
|
+
term_string(Agent, To),
|
|
6597
|
+
atom_json_dict(JsonAtom, Message, []), atom_string(JsonAtom, Json),
|
|
6598
|
+
get_session_id(SessionId), engine_yield(request_proc(agent_post(To, Json))),
|
|
6599
|
+
get_next_signal(SessionId, _),
|
|
6600
|
+
retract(session_proc_result(SessionId, agent_post, Result)),
|
|
6601
|
+
( Result = ok(Id) -> true ; throw(error(agent_inbox(Result), agent_post/3)) ).
|
|
6602
|
+
|
|
6603
|
+
%% Anonymous dict tags are harmless; values/keys must still be data, never goals.
|
|
6604
|
+
repl_inbox_data(Value, Depth) :-
|
|
6605
|
+
Depth > 0,
|
|
6606
|
+
( nonvar(Value), atomic(Value) -> hook_data(Value, Depth)
|
|
6607
|
+
; nonvar(Value), Next is Depth - 1,
|
|
6608
|
+
( is_list(Value) -> maplist(repl_inbox_data_at(Next), Value)
|
|
6609
|
+
; is_dict(Value), dict_pairs(Value, _, Pairs), maplist(repl_inbox_pair(Next), Pairs) )
|
|
6610
|
+
).
|
|
6611
|
+
repl_inbox_data_at(Depth, Value) :- repl_inbox_data(Value, Depth).
|
|
6612
|
+
repl_inbox_pair(Depth, Key-Value) :- atom(Key), repl_inbox_data(Value, Depth).
|
|
6613
|
+
|
|
4692
6614
|
%% mi_call(self(?Pid), +StateIn, -StateOut)
|
|
4693
6615
|
mi_call(self(Pid), StateIn, StateIn) :-
|
|
4694
6616
|
!,
|
|
@@ -4934,6 +6856,72 @@ mi_call(now_ms(Milliseconds), StateIn, StateIn) :-
|
|
|
4934
6856
|
get_time(Seconds),
|
|
4935
6857
|
Milliseconds is round(Seconds * 1000).
|
|
4936
6858
|
|
|
6859
|
+
%% ============================================================
|
|
6860
|
+
%% Time zones
|
|
6861
|
+
%% ============================================================
|
|
6862
|
+
%%
|
|
6863
|
+
%% This engine is WebAssembly without tzdata: `stamp_date_time/3` understands
|
|
6864
|
+
%% 'UTC' and explicit integer offsets, but not IANA zone names (and not a
|
|
6865
|
+
%% 'Local' pseudo-zone). The host does have that database, so these predicates
|
|
6866
|
+
%% resolve a zone to the DST-correct integer offset for one instant and then
|
|
6867
|
+
%% reuse the ordinary date/time term. Nothing here reads the filesystem,
|
|
6868
|
+
%% network or a model, and no host permission is involved.
|
|
6869
|
+
%%
|
|
6870
|
+
%% get_time(Stamp), zone_date_time(Stamp, 'Asia/Shanghai', DT)
|
|
6871
|
+
%% get_time(Stamp), zone_date_time(Stamp, 'America/New_York', DT)
|
|
6872
|
+
%% get_time(Stamp), local_date_time(Stamp, DT)
|
|
6873
|
+
%%
|
|
6874
|
+
%% `stamp_date_time/3` remains the primitive: these predicates only supply the
|
|
6875
|
+
%% offset it is missing. Use an explicit zone for reports whose audience is in
|
|
6876
|
+
%% that market; 'Local' follows the server's own zone and can move with DST.
|
|
6877
|
+
|
|
6878
|
+
%% mi_call(zone_offset(+Zone, +Stamp, -OffsetSeconds), +StateIn, -StateOut)
|
|
6879
|
+
mi_call(zone_offset(Zone, _Stamp, 0), StateIn, StateIn) :-
|
|
6880
|
+
Zone == 'UTC',
|
|
6881
|
+
!.
|
|
6882
|
+
mi_call(zone_offset(Zone, _Stamp, Zone), StateIn, StateIn) :-
|
|
6883
|
+
integer(Zone),
|
|
6884
|
+
!.
|
|
6885
|
+
mi_call(zone_offset(Zone, Stamp, Offset), StateIn, StateOut) :-
|
|
6886
|
+
!,
|
|
6887
|
+
( atom(Zone)
|
|
6888
|
+
-> true
|
|
6889
|
+
; throw(error(type_error(atom, Zone),
|
|
6890
|
+
context(zone_offset/3, 'pass an IANA zone atom such as Asia/Shanghai, America/New_York or Local')))
|
|
6891
|
+
),
|
|
6892
|
+
( number(Stamp)
|
|
6893
|
+
-> true
|
|
6894
|
+
; throw(error(type_error(number, Stamp),
|
|
6895
|
+
context(zone_offset/3, 'pass an epoch timestamp in seconds, normally from get_time/1')))
|
|
6896
|
+
),
|
|
6897
|
+
mi_call(exec(dc_timezone(zone: Zone, stamp: Stamp), Result), StateIn, StateOut),
|
|
6898
|
+
( is_dict(Result),
|
|
6899
|
+
get_dict(offset, Result, Found),
|
|
6900
|
+
integer(Found)
|
|
6901
|
+
-> Offset = Found
|
|
6902
|
+
; ( get_dict(error, Result, Why)
|
|
6903
|
+
-> true
|
|
6904
|
+
; Why = 'the host could not resolve that time zone'
|
|
6905
|
+
),
|
|
6906
|
+
throw(error(domain_error(timezone, Zone), context(zone_offset/3, Why)))
|
|
6907
|
+
).
|
|
6908
|
+
|
|
6909
|
+
%% mi_call(zone_date_time(+Stamp, +Zone, -DateTime), +StateIn, -StateOut)
|
|
6910
|
+
%%
|
|
6911
|
+
%% `zone_offset/3` reports seconds east of UTC (the IANA convention), while
|
|
6912
|
+
%% `stamp_date_time/3` takes its integer zone as seconds *west* of UTC, so the
|
|
6913
|
+
%% sign is flipped here. Callers never see that difference.
|
|
6914
|
+
mi_call(zone_date_time(Stamp, Zone, DateTime), StateIn, StateOut) :-
|
|
6915
|
+
!,
|
|
6916
|
+
mi_call(zone_offset(Zone, Stamp, East), StateIn, StateOut),
|
|
6917
|
+
West is -East,
|
|
6918
|
+
stamp_date_time(Stamp, DateTime, West).
|
|
6919
|
+
|
|
6920
|
+
%% mi_call(local_date_time(+Stamp, -DateTime), +StateIn, -StateOut)
|
|
6921
|
+
mi_call(local_date_time(Stamp, DateTime), StateIn, StateOut) :-
|
|
6922
|
+
!,
|
|
6923
|
+
mi_call(zone_date_time(Stamp, 'Local', DateTime), StateIn, StateOut).
|
|
6924
|
+
|
|
4937
6925
|
%% mi_call(serve(+Name, +Goal), +StateIn, -StateOut)
|
|
4938
6926
|
%% Spawn a session-scoped process and register it under Name. Sugar over
|
|
4939
6927
|
%% spawn/2 + register/2; the goal decides the process's shape.
|
|
@@ -5204,7 +7192,8 @@ mi_call(with_reasoning(Goal, Effort), StateIn, StateOut) :-
|
|
|
5204
7192
|
%% Run Goal with a specific model, and optionally a reasoning level. `Spec` is a
|
|
5205
7193
|
%% string — "provider:model", "provider:model#reasoning", "model",
|
|
5206
7194
|
%% "model#reasoning" — or a model{provider, id, temperature, maxTokens, reasoning}
|
|
5207
|
-
%% dict. It applies to task/N, prompt/N
|
|
7195
|
+
%% dict. It applies to task/N, prompt/N, sample_token/N and LLM-backed judgments.
|
|
7196
|
+
%% A judge backend with its own fixed model (for example Jev) keeps that model.
|
|
5208
7197
|
mi_call(with_model(Spec, Goal), StateIn, StateOut) :-
|
|
5209
7198
|
!,
|
|
5210
7199
|
set_model_state(StateIn, Spec, ScopedState),
|
|
@@ -5391,7 +7380,7 @@ mi_call(output(Media), StateIn, StateIn) :-
|
|
|
5391
7380
|
nonvar(Media),
|
|
5392
7381
|
( Media = media(_, _)
|
|
5393
7382
|
; Media = media(_)
|
|
5394
|
-
; is_dict(Media)
|
|
7383
|
+
; is_dict(Media), media_dict_keys(Media)
|
|
5395
7384
|
),
|
|
5396
7385
|
media_dict(Media, Dict),
|
|
5397
7386
|
!,
|
|
@@ -5404,6 +7393,45 @@ mi_call(output(Media), StateIn, StateIn) :-
|
|
|
5404
7393
|
add_trace_entry(SessionId, media, media, [Path], Depth),
|
|
5405
7394
|
engine_yield(media(media{path: Path, mimeType: Mime, kind: Kind, data: Data})).
|
|
5406
7395
|
|
|
7396
|
+
%% media_dict_keys(+Dict): a dict shaped like a media reference.
|
|
7397
|
+
media_dict_keys(Dict) :-
|
|
7398
|
+
( get_dict(mime, Dict, _) ; get_dict(svg, Dict, _) ; get_dict(path, Dict, _) ; get_dict(data, Dict, _) ).
|
|
7399
|
+
|
|
7400
|
+
%% A file reference, not executable HTML or inline bytes. The host validates access.
|
|
7401
|
+
mi_call(output(file(Path)), StateIn, StateIn) :-
|
|
7402
|
+
!,
|
|
7403
|
+
( string(Path) -> File=Path ; atom(Path) -> atom_string(Path,File)
|
|
7404
|
+
; throw(error(type_error(file_path,Path),output/1)) ),
|
|
7405
|
+
engine_yield(download(download{path:File})).
|
|
7406
|
+
|
|
7407
|
+
%% Named workflow composition is a host capability. The host pins the catalog,
|
|
7408
|
+
%% validates types and applies depth/call/cancellation limits; drafts cannot call it.
|
|
7409
|
+
mi_call(run_workflow(Name, Inputs, Result), StateIn, StateOut) :-
|
|
7410
|
+
!,
|
|
7411
|
+
( (string(Name);atom(Name)),is_dict(Inputs),dict_pairs(Inputs,_,Pairs),ground(Pairs) -> true
|
|
7412
|
+
; throw(error(domain_error(workflow_call,Name-Inputs),run_workflow/3)) ),
|
|
7413
|
+
dict_pairs(Plain,_,Pairs),
|
|
7414
|
+
mi_call(exec(run_workflow(name:Name,inputs:Plain),Result),StateIn,StateOut).
|
|
7415
|
+
|
|
7416
|
+
%% Cross-Agent delivery is a host capability, separate from delegated process inbox refs.
|
|
7417
|
+
mi_call(agent_recipients(Agents), StateIn, StateOut) :-
|
|
7418
|
+
!, mi_call(exec(agent_recipients,Agents),StateIn,StateOut).
|
|
7419
|
+
mi_call(agent_broadcast(Message, Report), StateIn, StateOut) :-
|
|
7420
|
+
!,
|
|
7421
|
+
( ground(Message),is_dict(Message) -> true ; throw(error(domain_error(agent_message,Message),agent_broadcast/2)) ),
|
|
7422
|
+
dict_pairs(Message,_,Pairs),dict_pairs(Plain,_,Pairs),
|
|
7423
|
+
mi_call(exec(agent_broadcast(message:Plain),Report),StateIn,StateOut).
|
|
7424
|
+
mi_call(agent_send(Target, Message, Receipt), StateIn, StateOut) :-
|
|
7425
|
+
!,
|
|
7426
|
+
( Target == recipient -> To=_{kind:"recipient"}
|
|
7427
|
+
; nonvar(Target), Target=existing(Id), (string(Id);atom(Id)) -> To=_{kind:"existing",id:Id}
|
|
7428
|
+
; nonvar(Target), Target=new(Options), is_dict(Options) -> To=Options.put(kind,"new")
|
|
7429
|
+
; throw(error(domain_error(agent_target,Target),agent_send/3)) ),
|
|
7430
|
+
( ground(Message), is_dict(Message) -> true
|
|
7431
|
+
; throw(error(domain_error(agent_message,Message),agent_send/3)) ),
|
|
7432
|
+
dict_pairs(Message,_,Pairs), dict_pairs(Plain,_,Pairs),
|
|
7433
|
+
mi_call(exec(agent_send(target:To,message:Plain),Receipt),StateIn,StateOut).
|
|
7434
|
+
|
|
5407
7435
|
%% mi_call(output(html(Path)), +StateIn, -StateOut)
|
|
5408
7436
|
%% Present a workspace HTML file as an interactive miniapp.
|
|
5409
7437
|
mi_call(output(html(Path)), StateIn, StateIn) :-
|
|
@@ -5420,6 +7448,14 @@ mi_call(output(html(Path)), StateIn, StateIn) :-
|
|
|
5420
7448
|
!,
|
|
5421
7449
|
engine_yield(html(html{path: HtmlPath})).
|
|
5422
7450
|
|
|
7451
|
+
%% A plain dict is not text. `output(R)` on an exec result used to reach the media
|
|
7452
|
+
%% path and fail inside sub_atom with `type_error(atom, dict{...})`.
|
|
7453
|
+
mi_call(output(Value), _StateIn, _StateOut) :-
|
|
7454
|
+
is_dict(Value),
|
|
7455
|
+
!,
|
|
7456
|
+
throw(error(type_error(text, Value), context(output/1,
|
|
7457
|
+
'output/1 expects text or media; read a field first, e.g. get_dict(stdout, R, Text), output(Text)'))).
|
|
7458
|
+
|
|
5423
7459
|
%% mi_call(output(Text), +StateIn, -StateOut)
|
|
5424
7460
|
mi_call(output(Text), StateIn, StateIn) :-
|
|
5425
7461
|
get_params(StateIn, Params),
|
|
@@ -5721,6 +7757,39 @@ mi_call(\+(Goal), StateIn, StateIn) :-
|
|
|
5721
7757
|
mi_call(!, StateIn, StateIn) :-
|
|
5722
7758
|
!.
|
|
5723
7759
|
|
|
7760
|
+
%% mi_call('$dc_cut_flag', +StateIn, -StateOut)
|
|
7761
|
+
%% A cut whose only remaining effect is committing the clause.
|
|
7762
|
+
mi_call('$dc_cut_flag', StateIn, StateOut) :- !,
|
|
7763
|
+
mi_cut_mark(StateIn),
|
|
7764
|
+
StateOut = StateIn.
|
|
7765
|
+
|
|
7766
|
+
%% mi_call('$dc_cut_commit'(Goal), +StateIn, -StateOut)
|
|
7767
|
+
%% Commit the goals to the left of a cut, then the clause. once/1 is
|
|
7768
|
+
%% interpreter-aware, so bindings and state are kept.
|
|
7769
|
+
mi_call('$dc_cut_commit'(Goal), StateIn, StateOut) :- !,
|
|
7770
|
+
once(mi_call(Goal, StateIn, State1)),
|
|
7771
|
+
mi_cut_mark(State1),
|
|
7772
|
+
StateOut = State1.
|
|
7773
|
+
|
|
7774
|
+
%% mi_call('$dc_cut_guard'(Goal), +StateIn, -StateOut)
|
|
7775
|
+
%% The right branch of a disjunction whose left branch was cut.
|
|
7776
|
+
mi_call('$dc_cut_guard'(Goal), StateIn, StateOut) :- !,
|
|
7777
|
+
( mi_cut_committed(StateIn)
|
|
7778
|
+
-> fail
|
|
7779
|
+
; mi_call(Goal, StateIn, StateOut)
|
|
7780
|
+
).
|
|
7781
|
+
|
|
7782
|
+
%% mi_call('$dc_cut_scope'(Goal), +StateIn, -StateOut)
|
|
7783
|
+
%% A nested cut scope: call/1, \+/1, findall/3, an if-then-else condition.
|
|
7784
|
+
mi_call('$dc_cut_scope'(Goal), StateIn, StateOut) :- !,
|
|
7785
|
+
( get_dict(call_id, StateIn, Outer) -> true ; Outer = none ),
|
|
7786
|
+
mi_fresh_call_id(CallId),
|
|
7787
|
+
call_cleanup(
|
|
7788
|
+
mi_call(Goal, StateIn.put(call_id, CallId), StateOut0),
|
|
7789
|
+
mi_clear_cut_mark(CallId)
|
|
7790
|
+
),
|
|
7791
|
+
StateOut = StateOut0.put(call_id, Outer).
|
|
7792
|
+
|
|
5724
7793
|
%% mi_call(true, +StateIn, -StateOut)
|
|
5725
7794
|
mi_call(true, StateIn, StateIn).
|
|
5726
7795
|
|
|
@@ -5766,17 +7835,30 @@ repl_reserved_clause(Head) :-
|
|
|
5766
7835
|
functor(Head, Name, _),
|
|
5767
7836
|
repl_protected_name(Name).
|
|
5768
7837
|
|
|
7838
|
+
guard_harness_mutation(StateIn, What) :-
|
|
7839
|
+
get_session_id(SessionId),
|
|
7840
|
+
get_harness_context(StateIn, Module),
|
|
7841
|
+
( Module == SessionId
|
|
7842
|
+
-> true
|
|
7843
|
+
; throw(error(permission_error(modify, imported_module, What),
|
|
7844
|
+
context(mi_call/3, 'imported harness modules are read-only')))
|
|
7845
|
+
).
|
|
7846
|
+
|
|
5769
7847
|
mi_call(assertz(Head), StateIn, StateIn) :-
|
|
5770
7848
|
get_session_id(SessionId),
|
|
7849
|
+
guard_harness_mutation(StateIn, Head),
|
|
5771
7850
|
guard_not_frozen(SessionId),
|
|
5772
7851
|
guard_reserved_clause(Head),
|
|
5773
|
-
assertz(SessionId:Head)
|
|
7852
|
+
assertz(SessionId:Head),
|
|
7853
|
+
mi_clear_cut_classes(SessionId).
|
|
5774
7854
|
|
|
5775
7855
|
mi_call(asserta(Head), StateIn, StateIn) :-
|
|
5776
7856
|
get_session_id(SessionId),
|
|
7857
|
+
guard_harness_mutation(StateIn, Head),
|
|
5777
7858
|
guard_not_frozen(SessionId),
|
|
5778
7859
|
guard_reserved_clause(Head),
|
|
5779
|
-
asserta(SessionId:Head)
|
|
7860
|
+
asserta(SessionId:Head),
|
|
7861
|
+
mi_clear_cut_classes(SessionId).
|
|
5780
7862
|
|
|
5781
7863
|
guard_reserved_clause(Head) :-
|
|
5782
7864
|
( repl_reserved_clause(Head)
|
|
@@ -5787,13 +7869,17 @@ guard_reserved_clause(Head) :-
|
|
|
5787
7869
|
|
|
5788
7870
|
mi_call(retract(Head), StateIn, StateIn) :-
|
|
5789
7871
|
get_session_id(SessionId),
|
|
7872
|
+
guard_harness_mutation(StateIn, Head),
|
|
5790
7873
|
guard_not_frozen(SessionId),
|
|
5791
|
-
retract(SessionId:Head)
|
|
7874
|
+
retract(SessionId:Head),
|
|
7875
|
+
mi_clear_cut_classes(SessionId).
|
|
5792
7876
|
|
|
5793
7877
|
mi_call(retractall(Head), StateIn, StateIn) :-
|
|
5794
7878
|
get_session_id(SessionId),
|
|
7879
|
+
guard_harness_mutation(StateIn, Head),
|
|
5795
7880
|
guard_not_frozen(SessionId),
|
|
5796
|
-
retractall(SessionId:Head)
|
|
7881
|
+
retractall(SessionId:Head),
|
|
7882
|
+
mi_clear_cut_classes(SessionId).
|
|
5797
7883
|
|
|
5798
7884
|
%% ============================================================
|
|
5799
7885
|
%% List Predicates - Interpreted for proper state threading
|
|
@@ -6413,7 +8499,7 @@ workspace_predicate_clause(S, (S:json_gets(D, P, Vs) :- deepclause_mi:json_gets(
|
|
|
6413
8499
|
workspace_predicate_clause(S, (S:json_put(D, P, V, D2) :- deepclause_mi:json_put(D, P, V, D2))).
|
|
6414
8500
|
workspace_predicate_clause(S, (S:json_text(D, T) :- deepclause_mi:json_text(D, T))).
|
|
6415
8501
|
workspace_predicate_clause(S, (S:schema_fill(N, V, F) :-
|
|
6416
|
-
deepclause_mi:schema_fields_of(S, N, Fields), deepclause_mi:schema_fill_fields(Fields, V, F))).
|
|
8502
|
+
deepclause_mi:schema_fields_of(S, N, Fields), deepclause_mi:schema_fill_fields(S, Fields, V, F))).
|
|
6417
8503
|
workspace_predicate_clause(S, (S:pattern(T, Text, B) :- deepclause_mi:pattern(T, Text, B))).
|
|
6418
8504
|
|
|
6419
8505
|
workspace_predicate_clause(S, (S:time_file(F, T) :-
|
|
@@ -6690,6 +8776,37 @@ mi_call(_Module:Goal, StateIn, StateOut) :-
|
|
|
6690
8776
|
!,
|
|
6691
8777
|
mi_call(Goal, StateIn, StateOut).
|
|
6692
8778
|
|
|
8779
|
+
% A goal qualified with a module other than the session's runs natively. Its
|
|
8780
|
+
% body must resolve its own unqualified helpers in *its* module: interpreting it
|
|
8781
|
+
% here called `interp_json/2` (and any other helper) as `deepclause_mi:…`.
|
|
8782
|
+
% Session-qualified goals still need the interpreter so task/N and exec/2 work.
|
|
8783
|
+
mi_call(Module:Goal, StateIn, StateOut) :-
|
|
8784
|
+
is_harness_module(Module),
|
|
8785
|
+
!,
|
|
8786
|
+
mi_call_harness_goal(Module, Goal, StateIn, StateOut).
|
|
8787
|
+
|
|
8788
|
+
mi_call(Module:Goal, StateIn, StateIn) :-
|
|
8789
|
+
external_callable_module(Module),
|
|
8790
|
+
!,
|
|
8791
|
+
call(Module:Goal).
|
|
8792
|
+
|
|
8793
|
+
%% external_callable_module(+Module)
|
|
8794
|
+
%% Modules whose predicates must run natively rather than be re-interpreted
|
|
8795
|
+
%% here. Deliberately a whitelist: a session module (a generated `repl_*`) must
|
|
8796
|
+
%% keep going through the interpreter, or `task/N` and `exec/2` inside a
|
|
8797
|
+
%% qualified goal would not be intercepted.
|
|
8798
|
+
external_callable_module(deepclause_strings).
|
|
8799
|
+
external_callable_module(deepclause_memory).
|
|
8800
|
+
external_callable_module(deepclause_analysis).
|
|
8801
|
+
external_callable_module(json).
|
|
8802
|
+
external_callable_module(clpfd).
|
|
8803
|
+
external_callable_module(clpr).
|
|
8804
|
+
external_callable_module(clpq).
|
|
8805
|
+
external_callable_module(lists).
|
|
8806
|
+
external_callable_module(apply).
|
|
8807
|
+
external_callable_module(system).
|
|
8808
|
+
external_callable_module(user).
|
|
8809
|
+
|
|
6693
8810
|
% For regular module-qualified goals, use clause/2 for backtracking
|
|
6694
8811
|
mi_call(Module:Goal, StateIn, StateOut) :-
|
|
6695
8812
|
predicate_property(Module:Goal, defined),
|
|
@@ -6729,8 +8846,15 @@ is_mi_special_predicate(task_named(_,_,_)). %% Transformed form of task/N
|
|
|
6729
8846
|
is_mi_special_predicate(sample_token(_,_)).
|
|
6730
8847
|
is_mi_special_predicate(sample_token(_,_,_)).
|
|
6731
8848
|
is_mi_special_predicate(exec(_,_)).
|
|
8849
|
+
is_mi_special_predicate(run_script(_,_,_)).
|
|
6732
8850
|
is_mi_special_predicate(spawn(_,_)).
|
|
6733
8851
|
is_mi_special_predicate(spawn_opt(_,_,_)).
|
|
8852
|
+
is_mi_special_predicate(agent_self(_)).
|
|
8853
|
+
is_mi_special_predicate(agent_post(_,_,_)).
|
|
8854
|
+
is_mi_special_predicate(agent_send(_,_,_)).
|
|
8855
|
+
is_mi_special_predicate(run_workflow(_,_,_)).
|
|
8856
|
+
is_mi_special_predicate(agent_recipients(_)).
|
|
8857
|
+
is_mi_special_predicate(agent_broadcast(_,_)).
|
|
6734
8858
|
is_mi_special_predicate(self(_)).
|
|
6735
8859
|
is_mi_special_predicate(send(_,_)).
|
|
6736
8860
|
is_mi_special_predicate(receive(_)).
|
|
@@ -6744,6 +8868,9 @@ is_mi_special_predicate(whereis(_,_)).
|
|
|
6744
8868
|
is_mi_special_predicate(sleep(_)).
|
|
6745
8869
|
is_mi_special_predicate(make_ref(_)).
|
|
6746
8870
|
is_mi_special_predicate(now_ms(_)).
|
|
8871
|
+
is_mi_special_predicate(zone_offset(_,_,_)).
|
|
8872
|
+
is_mi_special_predicate(zone_date_time(_,_,_)).
|
|
8873
|
+
is_mi_special_predicate(local_date_time(_,_)).
|
|
6747
8874
|
is_mi_special_predicate(serve(_,_)).
|
|
6748
8875
|
is_mi_special_predicate(serve(_,_,_)).
|
|
6749
8876
|
is_mi_special_predicate(service_cast(_,_)).
|
|
@@ -6821,6 +8948,10 @@ is_mi_special_predicate(\+(_)).
|
|
|
6821
8948
|
is_mi_special_predicate(catch(_,_,_)).
|
|
6822
8949
|
is_mi_special_predicate(throw(_)).
|
|
6823
8950
|
is_mi_special_predicate(!).
|
|
8951
|
+
is_mi_special_predicate('$dc_cut_flag').
|
|
8952
|
+
is_mi_special_predicate('$dc_cut_commit'(_)).
|
|
8953
|
+
is_mi_special_predicate('$dc_cut_guard'(_)).
|
|
8954
|
+
is_mi_special_predicate('$dc_cut_scope'(_)).
|
|
6824
8955
|
is_mi_special_predicate(true).
|
|
6825
8956
|
is_mi_special_predicate(fail).
|
|
6826
8957
|
is_mi_special_predicate(false).
|
|
@@ -7011,18 +9142,60 @@ consume_gas(StateIn, StateOut) :-
|
|
|
7011
9142
|
%% mi_call_dispatch(+Goal, +StateIn, -StateOut)
|
|
7012
9143
|
%% Dispatch user-defined vs built-in predicates
|
|
7013
9144
|
%% Uses separate clauses instead of if-then-else to preserve backtracking
|
|
7014
|
-
|
|
9145
|
+
%%
|
|
9146
|
+
%% A goal qualified with another module (for example
|
|
9147
|
+
%% `deepclause_strings:dc_interp_value/2`, which the interpolation rewrite emits)
|
|
9148
|
+
%% must run natively. Interpreting its body here would resolve *its* unqualified
|
|
9149
|
+
%% helper calls against this module and fail with `deepclause_mi:helper/2`.
|
|
9150
|
+
mi_call_dispatch(Goal, StateIn, StateOut) :-
|
|
9151
|
+
get_session_id(SessionId),
|
|
9152
|
+
nonvar(Goal),
|
|
9153
|
+
Goal = Module:Inner,
|
|
9154
|
+
atom(Module),
|
|
9155
|
+
Module \== SessionId,
|
|
9156
|
+
% Only external modules: a session-qualified goal still needs the
|
|
9157
|
+
% interpreter so task/N, exec/2 and gas accounting keep working.
|
|
9158
|
+
Module \== user,
|
|
9159
|
+
!,
|
|
9160
|
+
get_depth(StateIn, Depth),
|
|
9161
|
+
trace_goal_call(SessionId, Goal, Depth),
|
|
9162
|
+
( call(Module:Inner)
|
|
9163
|
+
*-> trace_goal_exit(SessionId, Goal, Depth), StateOut = StateIn
|
|
9164
|
+
; trace_goal_fail(SessionId, Goal, Depth), fail
|
|
9165
|
+
).
|
|
9166
|
+
|
|
9167
|
+
mi_call_dispatch(Goal, StateIn, StateOut) :-
|
|
7015
9168
|
get_session_id(SessionId),
|
|
7016
9169
|
callable(Goal),
|
|
7017
9170
|
predicate_property(SessionId:Goal, defined),
|
|
7018
9171
|
predicate_property(Goal, built_in),
|
|
7019
9172
|
!,
|
|
7020
|
-
|
|
9173
|
+
get_depth(StateIn, Depth),
|
|
9174
|
+
trace_goal_call(SessionId, Goal, Depth),
|
|
9175
|
+
( call(SessionId:Goal)
|
|
9176
|
+
*-> trace_goal_exit(SessionId, Goal, Depth), StateOut = StateIn
|
|
9177
|
+
; trace_goal_fail(SessionId, Goal, Depth), fail
|
|
9178
|
+
).
|
|
7021
9179
|
|
|
7022
|
-
mi_call_dispatch(Goal, StateIn,
|
|
9180
|
+
mi_call_dispatch(Goal, StateIn, StateOut) :-
|
|
7023
9181
|
predicate_property(Goal, built_in),
|
|
7024
9182
|
!, % Commit: it's built-in, no backtracking needed
|
|
7025
|
-
|
|
9183
|
+
get_session_id(SessionId),
|
|
9184
|
+
get_depth(StateIn, Depth),
|
|
9185
|
+
trace_goal_call(SessionId, Goal, Depth),
|
|
9186
|
+
( call(Goal)
|
|
9187
|
+
*-> trace_goal_exit(SessionId, Goal, Depth), StateOut = StateIn
|
|
9188
|
+
; trace_goal_fail(SessionId, Goal, Depth), fail
|
|
9189
|
+
).
|
|
9190
|
+
|
|
9191
|
+
%% `script_file/2` is a fact annotation, not a helper. Calling it before the
|
|
9192
|
+
%% fact exists used to fail as `existence_error(procedure, ...)` with no hint.
|
|
9193
|
+
mi_call_dispatch(script_file(_, _), _StateIn, _StateOut) :-
|
|
9194
|
+
get_session_id(SessionId),
|
|
9195
|
+
\+ current_predicate(SessionId:script_file/2),
|
|
9196
|
+
!,
|
|
9197
|
+
throw(error(existence_error(procedure, script_file/2), context(script_file/2,
|
|
9198
|
+
'script_file/2 is an annotation, not a helper: declare script_file(Name, "path/to/script.py"), then run it with run_script(Name, Args, Value)'))).
|
|
7026
9199
|
|
|
7027
9200
|
mi_call_dispatch(Goal, StateIn, StateOut) :-
|
|
7028
9201
|
get_session_id(SessionId),
|
|
@@ -7030,41 +9203,347 @@ mi_call_dispatch(Goal, StateIn, StateOut) :-
|
|
|
7030
9203
|
predicate_property(SessionId:Goal, defined),
|
|
7031
9204
|
% If it's a library/imported predicate, call it directly
|
|
7032
9205
|
( (predicate_property(SessionId:Goal, imported_from(_)) ; predicate_property(SessionId:Goal, foreign))
|
|
7033
|
-
->
|
|
7034
|
-
|
|
7035
|
-
|
|
7036
|
-
|
|
9206
|
+
-> get_depth(StateIn, Depth),
|
|
9207
|
+
trace_goal_call(SessionId, Goal, Depth),
|
|
9208
|
+
( call(SessionId:Goal)
|
|
9209
|
+
*-> trace_goal_exit(SessionId, Goal, Depth), StateOut = StateIn
|
|
9210
|
+
; trace_goal_fail(SessionId, Goal, Depth), fail
|
|
9211
|
+
)
|
|
9212
|
+
; % A pure predicate that uses a cut runs natively: interpreting its body
|
|
9213
|
+
% would turn `!` into `true` (docs/META_INTERPRETER_CUTS.md), because the
|
|
9214
|
+
% cut cannot reach the clause/2 choice point from mi_call/3.
|
|
9215
|
+
mi_call_predicate_class(SessionId, Goal, Class),
|
|
7037
9216
|
get_depth(StateIn, Depth),
|
|
7038
|
-
Goal
|
|
7039
|
-
|
|
7040
|
-
%
|
|
7041
|
-
|
|
9217
|
+
trace_goal_call(SessionId, Goal, Depth),
|
|
9218
|
+
( Class == native
|
|
9219
|
+
-> % Pure predicate with a cut: run the real clauses so `!` is a real
|
|
9220
|
+
% Prolog cut (bindings kept, clause committed, scopes honoured).
|
|
9221
|
+
( call(SessionId:Goal)
|
|
9222
|
+
*-> trace_goal_exit(SessionId, Goal, Depth), StateOut = StateIn
|
|
9223
|
+
; trace_goal_fail(SessionId, Goal, Depth), fail
|
|
9224
|
+
)
|
|
9225
|
+
; ( Class == cut
|
|
9226
|
+
-> % Interpreted goal by goal with a lowered cut. Each call gets its
|
|
9227
|
+
% own commit marker so the cut can stop later clauses from being
|
|
9228
|
+
% tried; the marker must survive backtracking, so it cannot live
|
|
9229
|
+
% in the threaded state.
|
|
9230
|
+
( get_dict(call_id, StateIn, OuterCall) -> true ; OuterCall = none ),
|
|
9231
|
+
mi_fresh_call_id(CallId),
|
|
9232
|
+
call_cleanup(
|
|
9233
|
+
mi_call_clause_loop(SessionId, Goal, Depth, StateIn.put(call_id, CallId),
|
|
9234
|
+
CallId, true, StateOut0),
|
|
9235
|
+
mi_clear_cut_mark(CallId)
|
|
9236
|
+
),
|
|
9237
|
+
StateOut = StateOut0.put(call_id, OuterCall)
|
|
9238
|
+
; % No cut: the plain interpreter path, unchanged.
|
|
9239
|
+
mi_call_clause_loop(SessionId, Goal, Depth, StateIn, none, false, StateOut)
|
|
9240
|
+
)
|
|
9241
|
+
)
|
|
9242
|
+
).
|
|
9243
|
+
|
|
9244
|
+
%% mi_call_clause_loop(+SessionId, +Goal, +Depth, +StateIn, +CallId, +Lower, -StateOut)
|
|
9245
|
+
%% Enumerate clauses, interpreting each lowered body. Once the call's commit
|
|
9246
|
+
%% marker is set, later clauses are never tried - that is the cut's second
|
|
9247
|
+
%% effect, which must survive backtracking and therefore cannot live in the
|
|
9248
|
+
%% threaded state.
|
|
9249
|
+
mi_call_clause_loop(SessionId, Goal, Depth, StateIn, CallId, Lower, StateOut) :-
|
|
9250
|
+
clause(SessionId:Goal, Body),
|
|
9251
|
+
( CallId \== none,
|
|
9252
|
+
mi_cut_done(CallId)
|
|
9253
|
+
-> fail
|
|
9254
|
+
; ( Lower == true -> mi_lower_cut_body(Body, Lowered) ; Lowered = Body ),
|
|
7042
9255
|
NewDepth is Depth + 1,
|
|
7043
9256
|
set_depth(StateIn, NewDepth, State1),
|
|
7044
|
-
|
|
7045
|
-
|
|
7046
|
-
( mi_call(Body, State1, State2)
|
|
7047
|
-
*-> add_trace_entry(SessionId, exit, Functor, Args, Depth),
|
|
9257
|
+
( mi_call(Lowered, State1, State2)
|
|
9258
|
+
*-> trace_goal_exit(SessionId, Goal, Depth),
|
|
7048
9259
|
set_depth(State2, Depth, StateOut)
|
|
7049
|
-
;
|
|
9260
|
+
; trace_goal_fail(SessionId, Goal, Depth),
|
|
7050
9261
|
fail
|
|
7051
9262
|
)
|
|
7052
9263
|
).
|
|
7053
9264
|
|
|
7054
|
-
|
|
9265
|
+
%% mi_native_query(+SessionId, +Goal): a pure query containing a cut.
|
|
9266
|
+
mi_native_query(SessionId, Goal) :-
|
|
9267
|
+
term_has_cut(Goal),
|
|
9268
|
+
mi_body_is_pure(SessionId, Goal).
|
|
9269
|
+
|
|
9270
|
+
%% ============================================================
|
|
9271
|
+
%% Cut inside user predicates
|
|
9272
|
+
%%
|
|
9273
|
+
%% `!` cannot be interpreted faithfully: the interpreted body is executed by
|
|
9274
|
+
%% mi_call/3, so a cut cannot prune the clause/2 choice point created in
|
|
9275
|
+
%% mi_call_dispatch/3 (see docs/META_INTERPRETER_CUTS.md). A *pure* predicate
|
|
9276
|
+
%% that contains a cut is therefore executed natively with call/1, which gives
|
|
9277
|
+
%% exact Prolog cut semantics: bindings are kept, the clause commits, and
|
|
9278
|
+
%% \+/1, call/1, findall/3 and if-then-else scope it correctly.
|
|
9279
|
+
%%
|
|
9280
|
+
%% "Pure" means: neither the predicate nor anything it calls needs state
|
|
9281
|
+
%% threading (task/N, prompt/N, llm/2, exec/2, forms, processes, system/1, ...).
|
|
9282
|
+
%% Such a predicate stays interpreted and its cut is ignored; that restriction
|
|
9283
|
+
%% is documented. Both decisions are cached per predicate and invalidated
|
|
9284
|
+
%% whenever the harness changes.
|
|
9285
|
+
%% ============================================================
|
|
9286
|
+
|
|
9287
|
+
%% mi_call_predicate_class(+SessionId, +Goal, -Class)
|
|
9288
|
+
%% native - contains a cut and is pure: run the real clauses
|
|
9289
|
+
%% cut - contains a cut but needs the interpreter: lower the cut
|
|
9290
|
+
%% no_cut - no cut: interpret the body as written
|
|
9291
|
+
mi_call_predicate_class(SessionId, Goal, Class) :-
|
|
9292
|
+
callable(Goal),
|
|
9293
|
+
functor(Goal, Name, Arity),
|
|
9294
|
+
( mi_cut_class(SessionId, Name/Arity, Cached)
|
|
9295
|
+
-> Class = Cached
|
|
9296
|
+
; ( mi_classify_cut_predicate(SessionId, Name/Arity, Class0)
|
|
9297
|
+
-> Class = Class0
|
|
9298
|
+
; Class = no_cut
|
|
9299
|
+
),
|
|
9300
|
+
assertz(mi_cut_class(SessionId, Name/Arity, Class))
|
|
9301
|
+
).
|
|
9302
|
+
|
|
9303
|
+
mi_classify_cut_predicate(SessionId, Name/Arity, Class) :-
|
|
9304
|
+
functor(Head, Name, Arity),
|
|
9305
|
+
findall(Body, clause(SessionId:Head, Body), Bodies),
|
|
9306
|
+
Bodies \== [],
|
|
9307
|
+
( member(Body0, Bodies),
|
|
9308
|
+
term_has_cut(Body0)
|
|
9309
|
+
-> ( forall(member(Body, Bodies), mi_body_is_pure(SessionId, Body))
|
|
9310
|
+
-> Class = native
|
|
9311
|
+
; Class = cut
|
|
9312
|
+
)
|
|
9313
|
+
; fail
|
|
9314
|
+
).
|
|
9315
|
+
|
|
9316
|
+
%% term_has_cut(+Term): a cut occurs anywhere in the term (conservative).
|
|
9317
|
+
term_has_cut(Term) :-
|
|
9318
|
+
( Term == !
|
|
9319
|
+
-> true
|
|
9320
|
+
; compound(Term),
|
|
9321
|
+
Term =.. [_|Args],
|
|
9322
|
+
member(Arg, Args),
|
|
9323
|
+
term_has_cut(Arg)
|
|
9324
|
+
).
|
|
9325
|
+
|
|
9326
|
+
mi_body_is_pure(SessionId, Body) :-
|
|
9327
|
+
\+ ( sub_term(Goal, Body),
|
|
9328
|
+
compound(Goal),
|
|
9329
|
+
mi_impure_goal(SessionId, Goal)
|
|
9330
|
+
).
|
|
9331
|
+
|
|
9332
|
+
%% A host/model predicate, or a user predicate that (transitively) needs one.
|
|
9333
|
+
mi_impure_goal(_SessionId, Goal) :-
|
|
9334
|
+
is_mi_special_predicate(Goal),
|
|
9335
|
+
\+ mi_control_construct(Goal).
|
|
9336
|
+
mi_impure_goal(SessionId, Goal) :-
|
|
9337
|
+
\+ is_mi_special_predicate(Goal),
|
|
9338
|
+
predicate_property(SessionId:Goal, defined),
|
|
9339
|
+
\+ predicate_property(SessionId:Goal, imported_from(_)),
|
|
9340
|
+
\+ predicate_property(SessionId:Goal, foreign),
|
|
9341
|
+
functor(Goal, Name, Arity),
|
|
9342
|
+
mi_predicate_purity(SessionId, Name/Arity, impure).
|
|
9343
|
+
|
|
9344
|
+
mi_predicate_purity(SessionId, Indicator, Purity) :-
|
|
9345
|
+
( mi_purity(SessionId, Indicator, Cached)
|
|
9346
|
+
-> Purity = Cached
|
|
9347
|
+
; assertz(mi_purity(SessionId, Indicator, in_progress)),
|
|
9348
|
+
Indicator = Name/Arity,
|
|
9349
|
+
functor(Head, Name, Arity),
|
|
9350
|
+
findall(Body, clause(SessionId:Head, Body), Bodies),
|
|
9351
|
+
( Bodies \== [],
|
|
9352
|
+
forall(member(Body, Bodies), mi_body_is_pure(SessionId, Body))
|
|
9353
|
+
-> Purity = pure
|
|
9354
|
+
; Purity = impure
|
|
9355
|
+
),
|
|
9356
|
+
retractall(mi_purity(SessionId, Indicator, _)),
|
|
9357
|
+
( Purity == pure -> true ; true ),
|
|
9358
|
+
assertz(mi_purity(SessionId, Indicator, Purity))
|
|
9359
|
+
).
|
|
9360
|
+
|
|
9361
|
+
%% Control constructs and native-safe builtins are not "impure": they carry no
|
|
9362
|
+
%% state or are re-implemented natively.
|
|
9363
|
+
mi_control_construct((_, _)).
|
|
9364
|
+
mi_control_construct((_; _)).
|
|
9365
|
+
mi_control_construct((_ -> _)).
|
|
9366
|
+
mi_control_construct((_ *-> _)).
|
|
9367
|
+
mi_control_construct((_ *-> _ ; _)).
|
|
9368
|
+
mi_control_construct(\+(_)).
|
|
9369
|
+
mi_control_construct(!).
|
|
9370
|
+
mi_control_construct(true).
|
|
9371
|
+
mi_control_construct(fail).
|
|
9372
|
+
mi_control_construct(false).
|
|
9373
|
+
mi_control_construct(catch(_, _, _)).
|
|
9374
|
+
mi_control_construct(throw(_)).
|
|
9375
|
+
mi_control_construct(findall(_, _, _)).
|
|
9376
|
+
mi_control_construct(bagof(_, _, _)).
|
|
9377
|
+
mi_control_construct(setof(_, _, _)).
|
|
9378
|
+
mi_control_construct(aggregate_all(_, _, _)).
|
|
9379
|
+
mi_control_construct(forall(_, _)).
|
|
9380
|
+
mi_control_construct(call(_)).
|
|
9381
|
+
mi_control_construct(call(_, _)).
|
|
9382
|
+
mi_control_construct(call(_, _, _)).
|
|
9383
|
+
mi_control_construct(call(_, _, _, _)).
|
|
9384
|
+
mi_control_construct(once(_)).
|
|
9385
|
+
mi_control_construct(ignore(_)).
|
|
9386
|
+
mi_control_construct(not(_)).
|
|
9387
|
+
mi_control_construct(assertz(_)).
|
|
9388
|
+
mi_control_construct(asserta(_)).
|
|
9389
|
+
mi_control_construct(retract(_)).
|
|
9390
|
+
mi_control_construct(retractall(_)).
|
|
9391
|
+
|
|
9392
|
+
%% sub_term(?Sub, +Term): any subterm, including arguments and list elements.
|
|
9393
|
+
sub_term(Term, Term).
|
|
9394
|
+
sub_term(Sub, Term) :-
|
|
9395
|
+
compound(Term),
|
|
9396
|
+
Term =.. [_|Args],
|
|
9397
|
+
member(Arg, Args),
|
|
9398
|
+
sub_term(Sub, Arg).
|
|
9399
|
+
|
|
9400
|
+
%% ============================================================
|
|
9401
|
+
%% Cut lowering for interpreted predicates
|
|
9402
|
+
%%
|
|
9403
|
+
%% A predicate that needs the interpreter (it calls task/N, exec/2, output/1,
|
|
9404
|
+
%% forms, processes, ...) cannot run natively, so its cuts are compiled away
|
|
9405
|
+
%% instead:
|
|
9406
|
+
%%
|
|
9407
|
+
%% ! -> '$dc_cut_flag' (commit the clause)
|
|
9408
|
+
%% (L, !, R) -> '$dc_cut_commit'(L), R (once(L), then commit)
|
|
9409
|
+
%% (L ; R) -> ( L' ; '$dc_cut_guard'(R') ) when L may cut
|
|
9410
|
+
%% \+ G, call(G), findall(_,G,_), (C->T;E) -> the goal keeps its own scope
|
|
9411
|
+
%%
|
|
9412
|
+
%% '$dc_cut_commit'(L) is once(L) plus the commit flag, and once/1 is already
|
|
9413
|
+
%% interpreter-aware, so bindings and the threaded state behave exactly as a
|
|
9414
|
+
%% real cut. The flag is a non-backtrackable fact per call (mi_cut_done/1): it
|
|
9415
|
+
%% must survive backtracking, which the threaded state cannot do.
|
|
9416
|
+
%% ============================================================
|
|
9417
|
+
|
|
9418
|
+
%% mi_lower_cut_body(+Body, -Lowered)
|
|
9419
|
+
mi_lower_cut_body(Body, Lowered) :-
|
|
9420
|
+
( term_has_cut(Body)
|
|
9421
|
+
-> mi_lower_goal(Body, Lowered, _)
|
|
9422
|
+
; Lowered = Body
|
|
9423
|
+
).
|
|
9424
|
+
|
|
9425
|
+
%% mi_lower_goal(+Goal, -Lowered, -HasClauseCut)
|
|
9426
|
+
mi_lower_goal(!, '$dc_cut_flag', true) :- !.
|
|
9427
|
+
mi_lower_goal((A, B), Lowered, HasCut) :- !,
|
|
9428
|
+
mi_lower_goal(A, A1, CutA),
|
|
9429
|
+
mi_lower_goal(B, B1, CutB),
|
|
9430
|
+
( CutA == true
|
|
9431
|
+
-> % The cut is inside A: A1 already commits its own prefix and keeps
|
|
9432
|
+
% enumerating after the cut, so do not wrap it again.
|
|
9433
|
+
Lowered = (A1, B1)
|
|
9434
|
+
; CutB == true
|
|
9435
|
+
-> % A cut follows: every goal to its left is committed.
|
|
9436
|
+
Lowered = ('$dc_cut_commit'(A1), B1)
|
|
9437
|
+
; Lowered = (A1, B1)
|
|
9438
|
+
),
|
|
9439
|
+
( CutA == true -> HasCut = true ; HasCut = CutB ).
|
|
9440
|
+
mi_lower_goal((A ; B), Lowered, HasCut) :- !,
|
|
9441
|
+
mi_lower_goal(A, A1, CutA),
|
|
9442
|
+
mi_lower_goal(B, B1, CutB),
|
|
9443
|
+
( CutA == true
|
|
9444
|
+
-> Lowered = (A1 ; '$dc_cut_guard'(B1))
|
|
9445
|
+
; Lowered = (A1 ; B1)
|
|
9446
|
+
),
|
|
9447
|
+
( CutA == true -> HasCut = true ; HasCut = CutB ).
|
|
9448
|
+
mi_lower_goal((Cond -> Then ; Else), Lowered, HasCut) :- !,
|
|
9449
|
+
mi_lower_scope(Cond, C1),
|
|
9450
|
+
mi_lower_goal(Then, T1, CutT),
|
|
9451
|
+
mi_lower_goal(Else, E1, CutE),
|
|
9452
|
+
Lowered = (C1 -> T1 ; E1),
|
|
9453
|
+
( CutT == true -> HasCut = true ; HasCut = CutE ).
|
|
9454
|
+
mi_lower_goal((Cond -> Then), Lowered, HasCut) :- !,
|
|
9455
|
+
mi_lower_scope(Cond, C1),
|
|
9456
|
+
mi_lower_goal(Then, T1, CutT),
|
|
9457
|
+
Lowered = (C1 -> T1),
|
|
9458
|
+
HasCut = CutT.
|
|
9459
|
+
mi_lower_goal(\+(Goal), \+(G1), false) :- !, mi_lower_scope(Goal, G1).
|
|
9460
|
+
mi_lower_goal(call(Goal), call(G1), false) :- !, mi_lower_scope(Goal, G1).
|
|
9461
|
+
mi_lower_goal(call(Goal, A), call(G1, A), false) :- !, mi_lower_scope(Goal, G1).
|
|
9462
|
+
mi_lower_goal(once(Goal), once(G1), false) :- !, mi_lower_scope(Goal, G1).
|
|
9463
|
+
mi_lower_goal(ignore(Goal), ignore(G1), false) :- !, mi_lower_scope(Goal, G1).
|
|
9464
|
+
mi_lower_goal(not(Goal), not(G1), false) :- !, mi_lower_scope(Goal, G1).
|
|
9465
|
+
mi_lower_goal(findall(T, Goal, L), findall(T, G1, L), false) :- !, mi_lower_scope(Goal, G1).
|
|
9466
|
+
mi_lower_goal(bagof(T, Goal, L), bagof(T, G1, L), false) :- !, mi_lower_scope(Goal, G1).
|
|
9467
|
+
mi_lower_goal(setof(T, Goal, L), setof(T, G1, L), false) :- !, mi_lower_scope(Goal, G1).
|
|
9468
|
+
mi_lower_goal(aggregate_all(T, Goal, R), aggregate_all(T, G1, R), false) :- !, mi_lower_scope(Goal, G1).
|
|
9469
|
+
mi_lower_goal(forall(Cond, Action), forall(C1, A1), false) :- !,
|
|
9470
|
+
mi_lower_scope(Cond, C1),
|
|
9471
|
+
mi_lower_scope(Action, A1).
|
|
9472
|
+
mi_lower_goal(Goal, Goal, false).
|
|
9473
|
+
|
|
9474
|
+
%% A goal that is a cut scope of its own: lower its cuts, then mark the scope.
|
|
9475
|
+
mi_lower_scope(Goal, Scoped) :-
|
|
9476
|
+
( term_has_cut(Goal)
|
|
9477
|
+
-> mi_lower_goal(Goal, Lowered, _),
|
|
9478
|
+
Scoped = '$dc_cut_scope'(Lowered)
|
|
9479
|
+
; Scoped = Goal
|
|
9480
|
+
).
|
|
9481
|
+
|
|
9482
|
+
%% ============================================================
|
|
9483
|
+
%% Cut lowering - runtime support
|
|
9484
|
+
%% ============================================================
|
|
9485
|
+
|
|
9486
|
+
mi_fresh_call_id(Id) :-
|
|
9487
|
+
( nb_current(mi_call_seq, N) -> true ; N = 0 ),
|
|
9488
|
+
N1 is N + 1,
|
|
9489
|
+
nb_setval(mi_call_seq, N1),
|
|
9490
|
+
Id = N1.
|
|
9491
|
+
|
|
9492
|
+
%% mi_cut_mark(+State): this call must not try later clauses.
|
|
9493
|
+
mi_cut_mark(State) :-
|
|
9494
|
+
( get_dict(call_id, State, CallId), CallId \== none
|
|
9495
|
+
-> ( mi_cut_done(CallId) -> true ; assertz(mi_cut_done(CallId)) )
|
|
9496
|
+
; true
|
|
9497
|
+
).
|
|
9498
|
+
|
|
9499
|
+
mi_cut_committed(State) :-
|
|
9500
|
+
get_dict(call_id, State, CallId),
|
|
9501
|
+
CallId \== none,
|
|
9502
|
+
mi_cut_done(CallId).
|
|
9503
|
+
|
|
9504
|
+
mi_clear_cut_mark(CallId) :-
|
|
9505
|
+
retractall(mi_cut_done(CallId)).
|
|
9506
|
+
|
|
9507
|
+
%% mi_clear_cut_classes(+SessionId): the harness changed; reclassify lazily.
|
|
9508
|
+
mi_clear_cut_classes(SessionId) :-
|
|
9509
|
+
retractall(mi_cut_class(SessionId, _, _)),
|
|
9510
|
+
retractall(mi_purity(SessionId, _, _)).
|
|
9511
|
+
|
|
9512
|
+
mi_call_dispatch(Goal, StateIn, StateOut) :-
|
|
9513
|
+
get_session_id(SessionId),
|
|
9514
|
+
get_harness_context(StateIn, Current),
|
|
9515
|
+
Current == SessionId,
|
|
9516
|
+
callable(Goal),
|
|
9517
|
+
functor(Goal, Name, Arity),
|
|
9518
|
+
import_target(SessionId, Name, Arity, TargetModule, TargetName),
|
|
9519
|
+
!,
|
|
9520
|
+
retarget_goal(Goal, TargetName, TargetGoal),
|
|
9521
|
+
mi_call_harness_goal(TargetModule, TargetGoal, StateIn, StateOut).
|
|
9522
|
+
|
|
9523
|
+
mi_call_dispatch(Goal, StateIn, StateOut) :-
|
|
7055
9524
|
% Fallback for external/library predicates
|
|
7056
9525
|
get_session_id(SessionId),
|
|
7057
9526
|
% Check if this predicate exists in the session module but not as user-defined
|
|
7058
9527
|
\+ predicate_property(SessionId:Goal, defined),
|
|
7059
|
-
|
|
9528
|
+
get_depth(StateIn, Depth),
|
|
9529
|
+
trace_goal_call(SessionId, Goal, Depth),
|
|
9530
|
+
( catch(call(SessionId:Goal), _, fail)
|
|
9531
|
+
-> trace_goal_exit(SessionId, Goal, Depth), StateOut = StateIn
|
|
9532
|
+
; trace_goal_fail(SessionId, Goal, Depth), fail
|
|
9533
|
+
),
|
|
7060
9534
|
!.
|
|
7061
9535
|
|
|
7062
|
-
mi_call_dispatch(Goal, StateIn,
|
|
9536
|
+
mi_call_dispatch(Goal, StateIn, StateOut) :-
|
|
7063
9537
|
% Final fallback: call without module
|
|
7064
9538
|
% Only for predicates not defined in session
|
|
7065
9539
|
get_session_id(SessionId),
|
|
7066
9540
|
\+ predicate_property(SessionId:Goal, defined),
|
|
7067
|
-
|
|
9541
|
+
get_depth(StateIn, Depth),
|
|
9542
|
+
trace_goal_call(SessionId, Goal, Depth),
|
|
9543
|
+
( call(Goal)
|
|
9544
|
+
*-> trace_goal_exit(SessionId, Goal, Depth), StateOut = StateIn
|
|
9545
|
+
; trace_goal_fail(SessionId, Goal, Depth), fail
|
|
9546
|
+
).
|
|
7068
9547
|
|
|
7069
9548
|
%% ============================================================
|
|
7070
9549
|
%% Helper Predicates
|
|
@@ -7373,13 +9852,23 @@ build_format_call(Template, Bindings, TempVar, FullGoal) :-
|
|
|
7373
9852
|
),
|
|
7374
9853
|
extract_interpolation_vars(T, VarNames, FormatString),
|
|
7375
9854
|
lookup_vars_with_params(VarNames, Bindings, VarList, ParamGoals),
|
|
7376
|
-
|
|
7377
|
-
|
|
9855
|
+
% Values are rendered by dc_interp_value/2 and spliced with `~s`, so a list
|
|
9856
|
+
% or a dict arrives as JSON rather than Prolog syntax.
|
|
9857
|
+
render_interp_vars(VarList, RenderedVars, RenderGoals),
|
|
9858
|
+
FormatGoal = format(string(TempVar), FormatString, RenderedVars),
|
|
9859
|
+
append(ParamGoals, RenderGoals, Goals),
|
|
9860
|
+
( Goals == []
|
|
7378
9861
|
-> FullGoal = FormatGoal
|
|
7379
|
-
; list_to_conjunction(
|
|
7380
|
-
FullGoal = (
|
|
9862
|
+
; list_to_conjunction(Goals, Conj),
|
|
9863
|
+
FullGoal = (Conj, FormatGoal)
|
|
7381
9864
|
).
|
|
7382
9865
|
|
|
9866
|
+
%% render_interp_vars(+Vars, -Rendered, -Goals)
|
|
9867
|
+
render_interp_vars([], [], []).
|
|
9868
|
+
render_interp_vars([Var|Vars], [Rendered|Rendereds], [Goal|Goals]) :-
|
|
9869
|
+
Goal = deepclause_strings:dc_interp_value(Var, Rendered),
|
|
9870
|
+
render_interp_vars(Vars, Rendereds, Goals).
|
|
9871
|
+
|
|
7383
9872
|
%% list_to_conjunction(+List, -Conjunction)
|
|
7384
9873
|
list_to_conjunction([G], G) :- !.
|
|
7385
9874
|
list_to_conjunction([G|Gs], (G, Rest)) :-
|
|
@@ -7402,7 +9891,7 @@ extract_interpolation_vars(Template, VarNames, FormatString) :-
|
|
|
7402
9891
|
extract_vars_from_codes([], [], []) :- !.
|
|
7403
9892
|
|
|
7404
9893
|
% Found opening brace
|
|
7405
|
-
extract_vars_from_codes([0'{|Rest], [VarName|VarNames], [0'~, 0'
|
|
9894
|
+
extract_vars_from_codes([0'{|Rest], [VarName|VarNames], [0'~, 0's|FormatRest]) :-
|
|
7406
9895
|
extract_var_until_close(Rest, VarNameCodes, AfterClose),
|
|
7407
9896
|
valid_placeholder_codes(VarNameCodes),
|
|
7408
9897
|
!,
|