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.
- package/.env.example +49 -49
- package/LICENSE +21 -21
- package/README.md +209 -209
- package/SECURITY.md +59 -119
- package/bin/nemoris +46 -46
- package/config/agents/agent.toml.example +28 -28
- package/config/agents/content.toml +23 -0
- package/config/agents/default.toml +22 -22
- package/config/agents/heartbeat.toml +35 -0
- package/config/agents/iris.toml +23 -0
- package/config/agents/lab.toml +23 -0
- package/config/agents/main.toml +45 -0
- package/config/agents/nemo.toml +21 -0
- package/config/agents/ops.toml +38 -0
- package/config/agents/orchestrator.toml +18 -18
- package/config/agents/revenue.toml +23 -0
- package/config/agents/testyboo.toml +19 -0
- package/config/delivery.toml +73 -73
- package/config/embeddings.toml +5 -5
- package/config/identity/content-purpose.md +11 -0
- package/config/identity/content-soul.md +45 -0
- package/config/identity/default-purpose.md +1 -1
- package/config/identity/default-soul.md +3 -3
- package/config/identity/heartbeat-purpose.md +9 -0
- package/config/identity/heartbeat-soul.md +16 -0
- package/config/identity/iris-purpose.md +17 -0
- package/config/identity/iris-soul.md +68 -0
- package/config/identity/lab-purpose.md +10 -0
- package/config/identity/lab-soul.md +38 -0
- package/config/identity/main-purpose.md +17 -0
- package/config/identity/main-soul.md +66 -0
- package/config/identity/main-user.md +22 -0
- package/config/identity/ops-purpose.md +9 -0
- package/config/identity/ops-soul.md +16 -0
- package/config/identity/orchestrator-purpose.md +1 -1
- package/config/identity/orchestrator-soul.md +1 -1
- package/config/identity/revenue-purpose.md +9 -0
- package/config/identity/revenue-soul.md +41 -0
- package/config/identity/testyboo-purpose.md +13 -0
- package/config/identity/testyboo-soul.md +20 -0
- package/config/improvement-targets.toml +15 -15
- package/config/jobs/heartbeat-check.toml +30 -30
- package/config/jobs/memory-rollup.toml +46 -46
- package/config/jobs/workspace-health.toml +63 -63
- package/config/mcp.toml +16 -16
- package/config/output-contracts.toml +17 -17
- package/config/peers.toml +32 -32
- package/config/peers.toml.example +32 -32
- package/config/policies/memory-default.toml +10 -10
- package/config/policies/memory-heartbeat.toml +5 -5
- package/config/policies/memory-ops.toml +10 -10
- package/config/policies/tools-heartbeat-minimal.toml +8 -8
- package/config/policies/tools-interactive-safe.toml +8 -8
- package/config/policies/tools-ops-bounded.toml +8 -8
- package/config/policies/tools-orchestrator.toml +7 -7
- package/config/providers/anthropic.toml +15 -15
- package/config/providers/ollama.toml +5 -5
- package/config/providers/openai-codex.toml +9 -9
- package/config/providers/openrouter.toml +5 -5
- package/config/router.toml +22 -22
- package/config/runtime.toml +114 -114
- package/config/skills/self-improvement.toml +15 -15
- package/config/skills/telegram-onboarding-spec.md +240 -240
- package/config/skills/workspace-monitor.toml +15 -15
- package/config/task-router.toml +42 -42
- package/install.sh +50 -50
- package/package.json +91 -90
- package/src/auth/auth-profiles.js +169 -169
- package/src/auth/openai-codex-oauth.js +285 -285
- package/src/battle.js +449 -449
- package/src/cli/help.js +265 -265
- package/src/cli/output-filter.js +49 -49
- package/src/cli/runtime-control.js +704 -704
- package/src/cli-main.js +2763 -2763
- package/src/cli.js +78 -78
- package/src/config/loader.js +332 -332
- package/src/config/schema-validator.js +214 -214
- package/src/config/toml-lite.js +8 -8
- package/src/daemon/action-handlers.js +71 -71
- package/src/daemon/healing-tick.js +87 -87
- package/src/daemon/health-probes.js +90 -90
- package/src/daemon/notifier.js +57 -57
- package/src/daemon/nurse.js +218 -218
- package/src/daemon/repair-log.js +106 -106
- package/src/daemon/rule-staging.js +90 -90
- package/src/daemon/rules.js +29 -29
- package/src/daemon/telegram-commands.js +54 -54
- package/src/daemon/updater.js +85 -85
- package/src/jobs/job-runner.js +78 -78
- package/src/mcp/consumer.js +129 -129
- package/src/memory/active-recall.js +171 -171
- package/src/memory/backend-manager.js +97 -97
- package/src/memory/backends/file-backend.js +38 -38
- package/src/memory/backends/qmd-backend.js +219 -219
- package/src/memory/embedding-guards.js +24 -24
- package/src/memory/embedding-index.js +118 -118
- package/src/memory/embedding-service.js +179 -179
- package/src/memory/file-index.js +177 -177
- package/src/memory/memory-signature.js +5 -5
- package/src/memory/memory-store.js +648 -648
- package/src/memory/retrieval-planner.js +66 -66
- package/src/memory/scoring.js +145 -145
- package/src/memory/simhash.js +78 -78
- package/src/memory/sqlite-active-store.js +824 -824
- package/src/memory/write-policy.js +36 -36
- package/src/onboarding/aliases.js +33 -33
- package/src/onboarding/auth/api-key.js +224 -224
- package/src/onboarding/auth/ollama-detect.js +42 -42
- package/src/onboarding/clack-prompter.js +77 -77
- package/src/onboarding/doctor.js +530 -530
- package/src/onboarding/lock.js +42 -42
- package/src/onboarding/model-catalog.js +344 -344
- package/src/onboarding/phases/auth.js +576 -589
- package/src/onboarding/phases/build.js +130 -130
- package/src/onboarding/phases/choose.js +82 -82
- package/src/onboarding/phases/detect.js +98 -98
- package/src/onboarding/phases/hatch.js +216 -216
- package/src/onboarding/phases/identity.js +79 -79
- package/src/onboarding/phases/ollama.js +345 -345
- package/src/onboarding/phases/scaffold.js +99 -99
- package/src/onboarding/phases/telegram.js +377 -377
- package/src/onboarding/phases/validate.js +204 -204
- package/src/onboarding/phases/verify.js +206 -206
- package/src/onboarding/platform.js +482 -482
- package/src/onboarding/status-bar.js +95 -95
- package/src/onboarding/templates.js +794 -794
- package/src/onboarding/toml-writer.js +38 -38
- package/src/onboarding/tui.js +250 -250
- package/src/onboarding/uninstall.js +153 -153
- package/src/onboarding/wizard.js +516 -499
- package/src/providers/anthropic.js +168 -168
- package/src/providers/base.js +247 -247
- package/src/providers/circuit-breaker.js +136 -136
- package/src/providers/ollama.js +163 -163
- package/src/providers/openai-codex.js +149 -149
- package/src/providers/openrouter.js +136 -136
- package/src/providers/registry.js +36 -36
- package/src/providers/router.js +16 -16
- package/src/runtime/bootstrap-cache.js +47 -47
- package/src/runtime/capabilities-prompt.js +25 -25
- package/src/runtime/completion-ping.js +99 -99
- package/src/runtime/config-validator.js +121 -121
- package/src/runtime/context-ledger.js +360 -360
- package/src/runtime/cutover-readiness.js +42 -42
- package/src/runtime/daemon.js +729 -729
- package/src/runtime/delivery-ack.js +195 -195
- package/src/runtime/delivery-adapters/local-file.js +41 -41
- package/src/runtime/delivery-adapters/openclaw-cli.js +94 -94
- package/src/runtime/delivery-adapters/openclaw-peer.js +98 -98
- package/src/runtime/delivery-adapters/shadow.js +13 -13
- package/src/runtime/delivery-adapters/standalone-http.js +98 -98
- package/src/runtime/delivery-adapters/telegram.js +104 -104
- package/src/runtime/delivery-adapters/tui.js +128 -128
- package/src/runtime/delivery-manager.js +807 -807
- package/src/runtime/delivery-store.js +168 -168
- package/src/runtime/dependency-health.js +118 -118
- package/src/runtime/envelope.js +114 -114
- package/src/runtime/evaluation.js +1089 -1089
- package/src/runtime/exec-approvals.js +216 -216
- package/src/runtime/executor.js +500 -500
- package/src/runtime/failure-ping.js +67 -67
- package/src/runtime/flows.js +83 -83
- package/src/runtime/guards.js +45 -45
- package/src/runtime/handoff.js +51 -51
- package/src/runtime/identity-cache.js +28 -28
- package/src/runtime/improvement-engine.js +109 -109
- package/src/runtime/improvement-harness.js +581 -581
- package/src/runtime/input-sanitiser.js +72 -72
- package/src/runtime/interaction-contract.js +347 -347
- package/src/runtime/lane-readiness.js +226 -226
- package/src/runtime/migration.js +323 -323
- package/src/runtime/model-resolution.js +78 -78
- package/src/runtime/network.js +64 -64
- package/src/runtime/notification-store.js +97 -97
- package/src/runtime/notifier.js +256 -256
- package/src/runtime/orchestrator.js +53 -53
- package/src/runtime/orphan-reaper.js +41 -41
- package/src/runtime/output-contract-schema.js +139 -139
- package/src/runtime/output-contract-validator.js +439 -439
- package/src/runtime/peer-readiness.js +69 -69
- package/src/runtime/peer-registry.js +133 -133
- package/src/runtime/pilot-status.js +108 -108
- package/src/runtime/prompt-builder.js +261 -261
- package/src/runtime/provider-attempt.js +582 -582
- package/src/runtime/report-fallback.js +71 -71
- package/src/runtime/result-normalizer.js +183 -183
- package/src/runtime/retention.js +74 -74
- package/src/runtime/review.js +244 -244
- package/src/runtime/route-job.js +15 -15
- package/src/runtime/run-store.js +38 -38
- package/src/runtime/schedule.js +88 -88
- package/src/runtime/scheduler-state.js +434 -434
- package/src/runtime/scheduler.js +656 -656
- package/src/runtime/session-compactor.js +182 -182
- package/src/runtime/session-search.js +155 -155
- package/src/runtime/slack-inbound.js +249 -249
- package/src/runtime/ssrf.js +102 -102
- package/src/runtime/status-aggregator.js +330 -330
- package/src/runtime/task-contract.js +140 -140
- package/src/runtime/task-packet.js +107 -107
- package/src/runtime/task-router.js +140 -140
- package/src/runtime/telegram-inbound.js +1565 -1565
- package/src/runtime/token-counter.js +134 -134
- package/src/runtime/token-estimator.js +59 -59
- package/src/runtime/tool-loop.js +200 -200
- package/src/runtime/transport-server.js +311 -311
- package/src/runtime/tui-server.js +411 -411
- package/src/runtime/ulid.js +44 -44
- package/src/security/ssrf-check.js +197 -197
- package/src/setup.js +369 -369
- package/src/shadow/bridge.js +303 -303
- package/src/skills/loader.js +84 -84
- package/src/tools/catalog.json +49 -49
- package/src/tools/cli-delegate.js +44 -44
- package/src/tools/mcp-client.js +106 -106
- package/src/tools/micro/cancel-task.js +6 -6
- package/src/tools/micro/complete-task.js +6 -6
- package/src/tools/micro/fail-task.js +6 -6
- package/src/tools/micro/http-fetch.js +74 -74
- package/src/tools/micro/index.js +36 -36
- package/src/tools/micro/lcm-recall.js +60 -60
- package/src/tools/micro/list-dir.js +17 -17
- package/src/tools/micro/list-skills.js +46 -46
- package/src/tools/micro/load-skill.js +38 -38
- package/src/tools/micro/memory-search.js +45 -45
- package/src/tools/micro/read-file.js +11 -11
- package/src/tools/micro/session-search.js +54 -54
- package/src/tools/micro/shell-exec.js +43 -43
- package/src/tools/micro/trigger-job.js +79 -79
- package/src/tools/micro/web-search.js +58 -58
- package/src/tools/micro/workspace-paths.js +39 -39
- package/src/tools/micro/write-file.js +14 -14
- package/src/tools/micro/write-memory.js +41 -41
- package/src/tools/registry.js +348 -348
- package/src/tools/tool-result-contract.js +36 -36
- package/src/tui/chat.js +835 -835
- package/src/tui/renderer.js +175 -175
- package/src/tui/socket-client.js +217 -217
- package/src/utils/canonical-json.js +29 -29
- package/src/utils/compaction.js +30 -30
- package/src/utils/env-loader.js +5 -5
- package/src/utils/errors.js +80 -80
- package/src/utils/fs.js +101 -101
- package/src/utils/ids.js +5 -5
- package/src/utils/model-context-limits.js +30 -30
- package/src/utils/token-budget.js +74 -74
- package/src/utils/usage-cost.js +25 -25
- package/src/utils/usage-metrics.js +14 -14
package/src/runtime/scheduler.js
CHANGED
|
@@ -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
|
+
}
|