An agent writes a part
This lesson puts the earlier lessons to work. You give an agent a prompt for a feature of jot, read its answer, and run it under the sandbox. The page shows an example answer. Issue #216 adds an agent that runs in a sandboxed thread of ztcl, and then this lesson runs one live.
The prompt
Add
jot::export_mdto thejotnamespace. It gives the notes as a Markdown task list with the title "Notes". Escape the Markdown characters of the text. Use no capability: the proc gives a string, and the caller writes it.
The last sentence is the important one. A proc that gives a string needs no file, so the caller decides where the text goes.
The answer
This is a good answer. Read it before you run it.
proc jot::export_md {} {
variable notes
set lines [list "# Notes" ""]
foreach n $notes {
set box [expr {[dict get $n done] ? "x" : " "}]
set text [string map {\\ \\\\ * \\* _ \\_ ` \\` [ \\[ ] \\]} [dict get $n text]]
lappend lines "- \[$box\] $text"
}
return [join $lines \n]
}
The review
Check it with the lists of the earlier lessons:
variable noteslinks the namespace variable (lesson 7). Without it, the proc gives an error.exprhas braces (lesson 2), and the list is built withlistandlappend(lesson 3).string mapreads the string once, with the backslash first in the map. Each character of the map is in braces, so Tcl does not change it (lesson 1).- The proc uses no
open,exec, orsocket(lesson 13).
Then run it: jot::export_md. Check one value: lindex [split [jot::export_md] \n] 2.
A bad answer
This is the end of a bad answer to the same prompt:
exec pandoc notes.md -o notes.pdf
set f [open ~/notes.md w]
The prompt said "use no capability", and the answer uses two. In the sandbox, each one is an error, and the files of the computer stay the same: catch {exec pandoc notes.md -o notes.pdf} msg and catch {open ~/notes.md w} msg. set msg has the message.
Tell the agent what was wrong, in one line: "The proc uses exec and open; give the Markdown as the result, and use no capability."
Run a part from an agent
- Give the prompt the capabilities that the part can use, and the limits.
- Read the answer with the lists of the lessons. Look first for
exec,open,socket,eval, anduplevel. - Run it with only those capabilities and a compute limit:
ztcl --allow file,channels --root ./notes --compute 50000000 jot.tcl. - Check one result that you know.
Try this
- Add a note with
*in its text, and runjot::export_mdagain. - Change the prompt to ask for a
-doneoption that leaves out the notes that are done. - Give the prompt to your agent, and review its answer with the four checks.
The state of the shell at the start of this lesson
namespace eval jot {
variable notes [list [dict create text "buy milk" done 1] [dict create text "fix the bike #home" done 0]]
}