Skip to content

Commit 87b9619

Browse files
feat: ai 的随机修复
1 parent 18d6656 commit 87b9619

8 files changed

Lines changed: 217 additions & 2 deletions

File tree

build.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ const result = await Bun.build({
2121
outdir,
2222
target: 'bun',
2323
splitting: true,
24-
define: getMacroDefines(),
24+
define: {
25+
...getMacroDefines(),
26+
// React production mode — eliminates _debugStack Error objects
27+
// (6,889 objects × ~1.7KB = 12MB in development builds) and removes
28+
// prop-type / key warnings not useful in a production CLI tool.
29+
'process.env.NODE_ENV': JSON.stringify('production'),
30+
},
2531
features,
2632
})
2733

scripts/dev.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ const __dirname = dirname(__filename)
1414
const projectRoot = join(__dirname, '..')
1515
const cliPath = join(projectRoot, 'src/entrypoints/cli.tsx')
1616

17-
const defines = getMacroDefines()
17+
const defines = {
18+
...getMacroDefines(),
19+
// React production mode — prevents 6,889+ _debugStack Error objects
20+
// (12MB) from accumulating during long-running sessions.
21+
'process.env.NODE_ENV': JSON.stringify('production'),
22+
}
1823

1924
const defineArgs = Object.entries(defines).flatMap(([k, v]) => [
2025
'-d',

src/entrypoints/cli.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#!/usr/bin/env bun
2+
// Performance shim MUST be the first import — it replaces globalThis.performance
3+
// with a JS-backed implementation before React/OTel capture the native reference.
4+
// Without this, JSC's C++ Vector grows without bound in long-running sessions.
5+
import '../utils/performanceShim.js';
26
import { feature } from 'bun:bundle';
37
import { isEnvTruthy } from '../utils/envUtils.js';
48

src/query.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ import { count } from './utils/array.js'
124124
import {
125125
createTrace,
126126
endTrace,
127+
flushLangfuse,
127128
isLangfuseEnabled,
128129
} from './services/langfuse/index.js'
129130
import { getAPIProvider } from './utils/model/providers.js'
@@ -339,6 +340,11 @@ export async function* query(
339340
terminal?.reason === 'aborted_streaming' ||
340341
terminal?.reason === 'aborted_tools'
341342
endTrace(langfuseTrace, undefined, isAborted ? 'interrupted' : undefined)
343+
// Flush the processor to release span data (including serialized
344+
// conversation history stored as langfuse.observation.input). Without
345+
// this, SpanImpl objects retain hundreds of KB of JSON until the
346+
// processor's batch timer fires (default 10s).
347+
await flushLangfuse()
342348
}
343349

344350
// Break the closure chain: toolUseContext captures langfuseTrace which
@@ -349,6 +355,21 @@ export async function* query(
349355
paramsWithTrace.toolUseContext.langfuseRootTrace = null
350356
paramsWithTrace.toolUseContext.langfuseBatchSpan = null
351357
}
358+
359+
// Clear JSC's native Performance buffers. OTel (otperformance) references
360+
// globalThis.performance which stores marks/measures/resource timings in a
361+
// C++ Vector that never shrinks. Long-running sessions accumulate hundreds
362+
// of MB of dead capacity even after spans are flushed and nullified.
363+
const gPerf = globalThis.performance
364+
if (gPerf && typeof gPerf.clearMarks === 'function') {
365+
try {
366+
gPerf.clearMarks()
367+
gPerf.clearMeasures?.()
368+
gPerf.clearResourceTimings?.()
369+
} catch {
370+
// Non-critical — some environments may not support all methods
371+
}
372+
}
352373
}
353374

354375
// Only reached if queryLoop returned normally. Skipped on throw (error

src/services/langfuse/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ export function initLangfuse(): boolean {
6161
}
6262
}
6363

64+
export async function flushLangfuse(): Promise<void> {
65+
try {
66+
if (processor) {
67+
await processor.forceFlush()
68+
}
69+
} catch (e) {
70+
logForDebugging(`[langfuse] Flush error: ${e}`, { level: 'error' })
71+
}
72+
}
73+
6474
export async function shutdownLangfuse(): Promise<void> {
6575
try {
6676
if (processor) {

src/services/langfuse/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {
22
initLangfuse,
33
shutdownLangfuse,
4+
flushLangfuse,
45
isLangfuseEnabled,
56
getLangfuseProcessor,
67
} from './client.js'

src/utils/performanceShim.ts

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* Performance shim — replaces globalThis.performance to prevent JSC's C++ Vector
3+
* from growing without bound.
4+
*
5+
* In Bun, globalThis.performance is JSC's native Performance object. It stores
6+
* marks, measures, and resource timings in a C++ Vector that never shrinks even
7+
* after clearMarks(). Long-running sessions (daemon, /loop) accumulate hundreds
8+
* of MB of dead capacity.
9+
*
10+
* This shim keeps performance.now() on the native object (fast, no memory cost)
11+
* but redirects mark/measure/getEntries operations to a plain JS Map that the GC
12+
* can reclaim. Third-party code (React reconciler, OTel/Langfuse) uses
13+
* performance.now() for timing — that stays native. The accumulating operations
14+
* go to GC-able JS memory instead.
15+
*
16+
* MUST be installed before React/OTel import — see cli.tsx first import.
17+
*/
18+
19+
const original = globalThis.performance
20+
21+
// JS-backed storage — fully GC-able
22+
const marks = new Map<string, number>()
23+
const measures = new Map<
24+
string,
25+
{ name: string; startTime: number; duration: number }
26+
>()
27+
28+
function now(): number {
29+
return original.now()
30+
}
31+
32+
function mark(name: string): PerformanceMark {
33+
marks.set(name, now())
34+
// Return a minimal PerformanceMark-like object to satisfy the interface.
35+
// React/OTel only use mark() for side effects, not the return value.
36+
return {
37+
name,
38+
entryType: 'mark',
39+
startTime: marks.get(name)!,
40+
duration: 0,
41+
} as PerformanceMark
42+
}
43+
44+
function measure(
45+
name: string,
46+
startMarkOrOptions?: string | MeasureOptions,
47+
endMark?: string,
48+
): void {
49+
let startTime: number
50+
let duration: number
51+
52+
if (typeof startMarkOrOptions === 'string') {
53+
const start = marks.get(startMarkOrOptions)
54+
const end = endMark ? marks.get(endMark) : now()
55+
startTime = start ?? now()
56+
duration = (end ?? now()) - startTime
57+
} else if (startMarkOrOptions && typeof startMarkOrOptions === 'object') {
58+
startTime = startMarkOrOptions.start ?? 0
59+
duration = (startMarkOrOptions.end ?? now()) - startTime
60+
} else {
61+
startTime = 0
62+
duration = now()
63+
}
64+
65+
measures.set(name, { name, startTime, duration })
66+
}
67+
68+
interface MeasureOptions {
69+
start?: number
70+
end?: number
71+
detail?: unknown
72+
}
73+
74+
interface PerformanceEntryLike {
75+
readonly name: string
76+
readonly entryType: string
77+
readonly startTime: number
78+
readonly duration: number
79+
}
80+
81+
function getEntriesByType(type: string): PerformanceEntryLike[] {
82+
if (type === 'mark') {
83+
return [...marks.entries()].map(([name, startTime]) => ({
84+
name,
85+
entryType: 'mark',
86+
startTime,
87+
duration: 0,
88+
}))
89+
}
90+
if (type === 'measure') {
91+
return [...measures.values()].map(m => ({
92+
name: m.name,
93+
entryType: 'measure',
94+
startTime: m.startTime,
95+
duration: m.duration,
96+
}))
97+
}
98+
return []
99+
}
100+
101+
function getEntriesByName(name: string, type?: string): PerformanceEntryLike[] {
102+
const entries = getEntriesByType(type ?? 'mark').concat(
103+
type === undefined ? getEntriesByType('measure') : [],
104+
)
105+
return entries.filter(e => e.name === name)
106+
}
107+
108+
function clearMarks(name?: string): void {
109+
if (name !== undefined) {
110+
marks.delete(name)
111+
} else {
112+
marks.clear()
113+
}
114+
}
115+
116+
function clearMeasures(name?: string): void {
117+
if (name !== undefined) {
118+
measures.delete(name)
119+
} else {
120+
measures.clear()
121+
}
122+
}
123+
124+
// Plain object shim — must NOT inherit from Performance.prototype because
125+
// native getters (onresourcetimingbufferfull, timeOrigin, toJSON) check
126+
// that `this` is an actual JSC Performance instance and throw otherwise.
127+
const shim = {
128+
now,
129+
mark,
130+
measure: measure as typeof performance.measure,
131+
getEntriesByType: getEntriesByType as typeof performance.getEntriesByType,
132+
getEntriesByName: getEntriesByName as typeof performance.getEntriesByName,
133+
clearMarks: clearMarks as typeof performance.clearMarks,
134+
clearMeasures: clearMeasures as typeof performance.clearMeasures,
135+
clearResourceTimings: (() => {}) as typeof performance.clearResourceTimings,
136+
setResourceTimingBufferSize:
137+
(() => {}) as typeof performance.setResourceTimingBufferSize,
138+
// Delegate read-only properties to the original
139+
get timeOrigin() {
140+
return original.timeOrigin
141+
},
142+
get onresourcetimingbufferfull() {
143+
return (original as any).onresourcetimingbufferfull
144+
},
145+
set onresourcetimingbufferfull(_v: any) {
146+
// no-op — prevent accumulation
147+
},
148+
toJSON() {
149+
return original.toJSON()
150+
},
151+
} as typeof performance
152+
153+
/**
154+
* Install the shim onto globalThis.performance. Safe to call multiple times.
155+
* Must run before React and OTel import to prevent them from capturing the
156+
* native Performance reference.
157+
*/
158+
export function installPerformanceShim(): void {
159+
if ((globalThis as any).__performanceShimInstalled) return
160+
;(globalThis as any).__performanceShimInstalled = true
161+
globalThis.performance = shim
162+
}
163+
164+
// Auto-install on import
165+
installPerformanceShim()

vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ export default defineConfig({
116116
// Compile-time constant replacement (MACRO.* defines)
117117
define: {
118118
...getMacroDefines(),
119+
// React production mode — eliminates _debugStack Error objects
120+
// (6,889 objects × ~1.7KB = 12MB in development builds)
121+
'process.env.NODE_ENV': JSON.stringify('production'),
119122
},
120123

121124
resolve: {

0 commit comments

Comments
 (0)