Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 6192x 59x 59x 59x 59x 59x 59x 59x 59x 59x 6127x 6127x 6127x 6127x 6127x 1007x 6127x 6127x 6127x 5090x 4437x 4439x 6127x 6127x 6127x 6127x 6127x 6127x 6127x 6127x 6127x 6127x 6127x 5069x 5069x 5069x 76x 76x 5030x 5030x 76x 76x 76x 76x 76x 76x 76x 76x 76x 5992x 16226x 7x 7x 110x 79x 79x 31x 7x 6071x 6071x 6071x 6071x 6071x 6071x 6071x 6071x 6071x 5059x 5059x 6071x 6127x 10429x 10429x 10429x | import { randomUUID } from "node:crypto";
import type { ExamplesRef, FullCommitSha, RepositorySlug } from "@shared/examples";
import { config, type ParsedTutorSessionConfig } from "../../config";
import type { RequestContext } from "../examples/source-provider";
import type { TutorCapability } from "./course-content-loader";
import type { ExampleTutorAnnotation } from "./embedded-tutor-annotation";
import { createTutorProgressionState, type TutorProgressionState } from "../tutor/curriculum/progression-state";
export interface TutorCourseContentRequest {
readonly repository: RepositorySlug;
readonly ref: ExamplesRef;
readonly revision: FullCommitSha;
readonly exampleId?: string;
}
export interface ResolvedTutorCourseContent extends TutorCourseContentRequest {
readonly tutor: TutorCapability;
readonly exampleTutorAnnotation?: ExampleTutorAnnotation;
readonly progressionState?: TutorProgressionState;
/** Size of the immutable snapshot the Tutor data comes from; charged once per pinned revision. */
readonly contentBytes: number;
}
export interface TutorCourseContentResolver {
resolveTutorContent(request: TutorCourseContentRequest, context: RequestContext): Promise<ResolvedTutorCourseContent>;
}
/** Course Content pinned to one revision together with its session-local progression state. */
export type PinnedTutorCourseContent = ResolvedTutorCourseContent & { readonly progressionState: TutorProgressionState };
export type TutorCourseContentSessionEvictionReason = "subject-limit" | "session-limit" | "content-budget";
export interface TutorCourseContentSessionStats {
readonly sessions: number;
readonly subjects: number;
readonly pinnedRevisions: number;
readonly pinnedContentBytes: number;
readonly expired: number;
readonly evicted: Readonly<Record<TutorCourseContentSessionEvictionReason, number>>;
}
/**
* Request-scoped content for a Tutor request without a session handle. Nothing is
* registered: the route commits it only after the request succeeded, so failed,
* rejected, or aborted requests leave no session behind.
*/
export function prepareTutorCourseContentSession(content: ResolvedTutorCourseContent): PinnedTutorCourseContent {
return { ...content, progressionState: content.progressionState ?? createTutorProgressionState(content.revision) };
}
type SessionEntry = {
readonly identity: string;
readonly content: PinnedTutorCourseContent;
readonly revisionKey: string;
readonly expiresAt: number;
};
type PinnedRevision = {
readonly contentBytes: number;
readonly handles: Set<string>;
};
/**
* Subject-owned Tutor sessions with an absolute TTL and three bounds: sessions per
* subject, sessions overall, and the Tutor data of distinct pinned revisions.
* Each map iterates least recently used first; a successful lookup refreshes the
* session, its subject slot, and its revision. Eviction drops whole sessions, so
* a learner sees an expired context instead of partial progression.
*/
export class TutorCourseContentSessionStore {
private readonly sessions = new Map<string, SessionEntry>();
private readonly subjects = new Map<string, Set<string>>();
private readonly revisions = new Map<string, PinnedRevision>();
private pinnedContentBytes = 0;
private nextExpiryAt = Number.POSITIVE_INFINITY;
private expired = 0;
private readonly evicted: Record<TutorCourseContentSessionEvictionReason, number> = {
"subject-limit": 0,
"session-limit": 0,
"content-budget": 0,
};
constructor(
private readonly limits: ParsedTutorSessionConfig = config.tutor.sessions,
private readonly now: () => number = Date.now,
) {}
commit(identity: string, content: PinnedTutorCourseContent): string {
const now = this.now();
this.sweepExpired(now);
const revisionKey = `${content.repository}@${content.revision}`;
const ownHandles = this.subjects.get(identity);
while (ownHandles && ownHandles.size >= this.limits.maxSessionsPerSubject) {
this.evict(firstOf(ownHandles), "subject-limit");
}
while (this.sessions.size >= this.limits.maxSessions) this.evict(firstOf(this.sessions.keys()), "session-limit");
const contentBytes = this.chargeableBytes(content.contentBytes);
if (!this.revisions.has(revisionKey)) {
// Releasing a revision releases every session that pins it; the most recently used revisions stay.
while (this.revisions.size > 0 && this.pinnedContentBytes + contentBytes > this.limits.maxPinnedContentBytes) {
const oldest = firstOf(this.revisions.values());
while (oldest.handles.size > 0) this.evict(firstOf(oldest.handles), "content-budget");
}
}
const handle = randomUUID();
const expiresAt = now + this.limits.ttlMs;
this.sessions.set(handle, { identity, content, revisionKey, expiresAt });
this.subjects.set(identity, (this.subjects.get(identity) ?? new Set()).add(handle));
const revision = this.revisions.get(revisionKey) ?? { contentBytes, handles: new Set<string>() };
if (revision.handles.size === 0) this.pinnedContentBytes += contentBytes;
revision.handles.add(handle);
this.revisions.delete(revisionKey);
this.revisions.set(revisionKey, revision);
this.nextExpiryAt = Math.min(this.nextExpiryAt, expiresAt);
return handle;
}
get(identity: string, handle: string): PinnedTutorCourseContent | null {
this.sweepExpired(this.now());
const entry = this.sessions.get(handle);
if (entry?.identity !== identity) return null;
this.touch(handle, entry);
return entry.content;
}
stats(): TutorCourseContentSessionStats {
this.sweepExpired(this.now());
return {
sessions: this.sessions.size,
subjects: this.subjects.size,
pinnedRevisions: this.revisions.size,
pinnedContentBytes: this.pinnedContentBytes,
expired: this.expired,
evicted: { ...this.evicted },
};
}
private touch(handle: string, entry: SessionEntry): void {
this.sessions.delete(handle);
this.sessions.set(handle, entry);
const subject = this.subjects.get(entry.identity);
subject?.delete(handle);
subject?.add(handle);
const revision = this.revisions.get(entry.revisionKey);
Eif (revision) {
this.revisions.delete(entry.revisionKey);
this.revisions.set(entry.revisionKey, revision);
}
}
private evict(handle: string, reason: TutorCourseContentSessionEvictionReason): void {
Eif (this.remove(handle)) this.evicted[reason] += 1;
}
/** Expiry is absolute; the scan runs only once the earliest known expiry has passed. */
private sweepExpired(now: number): void {
if (now < this.nextExpiryAt) return;
let nextExpiryAt = Number.POSITIVE_INFINITY;
for (const [handle, entry] of this.sessions) {
if (entry.expiresAt <= now) {
this.remove(handle);
this.expired += 1;
} else {
nextExpiryAt = Math.min(nextExpiryAt, entry.expiresAt);
}
}
this.nextExpiryAt = nextExpiryAt;
}
private remove(handle: string): boolean {
const entry = this.sessions.get(handle);
Iif (!entry) return false;
this.sessions.delete(handle);
const subject = this.subjects.get(entry.identity);
subject?.delete(handle);
if (subject?.size === 0) this.subjects.delete(entry.identity);
const revision = this.revisions.get(entry.revisionKey);
revision?.handles.delete(handle);
if (revision?.handles.size === 0) {
this.revisions.delete(entry.revisionKey);
this.pinnedContentBytes -= revision.contentBytes;
}
return true;
}
/** An unknown size is charged as the whole budget, so it can never be pinned beside other revisions. */
private chargeableBytes(contentBytes: number): number {
return Number.isSafeInteger(contentBytes) && contentBytes >= 0 ? contentBytes : this.limits.maxPinnedContentBytes;
}
}
function firstOf<T>(values: Iterable<T>): T {
const first = values[Symbol.iterator]().next();
Iif (first.done) throw new Error("Expected a non-empty Tutor session collection");
return first.value;
}
|