nemoris 0.1.0 → 0.1.2

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 (248) hide show
  1. package/.env.example +49 -49
  2. package/LICENSE +21 -21
  3. package/README.md +209 -209
  4. package/SECURITY.md +59 -119
  5. package/bin/nemoris +46 -46
  6. package/config/agents/agent.toml.example +28 -28
  7. package/config/agents/content.toml +23 -0
  8. package/config/agents/default.toml +22 -22
  9. package/config/agents/heartbeat.toml +35 -0
  10. package/config/agents/iris.toml +23 -0
  11. package/config/agents/lab.toml +23 -0
  12. package/config/agents/main.toml +45 -0
  13. package/config/agents/nemo.toml +21 -0
  14. package/config/agents/ops.toml +38 -0
  15. package/config/agents/orchestrator.toml +18 -18
  16. package/config/agents/revenue.toml +23 -0
  17. package/config/agents/testyboo.toml +19 -0
  18. package/config/delivery.toml +73 -73
  19. package/config/embeddings.toml +5 -5
  20. package/config/identity/content-purpose.md +11 -0
  21. package/config/identity/content-soul.md +45 -0
  22. package/config/identity/default-purpose.md +1 -1
  23. package/config/identity/default-soul.md +3 -3
  24. package/config/identity/heartbeat-purpose.md +9 -0
  25. package/config/identity/heartbeat-soul.md +16 -0
  26. package/config/identity/iris-purpose.md +17 -0
  27. package/config/identity/iris-soul.md +68 -0
  28. package/config/identity/lab-purpose.md +10 -0
  29. package/config/identity/lab-soul.md +38 -0
  30. package/config/identity/main-purpose.md +17 -0
  31. package/config/identity/main-soul.md +66 -0
  32. package/config/identity/main-user.md +22 -0
  33. package/config/identity/ops-purpose.md +9 -0
  34. package/config/identity/ops-soul.md +16 -0
  35. package/config/identity/orchestrator-purpose.md +1 -1
  36. package/config/identity/orchestrator-soul.md +1 -1
  37. package/config/identity/revenue-purpose.md +9 -0
  38. package/config/identity/revenue-soul.md +41 -0
  39. package/config/identity/testyboo-purpose.md +13 -0
  40. package/config/identity/testyboo-soul.md +20 -0
  41. package/config/improvement-targets.toml +15 -15
  42. package/config/jobs/heartbeat-check.toml +30 -30
  43. package/config/jobs/memory-rollup.toml +46 -46
  44. package/config/jobs/workspace-health.toml +63 -63
  45. package/config/mcp.toml +16 -16
  46. package/config/output-contracts.toml +17 -17
  47. package/config/peers.toml +32 -32
  48. package/config/peers.toml.example +32 -32
  49. package/config/policies/memory-default.toml +10 -10
  50. package/config/policies/memory-heartbeat.toml +5 -5
  51. package/config/policies/memory-ops.toml +10 -10
  52. package/config/policies/tools-heartbeat-minimal.toml +8 -8
  53. package/config/policies/tools-interactive-safe.toml +8 -8
  54. package/config/policies/tools-ops-bounded.toml +8 -8
  55. package/config/policies/tools-orchestrator.toml +7 -7
  56. package/config/providers/anthropic.toml +15 -15
  57. package/config/providers/ollama.toml +5 -5
  58. package/config/providers/openai-codex.toml +9 -9
  59. package/config/providers/openrouter.toml +5 -5
  60. package/config/router.toml +22 -22
  61. package/config/runtime.toml +114 -114
  62. package/config/skills/self-improvement.toml +15 -15
  63. package/config/skills/telegram-onboarding-spec.md +240 -240
  64. package/config/skills/workspace-monitor.toml +15 -15
  65. package/config/task-router.toml +42 -42
  66. package/install.sh +50 -50
  67. package/package.json +91 -90
  68. package/src/auth/auth-profiles.js +169 -169
  69. package/src/auth/openai-codex-oauth.js +285 -285
  70. package/src/battle.js +449 -449
  71. package/src/cli/help.js +265 -265
  72. package/src/cli/output-filter.js +49 -49
  73. package/src/cli/runtime-control.js +704 -704
  74. package/src/cli-main.js +2763 -2763
  75. package/src/cli.js +78 -78
  76. package/src/config/loader.js +332 -332
  77. package/src/config/schema-validator.js +214 -214
  78. package/src/config/toml-lite.js +8 -8
  79. package/src/daemon/action-handlers.js +71 -71
  80. package/src/daemon/healing-tick.js +87 -87
  81. package/src/daemon/health-probes.js +90 -90
  82. package/src/daemon/notifier.js +57 -57
  83. package/src/daemon/nurse.js +218 -218
  84. package/src/daemon/repair-log.js +106 -106
  85. package/src/daemon/rule-staging.js +90 -90
  86. package/src/daemon/rules.js +29 -29
  87. package/src/daemon/telegram-commands.js +54 -54
  88. package/src/daemon/updater.js +85 -85
  89. package/src/jobs/job-runner.js +78 -78
  90. package/src/mcp/consumer.js +129 -129
  91. package/src/memory/active-recall.js +171 -171
  92. package/src/memory/backend-manager.js +97 -97
  93. package/src/memory/backends/file-backend.js +38 -38
  94. package/src/memory/backends/qmd-backend.js +219 -219
  95. package/src/memory/embedding-guards.js +24 -24
  96. package/src/memory/embedding-index.js +118 -118
  97. package/src/memory/embedding-service.js +179 -179
  98. package/src/memory/file-index.js +177 -177
  99. package/src/memory/memory-signature.js +5 -5
  100. package/src/memory/memory-store.js +648 -648
  101. package/src/memory/retrieval-planner.js +66 -66
  102. package/src/memory/scoring.js +145 -145
  103. package/src/memory/simhash.js +78 -78
  104. package/src/memory/sqlite-active-store.js +824 -824
  105. package/src/memory/write-policy.js +36 -36
  106. package/src/onboarding/aliases.js +33 -33
  107. package/src/onboarding/auth/api-key.js +224 -224
  108. package/src/onboarding/auth/ollama-detect.js +42 -42
  109. package/src/onboarding/clack-prompter.js +77 -77
  110. package/src/onboarding/doctor.js +530 -530
  111. package/src/onboarding/lock.js +42 -42
  112. package/src/onboarding/model-catalog.js +344 -344
  113. package/src/onboarding/phases/auth.js +576 -589
  114. package/src/onboarding/phases/build.js +130 -130
  115. package/src/onboarding/phases/choose.js +82 -82
  116. package/src/onboarding/phases/detect.js +98 -98
  117. package/src/onboarding/phases/hatch.js +216 -216
  118. package/src/onboarding/phases/identity.js +79 -79
  119. package/src/onboarding/phases/ollama.js +345 -345
  120. package/src/onboarding/phases/scaffold.js +99 -99
  121. package/src/onboarding/phases/telegram.js +377 -377
  122. package/src/onboarding/phases/validate.js +204 -204
  123. package/src/onboarding/phases/verify.js +206 -206
  124. package/src/onboarding/platform.js +482 -482
  125. package/src/onboarding/status-bar.js +95 -95
  126. package/src/onboarding/templates.js +794 -794
  127. package/src/onboarding/toml-writer.js +38 -38
  128. package/src/onboarding/tui.js +250 -250
  129. package/src/onboarding/uninstall.js +153 -153
  130. package/src/onboarding/wizard.js +516 -499
  131. package/src/providers/anthropic.js +168 -168
  132. package/src/providers/base.js +247 -247
  133. package/src/providers/circuit-breaker.js +136 -136
  134. package/src/providers/ollama.js +163 -163
  135. package/src/providers/openai-codex.js +149 -149
  136. package/src/providers/openrouter.js +136 -136
  137. package/src/providers/registry.js +36 -36
  138. package/src/providers/router.js +16 -16
  139. package/src/runtime/bootstrap-cache.js +47 -47
  140. package/src/runtime/capabilities-prompt.js +25 -25
  141. package/src/runtime/completion-ping.js +99 -99
  142. package/src/runtime/config-validator.js +121 -121
  143. package/src/runtime/context-ledger.js +360 -360
  144. package/src/runtime/cutover-readiness.js +42 -42
  145. package/src/runtime/daemon.js +729 -729
  146. package/src/runtime/delivery-ack.js +195 -195
  147. package/src/runtime/delivery-adapters/local-file.js +41 -41
  148. package/src/runtime/delivery-adapters/openclaw-cli.js +94 -94
  149. package/src/runtime/delivery-adapters/openclaw-peer.js +98 -98
  150. package/src/runtime/delivery-adapters/shadow.js +13 -13
  151. package/src/runtime/delivery-adapters/standalone-http.js +98 -98
  152. package/src/runtime/delivery-adapters/telegram.js +104 -104
  153. package/src/runtime/delivery-adapters/tui.js +128 -128
  154. package/src/runtime/delivery-manager.js +807 -807
  155. package/src/runtime/delivery-store.js +168 -168
  156. package/src/runtime/dependency-health.js +118 -118
  157. package/src/runtime/envelope.js +114 -114
  158. package/src/runtime/evaluation.js +1089 -1089
  159. package/src/runtime/exec-approvals.js +216 -216
  160. package/src/runtime/executor.js +500 -500
  161. package/src/runtime/failure-ping.js +67 -67
  162. package/src/runtime/flows.js +83 -83
  163. package/src/runtime/guards.js +45 -45
  164. package/src/runtime/handoff.js +51 -51
  165. package/src/runtime/identity-cache.js +28 -28
  166. package/src/runtime/improvement-engine.js +109 -109
  167. package/src/runtime/improvement-harness.js +581 -581
  168. package/src/runtime/input-sanitiser.js +72 -72
  169. package/src/runtime/interaction-contract.js +347 -347
  170. package/src/runtime/lane-readiness.js +226 -226
  171. package/src/runtime/migration.js +323 -323
  172. package/src/runtime/model-resolution.js +78 -78
  173. package/src/runtime/network.js +64 -64
  174. package/src/runtime/notification-store.js +97 -97
  175. package/src/runtime/notifier.js +256 -256
  176. package/src/runtime/orchestrator.js +53 -53
  177. package/src/runtime/orphan-reaper.js +41 -41
  178. package/src/runtime/output-contract-schema.js +139 -139
  179. package/src/runtime/output-contract-validator.js +439 -439
  180. package/src/runtime/peer-readiness.js +69 -69
  181. package/src/runtime/peer-registry.js +133 -133
  182. package/src/runtime/pilot-status.js +108 -108
  183. package/src/runtime/prompt-builder.js +261 -261
  184. package/src/runtime/provider-attempt.js +582 -582
  185. package/src/runtime/report-fallback.js +71 -71
  186. package/src/runtime/result-normalizer.js +183 -183
  187. package/src/runtime/retention.js +74 -74
  188. package/src/runtime/review.js +244 -244
  189. package/src/runtime/route-job.js +15 -15
  190. package/src/runtime/run-store.js +38 -38
  191. package/src/runtime/schedule.js +88 -88
  192. package/src/runtime/scheduler-state.js +434 -434
  193. package/src/runtime/scheduler.js +656 -656
  194. package/src/runtime/session-compactor.js +182 -182
  195. package/src/runtime/session-search.js +155 -155
  196. package/src/runtime/slack-inbound.js +249 -249
  197. package/src/runtime/ssrf.js +102 -102
  198. package/src/runtime/status-aggregator.js +330 -330
  199. package/src/runtime/task-contract.js +140 -140
  200. package/src/runtime/task-packet.js +107 -107
  201. package/src/runtime/task-router.js +140 -140
  202. package/src/runtime/telegram-inbound.js +1565 -1565
  203. package/src/runtime/token-counter.js +134 -134
  204. package/src/runtime/token-estimator.js +59 -59
  205. package/src/runtime/tool-loop.js +200 -200
  206. package/src/runtime/transport-server.js +311 -311
  207. package/src/runtime/tui-server.js +411 -411
  208. package/src/runtime/ulid.js +44 -44
  209. package/src/security/ssrf-check.js +197 -197
  210. package/src/setup.js +369 -369
  211. package/src/shadow/bridge.js +303 -303
  212. package/src/skills/loader.js +84 -84
  213. package/src/tools/catalog.json +49 -49
  214. package/src/tools/cli-delegate.js +44 -44
  215. package/src/tools/mcp-client.js +106 -106
  216. package/src/tools/micro/cancel-task.js +6 -6
  217. package/src/tools/micro/complete-task.js +6 -6
  218. package/src/tools/micro/fail-task.js +6 -6
  219. package/src/tools/micro/http-fetch.js +74 -74
  220. package/src/tools/micro/index.js +36 -36
  221. package/src/tools/micro/lcm-recall.js +60 -60
  222. package/src/tools/micro/list-dir.js +17 -17
  223. package/src/tools/micro/list-skills.js +46 -46
  224. package/src/tools/micro/load-skill.js +38 -38
  225. package/src/tools/micro/memory-search.js +45 -45
  226. package/src/tools/micro/read-file.js +11 -11
  227. package/src/tools/micro/session-search.js +54 -54
  228. package/src/tools/micro/shell-exec.js +43 -43
  229. package/src/tools/micro/trigger-job.js +79 -79
  230. package/src/tools/micro/web-search.js +58 -58
  231. package/src/tools/micro/workspace-paths.js +39 -39
  232. package/src/tools/micro/write-file.js +14 -14
  233. package/src/tools/micro/write-memory.js +41 -41
  234. package/src/tools/registry.js +348 -348
  235. package/src/tools/tool-result-contract.js +36 -36
  236. package/src/tui/chat.js +835 -835
  237. package/src/tui/renderer.js +175 -175
  238. package/src/tui/socket-client.js +217 -217
  239. package/src/utils/canonical-json.js +29 -29
  240. package/src/utils/compaction.js +30 -30
  241. package/src/utils/env-loader.js +5 -5
  242. package/src/utils/errors.js +80 -80
  243. package/src/utils/fs.js +101 -101
  244. package/src/utils/ids.js +5 -5
  245. package/src/utils/model-context-limits.js +30 -30
  246. package/src/utils/token-budget.js +74 -74
  247. package/src/utils/usage-cost.js +25 -25
  248. package/src/utils/usage-metrics.js +14 -14
@@ -1,656 +1,656 @@
1
- import path from "node:path";
2
- import { ConfigLoader } from "../config/loader.js";
3
- import { buildTaskPacket } from "./task-packet.js";
4
- import { MemoryStore } from "../memory/memory-store.js";
5
- import { OpenClawShadowBridge } from "../shadow/bridge.js";
6
- import { readText } from "../utils/fs.js";
7
- import { EmbeddingIndex } from "../memory/embedding-index.js";
8
- import { EmbeddingService } from "../memory/embedding-service.js";
9
- import { MemoryBackendManager } from "../memory/backend-manager.js";
10
- import { planRetrievalQueries } from "../memory/retrieval-planner.js";
11
- import { TaskRouter } from "./task-router.js";
12
- import { resolveInteractionContract } from "./interaction-contract.js";
13
- import { IdentityBootstrapCache } from "./bootstrap-cache.js";
14
- import { getCached, setCached } from "./identity-cache.js";
15
- import { getContextLimit, getMaxOutputTokens } from "../utils/model-context-limits.js";
16
- import { pruneContext, estimateTokens } from "../utils/token-budget.js";
17
- import { PromptBuilder } from "./prompt-builder.js";
18
- import { resolveModelOverride } from "./model-resolution.js";
19
-
20
- function normalizePolicy(policy) {
21
- const categories = policy.categories || {};
22
- return {
23
- allow_durable_writes: policy.allowDurableWrites ?? false,
24
- allow_identity_updates: policy.allowIdentityUpdates ?? false,
25
- require_source_reference: policy.requireSourceReference ?? false,
26
- require_write_reason: policy.requireWriteReason ?? false,
27
- max_writes_per_run: policy.maxWritesPerRun ?? 0,
28
- categories: {
29
- allowed: categories.allowed || [],
30
- blocked: categories.blocked || []
31
- }
32
- };
33
- }
34
-
35
- function deepMerge(base, override) {
36
- if (Array.isArray(base) || Array.isArray(override)) {
37
- return Array.isArray(override) ? [...override] : Array.isArray(base) ? [...base] : [];
38
- }
39
-
40
- if (!base || typeof base !== "object") {
41
- return override === undefined ? base : override;
42
- }
43
-
44
- if (!override || typeof override !== "object") {
45
- return override === undefined ? { ...base } : override;
46
- }
47
-
48
- const merged = { ...base };
49
- for (const [key, value] of Object.entries(override)) {
50
- merged[key] = key in base ? deepMerge(base[key], value) : value;
51
- }
52
- return merged;
53
- }
54
-
55
- function normalizeJob(jobId, jobConfig, runtime = {}) {
56
- return {
57
- id: jobId,
58
- trigger: jobConfig.trigger,
59
- taskType: jobConfig.taskType,
60
- agentId: jobConfig.agentId,
61
- modelLane: jobConfig.modelLane,
62
- outputTarget: jobConfig.outputTarget,
63
- idempotencyKey: jobConfig.idempotencyKey,
64
- liveJobHints: jobConfig.liveJobHints || [],
65
- memoryBackends: jobConfig.memoryBackends || null,
66
- qmdSupplementLimit: jobConfig.qmdSupplementLimit ?? null,
67
- memoryLimit: jobConfig.memoryLimit ?? null,
68
- interactionContract: jobConfig.interactionContract || null,
69
- reportFallback: jobConfig.reportFallback
70
- ? {
71
- enabled: jobConfig.reportFallback.enabled ?? false,
72
- lane: jobConfig.reportFallback.lane || runtime.runtime?.reportFallback?.lane || "report_fallback_lowcost",
73
- allowedFailureClasses:
74
- jobConfig.reportFallback.allowedFailureClasses ||
75
- runtime.runtime?.reportFallback?.allowedFailureClasses ||
76
- ["timeout", "provider_loading"]
77
- }
78
- : null,
79
- outputContract: normalizeOutputContract(jobConfig.outputContract || null, runtime.outputContracts || null),
80
- reportGuidance: normalizeReportGuidance(jobConfig.reportGuidance || null),
81
- budget: {
82
- maxTokens: jobConfig.budget?.maxTokens ?? 4000,
83
- maxRuntimeSeconds: jobConfig.budget?.maxRuntimeSeconds ?? 30
84
- },
85
- retry: {
86
- maxAttempts: jobConfig.retry?.maxAttempts ?? 1
87
- },
88
- stop: {
89
- haltOnPolicyError: jobConfig.stop?.haltOnPolicyError ?? true,
90
- haltOnBudgetExceeded: jobConfig.stop?.haltOnBudgetExceeded ?? true
91
- }
92
- };
93
- }
94
-
95
- export class Scheduler {
96
- constructor({ projectRoot, liveRoot, stateRoot }) {
97
- this.projectRoot = projectRoot;
98
- this.liveRoot = liveRoot;
99
- this.stateRoot = stateRoot;
100
- this.loader = new ConfigLoader({ rootDir: path.join(projectRoot, "config") });
101
- this.memoryStore = new MemoryStore({ rootDir: path.join(stateRoot, "memory") });
102
- this.bridge = new OpenClawShadowBridge({ liveRoot });
103
- this.embeddingIndex = new EmbeddingIndex({
104
- rootDir: path.join(stateRoot, "memory"),
105
- embeddingService: new EmbeddingService({ projectRoot })
106
- });
107
- this.backendManager = new MemoryBackendManager({
108
- memoryStore: this.memoryStore,
109
- embeddingIndex: this.embeddingIndex,
110
- liveRoot
111
- });
112
- this.identityCache = new IdentityBootstrapCache({
113
- rootDir: path.join(stateRoot, "bootstrap", "identity")
114
- });
115
- }
116
-
117
- async loadRuntime() {
118
- const config = await this.loader.loadAll();
119
- this.identityCache.ttlMs = config.runtime?.bootstrapCache?.identityTtlMs ?? this.identityCache.ttlMs;
120
- return config;
121
- }
122
-
123
- async listJobPlans() {
124
- const runtime = await this.loadRuntime();
125
- return Object.entries(runtime.jobs).map(([jobId, jobConfig]) => {
126
- const normalizedJob = normalizeJob(jobId, jobConfig, runtime);
127
- const agent = runtime.agents[normalizedJob.agentId];
128
- return {
129
- ...normalizedJob,
130
- workspaceRoot: agent?.workspaceRoot || null,
131
- primaryLane: agent?.primaryLane || null,
132
- deliveryProfile: agent?.delivery?.profile || null
133
- };
134
- });
135
- }
136
-
137
- async hydrateJob(jobId, options = {}) {
138
- const runtime = await this.loadRuntime();
139
- const rawJob = runtime.jobs[jobId];
140
- if (!rawJob) {
141
- throw new Error(`Unknown job: ${jobId}`);
142
- }
143
-
144
- const job = normalizeJob(jobId, rawJob, runtime);
145
- const agent = runtime.agents[job.agentId];
146
- if (!agent) {
147
- throw new Error(`Unknown agent for job ${jobId}: ${job.agentId}`);
148
- }
149
-
150
- if (options.shadowImport) {
151
- const policy = normalizePolicy(runtime.policies[`memory-${agent.memoryPolicy}`] || runtime.policies[agent.memoryPolicy]);
152
- await this.bridge.importWorkspaceSnapshot(job.agentId, this.memoryStore, policy, {
153
- workspaceOverride: agent.workspaceRoot
154
- });
155
- }
156
-
157
- const objective =
158
- options.objective ||
159
- `Execute job ${job.id} of type ${job.taskType} with bounded runtime and low context overhead.`;
160
-
161
- const cacheKey = [
162
- agent.soulRef || "",
163
- agent.purposeRef || "",
164
- agent.userRef || "",
165
- (agent.workspaceContextFiles || []).join(","),
166
- agent.workspaceRoot || ""
167
- ].join("|");
168
-
169
- let identity = getCached(cacheKey);
170
- if (!identity) {
171
- identity = await this.identityCache.get(
172
- job.agentId,
173
- {
174
- soulRef: agent.soulRef || null, purposeRef: agent.purposeRef || null, userRef: agent.userRef || null },
175
- () => loadAgentIdentity(this.projectRoot, agent)
176
- );
177
- setCached(cacheKey, identity);
178
- }
179
- const effectiveMemoryBackends = job.memoryBackends || agent.memoryBackends || ["file"];
180
- const effectiveQmdSupplementLimit = job.qmdSupplementLimit ?? agent.qmdSupplementLimit ?? 2;
181
- const effectiveMemoryLimit = job.memoryLimit ?? options.memoryLimit ?? 8;
182
- const effectiveInteractionContract = resolveInteractionContract(agent.interactionContract || null, job.interactionContract);
183
- const effectiveReportGuidance = options.reportGuidanceOverride
184
- ? normalizeReportGuidance(deepMerge(job.reportGuidance || {}, options.reportGuidanceOverride))
185
- : job.reportGuidance;
186
- const effectiveRetrievalBlend = options.retrievalBlendOverride
187
- ? {
188
- ...(runtime.runtime?.retrieval || {}),
189
- ...options.retrievalBlendOverride
190
- }
191
- : runtime.runtime?.retrieval || null;
192
- const retrievalQueries = planRetrievalQueries({
193
- objective,
194
- taskType: job.taskType,
195
- purpose: identity?.purpose,
196
- identity,
197
- reportGuidance: effectiveReportGuidance
198
- });
199
-
200
- const packet = await buildTaskPacket({
201
- agentId: job.agentId,
202
- objective,
203
- memoryStore: this.memoryStore,
204
- backendManager: this.backendManager,
205
- workspaceRoot: agent.workspaceRoot,
206
- backendOrder: effectiveMemoryBackends,
207
- qmdSupplementLimit: effectiveQmdSupplementLimit,
208
- retrievalQueries,
209
- identity,
210
- interactionContract: effectiveInteractionContract,
211
- outputContract: job.outputContract,
212
- reportGuidance: effectiveReportGuidance,
213
- allowedTools: inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]),
214
- artifactRefs: options.artifactRefs || [],
215
- budget: job.budget,
216
- memoryLimit: effectiveMemoryLimit,
217
- retrievalBlend: effectiveRetrievalBlend
218
- });
219
-
220
- const allowedTools = inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]);
221
- const routing = new TaskRouter(runtime.taskRouter).choose({
222
- jobId: job.id,
223
- taskType: job.taskType,
224
- objective,
225
- requestedLane: job.modelLane,
226
- agentPrimaryLane: agent.primaryLane,
227
- outputTarget: job.outputTarget || null,
228
- requiresPingback: effectiveInteractionContract.requiresPingback,
229
- notifyOnDone: effectiveInteractionContract.notifyOnDone,
230
- deliveryProfile: agent.delivery?.profile || null,
231
- artifactRefs: options.artifactRefs || [],
232
- allowedTools
233
- });
234
-
235
- return {
236
- job,
237
- agent: {
238
- id: job.agentId,
239
- name: agent.name || job.agentId,
240
- workspaceRoot: agent.workspaceRoot,
241
- primaryLane: agent.primaryLane,
242
- fallbackLane: agent.fallbackLane || null,
243
- memoryPolicy: agent.memoryPolicy,
244
- toolPolicy: agent.toolPolicy,
245
- soulRef: agent.soulRef || null,
246
- purposeRef: agent.purposeRef || null,
247
- memoryBackendOrder: effectiveMemoryBackends,
248
- memoryBackendSource: job.memoryBackends ? "job" : "agent",
249
- qmdSupplementLimit: effectiveQmdSupplementLimit,
250
- memoryLimit: effectiveMemoryLimit,
251
- interactionContractSource: job.interactionContract ? "job" : agent.interactionContract ? "agent" : "default",
252
- interactionContract: effectiveInteractionContract,
253
- deliveryProfile: agent.delivery?.profile || null,
254
- memoryBackends: await this.backendManager.inspect(job.agentId, agent.workspaceRoot),
255
- reportGuidance: effectiveReportGuidance,
256
- retrievalBlend: effectiveRetrievalBlend,
257
- execApprovals: agent.execApprovals ?? false,
258
- },
259
- packet,
260
- routing: {
261
- requestedLane: job.modelLane,
262
- selectedLane: routing.selectedLane,
263
- routeMode: routing.routeMode,
264
- matchedRule: routing.matchedRule,
265
- reasons: routing.reasons,
266
- description: routing.description || null
267
- }
268
- };
269
- }
270
-
271
- /**
272
- * Hydrate an interactive job (Telegram message or agent dispatch) into a
273
- * plan object compatible with executor.executeHydratedJob(). Unlike
274
- * hydrateJob(), this does not look up a TOML job definition — it builds
275
- * the plan from the raw message text and the target agent's config.
276
- */
277
- async hydrateInteractiveJob({ jobId, agentId, input, imageRefs = null, sessionId = null, contextLedger = null, conversationContext = null }, options = {}) {
278
- const runtime = await this.loadRuntime();
279
- const agent = runtime.agents[agentId];
280
- if (!agent) {
281
- throw new Error(`Unknown agent for interactive job ${jobId}: ${agentId}`);
282
- }
283
-
284
- if (options.shadowImport && agent.workspaceRoot) {
285
- const policy = normalizePolicy(runtime.policies[`memory-${agent.memoryPolicy}`] || runtime.policies[agent.memoryPolicy]);
286
- await this.bridge.importWorkspaceSnapshot(agentId, this.memoryStore, policy, {
287
- workspaceOverride: agent.workspaceRoot
288
- });
289
- }
290
-
291
- const objective = input;
292
-
293
- const cacheKey = [
294
- agent.soulRef || "",
295
- agent.purposeRef || "",
296
- agent.userRef || "",
297
- (agent.workspaceContextFiles || []).join(","),
298
- agent.workspaceRoot || ""
299
- ].join("|");
300
-
301
- let identity = getCached(cacheKey);
302
- if (!identity) {
303
- identity = await this.identityCache.get(
304
- agentId,
305
- { soulRef: agent.soulRef || null, purposeRef: agent.purposeRef || null, userRef: agent.userRef || null },
306
- () => loadAgentIdentity(this.projectRoot, agent)
307
- );
308
- setCached(cacheKey, identity);
309
- }
310
-
311
- const effectiveMemoryBackends = agent.memoryBackends || ["file"];
312
- const effectiveMemoryLimit = options.memoryLimit ?? 8;
313
- // Interactive jobs use a conversational contract — strip completion_sections
314
- // and handoff_format that force structured JSON output
315
- const effectiveInteractionContract = {
316
- ...resolveInteractionContract(agent.interactionContract || null, null),
317
- completionSections: [],
318
- handoffFormat: "conversational",
319
- ackMode: "silent",
320
- requiresPingback: false,
321
- notifyOnDone: false,
322
- };
323
- const retrievalQueries = planRetrievalQueries({
324
- objective,
325
- taskType: "interactive",
326
- purpose: identity?.purpose,
327
- identity,
328
- });
329
-
330
- const modelOverride = resolveModelOverride(options.modelOverride, { routerConfig: runtime.router });
331
-
332
- const modelId = modelOverride || runtime.router[agent.primaryLane]?.primary || "default";
333
- const maxOutput = getMaxOutputTokens(modelId);
334
- const contextLimit = getContextLimit(modelId);
335
-
336
- let effectiveContext;
337
- let tokenBudgetMeta = { removed: 0, estimatedTokens: 0 };
338
-
339
- // Try Context Ledger path (Phase 3)
340
- if (contextLedger && sessionId) {
341
- try {
342
- const { snapshot, events } = contextLedger.loadState(sessionId);
343
- if (events.length > 0) {
344
- const builder = new PromptBuilder({ contextLedger });
345
- let systemText = `You are ${identity?.soul || agentId}. ${identity?.purpose || ""}\n\n${(identity?.contextFiles || []).map(f => `[File: ${f.name}]\n${f.content}`).join("\n\n")}`;
346
- if (options && options.focusMode && options.focusMode !== "off") {
347
- const focusModeInstructions = {
348
- deep: "Current mode: deep work. Be terse. No preamble. No summaries. Direct answers only.",
349
- planning: "Current mode: planning. Use structured bullet points and numbered lists. Surface tradeoffs."
350
- };
351
- const focusInstruction = focusModeInstructions[options.focusMode] || `Current mode: ${options.focusMode}.`;
352
- systemText += `\n\n${focusInstruction}`;
353
- }
354
- if (options && options.sourceType === 'agent') {
355
- const agentLabel = options.sourceAgentId || 'unknown';
356
- systemText += `\n\nContext: This message is from a peer agent (${agentLabel}), not from the human operator. Reply in structured, direct format. No conversational padding. Route your reply back to the agent.`;
357
- }
358
- const tools = inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]);
359
-
360
- const result = await builder.buildPrompt({
361
- sessionId,
362
- events,
363
- snapshot,
364
- systemText,
365
- tools: (tools || []).map(t => ({ name: t })),
366
- tokenBudgetTotal: contextLimit
367
- });
368
-
369
- effectiveContext = result.messages;
370
- tokenBudgetMeta = { removed: 0, estimatedTokens: result.tokenEstimate };
371
-
372
- console.log(JSON.stringify({
373
- service: "prompt_builder",
374
- session_id: sessionId,
375
- prefix_hash: result.prefixHash,
376
- token_estimate: result.tokenEstimate
377
- }));
378
- }
379
- } catch (err) {
380
- console.error(JSON.stringify({ service: "prompt_builder", event: "error", error: err.message, session_id: sessionId }));
381
- }
382
- }
383
-
384
- // Fallback to blob-based path
385
- if (!effectiveContext && conversationContext) {
386
- const parsed = JSON.parse(conversationContext);
387
- const { pruned, removed, estimatedTokens } = pruneContext(parsed, contextLimit);
388
- effectiveContext = pruned;
389
- tokenBudgetMeta = { removed, estimatedTokens };
390
- }
391
-
392
- // Inject focus mode into identity soul for blob path
393
- if (options && options.focusMode && options.focusMode !== "off") {
394
- const focusModeInstructions = {
395
- deep: "Current mode: deep work. Be terse. No preamble. No summaries. Direct answers only.",
396
- planning: "Current mode: planning. Use structured bullet points and numbered lists. Surface tradeoffs."
397
- };
398
- const focusInstruction = focusModeInstructions[options.focusMode] || `Current mode: ${options.focusMode}.`;
399
- identity = { ...identity, soul: (identity?.soul || "") + `\n\n${focusInstruction}` };
400
- }
401
-
402
- // Inject source context for agent-originated messages
403
- if (options && options.sourceType === 'agent') {
404
- const agentLabel = options.sourceAgentId || 'unknown';
405
- const agentInstruction = `\n\nContext: This message is from a peer agent (${agentLabel}), not from the human operator. Reply in structured, direct format. No conversational padding. Route your reply back to the agent.`;
406
- identity = { ...identity, soul: (identity?.soul || "") + agentInstruction };
407
- }
408
-
409
- console.log(JSON.stringify({
410
- service: "token_budget",
411
- model: modelId,
412
- estimated_tokens: tokenBudgetMeta.estimatedTokens,
413
- removed_turns: tokenBudgetMeta.removed,
414
- max_output: maxOutput,
415
- }));
416
-
417
- const packet = await buildTaskPacket({
418
- agentId,
419
- objective,
420
- memoryStore: this.memoryStore,
421
- backendManager: this.backendManager,
422
- workspaceRoot: agent.workspaceRoot,
423
- backendOrder: effectiveMemoryBackends,
424
- qmdSupplementLimit: agent.qmdSupplementLimit ?? 2,
425
- retrievalQueries,
426
- identity,
427
- interactionContract: effectiveInteractionContract,
428
- outputContract: null,
429
- reportGuidance: null,
430
- allowedTools: inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]),
431
- artifactRefs: [],
432
- budget: { maxTokens: maxOutput, maxRuntimeSeconds: 120 },
433
- memoryLimit: effectiveMemoryLimit,
434
- retrievalBlend: runtime.runtime?.retrieval || null,
435
- conversationContext: effectiveContext,
436
- });
437
- packet.modelOverride = modelOverride;
438
- packet.imageRefs = imageRefs;
439
- if (options.thinkMode) {
440
- packet.thinkMode = options.thinkMode;
441
- }
442
-
443
- const allowedTools = inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]);
444
- const router = new TaskRouter(runtime.taskRouter);
445
- const routing = router.choose({
446
- jobId,
447
- taskType: "interactive",
448
- objective,
449
- requestedLane: null,
450
- agentPrimaryLane: agent.primaryLane,
451
- outputTarget: null,
452
- requiresPingback: effectiveInteractionContract.requiresPingback,
453
- notifyOnDone: effectiveInteractionContract.notifyOnDone,
454
- deliveryProfile: agent.delivery?.profile || null,
455
- artifactRefs: [],
456
- allowedTools
457
- });
458
-
459
- return {
460
- job: {
461
- id: jobId,
462
- trigger: "interactive",
463
- taskType: "interactive",
464
- agentId,
465
- modelLane: null,
466
- outputTarget: null,
467
- idempotencyKey: null,
468
- liveJobHints: [],
469
- budget: { maxTokens: maxOutput, maxRuntimeSeconds: 120 },
470
- },
471
- agent: {
472
- id: agentId,
473
- name: agent.name || agentId,
474
- workspaceRoot: agent.workspaceRoot,
475
- primaryLane: agent.primaryLane,
476
- fallbackLane: agent.fallbackLane || null,
477
- memoryPolicy: agent.memoryPolicy,
478
- toolPolicy: agent.toolPolicy,
479
- soulRef: agent.soulRef || null,
480
- purposeRef: agent.purposeRef || null,
481
- memoryBackendOrder: effectiveMemoryBackends,
482
- memoryBackendSource: "agent",
483
- qmdSupplementLimit: agent.qmdSupplementLimit ?? 2,
484
- memoryLimit: effectiveMemoryLimit,
485
- interactionContractSource: agent.interactionContract ? "agent" : "default",
486
- interactionContract: effectiveInteractionContract,
487
- deliveryProfile: agent.delivery?.profile || null,
488
- memoryBackends: await this.backendManager.inspect(agentId, agent.workspaceRoot),
489
- reportGuidance: null,
490
- retrievalBlend: runtime.runtime?.retrieval || null,
491
- execApprovals: agent.execApprovals ?? false,
492
- },
493
- packet,
494
- routing: {
495
- requestedLane: null,
496
- selectedLane: routing.selectedLane,
497
- routeMode: routing.routeMode,
498
- matchedRule: routing.matchedRule,
499
- reasons: routing.reasons,
500
- description: routing.description || null
501
- }
502
- };
503
- }
504
-
505
- async summarizeLiveCron() {
506
- const jobs = await this.bridge.loadCronJobs();
507
- return jobs.map((job) => ({
508
- id: job.id,
509
- name: job.name,
510
- description: job.description || null,
511
- enabled: job.enabled,
512
- schedule: job.schedule,
513
- targetModel: job.payload?.model || null,
514
- timeoutSeconds: job.payload?.timeoutSeconds || null,
515
- kind: job.payload?.kind || null,
516
- messagePreview: String(job.payload?.message || job.payload?.text || "").slice(0, 160),
517
- lastStatus: job.state?.lastStatus || null,
518
- consecutiveErrors: job.state?.consecutiveErrors || 0
519
- }));
520
- }
521
-
522
- async compareJobs() {
523
- const [v2Jobs, liveJobs] = await Promise.all([this.listJobPlans(), this.summarizeLiveCron()]);
524
-
525
- return v2Jobs.map((job) => ({
526
- v2JobId: job.id,
527
- v2TaskType: job.taskType,
528
- v2Trigger: job.trigger,
529
- liveJobHints: job.liveJobHints,
530
- closestLiveJobs: liveJobs
531
- .map((liveJob) => ({
532
- ...liveJob,
533
- _score: compareJobText(job, liveJob)
534
- }))
535
- .filter((liveJob) => liveJob._score > 0)
536
- .sort((a, b) => b._score - a._score)
537
- .slice(0, 3)
538
- .map(({ _score, ...liveJob }) => liveJob)
539
- }));
540
- }
541
- }
542
-
543
- function normalizeOutputContract(contract, outputContracts = null) {
544
- const defaultProfile = outputContracts?.profiles?.default || null;
545
- if (!contract) {
546
- if (!defaultProfile) return null;
547
- return {
548
- format: null,
549
- requiredSections: [],
550
- styleHints: [],
551
- profile: {
552
- requireStatus: defaultProfile.requireStatus ?? false,
553
- sectionStyle: defaultProfile.sectionStyle || "bullets",
554
- requireSectionItems: defaultProfile.requireSectionItems ?? true,
555
- templateLines: defaultProfile.templateLines || []
556
- }
557
- };
558
- }
559
- const profileKey = String(contract.format || "").replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
560
- const profile = outputContracts?.profiles?.[profileKey] || defaultProfile || null;
561
- return {
562
- format: contract.format || null,
563
- requiredSections: contract.requiredSections || [],
564
- styleHints: contract.styleHints || [],
565
- profile: profile
566
- ? {
567
- requireStatus: profile.requireStatus ?? false,
568
- sectionStyle: profile.sectionStyle || "bullets",
569
- requireSectionItems: profile.requireSectionItems ?? true,
570
- templateLines: profile.templateLines || []
571
- }
572
- : null
573
- };
574
- }
575
-
576
- function normalizeReportGuidance(reportGuidance) {
577
- if (!reportGuidance) return null;
578
- return {
579
- focus: reportGuidance.focus || [],
580
- qualityChecks: reportGuidance.qualityChecks || [],
581
- avoid: reportGuidance.avoid || []
582
- };
583
- }
584
-
585
- async function loadAgentIdentity(projectRoot, agent) {
586
- const cacheKey = [
587
- agent.soulRef || "",
588
- agent.purposeRef || "",
589
- agent.userRef || "",
590
- (agent.workspaceContextFiles || []).join(","),
591
- agent.workspaceRoot || ""
592
- ].join("|");
593
-
594
- const cached = getCached(cacheKey);
595
- if (cached) {
596
- console.log(`[identity-cache] hit for agent ${agent.id || "unknown"}`);
597
- return cached;
598
- }
599
-
600
- console.log(`[identity-cache] miss — loading from disk for agent ${agent.id || "unknown"}`);
601
-
602
- const soul = await readOptionalIdentity(projectRoot, agent.soulRef);
603
- const purpose = await readOptionalIdentity(projectRoot, agent.purposeRef);
604
- const user = await readOptionalIdentity(projectRoot, agent.userRef);
605
-
606
- // Load workspace context files (MEMORY.md, AGENTS.md, etc.) relative to workspace_root
607
- const contextFiles = [];
608
- if (agent.workspaceRoot && agent.workspaceContextFiles?.length) {
609
- const cap = agent.workspaceContextCap ?? 8000;
610
- for (const filename of agent.workspaceContextFiles) {
611
- const filePath = path.join(agent.workspaceRoot, filename);
612
- const content = await readText(filePath, null);
613
- if (content) {
614
- const capped = content.length > cap ? content.slice(0, cap) + `\n\n[truncated — full file at ${filePath}]` : content;
615
- contextFiles.push({ name: filename, content: capped });
616
- }
617
- }
618
- }
619
-
620
- const result = (!soul && !purpose && !user && contextFiles.length === 0)
621
- ? null
622
- : {
623
- soul,
624
- purpose,
625
- user,
626
- contextFiles
627
- };
628
-
629
- setCached(cacheKey, result);
630
- return result;
631
- }
632
-
633
- async function readOptionalIdentity(projectRoot, ref) {
634
- if (!ref) return null;
635
- return readText(path.isAbsolute(ref) ? ref : path.join(projectRoot, ref), null);
636
- }
637
-
638
- function inferToolsFromPolicy(policy) {
639
- return policy?.allowed || [];
640
- }
641
-
642
- function compareJobText(v2Job, liveJob) {
643
- const haystack = `${liveJob.name || ""} ${liveJob.description || ""} ${liveJob.id || ""} ${liveJob.kind || ""} ${liveJob.messagePreview || ""}`.toLowerCase();
644
- const stopwords = new Set(["check", "job", "task", "run", "daily", "hourly", "every", "rollup", "health"]);
645
- const needles = [...v2Job.taskType.split("_"), ...v2Job.id.split("-")].filter(
646
- (token) => token.length > 2 && !stopwords.has(token.toLowerCase())
647
- );
648
- let score = 0;
649
- for (const token of needles) {
650
- if (haystack.includes(token.toLowerCase())) score += 1;
651
- }
652
- for (const hint of v2Job.liveJobHints || []) {
653
- if (haystack.includes(String(hint).toLowerCase())) score += 3;
654
- }
655
- return score;
656
- }
1
+ import path from "node:path";
2
+ import { ConfigLoader } from "../config/loader.js";
3
+ import { buildTaskPacket } from "./task-packet.js";
4
+ import { MemoryStore } from "../memory/memory-store.js";
5
+ import { OpenClawShadowBridge } from "../shadow/bridge.js";
6
+ import { readText } from "../utils/fs.js";
7
+ import { EmbeddingIndex } from "../memory/embedding-index.js";
8
+ import { EmbeddingService } from "../memory/embedding-service.js";
9
+ import { MemoryBackendManager } from "../memory/backend-manager.js";
10
+ import { planRetrievalQueries } from "../memory/retrieval-planner.js";
11
+ import { TaskRouter } from "./task-router.js";
12
+ import { resolveInteractionContract } from "./interaction-contract.js";
13
+ import { IdentityBootstrapCache } from "./bootstrap-cache.js";
14
+ import { getCached, setCached } from "./identity-cache.js";
15
+ import { getContextLimit, getMaxOutputTokens } from "../utils/model-context-limits.js";
16
+ import { pruneContext, estimateTokens } from "../utils/token-budget.js";
17
+ import { PromptBuilder } from "./prompt-builder.js";
18
+ import { resolveModelOverride } from "./model-resolution.js";
19
+
20
+ function normalizePolicy(policy) {
21
+ const categories = policy.categories || {};
22
+ return {
23
+ allow_durable_writes: policy.allowDurableWrites ?? false,
24
+ allow_identity_updates: policy.allowIdentityUpdates ?? false,
25
+ require_source_reference: policy.requireSourceReference ?? false,
26
+ require_write_reason: policy.requireWriteReason ?? false,
27
+ max_writes_per_run: policy.maxWritesPerRun ?? 0,
28
+ categories: {
29
+ allowed: categories.allowed || [],
30
+ blocked: categories.blocked || []
31
+ }
32
+ };
33
+ }
34
+
35
+ function deepMerge(base, override) {
36
+ if (Array.isArray(base) || Array.isArray(override)) {
37
+ return Array.isArray(override) ? [...override] : Array.isArray(base) ? [...base] : [];
38
+ }
39
+
40
+ if (!base || typeof base !== "object") {
41
+ return override === undefined ? base : override;
42
+ }
43
+
44
+ if (!override || typeof override !== "object") {
45
+ return override === undefined ? { ...base } : override;
46
+ }
47
+
48
+ const merged = { ...base };
49
+ for (const [key, value] of Object.entries(override)) {
50
+ merged[key] = key in base ? deepMerge(base[key], value) : value;
51
+ }
52
+ return merged;
53
+ }
54
+
55
+ function normalizeJob(jobId, jobConfig, runtime = {}) {
56
+ return {
57
+ id: jobId,
58
+ trigger: jobConfig.trigger,
59
+ taskType: jobConfig.taskType,
60
+ agentId: jobConfig.agentId,
61
+ modelLane: jobConfig.modelLane,
62
+ outputTarget: jobConfig.outputTarget,
63
+ idempotencyKey: jobConfig.idempotencyKey,
64
+ liveJobHints: jobConfig.liveJobHints || [],
65
+ memoryBackends: jobConfig.memoryBackends || null,
66
+ qmdSupplementLimit: jobConfig.qmdSupplementLimit ?? null,
67
+ memoryLimit: jobConfig.memoryLimit ?? null,
68
+ interactionContract: jobConfig.interactionContract || null,
69
+ reportFallback: jobConfig.reportFallback
70
+ ? {
71
+ enabled: jobConfig.reportFallback.enabled ?? false,
72
+ lane: jobConfig.reportFallback.lane || runtime.runtime?.reportFallback?.lane || "report_fallback_lowcost",
73
+ allowedFailureClasses:
74
+ jobConfig.reportFallback.allowedFailureClasses ||
75
+ runtime.runtime?.reportFallback?.allowedFailureClasses ||
76
+ ["timeout", "provider_loading"]
77
+ }
78
+ : null,
79
+ outputContract: normalizeOutputContract(jobConfig.outputContract || null, runtime.outputContracts || null),
80
+ reportGuidance: normalizeReportGuidance(jobConfig.reportGuidance || null),
81
+ budget: {
82
+ maxTokens: jobConfig.budget?.maxTokens ?? 4000,
83
+ maxRuntimeSeconds: jobConfig.budget?.maxRuntimeSeconds ?? 30
84
+ },
85
+ retry: {
86
+ maxAttempts: jobConfig.retry?.maxAttempts ?? 1
87
+ },
88
+ stop: {
89
+ haltOnPolicyError: jobConfig.stop?.haltOnPolicyError ?? true,
90
+ haltOnBudgetExceeded: jobConfig.stop?.haltOnBudgetExceeded ?? true
91
+ }
92
+ };
93
+ }
94
+
95
+ export class Scheduler {
96
+ constructor({ projectRoot, liveRoot, stateRoot }) {
97
+ this.projectRoot = projectRoot;
98
+ this.liveRoot = liveRoot;
99
+ this.stateRoot = stateRoot;
100
+ this.loader = new ConfigLoader({ rootDir: path.join(projectRoot, "config") });
101
+ this.memoryStore = new MemoryStore({ rootDir: path.join(stateRoot, "memory") });
102
+ this.bridge = new OpenClawShadowBridge({ liveRoot });
103
+ this.embeddingIndex = new EmbeddingIndex({
104
+ rootDir: path.join(stateRoot, "memory"),
105
+ embeddingService: new EmbeddingService({ projectRoot })
106
+ });
107
+ this.backendManager = new MemoryBackendManager({
108
+ memoryStore: this.memoryStore,
109
+ embeddingIndex: this.embeddingIndex,
110
+ liveRoot
111
+ });
112
+ this.identityCache = new IdentityBootstrapCache({
113
+ rootDir: path.join(stateRoot, "bootstrap", "identity")
114
+ });
115
+ }
116
+
117
+ async loadRuntime() {
118
+ const config = await this.loader.loadAll();
119
+ this.identityCache.ttlMs = config.runtime?.bootstrapCache?.identityTtlMs ?? this.identityCache.ttlMs;
120
+ return config;
121
+ }
122
+
123
+ async listJobPlans() {
124
+ const runtime = await this.loadRuntime();
125
+ return Object.entries(runtime.jobs).map(([jobId, jobConfig]) => {
126
+ const normalizedJob = normalizeJob(jobId, jobConfig, runtime);
127
+ const agent = runtime.agents[normalizedJob.agentId];
128
+ return {
129
+ ...normalizedJob,
130
+ workspaceRoot: agent?.workspaceRoot || null,
131
+ primaryLane: agent?.primaryLane || null,
132
+ deliveryProfile: agent?.delivery?.profile || null
133
+ };
134
+ });
135
+ }
136
+
137
+ async hydrateJob(jobId, options = {}) {
138
+ const runtime = await this.loadRuntime();
139
+ const rawJob = runtime.jobs[jobId];
140
+ if (!rawJob) {
141
+ throw new Error(`Unknown job: ${jobId}`);
142
+ }
143
+
144
+ const job = normalizeJob(jobId, rawJob, runtime);
145
+ const agent = runtime.agents[job.agentId];
146
+ if (!agent) {
147
+ throw new Error(`Unknown agent for job ${jobId}: ${job.agentId}`);
148
+ }
149
+
150
+ if (options.shadowImport) {
151
+ const policy = normalizePolicy(runtime.policies[`memory-${agent.memoryPolicy}`] || runtime.policies[agent.memoryPolicy]);
152
+ await this.bridge.importWorkspaceSnapshot(job.agentId, this.memoryStore, policy, {
153
+ workspaceOverride: agent.workspaceRoot
154
+ });
155
+ }
156
+
157
+ const objective =
158
+ options.objective ||
159
+ `Execute job ${job.id} of type ${job.taskType} with bounded runtime and low context overhead.`;
160
+
161
+ const cacheKey = [
162
+ agent.soulRef || "",
163
+ agent.purposeRef || "",
164
+ agent.userRef || "",
165
+ (agent.workspaceContextFiles || []).join(","),
166
+ agent.workspaceRoot || ""
167
+ ].join("|");
168
+
169
+ let identity = getCached(cacheKey);
170
+ if (!identity) {
171
+ identity = await this.identityCache.get(
172
+ job.agentId,
173
+ {
174
+ soulRef: agent.soulRef || null, purposeRef: agent.purposeRef || null, userRef: agent.userRef || null },
175
+ () => loadAgentIdentity(this.projectRoot, agent)
176
+ );
177
+ setCached(cacheKey, identity);
178
+ }
179
+ const effectiveMemoryBackends = job.memoryBackends || agent.memoryBackends || ["file"];
180
+ const effectiveQmdSupplementLimit = job.qmdSupplementLimit ?? agent.qmdSupplementLimit ?? 2;
181
+ const effectiveMemoryLimit = job.memoryLimit ?? options.memoryLimit ?? 8;
182
+ const effectiveInteractionContract = resolveInteractionContract(agent.interactionContract || null, job.interactionContract);
183
+ const effectiveReportGuidance = options.reportGuidanceOverride
184
+ ? normalizeReportGuidance(deepMerge(job.reportGuidance || {}, options.reportGuidanceOverride))
185
+ : job.reportGuidance;
186
+ const effectiveRetrievalBlend = options.retrievalBlendOverride
187
+ ? {
188
+ ...(runtime.runtime?.retrieval || {}),
189
+ ...options.retrievalBlendOverride
190
+ }
191
+ : runtime.runtime?.retrieval || null;
192
+ const retrievalQueries = planRetrievalQueries({
193
+ objective,
194
+ taskType: job.taskType,
195
+ purpose: identity?.purpose,
196
+ identity,
197
+ reportGuidance: effectiveReportGuidance
198
+ });
199
+
200
+ const packet = await buildTaskPacket({
201
+ agentId: job.agentId,
202
+ objective,
203
+ memoryStore: this.memoryStore,
204
+ backendManager: this.backendManager,
205
+ workspaceRoot: agent.workspaceRoot,
206
+ backendOrder: effectiveMemoryBackends,
207
+ qmdSupplementLimit: effectiveQmdSupplementLimit,
208
+ retrievalQueries,
209
+ identity,
210
+ interactionContract: effectiveInteractionContract,
211
+ outputContract: job.outputContract,
212
+ reportGuidance: effectiveReportGuidance,
213
+ allowedTools: inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]),
214
+ artifactRefs: options.artifactRefs || [],
215
+ budget: job.budget,
216
+ memoryLimit: effectiveMemoryLimit,
217
+ retrievalBlend: effectiveRetrievalBlend
218
+ });
219
+
220
+ const allowedTools = inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]);
221
+ const routing = new TaskRouter(runtime.taskRouter).choose({
222
+ jobId: job.id,
223
+ taskType: job.taskType,
224
+ objective,
225
+ requestedLane: job.modelLane,
226
+ agentPrimaryLane: agent.primaryLane,
227
+ outputTarget: job.outputTarget || null,
228
+ requiresPingback: effectiveInteractionContract.requiresPingback,
229
+ notifyOnDone: effectiveInteractionContract.notifyOnDone,
230
+ deliveryProfile: agent.delivery?.profile || null,
231
+ artifactRefs: options.artifactRefs || [],
232
+ allowedTools
233
+ });
234
+
235
+ return {
236
+ job,
237
+ agent: {
238
+ id: job.agentId,
239
+ name: agent.name || job.agentId,
240
+ workspaceRoot: agent.workspaceRoot,
241
+ primaryLane: agent.primaryLane,
242
+ fallbackLane: agent.fallbackLane || null,
243
+ memoryPolicy: agent.memoryPolicy,
244
+ toolPolicy: agent.toolPolicy,
245
+ soulRef: agent.soulRef || null,
246
+ purposeRef: agent.purposeRef || null,
247
+ memoryBackendOrder: effectiveMemoryBackends,
248
+ memoryBackendSource: job.memoryBackends ? "job" : "agent",
249
+ qmdSupplementLimit: effectiveQmdSupplementLimit,
250
+ memoryLimit: effectiveMemoryLimit,
251
+ interactionContractSource: job.interactionContract ? "job" : agent.interactionContract ? "agent" : "default",
252
+ interactionContract: effectiveInteractionContract,
253
+ deliveryProfile: agent.delivery?.profile || null,
254
+ memoryBackends: await this.backendManager.inspect(job.agentId, agent.workspaceRoot),
255
+ reportGuidance: effectiveReportGuidance,
256
+ retrievalBlend: effectiveRetrievalBlend,
257
+ execApprovals: agent.execApprovals ?? false,
258
+ },
259
+ packet,
260
+ routing: {
261
+ requestedLane: job.modelLane,
262
+ selectedLane: routing.selectedLane,
263
+ routeMode: routing.routeMode,
264
+ matchedRule: routing.matchedRule,
265
+ reasons: routing.reasons,
266
+ description: routing.description || null
267
+ }
268
+ };
269
+ }
270
+
271
+ /**
272
+ * Hydrate an interactive job (Telegram message or agent dispatch) into a
273
+ * plan object compatible with executor.executeHydratedJob(). Unlike
274
+ * hydrateJob(), this does not look up a TOML job definition — it builds
275
+ * the plan from the raw message text and the target agent's config.
276
+ */
277
+ async hydrateInteractiveJob({ jobId, agentId, input, imageRefs = null, sessionId = null, contextLedger = null, conversationContext = null }, options = {}) {
278
+ const runtime = await this.loadRuntime();
279
+ const agent = runtime.agents[agentId];
280
+ if (!agent) {
281
+ throw new Error(`Unknown agent for interactive job ${jobId}: ${agentId}`);
282
+ }
283
+
284
+ if (options.shadowImport && agent.workspaceRoot) {
285
+ const policy = normalizePolicy(runtime.policies[`memory-${agent.memoryPolicy}`] || runtime.policies[agent.memoryPolicy]);
286
+ await this.bridge.importWorkspaceSnapshot(agentId, this.memoryStore, policy, {
287
+ workspaceOverride: agent.workspaceRoot
288
+ });
289
+ }
290
+
291
+ const objective = input;
292
+
293
+ const cacheKey = [
294
+ agent.soulRef || "",
295
+ agent.purposeRef || "",
296
+ agent.userRef || "",
297
+ (agent.workspaceContextFiles || []).join(","),
298
+ agent.workspaceRoot || ""
299
+ ].join("|");
300
+
301
+ let identity = getCached(cacheKey);
302
+ if (!identity) {
303
+ identity = await this.identityCache.get(
304
+ agentId,
305
+ { soulRef: agent.soulRef || null, purposeRef: agent.purposeRef || null, userRef: agent.userRef || null },
306
+ () => loadAgentIdentity(this.projectRoot, agent)
307
+ );
308
+ setCached(cacheKey, identity);
309
+ }
310
+
311
+ const effectiveMemoryBackends = agent.memoryBackends || ["file"];
312
+ const effectiveMemoryLimit = options.memoryLimit ?? 8;
313
+ // Interactive jobs use a conversational contract — strip completion_sections
314
+ // and handoff_format that force structured JSON output
315
+ const effectiveInteractionContract = {
316
+ ...resolveInteractionContract(agent.interactionContract || null, null),
317
+ completionSections: [],
318
+ handoffFormat: "conversational",
319
+ ackMode: "silent",
320
+ requiresPingback: false,
321
+ notifyOnDone: false,
322
+ };
323
+ const retrievalQueries = planRetrievalQueries({
324
+ objective,
325
+ taskType: "interactive",
326
+ purpose: identity?.purpose,
327
+ identity,
328
+ });
329
+
330
+ const modelOverride = resolveModelOverride(options.modelOverride, { routerConfig: runtime.router });
331
+
332
+ const modelId = modelOverride || runtime.router[agent.primaryLane]?.primary || "default";
333
+ const maxOutput = getMaxOutputTokens(modelId);
334
+ const contextLimit = getContextLimit(modelId);
335
+
336
+ let effectiveContext;
337
+ let tokenBudgetMeta = { removed: 0, estimatedTokens: 0 };
338
+
339
+ // Try Context Ledger path (Phase 3)
340
+ if (contextLedger && sessionId) {
341
+ try {
342
+ const { snapshot, events } = contextLedger.loadState(sessionId);
343
+ if (events.length > 0) {
344
+ const builder = new PromptBuilder({ contextLedger });
345
+ let systemText = `You are ${identity?.soul || agentId}. ${identity?.purpose || ""}\n\n${(identity?.contextFiles || []).map(f => `[File: ${f.name}]\n${f.content}`).join("\n\n")}`;
346
+ if (options && options.focusMode && options.focusMode !== "off") {
347
+ const focusModeInstructions = {
348
+ deep: "Current mode: deep work. Be terse. No preamble. No summaries. Direct answers only.",
349
+ planning: "Current mode: planning. Use structured bullet points and numbered lists. Surface tradeoffs."
350
+ };
351
+ const focusInstruction = focusModeInstructions[options.focusMode] || `Current mode: ${options.focusMode}.`;
352
+ systemText += `\n\n${focusInstruction}`;
353
+ }
354
+ if (options && options.sourceType === 'agent') {
355
+ const agentLabel = options.sourceAgentId || 'unknown';
356
+ systemText += `\n\nContext: This message is from a peer agent (${agentLabel}), not from the human operator. Reply in structured, direct format. No conversational padding. Route your reply back to the agent.`;
357
+ }
358
+ const tools = inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]);
359
+
360
+ const result = await builder.buildPrompt({
361
+ sessionId,
362
+ events,
363
+ snapshot,
364
+ systemText,
365
+ tools: (tools || []).map(t => ({ name: t })),
366
+ tokenBudgetTotal: contextLimit
367
+ });
368
+
369
+ effectiveContext = result.messages;
370
+ tokenBudgetMeta = { removed: 0, estimatedTokens: result.tokenEstimate };
371
+
372
+ console.log(JSON.stringify({
373
+ service: "prompt_builder",
374
+ session_id: sessionId,
375
+ prefix_hash: result.prefixHash,
376
+ token_estimate: result.tokenEstimate
377
+ }));
378
+ }
379
+ } catch (err) {
380
+ console.error(JSON.stringify({ service: "prompt_builder", event: "error", error: err.message, session_id: sessionId }));
381
+ }
382
+ }
383
+
384
+ // Fallback to blob-based path
385
+ if (!effectiveContext && conversationContext) {
386
+ const parsed = JSON.parse(conversationContext);
387
+ const { pruned, removed, estimatedTokens } = pruneContext(parsed, contextLimit);
388
+ effectiveContext = pruned;
389
+ tokenBudgetMeta = { removed, estimatedTokens };
390
+ }
391
+
392
+ // Inject focus mode into identity soul for blob path
393
+ if (options && options.focusMode && options.focusMode !== "off") {
394
+ const focusModeInstructions = {
395
+ deep: "Current mode: deep work. Be terse. No preamble. No summaries. Direct answers only.",
396
+ planning: "Current mode: planning. Use structured bullet points and numbered lists. Surface tradeoffs."
397
+ };
398
+ const focusInstruction = focusModeInstructions[options.focusMode] || `Current mode: ${options.focusMode}.`;
399
+ identity = { ...identity, soul: (identity?.soul || "") + `\n\n${focusInstruction}` };
400
+ }
401
+
402
+ // Inject source context for agent-originated messages
403
+ if (options && options.sourceType === 'agent') {
404
+ const agentLabel = options.sourceAgentId || 'unknown';
405
+ const agentInstruction = `\n\nContext: This message is from a peer agent (${agentLabel}), not from the human operator. Reply in structured, direct format. No conversational padding. Route your reply back to the agent.`;
406
+ identity = { ...identity, soul: (identity?.soul || "") + agentInstruction };
407
+ }
408
+
409
+ console.log(JSON.stringify({
410
+ service: "token_budget",
411
+ model: modelId,
412
+ estimated_tokens: tokenBudgetMeta.estimatedTokens,
413
+ removed_turns: tokenBudgetMeta.removed,
414
+ max_output: maxOutput,
415
+ }));
416
+
417
+ const packet = await buildTaskPacket({
418
+ agentId,
419
+ objective,
420
+ memoryStore: this.memoryStore,
421
+ backendManager: this.backendManager,
422
+ workspaceRoot: agent.workspaceRoot,
423
+ backendOrder: effectiveMemoryBackends,
424
+ qmdSupplementLimit: agent.qmdSupplementLimit ?? 2,
425
+ retrievalQueries,
426
+ identity,
427
+ interactionContract: effectiveInteractionContract,
428
+ outputContract: null,
429
+ reportGuidance: null,
430
+ allowedTools: inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]),
431
+ artifactRefs: [],
432
+ budget: { maxTokens: maxOutput, maxRuntimeSeconds: 120 },
433
+ memoryLimit: effectiveMemoryLimit,
434
+ retrievalBlend: runtime.runtime?.retrieval || null,
435
+ conversationContext: effectiveContext,
436
+ });
437
+ packet.modelOverride = modelOverride;
438
+ packet.imageRefs = imageRefs;
439
+ if (options.thinkMode) {
440
+ packet.thinkMode = options.thinkMode;
441
+ }
442
+
443
+ const allowedTools = inferToolsFromPolicy(runtime.policies[`tools-${agent.toolPolicy}`] || runtime.policies[agent.toolPolicy]);
444
+ const router = new TaskRouter(runtime.taskRouter);
445
+ const routing = router.choose({
446
+ jobId,
447
+ taskType: "interactive",
448
+ objective,
449
+ requestedLane: null,
450
+ agentPrimaryLane: agent.primaryLane,
451
+ outputTarget: null,
452
+ requiresPingback: effectiveInteractionContract.requiresPingback,
453
+ notifyOnDone: effectiveInteractionContract.notifyOnDone,
454
+ deliveryProfile: agent.delivery?.profile || null,
455
+ artifactRefs: [],
456
+ allowedTools
457
+ });
458
+
459
+ return {
460
+ job: {
461
+ id: jobId,
462
+ trigger: "interactive",
463
+ taskType: "interactive",
464
+ agentId,
465
+ modelLane: null,
466
+ outputTarget: null,
467
+ idempotencyKey: null,
468
+ liveJobHints: [],
469
+ budget: { maxTokens: maxOutput, maxRuntimeSeconds: 120 },
470
+ },
471
+ agent: {
472
+ id: agentId,
473
+ name: agent.name || agentId,
474
+ workspaceRoot: agent.workspaceRoot,
475
+ primaryLane: agent.primaryLane,
476
+ fallbackLane: agent.fallbackLane || null,
477
+ memoryPolicy: agent.memoryPolicy,
478
+ toolPolicy: agent.toolPolicy,
479
+ soulRef: agent.soulRef || null,
480
+ purposeRef: agent.purposeRef || null,
481
+ memoryBackendOrder: effectiveMemoryBackends,
482
+ memoryBackendSource: "agent",
483
+ qmdSupplementLimit: agent.qmdSupplementLimit ?? 2,
484
+ memoryLimit: effectiveMemoryLimit,
485
+ interactionContractSource: agent.interactionContract ? "agent" : "default",
486
+ interactionContract: effectiveInteractionContract,
487
+ deliveryProfile: agent.delivery?.profile || null,
488
+ memoryBackends: await this.backendManager.inspect(agentId, agent.workspaceRoot),
489
+ reportGuidance: null,
490
+ retrievalBlend: runtime.runtime?.retrieval || null,
491
+ execApprovals: agent.execApprovals ?? false,
492
+ },
493
+ packet,
494
+ routing: {
495
+ requestedLane: null,
496
+ selectedLane: routing.selectedLane,
497
+ routeMode: routing.routeMode,
498
+ matchedRule: routing.matchedRule,
499
+ reasons: routing.reasons,
500
+ description: routing.description || null
501
+ }
502
+ };
503
+ }
504
+
505
+ async summarizeLiveCron() {
506
+ const jobs = await this.bridge.loadCronJobs();
507
+ return jobs.map((job) => ({
508
+ id: job.id,
509
+ name: job.name,
510
+ description: job.description || null,
511
+ enabled: job.enabled,
512
+ schedule: job.schedule,
513
+ targetModel: job.payload?.model || null,
514
+ timeoutSeconds: job.payload?.timeoutSeconds || null,
515
+ kind: job.payload?.kind || null,
516
+ messagePreview: String(job.payload?.message || job.payload?.text || "").slice(0, 160),
517
+ lastStatus: job.state?.lastStatus || null,
518
+ consecutiveErrors: job.state?.consecutiveErrors || 0
519
+ }));
520
+ }
521
+
522
+ async compareJobs() {
523
+ const [v2Jobs, liveJobs] = await Promise.all([this.listJobPlans(), this.summarizeLiveCron()]);
524
+
525
+ return v2Jobs.map((job) => ({
526
+ v2JobId: job.id,
527
+ v2TaskType: job.taskType,
528
+ v2Trigger: job.trigger,
529
+ liveJobHints: job.liveJobHints,
530
+ closestLiveJobs: liveJobs
531
+ .map((liveJob) => ({
532
+ ...liveJob,
533
+ _score: compareJobText(job, liveJob)
534
+ }))
535
+ .filter((liveJob) => liveJob._score > 0)
536
+ .sort((a, b) => b._score - a._score)
537
+ .slice(0, 3)
538
+ .map(({ _score, ...liveJob }) => liveJob)
539
+ }));
540
+ }
541
+ }
542
+
543
+ function normalizeOutputContract(contract, outputContracts = null) {
544
+ const defaultProfile = outputContracts?.profiles?.default || null;
545
+ if (!contract) {
546
+ if (!defaultProfile) return null;
547
+ return {
548
+ format: null,
549
+ requiredSections: [],
550
+ styleHints: [],
551
+ profile: {
552
+ requireStatus: defaultProfile.requireStatus ?? false,
553
+ sectionStyle: defaultProfile.sectionStyle || "bullets",
554
+ requireSectionItems: defaultProfile.requireSectionItems ?? true,
555
+ templateLines: defaultProfile.templateLines || []
556
+ }
557
+ };
558
+ }
559
+ const profileKey = String(contract.format || "").replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
560
+ const profile = outputContracts?.profiles?.[profileKey] || defaultProfile || null;
561
+ return {
562
+ format: contract.format || null,
563
+ requiredSections: contract.requiredSections || [],
564
+ styleHints: contract.styleHints || [],
565
+ profile: profile
566
+ ? {
567
+ requireStatus: profile.requireStatus ?? false,
568
+ sectionStyle: profile.sectionStyle || "bullets",
569
+ requireSectionItems: profile.requireSectionItems ?? true,
570
+ templateLines: profile.templateLines || []
571
+ }
572
+ : null
573
+ };
574
+ }
575
+
576
+ function normalizeReportGuidance(reportGuidance) {
577
+ if (!reportGuidance) return null;
578
+ return {
579
+ focus: reportGuidance.focus || [],
580
+ qualityChecks: reportGuidance.qualityChecks || [],
581
+ avoid: reportGuidance.avoid || []
582
+ };
583
+ }
584
+
585
+ async function loadAgentIdentity(projectRoot, agent) {
586
+ const cacheKey = [
587
+ agent.soulRef || "",
588
+ agent.purposeRef || "",
589
+ agent.userRef || "",
590
+ (agent.workspaceContextFiles || []).join(","),
591
+ agent.workspaceRoot || ""
592
+ ].join("|");
593
+
594
+ const cached = getCached(cacheKey);
595
+ if (cached) {
596
+ console.log(`[identity-cache] hit for agent ${agent.id || "unknown"}`);
597
+ return cached;
598
+ }
599
+
600
+ console.log(`[identity-cache] miss — loading from disk for agent ${agent.id || "unknown"}`);
601
+
602
+ const soul = await readOptionalIdentity(projectRoot, agent.soulRef);
603
+ const purpose = await readOptionalIdentity(projectRoot, agent.purposeRef);
604
+ const user = await readOptionalIdentity(projectRoot, agent.userRef);
605
+
606
+ // Load workspace context files (MEMORY.md, AGENTS.md, etc.) relative to workspace_root
607
+ const contextFiles = [];
608
+ if (agent.workspaceRoot && agent.workspaceContextFiles?.length) {
609
+ const cap = agent.workspaceContextCap ?? 8000;
610
+ for (const filename of agent.workspaceContextFiles) {
611
+ const filePath = path.join(agent.workspaceRoot, filename);
612
+ const content = await readText(filePath, null);
613
+ if (content) {
614
+ const capped = content.length > cap ? content.slice(0, cap) + `\n\n[truncated — full file at ${filePath}]` : content;
615
+ contextFiles.push({ name: filename, content: capped });
616
+ }
617
+ }
618
+ }
619
+
620
+ const result = (!soul && !purpose && !user && contextFiles.length === 0)
621
+ ? null
622
+ : {
623
+ soul,
624
+ purpose,
625
+ user,
626
+ contextFiles
627
+ };
628
+
629
+ setCached(cacheKey, result);
630
+ return result;
631
+ }
632
+
633
+ async function readOptionalIdentity(projectRoot, ref) {
634
+ if (!ref) return null;
635
+ return readText(path.isAbsolute(ref) ? ref : path.join(projectRoot, ref), null);
636
+ }
637
+
638
+ function inferToolsFromPolicy(policy) {
639
+ return policy?.allowed || [];
640
+ }
641
+
642
+ function compareJobText(v2Job, liveJob) {
643
+ const haystack = `${liveJob.name || ""} ${liveJob.description || ""} ${liveJob.id || ""} ${liveJob.kind || ""} ${liveJob.messagePreview || ""}`.toLowerCase();
644
+ const stopwords = new Set(["check", "job", "task", "run", "daily", "hourly", "every", "rollup", "health"]);
645
+ const needles = [...v2Job.taskType.split("_"), ...v2Job.id.split("-")].filter(
646
+ (token) => token.length > 2 && !stopwords.has(token.toLowerCase())
647
+ );
648
+ let score = 0;
649
+ for (const token of needles) {
650
+ if (haystack.includes(token.toLowerCase())) score += 1;
651
+ }
652
+ for (const hint of v2Job.liveJobHints || []) {
653
+ if (haystack.includes(String(hint).toLowerCase())) score += 3;
654
+ }
655
+ return score;
656
+ }