mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-26 05:27:06 +00:00
fix: preserve deployed session fixes
Persist paged Codex history checkpoints and bounded, acknowledged delivery. Keep usage sources and accounting across deleted or merged sessions. Include native Goal controls, faster peer/session queries, and release guards. Track the complete deployed source so isolated builds cannot omit local fixes. Validation: all typechecks pass. The full suite reports 7,997 passing tests and 46 existing failures, each reproduced against the prior commit.
This commit is contained in:
+18
-1
@@ -6,7 +6,8 @@ import {
|
||||
DecryptedMessageSchema,
|
||||
MachineSchema,
|
||||
PermissionModeSchema,
|
||||
SessionSchema
|
||||
SessionSchema,
|
||||
ThreadGoalSchema
|
||||
} from './schemas'
|
||||
import { AgentFlavorSchema } from './modes'
|
||||
import type {
|
||||
@@ -808,6 +809,22 @@ export const ImplementCodexPlanRequestSchema = z.object({
|
||||
|
||||
export type ImplementCodexPlanRequest = z.infer<typeof ImplementCodexPlanRequestSchema>
|
||||
|
||||
export const CodexGoalRequestSchema = z.discriminatedUnion('action', [
|
||||
z.object({ action: z.literal('get') }).strict(),
|
||||
z.object({
|
||||
action: z.literal('update'),
|
||||
objective: z.string().trim().min(1).refine(value => Array.from(value).length <= 4000, 'Goal must be at most 4000 characters'),
|
||||
tokenBudget: z.number().int().positive().max(Number.MAX_SAFE_INTEGER).nullable().optional()
|
||||
}).strict(),
|
||||
z.object({ action: z.literal('pause') }).strict(),
|
||||
z.object({ action: z.literal('resume') }).strict(),
|
||||
z.object({ action: z.literal('clear') }).strict()
|
||||
])
|
||||
|
||||
export type CodexGoalRequest = z.infer<typeof CodexGoalRequestSchema>
|
||||
export const CodexGoalResponseSchema = z.object({ goal: ThreadGoalSchema.nullable() })
|
||||
export type CodexGoalResponse = z.infer<typeof CodexGoalResponseSchema>
|
||||
|
||||
export type ImplementCodexPlanResult = { ok: true } | {
|
||||
ok: false
|
||||
code: 'stale_plan' | 'unavailable' | 'indeterminate' | 'failed'
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
/** Native positions, not Hub arrival sequences. The page containing the
|
||||
* anchor turn is deliberately revisited so an active turn can grow. */
|
||||
export const CodexHistoryCheckpointSchema = z.object({
|
||||
version: z.literal(1),
|
||||
pageCursor: z.string().nullable(),
|
||||
turnId: z.string().nullable(),
|
||||
itemId: z.string().nullable(),
|
||||
turnComplete: z.boolean()
|
||||
})
|
||||
export type CodexHistoryCheckpoint = z.infer<typeof CodexHistoryCheckpointSchema>
|
||||
export const CodexHistoryStateSchema = z.object({
|
||||
epoch: z.number().int().nonnegative(),
|
||||
revision: z.number().int().nonnegative(),
|
||||
checkpoint: CodexHistoryCheckpointSchema.nullable()
|
||||
})
|
||||
export type CodexHistoryState = z.infer<typeof CodexHistoryStateSchema>
|
||||
export const CodexHistorySyncRequestSchema = z.object({
|
||||
sid: z.string().min(1),
|
||||
threadId: z.string().min(1),
|
||||
commit: z.object({
|
||||
epoch: z.number().int().nonnegative(),
|
||||
revision: z.number().int().nonnegative(),
|
||||
checkpoint: CodexHistoryCheckpointSchema.nullable(),
|
||||
localIds: z.array(z.string().min(1)).max(1000)
|
||||
}).optional()
|
||||
})
|
||||
export type CodexHistorySyncRequest = z.infer<typeof CodexHistorySyncRequestSchema>
|
||||
export type CodexHistorySyncAck = { ok: true; state: CodexHistoryState } | { ok: false }
|
||||
@@ -14,6 +14,8 @@ export * from './rpcMethods'
|
||||
export * from './runnerCapabilities'
|
||||
export * from './search'
|
||||
export * from './socket'
|
||||
export * from './historySync'
|
||||
export * from './messageTruncation'
|
||||
export * from './sessionSummary'
|
||||
export * from './sessionCitation'
|
||||
export * from './sessionExport'
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/** Display-only agent payloads. Preserve user inputs verbatim. This is shared
|
||||
* by the CLI and Hub so truncation happens before network transmission too. */
|
||||
export const TRUNCATE_STRING_LIMIT = 64 * 1024
|
||||
const TRUNCATE_HEAD = 48 * 1024
|
||||
const TRUNCATE_TAIL = 12 * 1024
|
||||
|
||||
function truncateDeep(value: unknown): unknown {
|
||||
if (typeof value === 'string') {
|
||||
if (value.length <= TRUNCATE_STRING_LIMIT) return value
|
||||
const removed = value.length - TRUNCATE_HEAD - TRUNCATE_TAIL
|
||||
return `${value.slice(0, TRUNCATE_HEAD)}\n…[hapi: truncated ${removed} chars]…\n${value.slice(value.length - TRUNCATE_TAIL)}`
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
let copy: unknown[] | null = null
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
const item = truncateDeep(value[i])
|
||||
if (item !== value[i]) { copy ??= value.slice(); copy[i] = item }
|
||||
}
|
||||
return copy ?? value
|
||||
}
|
||||
if (value !== null && typeof value === 'object') {
|
||||
const original = value as Record<string, unknown>
|
||||
let copy: Record<string, unknown> | null = null
|
||||
for (const key of Object.keys(original)) {
|
||||
const item = truncateDeep(original[key])
|
||||
if (item !== original[key]) { copy ??= { ...original }; copy[key] = item }
|
||||
}
|
||||
return copy ?? value
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
export function truncateOversizedMessageContent(content: unknown): unknown {
|
||||
if (content === null || typeof content !== 'object' || (content as Record<string, unknown>).role !== 'agent') return content
|
||||
return truncateDeep(content)
|
||||
}
|
||||
@@ -1,9 +1,16 @@
|
||||
import { describe, expect, it } from 'bun:test'
|
||||
import { LocalResumeTargetSchema, ResumableSessionSchema } from './resume'
|
||||
import { LocalResumeTargetSchema, ResumableSessionSchema, sessionStartupTimeoutMs } from './resume'
|
||||
import { SyncEventSchema } from './schemas'
|
||||
import { SessionEndReasonSchema } from './socket'
|
||||
|
||||
describe('resume schemas', () => {
|
||||
it('allows a longer startup only for cold Codex resumes', () => {
|
||||
expect(sessionStartupTimeoutMs('codex', 'native-thread')).toBe(120_000)
|
||||
expect(sessionStartupTimeoutMs('codex')).toBe(15_000)
|
||||
expect(sessionStartupTimeoutMs('claude', 'native-session')).toBe(15_000)
|
||||
expect(sessionStartupTimeoutMs()).toBe(15_000)
|
||||
})
|
||||
|
||||
it('accepts a local resume target', () => {
|
||||
const parsed = LocalResumeTargetSchema.safeParse({
|
||||
sessionId: 'hapi-session-1',
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { z } from 'zod'
|
||||
import { CodexCollaborationModeSchema, CopilotAgentModeSchema, PermissionModeSchema } from './schemas'
|
||||
import { AgentFlavorSchema } from './modes'
|
||||
import { AgentFlavorSchema, type AgentFlavor } from './modes'
|
||||
|
||||
/** Cold Codex resumes can spend substantially longer loading native thread state. */
|
||||
export function sessionStartupTimeoutMs(flavor?: AgentFlavor, resumeSessionId?: string): number {
|
||||
return flavor === 'codex' && resumeSessionId ? 120_000 : 15_000
|
||||
}
|
||||
|
||||
export const LocalResumeTargetSchema = z.object({
|
||||
sessionId: z.string().min(1),
|
||||
|
||||
@@ -55,6 +55,7 @@ export const RPC_METHODS = {
|
||||
RewindConversation: 'rewind-conversation',
|
||||
ClearConversation: 'clear-conversation',
|
||||
ImplementCodexPlan: 'implement-codex-plan',
|
||||
CodexGoal: 'codex-goal',
|
||||
} as const
|
||||
|
||||
export const RPC_TARGET_MISSING_ERROR_CODE = 'rpc_target_missing' as const
|
||||
|
||||
@@ -36,6 +36,7 @@ export type OpencodeClearOperation = z.infer<typeof OpencodeClearOperationSchema
|
||||
|
||||
const SessionCapabilitiesSchema = z.object({
|
||||
concurrentClients: z.boolean().optional(),
|
||||
codexGoal: z.boolean().optional(),
|
||||
terminal: z.boolean().optional(),
|
||||
conversationHistory: ConversationHistoryCapabilitiesSchema.optional()
|
||||
})
|
||||
|
||||
@@ -253,11 +253,11 @@ export interface ServerToClientEvents {
|
||||
}
|
||||
|
||||
export interface ClientToServerEvents {
|
||||
'codex-history-sync': (data: import('./historySync').CodexHistorySyncRequest, ack: (response: import('./historySync').CodexHistorySyncAck) => void) => void
|
||||
// createdAt: optional client-provided timestamp (epoch ms) for the entry's
|
||||
// own origin time (e.g. a Claude transcript entry's timestamp). Currently
|
||||
// only honored by the hub for agent messages (no localId) — see
|
||||
// messages.ts addMessage. Absent -> hub falls back to Date.now().
|
||||
message: (data: { sid: string; message: unknown; localId?: string; createdAt?: number }) => void
|
||||
// own origin time (e.g. a Claude transcript or Codex history item).
|
||||
// Absent -> hub falls back to Date.now().
|
||||
message: (data: { sid: string; message: unknown; localId?: string; createdAt?: number }, ack?: (response: { ok: boolean }) => void) => void
|
||||
'session-alive': (data: {
|
||||
sid: string
|
||||
time: number
|
||||
|
||||
Reference in New Issue
Block a user