Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,11 +849,15 @@ export interface KataGoMoveAnalysis {
before: {
winrate: number
scoreLead: number
ownership?: number[]
ownershipStdev?: number[]
topMoves: KataGoCandidate[]
}
after: {
winrate: number
scoreLead: number
ownership?: number[]
ownershipStdev?: number[]
topMoves: KataGoCandidate[]
}
playedMove?: {
Expand Down
15 changes: 15 additions & 0 deletions src/main/services/analysis/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface AnalysisCacheRequirement {
minBestVisits?: number
minActualVisits?: number
minTier?: AnalysisCacheTier
requireOwnership?: boolean
requireStablePv?: boolean
requireMediumConfidence?: boolean
allowStaleMs?: number
Expand Down Expand Up @@ -152,6 +153,9 @@ function cacheEntryMeetsRequirement(entry: AnalysisCacheEntry, requirement: Anal
if (requirement.minActualVisits && entry.quality.actualVisits < requirement.minActualVisits) {
return { status: 'lower-quality', entry, reason: `actualVisits ${entry.quality.actualVisits} is below ${requirement.minActualVisits}` }
}
if (requirement.requireOwnership && !hasRootOwnership(entry.analysis)) {
return { status: 'lower-quality', entry, reason: 'cached analysis does not include root ownership' }
}
if (requirement.requireStablePv && pvRank[entry.quality.pvConfidence ?? 'unstable'] < pvRank.medium) {
return { status: 'lower-quality', entry, reason: `PV confidence ${entry.quality.pvConfidence ?? 'unknown'} is not stable enough` }
}
Expand All @@ -161,6 +165,17 @@ function cacheEntryMeetsRequirement(entry: AnalysisCacheEntry, requirement: Anal
return null
}

function hasRootOwnership(analysis: KataGoMoveAnalysis): boolean {
const boardSize = Math.max(2, Math.round(analysis.boardSize || 19))
const expected = boardSize * boardSize
const afterOwnership = analysis.after.ownership
const beforeOwnership = analysis.moveNumber === 0 ? analysis.before.ownership : undefined
return Boolean(
(Array.isArray(afterOwnership) && afterOwnership.length >= expected) ||
(Array.isArray(beforeOwnership) && beforeOwnership.length >= expected)
)
}

export function readAnalysisCache(input: AnalysisCacheKeyInput, requirement: AnalysisCacheRequirement = {}): AnalysisCacheLookupResult {
const path = analysisCachePath(input)
if (!existsSync(path)) return { status: 'miss', path, reason: 'cache file does not exist' }
Expand Down
1 change: 1 addition & 0 deletions src/main/services/analysis/runtimeIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function cacheRequirementForProfile(profile: AdaptiveAnalysisProfile): AnalysisC
minTier: profile.cacheTier,
minBestVisits,
minActualVisits: teaching ? Math.max(60, Math.min(profile.maxVisits, Math.round(profile.maxVisits * 0.12))) : undefined,
requireOwnership: profile.includeOwnership,
requireStablePv: teaching,
requireMediumConfidence: teaching,
allowStaleMs: profile.cacheTier === 'oracle' ? -1 : 1000 * 60 * 60 * 24 * 30
Expand Down
8 changes: 6 additions & 2 deletions src/main/services/katago.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ interface KataGoResponse {
scoreStdev?: number
utility?: number
scoreMean?: number
ownership?: number[]
ownershipStdev?: number[]
}
moveInfos?: Array<{
move?: string
Expand Down Expand Up @@ -265,7 +267,7 @@ function responseSideToMove(response: KataGoResponse, fallback: GameMove['color'
: fallback
}

function root(response: KataGoResponse, sideToMove: GameMove['color']): { winrate: number; scoreLead: number } {
function root(response: KataGoResponse, sideToMove: GameMove['color']): { winrate: number; scoreLead: number; ownership?: number[]; ownershipStdev?: number[] } {
if (!response.rootInfo) {
throw new Error(`KataGo 没有返回 rootInfo${response.error ? `: ${response.error}` : ''}`)
}
Expand All @@ -274,7 +276,9 @@ function root(response: KataGoResponse, sideToMove: GameMove['color']): { winrat
const rawScoreLead = Number(response.rootInfo.scoreLead ?? response.rootInfo.scoreMean ?? 0)
return {
winrate: blackWinrateFromSideToMove(rawWinrate, actualSideToMove),
scoreLead: blackScoreLeadFromSideToMove(rawScoreLead, actualSideToMove)
scoreLead: blackScoreLeadFromSideToMove(rawScoreLead, actualSideToMove),
ownership: Array.isArray(response.rootInfo.ownership) ? response.rootInfo.ownership.map(Number) : undefined,
ownershipStdev: Array.isArray(response.rootInfo.ownershipStdev) ? response.rootInfo.ownershipStdev.map(Number) : undefined
}
}

Expand Down
Loading
Loading