mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
fix(deploy): gate unavailable agents and bound session scans
Co-Authored-By: Codex <noreply@anthropic.com>
This commit is contained in:
@@ -136,6 +136,33 @@ describe('listLocalCodexSessionSummaries', () => {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('keeps the summary scan bounded for large transcripts while retaining the tail preview', () => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'codex-home-'))
|
||||
process.env.CODEX_HOME = root
|
||||
const sessionsDir = join(root, 'sessions', '2026', '07', '19')
|
||||
mkdirSync(sessionsDir, { recursive: true })
|
||||
const sessionFile = join(sessionsDir, 'large.jsonl')
|
||||
const lastMessage = JSON.stringify({
|
||||
type: 'response_item',
|
||||
payload: { type: 'message', role: 'user', content: [{ type: 'input_text', text: 'last prompt' }] }
|
||||
})
|
||||
writeFileSync(sessionFile, [
|
||||
JSON.stringify({ type: 'session_meta', payload: { id: 'large-session-id', cwd: '/tmp/project' } }),
|
||||
JSON.stringify({ type: 'response_item', payload: { type: 'message', role: 'user', content: [{ type: 'input_text', text: 'first prompt' }] } }),
|
||||
'x'.repeat(96 * 1024),
|
||||
lastMessage
|
||||
].join('\n'))
|
||||
|
||||
expect(listLocalCodexSessionSummaries()).toEqual([
|
||||
expect.objectContaining({
|
||||
id: 'large-session-id',
|
||||
lastUserMessage: 'last prompt'
|
||||
})
|
||||
])
|
||||
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('skips subagent transcripts', () => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'codex-home-'))
|
||||
process.env.CODEX_HOME = root
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, statSync } from 'node:fs'
|
||||
import { closeSync, existsSync, fstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, renameSync, statSync } from 'node:fs'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { basename, dirname, join, relative } from 'node:path'
|
||||
import { homedir } from 'node:os'
|
||||
@@ -6,6 +6,7 @@ import { AGENT_MESSAGE_PAYLOAD_TYPE } from '@hapi/protocol'
|
||||
import { isCodexSubagentSource } from '@/codex/utils/codexSessionMetadata'
|
||||
|
||||
const DEFAULT_CODEX_SESSION_SCAN_LIMIT = 200
|
||||
const CODEX_SESSION_SUMMARY_READ_BYTES = 64 * 1024
|
||||
|
||||
type CodexSessionIndexTitle = {
|
||||
threadName: string
|
||||
@@ -142,6 +143,39 @@ function readCodexSessionIndexTitles(): Map<string, CodexSessionIndexTitle> {
|
||||
return titles
|
||||
}
|
||||
|
||||
function readFileChunk(descriptor: number, position: number, length: number): string {
|
||||
const buffer = Buffer.alloc(length)
|
||||
const bytesRead = readSync(descriptor, buffer, 0, length, position)
|
||||
return buffer.toString('utf-8', 0, bytesRead)
|
||||
}
|
||||
|
||||
function readCodexSessionSummaryContent(filePath: string): { head: string; tail: string; modifiedAt: number } | null {
|
||||
let descriptor: number
|
||||
try {
|
||||
descriptor = openSync(filePath, 'r')
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
const fileStats = fstatSync(descriptor)
|
||||
const modifiedAt = fileStats.mtimeMs
|
||||
const fileSize = fileStats.size
|
||||
const headLength = Math.min(fileSize, CODEX_SESSION_SUMMARY_READ_BYTES)
|
||||
const head = readFileChunk(descriptor, 0, headLength)
|
||||
if (fileSize <= CODEX_SESSION_SUMMARY_READ_BYTES) {
|
||||
return { head, tail: head, modifiedAt }
|
||||
}
|
||||
|
||||
const tail = readFileChunk(descriptor, fileSize - CODEX_SESSION_SUMMARY_READ_BYTES, CODEX_SESSION_SUMMARY_READ_BYTES)
|
||||
return { head, tail, modifiedAt }
|
||||
} catch {
|
||||
return null
|
||||
} finally {
|
||||
closeSync(descriptor)
|
||||
}
|
||||
}
|
||||
|
||||
function extractCodexChangedTitle(record: Record<string, unknown>): string | null {
|
||||
if (record.type === 'response_item') {
|
||||
const payload = asRecord(record.payload)
|
||||
@@ -304,9 +338,24 @@ function parseCodexLocalSession(
|
||||
includeMessages: boolean,
|
||||
sessionIndexTitles = new Map<string, CodexSessionIndexTitle>()
|
||||
): LocalCodexSessionWithMessages | LocalCodexSessionSummary | null {
|
||||
let content: string
|
||||
try { content = readFileSync(filePath, 'utf-8') } catch { return null }
|
||||
const lines = content.split(/\r?\n/).filter(Boolean)
|
||||
let lines: string[]
|
||||
let summaryTailLines: string[]
|
||||
let modifiedAt = Date.now()
|
||||
if (includeMessages) {
|
||||
let content: string
|
||||
try {
|
||||
content = readFileSync(filePath, 'utf-8')
|
||||
modifiedAt = statSync(filePath).mtimeMs
|
||||
} catch { return null }
|
||||
lines = content.split(/\r?\n/).filter(Boolean)
|
||||
summaryTailLines = lines
|
||||
} else {
|
||||
const content = readCodexSessionSummaryContent(filePath)
|
||||
if (!content) return null
|
||||
lines = content.head.split(/\r?\n/).filter(Boolean)
|
||||
summaryTailLines = content.tail.split(/\r?\n/).filter(Boolean)
|
||||
modifiedAt = content.modifiedAt
|
||||
}
|
||||
const headLines = lines.slice(0, 200)
|
||||
let sessionId: string | null = null
|
||||
let cwd: string | null = null
|
||||
@@ -356,10 +405,8 @@ function parseCodexLocalSession(
|
||||
sessionId = sessionId ?? inferSessionIdFromFileName(filePath)
|
||||
if (!sessionId) return null
|
||||
const sessionIndexTitle = sessionIndexTitles.get(sessionId)?.threadName ?? null
|
||||
const changedTitle = getLatestCodexChangedTitle(lines)
|
||||
const lastUserMessage = getLatestCodexUserMessage(lines)
|
||||
let modifiedAt = Date.now()
|
||||
try { modifiedAt = statSync(filePath).mtimeMs } catch {}
|
||||
const changedTitle = getLatestCodexChangedTitle(summaryTailLines)
|
||||
const lastUserMessage = getLatestCodexUserMessage(summaryTailLines)
|
||||
const summary = {
|
||||
id: sessionId,
|
||||
title: getCodexSessionTitle(cwd, sessionId, sessionIndexTitle, changedTitle, firstUserMessage),
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { listCopilotModelsForCwd } from './copilotModels'
|
||||
|
||||
describe('Copilot model discovery', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs()
|
||||
})
|
||||
|
||||
it('returns an availability error without spawning a missing Copilot CLI', async () => {
|
||||
vi.stubEnv('COPILOT_CLI_PATH', '/definitely/missing/copilot')
|
||||
|
||||
await expect(listCopilotModelsForCwd('/tmp/hapi-copilot-unavailable'))
|
||||
.resolves.toMatchObject({
|
||||
success: false,
|
||||
error: 'Copilot CLI is not installed or not on PATH',
|
||||
availableModels: [],
|
||||
currentModelId: null,
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -3,6 +3,7 @@ import { createMessageConnection, StreamMessageReader, StreamMessageWriter } fro
|
||||
import { asString, isObject } from '@hapi/protocol';
|
||||
import type { CopilotModelsResponse, CopilotModelSummary } from '@hapi/protocol/apiTypes';
|
||||
import { createCopilotBackend } from '@/copilot/utils/copilotBackend';
|
||||
import { getAgentAvailability } from '@/agent/agentAvailability';
|
||||
import { getErrorMessage } from './rpcResponses';
|
||||
|
||||
export interface ListCopilotModelsForCwdRequest {
|
||||
@@ -167,6 +168,16 @@ async function listModelsViaAcpProbe(cwd: string): Promise<{
|
||||
}
|
||||
|
||||
async function runCopilotProbe(cwd: string): Promise<ListCopilotModelsForCwdResponse> {
|
||||
const availability = getAgentAvailability('copilot');
|
||||
if (!availability.available) {
|
||||
return {
|
||||
success: false,
|
||||
error: `Copilot CLI ${availability.reason === 'invalid_configuration' ? 'configuration is invalid' : 'is not installed or not on PATH'}`,
|
||||
availableModels: [],
|
||||
currentModelId: null,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
// Prefer SDK headless models.list — subscription-aware (Student → auto only).
|
||||
const sdkModels = await listModelsViaSdkHeadless();
|
||||
|
||||
Reference in New Issue
Block a user