"use strict"; // Source-level pins for the scheduled custom-prompt agent fire's activity // entry (adpanel:scheduler-agent-prompt-activity). // // Mirror of scheduler-planner-activity.test.js for the agent_prompt sibling: // every other scheduled action (console_command, server_start/stop, // create/modify_file, backup, planner_next_to_agent) records a server // activity entry, but the agent_prompt case only console.logged — an // unattended BILLABLE agent run was invisible in the per-server activity // feed. No interactive counterpart records a custom-prompt run (the chat // path records no activity), so the entry uses agent_prompt_send with // ids/metadata only, marked source: "scheduler", under the schedule // creator's identity and the "scheduler" ip (the convention every sibling // scheduled case uses; the activity endpoint masks email identities to the // local part at read time). The prompt payload text is user content and is // NEVER recorded (the same disclosure doctrine as the tasks-GET payload // strip). // // index.js is a ~58k-line application bundle (module side effects, DB and // network at require time — never require() it from a test), so these tests // pin the fix structurally on well-anchored source windows. const test = require("node:test"); const assert = require("node:assert"); const fs = require("node:fs"); const path = require("node:path"); const ROOT = path.join(__dirname, ".."); const INDEX_SRC = fs.readFileSync(path.join(ROOT, "index.js"), "utf8"); function windowBetween(src, startAnchor, endAnchor, label) { const start = src.indexOf(startAnchor); assert.notStrictEqual(start, -1, `${label}: start anchor not found: ${startAnchor}`); const end = src.indexOf(endAnchor, start + startAnchor.length); assert.notStrictEqual(end, -1, `${label}: end anchor not found after start: ${endAnchor}`); return src.slice(start, end); } function functionWindow(src, anchor, label) { const start = src.indexOf(anchor); assert.notStrictEqual(start, -1, `${label}: anchor not found: ${anchor}`); const end = src.indexOf("\n}", start); assert.notStrictEqual(end, -1, `${label}: no closing brace after: ${anchor}`); return src.slice(start, end + 2); } test("the scheduler's agent_prompt case records agent_prompt_send after a successful queue", () => { const promptCase = windowBetween( INDEX_SRC, 'case "agent_prompt": {', "\n default:", "worker agent_prompt case" ); assert.ok( promptCase.includes("adpanel:scheduler-agent-prompt-activity"), "the case must carry the adpanel:scheduler-agent-prompt-activity constraint comment (audit parity with adpanel:scheduler-planner-activity)" ); const recordAt = promptCase.indexOf("recordActivity("); assert.notStrictEqual(recordAt, -1, "the case must record an activity entry — the unattended agent run is billable and every sibling scheduled case records one"); // No interactive counterpart records a custom agent prompt, so this is a // dedicated action (not the planner send's planner_item_send_agent), // marked as a scheduled fire via source: "scheduler" — the exact // convention the backup/file/planner cases use. assert.ok( promptCase.includes('"agent_prompt_send"'), "the recorded action must be agent_prompt_send" ); // Ids/metadata only: the prompt payload text is user content and must // never reach the activity feed (tasks-GET payload-strip doctrine). assert.ok( promptCase.includes('{ jobId: result.jobId, source: "scheduler" }'), "the details must carry the job id plus source: \"scheduler\" — ids/metadata only, never the prompt payload" ); assert.ok( !promptCase.includes("payload?.prompt") && !promptCase.includes("payload.prompt") && !promptCase.includes("result.prompt") && !promptCase.includes("JSON.stringify(payload"), "the case must not record the prompt payload text in any form" ); // Identity/ip convention of the sibling scheduled cases: the schedule // creator's identity (email falls back to id falls back to the literal // "scheduler") and the literal "scheduler" ip. The activity endpoint masks // email identities to the local part at read time. assert.ok( promptCase.includes("userName || userId || \"scheduler\",") && promptCase.includes("\"scheduler\"\n"), "the entry identity must follow the scheduled console/backup convention: userName || userId || 'scheduler', ip 'scheduler'" ); // Key the entry under the STORED name the fire resolved (the job row is // keyed under entry.name, which a legacy schedule's sanitizeServerToken // form can differ from), falling back to the schedule's form. assert.ok( promptCase.includes("result.serverName || serverName,"), "the activity entry must be keyed under the fire's resolved stored server name (fallback: the schedule's form)" ); // Non-critical: a recording failure must never fail the schedule run (the // file/backup/planner convention). assert.ok( promptCase.includes("} catch (actErr) {") && promptCase.includes("Failed to record agent_prompt_send activity"), "a recording failure must be caught and warned, never fail the schedule run" ); }); test("queueSchedulerPromptToAgent returns the stored server name for the activity entry", () => { const body = functionWindow(INDEX_SRC, "async function queueSchedulerPromptToAgent", "queueSchedulerPromptToAgent"); // The worker case has only the schedule's serverName form; the resolved // stored name rides on the return so the case handler keys the activity // entry under it (the same propagation queueSchedulerPlannerNextToAgent // does for its worker's entry). assert.ok( body.includes("return { ...insertResult, serverName: storedServerName };"), "the return must carry the resolved stored server name — the case handler keys the activity entry under it" ); });