MCPcopy Index your code
hub / github.com/Opencode-DCP/opencode-dynamic-context-pruning / checkSession

Function checkSession

lib/state/state.ts:17–64  ·  view source on GitHub ↗
(
    client: any,
    state: SessionState,
    logger: Logger,
    messages: WithParts[],
    manualModeDefault: boolean,
)

Source from the content-addressed store, hash-verified

15import { getLastUserMessage } from "../messages/query"
16
17export const checkSession = async (
18 client: any,
19 state: SessionState,
20 logger: Logger,
21 messages: WithParts[],
22 manualModeDefault: boolean,
23): Promise<void> => {
24 const lastUserMessage = getLastUserMessage(messages)
25 if (!lastUserMessage) {
26 return
27 }
28
29 const lastSessionId = lastUserMessage.info.sessionID
30
31 if (state.sessionId === null || state.sessionId !== lastSessionId) {
32 logger.info(`Session changed: ${state.sessionId} -> ${lastSessionId}`)
33 try {
34 await ensureSessionInitialized(
35 client,
36 state,
37 lastSessionId,
38 logger,
39 messages,
40 manualModeDefault,
41 )
42 } catch (err: any) {
43 logger.error("Failed to initialize session state", { error: err.message })
44 }
45 }
46
47 const lastCompactionTimestamp = findLastCompactionTimestamp(messages)
48 if (lastCompactionTimestamp > state.lastCompaction) {
49 state.lastCompaction = lastCompactionTimestamp
50 resetOnCompaction(state)
51 logger.info("Detected compaction - reset stale state", {
52 timestamp: lastCompactionTimestamp,
53 })
54
55 saveSessionState(state, logger).catch((error) => {
56 logger.warn("Failed to persist state reset after compaction", {
57 error: error instanceof Error ? error.message : String(error),
58 })
59 })
60 }
61
62 state.currentTurn = countTurns(state, messages)
63 await refreshManualMode(state, lastSessionId, logger, manualModeDefault)
64}
65
66export function createSessionState(): SessionState {
67 return {

Callers 2

Calls 10

getLastUserMessageFunction · 0.90
resetOnCompactionFunction · 0.90
saveSessionStateFunction · 0.90
countTurnsFunction · 0.90
ensureSessionInitializedFunction · 0.85
refreshManualModeFunction · 0.85
infoMethod · 0.80
errorMethod · 0.80
warnMethod · 0.80

Tested by

no test coverage detected