Fifth revision: share-link AI safety, sturdier generation, cleaner UI
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 1m0s

- Malformed model JSON is sent back for repair, then retried; a failed job
  starts over once before it fails. Visitors can dismiss failed sets.
- Generated sets get a short distinct name and description; flashcards are
  atomic and have no hints; standard study shows known/learning counts.
- Share links: "In progress" list, finished sets join the normal lists with a
  "New" tag, plain progress steps, newest exam first.
- Share-link AI expires after a week, can be limited to the US or Arizona
  (DB-IP Lite or Cloudflare headers) and is locked by abuse protection
  (migration 8).
- The assistant never answers from general knowledge; MCP and web search
  tools load without a manual test.
- Plain-text copy, no question count in exam mode, filler text removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-16 09:17:50 -07:00
parent d8df5edf30
commit 85d5c9186d
46 changed files with 1242 additions and 283 deletions

View file

@ -27,6 +27,8 @@ export type McpAreas = Record<McpArea, boolean>;
export const DEFAULT_MCP_AREAS: McpAreas = { chat: true, explain: true, generate: false, check: false, share: false };
const PROTOCOL_VERSION = '2025-06-18';
const LIST_TIMEOUT_MS = 10_000;
/** A first connection (sign-in refresh, session set-up) can take longer than a warm listing. */
const FIRST_LIST_TIMEOUT_MS = 20_000;
const CALL_TIMEOUT_MS = 90_000;
const TOOLS_TTL_MS = 5 * 60_000;
const MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
@ -742,11 +744,19 @@ export async function loadMcpTools(app: AppContext, area: McpArea, signal?: Abor
const lists = await Promise.all(
servers.map(async (s) => {
try {
return { s, tools: await listTools(app, s, anySignal([signal, AbortSignal.timeout(LIST_TIMEOUT_MS)])) };
} catch (err) {
app.log('warn', `MCP server "${s.name}" is unavailable`, err instanceof Error ? err.message : err);
unavailable.push(s.name);
return { s, tools: [] as McpTool[] };
const warm = sessions.has(s.id);
return { s, tools: await listTools(app, s, anySignal([signal, AbortSignal.timeout(warm ? LIST_TIMEOUT_MS : FIRST_LIST_TIMEOUT_MS)])) };
} catch (first) {
if (signal?.aborted) return { s, tools: [] as McpTool[] };
// One more try on a fresh connection before giving up on the server for this turn.
try {
forget(s.id);
return { s, tools: await listTools(app, s, anySignal([signal, AbortSignal.timeout(FIRST_LIST_TIMEOUT_MS)]), true) };
} catch (err) {
app.log('warn', `MCP server "${s.name}" is unavailable`, err instanceof Error ? err.message : (first instanceof Error ? first.message : err));
unavailable.push(s.name);
return { s, tools: [] as McpTool[] };
}
}
}),
);
@ -802,7 +812,7 @@ export function mcpPromptNote(mcp: LoadedMcp, opts: { askBeforeWeb?: boolean } =
const others = mcp.servers.filter((s) => s.builtin !== 'web');
if (web) {
const when = opts.askBeforeWeb
? 'Their own documents come first. When the documents do not answer a question, do not answer from your own knowledge and do not search on your own: call ask_user with one choice question, e.g. "Your class materials do not cover this. Should I look it up on the web?" with the options "Search the web" (first, the default) and "Answer from general knowledge", then stop. Search straight away only when the user asked you to search or look something up online, or already said yes to searching in this conversation.'
? 'Their own documents come first. When the documents do not answer a question, never answer from your own knowledge and do not search on your own: call ask_user with one choice question, e.g. "Your class materials do not cover this. Should I look it up on the web?" with the options "Search the web" (first, the default) and "No, skip it", then stop. If they choose not to search, say you do not have information to answer it. Search straight away only when the user asked you to search or look something up online, or already said yes to searching in this conversation.'
: "Use it for current facts, guidelines, drug information and anything the student's own material does not cover. For questions about their course material, use their documents first.";
lines.push(
`Web search: you can search the web with web_search and read specific pages with web_fetch${web.research ? ', and answer a hard question in depth with web_research (slower; use it sparingly)' : ''}. ${when}${
@ -823,6 +833,29 @@ export function mcpPromptNote(mcp: LoadedMcp, opts: { askBeforeWeb?: boolean } =
}
/** Connects afresh, lists the tools and records the outcome on the server. */
/**
* Connects to every enabled server and lists its tools in the background, so the web search and
* MCP tools are ready the first time the AI needs them (no manual connection test).
*/
export function warmMcpServers(app: AppContext): void {
if (app.config.env === 'test') return;
for (const s of load(app).filter((x) => x.enabled)) {
void listTools(app, s, AbortSignal.timeout(FIRST_LIST_TIMEOUT_MS), true).catch((err) => app.log('warn', `MCP server "${s.name}" could not be reached yet`, err instanceof Error ? err.message : err));
}
}
/** Keeps the tool lists warm while the server runs. */
export function startMcpWarmup(app: AppContext): () => void {
const first = setTimeout(() => warmMcpServers(app), 3_000);
const timer = setInterval(() => warmMcpServers(app), TOOLS_TTL_MS - 30_000);
first.unref();
timer.unref();
return () => {
clearTimeout(first);
clearInterval(timer);
};
}
export async function testMcpServer(app: AppContext, id: string): Promise<{ ok: boolean; message: string; tools: { name: string; description: string }[]; latencyMs: number }> {
const servers = load(app);
const s = servers.find((x) => x.id === id);