odai-dsh-agent 0.2.0 → 0.2.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/LICENSE +21 -21
- package/README.md +138 -134
- package/bin/odai-dsh-agent.mjs +108 -108
- package/package.json +69 -69
- package/preset/odai/agent.cordis.yml +260 -260
- package/preset/odai/preset.yml +3 -3
- package/preset/odai/runtime/compaction-config.mjs +388 -388
- package/preset/odai/runtime/context-activation.mjs +71 -0
- package/preset/odai/runtime/context-capability.mjs +86 -0
- package/preset/odai/runtime/governance.mjs +128 -121
- package/preset/odai/runtime/human-care.mjs +45 -0
- package/preset/odai/runtime/human-safety-continuity.mjs +2 -2
- package/preset/odai/runtime/human-safety.mjs +3 -3
- package/preset/odai/runtime/index.mjs +2461 -1977
- package/preset/odai/runtime/output-config.mjs +360 -304
- package/preset/odai/runtime/research-packet.mjs +150 -150
- package/preset/odai/runtime/responsibility-gap.mjs +8 -10
- package/preset/odai/runtime/responsibility-scope.mjs +199 -0
- package/preset/odai/runtime/role-overlays.mjs +27 -27
- package/preset/odai/runtime/route-card.mjs +168 -152
- package/preset/odai/runtime/router.mjs +683 -651
- package/preset/odai/runtime/routing-config.mjs +480 -436
- package/preset/odai/runtime/routing-context.mjs +321 -321
- package/preset/odai/runtime/semantic-memory-store.mjs +452 -452
- package/preset/odai/runtime/semantic-memory.mjs +842 -842
- package/preset/odai/runtime/session-compat.mjs +389 -389
- package/preset/odai/runtime/skill-bundle.mjs +318 -318
- package/preset/odai/runtime/skill-evolution.mjs +1413 -1413
- package/preset/odai/skills/odai/SKILL.md +69 -68
- package/preset/odai/skills/odai/agents/openai.yaml +4 -4
- package/preset/odai/skills/odai/assets/claude-agents/agent.md +11 -11
- package/preset/odai/skills/odai/assets/codex-agents/config.toml +11 -11
- package/preset/odai/skills/odai/assets/codex-agents/role.toml +5 -5
- package/preset/odai/skills/odai/assets/copilot-agents/agent.md +13 -13
- package/preset/odai/skills/odai/assets/hooks-policy.example.json +24 -24
- package/preset/odai/skills/odai/assets/routing-roles/controller.md +15 -15
- package/preset/odai/skills/odai/assets/routing-roles/executor.md +11 -11
- package/preset/odai/skills/odai/assets/routing-roles/frontend.md +7 -7
- package/preset/odai/skills/odai/assets/routing-roles/planner.md +29 -29
- package/preset/odai/skills/odai/assets/routing-roles/researcher.md +6 -6
- package/preset/odai/skills/odai/assets/routing-roles/reviewer.md +11 -11
- package/preset/odai/skills/odai/assets/task-state.md +14 -14
- package/preset/odai/skills/odai/manifest.json +37 -36
- package/preset/odai/skills/odai/references/care.md +26 -0
- package/preset/odai/skills/odai/references/craft.md +54 -54
- package/preset/odai/skills/odai/references/dao.md +30 -30
- package/preset/odai/skills/odai/references/human-safety.md +33 -54
- package/preset/odai/skills/odai/references/leverage.md +53 -53
- package/preset/odai/skills/odai/references/planning.md +82 -82
- package/preset/odai/skills/odai/references/support.md +36 -36
- package/preset/odai/skills/odai/references/verification.md +23 -23
- package/preset/odai/skills/odai/scripts/build-hooks.mjs +273 -273
- package/preset/odai/skills/odai/scripts/build-routing.mjs +274 -274
- package/preset/odai/skills/odai/scripts/install-routing.mjs +397 -397
- package/preset/odai/skills/odai/scripts/odai-hook.mjs +457 -457
- package/preset/odai/skills/odai/scripts/run-role.mjs +336 -336
- package/preset/odai/skills/odai/scripts/run-routing.mjs +1154 -1154
- package/preset/odai/skills/odai/scripts/verify-routing.mjs +103 -103
- package/scripts/verify-agent-load.mjs +257 -257
- package/src/dsh-version.mjs +6 -2
- package/src/installer.mjs +382 -382
|
@@ -1,304 +1,360 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
-
mkdirSync,
|
|
4
|
-
readFileSync,
|
|
5
|
-
renameSync,
|
|
6
|
-
rmSync,
|
|
7
|
-
writeFileSync,
|
|
8
|
-
} from "node:fs";
|
|
9
|
-
import { homedir } from "node:os";
|
|
10
|
-
import { dirname, resolve } from "node:path";
|
|
11
|
-
|
|
12
|
-
import { acquireOwnedStoreLock } from "./store-lock.mjs";
|
|
13
|
-
|
|
14
|
-
const STORE_SCHEMA_VERSION = 1;
|
|
15
|
-
const POLICY_FIELDS = new Set(["concise", "maxTokens"]);
|
|
16
|
-
const OUTPUT_MODES = new Set(["normal", "concise", "economy"]);
|
|
17
|
-
const DEFAULT_ECONOMY_MAX_TOKENS = 500;
|
|
18
|
-
export const DEFAULT_OUTPUT_POLICY = Object.freeze({ concise: true });
|
|
19
|
-
|
|
20
|
-
export class OutputPolicyStoreValidationError extends Error {}
|
|
21
|
-
|
|
22
|
-
function assertPlainObject(value, field) {
|
|
23
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
24
|
-
throw new TypeError(`${field} must be an object`);
|
|
25
|
-
}
|
|
26
|
-
return value;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function resolveOutputPolicy(value, field = "Odai output policy") {
|
|
30
|
-
const policy = assertPlainObject(value, field);
|
|
31
|
-
const unknown = Object.keys(policy).filter((name) => !POLICY_FIELDS.has(name));
|
|
32
|
-
if (unknown.length > 0) throw new TypeError(`${field} has unknown fields: ${unknown.join(", ")}`);
|
|
33
|
-
if (!Object.hasOwn(policy, "concise") || typeof policy.concise !== "boolean") {
|
|
34
|
-
throw new TypeError(`${field}.concise must be an own boolean property`);
|
|
35
|
-
}
|
|
36
|
-
if (policy.maxTokens !== undefined
|
|
37
|
-
&& (!Number.isSafeInteger(policy.maxTokens) || policy.maxTokens <= 0)) {
|
|
38
|
-
throw new TypeError(`${field}.maxTokens must be a positive integer`);
|
|
39
|
-
}
|
|
40
|
-
return Object.freeze({
|
|
41
|
-
concise: policy.concise,
|
|
42
|
-
...(policy.maxTokens === undefined ? {} : { maxTokens: policy.maxTokens }),
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function resolveOutputConfigPath(configuredPath, env = process.env) {
|
|
47
|
-
if (configuredPath !== undefined) {
|
|
48
|
-
if (typeof configuredPath !== "string" || configuredPath.trim() === "") {
|
|
49
|
-
throw new TypeError("config.output.configPath must be a non-empty string");
|
|
50
|
-
}
|
|
51
|
-
return resolve(configuredPath.trim());
|
|
52
|
-
}
|
|
53
|
-
const dshHome = typeof env.DSH_HOME === "string" && env.DSH_HOME.trim() !== ""
|
|
54
|
-
? resolve(env.DSH_HOME.trim())
|
|
55
|
-
: resolve(homedir(), ".dsh");
|
|
56
|
-
return resolve(dshHome, "odai", "output.json");
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export function readOutputPolicyStore(configPath) {
|
|
60
|
-
let text;
|
|
61
|
-
try {
|
|
62
|
-
text = readFileSync(configPath, "utf8");
|
|
63
|
-
} catch (error) {
|
|
64
|
-
if (error?.code === "ENOENT") return Object.freeze({ schemaVersion: STORE_SCHEMA_VERSION });
|
|
65
|
-
throw new Error(`cannot read Odai output configuration ${configPath}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
66
|
-
}
|
|
67
|
-
let parsed;
|
|
68
|
-
try {
|
|
69
|
-
parsed = JSON.parse(text);
|
|
70
|
-
} catch (error) {
|
|
71
|
-
throw new OutputPolicyStoreValidationError(`Odai output configuration ${configPath} is not valid JSON`, { cause: error });
|
|
72
|
-
}
|
|
73
|
-
try {
|
|
74
|
-
const store = assertPlainObject(parsed, `Odai output configuration ${configPath}`);
|
|
75
|
-
const unknown = Object.keys(store).filter((field) => !["schemaVersion", "policy"].includes(field));
|
|
76
|
-
if (unknown.length > 0) {
|
|
77
|
-
throw new TypeError(`Odai output configuration ${configPath} has unknown fields: ${unknown.join(", ")}`);
|
|
78
|
-
}
|
|
79
|
-
if (store.schemaVersion !== STORE_SCHEMA_VERSION) {
|
|
80
|
-
throw new TypeError(`Odai output configuration ${configPath} has unsupported schemaVersion ${String(store.schemaVersion)}`);
|
|
81
|
-
}
|
|
82
|
-
if (store.policy === undefined) return Object.freeze({ schemaVersion: STORE_SCHEMA_VERSION });
|
|
83
|
-
return Object.freeze({
|
|
84
|
-
schemaVersion: STORE_SCHEMA_VERSION,
|
|
85
|
-
policy: resolveOutputPolicy(store.policy, `Odai output configuration ${configPath}.policy`),
|
|
86
|
-
});
|
|
87
|
-
} catch (error) {
|
|
88
|
-
if (error instanceof OutputPolicyStoreValidationError) throw error;
|
|
89
|
-
throw new OutputPolicyStoreValidationError(
|
|
90
|
-
`Odai output configuration ${configPath} failed validation: ${error instanceof Error ? error.message : String(error)}`,
|
|
91
|
-
{ cause: error },
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function effectiveOutputPolicy(configPath) {
|
|
97
|
-
const stored = readOutputPolicyStore(configPath);
|
|
98
|
-
return Object.freeze({
|
|
99
|
-
policy: stored.policy ?? DEFAULT_OUTPUT_POLICY,
|
|
100
|
-
source: stored.policy ? "persisted" : "default",
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return
|
|
172
|
-
|
|
173
|
-
...(
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
type: "
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
mkdirSync,
|
|
4
|
+
readFileSync,
|
|
5
|
+
renameSync,
|
|
6
|
+
rmSync,
|
|
7
|
+
writeFileSync,
|
|
8
|
+
} from "node:fs";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import { dirname, resolve } from "node:path";
|
|
11
|
+
|
|
12
|
+
import { acquireOwnedStoreLock } from "./store-lock.mjs";
|
|
13
|
+
|
|
14
|
+
const STORE_SCHEMA_VERSION = 1;
|
|
15
|
+
const POLICY_FIELDS = new Set(["concise", "maxTokens"]);
|
|
16
|
+
const OUTPUT_MODES = new Set(["normal", "concise", "economy"]);
|
|
17
|
+
const DEFAULT_ECONOMY_MAX_TOKENS = 500;
|
|
18
|
+
export const DEFAULT_OUTPUT_POLICY = Object.freeze({ concise: true });
|
|
19
|
+
|
|
20
|
+
export class OutputPolicyStoreValidationError extends Error {}
|
|
21
|
+
|
|
22
|
+
function assertPlainObject(value, field) {
|
|
23
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
24
|
+
throw new TypeError(`${field} must be an object`);
|
|
25
|
+
}
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function resolveOutputPolicy(value, field = "Odai output policy") {
|
|
30
|
+
const policy = assertPlainObject(value, field);
|
|
31
|
+
const unknown = Object.keys(policy).filter((name) => !POLICY_FIELDS.has(name));
|
|
32
|
+
if (unknown.length > 0) throw new TypeError(`${field} has unknown fields: ${unknown.join(", ")}`);
|
|
33
|
+
if (!Object.hasOwn(policy, "concise") || typeof policy.concise !== "boolean") {
|
|
34
|
+
throw new TypeError(`${field}.concise must be an own boolean property`);
|
|
35
|
+
}
|
|
36
|
+
if (policy.maxTokens !== undefined
|
|
37
|
+
&& (!Number.isSafeInteger(policy.maxTokens) || policy.maxTokens <= 0)) {
|
|
38
|
+
throw new TypeError(`${field}.maxTokens must be a positive integer`);
|
|
39
|
+
}
|
|
40
|
+
return Object.freeze({
|
|
41
|
+
concise: policy.concise,
|
|
42
|
+
...(policy.maxTokens === undefined ? {} : { maxTokens: policy.maxTokens }),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function resolveOutputConfigPath(configuredPath, env = process.env) {
|
|
47
|
+
if (configuredPath !== undefined) {
|
|
48
|
+
if (typeof configuredPath !== "string" || configuredPath.trim() === "") {
|
|
49
|
+
throw new TypeError("config.output.configPath must be a non-empty string");
|
|
50
|
+
}
|
|
51
|
+
return resolve(configuredPath.trim());
|
|
52
|
+
}
|
|
53
|
+
const dshHome = typeof env.DSH_HOME === "string" && env.DSH_HOME.trim() !== ""
|
|
54
|
+
? resolve(env.DSH_HOME.trim())
|
|
55
|
+
: resolve(homedir(), ".dsh");
|
|
56
|
+
return resolve(dshHome, "odai", "output.json");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function readOutputPolicyStore(configPath) {
|
|
60
|
+
let text;
|
|
61
|
+
try {
|
|
62
|
+
text = readFileSync(configPath, "utf8");
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if (error?.code === "ENOENT") return Object.freeze({ schemaVersion: STORE_SCHEMA_VERSION });
|
|
65
|
+
throw new Error(`cannot read Odai output configuration ${configPath}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
66
|
+
}
|
|
67
|
+
let parsed;
|
|
68
|
+
try {
|
|
69
|
+
parsed = JSON.parse(text);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw new OutputPolicyStoreValidationError(`Odai output configuration ${configPath} is not valid JSON`, { cause: error });
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const store = assertPlainObject(parsed, `Odai output configuration ${configPath}`);
|
|
75
|
+
const unknown = Object.keys(store).filter((field) => !["schemaVersion", "policy"].includes(field));
|
|
76
|
+
if (unknown.length > 0) {
|
|
77
|
+
throw new TypeError(`Odai output configuration ${configPath} has unknown fields: ${unknown.join(", ")}`);
|
|
78
|
+
}
|
|
79
|
+
if (store.schemaVersion !== STORE_SCHEMA_VERSION) {
|
|
80
|
+
throw new TypeError(`Odai output configuration ${configPath} has unsupported schemaVersion ${String(store.schemaVersion)}`);
|
|
81
|
+
}
|
|
82
|
+
if (store.policy === undefined) return Object.freeze({ schemaVersion: STORE_SCHEMA_VERSION });
|
|
83
|
+
return Object.freeze({
|
|
84
|
+
schemaVersion: STORE_SCHEMA_VERSION,
|
|
85
|
+
policy: resolveOutputPolicy(store.policy, `Odai output configuration ${configPath}.policy`),
|
|
86
|
+
});
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (error instanceof OutputPolicyStoreValidationError) throw error;
|
|
89
|
+
throw new OutputPolicyStoreValidationError(
|
|
90
|
+
`Odai output configuration ${configPath} failed validation: ${error instanceof Error ? error.message : String(error)}`,
|
|
91
|
+
{ cause: error },
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function effectiveOutputPolicy(configPath) {
|
|
97
|
+
const stored = readOutputPolicyStore(configPath);
|
|
98
|
+
return Object.freeze({
|
|
99
|
+
policy: stored.policy ?? DEFAULT_OUTPUT_POLICY,
|
|
100
|
+
source: stored.policy ? "persisted" : "default",
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const IN_PLACE_OUTPUT_RESPONSIBILITIES = Object.freeze(["planner", "executor", "frontend"]);
|
|
105
|
+
|
|
106
|
+
function resolveResponsibilityOutputBudget(policy, route) {
|
|
107
|
+
if (route.maxTokens !== undefined) {
|
|
108
|
+
return Object.freeze({
|
|
109
|
+
source: "responsibility-override",
|
|
110
|
+
maxTokens: route.maxTokens,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (policy?.maxTokens !== undefined) {
|
|
114
|
+
return Object.freeze({
|
|
115
|
+
source: "controller-policy",
|
|
116
|
+
maxTokens: policy.maxTokens,
|
|
117
|
+
warning: "responsibility-inherits-controller-ceiling",
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return Object.freeze({ source: "unbounded-by-odai" });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function resolveInPlaceResponsibilityOutputBudgets(policy, routes) {
|
|
124
|
+
if (!routes || typeof routes !== "object") return undefined;
|
|
125
|
+
const budgets = Object.fromEntries(IN_PLACE_OUTPUT_RESPONSIBILITIES.flatMap((responsibility) => {
|
|
126
|
+
const route = routes[responsibility];
|
|
127
|
+
return route && typeof route === "object"
|
|
128
|
+
? [[responsibility, resolveResponsibilityOutputBudget(policy, route)]]
|
|
129
|
+
: [];
|
|
130
|
+
}));
|
|
131
|
+
return Object.keys(budgets).length > 0 ? Object.freeze(budgets) : undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function writeOutputPolicyStore(configPath, policy) {
|
|
135
|
+
mkdirSync(dirname(configPath), { recursive: true, mode: 0o700 });
|
|
136
|
+
const temporary = `${configPath}.tmp-${process.pid}-${randomUUID()}`;
|
|
137
|
+
const value = `${JSON.stringify({ schemaVersion: STORE_SCHEMA_VERSION, policy }, null, 2)}\n`;
|
|
138
|
+
try {
|
|
139
|
+
writeFileSync(temporary, value, { encoding: "utf8", mode: 0o600, flag: "wx" });
|
|
140
|
+
renameSync(temporary, configPath);
|
|
141
|
+
} finally {
|
|
142
|
+
rmSync(temporary, { force: true });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function preserveInvalidStore(configPath) {
|
|
147
|
+
try {
|
|
148
|
+
renameSync(configPath, `${configPath}.invalid-${Date.now()}-${randomUUID()}`);
|
|
149
|
+
return true;
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (error?.code === "ENOENT") return false;
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function resultFor(configPath, action, selection, recoveredInvalidStore = false, responsibilityRoutes) {
|
|
157
|
+
const responsibilityBudgets = resolveInPlaceResponsibilityOutputBudgets(selection.policy, responsibilityRoutes);
|
|
158
|
+
return {
|
|
159
|
+
action,
|
|
160
|
+
configPath,
|
|
161
|
+
policy: selection.policy,
|
|
162
|
+
source: selection.source,
|
|
163
|
+
requiresNextTurn: action !== "show",
|
|
164
|
+
...(responsibilityBudgets ? { responsibilityBudgets } : {}),
|
|
165
|
+
...(recoveredInvalidStore ? { recoveredInvalidStore: true } : {}),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function renderOutputPolicyPrompt(policy) {
|
|
170
|
+
if (!policy.concise && policy.maxTokens === undefined) return "";
|
|
171
|
+
return [
|
|
172
|
+
"## Odai controller output policy",
|
|
173
|
+
...(policy.concise ? [
|
|
174
|
+
"Keep the final user-facing response concise. Include only the result, decisive evidence, unresolved items, and necessary next action; omit routine process narration and repeated context unless the user explicitly asks for detail.",
|
|
175
|
+
] : []),
|
|
176
|
+
...(policy.maxTokens === undefined ? [] : [
|
|
177
|
+
`Each controller model request carries a provider output ceiling request of ${policy.maxTokens} tokens, which may include reasoning. Provider enforcement is not guaranteed; prioritize completion and finish before the requested ceiling.`,
|
|
178
|
+
]),
|
|
179
|
+
"This policy applies only to controller requests and the final user-facing response. It never reduces child-agent, compaction, checkpoint, or other internal context budgets. A user-configured in-place responsibility maxTokens explicitly overrides this ceiling only inside that routed planner, executor, or frontend scope; the runtime records that exception.",
|
|
180
|
+
"The policy changes presentation and the requested controller budget only; it never permits omitting required results, evidence, risks, blockers, or verification.",
|
|
181
|
+
].join("\n");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function resolveSetPolicy(args) {
|
|
185
|
+
if (args.mode === undefined) {
|
|
186
|
+
if (args.concise === false && args.maxTokens !== undefined) {
|
|
187
|
+
throw new TypeError("maxTokens requires concise=true or mode=economy");
|
|
188
|
+
}
|
|
189
|
+
return resolveOutputPolicy({
|
|
190
|
+
concise: args.concise,
|
|
191
|
+
...(args.maxTokens === undefined ? {} : { maxTokens: args.maxTokens }),
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (!OUTPUT_MODES.has(args.mode)) {
|
|
195
|
+
throw new TypeError("mode must be normal, concise, or economy");
|
|
196
|
+
}
|
|
197
|
+
if (args.concise !== undefined) {
|
|
198
|
+
throw new TypeError("concise must be omitted when mode is supplied");
|
|
199
|
+
}
|
|
200
|
+
if (args.mode !== "economy" && args.maxTokens !== undefined) {
|
|
201
|
+
throw new TypeError("maxTokens is accepted only with economy mode");
|
|
202
|
+
}
|
|
203
|
+
return resolveOutputPolicy({
|
|
204
|
+
concise: args.mode !== "normal",
|
|
205
|
+
...(args.mode === "economy"
|
|
206
|
+
? { maxTokens: args.maxTokens ?? DEFAULT_ECONOMY_MAX_TOKENS }
|
|
207
|
+
: {}),
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function createOutputConfigTool(configPath, options = {}) {
|
|
212
|
+
const onConfigured = typeof options.onConfigured === "function" ? options.onConfigured : () => {};
|
|
213
|
+
const responsibilityRoutesFor = typeof options.responsibilityRoutesFor === "function" ? options.responsibilityRoutesFor : () => undefined;
|
|
214
|
+
const isChild = typeof options.isChild === "function"
|
|
215
|
+
? options.isChild
|
|
216
|
+
: (agent) => {
|
|
217
|
+
const header = agent?.session?.header;
|
|
218
|
+
return header?.origin === "subagent"
|
|
219
|
+
|| (Number.isSafeInteger(header?.delegationDepth) && header.delegationDepth > 0);
|
|
220
|
+
};
|
|
221
|
+
return {
|
|
222
|
+
name: "odai_output_config",
|
|
223
|
+
description: [
|
|
224
|
+
"Inspect, set, or remove the shared Odai controller output policy; the package default is soft concise.",
|
|
225
|
+
"Normal mode is concise=false with no maxTokens; soft concise is concise=true with no maxTokens; economy mode is concise=true with a user-adjustable maxTokens request ceiling and defaults to 500 only when the user names economy without another value.",
|
|
226
|
+
"Use only when the user explicitly requests an output mode or supplies a custom maxTokens; never invent a non-default custom value. Remove restores soft concise.",
|
|
227
|
+
"Changes start next user turn. maxTokens is a provider request ceiling, not locally enforced; providers may include reasoning, exceed or ignore it, or truncate responses. Child-agent, compaction, checkpoint, and other internal budgets stay unchanged.",
|
|
228
|
+
].join(" "),
|
|
229
|
+
parameters: {
|
|
230
|
+
type: "object",
|
|
231
|
+
additionalProperties: false,
|
|
232
|
+
required: ["action"],
|
|
233
|
+
properties: {
|
|
234
|
+
action: {
|
|
235
|
+
type: "string",
|
|
236
|
+
enum: ["show", "set", "remove"],
|
|
237
|
+
},
|
|
238
|
+
mode: {
|
|
239
|
+
type: "string",
|
|
240
|
+
enum: ["normal", "concise", "economy"],
|
|
241
|
+
description: "Preferred for set; economy defaults maxTokens to 500 unless the user supplies another positive value.",
|
|
242
|
+
},
|
|
243
|
+
concise: {
|
|
244
|
+
type: "boolean",
|
|
245
|
+
description: "Backward-compatible alternative for set; omit when mode is supplied.",
|
|
246
|
+
},
|
|
247
|
+
maxTokens: {
|
|
248
|
+
type: "integer",
|
|
249
|
+
description: "Optional positive replacement for economy mode's default 500-token provider request ceiling.",
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
output: {
|
|
254
|
+
schema: {
|
|
255
|
+
type: "object",
|
|
256
|
+
additionalProperties: false,
|
|
257
|
+
required: ["action", "configPath", "policy", "source", "requiresNextTurn"],
|
|
258
|
+
properties: {
|
|
259
|
+
action: { type: "string", enum: ["show", "set", "remove"] },
|
|
260
|
+
configPath: { type: "string" },
|
|
261
|
+
policy: {
|
|
262
|
+
type: "object",
|
|
263
|
+
additionalProperties: false,
|
|
264
|
+
required: ["concise"],
|
|
265
|
+
properties: {
|
|
266
|
+
concise: { type: "boolean" },
|
|
267
|
+
maxTokens: { type: "integer" },
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
source: { type: "string", enum: ["default", "persisted"] },
|
|
271
|
+
requiresNextTurn: { type: "boolean" },
|
|
272
|
+
recoveredInvalidStore: { type: "boolean" },
|
|
273
|
+
responsibilityBudgets: {
|
|
274
|
+
type: "object",
|
|
275
|
+
additionalProperties: false,
|
|
276
|
+
properties: Object.fromEntries(IN_PLACE_OUTPUT_RESPONSIBILITIES.map((responsibility) => [responsibility, {
|
|
277
|
+
type: "object",
|
|
278
|
+
additionalProperties: false,
|
|
279
|
+
required: ["source"],
|
|
280
|
+
properties: {
|
|
281
|
+
source: { type: "string", enum: ["responsibility-override", "controller-policy", "unbounded-by-odai"] },
|
|
282
|
+
maxTokens: { type: "integer" },
|
|
283
|
+
warning: { type: "string", enum: ["responsibility-inherits-controller-ceiling"] },
|
|
284
|
+
},
|
|
285
|
+
}])),
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
render(_args, value) {
|
|
290
|
+
const settings = [
|
|
291
|
+
`concise=${value.policy.concise ? "on" : "off"}`,
|
|
292
|
+
...(value.policy.maxTokens === undefined ? [] : [`maxTokens=${value.policy.maxTokens}`]),
|
|
293
|
+
].join(", ");
|
|
294
|
+
const responsibilityBudgets = Object.entries(value.responsibilityBudgets ?? {});
|
|
295
|
+
const configuredResponsibilities = responsibilityBudgets.map(([responsibility, budget]) => (
|
|
296
|
+
`${responsibility}=${budget.maxTokens === undefined ? "no Odai maxTokens" : `maxTokens=${budget.maxTokens}`} [${budget.source}]`
|
|
297
|
+
));
|
|
298
|
+
const inheritedWarnings = responsibilityBudgets
|
|
299
|
+
.filter(([, budget]) => budget.warning === "responsibility-inherits-controller-ceiling")
|
|
300
|
+
.map(([responsibility]) => ` Warning: ${responsibility} has no explicit maxTokens and inherits the controller ceiling when routed in-place; providers may count reasoning and truncate substantial work.`);
|
|
301
|
+
return [{
|
|
302
|
+
type: "text",
|
|
303
|
+
text: [
|
|
304
|
+
`${value.action === "show" ? "Current" : "Updated"} Odai controller output policy (${value.source}): ${settings}.`,
|
|
305
|
+
...(value.policy.maxTokens === undefined ? [] : [" maxTokens is sent as a provider request ceiling; strict provider compliance is not guaranteed and must be checked from usage."]),
|
|
306
|
+
...(configuredResponsibilities.length > 0 ? [` In-place responsibility ceilings: ${configuredResponsibilities.join("; ")}.`] : []),
|
|
307
|
+
...inheritedWarnings,
|
|
308
|
+
...(value.recoveredInvalidStore ? [" An invalid prior store was preserved and replaced."] : []),
|
|
309
|
+
...(value.requiresNextTurn ? [" The change applies from the next user turn."] : []),
|
|
310
|
+
].join(""),
|
|
311
|
+
}];
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
execute(args, execution) {
|
|
315
|
+
if (!execution.agent) throw new Error("odai_output_config requires an owning agent session");
|
|
316
|
+
if (isChild(execution.agent)) throw new Error("child agents may not change Odai output configuration");
|
|
317
|
+
if (!args || typeof args !== "object" || Array.isArray(args)) throw new TypeError("arguments must be an object");
|
|
318
|
+
const unknown = Object.keys(args).filter((field) => !["action", "mode", "concise", "maxTokens"].includes(field));
|
|
319
|
+
if (unknown.length > 0) throw new TypeError(`unknown arguments: ${unknown.join(", ")}`);
|
|
320
|
+
if (!["show", "set", "remove"].includes(args.action)) throw new TypeError("action must be show, set, or remove");
|
|
321
|
+
if (args.action === "show") {
|
|
322
|
+
if (args.mode !== undefined || args.concise !== undefined || args.maxTokens !== undefined) {
|
|
323
|
+
throw new TypeError("mode, concise, and maxTokens must be omitted for show");
|
|
324
|
+
}
|
|
325
|
+
return Promise.resolve(resultFor(configPath, "show", effectiveOutputPolicy(configPath), false, responsibilityRoutesFor()));
|
|
326
|
+
}
|
|
327
|
+
if (args.action === "remove"
|
|
328
|
+
&& (args.mode !== undefined || args.concise !== undefined || args.maxTokens !== undefined)) {
|
|
329
|
+
throw new TypeError("mode, concise, and maxTokens must be omitted for remove");
|
|
330
|
+
}
|
|
331
|
+
const proposed = args.action === "set" ? resolveSetPolicy(args) : undefined;
|
|
332
|
+
|
|
333
|
+
const releaseLock = acquireOwnedStoreLock(configPath, "Odai output configuration");
|
|
334
|
+
try {
|
|
335
|
+
let recoveredInvalidStore = false;
|
|
336
|
+
try {
|
|
337
|
+
readOutputPolicyStore(configPath);
|
|
338
|
+
} catch (error) {
|
|
339
|
+
if (!(error instanceof OutputPolicyStoreValidationError)) {
|
|
340
|
+
throw new Error("Odai output configuration could not be read safely; no changes were made", { cause: error });
|
|
341
|
+
}
|
|
342
|
+
recoveredInvalidStore = preserveInvalidStore(configPath);
|
|
343
|
+
}
|
|
344
|
+
if (args.action === "set") writeOutputPolicyStore(configPath, proposed);
|
|
345
|
+
else rmSync(configPath, { force: true });
|
|
346
|
+
const selection = args.action === "set"
|
|
347
|
+
? Object.freeze({ policy: proposed, source: "persisted" })
|
|
348
|
+
: Object.freeze({ policy: DEFAULT_OUTPUT_POLICY, source: "default" });
|
|
349
|
+
onConfigured(execution.agent, {
|
|
350
|
+
action: args.action,
|
|
351
|
+
policy: selection.policy,
|
|
352
|
+
...(recoveredInvalidStore ? { recoveredInvalidStore: true } : {}),
|
|
353
|
+
});
|
|
354
|
+
return Promise.resolve(resultFor(configPath, args.action, selection, recoveredInvalidStore, responsibilityRoutesFor()));
|
|
355
|
+
} finally {
|
|
356
|
+
releaseLock();
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
}
|