ptbk 0.119.0-21 → 0.119.0-26

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 (223) hide show
  1. package/README.md +16 -9
  2. package/dist/browser/browser.d.ts +50 -36
  3. package/dist/browser/browser.js +162 -118
  4. package/dist/browser/browser.js.map +1 -1
  5. package/dist/browser/fleet.d.ts +59 -0
  6. package/dist/browser/fleet.js +148 -0
  7. package/dist/browser/fleet.js.map +1 -0
  8. package/dist/browser/recording.js +2 -2
  9. package/dist/browser/recording.js.map +1 -1
  10. package/dist/chats/conversation.d.ts +1 -1
  11. package/dist/chats/conversation.js +8 -6
  12. package/dist/chats/conversation.js.map +1 -1
  13. package/dist/chats/local.js +5 -5
  14. package/dist/chats/local.js.map +1 -1
  15. package/dist/cli/commands/add.js +1 -1
  16. package/dist/cli/commands/add.js.map +1 -1
  17. package/dist/cli/commands/boilerplates.js +1 -1
  18. package/dist/cli/commands/boilerplates.js.map +1 -1
  19. package/dist/cli/commands/find.js +1 -1
  20. package/dist/cli/commands/find.js.map +1 -1
  21. package/dist/cli/commands/fix.js +9 -4
  22. package/dist/cli/commands/fix.js.map +1 -1
  23. package/dist/cli/commands/init.d.ts +5 -0
  24. package/dist/cli/commands/init.js +279 -118
  25. package/dist/cli/commands/init.js.map +1 -1
  26. package/dist/cli/commands/migrate.js +1 -1
  27. package/dist/cli/commands/migrate.js.map +1 -1
  28. package/dist/cli/commands/ping.js +20 -12
  29. package/dist/cli/commands/ping.js.map +1 -1
  30. package/dist/cli/commands/plan.js +20 -12
  31. package/dist/cli/commands/plan.js.map +1 -1
  32. package/dist/cli/commands/recover.js +1 -1
  33. package/dist/cli/commands/recover.js.map +1 -1
  34. package/dist/cli/commands/restart.js +6 -5
  35. package/dist/cli/commands/restart.js.map +1 -1
  36. package/dist/cli/commands/run.js +26 -15
  37. package/dist/cli/commands/run.js.map +1 -1
  38. package/dist/cli/commands/start.js +11 -7
  39. package/dist/cli/commands/start.js.map +1 -1
  40. package/dist/cli/commands/tool.d.ts +19 -0
  41. package/dist/cli/commands/tool.js +320 -0
  42. package/dist/cli/commands/tool.js.map +1 -0
  43. package/dist/cli/commands/verify.js +1 -1
  44. package/dist/cli/commands/verify.js.map +1 -1
  45. package/dist/cli/common.d.ts +7 -1
  46. package/dist/cli/common.js +37 -20
  47. package/dist/cli/common.js.map +1 -1
  48. package/dist/cli/compatibility.d.ts +8 -0
  49. package/dist/cli/compatibility.js +32 -0
  50. package/dist/cli/compatibility.js.map +1 -0
  51. package/dist/cli/parser.js +7 -3
  52. package/dist/cli/parser.js.map +1 -1
  53. package/dist/cli/registry.js +182 -7
  54. package/dist/cli/registry.js.map +1 -1
  55. package/dist/cli/service-view.js +1 -1
  56. package/dist/cli/service-view.js.map +1 -1
  57. package/dist/cli/sources.js +2 -0
  58. package/dist/cli/sources.js.map +1 -1
  59. package/dist/cli/spec.js +8 -6
  60. package/dist/cli/spec.js.map +1 -1
  61. package/dist/common/naming.d.ts +53 -0
  62. package/dist/common/naming.js +90 -0
  63. package/dist/common/naming.js.map +1 -0
  64. package/dist/engine/agenda.d.ts +70 -9
  65. package/dist/engine/agenda.js +294 -61
  66. package/dist/engine/agenda.js.map +1 -1
  67. package/dist/engine/executor.d.ts +5 -0
  68. package/dist/engine/executor.js +24 -11
  69. package/dist/engine/executor.js.map +1 -1
  70. package/dist/engine/harnesses.d.ts +82 -18
  71. package/dist/engine/harnesses.js +191 -39
  72. package/dist/engine/harnesses.js.map +1 -1
  73. package/dist/engine/preview.js +2 -2
  74. package/dist/engine/preview.js.map +1 -1
  75. package/dist/engine/routing.d.ts +10 -0
  76. package/dist/engine/routing.js +67 -28
  77. package/dist/engine/routing.js.map +1 -1
  78. package/dist/engine/snapshot.d.ts +107 -7
  79. package/dist/engine/state.d.ts +4 -2
  80. package/dist/engine/state.js.map +1 -1
  81. package/dist/engine/sync.d.ts +96 -0
  82. package/dist/engine/sync.js +199 -0
  83. package/dist/engine/sync.js.map +1 -0
  84. package/dist/engine/team.d.ts +1 -0
  85. package/dist/engine/team.js +12 -8
  86. package/dist/engine/team.js.map +1 -1
  87. package/dist/expert/_main.md +96 -67
  88. package/dist/expert/agents/_index.md +10 -7
  89. package/dist/expert/agents/core-agents.md +4 -2
  90. package/dist/expert/agents/fixer.md +15 -12
  91. package/dist/expert/cli/acceptance.md +96 -78
  92. package/dist/expert/cli/api.md +26 -14
  93. package/dist/expert/cli/autonomous-operation.md +17 -19
  94. package/dist/expert/cli/browser.md +24 -24
  95. package/dist/expert/cli/checks.md +18 -10
  96. package/dist/expert/cli/cli.md +48 -41
  97. package/dist/expert/cli/configuration-migrations.md +43 -0
  98. package/dist/expert/cli/configuration.md +24 -14
  99. package/dist/expert/cli/controls.md +23 -11
  100. package/dist/expert/cli/dashboard-details.md +28 -10
  101. package/dist/expert/cli/dashboard-layout.md +15 -15
  102. package/dist/expert/cli/dashboard.md +8 -10
  103. package/dist/expert/cli/environment.md +33 -0
  104. package/dist/expert/cli/execution.md +10 -8
  105. package/dist/expert/cli/git-persistence.md +27 -21
  106. package/dist/expert/cli/git-preflight.md +8 -4
  107. package/dist/expert/cli/git-synchronization.md +32 -6
  108. package/dist/expert/cli/harness-plugins.md +12 -12
  109. package/dist/expert/cli/harnesses.md +22 -16
  110. package/dist/expert/cli/initialization-ui.md +14 -14
  111. package/dist/expert/cli/initialization.md +31 -21
  112. package/dist/expert/cli/installation.md +6 -0
  113. package/dist/expert/cli/isolation.md +5 -3
  114. package/dist/expert/cli/releasing.md +17 -3
  115. package/dist/expert/cli/retries.md +10 -8
  116. package/dist/expert/cli/run.md +7 -5
  117. package/dist/expert/cli/start.md +21 -15
  118. package/dist/expert/cli/status-view.md +15 -17
  119. package/dist/expert/cli/synchronization-setup.md +24 -0
  120. package/dist/expert/cli/terminal.md +14 -16
  121. package/dist/expert/cli/tool-management.md +60 -0
  122. package/dist/expert/cli/tool-runtime.md +35 -0
  123. package/dist/expert/cli/tools-migration.md +50 -0
  124. package/dist/expert/cli/tools.md +73 -0
  125. package/dist/expert/cli/trace-configuration.md +9 -11
  126. package/dist/expert/cli/updates.md +7 -7
  127. package/dist/expert/cli/windows-installer.md +67 -0
  128. package/dist/expert/cli/workspace.md +2 -0
  129. package/dist/expert/coding-standards/_index.md +11 -0
  130. package/dist/expert/coding-standards/naming.md +30 -0
  131. package/dist/expert/coding-standards/shared-logic.md +27 -0
  132. package/dist/expert/concepts/automatic-repair.md +33 -0
  133. package/dist/expert/concepts/engine-and-project.md +7 -5
  134. package/dist/expert/dictionary.md +18 -18
  135. package/dist/harness/plugins/claude-code/index.js +4 -2
  136. package/dist/harness/plugins/claude-code/index.js.map +1 -1
  137. package/dist/harness/plugins/cline/index.js +1 -0
  138. package/dist/harness/plugins/cline/index.js.map +1 -1
  139. package/dist/harness/plugins/gemini/index.js +2 -0
  140. package/dist/harness/plugins/gemini/index.js.map +1 -1
  141. package/dist/harness/plugins/github-copilot/index.js +1 -0
  142. package/dist/harness/plugins/github-copilot/index.js.map +1 -1
  143. package/dist/harness/plugins/openai-codex/index.js +13 -9
  144. package/dist/harness/plugins/openai-codex/index.js.map +1 -1
  145. package/dist/harness/plugins/opencode/index.js +1 -0
  146. package/dist/harness/plugins/opencode/index.js.map +1 -1
  147. package/dist/harness/plugins/qwen-code/index.js +1 -0
  148. package/dist/harness/plugins/qwen-code/index.js.map +1 -1
  149. package/dist/harness/types.d.ts +16 -5
  150. package/dist/project/config.d.ts +135 -48
  151. package/dist/project/config.js +331 -119
  152. package/dist/project/config.js.map +1 -1
  153. package/dist/project/configuration.d.ts +43 -0
  154. package/dist/project/configuration.js +165 -0
  155. package/dist/project/configuration.js.map +1 -0
  156. package/dist/project/defaults.d.ts +4 -2
  157. package/dist/project/defaults.js +10 -5
  158. package/dist/project/defaults.js.map +1 -1
  159. package/dist/project/git.d.ts +28 -0
  160. package/dist/project/git.js +81 -0
  161. package/dist/project/git.js.map +1 -1
  162. package/dist/project/migrations.d.ts +96 -0
  163. package/dist/project/migrations.js +301 -0
  164. package/dist/project/migrations.js.map +1 -0
  165. package/dist/project/publication.d.ts +57 -0
  166. package/dist/project/publication.js +117 -0
  167. package/dist/project/publication.js.map +1 -0
  168. package/dist/project/readiness.d.ts +14 -5
  169. package/dist/project/readiness.js +23 -22
  170. package/dist/project/readiness.js.map +1 -1
  171. package/dist/project/setup.js +2 -0
  172. package/dist/project/setup.js.map +1 -1
  173. package/dist/service/actions.d.ts +12 -4
  174. package/dist/service/actions.js +32 -2
  175. package/dist/service/actions.js.map +1 -1
  176. package/dist/service/control.d.ts +1 -1
  177. package/dist/service/control.js +1 -1
  178. package/dist/service/host.d.ts +5 -2
  179. package/dist/service/host.js +26 -16
  180. package/dist/service/host.js.map +1 -1
  181. package/dist/service/http.js +7 -2
  182. package/dist/service/http.js.map +1 -1
  183. package/dist/service/launcher.d.ts +2 -1
  184. package/dist/service/launcher.js +1 -0
  185. package/dist/service/launcher.js.map +1 -1
  186. package/dist/service/records.d.ts +3 -0
  187. package/dist/service/records.js +1 -1
  188. package/dist/service/records.js.map +1 -1
  189. package/dist/service/spa/app.css +39 -0
  190. package/dist/service/spa/app.js +242 -39
  191. package/dist/service/worker.js +1 -0
  192. package/dist/service/worker.js.map +1 -1
  193. package/dist/tools/environment.d.ts +122 -0
  194. package/dist/tools/environment.js +199 -0
  195. package/dist/tools/environment.js.map +1 -0
  196. package/dist/tools/model.d.ts +180 -0
  197. package/dist/tools/model.js +111 -0
  198. package/dist/tools/model.js.map +1 -0
  199. package/dist/tools/normalize.d.ts +51 -0
  200. package/dist/tools/normalize.js +321 -0
  201. package/dist/tools/normalize.js.map +1 -0
  202. package/dist/tools/operations.d.ts +198 -0
  203. package/dist/tools/operations.js +378 -0
  204. package/dist/tools/operations.js.map +1 -0
  205. package/dist/tools/readiness.d.ts +72 -0
  206. package/dist/tools/readiness.js +160 -0
  207. package/dist/tools/readiness.js.map +1 -0
  208. package/dist/ui/dashboard.d.ts +3 -0
  209. package/dist/ui/dashboard.js +173 -2
  210. package/dist/ui/dashboard.js.map +1 -1
  211. package/dist/ui/details.d.ts +35 -1
  212. package/dist/ui/details.js +103 -2
  213. package/dist/ui/details.js.map +1 -1
  214. package/dist/ui/secret.d.ts +8 -0
  215. package/dist/ui/secret.js +41 -0
  216. package/dist/ui/secret.js.map +1 -0
  217. package/dist/ui/status-view.d.ts +1 -0
  218. package/dist/ui/status-view.js +33 -8
  219. package/dist/ui/status-view.js.map +1 -1
  220. package/dist/ui/tool-forms.d.ts +24 -0
  221. package/dist/ui/tool-forms.js +133 -0
  222. package/dist/ui/tool-forms.js.map +1 -0
  223. package/package.json +1 -1
@@ -1,24 +1,33 @@
1
1
  import { formatDuration, parseDuration } from '../common/duration.js';
2
2
  import { PtbkError } from '../common/errors.js';
3
3
  import { readTextIfExists, writeFileAtomic } from '../common/fs.js';
4
+ import { canonicalEnumValue, matchEnum, matchProperty } from '../common/naming.js';
4
5
  import { systemTimeZone } from '../common/time.js';
6
+ import { ENGINE_VERSION } from '../common/version.js';
7
+ import { HARNESS_VARIANTS, THINKING_LEVELS, enabledToolsOf, harnessPluginId, harnessVariantOf } from '../tools/model.js';
8
+ import { parseTools } from '../tools/normalize.js';
9
+ import { applyConfigurationMigration, planConfigurationMigration } from './migrations.js';
10
+ export { THINKING_LEVELS };
5
11
  /**
6
- * Supported reasoning/effort settings (each harness supports a subset).
7
- *
8
- * @private project configuration
9
- */
10
- export const THINKING_LEVELS = ['low', 'medium', 'high', 'xhigh', 'max'];
11
- /**
12
- * Every scalar setting (the per-harness map is handled separately).
12
+ * Every scalar setting (named tools live in `tools`, see the shared tool contract).
13
13
  *
14
14
  * @private project configuration
15
15
  */
16
16
  export const SETTING_SPECS = [
17
+ { key: 'isAutoSync', type: 'boolean', default: false, description: 'Automatic Git synchronization with the existing origin and current branch (pull every 60 s and at operation boundaries, push completed work).', application: 'live' },
17
18
  { key: 'paths.agents', type: 'string', default: 'agents', description: 'Directory of agent Books.', application: 'restart' },
18
19
  { key: 'paths.tasks', type: 'string', default: 'tasks', description: 'Task source directory.', application: 'restart' },
19
20
  { key: 'paths.context', type: 'nullable-string', default: 'AGENTS.md', description: 'Additional project context file (null disables).', application: 'next-task' },
20
21
  { key: 'routing.defaultAgent', type: 'string', default: 'Developer', description: 'Agent used by run/fix/plan when none is selected.', application: 'next-task' },
21
- { key: 'routing.harness', type: 'string', default: undefined, env: 'PTBK_HARNESS', description: 'Default/bootstrap harness (used directly by Manager).', application: 'next-task' },
22
+ {
23
+ key: 'routing.harness',
24
+ type: 'enum',
25
+ values: HARNESS_VARIANTS.map(harnessPluginId),
26
+ default: undefined,
27
+ env: 'PTBK_HARNESS',
28
+ description: 'Default/bootstrap harness variant (used directly by Manager).',
29
+ application: 'next-task',
30
+ },
22
31
  { key: 'routing.model', type: 'string', default: undefined, env: 'PTBK_MODEL', description: 'Default model of the default harness.', application: 'next-task' },
23
32
  {
24
33
  key: 'routing.thinkingLevel',
@@ -52,15 +61,13 @@ export const SETTING_SPECS = [
52
61
  description: 'Policy for unrelated uncommitted changes.',
53
62
  application: 'next-task',
54
63
  },
55
- { key: 'execution.commit', type: 'boolean', default: true, description: 'Commit versioned changes automatically.', application: 'restart' },
56
- { key: 'execution.autoPull', type: 'boolean', default: false, description: 'Pull with rebase at task boundaries.', application: 'next-task' },
57
- { key: 'execution.autoPush', type: 'boolean', default: false, description: 'Push completed work.', application: 'next-task' },
58
- { key: 'execution.isolate', type: 'boolean', default: false, description: 'Execute tasks in a separate Git worktree.', application: 'next-task' },
59
- { key: 'execution.normalizeLineEndings', type: 'boolean', default: true, description: 'Normalize CRLF in changed files of LF projects.', application: 'next-task' },
60
- { key: 'execution.allowCredits', type: 'boolean', default: false, description: 'Authorize explicit paid-credit fallback where supported.', application: 'next-task' },
61
- { key: 'execution.questions', type: 'boolean', default: true, description: 'Allow agents to ask the user.', application: 'next-task' },
62
- { key: 'execution.preserveLogs', type: 'boolean', default: false, description: 'Keep all system logs in .ptbk/ without rotation (task traces are never rotated).', application: 'live' },
63
- { key: 'execution.autoMigrate', type: 'boolean', default: false, description: 'Run the configured migration hook after agent work.', application: 'next-task' },
64
+ { key: 'execution.isCommitEnabled', type: 'boolean', default: true, description: 'Commit versioned changes automatically.', application: 'restart' },
65
+ { key: 'execution.isIsolated', type: 'boolean', default: false, description: 'Execute tasks in a separate Git worktree.', application: 'next-task' },
66
+ { key: 'execution.isLineEndingNormalizationEnabled', type: 'boolean', default: true, description: 'Normalize CRLF in changed files of LF projects.', application: 'next-task' },
67
+ { key: 'execution.isCreditFallbackAllowed', type: 'boolean', default: false, description: 'Authorize explicit paid-credit fallback where supported.', application: 'next-task' },
68
+ { key: 'execution.isQuestionsEnabled', type: 'boolean', default: true, description: 'Allow agents to ask the user.', application: 'next-task' },
69
+ { key: 'execution.isLogPreservationEnabled', type: 'boolean', default: false, description: 'Keep all system logs in .ptbk/ without rotation (task traces are never rotated).', application: 'live' },
70
+ { key: 'execution.isAutoMigrateEnabled', type: 'boolean', default: false, description: 'Run the configured migration hook after agent work.', application: 'next-task' },
64
71
  { key: 'execution.migrateCommand', type: 'string', default: undefined, description: 'Project migration hook command.', application: 'next-task' },
65
72
  {
66
73
  key: 'execution.stallTimeout',
@@ -69,15 +76,12 @@ export const SETTING_SPECS = [
69
76
  description: 'A harness producing no output for this long is treated as stalled and retried (0 disables).',
70
77
  application: 'next-task',
71
78
  },
72
- { key: 'traces.commit', type: 'boolean', default: false, description: 'Include task traces (traces/<task-file-basename>/) in result commits; false ignores the whole traces/ tree.', application: 'next-task' },
73
- { key: 'traces.ignoreVideos', type: 'boolean', default: true, description: 'Keep trace videos out of Git even when traces are committed.', application: 'next-task' },
74
- { key: 'traces.recordVideo', type: 'boolean', default: true, description: 'Record browser activity of tasks using the project browser.', application: 'next-task' },
75
- { key: 'browser.profile', type: 'string', default: 'global', description: '`global` (dedicated Promptbook profile shared by your projects), `isolated` (this project only, in .ptbk/), `user` (your Chrome profile) or a browser profile path.', application: 'next-task' },
76
- { key: 'browser.mode', type: 'enum', values: ['headless', 'headful'], default: 'headless', description: 'Browser visibility.', application: 'next-task' },
77
- { key: 'browser.executable', type: 'string', default: undefined, description: 'Chrome-compatible browser executable.', application: 'next-task' },
78
- { key: 'server.enabled', type: 'boolean', default: true, description: 'Serve the local HTTP API and browser dashboard.', application: 'restart' },
79
+ { key: 'traces.isCommitted', type: 'boolean', default: false, description: 'Include task traces (traces/<task-file-basename>/) in result commits; false ignores the whole traces/ tree.', application: 'next-task' },
80
+ { key: 'traces.isVideoIgnored', type: 'boolean', default: true, description: 'Keep trace videos out of Git even when traces are committed.', application: 'next-task' },
81
+ { key: 'traces.isVideoRecordingEnabled', type: 'boolean', default: true, description: 'Record attributable activity of browser tools used by tasks.', application: 'next-task' },
82
+ { key: 'server.isEnabled', type: 'boolean', default: true, description: 'Serve the local HTTP API and browser dashboard.', application: 'restart' },
79
83
  { key: 'server.port', type: 'port', default: undefined, description: 'Local HTTP port (automatic from 3000 when omitted).', application: 'restart' },
80
- { key: 'server.tunnel.enabled', type: 'boolean', default: true, description: 'Protected remote tunnel when HTTP runs.', application: 'restart' },
84
+ { key: 'server.tunnel.isEnabled', type: 'boolean', default: true, description: 'Protected remote tunnel when HTTP runs.', application: 'restart' },
81
85
  {
82
86
  key: 'server.tunnel.provider',
83
87
  type: 'enum',
@@ -90,12 +94,12 @@ export const SETTING_SPECS = [
90
94
  { key: 'server.tunnel.name', type: 'string', default: undefined, description: 'Named tunnel name or UUID.', application: 'restart' },
91
95
  { key: 'server.tunnel.credentialsFile', type: 'string', default: undefined, description: 'Path to named-tunnel credentials (reference, not content).', application: 'restart' },
92
96
  { key: 'server.tunnel.tokenEnv', type: 'string', default: undefined, description: 'Environment variable holding a named-tunnel token.', application: 'restart' },
93
- { key: 'service.persist', type: 'boolean', default: true, description: 'Restart automatically after crashes and reboots.', application: 'restart' },
97
+ { key: 'service.isPersistent', type: 'boolean', default: true, description: 'Restart automatically after crashes and reboots.', application: 'restart' },
94
98
  { key: 'service.harnessUpdates', type: 'enum', values: ['auto', 'off'], default: 'auto', description: 'Automatic harness updates.', application: 'live' },
95
- { key: 'service.qr', type: 'boolean', default: true, description: 'Show QR codes.', application: 'live' },
96
- { key: 'discovery.enabled', type: 'boolean', default: true, description: 'Goal discovery when the backlog is empty.', application: 'live' },
99
+ { key: 'service.isQrShown', type: 'boolean', default: true, description: 'Show QR codes.', application: 'live' },
100
+ { key: 'discovery.isEnabled', type: 'boolean', default: true, description: 'Goal discovery when the backlog is empty.', application: 'live' },
97
101
  { key: 'discovery.cooldown', type: 'duration', default: 6 * 3_600_000, description: 'Wait after a no-work enquiry.', application: 'live' },
98
- { key: 'learning.enabled', type: 'boolean', default: true, description: 'Teacher follow-ups for open agents.', application: 'next-task' },
102
+ { key: 'learning.isEnabled', type: 'boolean', default: true, description: 'Teacher follow-ups for open agents.', application: 'next-task' },
99
103
  { key: 'team.timeout', type: 'duration', default: 300_000, description: 'Consultation timeout.', application: 'next-task' },
100
104
  { key: 'team.maxDepth', type: 'integer', default: 4, description: 'Maximal consultation depth.', application: 'next-task' },
101
105
  { key: 'team.maxCalls', type: 'integer', default: 24, description: 'Consultations per task.', application: 'next-task' },
@@ -108,6 +112,21 @@ export const SETTING_SPECS = [
108
112
  * @private project configuration
109
113
  */
110
114
  export const SETTING_SPEC_BY_KEY = new Map(SETTING_SPECS.map((spec) => [spec.key, spec]));
115
+ /**
116
+ * The explicit informational check saved when a project has no real validation. It always succeeds and means that
117
+ * real automated validation is **not** configured; it never counts as evidence of passing tests.
118
+ *
119
+ * @private project configuration
120
+ */
121
+ export const CHECK_PLACEHOLDER = 'echo "Configure project checks in ptbk.json execution.check"';
122
+ /**
123
+ * Whether a check command is the informational placeholder rather than real validation.
124
+ *
125
+ * @private project configuration
126
+ */
127
+ export function isCheckPlaceholder(check) {
128
+ return typeof check === 'string' && check.trim() === CHECK_PLACEHOLDER;
129
+ }
111
130
  /**
112
131
  * Reads a dot path from a nested object.
113
132
  *
@@ -143,7 +162,7 @@ export function setPath(object, path, value) {
143
162
  current[last] = value;
144
163
  }
145
164
  /**
146
- * Parses a raw value according to a setting spec.
165
+ * Parses a raw value according to a setting spec (tolerant: `"true"`, canonical or kebab enum spellings).
147
166
  *
148
167
  * @private project configuration
149
168
  */
@@ -187,90 +206,228 @@ export function parseSettingValue(spec, raw) {
187
206
  return { ok: false, message: parsed.reason };
188
207
  return { ok: true, value: parsed.ms };
189
208
  }
190
- case 'enum':
191
- if (typeof raw === 'string' && spec.values.includes(raw))
192
- return { ok: true, value: raw };
193
- return { ok: false, message: `expected one of ${spec.values.join(', ')}` };
209
+ case 'enum': {
210
+ const value = matchEnum(raw, spec.values);
211
+ if (value)
212
+ return { ok: true, value };
213
+ return { ok: false, message: `expected one of ${spec.values.map(canonicalEnumValue).join(', ')}` };
214
+ }
194
215
  }
195
216
  }
196
217
  /**
197
- * Serializes an effective value back to its `ptbk.json` representation.
218
+ * Serializes an effective value back to its canonical `ptbk.json` representation.
198
219
  *
199
220
  * @private project configuration
200
221
  */
201
222
  export function serializeSettingValue(spec, value) {
202
223
  if (spec.type === 'duration' && typeof value === 'number')
203
224
  return value === 0 ? '0' : formatDuration(value).replace(/\s+/g, '');
225
+ if (spec.type === 'enum' && typeof value === 'string')
226
+ return canonicalEnumValue(value);
204
227
  return value;
205
228
  }
206
229
  /**
207
- * Reads `ptbk.json` as raw JSON; returns an empty object when missing.
230
+ * Parses `ptbk.json` text.
208
231
  *
209
232
  * @private project configuration
210
233
  */
211
- export async function readProjectConfigFile(project) {
212
- const text = await readTextIfExists(project.configPath);
234
+ export function parseConfigText(text) {
213
235
  if (text === undefined)
214
236
  return { raw: {}, exists: false, diagnostics: [] };
215
237
  try {
216
238
  const raw = JSON.parse(text);
217
239
  if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
218
- return { raw: {}, exists: true, diagnostics: [{ location: 'ptbk.json', message: 'The file must contain a JSON object.', severity: 'error' }] };
240
+ return { raw: {}, exists: true, text, diagnostics: [{ location: 'ptbk.json', message: 'The file must contain a JSON object.', severity: 'error' }] };
219
241
  }
220
- return { raw: raw, exists: true, diagnostics: [] };
242
+ return { raw: raw, exists: true, text, diagnostics: [] };
221
243
  }
222
244
  catch (error) {
223
- return { raw: {}, exists: true, diagnostics: [{ location: 'ptbk.json', message: `Invalid JSON: ${error.message}`, severity: 'error' }] };
245
+ return { raw: {}, exists: true, text, diagnostics: [{ location: 'ptbk.json', message: `Invalid JSON: ${error.message}`, severity: 'error' }] };
224
246
  }
225
247
  }
226
248
  /**
227
- * Known top-level keys of `ptbk.json`.
249
+ * Reads `ptbk.json` as raw JSON; returns an empty object when missing.
250
+ *
251
+ * @private project configuration
228
252
  */
229
- const KNOWN_TOP_LEVEL_KEYS = new Set(['$schema', 'version', 'paths', 'routing', 'execution', 'traces', 'browser', 'server', 'service', 'discovery', 'learning', 'team', 'timezone', 'harnesses', 'initialized']);
253
+ export async function readProjectConfigFile(project) {
254
+ return parseConfigText(await readTextIfExists(project.configPath));
255
+ }
256
+ /**
257
+ * Known top-level keys of `ptbk.json` (canonical spellings).
258
+ */
259
+ const KNOWN_TOP_LEVEL_KEYS = ['$schema', 'promptbookVersion', 'isAutoSync', 'tools', 'paths', 'routing', 'execution', 'traces', 'server', 'service', 'discovery', 'learning', 'team', 'timezone', 'initialized'];
230
260
  /**
231
- * Parses the per-harness map of `ptbk.json`.
261
+ * Known property names per configuration object path (for tolerant key canonicalization).
232
262
  */
233
- function parseHarnessMap(raw, diagnostics) {
234
- const result = {};
235
- if (raw === undefined)
236
- return result;
237
- if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
238
- diagnostics.push({ location: 'ptbk.json › harnesses', message: 'expected an object keyed by harness identifier', severity: 'error' });
239
- return result;
263
+ function knownKeysByPath() {
264
+ const map = new Map([['', [...KNOWN_TOP_LEVEL_KEYS]]]);
265
+ for (const spec of SETTING_SPECS) {
266
+ const parts = spec.key.split('.');
267
+ for (let index = 0; index < parts.length; index++) {
268
+ const parent = parts.slice(0, index).join('.');
269
+ const list = map.get(parent) ?? [];
270
+ if (!list.includes(parts[index]))
271
+ list.push(parts[index]);
272
+ map.set(parent, list);
273
+ }
240
274
  }
241
- for (const [id, value] of Object.entries(raw)) {
242
- const location = `ptbk.json › harnesses.${id}`;
243
- if (value === null || typeof value !== 'object' || Array.isArray(value)) {
244
- diagnostics.push({ location, message: 'expected an object', severity: 'error' });
245
- continue;
275
+ return map;
276
+ }
277
+ /**
278
+ * Canonicalizes known Promptbook-owned configuration: lower camelCase keys, `is`-prefixed booleans, kebab-case tool
279
+ * names and SCREAMING_SNAKE_CASE enum values. Tolerant input (case/separator differences) is corrected; ambiguous or
280
+ * colliding input is reported as a problem and preserved unchanged. Canonical input is returned unchanged.
281
+ *
282
+ * @private project configuration
283
+ */
284
+ export function canonicalizeConfig(input) {
285
+ const raw = structuredClone(input);
286
+ const corrections = [];
287
+ const problems = [];
288
+ const known = knownKeysByPath();
289
+ const walk = (object, path) => {
290
+ const names = known.get(path);
291
+ if (!names)
292
+ return;
293
+ const groups = new Map();
294
+ for (const key of Object.keys(object)) {
295
+ const canonical = matchProperty(key, names) ?? key;
296
+ groups.set(canonical, [...(groups.get(canonical) ?? []), key]);
246
297
  }
247
- const entry = value;
248
- const enabled = entry.enabled === undefined ? true : entry.enabled;
249
- if (typeof enabled !== 'boolean')
250
- diagnostics.push({ location: `${location}.enabled`, message: 'expected true or false', severity: 'error' });
251
- const thinkingLevel = entry.thinkingLevel;
252
- if (thinkingLevel !== undefined && !THINKING_LEVELS.includes(thinkingLevel)) {
253
- diagnostics.push({ location: `${location}.thinkingLevel`, message: `expected one of ${THINKING_LEVELS.join(', ')}`, severity: 'error' });
298
+ const rebuilt = {};
299
+ for (const [canonical, spellings] of groups) {
300
+ if (spellings.length > 1) {
301
+ problems.push({ location: `ptbk.json › ${path ? `${path}.` : ''}${canonical}`, message: `conflicting spellings ${spellings.map((spelling) => `"${spelling}"`).join(', ')} of the same setting (repair needed)`, severity: 'error' });
302
+ for (const spelling of spellings)
303
+ rebuilt[spelling] = object[spelling];
304
+ continue;
305
+ }
306
+ const spelling = spellings[0];
307
+ if (spelling !== canonical)
308
+ corrections.push({ location: `${path ? `${path}.` : ''}${spelling}`, from: spelling, to: canonical });
309
+ rebuilt[canonical] = object[spelling];
254
310
  }
255
- const models = entry.models;
256
- if (models !== undefined && (!Array.isArray(models) || models.some((model) => typeof model !== 'string'))) {
257
- diagnostics.push({ location: `${location}.models`, message: 'expected an array of model identifiers', severity: 'error' });
311
+ for (const key of Object.keys(object))
312
+ delete object[key];
313
+ Object.assign(object, rebuilt);
314
+ for (const [key, value] of Object.entries(object)) {
315
+ const childPath = path ? `${path}.${key}` : key;
316
+ if (value !== null && typeof value === 'object' && !Array.isArray(value) && known.has(childPath))
317
+ walk(value, childPath);
258
318
  }
259
- for (const key of Object.keys(entry)) {
260
- if (/key|token|secret|password/i.test(key) && key !== 'apiKeyEnv') {
261
- diagnostics.push({ location: `${location}.${key}`, message: 'store credential references (e.g. apiKeyEnv), never secret values', severity: 'error' });
319
+ };
320
+ walk(raw, '');
321
+ for (const spec of SETTING_SPECS) {
322
+ const value = getPath(raw, spec.key);
323
+ if (value === undefined)
324
+ continue;
325
+ if (spec.type === 'enum') {
326
+ const parsed = matchEnum(value, spec.values);
327
+ if (parsed && value !== canonicalEnumValue(parsed)) {
328
+ corrections.push({ location: spec.key, from: String(value), to: canonicalEnumValue(parsed) });
329
+ setPath(raw, spec.key, canonicalEnumValue(parsed));
262
330
  }
263
331
  }
264
- result[id] = {
265
- enabled: enabled === true,
266
- model: typeof entry.model === 'string' ? entry.model : undefined,
267
- thinkingLevel: THINKING_LEVELS.includes(thinkingLevel) ? thinkingLevel : undefined,
268
- models: Array.isArray(models) ? models.filter((model) => typeof model === 'string') : undefined,
269
- apiKeyEnv: typeof entry.apiKeyEnv === 'string' ? entry.apiKeyEnv : undefined,
270
- };
332
+ else if (spec.type === 'boolean' && (value === 'true' || value === 'false')) {
333
+ corrections.push({ location: spec.key, from: value, to: value });
334
+ setPath(raw, spec.key, value === 'true');
335
+ }
336
+ }
337
+ if (raw.tools !== undefined) {
338
+ const parsed = parseTools(raw.tools);
339
+ corrections.push(...parsed.corrections.map((correction) => ({ ...correction, location: correction.location.replace(/^ptbk\.json › /, '') })));
340
+ problems.push(...parsed.problems);
341
+ if (parsed.corrections.length)
342
+ raw.tools = parsed.canonical;
343
+ }
344
+ return { raw, corrections, problems };
345
+ }
346
+ /**
347
+ * Maps a legacy/temporary profile spelling to a typed profile.
348
+ *
349
+ * @private project configuration
350
+ */
351
+ export function profileFromSpelling(value) {
352
+ const legacy = { global: 'MANAGED_GLOBAL', isolated: 'PROJECT', user: 'USER_EXISTING' };
353
+ if (legacy[value])
354
+ return { source: legacy[value] };
355
+ const canonical = matchEnum(value, ['MANAGED_GLOBAL', 'PROJECT', 'USER_EXISTING']);
356
+ if (canonical)
357
+ return { source: canonical };
358
+ return { source: 'CUSTOM', path: value };
359
+ }
360
+ /**
361
+ * Applies `--harness` restrictions: variants keep their ordered same-variant instances; a named instance stays an
362
+ * explicit restriction; a selected variant without a configured instance gets a temporary native instance.
363
+ */
364
+ function restrictHarnesses(tools, selectors, diagnostics, overridden) {
365
+ const variants = new Set();
366
+ const names = new Set();
367
+ for (const selector of selectors) {
368
+ const variant = matchEnum(selector, HARNESS_VARIANTS) ?? harnessVariantOf(selector);
369
+ if (variant)
370
+ variants.add(variant);
371
+ else if (tools.some((tool) => tool.type === 'HARNESS' && tool.name === selector))
372
+ names.add(selector);
373
+ else
374
+ diagnostics.push({ location: `--harness ${selector}`, message: 'not a harness variant or configured harness tool name', severity: 'error' });
375
+ }
376
+ const result = tools.map((tool) => {
377
+ if (tool.type !== 'HARNESS')
378
+ return tool;
379
+ const selected = names.has(tool.name) || variants.has(tool.variant);
380
+ if (selected === tool.isEnabled)
381
+ return tool;
382
+ overridden.add(tool.name);
383
+ return { ...tool, isEnabled: selected };
384
+ });
385
+ for (const variant of variants) {
386
+ if (result.some((tool) => tool.type === 'HARNESS' && tool.variant === variant))
387
+ continue;
388
+ const name = harnessPluginId(variant);
389
+ if (result.some((tool) => tool.name === name))
390
+ continue;
391
+ overridden.add(name);
392
+ result.push({ type: 'HARNESS', variant: variant, name, isEnabled: true, authentication: 'NATIVE' });
271
393
  }
272
394
  return result;
273
395
  }
396
+ /**
397
+ * Applies temporary browser overrides to exactly one unambiguous browser tool.
398
+ */
399
+ function overrideBrowser(tools, overrides, diagnostics, overridden) {
400
+ if (!overrides.mode && !overrides.profile)
401
+ return tools;
402
+ const browsers = tools.filter((tool) => tool.type === 'BROWSER');
403
+ let target;
404
+ if (overrides.tool) {
405
+ target = browsers.find((tool) => tool.name === overrides.tool);
406
+ if (!target) {
407
+ diagnostics.push({ location: `--browser-tool ${overrides.tool}`, message: 'no browser tool has this name', severity: 'error' });
408
+ return tools;
409
+ }
410
+ }
411
+ else {
412
+ const enabled = browsers.filter((tool) => tool.isEnabled);
413
+ if (enabled.length > 1) {
414
+ diagnostics.push({ location: '--browser-mode/--browser-profile', message: `several browsers are enabled (${enabled.map((tool) => tool.name).join(', ')}); select one with --browser-tool <name>`, severity: 'error' });
415
+ return tools;
416
+ }
417
+ target = enabled[0] ?? browsers[0];
418
+ }
419
+ if (!target) {
420
+ diagnostics.push({ location: '--browser-mode/--browser-profile', message: 'no browser tool is configured (add one with `ptbk tool add BROWSER …`)', severity: 'error' });
421
+ return tools;
422
+ }
423
+ overridden.add(target.name);
424
+ const changed = {
425
+ ...target,
426
+ ...(overrides.mode ? { isHeadless: overrides.mode !== 'headful' } : {}),
427
+ ...(overrides.profile ? { profile: profileFromSpelling(overrides.profile) } : {}),
428
+ };
429
+ return tools.map((tool) => (tool === target ? changed : tool));
430
+ }
274
431
  /**
275
432
  * Resolves effective settings: engine defaults < legacy environment < `ptbk.json` < CLI flags < live runtime changes.
276
433
  *
@@ -284,7 +441,7 @@ export function resolveSettings(layers) {
284
441
  const sources = {};
285
442
  const env = layers.env ?? process.env;
286
443
  for (const key of Object.keys(layers.file)) {
287
- if (!KNOWN_TOP_LEVEL_KEYS.has(key))
444
+ if (!KNOWN_TOP_LEVEL_KEYS.includes(key))
288
445
  diagnostics.push({ location: `ptbk.json › ${key}`, message: 'unknown setting (preserved, ignored)', severity: 'warning' });
289
446
  }
290
447
  for (const spec of SETTING_SPECS) {
@@ -331,28 +488,65 @@ export function resolveSettings(layers) {
331
488
  values.timezone = systemTimeZone();
332
489
  }
333
490
  }
334
- const harnesses = parseHarnessMap(layers.file.harnesses, diagnostics);
335
- if (layers.cliHarnesses && layers.cliHarnesses.length) {
336
- const selected = new Set(layers.cliHarnesses);
337
- for (const id of Object.keys(harnesses))
338
- harnesses[id] = { ...harnesses[id], enabled: selected.has(id) };
339
- for (const id of selected)
340
- if (!harnesses[id])
341
- harnesses[id] = { enabled: true };
342
- }
343
- if (values['server.enabled'] === false && sources['server.tunnel.enabled'] !== 'cli')
344
- values['server.tunnel.enabled'] = false;
345
- return { values, sources, harnesses, diagnostics };
491
+ const parsedTools = parseTools(layers.file.tools);
492
+ diagnostics.push(...parsedTools.problems);
493
+ const overridden = new Set();
494
+ let tools = [...parsedTools.tools];
495
+ if (layers.cliHarnesses?.length)
496
+ tools = restrictHarnesses(tools, layers.cliHarnesses, diagnostics, overridden);
497
+ if (layers.cliBrowser)
498
+ tools = overrideBrowser(tools, layers.cliBrowser, diagnostics, overridden);
499
+ if (values['server.isEnabled'] === false && sources['server.tunnel.isEnabled'] !== 'cli')
500
+ values['server.tunnel.isEnabled'] = false;
501
+ return { values, sources, tools, overriddenTools: overridden, diagnostics };
346
502
  }
347
503
  /**
348
- * Loads and resolves the project's settings.
504
+ * Loads and resolves the project's settings without writing anything: a pending configuration migration and
505
+ * deterministic spelling corrections are applied in memory and reported; persisting them is the job of the mutating
506
+ * entry points (see the configuration coordinator).
349
507
  *
350
508
  * @private project configuration
351
509
  */
352
510
  export async function loadSettings(project, layers = {}) {
353
511
  const file = await readProjectConfigFile(project);
354
- const resolved = resolveSettings({ ...layers, file: file.raw });
355
- return { ...resolved, diagnostics: [...file.diagnostics, ...resolved.diagnostics], fileExists: file.exists, raw: file.raw };
512
+ return interpretConfig(file, layers);
513
+ }
514
+ /**
515
+ * Interprets a read configuration file for this engine (pure; see {@link loadSettings}).
516
+ *
517
+ * @private project configuration
518
+ */
519
+ export function interpretConfig(file, layers = {}, engineVersion = ENGINE_VERSION) {
520
+ const diagnostics = [...file.diagnostics];
521
+ const migration = file.exists && !file.diagnostics.length ? planConfigurationMigration(file.raw, engineVersion) : { kind: 'current', version: engineVersion };
522
+ let interpreted = file.raw;
523
+ if (migration.kind === 'migrate') {
524
+ const migrated = applyConfigurationMigration(file.raw, migration);
525
+ interpreted = migrated.raw;
526
+ diagnostics.push(...migrated.problems);
527
+ diagnostics.push({ location: 'ptbk.json › promptbookVersion', message: `configuration migration ${migration.from} → ${migration.to} is pending (it is applied and committed before new work)`, severity: 'warning' });
528
+ }
529
+ else if (migration.kind === 'older-engine') {
530
+ diagnostics.push({
531
+ location: 'ptbk.json › promptbookVersion',
532
+ message: `this project was configured by ptbk ${migration.saved}, which is newer than the running ptbk ${migration.engine}; update ptbk (npm install -g ptbk@latest, or the project's local dependency) and rerun — the project is never downgraded`,
533
+ severity: 'error',
534
+ });
535
+ }
536
+ else if (migration.kind === 'invalid') {
537
+ diagnostics.push({ location: 'ptbk.json › promptbookVersion', message: migration.reason, severity: 'error' });
538
+ }
539
+ const canonical = canonicalizeConfig(interpreted);
540
+ const resolved = resolveSettings({ ...layers, file: canonical.raw });
541
+ const seen = new Set();
542
+ const merged = [...diagnostics, ...canonical.problems.filter((problem) => !problem.location.includes('tools')), ...resolved.diagnostics].filter((diagnostic) => {
543
+ const key = `${diagnostic.location}\u0000${diagnostic.message}`;
544
+ if (seen.has(key))
545
+ return false;
546
+ seen.add(key);
547
+ return true;
548
+ });
549
+ return { ...resolved, diagnostics: merged, fileExists: file.exists, raw: file.raw, interpreted: canonical.raw, migration, corrections: migration.kind === 'migrate' ? [] : canonical.corrections };
356
550
  }
357
551
  /**
358
552
  * Throws a configuration error when settings contain errors (used before affected work).
@@ -382,19 +576,43 @@ export function setting(settings, key) {
382
576
  return settings.values[key];
383
577
  }
384
578
  /**
385
- * Enabled harness identifiers in configuration order.
579
+ * Enabled harness instances in configuration order.
580
+ *
581
+ * @private project configuration
582
+ */
583
+ export function enabledHarnessTools(settings) {
584
+ return enabledToolsOf(settings.tools, 'HARNESS');
585
+ }
586
+ /**
587
+ * Enabled browser instances in configuration order.
588
+ *
589
+ * @private project configuration
590
+ */
591
+ export function enabledBrowserTools(settings) {
592
+ return enabledToolsOf(settings.tools, 'BROWSER');
593
+ }
594
+ /**
595
+ * Harness plugin identifiers that have at least one enabled instance, in first-instance order.
386
596
  *
387
597
  * @private project configuration
388
598
  */
389
599
  export function enabledHarnesses(settings) {
390
- return Object.entries(settings.harnesses)
391
- .filter(([, harness]) => harness.enabled)
392
- .map(([id]) => id);
600
+ return [...new Set(enabledHarnessTools(settings).map((tool) => harnessPluginId(tool.variant)))];
601
+ }
602
+ /**
603
+ * Serializes `ptbk.json` text (stable 4-space JSON).
604
+ *
605
+ * @private project configuration
606
+ */
607
+ export function configText(raw) {
608
+ return `${JSON.stringify(raw, null, 4)}\n`;
393
609
  }
394
610
  /**
395
- * Applies changes to `ptbk.json`, preserving unknown keys, and returns whether the file content changed.
611
+ * Applies scalar setting changes to `ptbk.json`, preserving unknown keys, and returns whether the file content changed.
396
612
  *
397
- * Values are effective values (durations in ms); `undefined` removes a key.
613
+ * Values are effective values (durations in ms, internal enum spellings); `undefined` removes a key. A new file is
614
+ * stamped with the running engine's `promptbookVersion`. An existing file must already be current for this engine
615
+ * (the configuration coordinator migrates it first).
398
616
  *
399
617
  * @private project configuration
400
618
  */
@@ -409,33 +627,27 @@ export async function updateProjectConfig(project, changes) {
409
627
  });
410
628
  }
411
629
  const raw = structuredClone(file.raw);
412
- if (raw.version === undefined)
413
- raw.version = 1;
630
+ if (!file.exists)
631
+ raw.promptbookVersion = ENGINE_VERSION;
632
+ else {
633
+ const plan = planConfigurationMigration(raw);
634
+ if (plan.kind !== 'current') {
635
+ throw new PtbkError('ptbk.json must be migrated before it can be changed.', {
636
+ kind: 'configuration',
637
+ project: project.root,
638
+ reason: plan.kind === 'migrate' ? `The configuration is at ${plan.from}; this engine is ${plan.to}.` : plan.kind === 'older-engine' ? `The configuration was written by the newer ptbk ${plan.saved}.` : plan.reason,
639
+ next: ['Run `ptbk start` (or `ptbk init`) to apply the committed configuration migration first.'],
640
+ });
641
+ }
642
+ }
414
643
  for (const [key, value] of Object.entries(changes.settings ?? {})) {
415
644
  const spec = SETTING_SPEC_BY_KEY.get(key);
416
645
  if (!spec)
417
646
  throw new Error(`Unknown setting ${key}`);
418
647
  setPath(raw, key, value === undefined ? undefined : serializeSettingValue(spec, value));
419
648
  }
420
- for (const [id, harness] of Object.entries(changes.harnesses ?? {})) {
421
- const map = (raw.harnesses && typeof raw.harnesses === 'object' ? raw.harnesses : (raw.harnesses = {}));
422
- if (harness === undefined) {
423
- delete map[id];
424
- continue;
425
- }
426
- const current = (map[id] && typeof map[id] === 'object' ? map[id] : {});
427
- const next = { ...current };
428
- for (const [field, value] of Object.entries(harness)) {
429
- if (value === undefined)
430
- delete next[field];
431
- else
432
- next[field] = value;
433
- }
434
- map[id] = next;
435
- }
436
- const text = `${JSON.stringify(raw, null, 4)}\n`;
437
- const before = await readTextIfExists(project.configPath);
438
- if (before === text)
649
+ const text = configText(raw);
650
+ if (file.text === text)
439
651
  return false;
440
652
  await writeFileAtomic(project.configPath, text);
441
653
  return true;