惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
V
Visual Studio Blog
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 司徒正美
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
月光博客
月光博客

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
feat(agents): trace compaction summarization model calls ...
amknight · 2026-06-17 · via Recent Commits to openclaw:main

@@ -85,6 +85,7 @@ type ModelCallObservationState = {

8585

outputMessages?: unknown[];

8686

contentCapture?: DiagnosticModelContentCapturePolicy;

8787

lastStreamProgressAt?: number;

88+

terminalEventEmitted?: boolean;

8889

};

89909091

const MODEL_CALL_STREAM_PROGRESS_INTERVAL_MS = 30_000;

@@ -184,6 +185,23 @@ function observeOutputMessageContent(state: ModelCallObservationState, chunk: un

184185

}

185186

}

186187188+

function observeResultMessageContent(

189+

state: ModelCallObservationState,

190+

startedAt: number,

191+

result: unknown,

192+

): void {

193+

state.timeToFirstByteMs ??= Math.max(0, Date.now() - startedAt);

194+

if (state.contentCapture?.outputMessages && state.outputMessages === undefined) {

195+

state.outputMessages = [cloneDiagnosticContentValue(result)];

196+

}

197+

if (state.responseStreamBytes === 0) {

198+

const bytes = utf8JsonByteLength(result);

199+

if (bytes !== undefined) {

200+

state.responseStreamBytes = bytes;

201+

}

202+

}

203+

}

204+187205

function observeResponseChunk(

188206

state: ModelCallObservationState,

189207

startedAt: number,

@@ -419,6 +437,10 @@ function emitModelCallCompleted(

419437

startedAt: number,

420438

state: ModelCallObservationState,

421439

): void {

440+

if (state.terminalEventEmitted) {

441+

return;

442+

}

443+

state.terminalEventEmitted = true;

422444

const durationMs = Date.now() - startedAt;

423445

const sizeTimingFields = modelCallSizeTimingFields(state);

424446

emitTrustedDiagnosticEventWithPrivateData(

@@ -443,6 +465,10 @@ function emitModelCallError(

443465

state: ModelCallObservationState,

444466

fields: ModelCallErrorFields,

445467

): void {

468+

if (state.terminalEventEmitted) {

469+

return;

470+

}

471+

state.terminalEventEmitted = true;

446472

const durationMs = Date.now() - startedAt;

447473

const sizeTimingFields = modelCallSizeTimingFields(state);

448474

emitTrustedDiagnosticEventWithPrivateData(

@@ -548,33 +574,80 @@ async function* observeModelCallIterator<T>(

548574

startedAt: number,

549575

state: ModelCallObservationState,

550576

): AsyncIterable<T> {

551-

let terminalEmitted = false;

577+

// Tracks whether the underlying iterator terminated on its own (done or threw).

578+

// This is independent of state.terminalEventEmitted: result() can emit the

579+

// terminal event first, but the abandoned iterator still needs return() cleanup.

580+

let iteratorSettled = false;

552581

try {

553582

for (;;) {

554583

const next = await iterator.next();

555584

if (next.done) {

585+

iteratorSettled = true;

556586

break;

557587

}

558588

observeResponseChunk(state, startedAt, next.value);

559589

maybeEmitModelCallStreamProgress(eventBase, state);

560590

yield next.value;

561591

}

562-

terminalEmitted = true;

563592

emitModelCallCompleted(eventBase, startedAt, state);

564593

} catch (err) {

565-

terminalEmitted = true;

594+

iteratorSettled = true;

566595

emitModelCallError(eventBase, startedAt, state, modelCallErrorFields(err));

567596

throw err;

568597

} finally {

569-

if (!terminalEmitted) {

570-

// A consumer can stop reading before the provider emits done/error. Close

571-

// the iterator best-effort and record the call as completed with observed bytes.

598+

if (!iteratorSettled) {

599+

// A consumer can stop reading before the provider emits done/error — e.g.

600+

// the agent loop returns on the terminal event after awaiting result().

601+

// Close the underlying iterator for provider cleanup (idle-timeout abort

602+

// listeners, SSE readers) even when result() already emitted the terminal

603+

// event; emitModelCallCompleted self-dedupes via state.terminalEventEmitted.

572604

await safeReturnIterator(iterator);

573605

emitModelCallCompleted(eventBase, startedAt, state);

574606

}

575607

}

576608

}

577609610+

function observeModelCallFinalResult<T>(

611+

result: T,

612+

eventBase: ModelCallEventBase,

613+

startedAt: number,

614+

state: ModelCallObservationState,

615+

): T {

616+

observeResultMessageContent(state, startedAt, result);

617+

emitModelCallCompleted(eventBase, startedAt, state);

618+

return result;

619+

}

620+621+

function createObservedResultFunction(

622+

stream: unknown,

623+

eventBase: ModelCallEventBase,

624+

startedAt: number,

625+

state: ModelCallObservationState,

626+

): ((...args: unknown[]) => unknown) | undefined {

627+

if (!isRecord(stream) || typeof stream.result !== "function") {

628+

return undefined;

629+

}

630+

const resultFn = stream.result;

631+

return (...args: unknown[]) => {

632+

try {

633+

const result = resultFn.apply(stream, args);

634+

if (isPromiseLike(result)) {

635+

return result.then(

636+

(resolved) => observeModelCallFinalResult(resolved, eventBase, startedAt, state),

637+

(err: unknown) => {

638+

emitModelCallError(eventBase, startedAt, state, modelCallErrorFields(err));

639+

throw err;

640+

},

641+

);

642+

}

643+

return observeModelCallFinalResult(result, eventBase, startedAt, state);

644+

} catch (err) {

645+

emitModelCallError(eventBase, startedAt, state, modelCallErrorFields(err));

646+

throw err;

647+

}

648+

};

649+

}

650+578651

function observeModelCallStream<T extends AsyncIterable<unknown>>(

579652

stream: T,

580653

createIterator: () => AsyncIterator<unknown>,

@@ -584,6 +657,7 @@ function observeModelCallStream<T extends AsyncIterable<unknown>>(

584657

): T {

585658

const observedIterator = () =>

586659

observeModelCallIterator(createIterator(), eventBase, startedAt, state)[Symbol.asyncIterator]();

660+

const observedResult = createObservedResultFunction(stream, eventBase, startedAt, state);

587661

let hasNonConfigurableIterator;

588662

try {

589663

hasNonConfigurableIterator =

@@ -594,13 +668,17 @@ function observeModelCallStream<T extends AsyncIterable<unknown>>(

594668

if (hasNonConfigurableIterator) {

595669

return {

596670

[Symbol.asyncIterator]: observedIterator,

671+

...(observedResult ? { result: observedResult } : {}),

597672

} as T;

598673

}

599674

return new Proxy(stream, {

600675

get(target, property, receiver) {

601676

if (property === Symbol.asyncIterator) {

602677

return observedIterator;

603678

}

679+

if (property === "result" && observedResult) {

680+

return observedResult;

681+

}

604682

const value = Reflect.get(target, property, receiver);

605683

return typeof value === "function" ? value.bind(target) : value;

606684

},