seance-cli 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (190) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -2
  3. package/dist/commands/init.d.ts +29 -0
  4. package/dist/commands/init.js +310 -0
  5. package/dist/commands/init.js.map +1 -0
  6. package/dist/commands/mcp.d.ts +19 -0
  7. package/dist/commands/mcp.js +38 -0
  8. package/dist/commands/mcp.js.map +1 -0
  9. package/dist/commands/quiz.d.ts +23 -0
  10. package/dist/commands/quiz.js +187 -0
  11. package/dist/commands/quiz.js.map +1 -0
  12. package/dist/commands/review.d.ts +4 -0
  13. package/dist/commands/review.js +115 -0
  14. package/dist/commands/review.js.map +1 -0
  15. package/dist/commands/sync.d.ts +17 -0
  16. package/dist/commands/sync.js +367 -0
  17. package/dist/commands/sync.js.map +1 -0
  18. package/dist/git.d.ts +33 -0
  19. package/dist/git.js +103 -0
  20. package/dist/git.js.map +1 -0
  21. package/dist/hosted-api.d.ts +24 -0
  22. package/dist/hosted-api.js +85 -0
  23. package/dist/hosted-api.js.map +1 -0
  24. package/dist/hosted-consent.d.ts +8 -0
  25. package/dist/hosted-consent.js +37 -0
  26. package/dist/hosted-consent.js.map +1 -0
  27. package/dist/index.d.ts +2 -0
  28. package/dist/index.js +108 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/init-fs.d.ts +51 -0
  31. package/dist/init-fs.js +220 -0
  32. package/dist/init-fs.js.map +1 -0
  33. package/dist/init-questions.d.ts +21 -0
  34. package/dist/init-questions.js +161 -0
  35. package/dist/init-questions.js.map +1 -0
  36. package/dist/mcp-handlers.d.ts +22 -0
  37. package/dist/mcp-handlers.js +185 -0
  38. package/dist/mcp-handlers.js.map +1 -0
  39. package/dist/mcp-server.d.ts +4 -0
  40. package/dist/mcp-server.js +39 -0
  41. package/dist/mcp-server.js.map +1 -0
  42. package/dist/prompter.d.ts +29 -0
  43. package/dist/prompter.js +42 -0
  44. package/dist/prompter.js.map +1 -0
  45. package/dist/quiz-backend.d.ts +40 -0
  46. package/dist/quiz-backend.js +116 -0
  47. package/dist/quiz-backend.js.map +1 -0
  48. package/dist/quiz-input.d.ts +12 -0
  49. package/dist/quiz-input.js +52 -0
  50. package/dist/quiz-input.js.map +1 -0
  51. package/dist/quiz-io.d.ts +30 -0
  52. package/dist/quiz-io.js +83 -0
  53. package/dist/quiz-io.js.map +1 -0
  54. package/dist/quiz-ui.d.ts +18 -0
  55. package/dist/quiz-ui.js +51 -0
  56. package/dist/quiz-ui.js.map +1 -0
  57. package/node_modules/@seance/ai/LICENSE +21 -0
  58. package/node_modules/@seance/ai/dist/chain.d.ts +31 -0
  59. package/node_modules/@seance/ai/dist/chain.js +114 -0
  60. package/node_modules/@seance/ai/dist/chain.js.map +1 -0
  61. package/node_modules/@seance/ai/dist/grade.d.ts +14 -0
  62. package/node_modules/@seance/ai/dist/grade.js +44 -0
  63. package/node_modules/@seance/ai/dist/grade.js.map +1 -0
  64. package/node_modules/@seance/ai/dist/hosted-config.d.ts +9 -0
  65. package/node_modules/@seance/ai/dist/hosted-config.js +27 -0
  66. package/node_modules/@seance/ai/dist/hosted-config.js.map +1 -0
  67. package/node_modules/@seance/ai/dist/index.d.ts +9 -0
  68. package/node_modules/@seance/ai/dist/index.js +9 -0
  69. package/node_modules/@seance/ai/dist/index.js.map +1 -0
  70. package/node_modules/@seance/ai/dist/providers.d.ts +54 -0
  71. package/node_modules/@seance/ai/dist/providers.js +149 -0
  72. package/node_modules/@seance/ai/dist/providers.js.map +1 -0
  73. package/node_modules/@seance/ai/dist/quiz.d.ts +26 -0
  74. package/node_modules/@seance/ai/dist/quiz.js +41 -0
  75. package/node_modules/@seance/ai/dist/quiz.js.map +1 -0
  76. package/node_modules/@seance/ai/package.json +25 -0
  77. package/node_modules/@seance/core/LICENSE +21 -0
  78. package/node_modules/@seance/core/dist/ai-check.d.ts +58 -0
  79. package/node_modules/@seance/core/dist/ai-check.js +460 -0
  80. package/node_modules/@seance/core/dist/ai-check.js.map +1 -0
  81. package/node_modules/@seance/core/dist/ai-prompts.d.ts +29 -0
  82. package/node_modules/@seance/core/dist/ai-prompts.js +221 -0
  83. package/node_modules/@seance/core/dist/ai-prompts.js.map +1 -0
  84. package/node_modules/@seance/core/dist/ai-schemas.d.ts +155 -0
  85. package/node_modules/@seance/core/dist/ai-schemas.js +92 -0
  86. package/node_modules/@seance/core/dist/ai-schemas.js.map +1 -0
  87. package/node_modules/@seance/core/dist/api-client.d.ts +26 -0
  88. package/node_modules/@seance/core/dist/api-client.js +71 -0
  89. package/node_modules/@seance/core/dist/api-client.js.map +1 -0
  90. package/node_modules/@seance/core/dist/api-errors.d.ts +37 -0
  91. package/node_modules/@seance/core/dist/api-errors.js +66 -0
  92. package/node_modules/@seance/core/dist/api-errors.js.map +1 -0
  93. package/node_modules/@seance/core/dist/api-schemas.d.ts +284 -0
  94. package/node_modules/@seance/core/dist/api-schemas.js +67 -0
  95. package/node_modules/@seance/core/dist/api-schemas.js.map +1 -0
  96. package/node_modules/@seance/core/dist/decision-log.d.ts +73 -0
  97. package/node_modules/@seance/core/dist/decision-log.js +239 -0
  98. package/node_modules/@seance/core/dist/decision-log.js.map +1 -0
  99. package/node_modules/@seance/core/dist/dependency-guard.d.ts +20 -0
  100. package/node_modules/@seance/core/dist/dependency-guard.js +732 -0
  101. package/node_modules/@seance/core/dist/dependency-guard.js.map +1 -0
  102. package/node_modules/@seance/core/dist/gate.d.ts +11 -0
  103. package/node_modules/@seance/core/dist/gate.js +26 -0
  104. package/node_modules/@seance/core/dist/gate.js.map +1 -0
  105. package/node_modules/@seance/core/dist/git-parse.d.ts +11 -0
  106. package/node_modules/@seance/core/dist/git-parse.js +23 -0
  107. package/node_modules/@seance/core/dist/git-parse.js.map +1 -0
  108. package/node_modules/@seance/core/dist/index.d.ts +65 -0
  109. package/node_modules/@seance/core/dist/index.js +47 -0
  110. package/node_modules/@seance/core/dist/index.js.map +1 -0
  111. package/node_modules/@seance/core/dist/init-checklist.d.ts +153 -0
  112. package/node_modules/@seance/core/dist/init-checklist.js +200 -0
  113. package/node_modules/@seance/core/dist/init-checklist.js.map +1 -0
  114. package/node_modules/@seance/core/dist/init-detect.d.ts +9 -0
  115. package/node_modules/@seance/core/dist/init-detect.js +84 -0
  116. package/node_modules/@seance/core/dist/init-detect.js.map +1 -0
  117. package/node_modules/@seance/core/dist/init-templates.d.ts +36 -0
  118. package/node_modules/@seance/core/dist/init-templates.js +257 -0
  119. package/node_modules/@seance/core/dist/init-templates.js.map +1 -0
  120. package/node_modules/@seance/core/dist/map-basics.d.ts +13 -0
  121. package/node_modules/@seance/core/dist/map-basics.js +19 -0
  122. package/node_modules/@seance/core/dist/map-basics.js.map +1 -0
  123. package/node_modules/@seance/core/dist/map-summary.d.ts +5 -0
  124. package/node_modules/@seance/core/dist/map-summary.js +50 -0
  125. package/node_modules/@seance/core/dist/map-summary.js.map +1 -0
  126. package/node_modules/@seance/core/dist/markers.d.ts +51 -0
  127. package/node_modules/@seance/core/dist/markers.js +84 -0
  128. package/node_modules/@seance/core/dist/markers.js.map +1 -0
  129. package/node_modules/@seance/core/dist/mcp-quiz.d.ts +94 -0
  130. package/node_modules/@seance/core/dist/mcp-quiz.js +130 -0
  131. package/node_modules/@seance/core/dist/mcp-quiz.js.map +1 -0
  132. package/node_modules/@seance/core/dist/mcp-tools.d.ts +82 -0
  133. package/node_modules/@seance/core/dist/mcp-tools.js +136 -0
  134. package/node_modules/@seance/core/dist/mcp-tools.js.map +1 -0
  135. package/node_modules/@seance/core/dist/mcp-views.d.ts +21 -0
  136. package/node_modules/@seance/core/dist/mcp-views.js +83 -0
  137. package/node_modules/@seance/core/dist/mcp-views.js.map +1 -0
  138. package/node_modules/@seance/core/dist/quiz-assembly.d.ts +21 -0
  139. package/node_modules/@seance/core/dist/quiz-assembly.js +43 -0
  140. package/node_modules/@seance/core/dist/quiz-assembly.js.map +1 -0
  141. package/node_modules/@seance/core/dist/quiz-cards.d.ts +183 -0
  142. package/node_modules/@seance/core/dist/quiz-cards.js +215 -0
  143. package/node_modules/@seance/core/dist/quiz-cards.js.map +1 -0
  144. package/node_modules/@seance/core/dist/quiz-session.d.ts +67 -0
  145. package/node_modules/@seance/core/dist/quiz-session.js +141 -0
  146. package/node_modules/@seance/core/dist/quiz-session.js.map +1 -0
  147. package/node_modules/@seance/core/dist/quiz-subjects.d.ts +13 -0
  148. package/node_modules/@seance/core/dist/quiz-subjects.js +63 -0
  149. package/node_modules/@seance/core/dist/quiz-subjects.js.map +1 -0
  150. package/node_modules/@seance/core/dist/rate-limit.d.ts +33 -0
  151. package/node_modules/@seance/core/dist/rate-limit.js +114 -0
  152. package/node_modules/@seance/core/dist/rate-limit.js.map +1 -0
  153. package/node_modules/@seance/core/dist/scoring.d.ts +48 -0
  154. package/node_modules/@seance/core/dist/scoring.js +95 -0
  155. package/node_modules/@seance/core/dist/scoring.js.map +1 -0
  156. package/node_modules/@seance/core/dist/seance-config.d.ts +177 -0
  157. package/node_modules/@seance/core/dist/seance-config.js +43 -0
  158. package/node_modules/@seance/core/dist/seance-config.js.map +1 -0
  159. package/node_modules/@seance/core/dist/seance-state.d.ts +125 -0
  160. package/node_modules/@seance/core/dist/seance-state.js +61 -0
  161. package/node_modules/@seance/core/dist/seance-state.js.map +1 -0
  162. package/node_modules/@seance/core/dist/sm2.d.ts +18 -0
  163. package/node_modules/@seance/core/dist/sm2.js +87 -0
  164. package/node_modules/@seance/core/dist/sm2.js.map +1 -0
  165. package/node_modules/@seance/core/dist/stored-schemas.d.ts +29 -0
  166. package/node_modules/@seance/core/dist/stored-schemas.js +29 -0
  167. package/node_modules/@seance/core/dist/stored-schemas.js.map +1 -0
  168. package/node_modules/@seance/core/dist/structural-questions.d.ts +31 -0
  169. package/node_modules/@seance/core/dist/structural-questions.js +575 -0
  170. package/node_modules/@seance/core/dist/structural-questions.js.map +1 -0
  171. package/node_modules/@seance/core/dist/structure-map-schema.d.ts +84 -0
  172. package/node_modules/@seance/core/dist/structure-map-schema.js +73 -0
  173. package/node_modules/@seance/core/dist/structure-map-schema.js.map +1 -0
  174. package/node_modules/@seance/core/dist/structure-map.d.ts +78 -0
  175. package/node_modules/@seance/core/dist/structure-map.js +750 -0
  176. package/node_modules/@seance/core/dist/structure-map.js.map +1 -0
  177. package/node_modules/@seance/core/dist/sync-changes.d.ts +38 -0
  178. package/node_modules/@seance/core/dist/sync-changes.js +89 -0
  179. package/node_modules/@seance/core/dist/sync-changes.js.map +1 -0
  180. package/node_modules/@seance/core/dist/sync-render.d.ts +28 -0
  181. package/node_modules/@seance/core/dist/sync-render.js +113 -0
  182. package/node_modules/@seance/core/dist/sync-render.js.map +1 -0
  183. package/node_modules/@seance/core/dist/terminal-text.d.ts +8 -0
  184. package/node_modules/@seance/core/dist/terminal-text.js +33 -0
  185. package/node_modules/@seance/core/dist/terminal-text.js.map +1 -0
  186. package/node_modules/@seance/core/dist/write-plan.d.ts +51 -0
  187. package/node_modules/@seance/core/dist/write-plan.js +173 -0
  188. package/node_modules/@seance/core/dist/write-plan.js.map +1 -0
  189. package/node_modules/@seance/core/package.json +26 -0
  190. package/package.json +42 -6
@@ -0,0 +1,141 @@
1
+ // A quiz run in memory (Task 7): where the questions come from, the answers given so far, quality for SM-2,
2
+ // choice reshuffling and the result lines. Pure: the CLI asks the questions and passes the answers in.
3
+ import { QuizItemSchema } from "./ai-schemas.js";
4
+ import { gateDecision } from "./gate.js";
5
+ import { EXPLAIN_MISS_BELOW, scoreQuiz } from "./scoring.js";
6
+ import { fnv1a32 } from "./structural-questions.js";
7
+ /** `--offline` wins; else the user's own provider keys; else the hosted API when its URL is set; else structural questions only. */
8
+ export function pickQuizSource(input) {
9
+ if (input.offline)
10
+ return "offline";
11
+ if (input.providerCount > 0)
12
+ return "local-ai";
13
+ return input.hostedUrl === undefined ? "structural" : "hosted";
14
+ }
15
+ const MAX_SESSION_QUESTIONS = 10;
16
+ /** Starts a session. Throws RangeError for 0 or more than 10 questions, a repeated id or an item that fails `QuizItemSchema`. */
17
+ export function startSession(questions) {
18
+ if (questions.length === 0 || questions.length > MAX_SESSION_QUESTIONS) {
19
+ throw new RangeError(`a quiz needs 1 to ${MAX_SESSION_QUESTIONS} questions, got ${questions.length}`);
20
+ }
21
+ const ids = new Set();
22
+ for (const item of questions) {
23
+ if (!QuizItemSchema.safeParse(item).success)
24
+ throw new RangeError("a question does not match the quiz item schema");
25
+ if (ids.has(item.id))
26
+ throw new RangeError(`duplicate question id: ${item.id}`);
27
+ ids.add(item.id);
28
+ }
29
+ return { questions: [...questions], outcomes: [] };
30
+ }
31
+ export function isComplete(session) {
32
+ return session.outcomes.length >= session.questions.length;
33
+ }
34
+ /** The question to ask next, or undefined when every question has an answer. */
35
+ export function currentQuestion(session) {
36
+ return session.questions[session.outcomes.length];
37
+ }
38
+ function requireCurrent(session) {
39
+ const question = currentQuestion(session);
40
+ if (question === undefined)
41
+ throw new RangeError("the quiz is already complete");
42
+ return question;
43
+ }
44
+ /** Records the answer to the current choice question. Throws RangeError when complete, for an explain question or an index outside the choices. */
45
+ export function answerChoice(session, choiceIndex) {
46
+ const question = requireCurrent(session);
47
+ if (question.kind === "explain")
48
+ throw new RangeError(`question ${question.id} is an explain question and needs a graded answer`);
49
+ if (!Number.isInteger(choiceIndex) || choiceIndex < 0 || choiceIndex >= question.choices.length) {
50
+ throw new RangeError(`choiceIndex must be an integer in 0..${question.choices.length - 1}, got ${String(choiceIndex)}`);
51
+ }
52
+ const correct = choiceIndex === question.answerIndex;
53
+ const outcome = { questionId: question.id, kind: "choice", choiceIndex, correct };
54
+ return { session: { questions: session.questions, outcomes: [...session.outcomes, outcome] }, correct, correctIndex: question.answerIndex };
55
+ }
56
+ /** Records the grade of the current explain question. Throws RangeError when complete, for a choice question or a score outside [0, 1]. */
57
+ export function answerGraded(session, score, rough) {
58
+ const question = requireCurrent(session);
59
+ if (question.kind !== "explain")
60
+ throw new RangeError(`question ${question.id} is a choice question and needs a choice answer`);
61
+ if (typeof score !== "number" || !Number.isFinite(score) || score < 0 || score > 1) {
62
+ throw new RangeError(`score must be a finite number in [0, 1], got ${String(score)}`);
63
+ }
64
+ const outcome = { questionId: question.id, kind: "graded", score, rough };
65
+ return { questions: session.questions, outcomes: [...session.outcomes, outcome] };
66
+ }
67
+ /** SM-2 quality of one answer: choice right 4, wrong 1; explain by score. Below 3 means the question was missed. */
68
+ export function qualityFor(outcome) {
69
+ if (outcome.kind === "choice")
70
+ return outcome.correct ? 4 : 1;
71
+ const score = outcome.score;
72
+ if (score >= 0.9)
73
+ return 5;
74
+ if (score >= 0.7)
75
+ return 4;
76
+ if (score >= EXPLAIN_MISS_BELOW)
77
+ return 3;
78
+ if (score >= 0.25)
79
+ return 2;
80
+ return score > 0 ? 1 : 0;
81
+ }
82
+ function toAnswer(outcome) {
83
+ return outcome.kind === "choice"
84
+ ? { questionId: outcome.questionId, kind: "choice", choiceIndex: outcome.choiceIndex }
85
+ : { questionId: outcome.questionId, kind: "graded", score: outcome.score };
86
+ }
87
+ /** Scores a finished session and applies the gate. Throws RangeError when a question has no answer yet. */
88
+ export function finishSession(session, gate) {
89
+ if (!isComplete(session))
90
+ throw new RangeError("the quiz is not complete");
91
+ const result = scoreQuiz([...session.questions], session.outcomes.map(toAnswer));
92
+ const missed = session.questions.filter((_question, index) => {
93
+ const outcome = session.outcomes[index];
94
+ return outcome !== undefined && qualityFor(outcome) < 3;
95
+ });
96
+ return { result, gate: gateDecision(result.score, gate.passMark, gate.mode), passMark: gate.passMark, mode: gate.mode, missed };
97
+ }
98
+ /**
99
+ * The same question with its choices in another order for each `attempt`, so a retake cannot be passed by position.
100
+ * Attempt 0 and explain questions are returned unchanged. Indexes are tracked, so equal choice texts are safe.
101
+ */
102
+ export function reorderChoices(item, attempt) {
103
+ if (!Number.isSafeInteger(attempt) || attempt < 0)
104
+ throw new RangeError(`attempt must be a safe integer >= 0, got ${String(attempt)}`);
105
+ if (item.kind === "explain" || attempt === 0)
106
+ return item;
107
+ const order = item.choices
108
+ .map((text, index) => ({ index, hash: fnv1a32(`${attempt}|${item.id}|${index}#${text}`) }))
109
+ .sort((a, b) => a.hash - b.hash || a.index - b.index);
110
+ return { ...item, choices: order.map((entry) => entry.index).map((index) => item.choices[index]), answerIndex: order.findIndex((entry) => entry.index === item.answerIndex) };
111
+ }
112
+ function percent(value) {
113
+ return Math.round(value * 100);
114
+ }
115
+ const GATE_LINES = {
116
+ passed: "Passed.",
117
+ "soft-below": "Allowed, but not understood yet. Run seance review when cards are due.",
118
+ "strict-below": "Not passed. Run seance quiz again to retake it (choices reshuffled).",
119
+ "gate-off": "Gate is off.",
120
+ };
121
+ /** The result lines printed after a quiz (design "Output"). `cards` says what `applyQuizResult` did. */
122
+ export function formatQuizResult(summary, cards) {
123
+ const lines = [`Score: ${percent(summary.result.score)}% (pass mark ${percent(summary.passMark)}%).`, GATE_LINES[summary.gate.reason]];
124
+ const concepts = summary.result.missedConcepts.join(", ");
125
+ if (summary.missed.length > 0) {
126
+ if (cards.added > 0) {
127
+ lines.push(`Missed: ${concepts}. ${cards.added} new review ${cards.added === 1 ? "card" : "cards"}, first review ${cards.firstReview ?? "tomorrow"}.`);
128
+ }
129
+ else if (cards.notAdded > 0) {
130
+ lines.push(`Missed: ${concepts}.`);
131
+ }
132
+ else {
133
+ lines.push(`Missed: ${concepts}. Existing review cards updated.`);
134
+ }
135
+ }
136
+ if (cards.notAdded > 0) {
137
+ lines.push(`${cards.notAdded} missed ${cards.notAdded === 1 ? "question" : "questions"} not saved as cards (state file full)`);
138
+ }
139
+ return lines;
140
+ }
141
+ //# sourceMappingURL=quiz-session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quiz-session.js","sourceRoot":"","sources":["../src/quiz-session.ts"],"names":[],"mappings":"AAAA,4GAA4G;AAC5G,uGAAuG;AACvG,OAAO,EAAE,cAAc,EAAiB,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,YAAY,EAAkC,MAAM,WAAW,CAAC;AAC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AC,IAAI,CAAC,GAAG,KAAK,CAAC,QAAQ,WAAW,KAAK,CAAC,QAAQ,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,WAAW,uCAAuC,CAAC,CAAC;IACjI,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { QuizItem } from "./ai-schemas.js";
2
+ /**
3
+ * The name used to compare subjects: no call parentheses, no `this.` prefix, lower-cased. A `Class.` prefix is kept
4
+ * (changed Dec 2 after review), so `Todo.save` and `User.save` are different subjects while `this.save` and `save()` are `save`.
5
+ * Nothing is split at a dot, so a module name such as `socket.io` stays whole.
6
+ */
7
+ export declare function normalizeSubject(name: string): string;
8
+ /** The subject of a question, or undefined when its prompt names no function, class, export or module (only file paths). */
9
+ export declare function subjectOf(item: Pick<QuizItem, "prompt">): string | undefined;
10
+ /** `concept|subject` for a question, or undefined when it has no subject (such a question is never dropped as a repeat). */
11
+ export declare function subjectKey(item: Pick<QuizItem, "prompt" | "concept">): string | undefined;
12
+ /** Keeps the first question of every (concept, subject) pair. Questions without a subject are all kept. */
13
+ export declare function dropRepeatedSubjects<T extends Pick<QuizItem, "prompt" | "concept">>(items: readonly T[]): T[];
@@ -0,0 +1,63 @@
1
+ // A span that is a file path: it has a `/` or ends in a source file extension.
2
+ const FILE_EXTENSION = /\.(?:[cm]?[jt]sx?|json|md)$/i;
3
+ /** Text between pairs of backticks, in order. Linear: only `indexOf`. */
4
+ function backtickedSpans(text) {
5
+ const spans = [];
6
+ for (let from = 0;;) {
7
+ const open = text.indexOf("`", from);
8
+ if (open === -1)
9
+ return spans;
10
+ const close = text.indexOf("`", open + 1);
11
+ if (close === -1)
12
+ return spans;
13
+ spans.push(text.slice(open + 1, close));
14
+ from = close + 1;
15
+ }
16
+ }
17
+ /**
18
+ * The name used to compare subjects: no call parentheses, no `this.` prefix, lower-cased. A `Class.` prefix is kept
19
+ * (changed Dec 2 after review), so `Todo.save` and `User.save` are different subjects while `this.save` and `save()` are `save`.
20
+ * Nothing is split at a dot, so a module name such as `socket.io` stays whole.
21
+ */
22
+ export function normalizeSubject(name) {
23
+ let text = name.trim();
24
+ const paren = text.indexOf("(");
25
+ if (paren > 0)
26
+ text = text.slice(0, paren).trim();
27
+ if (text.toLowerCase().startsWith("this."))
28
+ text = text.slice("this.".length);
29
+ return text.trim().toLowerCase();
30
+ }
31
+ /** The subject of a question, or undefined when its prompt names no function, class, export or module (only file paths). */
32
+ export function subjectOf(item) {
33
+ for (const span of backtickedSpans(item.prompt)) {
34
+ const text = span.trim();
35
+ if (text === "" || text.includes("/") || FILE_EXTENSION.test(text))
36
+ continue;
37
+ const subject = normalizeSubject(text);
38
+ if (subject !== "")
39
+ return subject;
40
+ }
41
+ return undefined;
42
+ }
43
+ /** `concept|subject` for a question, or undefined when it has no subject (such a question is never dropped as a repeat). */
44
+ export function subjectKey(item) {
45
+ const subject = subjectOf(item);
46
+ return subject === undefined ? undefined : `${item.concept}|${subject}`;
47
+ }
48
+ /** Keeps the first question of every (concept, subject) pair. Questions without a subject are all kept. */
49
+ export function dropRepeatedSubjects(items) {
50
+ const seen = new Set();
51
+ const kept = [];
52
+ for (const item of items) {
53
+ const key = subjectKey(item);
54
+ if (key !== undefined) {
55
+ if (seen.has(key))
56
+ continue;
57
+ seen.add(key);
58
+ }
59
+ kept.push(item);
60
+ }
61
+ return kept;
62
+ }
63
+ //# sourceMappingURL=quiz-subjects.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quiz-subjects.js","sourceRoot":"","sources":["../src/quiz-subjects.ts"],"names":[],"mappings":"AAIA,+EAA+E;AAC/E,MAAM,cAAc,GAAG,8BAA8B,CAAC;AAEtD,yEAAyE;AACzE,SAAS,eAAe,CAAC,IAAY;IACnC,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,IAAI,IAAI,GAAG,CAAC,IAAM,CAAC;QACtB,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC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C7E,MAAM,OAAO,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;QACvC,IAAI,OAAO,KAAK,EAAE;YAAE,OAAO,OAAO,CAAC;IACrC,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,4HAA4H;AAC5H,MAAM,UAAU,UAAU,CAAC,IAA0C;IACnE,MAAM,OAAO,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC;IAChC,OAAO,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,IAAI,OAAO,EAAE,CAAC;AAC1E,CAAC;AAED,2GAA2G;AAC3G,MAAM,UAAU,oBAAoB,CAAiD,KAAmB;IACtG,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,MAAM,IAAI,GAAQ,EAAE,CAAC;IACrB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,GAAG,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;QAC7B,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtB,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC;gBAAE,SAAS;YAC5B,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAChB,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAClB,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,33 @@
1
+ export interface RateRule {
2
+ /** Requests allowed per window; a whole number of at least 1. */
3
+ limit: number;
4
+ /** Window length in milliseconds; a whole number of at least 1 000. */
5
+ windowMs: number;
6
+ }
7
+ export interface RateDecision {
8
+ allowed: boolean;
9
+ remaining: number;
10
+ /** Whole seconds until the window ends; 0 when the request is allowed. */
11
+ retryAfterSec: number;
12
+ }
13
+ export interface RateLimiter {
14
+ check(key: string, nowMs: number): RateDecision;
15
+ size(): number;
16
+ }
17
+ /** Limits per client key and function instance (D2, decided by Vedant Nov 30): 10-minute windows. */
18
+ export declare const HOSTED_RATE_RULES: {
19
+ structure: RateRule;
20
+ quiz: RateRule;
21
+ grade: RateRule;
22
+ };
23
+ /** Most client keys one limiter remembers; the oldest key is dropped first. */
24
+ export declare const RATE_MAX_KEYS = 5000;
25
+ /** Makes a limiter. Throws RangeError for a bad rule or `maxKeys`. */
26
+ export declare function createRateLimiter(rule: RateRule, options?: {
27
+ maxKeys?: number;
28
+ }): RateLimiter;
29
+ /**
30
+ * The key a client is counted under. IPv4 stays as it is, an IPv4-mapped IPv6 address (`::ffff:a.b.c.d`) becomes the IPv4,
31
+ * IPv6 is cut to its /64 prefix, and anything else (missing, unreadable, too long) is `unknown`. Never throws.
32
+ */
33
+ export declare function rateKey(ip: string | undefined): string;
@@ -0,0 +1,114 @@
1
+ // A fixed-window rate limiter and the client key for it (Task 8). Pure: the caller passes the time in,
2
+ // so tests need no clock. The hosted API keeps one limiter per function instance in memory.
3
+ /** Limits per client key and function instance (D2, decided by Vedant Nov 30): 10-minute windows. */
4
+ export const HOSTED_RATE_RULES = {
5
+ structure: { limit: 60, windowMs: 600_000 },
6
+ quiz: { limit: 20, windowMs: 600_000 },
7
+ grade: { limit: 60, windowMs: 600_000 },
8
+ };
9
+ /** Most client keys one limiter remembers; the oldest key is dropped first. */
10
+ export const RATE_MAX_KEYS = 5_000;
11
+ function checkWholeNumber(name, value, min) {
12
+ if (!Number.isInteger(value) || value < min)
13
+ throw new RangeError(`${name} must be a whole number of at least ${min}`);
14
+ }
15
+ /** Makes a limiter. Throws RangeError for a bad rule or `maxKeys`. */
16
+ export function createRateLimiter(rule, options = {}) {
17
+ const maxKeys = options.maxKeys ?? RATE_MAX_KEYS;
18
+ checkWholeNumber("limit", rule?.limit, 1);
19
+ checkWholeNumber("windowMs", rule?.windowMs, 1_000);
20
+ checkWholeNumber("maxKeys", maxKeys, 1);
21
+ const { limit, windowMs } = rule;
22
+ // A Map keeps insertion order, so the first entry is the oldest key.
23
+ const entries = new Map();
24
+ return {
25
+ check(key, nowMs) {
26
+ let entry = entries.get(key);
27
+ if (entry === undefined || nowMs >= entry.start + windowMs || nowMs < entry.start) {
28
+ entries.delete(key);
29
+ entry = { start: nowMs, count: 0 };
30
+ entries.set(key, entry);
31
+ while (entries.size > maxKeys) {
32
+ const oldest = entries.keys().next().value;
33
+ if (oldest === undefined)
34
+ break;
35
+ entries.delete(oldest);
36
+ }
37
+ }
38
+ if (entry.count < limit) {
39
+ entry.count += 1;
40
+ return { allowed: true, remaining: limit - entry.count, retryAfterSec: 0 };
41
+ }
42
+ // A denied request is not counted.
43
+ return { allowed: false, remaining: 0, retryAfterSec: Math.max(1, Math.ceil((entry.start + windowMs - nowMs) / 1000)) };
44
+ },
45
+ size: () => entries.size,
46
+ };
47
+ }
48
+ const MAX_IP_CHARS = 100;
49
+ const UNKNOWN_KEY = "unknown";
50
+ /** `a.b.c.d` with each part 0..255 gives the four numbers joined by dots ("010" becomes "10"); anything else gives undefined. */
51
+ function ipv4Key(text) {
52
+ const parts = text.split(".");
53
+ if (parts.length !== 4)
54
+ return undefined;
55
+ const numbers = [];
56
+ for (const part of parts) {
57
+ if (!/^\d{1,3}$/.test(part))
58
+ return undefined;
59
+ const n = Number(part);
60
+ if (n > 255)
61
+ return undefined;
62
+ numbers.push(n);
63
+ }
64
+ return numbers.join(".");
65
+ }
66
+ function groupsOf(text) {
67
+ if (text === "")
68
+ return [];
69
+ const groups = text.split(":");
70
+ return groups.every((group) => /^[0-9a-f]{1,4}$/i.test(group)) ? groups : undefined;
71
+ }
72
+ /** Eight hex groups (at most one `::`) give the first four groups plus `::/64`; anything else gives undefined. */
73
+ function ipv6Key(text) {
74
+ const halves = text.split("::");
75
+ if (halves.length > 2)
76
+ return undefined;
77
+ const head = groupsOf(halves[0] ?? "");
78
+ const tail = halves.length === 2 ? groupsOf(halves[1] ?? "") : [];
79
+ if (head === undefined || tail === undefined)
80
+ return undefined;
81
+ let groups;
82
+ if (halves.length === 2) {
83
+ const missing = 8 - head.length - tail.length;
84
+ if (missing < 1)
85
+ return undefined;
86
+ groups = [...head, ...Array(missing).fill("0"), ...tail];
87
+ }
88
+ else {
89
+ groups = head;
90
+ }
91
+ if (groups.length !== 8)
92
+ return undefined;
93
+ return `${groups
94
+ .slice(0, 4)
95
+ .map((group) => Number.parseInt(group, 16).toString(16))
96
+ .join(":")}::/64`;
97
+ }
98
+ /**
99
+ * The key a client is counted under. IPv4 stays as it is, an IPv4-mapped IPv6 address (`::ffff:a.b.c.d`) becomes the IPv4,
100
+ * IPv6 is cut to its /64 prefix, and anything else (missing, unreadable, too long) is `unknown`. Never throws.
101
+ */
102
+ export function rateKey(ip) {
103
+ if (typeof ip !== "string")
104
+ return UNKNOWN_KEY;
105
+ let text = ip.trim();
106
+ if (text === "" || text.length > MAX_IP_CHARS)
107
+ return UNKNOWN_KEY;
108
+ text = text.replace(/%[^\]]*/, "");
109
+ if (text.startsWith("[") && text.endsWith("]"))
110
+ text = text.slice(1, -1);
111
+ const mapped = /^::ffff:(.+)$/i.exec(text);
112
+ return ipv4Key(mapped?.[1] ?? text) ?? ipv6Key(text) ?? UNKNOWN_KEY;
113
+ }
114
+ //# sourceMappingURL=rate-limit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limit.js","sourceRoot":"","sources":["../src/rate-limit.ts"],"names":[],"mappings":"AAAA,uGAAuG;AACvG,4FAA4F;AAqB5F,qGAAqG;AACrG,MAAM,CAAC,MAAM,iBAAiB,GAA6D;IACzF,SAAS,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE;IAC3C,IAAI,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE;IACtC,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE;CACxC,CAAC;AAEF,+EAA+E;AAC/E,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,CAAC;AAEnC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this. */
2
+ export interface SourceRef {
3
+ path: string;
4
+ line?: number;
5
+ }
6
+ /** What a good explanation must cover: 1 to 5 key points, and an optional model answer. */
7
+ export interface Rubric {
8
+ keyPoints: string[];
9
+ modelAnswer?: string;
10
+ }
11
+ export type QuizQuestion = {
12
+ id: string;
13
+ concept: string;
14
+ kind: "mcq" | "what-if";
15
+ choices: string[];
16
+ answerIndex: number;
17
+ prompt?: string;
18
+ source?: SourceRef;
19
+ } | {
20
+ id: string;
21
+ concept: string;
22
+ kind: "explain";
23
+ prompt?: string;
24
+ source?: SourceRef;
25
+ rubric?: Rubric;
26
+ };
27
+ export type QuizAnswer = {
28
+ questionId: string;
29
+ kind: "choice";
30
+ choiceIndex: number;
31
+ } | {
32
+ questionId: string;
33
+ kind: "graded";
34
+ score: number;
35
+ };
36
+ export interface QuizResult {
37
+ /** Average points per question, from 0 to 1. */
38
+ score: number;
39
+ /** Concepts of missed questions, each once, in question order. */
40
+ missedConcepts: string[];
41
+ /** How many questions have an answer. */
42
+ answered: number;
43
+ /** How many questions the quiz has. */
44
+ total: number;
45
+ }
46
+ /** An explain answer graded below this counts as a missed concept. */
47
+ export declare const EXPLAIN_MISS_BELOW = 0.5;
48
+ export declare function scoreQuiz(questions: QuizQuestion[], answers: QuizAnswer[]): QuizResult;
@@ -0,0 +1,95 @@
1
+ // Quiz scoring: turns a quiz plus the developer's answers into a 0..1 score and missed concepts.
2
+ // Pure function. Any invalid input throws a RangeError that names the problem.
3
+ /** An explain answer graded below this counts as a missed concept. */
4
+ export const EXPLAIN_MISS_BELOW = 0.5;
5
+ function round4(x) {
6
+ return Math.round(x * 10_000) / 10_000;
7
+ }
8
+ /** Makes a lookup by id. Throws on a repeated id. */
9
+ function indexById(items, getId, label) {
10
+ const map = new Map();
11
+ for (const item of items) {
12
+ const id = getId(item);
13
+ if (map.has(id))
14
+ throw new RangeError(`duplicate ${label} id: ${id}`);
15
+ map.set(id, item);
16
+ }
17
+ return map;
18
+ }
19
+ /** Throws unless the answer fits its question (same kind, valid index or score). */
20
+ function checkAnswer(question, answer) {
21
+ const id = question.id;
22
+ if (question.kind === "explain") {
23
+ if (answer.kind !== "graded") {
24
+ throw new RangeError(`question ${id} is an explain question and needs a graded answer`);
25
+ }
26
+ const s = answer.score;
27
+ if (typeof s !== "number" || !Number.isFinite(s) || s < 0 || s > 1) {
28
+ throw new RangeError(`graded score for ${id} must be a finite number in [0, 1], got ${String(s)}`);
29
+ }
30
+ return;
31
+ }
32
+ if (answer.kind !== "choice") {
33
+ throw new RangeError(`question ${id} is a choice question and needs a choice answer`);
34
+ }
35
+ const i = answer.choiceIndex;
36
+ if (!Number.isInteger(i) || i < 0 || i >= question.choices.length) {
37
+ throw new RangeError(`choiceIndex for ${id} must be an integer in 0..${question.choices.length - 1}, got ${String(i)}`);
38
+ }
39
+ }
40
+ /** Throws unless a choice question has 2+ choices and a valid correct answerIndex (req 3.13). */
41
+ function checkQuestion(question) {
42
+ if (question.kind === "explain")
43
+ return;
44
+ const { id, choices, answerIndex } = question;
45
+ if (!Array.isArray(choices) || choices.length < 2) {
46
+ throw new RangeError(`question ${id} needs at least 2 choices`);
47
+ }
48
+ if (!Number.isInteger(answerIndex) || answerIndex < 0 || answerIndex >= choices.length) {
49
+ throw new RangeError(`answerIndex for ${id} must be an integer in 0..${choices.length - 1}, got ${String(answerIndex)}`);
50
+ }
51
+ }
52
+ /** Points for one question: 0 to 1. A missing answer earns 0. */
53
+ function pointsFor(question, answer) {
54
+ if (!answer)
55
+ return 0;
56
+ // checkAnswer already made sure the answer kind fits the question kind, so the casts are safe.
57
+ if (question.kind === "explain")
58
+ return answer.score;
59
+ return answer.choiceIndex === question.answerIndex ? 1 : 0;
60
+ }
61
+ /** Is the question a missed concept? Choice: 0 points. Explain: graded below 0.5. */
62
+ function isMissed(question, points) {
63
+ if (question.kind === "explain")
64
+ return points < EXPLAIN_MISS_BELOW;
65
+ return points === 0;
66
+ }
67
+ export function scoreQuiz(questions, answers) {
68
+ if (questions.length === 0) {
69
+ return { score: 0, missedConcepts: [], answered: 0, total: 0 };
70
+ }
71
+ const questionById = indexById(questions, (q) => q.id, "question");
72
+ for (const question of questions)
73
+ checkQuestion(question);
74
+ const answerById = indexById(answers, (a) => a.questionId, "answer");
75
+ for (const [id, answer] of answerById) {
76
+ const question = questionById.get(id);
77
+ if (!question)
78
+ throw new RangeError(`answer refers to unknown question id: ${id}`);
79
+ checkAnswer(question, answer);
80
+ }
81
+ let totalPoints = 0;
82
+ const missed = [];
83
+ for (const question of questions) {
84
+ const points = pointsFor(question, answerById.get(question.id));
85
+ totalPoints += points;
86
+ if (isMissed(question, points) && !missed.includes(question.concept)) {
87
+ missed.push(question.concept);
88
+ }
89
+ }
90
+ // Clamp so floating point error can never push the score outside 0..1,
91
+ // then round to 4 decimals so 0.7000000000000001 compares equal to a 0.7 pass mark.
92
+ const score = round4(Math.min(1, Math.max(0, totalPoints / questions.length)));
93
+ return { score, missedConcepts: missed, answered: answerById.size, total: questions.length };
94
+ }
95
+ //# sourceMappingURL=scoring.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scoring.js","sourceRoot":"","sources":["../src/scoring.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,+EAA+E;AA6C/E,sEAAsE;AACtE,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAEtC,SAAS,MAAM,CAAC,CAAS;IACvB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC;AACzC,CAAC;AAED,qDAAqD;AACrD,SAAS,SAAS,CAAI,KAAU,EAAE,KAA0B,EAAE,KAAa;IACzE,MAAM,GAAG,GAAG,IAAI,GAAG,EAAa,CAAC;IACjC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;QACvB,IAAI,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAAE,MAAM,IAAI,UA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