blun-king-cli 9.1.343 → 9.1.345

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.
@@ -198,6 +198,50 @@ function createCognitiveTurnLifecycle({ home, tenantId, agentId, runtimeId, now
198
198
  ]);
199
199
  }
200
200
 
201
+ function recordToolBatch(input) {
202
+ if (!exactKeys(input, new Set(['turnId', 'entries']))) fail('COGNITIVE_LIFECYCLE_INVALID');
203
+ const turnId = safeTurnId(input.turnId);
204
+ if (turnId === null || !Array.isArray(input.entries) || input.entries.length < 1 || input.entries.length > 10) {
205
+ fail('COGNITIVE_LIFECYCLE_INVALID');
206
+ }
207
+ const observations = [];
208
+ const stageParts = [];
209
+ for (const entry of input.entries) {
210
+ const { toolCallId, toolName, callKey } = toolFields(
211
+ { turnId, ...entry },
212
+ new Set(['turnId', 'toolCallId', 'toolName', 'decision', 'outcome', 'durationMs']),
213
+ );
214
+ const decision = String(entry.decision ?? '');
215
+ const outcome = String(entry.outcome ?? '');
216
+ const durationMs = Number(entry.durationMs);
217
+ if (!TOOL_POLICY_DECISIONS.has(decision) || !TOOL_OUTCOMES.has(outcome)
218
+ || !Number.isSafeInteger(durationMs) || durationMs < 0) fail('COGNITIVE_LIFECYCLE_INVALID');
219
+ stageParts.push(`${toolCallId}:${decision}:${outcome}:${durationMs}`);
220
+ observations.push({
221
+ domain: 'world',
222
+ key: `turn:${turnId}:${callKey}:tool-policy`,
223
+ value: `${decision}:runtime_tool_policy:${toolName}`,
224
+ confidence: 1,
225
+ scope: 'runtime',
226
+ }, {
227
+ domain: 'expected_evidence',
228
+ key: `turn:${turnId}:${callKey}:tool-result`,
229
+ value: `${outcome}:${durationMs}ms:${toolName}`,
230
+ confidence: 1,
231
+ scope: 'runtime',
232
+ }, {
233
+ domain: 'next_trigger',
234
+ key: `turn:${turnId}:${callKey}:tool-next`,
235
+ value: outcome === 'success' ? 'continue-after-tool' : 'inspect-tool-failure',
236
+ confidence: 1,
237
+ scope: 'runtime',
238
+ });
239
+ }
240
+ const batchKey = digestId('toolbatch', stageParts);
241
+ const result = commitStage(`turn-${turnId}-${batchKey}`, observations);
242
+ return { ...result, events: 1, entries: input.entries.length };
243
+ }
244
+
201
245
  function projectForTurn(input) {
202
246
  if (!exactKeys(input, new Set(['turnId', 'focusScopes']))) fail('COGNITIVE_LIFECYCLE_INVALID');
203
247
  const turnId = safeTurnId(input.turnId);
@@ -226,6 +270,7 @@ function createCognitiveTurnLifecycle({ home, tenantId, agentId, runtimeId, now
226
270
  recordRightsCheck,
227
271
  recordToolPolicy,
228
272
  recordToolResult,
273
+ recordToolBatch,
229
274
  recordFocusSnapshot,
230
275
  authorizeAttention,
231
276
  projectForTurn,
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ const SHORT_STREAM_MAX_CHARS = 8_192;
4
+ const MEDIUM_STREAM_MAX_CHARS = 32_768;
5
+ const LONG_STREAM_MAX_CHARS = 131_072;
6
+
7
+ function safeChars(value) {
8
+ return Number.isFinite(value) && value > 0 ? Math.floor(value) : 0;
9
+ }
10
+
11
+ function resolveStreamingFlushInterval(input = {}) {
12
+ const chars = Math.max(safeChars(input.assistantChars), safeChars(input.thinkingChars));
13
+ if (chars <= SHORT_STREAM_MAX_CHARS) return 50;
14
+ if (chars <= MEDIUM_STREAM_MAX_CHARS) return 100;
15
+ if (chars <= LONG_STREAM_MAX_CHARS) return 250;
16
+ return 500;
17
+ }
18
+
19
+ module.exports = {
20
+ LONG_STREAM_MAX_CHARS,
21
+ MEDIUM_STREAM_MAX_CHARS,
22
+ SHORT_STREAM_MAX_CHARS,
23
+ resolveStreamingFlushInterval,
24
+ };
package/blun.mjs CHANGED
@@ -261553,6 +261553,8 @@ var init_turn = __esmMin((() => {
261553
261553
  currentStep = 0;
261554
261554
  cognitiveLifecycle;
261555
261555
  cognitiveLifecycleUnavailable = false;
261556
+ cognitiveToolPolicyByCall = /* @__PURE__ */ new Map();
261557
+ cognitiveToolBatchesByTurn = /* @__PURE__ */ new Map();
261556
261558
  constructor(agent) {
261557
261559
  this.agent = agent;
261558
261560
  }
@@ -261587,6 +261589,40 @@ var init_turn = __esmMin((() => {
261587
261589
  this.agent.telemetry.track("cognitive_lifecycle_error", { stage: method, error_type: error?.code ?? error?.name ?? "Error" });
261588
261590
  }
261589
261591
  }
261592
+ bufferCognitiveToolPolicy(input) {
261593
+ this.cognitiveToolPolicyByCall.set(input.toolCallId, input);
261594
+ }
261595
+ bufferCognitiveToolResult(input) {
261596
+ const policy = this.cognitiveToolPolicyByCall.get(input.toolCallId);
261597
+ if (policy === void 0 || policy.turnId !== input.turnId) {
261598
+ this.recordCognitiveStage("recordToolResult", input);
261599
+ return;
261600
+ }
261601
+ this.cognitiveToolPolicyByCall.delete(input.toolCallId);
261602
+ const entries = this.cognitiveToolBatchesByTurn.get(input.turnId) ?? [];
261603
+ entries.push({
261604
+ toolCallId: input.toolCallId,
261605
+ toolName: input.toolName,
261606
+ decision: policy.decision,
261607
+ outcome: input.outcome,
261608
+ durationMs: input.durationMs
261609
+ });
261610
+ this.cognitiveToolBatchesByTurn.set(input.turnId, entries);
261611
+ }
261612
+ flushCognitiveToolEvidence(turnId, includePending) {
261613
+ const entries = this.cognitiveToolBatchesByTurn.get(turnId) ?? [];
261614
+ this.cognitiveToolBatchesByTurn.delete(turnId);
261615
+ for (let offset = 0; offset < entries.length; offset += 10) this.recordCognitiveStage("recordToolBatch", {
261616
+ turnId,
261617
+ entries: entries.slice(offset, offset + 10)
261618
+ });
261619
+ if (!includePending) return;
261620
+ for (const [toolCallId, policy] of this.cognitiveToolPolicyByCall) {
261621
+ if (policy.turnId !== turnId) continue;
261622
+ this.cognitiveToolPolicyByCall.delete(toolCallId);
261623
+ this.recordCognitiveStage("recordToolPolicy", policy);
261624
+ }
261625
+ }
261590
261626
  projectCognitiveState(turnId) {
261591
261627
  try {
261592
261628
  return this.getCognitiveLifecycle()?.projectForTurn({ turnId }) ?? null;
@@ -261948,6 +261984,7 @@ var init_turn = __esmMin((() => {
261948
261984
  durationMs: Date.now() - startedAt
261949
261985
  };
261950
261986
  this.agent.usage.endTurn();
261987
+ this.flushCognitiveToolEvidence(turnId, true);
261951
261988
  this.recordCognitiveStage("endTurn", { turnId, reason: ended.reason, durationMs: ended.durationMs ?? 0 });
261952
261989
  this.agent.emitEvent(ended);
261953
261990
  return ended;
@@ -262062,6 +262099,7 @@ var init_turn = __esmMin((() => {
262062
262099
  mode: this.telemetryModeByTurn.get(turnId) ?? this.telemetryMode(),
262063
262100
  ...this.requestProviderProps()
262064
262101
  });
262102
+ this.flushCognitiveToolEvidence(turnId, true);
262065
262103
  this.recordCognitiveStage("endTurn", { turnId, reason: ended.reason, durationMs: ended.durationMs ?? 0 });
262066
262104
  this.agent.emitEvent(ended);
262067
262105
  this.agent.endResponderTurn();
@@ -262270,6 +262308,7 @@ var init_turn = __esmMin((() => {
262270
262308
  },
262271
262309
  afterStep: async ({ usage }) => {
262272
262310
  previousStepToolOutcome = currentStepHadTool ? currentStepHadFailure ? "failure" : "success" : "none";
262311
+ this.flushCognitiveToolEvidence(turnId, false);
262273
262312
  this.agent.usage.record(this.agent.activeResponderModel ?? model, usage, "turn");
262274
262313
  if (stopForGoalBudget) this.setActiveSteerAcceptance(turnId, false);
262275
262314
  await this.agent.toolResultBatchOffload.detect();
@@ -262322,7 +262361,7 @@ var init_turn = __esmMin((() => {
262322
262361
  authorizeToolExecution: async (ctx) => {
262323
262362
  try {
262324
262363
  const resolution = await this.agent.permission.beforeToolCall(ctx);
262325
- this.recordCognitiveStage("recordToolPolicy", {
262364
+ this.bufferCognitiveToolPolicy({
262326
262365
  turnId,
262327
262366
  toolCallId: ctx.toolCall.id,
262328
262367
  toolName: ctx.toolCall.name,
@@ -262330,7 +262369,7 @@ var init_turn = __esmMin((() => {
262330
262369
  });
262331
262370
  return resolution;
262332
262371
  } catch (error) {
262333
- this.recordCognitiveStage("recordToolPolicy", {
262372
+ this.bufferCognitiveToolPolicy({
262334
262373
  turnId,
262335
262374
  toolCallId: ctx.toolCall.id,
262336
262375
  toolName: ctx.toolCall.name,
@@ -262480,7 +262519,7 @@ var init_turn = __esmMin((() => {
262480
262519
  };
262481
262520
  const errorType = outcome === "error" ? telemetryToolErrorType(event.result) : void 0;
262482
262521
  if (errorType !== void 0) properties["error_type"] = errorType;
262483
- this.recordCognitiveStage("recordToolResult", {
262522
+ this.bufferCognitiveToolResult({
262484
262523
  turnId,
262485
262524
  toolCallId: event.toolCallId,
262486
262525
  toolName: started.name,
@@ -511375,7 +511414,11 @@ var StreamingUIController = class {
511375
511414
  scheduleFlush() {
511376
511415
  if (!this.hasPending()) return;
511377
511416
  if (this.flushTimer !== void 0) return;
511378
- const delay = this.lastFlushAt === void 0 ? 0 : Math.max(0, 50 - (Date.now() - this.lastFlushAt));
511417
+ const flushIntervalMs = resolveStreamingFlushInterval({
511418
+ assistantChars: this._assistantDraft.length,
511419
+ thinkingChars: this._thinkingDraft.length
511420
+ });
511421
+ const delay = this.lastFlushAt === void 0 ? 0 : Math.max(0, flushIntervalMs - (Date.now() - this.lastFlushAt));
511379
511422
  this.flushTimer = setTimeout(() => {
511380
511423
  this.flushTimer = void 0;
511381
511424
  this.flush();
@@ -515874,6 +515917,7 @@ const {
515874
515917
  readRunningUpdateMode,
515875
515918
  writeRunningUpdateMode
515876
515919
  } = __require("./bin/running-update-preference.cjs");
515920
+ const { resolveStreamingFlushInterval } = __require("./bin/streaming-flush-performance-policy.cjs");
515877
515921
  const RUNNING_UPDATE_RESUME_SESSION_ENV = "BLUN_RUNNING_UPDATE_RESUME_SESSION_ID";
515878
515922
  function shouldContinueActiveGoalAfterRunningUpdate(expectedSessionId, currentSessionId, goalStatus) {
515879
515923
  return typeof expectedSessionId === "string" && expectedSessionId.length > 0 && currentSessionId === expectedSessionId && goalStatus === "active";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blun-king-cli",
3
- "version": "9.1.343",
3
+ "version": "9.1.345",
4
4
  "description": "BLUN CLI - your own AI agent with a Telegram channel. Get it done. With BLUN.",
5
5
  "license": "MIT",
6
6
  "bin": {