2026-03-18 17:31:27 +09:00
|
|
|
import type { PluginInput } from "@opencode-ai/plugin"
|
2026-04-16 13:52:12 +09:00
|
|
|
import { extractTaskLink } from "../../features/tool-metadata-store"
|
2026-03-18 17:31:27 +09:00
|
|
|
import { log } from "../../shared/logger"
|
|
|
|
|
import { isSessionInBoulderLineage } from "./boulder-session-lineage"
|
|
|
|
|
import { HOOK_NAME } from "./hook-name"
|
|
|
|
|
|
2026-03-27 12:05:00 +08:00
|
|
|
export function extractSessionIdFromMetadata(metadata: unknown): string | undefined {
|
2026-04-16 13:52:12 +09:00
|
|
|
const sessionId = extractTaskLink(metadata, "").sessionId
|
|
|
|
|
if (typeof sessionId === "string" && sessionId.startsWith("ses_")) {
|
|
|
|
|
return sessionId
|
2026-03-27 12:05:00 +08:00
|
|
|
}
|
2026-04-16 13:52:12 +09:00
|
|
|
|
2026-03-27 12:05:00 +08:00
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 17:25:46 +09:00
|
|
|
export function extractSessionIdFromOutput(output: string): string | undefined {
|
2026-04-16 13:52:12 +09:00
|
|
|
return extractTaskLink(undefined, output).sessionId
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|
2026-03-18 17:31:27 +09:00
|
|
|
|
|
|
|
|
export async function validateSubagentSessionId(input: {
|
|
|
|
|
client: PluginInput["client"]
|
|
|
|
|
sessionID?: string
|
|
|
|
|
lineageSessionIDs: string[]
|
|
|
|
|
}): Promise<string | undefined> {
|
|
|
|
|
if (!input.sessionID || input.lineageSessionIDs.length === 0) {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const belongsToLineage = await isSessionInBoulderLineage({
|
|
|
|
|
client: input.client,
|
|
|
|
|
sessionID: input.sessionID,
|
|
|
|
|
boulderSessionIDs: input.lineageSessionIDs,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!belongsToLineage) {
|
|
|
|
|
log(`[${HOOK_NAME}] Ignoring extracted session id outside active lineage`, {
|
|
|
|
|
sessionID: input.sessionID,
|
|
|
|
|
lineageSessionIDs: input.lineageSessionIDs,
|
|
|
|
|
})
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return input.sessionID
|
|
|
|
|
}
|