@suifei/dsh-ai-team 0.4.145 → 0.4.146

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/LICENSE +21 -21
  3. package/README.md +19 -1
  4. package/client.js +118 -17
  5. package/cordis.patch.yml +31 -31
  6. package/icon.svg +12 -12
  7. package/index.js +7 -1
  8. package/lib/domain/ids.js +26 -26
  9. package/lib/domain/inbox.js +93 -93
  10. package/lib/domain/member-config.js +168 -168
  11. package/lib/host/decision-report.js +72 -72
  12. package/lib/host/default-workspace.js +69 -69
  13. package/lib/host/fault-adapter.js +50 -50
  14. package/lib/host/people-view.js +9 -2
  15. package/lib/host/permission-guard.js +61 -61
  16. package/lib/host/project-registry.js +104 -104
  17. package/lib/host/risk-view.js +99 -99
  18. package/lib/host/weekly-view.js +86 -86
  19. package/lib/host/workspace-path.js +94 -94
  20. package/lib/ledger/checkpoint.js +415 -415
  21. package/lib/ledger/events.js +398 -398
  22. package/lib/ledger/namespace.js +124 -124
  23. package/lib/ledger/tools.js +118 -118
  24. package/lib/ledger/writer.js +1266 -1266
  25. package/lib/runtime/error-chain.js +31 -31
  26. package/lib/runtime/shell-guard.js +102 -102
  27. package/lib/runtime/skill-catalog.js +41 -41
  28. package/lib/runtime/skill-policy.js +12 -12
  29. package/lib/skillopt/adopt-gate.js +108 -108
  30. package/lib/skillopt/gate-wiring.js +146 -146
  31. package/lib/skillopt/jobs.js +135 -135
  32. package/lib/skillopt/proposal-store.js +33 -33
  33. package/lib/skillopt/review-runner.js +107 -107
  34. package/lib/skillopt/rubric.js +189 -189
  35. package/lib/skillopt/subagent-text.js +101 -101
  36. package/lib/skillopt/tool-access.js +56 -56
  37. package/locale/en.json +6 -6
  38. package/locale/zh.json +6 -6
  39. package/package.json +1 -1
@@ -1,93 +1,93 @@
1
- import { ERRORS, fail } from './errors.js';
2
- import { ids } from './ids.js';
3
-
4
- /** The 12 closed inbox types from 02 FR-028. */
5
- export const INBOX_TYPES = new Set([
6
- 'mention',
7
- 'assigned',
8
- 'handoff',
9
- 'review_requested',
10
- 'review_result',
11
- 'task_blocked',
12
- 'dependency_cleared',
13
- 'member_stalled',
14
- 'project_recovered',
15
- 'followed_task_update',
16
- 'decision_required',
17
- 'stale_reply_rejected',
18
- ]);
19
-
20
- export const DECISION_TYPES = new Set(['decision_required']);
21
-
22
- /** Progress reports mentioning Human are not actionable decisions. */
23
- export function isInformationalThreadMention(item) {
24
- return item?.ref?.category === 'thread_mention'
25
- && !(Array.isArray(item.ref?.questions) && item.ref.questions.length > 0);
26
- }
27
-
28
- /**
29
- * Create one inbox item. It points at a source object; it is not a copy of the
30
- * message body (the reader pulls the source on demand).
31
- */
32
- export function createInboxItem(input, options = {}) {
33
- if (typeof input.memberId !== 'string' || input.memberId === '') fail(ERRORS.INVALID_ARGUMENT, 'memberId is required');
34
- if (!INBOX_TYPES.has(input.type)) fail(ERRORS.INVALID_ARGUMENT, `unknown inbox type: ${String(input.type)}`);
35
- if (input.ref === undefined || input.ref === null) fail(ERRORS.INVALID_ARGUMENT, 'ref is required');
36
- const requiresDecision = input.requiresDecision ?? DECISION_TYPES.has(input.type);
37
- return {
38
- id: input.id ?? ids.inbox(),
39
- memberId: input.memberId,
40
- type: input.type,
41
- ref: input.ref,
42
- requiresDecision,
43
- state: 'unread',
44
- createdAt: options.at ?? Date.now(),
45
- ackedAt: null,
46
- disposition: null,
47
- };
48
- }
49
-
50
- /**
51
- * Acknowledge one item. Decision items may be marked read but cannot be marked
52
- * done without an explicit decision; they can never be bulk-acked.
53
- */
54
- export function ackInboxItem(item, input, options = {}) {
55
- if (item.state === 'resolved' || item.state === 'dismissed' || item.state === 'superseded') return item;
56
- const disposition = input.disposition ?? 'read';
57
- if (disposition !== 'read' && disposition !== 'done' && disposition !== 'dismiss') {
58
- fail(ERRORS.INVALID_ARGUMENT, 'disposition must be read, done or dismiss');
59
- }
60
- if (item.requiresDecision && disposition === 'done' && input.decision === undefined) {
61
- fail(ERRORS.INVALID_STATE, 'a decision item requires an explicit decision before it can be done', {
62
- details: { itemId: item.id },
63
- });
64
- }
65
- return {
66
- ...item,
67
- state: disposition === 'done' ? 'resolved' : disposition === 'dismiss' ? 'dismissed' : 'read',
68
- disposition,
69
- decision: input.decision ?? null,
70
- ackedAt: options.at ?? Date.now(),
71
- };
72
- }
73
-
74
- /** Bulk read only touches non-decision items; decision items stay unread. */
75
- export function markAllRead(items, options = {}) {
76
- let changed = 0;
77
- const next = items.map((item) => {
78
- if (item.state !== 'unread' || item.requiresDecision) return item;
79
- changed += 1;
80
- return { ...item, state: 'read', disposition: 'read', ackedAt: options.at ?? Date.now() };
81
- });
82
- return { items: next, changed };
83
- }
84
-
85
- /** Five fixed Human-inbox groups, in product order. */
86
- export function inboxGroup(item) {
87
- if (item.requiresDecision) return 'needs_decision';
88
- if (item.type === 'review_requested') return 'needs_review';
89
- if (item.type === 'mention') return 'mentions';
90
- if (item.type === 'followed_task_update') return 'following';
91
- if (item.type === 'member_stalled' || item.type === 'project_recovered' || item.type === 'task_blocked') return 'failures';
92
- return 'other';
93
- }
1
+ import { ERRORS, fail } from './errors.js';
2
+ import { ids } from './ids.js';
3
+
4
+ /** The 12 closed inbox types from 02 FR-028. */
5
+ export const INBOX_TYPES = new Set([
6
+ 'mention',
7
+ 'assigned',
8
+ 'handoff',
9
+ 'review_requested',
10
+ 'review_result',
11
+ 'task_blocked',
12
+ 'dependency_cleared',
13
+ 'member_stalled',
14
+ 'project_recovered',
15
+ 'followed_task_update',
16
+ 'decision_required',
17
+ 'stale_reply_rejected',
18
+ ]);
19
+
20
+ export const DECISION_TYPES = new Set(['decision_required']);
21
+
22
+ /** Progress reports mentioning Human are not actionable decisions. */
23
+ export function isInformationalThreadMention(item) {
24
+ return item?.ref?.category === 'thread_mention'
25
+ && !(Array.isArray(item.ref?.questions) && item.ref.questions.length > 0);
26
+ }
27
+
28
+ /**
29
+ * Create one inbox item. It points at a source object; it is not a copy of the
30
+ * message body (the reader pulls the source on demand).
31
+ */
32
+ export function createInboxItem(input, options = {}) {
33
+ if (typeof input.memberId !== 'string' || input.memberId === '') fail(ERRORS.INVALID_ARGUMENT, 'memberId is required');
34
+ if (!INBOX_TYPES.has(input.type)) fail(ERRORS.INVALID_ARGUMENT, `unknown inbox type: ${String(input.type)}`);
35
+ if (input.ref === undefined || input.ref === null) fail(ERRORS.INVALID_ARGUMENT, 'ref is required');
36
+ const requiresDecision = input.requiresDecision ?? DECISION_TYPES.has(input.type);
37
+ return {
38
+ id: input.id ?? ids.inbox(),
39
+ memberId: input.memberId,
40
+ type: input.type,
41
+ ref: input.ref,
42
+ requiresDecision,
43
+ state: 'unread',
44
+ createdAt: options.at ?? Date.now(),
45
+ ackedAt: null,
46
+ disposition: null,
47
+ };
48
+ }
49
+
50
+ /**
51
+ * Acknowledge one item. Decision items may be marked read but cannot be marked
52
+ * done without an explicit decision; they can never be bulk-acked.
53
+ */
54
+ export function ackInboxItem(item, input, options = {}) {
55
+ if (item.state === 'resolved' || item.state === 'dismissed' || item.state === 'superseded') return item;
56
+ const disposition = input.disposition ?? 'read';
57
+ if (disposition !== 'read' && disposition !== 'done' && disposition !== 'dismiss') {
58
+ fail(ERRORS.INVALID_ARGUMENT, 'disposition must be read, done or dismiss');
59
+ }
60
+ if (item.requiresDecision && disposition === 'done' && input.decision === undefined) {
61
+ fail(ERRORS.INVALID_STATE, 'a decision item requires an explicit decision before it can be done', {
62
+ details: { itemId: item.id },
63
+ });
64
+ }
65
+ return {
66
+ ...item,
67
+ state: disposition === 'done' ? 'resolved' : disposition === 'dismiss' ? 'dismissed' : 'read',
68
+ disposition,
69
+ decision: input.decision ?? null,
70
+ ackedAt: options.at ?? Date.now(),
71
+ };
72
+ }
73
+
74
+ /** Bulk read only touches non-decision items; decision items stay unread. */
75
+ export function markAllRead(items, options = {}) {
76
+ let changed = 0;
77
+ const next = items.map((item) => {
78
+ if (item.state !== 'unread' || item.requiresDecision) return item;
79
+ changed += 1;
80
+ return { ...item, state: 'read', disposition: 'read', ackedAt: options.at ?? Date.now() };
81
+ });
82
+ return { items: next, changed };
83
+ }
84
+
85
+ /** Five fixed Human-inbox groups, in product order. */
86
+ export function inboxGroup(item) {
87
+ if (item.requiresDecision) return 'needs_decision';
88
+ if (item.type === 'review_requested') return 'needs_review';
89
+ if (item.type === 'mention') return 'mentions';
90
+ if (item.type === 'followed_task_update') return 'following';
91
+ if (item.type === 'member_stalled' || item.type === 'project_recovered' || item.type === 'task_blocked') return 'failures';
92
+ return 'other';
93
+ }
@@ -1,168 +1,168 @@
1
- import { ERRORS, fail } from './errors.js';
2
-
3
- /**
4
- * Member model/context configuration. The Project owner (Agent Lead) may
5
- * update it within the Team policy; Human may always update it. Config is
6
- * versioned and takes effect at the next wake, never mid-turn.
7
- */
8
- export const MEMBER_CONFIG_VERSION = 1;
9
-
10
- export const MEMBER_PRESETS = Object.freeze(['standard', 'ptc', 'minimal', 'cordis']);
11
-
12
- export const DEFAULT_MEMBER_CONFIG = Object.freeze({
13
- preset: 'standard',
14
- provider: '',
15
- model: '',
16
- reasoningEffort: null,
17
- maxTokens: null,
18
- contextWindow: null,
19
- compactionThreshold: 0.8,
20
- fallback: [],
21
- retry: { maxAttempts: 3 },
22
- memoryBudgetTokens: 4096,
23
- skillBudgetTokens: 2048,
24
- notesBudgetTokens: 2048,
25
- });
26
-
27
- export const CONFIG_FIELDS = Object.freeze([
28
- 'preset',
29
- 'provider', 'model', 'reasoningEffort', 'maxTokens', 'contextWindow',
30
- 'compactionThreshold', 'fallback', 'retry',
31
- 'memoryBudgetTokens', 'skillBudgetTokens', 'notesBudgetTokens',
32
- ]);
33
-
34
- function isInt(value) {
35
- return Number.isSafeInteger(value);
36
- }
37
-
38
- function positiveInt(value, name, max) {
39
- if (!isInt(value) || value < 1 || value > max) fail(ERRORS.INVALID_ARGUMENT, `${name} must be an integer in 1..${max}`);
40
- }
41
-
42
- function textOrNull(value) {
43
- return typeof value === 'string' && value !== '' ? value : null;
44
- }
45
-
46
- function normalizeFallback(value) {
47
- if (!Array.isArray(value)) fail(ERRORS.INVALID_ARGUMENT, 'fallback must be an array');
48
- if (value.length > 3) fail(ERRORS.INVALID_ARGUMENT, 'fallback supports at most 3 entries');
49
- return value.map((entry, index) => {
50
- if (entry === null || typeof entry !== 'object') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}] must be an object`);
51
- if (typeof entry.provider !== 'string' || entry.provider === '') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}].provider is required`);
52
- if (typeof entry.model !== 'string' || entry.model === '') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}].model is required`);
53
- if (entry.maxTokens !== undefined) positiveInt(entry.maxTokens, `fallback[${index}].maxTokens`, 1_000_000);
54
- return {
55
- provider: entry.provider,
56
- model: entry.model,
57
- ...(textOrNull(entry.reasoningEffort) === null ? {} : { reasoningEffort: entry.reasoningEffort }),
58
- ...(entry.maxTokens === undefined ? {} : { maxTokens: entry.maxTokens }),
59
- };
60
- });
61
- }
62
-
63
- /** Merge + validate one complete config; throws DomainError on invalid fields. */
64
- export function normalizeMemberConfig(input = {}, base = DEFAULT_MEMBER_CONFIG) {
65
- const value = input ?? {};
66
- const preset = value.preset === undefined ? base.preset : value.preset;
67
- if (typeof preset !== 'string' || !MEMBER_PRESETS.includes(preset)) {
68
- fail(ERRORS.INVALID_ARGUMENT, `preset must be one of ${MEMBER_PRESETS.join(', ')}`);
69
- }
70
- const provider = value.provider === undefined ? base.provider : value.provider;
71
- const model = value.model === undefined ? base.model : value.model;
72
- if (typeof provider !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'provider must be a string');
73
- if (typeof model !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'model must be a string');
74
- const reasoningEffort = value.reasoningEffort === undefined ? base.reasoningEffort : textOrNull(value.reasoningEffort);
75
- if (reasoningEffort !== null && typeof reasoningEffort !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'reasoningEffort must be a string or null');
76
- const maxTokens = value.maxTokens === undefined ? base.maxTokens : value.maxTokens;
77
- if (maxTokens !== null) positiveInt(maxTokens, 'maxTokens', 1_000_000);
78
- const contextWindow = value.contextWindow === undefined ? base.contextWindow : value.contextWindow;
79
- if (contextWindow !== null) {
80
- if (!isInt(contextWindow) || contextWindow < 4_096 || contextWindow > 2_000_000) {
81
- fail(ERRORS.INVALID_ARGUMENT, 'contextWindow must be an integer in 4096..2000000 or null');
82
- }
83
- }
84
- const compactionThreshold = value.compactionThreshold === undefined ? base.compactionThreshold : value.compactionThreshold;
85
- if (typeof compactionThreshold !== 'number' || !Number.isFinite(compactionThreshold) || compactionThreshold < 0.5 || compactionThreshold > 0.95) {
86
- fail(ERRORS.INVALID_ARGUMENT, 'compactionThreshold must be in 0.5..0.95');
87
- }
88
- const fallback = value.fallback === undefined ? normalizeFallback(base.fallback) : normalizeFallback(value.fallback);
89
- const retryInput = value.retry === undefined ? base.retry : value.retry;
90
- if (retryInput === null || typeof retryInput !== 'object') fail(ERRORS.INVALID_ARGUMENT, 'retry must be an object');
91
- const maxAttempts = retryInput.maxAttempts === undefined ? base.retry.maxAttempts : retryInput.maxAttempts;
92
- positiveInt(maxAttempts, 'retry.maxAttempts', 5);
93
- const memoryBudgetTokens = value.memoryBudgetTokens === undefined ? base.memoryBudgetTokens : value.memoryBudgetTokens;
94
- const skillBudgetTokens = value.skillBudgetTokens === undefined ? base.skillBudgetTokens : value.skillBudgetTokens;
95
- const notesBudgetTokens = value.notesBudgetTokens === undefined ? base.notesBudgetTokens : value.notesBudgetTokens;
96
- for (const [name, amount] of [['memoryBudgetTokens', memoryBudgetTokens], ['skillBudgetTokens', skillBudgetTokens], ['notesBudgetTokens', notesBudgetTokens]]) {
97
- positiveInt(amount, name, 128_000);
98
- }
99
- return {
100
- preset,
101
- provider,
102
- model,
103
- reasoningEffort,
104
- maxTokens,
105
- contextWindow,
106
- compactionThreshold,
107
- fallback,
108
- retry: { maxAttempts },
109
- memoryBudgetTokens,
110
- skillBudgetTokens,
111
- notesBudgetTokens,
112
- };
113
- }
114
-
115
- /** Team-level allow-list; a Lead can only choose inside this envelope. */
116
- export function validateMemberConfigPolicy(config, policy = {}) {
117
- if (Array.isArray(policy.allowedProviders) && !policy.allowedProviders.includes(config.provider)) {
118
- fail(ERRORS.CONFIG_POLICY, `provider ${config.provider} is not allowed by the Team policy`);
119
- }
120
- if (Array.isArray(policy.allowedModels) && !policy.allowedModels.includes(config.model)) {
121
- fail(ERRORS.CONFIG_POLICY, `model ${config.model} is not allowed by the Team policy`);
122
- }
123
- for (const [index, fallback] of config.fallback.entries()) {
124
- if (Array.isArray(policy.allowedProviders) && !policy.allowedProviders.includes(fallback.provider)) {
125
- fail(ERRORS.CONFIG_POLICY, `fallback[${index}].provider ${fallback.provider} is not allowed`);
126
- }
127
- if (Array.isArray(policy.allowedModels) && !policy.allowedModels.includes(fallback.model)) {
128
- fail(ERRORS.CONFIG_POLICY, `fallback[${index}].model ${fallback.model} is not allowed`);
129
- }
130
- }
131
- if (isInt(policy.maxAttempts) && config.retry.maxAttempts > policy.maxAttempts) {
132
- fail(ERRORS.CONFIG_POLICY, `retry.maxAttempts exceeds Team policy ${policy.maxAttempts}`);
133
- }
134
- if (isInt(policy.maxTokens) && config.maxTokens !== null && config.maxTokens > policy.maxTokens) {
135
- fail(ERRORS.CONFIG_POLICY, `maxTokens exceeds Team policy ${policy.maxTokens}`);
136
- }
137
- if (Array.isArray(policy.allowedPresets) && !policy.allowedPresets.includes(config.preset)) {
138
- fail(ERRORS.CONFIG_POLICY, `preset ${config.preset} is not allowed by the Team policy`);
139
- }
140
- return true;
141
- }
142
-
143
- /**
144
- * Apply a patch. The caller is responsible for the actor check (Lead/Human);
145
- * this function only validates and versions. A no-op patch does not bump.
146
- */
147
- export function applyMemberConfigPatch(current, patch, options = {}) {
148
- const before = normalizeMemberConfig(current);
149
- const merged = normalizeMemberConfig({ ...before, ...patch }, before);
150
- validateMemberConfigPolicy(merged, options.policy ?? {});
151
- const changed = CONFIG_FIELDS.some((field) => JSON.stringify(before[field]) !== JSON.stringify(merged[field]));
152
- if (!changed) return { config: { ...before, configVersion: options.configVersion ?? 0 }, before, changed: false };
153
- return {
154
- config: { ...merged, configVersion: (options.configVersion ?? 0) + 1 },
155
- before,
156
- changed: true,
157
- };
158
- }
159
-
160
- /** Only the fields DSH AgentOptions understands. */
161
- export function buildAgentOptions(config) {
162
- const options = {};
163
- if (config.provider) options.provider = config.provider;
164
- if (config.model) options.model = config.model;
165
- if (typeof config.reasoningEffort === 'string' && config.reasoningEffort !== '') options.reasoningEffort = config.reasoningEffort;
166
- if (Number.isSafeInteger(config.maxTokens)) options.maxTokens = config.maxTokens;
167
- return options;
168
- }
1
+ import { ERRORS, fail } from './errors.js';
2
+
3
+ /**
4
+ * Member model/context configuration. The Project owner (Agent Lead) may
5
+ * update it within the Team policy; Human may always update it. Config is
6
+ * versioned and takes effect at the next wake, never mid-turn.
7
+ */
8
+ export const MEMBER_CONFIG_VERSION = 1;
9
+
10
+ export const MEMBER_PRESETS = Object.freeze(['standard', 'ptc', 'minimal', 'cordis']);
11
+
12
+ export const DEFAULT_MEMBER_CONFIG = Object.freeze({
13
+ preset: 'standard',
14
+ provider: '',
15
+ model: '',
16
+ reasoningEffort: null,
17
+ maxTokens: null,
18
+ contextWindow: null,
19
+ compactionThreshold: 0.8,
20
+ fallback: [],
21
+ retry: { maxAttempts: 3 },
22
+ memoryBudgetTokens: 4096,
23
+ skillBudgetTokens: 2048,
24
+ notesBudgetTokens: 2048,
25
+ });
26
+
27
+ export const CONFIG_FIELDS = Object.freeze([
28
+ 'preset',
29
+ 'provider', 'model', 'reasoningEffort', 'maxTokens', 'contextWindow',
30
+ 'compactionThreshold', 'fallback', 'retry',
31
+ 'memoryBudgetTokens', 'skillBudgetTokens', 'notesBudgetTokens',
32
+ ]);
33
+
34
+ function isInt(value) {
35
+ return Number.isSafeInteger(value);
36
+ }
37
+
38
+ function positiveInt(value, name, max) {
39
+ if (!isInt(value) || value < 1 || value > max) fail(ERRORS.INVALID_ARGUMENT, `${name} must be an integer in 1..${max}`);
40
+ }
41
+
42
+ function textOrNull(value) {
43
+ return typeof value === 'string' && value !== '' ? value : null;
44
+ }
45
+
46
+ function normalizeFallback(value) {
47
+ if (!Array.isArray(value)) fail(ERRORS.INVALID_ARGUMENT, 'fallback must be an array');
48
+ if (value.length > 3) fail(ERRORS.INVALID_ARGUMENT, 'fallback supports at most 3 entries');
49
+ return value.map((entry, index) => {
50
+ if (entry === null || typeof entry !== 'object') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}] must be an object`);
51
+ if (typeof entry.provider !== 'string' || entry.provider === '') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}].provider is required`);
52
+ if (typeof entry.model !== 'string' || entry.model === '') fail(ERRORS.INVALID_ARGUMENT, `fallback[${index}].model is required`);
53
+ if (entry.maxTokens !== undefined) positiveInt(entry.maxTokens, `fallback[${index}].maxTokens`, 1_000_000);
54
+ return {
55
+ provider: entry.provider,
56
+ model: entry.model,
57
+ ...(textOrNull(entry.reasoningEffort) === null ? {} : { reasoningEffort: entry.reasoningEffort }),
58
+ ...(entry.maxTokens === undefined ? {} : { maxTokens: entry.maxTokens }),
59
+ };
60
+ });
61
+ }
62
+
63
+ /** Merge + validate one complete config; throws DomainError on invalid fields. */
64
+ export function normalizeMemberConfig(input = {}, base = DEFAULT_MEMBER_CONFIG) {
65
+ const value = input ?? {};
66
+ const preset = value.preset === undefined ? base.preset : value.preset;
67
+ if (typeof preset !== 'string' || !MEMBER_PRESETS.includes(preset)) {
68
+ fail(ERRORS.INVALID_ARGUMENT, `preset must be one of ${MEMBER_PRESETS.join(', ')}`);
69
+ }
70
+ const provider = value.provider === undefined ? base.provider : value.provider;
71
+ const model = value.model === undefined ? base.model : value.model;
72
+ if (typeof provider !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'provider must be a string');
73
+ if (typeof model !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'model must be a string');
74
+ const reasoningEffort = value.reasoningEffort === undefined ? base.reasoningEffort : textOrNull(value.reasoningEffort);
75
+ if (reasoningEffort !== null && typeof reasoningEffort !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'reasoningEffort must be a string or null');
76
+ const maxTokens = value.maxTokens === undefined ? base.maxTokens : value.maxTokens;
77
+ if (maxTokens !== null) positiveInt(maxTokens, 'maxTokens', 1_000_000);
78
+ const contextWindow = value.contextWindow === undefined ? base.contextWindow : value.contextWindow;
79
+ if (contextWindow !== null) {
80
+ if (!isInt(contextWindow) || contextWindow < 4_096 || contextWindow > 2_000_000) {
81
+ fail(ERRORS.INVALID_ARGUMENT, 'contextWindow must be an integer in 4096..2000000 or null');
82
+ }
83
+ }
84
+ const compactionThreshold = value.compactionThreshold === undefined ? base.compactionThreshold : value.compactionThreshold;
85
+ if (typeof compactionThreshold !== 'number' || !Number.isFinite(compactionThreshold) || compactionThreshold < 0.5 || compactionThreshold > 0.95) {
86
+ fail(ERRORS.INVALID_ARGUMENT, 'compactionThreshold must be in 0.5..0.95');
87
+ }
88
+ const fallback = value.fallback === undefined ? normalizeFallback(base.fallback) : normalizeFallback(value.fallback);
89
+ const retryInput = value.retry === undefined ? base.retry : value.retry;
90
+ if (retryInput === null || typeof retryInput !== 'object') fail(ERRORS.INVALID_ARGUMENT, 'retry must be an object');
91
+ const maxAttempts = retryInput.maxAttempts === undefined ? base.retry.maxAttempts : retryInput.maxAttempts;
92
+ positiveInt(maxAttempts, 'retry.maxAttempts', 5);
93
+ const memoryBudgetTokens = value.memoryBudgetTokens === undefined ? base.memoryBudgetTokens : value.memoryBudgetTokens;
94
+ const skillBudgetTokens = value.skillBudgetTokens === undefined ? base.skillBudgetTokens : value.skillBudgetTokens;
95
+ const notesBudgetTokens = value.notesBudgetTokens === undefined ? base.notesBudgetTokens : value.notesBudgetTokens;
96
+ for (const [name, amount] of [['memoryBudgetTokens', memoryBudgetTokens], ['skillBudgetTokens', skillBudgetTokens], ['notesBudgetTokens', notesBudgetTokens]]) {
97
+ positiveInt(amount, name, 128_000);
98
+ }
99
+ return {
100
+ preset,
101
+ provider,
102
+ model,
103
+ reasoningEffort,
104
+ maxTokens,
105
+ contextWindow,
106
+ compactionThreshold,
107
+ fallback,
108
+ retry: { maxAttempts },
109
+ memoryBudgetTokens,
110
+ skillBudgetTokens,
111
+ notesBudgetTokens,
112
+ };
113
+ }
114
+
115
+ /** Team-level allow-list; a Lead can only choose inside this envelope. */
116
+ export function validateMemberConfigPolicy(config, policy = {}) {
117
+ if (Array.isArray(policy.allowedProviders) && !policy.allowedProviders.includes(config.provider)) {
118
+ fail(ERRORS.CONFIG_POLICY, `provider ${config.provider} is not allowed by the Team policy`);
119
+ }
120
+ if (Array.isArray(policy.allowedModels) && !policy.allowedModels.includes(config.model)) {
121
+ fail(ERRORS.CONFIG_POLICY, `model ${config.model} is not allowed by the Team policy`);
122
+ }
123
+ for (const [index, fallback] of config.fallback.entries()) {
124
+ if (Array.isArray(policy.allowedProviders) && !policy.allowedProviders.includes(fallback.provider)) {
125
+ fail(ERRORS.CONFIG_POLICY, `fallback[${index}].provider ${fallback.provider} is not allowed`);
126
+ }
127
+ if (Array.isArray(policy.allowedModels) && !policy.allowedModels.includes(fallback.model)) {
128
+ fail(ERRORS.CONFIG_POLICY, `fallback[${index}].model ${fallback.model} is not allowed`);
129
+ }
130
+ }
131
+ if (isInt(policy.maxAttempts) && config.retry.maxAttempts > policy.maxAttempts) {
132
+ fail(ERRORS.CONFIG_POLICY, `retry.maxAttempts exceeds Team policy ${policy.maxAttempts}`);
133
+ }
134
+ if (isInt(policy.maxTokens) && config.maxTokens !== null && config.maxTokens > policy.maxTokens) {
135
+ fail(ERRORS.CONFIG_POLICY, `maxTokens exceeds Team policy ${policy.maxTokens}`);
136
+ }
137
+ if (Array.isArray(policy.allowedPresets) && !policy.allowedPresets.includes(config.preset)) {
138
+ fail(ERRORS.CONFIG_POLICY, `preset ${config.preset} is not allowed by the Team policy`);
139
+ }
140
+ return true;
141
+ }
142
+
143
+ /**
144
+ * Apply a patch. The caller is responsible for the actor check (Lead/Human);
145
+ * this function only validates and versions. A no-op patch does not bump.
146
+ */
147
+ export function applyMemberConfigPatch(current, patch, options = {}) {
148
+ const before = normalizeMemberConfig(current);
149
+ const merged = normalizeMemberConfig({ ...before, ...patch }, before);
150
+ validateMemberConfigPolicy(merged, options.policy ?? {});
151
+ const changed = CONFIG_FIELDS.some((field) => JSON.stringify(before[field]) !== JSON.stringify(merged[field]));
152
+ if (!changed) return { config: { ...before, configVersion: options.configVersion ?? 0 }, before, changed: false };
153
+ return {
154
+ config: { ...merged, configVersion: (options.configVersion ?? 0) + 1 },
155
+ before,
156
+ changed: true,
157
+ };
158
+ }
159
+
160
+ /** Only the fields DSH AgentOptions understands. */
161
+ export function buildAgentOptions(config) {
162
+ const options = {};
163
+ if (config.provider) options.provider = config.provider;
164
+ if (config.model) options.model = config.model;
165
+ if (typeof config.reasoningEffort === 'string' && config.reasoningEffort !== '') options.reasoningEffort = config.reasoningEffort;
166
+ if (Number.isSafeInteger(config.maxTokens)) options.maxTokens = config.maxTokens;
167
+ return options;
168
+ }