// engine/session/session-tracker.ts — In-memory call counter and file modification tracker
// for auto session checkpointing (SPEC-563).
const DEFAULT_THRESHOLD = 10;
const MAX_LAST_CALLS = 10;
const MAX_ERRORS = 20;
class SessionTracker {
    callCount = 0;
    lastCalls = [];
    modifiedFiles = new Set();
    errors = [];
    policy = { threshold: DEFAULT_THRESHOLD, enabled: true };
    /** Record a tool call. Appends to lastCalls (capped at MAX_LAST_CALLS). */
    recordCall(tool, success) {
        this.callCount++;
        this.lastCalls.push({ tool, success, timestamp: new Date().toISOString() });
        if (this.lastCalls.length > MAX_LAST_CALLS) {
            this.lastCalls.shift();
        }
    }
    /** Record a file path as modified during this session. */
    recordFile(path) {
        this.modifiedFiles.add(path);
    }
    /** Record an error message (capped at MAX_ERRORS). */
    recordError(msg) {
        if (this.errors.length >= MAX_ERRORS) {
            this.errors.shift();
        }
        this.errors.push(msg);
    }
    /**
     * Returns true when a checkpoint should be written:
     * policy is enabled, callCount > 0, and callCount is a multiple of threshold.
     */
    shouldCheckpoint() {
        return (this.policy.enabled && this.callCount > 0 && this.callCount % this.policy.threshold === 0);
    }
    /** Return a compact snapshot of the current session state. */
    getSnapshot() {
        return {
            callCount: this.callCount,
            lastCalls: [...this.lastCalls],
            modifiedFiles: [...this.modifiedFiles],
            errors: [...this.errors],
        };
    }
    /** Update the checkpoint policy (partial update — unspecified fields keep current values). */
    setPolicy(p) {
        if (p.threshold !== undefined) {
            this.policy = { ...this.policy, threshold: Math.max(1, Math.round(p.threshold)) };
        }
        if (p.enabled !== undefined) {
            this.policy = { ...this.policy, enabled: p.enabled };
        }
    }
    /** Return the current checkpoint policy. */
    getPolicy() {
        return { ...this.policy };
    }
    /** Reset the tracker state (useful for testing). */
    reset() {
        this.callCount = 0;
        this.lastCalls.splice(0);
        this.modifiedFiles.clear();
        this.errors.splice(0);
        this.policy = { threshold: DEFAULT_THRESHOLD, enabled: true };
    }
}
/** Singleton session tracker — one per process. */
export const sessionTracker = new SessionTracker();
//# sourceMappingURL=session-tracker.js.map