zettcode-engine 0.5.1__tar.gz → 0.5.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/.gitignore +1 -1
  2. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/PKG-INFO +3 -3
  3. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/pyproject.toml +2 -2
  4. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/config.py +11 -5
  5. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/config_edit.py +5 -0
  6. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/agent/context.py +1 -1
  7. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/core.py +44 -16
  8. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/subagents.py +18 -3
  9. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/usage.py +20 -3
  10. zettcode_engine-0.5.3/src/zettcode/engine/harness_library.py +910 -0
  11. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/permission/approval.py +11 -7
  12. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/permission/sandbox.py +12 -1
  13. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/__init__.py +8 -4
  14. zettcode_engine-0.5.3/src/zettcode/engine/plugins/children.py +94 -0
  15. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/cli.py +7 -5
  16. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/commands.py +31 -5
  17. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/context.py +9 -4
  18. zettcode_engine-0.5.3/src/zettcode/engine/plugins/environment.py +5 -0
  19. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/loader.py +130 -200
  20. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/plugin.py +67 -18
  21. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/state.py +5 -0
  22. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/switches.py +117 -151
  23. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/storage/ledger.py +63 -18
  24. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/version.py +1 -1
  25. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/wire.py +10 -0
  26. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/_compat.py +0 -0
  27. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/budget.py +0 -0
  28. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/__init__.py +0 -0
  29. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/agent/__init__.py +0 -0
  30. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/agent/base.py +0 -0
  31. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/agent/exports.py +0 -0
  32. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/agent/interaction.py +0 -0
  33. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/agent/sessions.py +0 -0
  34. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/agent/settings.py +0 -0
  35. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/agent/turns.py +0 -0
  36. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/delivery.py +0 -0
  37. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/delivery_runtime.py +0 -0
  38. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/dialogue/__init__.py +0 -0
  39. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/dialogue/ask.py +0 -0
  40. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/dialogue/side.py +0 -0
  41. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/dialogue/title.py +0 -0
  42. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/export/__init__.py +0 -0
  43. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/export/trace/script.js +0 -0
  44. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/export/trace/style.css +0 -0
  45. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/export/trace/template.html +0 -0
  46. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/__init__.py +0 -0
  47. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/capabilities.py +0 -0
  48. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/compaction.py +0 -0
  49. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/context.py +0 -0
  50. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/files.py +0 -0
  51. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/guidance.py +0 -0
  52. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/harness_switch.py +0 -0
  53. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/mcp_notice.py +0 -0
  54. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/extensions/relay.py +0 -0
  55. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/harness_paths.py +0 -0
  56. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/inbox.py +0 -0
  57. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/permission/__init__.py +0 -0
  58. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/permission/readonly.py +0 -0
  59. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/pipeline.py +0 -0
  60. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/extension.py +0 -0
  61. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/graph.py +0 -0
  62. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/groups.py +0 -0
  63. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/llm.py +0 -0
  64. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/mixins.py +0 -0
  65. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/services.py +0 -0
  66. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/settings.py +0 -0
  67. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/plugins/triggers.py +0 -0
  68. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/provided.py +0 -0
  69. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/py.typed +0 -0
  70. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/server_tools.py +0 -0
  71. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/service_contracts.py +0 -0
  72. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/storage/__init__.py +0 -0
  73. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/storage/layout.py +0 -0
  74. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/storage/locks.py +0 -0
  75. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/storage/metadata.py +0 -0
  76. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/storage/records.py +0 -0
  77. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/engine/storage/store.py +0 -0
  78. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/error_text.py +0 -0
  79. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/filelock.py +0 -0
  80. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/logging_setup.py +0 -0
  81. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/mention.py +0 -0
  82. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/modes.py +0 -0
  83. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/paths.py +0 -0
  84. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/plugin_tui.py +0 -0
  85. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/processes.py +0 -0
  86. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/prompt.py +0 -0
  87. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/providers.py +0 -0
  88. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/py.typed +0 -0
  89. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/registry.py +0 -0
  90. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/relay.py +0 -0
  91. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/statusline_parts.py +0 -0
  92. {zettcode_engine-0.5.1 → zettcode_engine-0.5.3}/src/zettcode/wording.py +0 -0
@@ -16,7 +16,7 @@ dist/
16
16
  /result.json
17
17
 
18
18
  # The docs site: dependencies, build output, and VitePress' cache.
19
- node_modules/
19
+ node_modules
20
20
  docs/.vitepress/dist/
21
21
  docs/.vitepress/cache/
22
22
  docs/.vitepress/.temp/
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: zettcode-engine
3
- Version: 0.5.1
3
+ Version: 0.5.3
4
4
  Summary: The agent core of ZettCode: the runtime over zett-agent, and the leaf types it is written on
5
5
  Project-URL: Repository, https://github.com/Chang-LeHung/zettcode
6
6
  Author: Chang-LeHung
@@ -18,7 +18,7 @@ Requires-Python: >=3.10
18
18
  Requires-Dist: exceptiongroup<2,>=1.2; python_version < '3.11'
19
19
  Requires-Dist: pydantic<3,>=2
20
20
  Requires-Dist: tomli<3,>=2; python_version < '3.11'
21
- Requires-Dist: zett-agent<0.3,>=0.2.10
22
- Requires-Dist: zettcode-tools<0.6,>=0.5.1
21
+ Requires-Dist: zett-agent<0.4,>=0.3.2
22
+ Requires-Dist: zettcode-tools<0.6,>=0.5.3
23
23
  Provides-Extra: tiktoken
24
24
  Requires-Dist: tiktoken<1,>=0.12; extra == 'tiktoken'
@@ -18,9 +18,9 @@ classifiers = [
18
18
  ]
19
19
  dependencies = [
20
20
  # The coding tools the runtime hands the model, a leaf of its own beneath the engine.
21
- "zettcode-tools>=0.5.1,<0.6",
21
+ "zettcode-tools>=0.5.3,<0.6",
22
22
  # The runtime this is a core over. Imported directly: the agent, the message types, the approval mode.
23
- "zett-agent>=0.2.10,<0.3",
23
+ "zett-agent>=0.3.2,<0.4",
24
24
  # The session store's records are pydantic models.
25
25
  "pydantic>=2,<3",
26
26
  # The stdlib gained both of these in 3.11; older runtimes take the backports.
@@ -134,7 +134,7 @@ STATUSLINE_KEYS = frozenset(STATUSLINE_NAMES)
134
134
  SHELL_KEYS = frozenset({"network"})
135
135
 
136
136
  #: Keys the ``[harness]`` table may set.
137
- HARNESS_KEYS = frozenset({"subagent", "subagent_max_iterations", "subagent_messaging", "todowrite", "jit"})
137
+ HARNESS_KEYS = frozenset({"subagent", "subagent_max_iterations", "subagent_messaging", "todowrite", "jit", "use"})
138
138
 
139
139
  #: Model calls one subagent may make for the task it was given. A child that is exploring or editing
140
140
  #: a real codebase makes dozens (a search, a read, an edit, a re-read, for each file), and the framework's
@@ -190,10 +190,11 @@ KEEP_SHARE = 4
190
190
 
191
191
  #: Tokens one reply may spend before the endpoint cuts it off, when the config does not say otherwise. The providers'
192
192
  #: own defaults are much smaller — 4,096 on most chat endpoints, 16,384 on Anthropic — and a long answer hit that
193
- #: limit, ended early, and cost the reader a `/continue` for what was one reply. Twenty thousand is under what every
194
- #: current frontier model emits and over what a long answer needs, and an entry whose endpoint refuses a budget says
195
- #: so on its first request, where the fix is one number in the file.
196
- DEFAULT_MAX_TOKENS = 20_000
193
+ #: limit, ended early, and cost the reader a `/continue` for what was one reply. Sixty-four thousand is under what
194
+ #: every current frontier model emits and over what a long answer needs, while still leaving a 128,000-token window
195
+ #: room for the request; an entry whose endpoint refuses a budget says so on its first request, where the fix is one
196
+ #: number in the file.
197
+ DEFAULT_MAX_TOKENS = 64_000
197
198
 
198
199
  #: The sampling temperature a model may be given: 0 is the most deterministic. OpenAI-compatible endpoints take up to
199
200
  #: 2; Anthropic's Messages API stops at 1, which is checked for an ``anthropic`` entry.
@@ -519,6 +520,8 @@ class ZettCodeConfig:
519
520
  update_file: Where that check leaves what it found; one small JSON file.
520
521
  harness_dir: Where the just-in-time harness keeps each session's tools, one folder per session id. Directly
521
522
  under ``~/.zettcode`` by default, not inside the session store, so clearing sessions does not take them.
523
+ harness_use: The name of a library harness (``zettcode harness``) that every session works in, in place of its
524
+ own folder; empty by default. ``[harness] use`` sets it, and ``/harness use`` writes it.
522
525
  plugins_file: The older switch file. It is still read and merged with ``[plugins] disable``; a switch written
523
526
  now removes the name from it, so it cannot go on overriding the configuration.
524
527
  plugins_dir: Where single-file plugins live, one ``.py`` each; a file there is loaded at the next start
@@ -567,6 +570,8 @@ class ZettCodeConfig:
567
570
  update_enabled: bool = True
568
571
  update_file: Path = DEFAULT_UPDATE_FILE
569
572
  harness_dir: Path = DEFAULT_HARNESS_DIR
573
+ #: The library harness every session works in (``[harness] use``); empty means each session's own.
574
+ harness_use: str = ""
570
575
  #: Plugins the reader switched off from the shell; a JSON file, read at the next start.
571
576
  plugins_file: Path = DEFAULT_PLUGINS_FILE
572
577
  #: Single-file plugins: one ``.py`` each, loaded at the next start instead of being installed into site-packages.
@@ -1136,6 +1141,7 @@ def load_config(
1136
1141
  subagent_messaging=bool(_setting(harness, "subagent_messaging", bool, False)),
1137
1142
  todowrite_enabled=bool(_setting(harness, "todowrite", bool, False)),
1138
1143
  jit_harness=bool(_setting(harness, "jit", bool, True)),
1144
+ harness_use=str(_setting(harness, "use", str, "")).strip(),
1139
1145
  shell_network=bool(_setting(shell, "network", bool, False)),
1140
1146
  sessions_enabled=bool(_setting(sessions, "enabled", bool, True)),
1141
1147
  update_enabled=bool(_setting(update, "enabled", bool, True)),
@@ -412,6 +412,11 @@ def set_string_list(text: str, table: str, key: str, values: Sequence[str], *, w
412
412
  return _set_key(text, table, key, literal, list(values), where=where, command=command)
413
413
 
414
414
 
415
+ def set_string(text: str, table: str, key: str, value: str, *, where: Path, command: str) -> str:
416
+ """Return ``text`` with ``[table] key`` set to a string, touching nothing else, checked like every key edit."""
417
+ return _set_key(text, table, key, toml_string(value), value, where=where, command=command)
418
+
419
+
415
420
  def set_boolean(text: str, table: str, key: str, enabled: bool, *, where: Path, command: str) -> str:
416
421
  """Return ``text`` with ``[table] key`` set to a boolean, touching nothing else.
417
422
 
@@ -141,7 +141,7 @@ class ContextMixin(AgentBase):
141
141
  reader may have moved to another session by the time it is wanted.
142
142
  """
143
143
  instructions = self.runtime.context.instructions or (
144
- SystemMessage(content=build_system_prompt(self.runtime.config)),
144
+ SystemMessage(content=build_system_prompt(self.runtime.config, role=self.runtime.system_prompt)),
145
145
  )
146
146
  checkpoint, tail = session.active()
147
147
  stored: list[AnyMessage] = [checkpoint.message[0]] if checkpoint is not None else []
@@ -120,16 +120,16 @@ WARM_MODULES_WHEN = (
120
120
 
121
121
 
122
122
  def _load_engine_plugins(config: ZettCodeConfig, shared: Mapping[str, object]) -> Plugins:
123
- """Load the installed plugins that need only the engine, and the switches that turn them off.
123
+ """Load every installed plugin, and honour the switches that turn them off.
124
124
 
125
- The engine ships no built-in plugin of its own: composing the agent (the tools, the store, the permission, the
126
- compaction, the MCP client) is :mod:`zettcode.sdk.builtins`, which passes its own loader here. A runtime built
127
- this way still honours ``[plugins]`` for the distributions and single files a reader installed.
125
+ The engine ships no plugin of its own: composing the agent (the tools, the store, the permission, the compaction,
126
+ the MCP client) is done by plugins that distributions publish, found through their entry points like any other.
127
+ A front end that has a screen, or that requires some of them by name, passes its own loader here.
128
128
  """
129
- return load_plugins(config, builtins=(), shared=shared, agent=False)
129
+ return load_plugins(config, shared=shared)
130
130
 
131
131
 
132
- def build_system_prompt(config: ZettCodeConfig, *, now: datetime | None = None) -> str:
132
+ def build_system_prompt(config: ZettCodeConfig, *, now: datetime | None = None, role: str | None = None) -> str:
133
133
  """Build request instructions with an explicit workspace boundary.
134
134
 
135
135
  The prompt is the head of every request, so whatever it contains is part of
@@ -141,9 +141,13 @@ def build_system_prompt(config: ZettCodeConfig, *, now: datetime | None = None)
141
141
  config: Settings the runtime was built from.
142
142
  now: Moment to describe, injectable for tests; defaults to the local
143
143
  current time.
144
+ role: The part this run plays, appended after the rules above — a client's
145
+ ``system_prompt``, which is what ``daemon spawn --system`` sets. It is
146
+ the process's, not the session's, and blank or ``None`` adds nothing,
147
+ so a program that sets no role sends the head every other one sends.
144
148
  """
145
149
  moment = now or datetime.now().astimezone()
146
- return f"""You are ZettCode, a focused coding agent working with the user in one local workspace.
150
+ prompt = f"""You are ZettCode, a focused coding agent working with the user in one local workspace.
147
151
 
148
152
  Inspect relevant files before editing. Make focused changes, preserve unrelated work,
149
153
  and run checks proportional to risk. Explain blockers concretely. Do not perform
@@ -151,8 +155,9 @@ destructive actions unless the user explicitly requests them.
151
155
 
152
156
  Changing files: use the file tools, and only them. To edit a file that exists, call
153
157
  edit directly: read the lines first, copy old_string exactly, and make one change a
154
- call — call edit again for the next one. Use write_file for a new file or a deliberate
155
- whole-file rewrite.
158
+ call — call edit again for the next one. A read prefixes each line with its number and a
159
+ tab; the prefix is not in the file, so leave it out of old_string and write_file content.
160
+ Use write_file for a new file or a deliberate whole-file rewrite.
156
161
  Never change a file's contents through the shell: no sed, awk, perl -i,
157
162
  python -c, a script you write to do the editing, cat or echo with a redirection, a heredoc,
158
163
  or tee. There is no delete tool: remove a file with rm in the shell, like any command.
@@ -162,9 +167,10 @@ edit fails, read the file again and retry it with corrected old_string; do not f
162
167
  back to a shell command.
163
168
 
164
169
  Reading files: call read tool, and do not shell out to cat, sed, head, tail, nl, awk or
165
- python -c to look at a file. read takes file_path, offset and limit (at most 2000
166
- lines a call), so a long file is read in the parts you need rather than whole; the
167
- reply says where to continue when more remains. Those shell tools are for what read
170
+ python -c to look at a file. read takes file_path, offset and limit (2,000
171
+ lines a call and never more, or 50 KB, whichever comes first; pass a smaller
172
+ limit to read less), so a long file is read in the parts you need rather than
173
+ whole; the reply says where to continue when more remains. Those shell tools are for what read
168
174
  cannot do — a binary, a file past its limits, a transformation no line range
169
175
  expresses — and only then; say what read could not do. grep and glob are for finding
170
176
  what to read — the matching lines — not for printing a file. A bash call starts in
@@ -181,6 +187,14 @@ Runtime environment:
181
187
 
182
188
  Treat the workspace as the current working directory. Prefer relative paths in tool
183
189
  calls. Verify mutable facts with tools instead of assuming this startup snapshot is current."""
190
+ role = (role or "").strip()
191
+ if not role:
192
+ return prompt
193
+ return (
194
+ f"{prompt}\n\n# Your role\n"
195
+ "This session was started with a role for it. Follow it along with the instructions above:\n\n"
196
+ f"{role}"
197
+ )
184
198
 
185
199
 
186
200
  def _build_provider(selected: ModelConfig) -> Adapter:
@@ -204,7 +218,7 @@ def _build_provider(selected: ModelConfig) -> Adapter:
204
218
  ``max_tokens``.
205
219
 
206
220
  A budget larger than the window is sent as the window: a reply longer than the context that has to hold it is not
207
- a request any endpoint can serve, and the default of 20,000 would otherwise make every small-context entry
221
+ a request any endpoint can serve, and the default of 64,000 would otherwise make every small-context entry
208
222
  impossible the moment it was read. The entry keeps the number it was given; only the request is bounded.
209
223
  """
210
224
  token = selected.token or KEYLESS_TOKEN
@@ -346,6 +360,10 @@ class ZettCodeEngine:
346
360
  #: The lock on the session this process has open: only the process that holds it writes to the session.
347
361
  #: A runtime that was not given one (a test's) shares nothing, so every claim is granted.
348
362
  session_locks: SessionGuard = field(default_factory=NoLocks)
363
+ #: A role this process was started with, appended to the system prompt of every request it makes: what a client's
364
+ #: ``system_prompt`` sets, and what ``daemon spawn --system`` hands a daemon. ``None`` adds nothing. It belongs to
365
+ #: the process and not to the session — a stored conversation resumed elsewhere runs without it.
366
+ system_prompt: str | None = None
349
367
  event_dispatcher: AgentEventDispatcher | None = None
350
368
  client: AgentClient | None = None
351
369
  model: Adapter | None = None
@@ -385,7 +403,12 @@ class ZettCodeEngine:
385
403
 
386
404
  @classmethod
387
405
  def preview(
388
- cls, config: ZettCodeConfig, load: PluginLoader | None = None, *, shared: Mapping[str, object] | None = None
406
+ cls,
407
+ config: ZettCodeConfig,
408
+ load: PluginLoader | None = None,
409
+ *,
410
+ shared: Mapping[str, object] | None = None,
411
+ system_prompt: str | None = None,
389
412
  ) -> ZettCodeEngine:
390
413
  """Build the session side of the runtime, without a provider.
391
414
 
@@ -402,6 +425,7 @@ class ZettCodeEngine:
402
425
  hands over itself is refused. A name another plugin provides fails that plugin unless the plugin
403
426
  is written to keep what it is given — the messaging plugin keeps a supplied ``INBOX`` or
404
427
  ``SESSION_LINK``, which is how a front end hands over its line or replaces the queue.
428
+ system_prompt: A role for this run, appended to the system prompt of every request; ``None`` adds none.
405
429
 
406
430
  Raises:
407
431
  ValueError: ``shared`` names a service the runtime hands over itself (``DELIVERY``, ``PERSISTENCE``, ...).
@@ -494,6 +518,7 @@ class ZettCodeEngine:
494
518
  active_model=selected,
495
519
  compaction_counter=counter,
496
520
  plugins=plugins,
521
+ system_prompt=system_prompt,
497
522
  _models=models,
498
523
  file_models=file_models,
499
524
  provided_models=offered,
@@ -655,11 +680,14 @@ class ZettCodeEngine:
655
680
  # store, the shell approval, the compaction ... see `zettcode.sdk.builtins`) in the order
656
681
  # they are listed there, then any a third-party plugin brings, and last the one host that drives
657
682
  # the plugins' hooks, which places itself at the earliest priority any plugin asked for.
658
- extensions = [*self.plugins.agent_extensions(AgentBuild(model=model)), *self.plugins.extensions]
683
+ extensions = [
684
+ *self.plugins.agent_extensions(AgentBuild(model=model, plugins=self.plugins)),
685
+ *self.plugins.extensions,
686
+ ]
659
687
  client = await create_agent(
660
688
  model,
661
689
  config=AgentRunConfig(session_id=self.session_id),
662
- system_prompt=build_system_prompt(self.config),
690
+ system_prompt=build_system_prompt(self.config, role=self.system_prompt),
663
691
  extensions=extensions,
664
692
  reasoning_effort=self.config.reasoning_effort,
665
693
  parallel_tool_call=self.config.parallel_tool_call,
@@ -39,6 +39,7 @@ from typing import TYPE_CHECKING, Any
39
39
 
40
40
  from zettcode.config import DEFAULT_SUBAGENT_MAX_ITERATIONS
41
41
 
42
+ from ..plugins.children import ChildSession
42
43
  from ..storage import SessionStore
43
44
  from .capabilities import ModelCapabilities
44
45
 
@@ -96,7 +97,8 @@ CODING_PROMPT = (
96
97
  "reads your report."
97
98
  "\n\nHow to work:\n"
98
99
  "- Read before you edit: the file, and enough of its neighbours to match their conventions (naming, "
99
- "imports, error handling, tests). Follow the project instructions you were given.\n"
100
+ "imports, error handling, tests). A read prefixes each line with its number and a tab; the prefix is not in "
101
+ "the file, so leave it out of old_string and write_file content. Follow the project instructions you were given.\n"
100
102
  "- Make the smallest change that does what was asked. Do not refactor, rename, reformat or fix what you "
101
103
  "were not asked to touch; mention it instead.\n"
102
104
  "- Prefer edit with a short, unique old_string to rewriting a whole file, and read the result "
@@ -120,7 +122,8 @@ CODING_SHELL_PROMPT = (
120
122
  "There is no file-deletion tool: remove a file with rm, which follows the same rule."
121
123
  "\n\nHow to work:\n"
122
124
  "- Read before you edit: the file, and enough of its neighbours to match their conventions (naming, "
123
- "imports, error handling, tests). Follow the project instructions you were given.\n"
125
+ "imports, error handling, tests). A read prefixes each line with its number and a tab; the prefix is not in "
126
+ "the file, so leave it out of old_string and write_file content. Follow the project instructions you were given.\n"
124
127
  "- Make the smallest change that does what was asked. Do not refactor, rename, reformat or fix what you "
125
128
  "were not asked to touch; mention it instead.\n"
126
129
  "- Prefer edit with a short, unique old_string to rewriting a whole file, and read the result "
@@ -262,10 +265,12 @@ def _extension_class() -> Callable[..., SubAgentExtension]:
262
265
  *,
263
266
  capabilities: ModelCapabilities,
264
267
  shares: Callable[[AgentExtension], bool] | None = None,
268
+ child_extensions: Callable[[ChildSession], tuple[AgentExtension, ...]] | None = None,
265
269
  ) -> None:
266
270
  super().__init__(subagents)
267
271
  self._capabilities = capabilities
268
272
  self._shares = shares
273
+ self._child_extensions = child_extensions
269
274
 
270
275
  def _levels(self, context: AgentRunContext) -> tuple[ReasoningEffort, ...]:
271
276
  """Return the levels the parent's model takes: every one for a model the runtime has no entry for."""
@@ -293,12 +298,20 @@ def _extension_class() -> Callable[..., SubAgentExtension]:
293
298
  config = self._capabilities.config_of(parent)
294
299
  if config is not None:
295
300
  effort = config.accepted_effort(effort)
301
+ fresh = (
302
+ ()
303
+ if self._child_extensions is None
304
+ else self._child_extensions(ChildSession("subagent", definition.name))
305
+ )
306
+ names = {extension.name for extension in fresh}
307
+ defaults = (*definition.extensions, *self._inherited(definition, context))
308
+ extensions = (*(extension for extension in defaults if extension.name not in names), *fresh)
296
309
  child = await RelayingAgent.create(
297
310
  parent,
298
311
  config=AgentRunConfig(session_id=child_session_id, parent_session_id=context.session_id),
299
312
  system_prompt=definition.system_prompt,
300
313
  tools=definition.tools,
301
- extensions=(*definition.extensions, *self._inherited(definition, context)),
314
+ extensions=extensions,
302
315
  reasoning_effort=effort,
303
316
  max_iterations=definition.max_iterations,
304
317
  )
@@ -356,6 +369,7 @@ def subagent_extension(
356
369
  workspace: Path | None = None,
357
370
  max_iterations: int = DEFAULT_SUBAGENT_MAX_ITERATIONS,
358
371
  shares: Callable[[AgentExtension], bool] | None = None,
372
+ child_extensions: Callable[[ChildSession], tuple[AgentExtension, ...]] | None = None,
359
373
  ) -> AgentExtension:
360
374
  """Return the ``task`` tool with ZettCode's profiles and their tool boundaries.
361
375
 
@@ -388,4 +402,5 @@ def subagent_extension(
388
402
  ),
389
403
  capabilities=capabilities,
390
404
  shares=shares,
405
+ child_extensions=child_extensions,
391
406
  )
@@ -47,6 +47,8 @@ class UsageSnapshot:
47
47
  output_tokens: Generated tokens, including reasoning when reported.
48
48
  cache_read_tokens: Input tokens the provider served from its cache.
49
49
  cache_write_tokens: Input tokens written into the provider cache.
50
+ cache_reported: Whether every counted call reported how much input it read from the cache; a
51
+ call that reported nothing leaves the share unknown, not zero.
50
52
  reasoning_tokens: Reasoning subset of ``output_tokens``.
51
53
  seconds: Time from the first token of each call to that call's terminal
52
54
  response, summed: the window output was generated in, the rate's
@@ -60,6 +62,7 @@ class UsageSnapshot:
60
62
  output_tokens: int = 0
61
63
  cache_read_tokens: int = 0
62
64
  cache_write_tokens: int = 0
65
+ cache_reported: bool = True
63
66
  reasoning_tokens: int = 0
64
67
  seconds: float = 0.0
65
68
  requests: int = 0
@@ -72,8 +75,15 @@ class UsageSnapshot:
72
75
 
73
76
  @property
74
77
  def cache_hit_rate(self) -> float | None:
75
- """Return the cached share of all input, or None before any input."""
76
- return self.cache_read_tokens / self.input_tokens if self.input_tokens else None
78
+ """Return the cached share of all input, or None when it was not measured.
79
+
80
+ None means there is no ratio to report: no input yet, or a counted call said nothing about what
81
+ it read from the cache. A provider that reports a zero read has still measured a miss, and that
82
+ is 0.0 rather than None.
83
+ """
84
+ if not self.cache_reported or not self.input_tokens:
85
+ return None
86
+ return self.cache_read_tokens / self.input_tokens
77
87
 
78
88
  @property
79
89
  def output_rate(self) -> float | None:
@@ -107,6 +117,9 @@ class UsageSnapshot:
107
117
  output_tokens=self.output_tokens + usage.output_tokens,
108
118
  cache_read_tokens=self.cache_read_tokens + usage.cache_read_tokens,
109
119
  cache_write_tokens=self.cache_write_tokens + usage.cache_write_tokens,
120
+ # One call that said nothing about its cache leaves the running share unknown: the
121
+ # percentage would be over the calls that happened to report.
122
+ cache_reported=self.cache_reported and usage.cache_reported,
110
123
  reasoning_tokens=self.reasoning_tokens + usage.reasoning_tokens,
111
124
  seconds=self.seconds + max(0.0, seconds),
112
125
  requests=self.requests + 1,
@@ -115,13 +128,14 @@ class UsageSnapshot:
115
128
  context_tokens=(usage.input_tokens or self.context_tokens) if context_tokens is None else context_tokens,
116
129
  )
117
130
 
118
- def to_payload(self) -> dict[str, int | float]:
131
+ def to_payload(self) -> dict[str, int | float | bool]:
119
132
  """Return the JSON-serializable form carried by the custom event."""
120
133
  return {
121
134
  "input_tokens": self.input_tokens,
122
135
  "output_tokens": self.output_tokens,
123
136
  "cache_read_tokens": self.cache_read_tokens,
124
137
  "cache_write_tokens": self.cache_write_tokens,
138
+ "cache_reported": self.cache_reported,
125
139
  "reasoning_tokens": self.reasoning_tokens,
126
140
  "seconds": self.seconds,
127
141
  "requests": self.requests,
@@ -145,6 +159,9 @@ class UsageSnapshot:
145
159
  output_tokens=count("output_tokens"),
146
160
  cache_read_tokens=count("cache_read_tokens"),
147
161
  cache_write_tokens=count("cache_write_tokens"),
162
+ # A payload written before this flag existed said what the provider said, so it keeps
163
+ # the old meaning rather than turning every replayed session's share into unknown.
164
+ cache_reported=payload.get("cache_reported") is not False,
148
165
  reasoning_tokens=count("reasoning_tokens"),
149
166
  seconds=seconds("seconds"),
150
167
  requests=count("requests"),