fix(prometheus): prevent agent fallback to build in background tasks (#695)

This commit is contained in:
Nguyen Khac Trung Kien
2026-01-13 07:39:25 +07:00
committed by GitHub
parent 2314a0d371
commit 8d65748ad3
8 changed files with 139 additions and 9 deletions
@@ -9,3 +9,23 @@ export function setMainSession(id: string | undefined) {
export function getMainSessionID(): string | undefined {
return mainSessionID
}
const sessionAgentMap = new Map<string, string>()
export function setSessionAgent(sessionID: string, agent: string): void {
if (!sessionAgentMap.has(sessionID)) {
sessionAgentMap.set(sessionID, agent)
}
}
export function updateSessionAgent(sessionID: string, agent: string): void {
sessionAgentMap.set(sessionID, agent)
}
export function getSessionAgent(sessionID: string): string | undefined {
return sessionAgentMap.get(sessionID)
}
export function clearSessionAgent(sessionID: string): void {
sessionAgentMap.delete(sessionID)
}
+1 -1
View File
@@ -1,4 +1,4 @@
export { injectHookMessage, findNearestMessageWithFields } from "./injector"
export { injectHookMessage, findNearestMessageWithFields, findFirstMessageWithAgent } from "./injector"
export type { StoredMessage } from "./injector"
export type { MessageMeta, OriginalMessageContext, TextPart } from "./types"
export { MESSAGE_STORAGE } from "./constants"
@@ -48,6 +48,35 @@ export function findNearestMessageWithFields(messageDir: string): StoredMessage
return null
}
/**
* Finds the FIRST (oldest) message in the session with agent field.
* This is used to get the original agent that started the session,
* avoiding issues where newer messages may have a different agent
* due to OpenCode's internal agent switching.
*/
export function findFirstMessageWithAgent(messageDir: string): string | null {
try {
const files = readdirSync(messageDir)
.filter((f) => f.endsWith(".json"))
.sort() // Oldest first (no reverse)
for (const file of files) {
try {
const content = readFileSync(join(messageDir, file), "utf-8")
const msg = JSON.parse(content) as StoredMessage
if (msg.agent) {
return msg.agent
}
} catch {
continue
}
}
} catch {
return null
}
return null
}
function generateMessageId(): string {
const timestamp = Date.now().toString(16)
const random = Math.random().toString(36).substring(2, 14)