blun-king-cli 9.1.304 → 9.1.306
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/bin/cognitive-state-store.cjs +9 -1
- package/bin/cognitive-turn-lifecycle.cjs +200 -0
- package/blun.mjs +70 -2
- package/package.json +1 -1
|
@@ -235,7 +235,15 @@ function openCognitiveStateStore({ home } = {}) {
|
|
|
235
235
|
return { valid: true, events: rows.length, head_hash: previousHash };
|
|
236
236
|
}
|
|
237
237
|
|
|
238
|
-
return {
|
|
238
|
+
return {
|
|
239
|
+
commit,
|
|
240
|
+
read,
|
|
241
|
+
verify,
|
|
242
|
+
close: () => {
|
|
243
|
+
try { db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); } catch {}
|
|
244
|
+
db.close();
|
|
245
|
+
},
|
|
246
|
+
};
|
|
239
247
|
}
|
|
240
248
|
|
|
241
249
|
module.exports = { openCognitiveStateStore };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('node:crypto');
|
|
4
|
+
const { openCognitiveStateStore } = require('./cognitive-state-store.cjs');
|
|
5
|
+
|
|
6
|
+
const SAFE_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/u;
|
|
7
|
+
const RIGHTS_AUTHORITIES = new Set(['runtime_user_prompt_hook', 'runtime_tool_policy']);
|
|
8
|
+
const RIGHTS_DECISIONS = new Set(['passed', 'blocked', 'not_applicable']);
|
|
9
|
+
const TOOL_POLICY_DECISIONS = new Set(['passed', 'blocked', 'error']);
|
|
10
|
+
const TOOL_OUTCOMES = new Set(['success', 'error', 'cancelled']);
|
|
11
|
+
const TURN_REASONS = new Set(['completed', 'cancelled', 'filtered', 'failed']);
|
|
12
|
+
|
|
13
|
+
function fail(code) {
|
|
14
|
+
const error = new Error(code);
|
|
15
|
+
error.code = code;
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function exactKeys(value, keys) {
|
|
20
|
+
return value && typeof value === 'object' && !Array.isArray(value)
|
|
21
|
+
&& Object.keys(value).every((key) => keys.has(key));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function safeId(value) {
|
|
25
|
+
const text = String(value ?? '').trim();
|
|
26
|
+
return SAFE_ID_RE.test(text) ? text : '';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function safeTurnId(value) {
|
|
30
|
+
return Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function cleanLabel(value, max = 128) {
|
|
34
|
+
const text = String(value ?? '').replace(/[\u0000-\u001f\u007f]+/gu, ' ').replace(/\s+/gu, ' ').trim();
|
|
35
|
+
return text && text.length <= max ? text : '';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function digestId(prefix, values) {
|
|
39
|
+
const digest = crypto.createHash('sha256').update(values.join('\0')).digest('hex').slice(0, 40);
|
|
40
|
+
return `${prefix}-${digest}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function createCognitiveTurnLifecycle({ home, tenantId, agentId, runtimeId, now = () => new Date().toISOString() } = {}) {
|
|
44
|
+
const tenant = safeId(tenantId);
|
|
45
|
+
const agent = safeId(agentId);
|
|
46
|
+
const runtime = safeId(runtimeId ?? `runtime-${process.pid}-${Date.now()}`);
|
|
47
|
+
if (!tenant || !agent || !runtime || typeof now !== 'function') fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
48
|
+
const store = openCognitiveStateStore({ home });
|
|
49
|
+
const stageTimes = new Map();
|
|
50
|
+
const stageResults = new Map();
|
|
51
|
+
|
|
52
|
+
function stageTime(stageKey) {
|
|
53
|
+
if (!stageTimes.has(stageKey)) {
|
|
54
|
+
const occurredAt = String(now());
|
|
55
|
+
if (Number.isNaN(Date.parse(occurredAt))) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
56
|
+
stageTimes.set(stageKey, occurredAt);
|
|
57
|
+
}
|
|
58
|
+
return stageTimes.get(stageKey);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function commitStage(stageKey, observations) {
|
|
62
|
+
if (stageResults.has(stageKey)) return { ...stageResults.get(stageKey), idempotent: true };
|
|
63
|
+
const occurredAt = stageTime(stageKey);
|
|
64
|
+
const eventId = digestId('cycle', [tenant, agent, runtime, stageKey]);
|
|
65
|
+
const normalized = observations.map((item, index) => ({
|
|
66
|
+
observation_id: digestId('cycleobs', [eventId, String(index)]),
|
|
67
|
+
...item,
|
|
68
|
+
}));
|
|
69
|
+
for (let attempt = 0; attempt < 4; attempt += 1) {
|
|
70
|
+
const expectedVersion = store.read({ tenantId: tenant, agentId: agent }).version;
|
|
71
|
+
try {
|
|
72
|
+
const result = store.commit({
|
|
73
|
+
tenantId: tenant,
|
|
74
|
+
agentId: agent,
|
|
75
|
+
eventId,
|
|
76
|
+
expectedVersion,
|
|
77
|
+
occurredAt,
|
|
78
|
+
source: {
|
|
79
|
+
provider: 'runtime',
|
|
80
|
+
actor_id: agent,
|
|
81
|
+
context_id: runtime,
|
|
82
|
+
message_id: stageKey,
|
|
83
|
+
},
|
|
84
|
+
observations: normalized,
|
|
85
|
+
});
|
|
86
|
+
stageResults.set(stageKey, result);
|
|
87
|
+
return result;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (error?.code !== 'COGNITIVE_VERSION_CONFLICT' || attempt === 3) throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
fail('COGNITIVE_VERSION_CONFLICT');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function startTurn(input) {
|
|
96
|
+
if (!exactKeys(input, new Set(['turnId', 'originKind']))) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
97
|
+
const turnId = safeTurnId(input.turnId);
|
|
98
|
+
const originKind = safeId(input.originKind);
|
|
99
|
+
if (turnId === null || !originKind) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
100
|
+
const key = `turn:${turnId}`;
|
|
101
|
+
return commitStage(`turn-${turnId}-start`, [
|
|
102
|
+
{ domain: 'open_thread', key: `${key}:phase`, value: 'perceived', confidence: 1, scope: 'runtime' },
|
|
103
|
+
{ domain: 'assumption', key: `${key}:classification`, value: `origin:${originKind}`, confidence: 1, scope: 'runtime' },
|
|
104
|
+
{ domain: 'goal', key: `${key}:plan`, value: 'run-bounded-model-turn', confidence: 1, scope: 'runtime' },
|
|
105
|
+
{ domain: 'next_trigger', key: `${key}:next`, value: 'runtime-policy-check', confidence: 1, scope: 'runtime' },
|
|
106
|
+
{ domain: 'expected_evidence', key: `${key}:evidence`, value: 'turn.ended-event', confidence: 1, scope: 'runtime' },
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function recordRightsCheck(input) {
|
|
111
|
+
if (!exactKeys(input, new Set(['turnId', 'decision', 'authority']))) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
112
|
+
const turnId = safeTurnId(input.turnId);
|
|
113
|
+
const decision = String(input.decision ?? '');
|
|
114
|
+
const authority = String(input.authority ?? '');
|
|
115
|
+
if (turnId === null || !RIGHTS_DECISIONS.has(decision)) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
116
|
+
if (!RIGHTS_AUTHORITIES.has(authority)) fail('COGNITIVE_RIGHTS_AUTHORITY_REQUIRED');
|
|
117
|
+
return commitStage(`turn-${turnId}-rights-${authority}`, [{
|
|
118
|
+
domain: 'world',
|
|
119
|
+
key: `turn:${turnId}:rights-check`,
|
|
120
|
+
value: `${decision}:${authority}`,
|
|
121
|
+
confidence: 1,
|
|
122
|
+
scope: 'runtime',
|
|
123
|
+
}]);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function endTurn(input) {
|
|
127
|
+
if (!exactKeys(input, new Set(['turnId', 'reason', 'durationMs']))) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
128
|
+
const turnId = safeTurnId(input.turnId);
|
|
129
|
+
const reason = String(input.reason ?? '');
|
|
130
|
+
const durationMs = Number(input.durationMs);
|
|
131
|
+
if (turnId === null || !TURN_REASONS.has(reason) || !Number.isSafeInteger(durationMs) || durationMs < 0) {
|
|
132
|
+
fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
133
|
+
}
|
|
134
|
+
const key = `turn:${turnId}`;
|
|
135
|
+
return commitStage(`turn-${turnId}-end`, [
|
|
136
|
+
{ domain: 'open_thread', key: `${key}:phase`, value: reason, confidence: 1, scope: 'runtime' },
|
|
137
|
+
{ domain: 'expected_evidence', key: `${key}:result`, value: `turn.ended:${reason}:${durationMs}ms`, confidence: 1, scope: 'runtime' },
|
|
138
|
+
{ domain: 'next_trigger', key: `${key}:next`, value: reason === 'completed' ? 'await-next-input' : 'inspect-turn-outcome', confidence: 1, scope: 'runtime' },
|
|
139
|
+
]);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function toolFields(input, allowedKeys) {
|
|
143
|
+
if (!exactKeys(input, allowedKeys)) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
144
|
+
const turnId = safeTurnId(input.turnId);
|
|
145
|
+
const toolCallId = cleanLabel(input.toolCallId, 256);
|
|
146
|
+
const toolName = cleanLabel(input.toolName, 128);
|
|
147
|
+
if (turnId === null || !toolCallId || !toolName) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
148
|
+
return { turnId, toolCallId, toolName, callKey: digestId('call', [toolCallId]).slice(0, 21) };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function recordToolPolicy(input) {
|
|
152
|
+
const { turnId, toolCallId, toolName, callKey } = toolFields(input, new Set(['turnId', 'toolCallId', 'toolName', 'decision']));
|
|
153
|
+
const decision = String(input.decision ?? '');
|
|
154
|
+
if (!TOOL_POLICY_DECISIONS.has(decision)) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
155
|
+
return commitStage(`turn-${turnId}-${callKey}-policy`, [{
|
|
156
|
+
domain: 'world',
|
|
157
|
+
key: `turn:${turnId}:${callKey}:tool-policy`,
|
|
158
|
+
value: `${decision}:runtime_tool_policy:${toolName}`,
|
|
159
|
+
confidence: 1,
|
|
160
|
+
scope: 'runtime',
|
|
161
|
+
}]);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function recordToolResult(input) {
|
|
165
|
+
const { turnId, toolCallId, toolName, callKey } = toolFields(input, new Set(['turnId', 'toolCallId', 'toolName', 'outcome', 'durationMs']));
|
|
166
|
+
const outcome = String(input.outcome ?? '');
|
|
167
|
+
const durationMs = Number(input.durationMs);
|
|
168
|
+
if (!TOOL_OUTCOMES.has(outcome) || !Number.isSafeInteger(durationMs) || durationMs < 0) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
169
|
+
return commitStage(`turn-${turnId}-${callKey}-result`, [
|
|
170
|
+
{ domain: 'expected_evidence', key: `turn:${turnId}:${callKey}:tool-result`, value: `${outcome}:${durationMs}ms:${toolName}`, confidence: 1, scope: 'runtime' },
|
|
171
|
+
{ domain: 'next_trigger', key: `turn:${turnId}:${callKey}:tool-next`, value: outcome === 'success' ? 'continue-after-tool' : 'inspect-tool-failure', confidence: 1, scope: 'runtime' },
|
|
172
|
+
]);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
startTurn,
|
|
177
|
+
recordRightsCheck,
|
|
178
|
+
recordToolPolicy,
|
|
179
|
+
recordToolResult,
|
|
180
|
+
endTurn,
|
|
181
|
+
read: () => store.read({ tenantId: tenant, agentId: agent }),
|
|
182
|
+
verify: () => store.verify({ tenantId: tenant, agentId: agent }),
|
|
183
|
+
close: () => store.close(),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function createRuntimeCognitiveTurnLifecycle({ home, agentName, runtimeId, now } = {}) {
|
|
188
|
+
const resolvedHome = String(home ?? '').trim();
|
|
189
|
+
const resolvedAgent = String(agentName ?? '').trim();
|
|
190
|
+
if (!resolvedHome || !resolvedAgent) fail('COGNITIVE_LIFECYCLE_INVALID');
|
|
191
|
+
return createCognitiveTurnLifecycle({
|
|
192
|
+
home: resolvedHome,
|
|
193
|
+
tenantId: digestId('home', [resolvedHome.toLowerCase()]),
|
|
194
|
+
agentId: digestId('agent', [resolvedAgent.toLowerCase()]),
|
|
195
|
+
runtimeId,
|
|
196
|
+
now,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
module.exports = { createCognitiveTurnLifecycle, createRuntimeCognitiveTurnLifecycle };
|
package/blun.mjs
CHANGED
|
@@ -261391,7 +261391,7 @@ function toolResultText(result) {
|
|
|
261391
261391
|
function abandonedToolResultOutput(ended) {
|
|
261392
261392
|
return `Tool call did not complete: ${ended.reason === "cancelled" ? "the turn was cancelled" : ended.reason === "failed" ? `the turn failed${ended.error !== void 0 ? ` (${ended.error.message})` : ""}` : "the turn ended"} before its result was recorded. Do not assume the tool completed successfully.`;
|
|
261393
261393
|
}
|
|
261394
|
-
var BLUN_CORE_TOOL_NAMES, BLUN_LEAN_TOOL_NAMES, BLUN_ATTACHMENT_MARKER_RE, BLUN_TELEGRAM_OUTBOUND_TOOL_RE, BLUN_TELEGRAM_CHANNEL_RE, BLUN_TOOL_BUDGET_RATIO, createDeferredToolLoader, mediaGenerationToolNamesForText, rememberDeferredToolAfterNotFound, toolSchemaBudgetTokens, projectRecurringCronHistory, TelegramDeliveryLedger, LLM_NOT_SET_MESSAGE, GOAL_CONTINUATION_ORIGIN, GOAL_COMPLETION_REMINDER_NAME, GOAL_BLOCKED_REMINDER_NAME, GOAL_RATE_LIMIT_PAUSE_REASON, GOAL_PROVIDER_CONNECTION_PAUSE_PREFIX, GOAL_PROVIDER_AUTH_PAUSE_PREFIX, GOAL_PROVIDER_API_PAUSE_PREFIX, GOAL_MODEL_CONFIG_PAUSE_PREFIX, GOAL_RUNTIME_PAUSE_PREFIX, GOAL_PROVIDER_FILTERED_PAUSE_REASON, GOAL_CONTINUATION_PROMPT, TurnFlow;
|
|
261394
|
+
var BLUN_CORE_TOOL_NAMES, BLUN_LEAN_TOOL_NAMES, BLUN_ATTACHMENT_MARKER_RE, BLUN_TELEGRAM_OUTBOUND_TOOL_RE, BLUN_TELEGRAM_CHANNEL_RE, BLUN_TOOL_BUDGET_RATIO, createDeferredToolLoader, mediaGenerationToolNamesForText, rememberDeferredToolAfterNotFound, toolSchemaBudgetTokens, projectRecurringCronHistory, TelegramDeliveryLedger, createRuntimeCognitiveTurnLifecycle, LLM_NOT_SET_MESSAGE, GOAL_CONTINUATION_ORIGIN, GOAL_COMPLETION_REMINDER_NAME, GOAL_BLOCKED_REMINDER_NAME, GOAL_RATE_LIMIT_PAUSE_REASON, GOAL_PROVIDER_CONNECTION_PAUSE_PREFIX, GOAL_PROVIDER_AUTH_PAUSE_PREFIX, GOAL_PROVIDER_API_PAUSE_PREFIX, GOAL_MODEL_CONFIG_PAUSE_PREFIX, GOAL_RUNTIME_PAUSE_PREFIX, GOAL_PROVIDER_FILTERED_PAUSE_REASON, GOAL_CONTINUATION_PROMPT, TurnFlow;
|
|
261395
261395
|
var init_turn = __esmMin((() => {
|
|
261396
261396
|
init_dist$4();
|
|
261397
261397
|
init_src$4();
|
|
@@ -261411,6 +261411,7 @@ var init_turn = __esmMin((() => {
|
|
|
261411
261411
|
({ CORE_TOOL_NAMES: BLUN_CORE_TOOL_NAMES, createDeferredToolLoader, mediaGenerationToolNamesForText, rememberDeferredToolAfterNotFound, toolSchemaBudgetTokens } = createRequire(import.meta.url)("./bin/turn-tool-performance-policy.cjs"));
|
|
261412
261412
|
({ projectRecurringCronHistory } = createRequire(import.meta.url)("./bin/recurring-cron-history-policy.cjs"));
|
|
261413
261413
|
({ TelegramDeliveryLedger } = createRequire(import.meta.url)("./bin/repeated-user-message-projection.cjs"));
|
|
261414
|
+
({ createRuntimeCognitiveTurnLifecycle } = createRequire(import.meta.url)("./bin/cognitive-turn-lifecycle.cjs"));
|
|
261414
261415
|
BLUN_LEAN_TOOL_NAMES = new Set(BLUN_CORE_TOOL_NAMES);
|
|
261415
261416
|
BLUN_ATTACHMENT_MARKER_RE = /\b(?:attachment_file_id|telegram-anhang|telegram attachment)\b/i;
|
|
261416
261417
|
BLUN_TELEGRAM_OUTBOUND_TOOL_RE = /^mcp__[^\s]*telegram[^\s]*__(?:reply|react|edit_message)$/i;
|
|
@@ -261471,6 +261472,8 @@ var init_turn = __esmMin((() => {
|
|
|
261471
261472
|
interruptedTelemetryTurnIds = /* @__PURE__ */ new Set();
|
|
261472
261473
|
stepFailureByTurn = /* @__PURE__ */ new Map();
|
|
261473
261474
|
currentStep = 0;
|
|
261475
|
+
cognitiveLifecycle;
|
|
261476
|
+
cognitiveLifecycleUnavailable = false;
|
|
261474
261477
|
constructor(agent) {
|
|
261475
261478
|
this.agent = agent;
|
|
261476
261479
|
}
|
|
@@ -261478,6 +261481,33 @@ var init_turn = __esmMin((() => {
|
|
|
261478
261481
|
get agentId() {
|
|
261479
261482
|
return this.agent.homedir ? basename$2(this.agent.homedir) : this.agent.type;
|
|
261480
261483
|
}
|
|
261484
|
+
getCognitiveLifecycle() {
|
|
261485
|
+
if (this.cognitiveLifecycleUnavailable) return null;
|
|
261486
|
+
if (this.cognitiveLifecycle !== void 0) return this.cognitiveLifecycle;
|
|
261487
|
+
const home = this.agent.blunHomeDir ?? this.agent.homedir;
|
|
261488
|
+
if (home === void 0) {
|
|
261489
|
+
this.cognitiveLifecycleUnavailable = true;
|
|
261490
|
+
return null;
|
|
261491
|
+
}
|
|
261492
|
+
try {
|
|
261493
|
+
this.cognitiveLifecycle = createRuntimeCognitiveTurnLifecycle({
|
|
261494
|
+
home,
|
|
261495
|
+
agentName: this.agent.activeProfile?.name ?? this.agent.type
|
|
261496
|
+
});
|
|
261497
|
+
return this.cognitiveLifecycle;
|
|
261498
|
+
} catch (error) {
|
|
261499
|
+
this.cognitiveLifecycleUnavailable = true;
|
|
261500
|
+
this.agent.telemetry.track("cognitive_lifecycle_error", { stage: "open", error_type: error?.code ?? error?.name ?? "Error" });
|
|
261501
|
+
return null;
|
|
261502
|
+
}
|
|
261503
|
+
}
|
|
261504
|
+
recordCognitiveStage(method, input) {
|
|
261505
|
+
try {
|
|
261506
|
+
this.getCognitiveLifecycle()?.[method](input);
|
|
261507
|
+
} catch (error) {
|
|
261508
|
+
this.agent.telemetry.track("cognitive_lifecycle_error", { stage: method, error_type: error?.code ?? error?.name ?? "Error" });
|
|
261509
|
+
}
|
|
261510
|
+
}
|
|
261481
261511
|
prompt(input, origin = USER_PROMPT_ORIGIN) {
|
|
261482
261512
|
return this.promptWithAcceptance(input, origin).turnId;
|
|
261483
261513
|
}
|
|
@@ -261805,6 +261835,12 @@ var init_turn = __esmMin((() => {
|
|
|
261805
261835
|
origin
|
|
261806
261836
|
});
|
|
261807
261837
|
this.agent.context.appendUserMessage(input, origin);
|
|
261838
|
+
this.recordCognitiveStage("startTurn", { turnId, originKind: origin.kind });
|
|
261839
|
+
this.recordCognitiveStage("recordRightsCheck", {
|
|
261840
|
+
turnId,
|
|
261841
|
+
decision: "not_applicable",
|
|
261842
|
+
authority: "runtime_user_prompt_hook"
|
|
261843
|
+
});
|
|
261808
261844
|
const ended = {
|
|
261809
261845
|
type: "turn.ended",
|
|
261810
261846
|
turnId,
|
|
@@ -261812,6 +261848,7 @@ var init_turn = __esmMin((() => {
|
|
|
261812
261848
|
durationMs: Date.now() - startedAt
|
|
261813
261849
|
};
|
|
261814
261850
|
this.agent.usage.endTurn();
|
|
261851
|
+
this.recordCognitiveStage("endTurn", { turnId, reason: ended.reason, durationMs: ended.durationMs ?? 0 });
|
|
261815
261852
|
this.agent.emitEvent(ended);
|
|
261816
261853
|
return ended;
|
|
261817
261854
|
}
|
|
@@ -261841,6 +261878,7 @@ var init_turn = __esmMin((() => {
|
|
|
261841
261878
|
origin
|
|
261842
261879
|
});
|
|
261843
261880
|
this.agent.context.appendUserMessage(input, origin);
|
|
261881
|
+
this.recordCognitiveStage("startTurn", { turnId, originKind: origin.kind });
|
|
261844
261882
|
const startedAt = Date.now();
|
|
261845
261883
|
let ended;
|
|
261846
261884
|
let blockedByUserPromptHook = false;
|
|
@@ -261848,6 +261886,11 @@ var init_turn = __esmMin((() => {
|
|
|
261848
261886
|
let errorEvent;
|
|
261849
261887
|
try {
|
|
261850
261888
|
const promptHookEnded = await this.applyUserPromptHook(turnId, input, origin, signal, startedAt);
|
|
261889
|
+
this.recordCognitiveStage("recordRightsCheck", {
|
|
261890
|
+
turnId,
|
|
261891
|
+
decision: origin.kind !== "user" ? "not_applicable" : promptHookEnded?.blocked === true ? "blocked" : "passed",
|
|
261892
|
+
authority: "runtime_user_prompt_hook"
|
|
261893
|
+
});
|
|
261851
261894
|
if (promptHookEnded !== void 0) {
|
|
261852
261895
|
ended = promptHookEnded.event;
|
|
261853
261896
|
blockedByUserPromptHook = promptHookEnded.blocked;
|
|
@@ -261919,6 +261962,7 @@ var init_turn = __esmMin((() => {
|
|
|
261919
261962
|
mode: this.telemetryModeByTurn.get(turnId) ?? this.telemetryMode(),
|
|
261920
261963
|
...this.requestProviderProps()
|
|
261921
261964
|
});
|
|
261965
|
+
this.recordCognitiveStage("endTurn", { turnId, reason: ended.reason, durationMs: ended.durationMs ?? 0 });
|
|
261922
261966
|
this.agent.emitEvent(ended);
|
|
261923
261967
|
this.agent.endResponderTurn();
|
|
261924
261968
|
if (standalone && this.currentId === turnId && this.agent.goal.getGoal().goal?.status !== "active") this.activeTurn = null;
|
|
@@ -262170,7 +262214,24 @@ var init_turn = __esmMin((() => {
|
|
|
262170
262214
|
if (cached !== null) return { syntheticResult: cached };
|
|
262171
262215
|
},
|
|
262172
262216
|
authorizeToolExecution: async (ctx) => {
|
|
262173
|
-
|
|
262217
|
+
try {
|
|
262218
|
+
const resolution = await this.agent.permission.beforeToolCall(ctx);
|
|
262219
|
+
this.recordCognitiveStage("recordToolPolicy", {
|
|
262220
|
+
turnId,
|
|
262221
|
+
toolCallId: ctx.toolCall.id,
|
|
262222
|
+
toolName: ctx.toolCall.name,
|
|
262223
|
+
decision: resolution?.block === true ? "blocked" : "passed"
|
|
262224
|
+
});
|
|
262225
|
+
return resolution;
|
|
262226
|
+
} catch (error) {
|
|
262227
|
+
this.recordCognitiveStage("recordToolPolicy", {
|
|
262228
|
+
turnId,
|
|
262229
|
+
toolCallId: ctx.toolCall.id,
|
|
262230
|
+
toolName: ctx.toolCall.name,
|
|
262231
|
+
decision: "error"
|
|
262232
|
+
});
|
|
262233
|
+
throw error;
|
|
262234
|
+
}
|
|
262174
262235
|
},
|
|
262175
262236
|
finalizeToolResult: async (ctx) => {
|
|
262176
262237
|
const finalResult = await deduper.finalizeResult(ctx.toolCall.id, ctx.toolCall.name, ctx.args, ctx.result);
|
|
@@ -262312,6 +262373,13 @@ var init_turn = __esmMin((() => {
|
|
|
262312
262373
|
};
|
|
262313
262374
|
const errorType = outcome === "error" ? telemetryToolErrorType(event.result) : void 0;
|
|
262314
262375
|
if (errorType !== void 0) properties["error_type"] = errorType;
|
|
262376
|
+
this.recordCognitiveStage("recordToolResult", {
|
|
262377
|
+
turnId,
|
|
262378
|
+
toolCallId: event.toolCallId,
|
|
262379
|
+
toolName: started.name,
|
|
262380
|
+
outcome,
|
|
262381
|
+
durationMs: Date.now() - started.startedAt
|
|
262382
|
+
});
|
|
262315
262383
|
this.agent.telemetry.track("tool_call", properties);
|
|
262316
262384
|
this.agent.feedRootMissionContract("result", {
|
|
262317
262385
|
toolCallId: event.toolCallId,
|