"use strict"; // Source-level pins for the scheduled planner agent fire's activity entry // (adpanel:scheduler-planner-activity). // // Every other scheduled action (console_command, server_start/stop, // create/modify_file, backup) records a server activity entry, but the // planner_next_to_agent case only console.logged — an unattended BILLABLE // agent run was the one action co-users watching the activity feed could not // see. The fix records planner_item_send_agent (the manual send-agent route's // action) with source: "scheduler" after a successful queue, 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). A deduped fire (the fire found an existing // queued/running job and enqueued nothing) is NOT recorded as a run. // // index.js is a ~54k-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 planner_next_to_agent case records planner_item_send_agent after a successful queue", () => { const plannerCase = windowBetween( INDEX_SRC, 'case "planner_next_to_agent": {', 'case "agent_prompt": {', "worker planner_next_to_agent case" ); assert.ok( plannerCase.includes("adpanel:scheduler-planner-activity"), "the case must carry the adpanel:scheduler-planner-activity constraint comment (audit parity between manual and scheduled planner sends)" ); const recordAt = plannerCase.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"); // Same action as the manual send (the send-agent route's // planner_item_send_agent), marked as a scheduled fire via // source: "scheduler" — the exact convention the backup/file cases use. assert.ok( plannerCase.includes('"planner_item_send_agent"'), "the recorded action must be planner_item_send_agent (the manual send's action) so the feed shows one entry shape per agent run" ); assert.ok( plannerCase.includes('{ itemId: result.itemId, chatId: result.chatId, jobId: result.jobId, source: "scheduler" }'), "the details must carry the item, chat and job ids plus source: \"scheduler\" — parity with the manual send's { itemId, chatId, jobId } details, marked as a scheduled fire" ); // 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( plannerCase.includes("userName || userId || \"scheduler\",") && plannerCase.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 item and job // rows are keyed under entry.name, which a legacy schedule's // sanitizeServerToken form can differ from), falling back to the // schedule's form. assert.ok( plannerCase.includes("result.serverName || serverName,"), "the activity entry must be keyed under the fire's resolved stored server name (fallback: the schedule's form)" ); // A deduped fire enqueued nothing and must not be recorded as a run. const skipGuardAt = plannerCase.indexOf("if (!result.skipped) {"); assert.notStrictEqual(skipGuardAt, -1, "the record must be guarded against the dedupe skip (result.skipped) — a fire that enqueued nothing is not a run"); assert.ok( skipGuardAt < recordAt, "the skip guard must wrap the recordActivity call" ); // Non-critical: a recording failure must never fail the schedule run (the // file/backup convention). assert.ok( plannerCase.includes("} catch (actErr) {") && plannerCase.includes("Failed to record planner_item_send_agent activity"), "a recording failure must be caught and warned, never fail the schedule run" ); }); test("queueSchedulerPlannerNextToAgent returns itemId and the stored server name for the activity entry", () => { const body = functionWindow(INDEX_SRC, "async function queueSchedulerPlannerNextToAgent", "queueSchedulerPlannerNextToAgent"); // The worker case has only the schedule's serverName form; the resolved // stored name and the fired item id ride on BOTH return shapes. The skip // assertion is windowed to the dedupe branch itself (from its unique query // to the first statement after the skip return): the bare tail // "itemId: ... serverName: ... }" ALSO matched the enqueue return's tail, // so a skip return that dropped itemId/serverName still shipped green — // the pin must discriminate the skip return's own shape. const skipWindowStart = body.indexOf("const existingJob = await plannerItemHasActiveAgentJob"); assert.notStrictEqual(skipWindowStart, -1, "the dedupe query is missing"); const skipWindowEnd = body.indexOf("const payloadSelection", skipWindowStart); assert.notStrictEqual(skipWindowEnd, -1, "the first statement after the skip return is missing"); const skipReturnWindow = body.slice(skipWindowStart, skipWindowEnd); assert.ok( skipReturnWindow.includes("itemId: plannerItem.id, serverName: storedServerName }"), "the dedupe skip return must carry itemId + serverName (the skipped fire is not recorded, but the shape stays uniform)" ); assert.ok( body.includes("return { ...result, itemId: plannerItem.id, serverName: storedServerName };"), "the enqueue return must carry itemId + the resolved stored server name — the case handler keys the activity entry under it" ); });