Skip to content

matthewwong525/WongStack / .agents/plugins/daily/skills/schedule

schedule

Unverified

Schedule scripts or future sessions; save, approve and manage app schedules.

FileKindSize
SKILL.mdskill md1.2 KB
references/native.mdmarkdown445 B
references/paseo.mdmarkdown426 B
references/registration.mdmarkdown920 B
references/run.mdmarkdown477 B
scripts/lib/hosts.mjsscript5.3 KB
scripts/lib/paseo-store.mjsscript7.0 KB
scripts/lib/records.mjsscript10.4 KB
scripts/lib/start-run.mjsscript6.1 KB
scripts/lib/version.mjsscript1.1 KB
scripts/paseo-presets.jsonconfig1.5 KB
scripts/presets.mjsscript5.4 KB
scripts/schedule.mjsscript5.4 KB
scripts/tidy.mjsscript20.5 KB
scripts/workspace.mjsexecutablescript15.1 KB

scripts/tidy.mjs

View on GitHub
#!/usr/bin/env node
// Puts away what WongStack leaves behind, touching nothing it did not make: every destructive step checks ownership
// first. USAGE below lists the commands; the sweep closes no chat, it only cleans leftovers.

import { execFileSync, spawn } from 'node:child_process';
import {
  appendFileSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, realpathSync,
  renameSync, rmdirSync, rmSync, statSync, unlinkSync, writeFileSync,
} from 'node:fs';
import { homedir, tmpdir } from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { isMain } from '../../../scripts/lib/cli.mjs';
import { primaryRoot, PrimaryRootError } from '../../../scripts/lib/primary-root.mjs';
import { EXIT, PaseoError, findPaseo, git, paseo, parseCommand } from '../../../scripts/lib/paseo.mjs';

const USAGE = `usage: tidy.mjs scratch [--dry-run]         make .scratch/ here and print its path
       tidy.mjs close [--discard] [--dry-run] close this workspace once this reply ends;
                                            --discard throws its unpublished work away first
       tidy.mjs sweep [--report] [--dry-run] the background tidy-up; --report prints and clears its last line`;
const COMMANDS = ['scratch', 'close', 'sweep', 'close-child'];

const HOUR = 3600 * 1000;
const DAY = 24 * HOUR;
export const OLD_MS = DAY;
export const SWEEP_EVERY_MS = 6 * HOUR;
const LOCK_STALE_MS = HOUR;
const WAIT_SECONDS = 1800;
const BUSY = new Set(['running', 'initializing']);
const DELETED = ' (deleted)';
const SELF = fileURLToPath(import.meta.url);

/** True when `file` is `dir` or inside it. */
export function inside(dir, file) {
  if (!dir || !file) return false;
  const base = dir.endsWith(path.sep) ? dir.slice(0, -1) : dir;
  return file === base || file.startsWith(base + path.sep);
}

/** A Paseo workspace is ours when it is a worktree of this repo, and never the primary checkout. */
export function ownWorkspace(ws, { commonDir, wsCommonDir, primary }) {
  return ws?.isolation === 'worktree' && Boolean(wsCommonDir) && wsCommonDir === commonDir && ws.cwd !== primary;
}

// Saved: a clean tree (ignored files do not count) and no commit missing from every remote, unless the branch's
// pull request merged at this commit. `porcelain` or `unpushed` null means git could not tell.
export function savedState({ porcelain, unpushed, merged = false }) {
  if (porcelain === null || porcelain === undefined || unpushed === null || unpushed === undefined) {
    return { saved: false, reason: 'git could not read it', files: [] };
  }
  const files = porcelain.split('\n').filter(Boolean).map(line => line.slice(3));
  if (files.length) return { saved: false, reason: 'unsaved files', files };
  if (unpushed.trim() && !merged) return { saved: false, reason: 'commits on no remote', files: [] };
  return { saved: true, reason: null, files: [] };
}

/** `gh pr view`'s answer shows the pull request merged at exactly this commit. */
export function mergedAtTip(pr, tip) {
  return pr?.state === 'MERGED' && Boolean(tip) && pr.headRefOid === tip;
}

// The pids to stop: this user's processes, not this one, whose working folder was deleted, lies under one of
// `roots`, and lies in no `live` worktree. `procs` are { pid, uid, cwd } with `cwd` as /proc/<pid>/cwd reads.
export function orphanPids(procs, { uid, self, roots, live = [] }) {
  return procs.filter(proc => {
    if (proc.uid !== uid || proc.pid === self || !proc.cwd?.endsWith(DELETED)) return false;
    const cwd = proc.cwd.slice(0, -DELETED.length);
    return roots.some(root => inside(root, cwd)) && !live.some(dir => inside(dir, cwd));
  }).map(proc => proc.pid);
}

/** A temp entry is ours and old: a `wong-` name, this user's, and it and its top-level entries untouched OLD_MS. */
export function isOldTemp(entry, { uid, now }) {
  return entry.name.startsWith('wong-') && entry.uid === uid
    && [entry.mtimeMs, ...(entry.childMtimes ?? [])].every(time => now - time > OLD_MS);
}

export function emptyReport() {
  return { closed: [], left: [], stopped: 0, deleted: 0, freed: 0, branches: [], notes: [] };
}

/** Adds one run's results to the saved ones. */
export function mergeReports(a, b) {
  const x = { ...emptyReport(), ...a };
  const y = { ...emptyReport(), ...b };
  return {
    closed: [...x.closed, ...y.closed],
    left: [...x.left, ...y.left],
    stopped: x.stopped + y.stopped,
    deleted: x.deleted + y.deleted,
    freed: x.freed + y.freed,
    branches: [...x.branches, ...y.branches],
    notes: [...x.notes, ...y.notes],
  };
}

const plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
const quoted = names => names.map(name => `"${name}"`).join(', ');

export function formatBytes(bytes) {
  if (bytes >= 1024 * 1024) return `${Math.round(bytes / (1024 * 1024))} MB`;
  if (bytes >= 1024) return `${Math.round(bytes / 1024)} KB`;
  return `${bytes} bytes`;
}

/** The one line a session start prints, or '' when the tidy-up did nothing. */
export function reportLine(report) {
  const r = { ...emptyReport(), ...report };
  const parts = [];
  if (r.closed.length) parts.push(`closed ${plural(r.closed.length, 'workspace')} (${quoted(r.closed)})`);
  if (r.branches.length) parts.push(`deleted ${plural(r.branches.length, 'merged branch', 'merged branches')}`);
  if (r.stopped) parts.push(`stopped ${plural(r.stopped, 'leftover process', 'leftover processes')}`);
  if (r.deleted) parts.push(`deleted ${plural(r.deleted, 'old temp or scratch entry', 'old temp or scratch entries')}, freeing ${formatBytes(r.freed)}`);
  for (const { name, reason } of r.left) parts.push(`left "${name}" open: ${reason}`);
  parts.push(...r.notes);
  if (!parts.length) return '';
  const text = parts.join('; ');
  return `Tidy-up: ${text}.`;
}

function tryGit(cwd, ...args) {
  try { return git(cwd, ...args); } catch { return null; }
}

function realpath(file) {
  try { return realpathSync(file); } catch { return file; }
}

function here(cwd = process.cwd()) {
  try {
    return primaryRoot(cwd);
  } catch (error) {
    if (error instanceof PrimaryRootError) throw new PaseoError(EXIT.input, error.message);
    throw error;
  }
}

/** The worktree's own branch, tip, and saved-state inputs. */
export function worktreeState(dir) {
  return {
    porcelain: tryGit(dir, 'status', '--porcelain'),
    unpushed: tryGit(dir, 'rev-list', 'HEAD', '--not', '--remotes'),
    branch: tryGit(dir, 'symbolic-ref', '--quiet', '--short', 'HEAD'),
    tip: tryGit(dir, 'rev-parse', 'HEAD'),
  };
}

/** The pull request for `branch`, as `gh pr view` shows it, or null without `gh` or a match. */
export function pullRequest(dir, branch) {
  if (!branch) return null;
  try {
    return JSON.parse(execFileSync('gh', ['pr', 'view', branch, '--json', 'number,state,headRefOid'], {
      cwd: dir, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'],
    }));
  } catch { return null; }
}

function tidyDir(commonDir) {
  const dir = path.join(commonDir, 'wong-tidy');
  mkdirSync(dir, { recursive: true });
  return dir;
}

function readJsonFile(file) {
  try { return JSON.parse(readFileSync(file, 'utf8')); } catch { return null; }
}

function writeJsonFile(file, value) {
  const temp = `${file}.${process.pid}.tmp`;
  writeFileSync(temp, `${JSON.stringify(value, null, 2)}\n`);
  renameSync(temp, file);
}

/** Merges `part` into <commonDir>/wong-tidy/report.json. */
export function addToReport(commonDir, part) {
  if (!reportLine(part)) return;
  const file = path.join(tidyDir(commonDir), 'report.json');
  writeJsonFile(file, mergeReports(readJsonFile(file) ?? {}, part));
}

/** The last tidy-up's line, once: reads and deletes the report. '' when there is none. */
export function takeReport(commonDir) {
  const file = path.join(commonDir, 'wong-tidy', 'report.json');
  if (!existsSync(file)) return '';
  const report = readJsonFile(file);
  rmSync(file, { force: true });
  return report ? reportLine(report) : '';
}

/** One lock file: false while another holder took it under LOCK_STALE_MS ago. */
export function takeLock(file, now = Date.now()) {
  try {
    writeFileSync(file, String(process.pid), { flag: 'wx' });
    return true;
  } catch (error) {
    if (error.code !== 'EEXIST') throw error;
  }
  try {
    if (now - statSync(file).mtimeMs < LOCK_STALE_MS) return false;
  } catch { /* gone meanwhile */ }
  rmSync(file, { force: true });
  try { writeFileSync(file, String(process.pid), { flag: 'wx' }); return true; } catch { return false; }
}

function sizeOf(file) {
  let stat;
  try { stat = lstatSync(file); } catch { return 0; }
  if (!stat.isDirectory()) return stat.size;
  let total = 0;
  try { for (const name of readdirSync(file)) total += sizeOf(path.join(file, name)); } catch { /* unreadable */ }
  return total;
}

function uid() {
  return typeof process.getuid === 'function' ? process.getuid() : undefined;
}

/** Every process's { pid, uid, cwd } from /proc, or null where there is no /proc. */
function readProcs(procRoot = '/proc') {
  let names;
  try { names = readdirSync(procRoot); } catch { return null; }
  if (!existsSync(path.join(procRoot, 'self', 'cwd'))) return null;
  const procs = [];
  for (const name of names) {
    if (!/^\d+$/.test(name)) continue;
    try {
      const dir = path.join(procRoot, name);
      procs.push({ pid: Number(name), uid: statSync(dir).uid, cwd: readlinkSync(path.join(dir, 'cwd')) });
    } catch { /* gone, or not ours to read */ }
  }
  return procs;
}

/** Stops orphaned processes with SIGTERM; returns how many, or null without /proc. */
function stopOrphans({ roots, live, dryRun }) {
  const procs = readProcs();
  if (!procs) return null;
  const pids = orphanPids(procs, { uid: uid(), self: process.pid, roots, live }).filter(pid => {
    const cwd = procs.find(proc => proc.pid === pid).cwd.slice(0, -DELETED.length);
    return !existsSync(cwd);
  });
  let stopped = 0;
  for (const pid of pids) {
    if (dryRun) { stopped++; continue; }
    try { process.kill(pid, 'SIGTERM'); stopped++; } catch { /* already gone */ }
  }
  return stopped;
}

function paseoWorktrees(env) {
  return path.join(env.PASEO_HOME || path.join(homedir(), '.paseo'), 'worktrees');
}

/** This repo's worktrees still on disk, and the parent folders of its linked ones under Paseo's worktrees folder. */
function worktrees(repo, env) {
  const list = (tryGit(repo.primary, 'worktree', 'list', '--porcelain') ?? '')
    .split('\n').filter(line => line.startsWith('worktree ')).map(line => line.slice('worktree '.length));
  const base = realpath(paseoWorktrees(env));
  const parents = [...new Set(list.filter(dir => dir !== repo.primary)
    .map(dir => path.dirname(realpath(dir))).filter(dir => inside(base, dir) && dir !== base))];
  return { live: list.filter(dir => existsSync(dir)).map(realpath), parents };
}

function checkList(data, what, fields) {
  if (!Array.isArray(data) || data.some(item => fields.some(field => typeof item?.[field] !== 'string'))) {
    throw new PaseoError(EXIT.client, `Paseo's ${what} output has changed: expected a list with ${fields.join(', ')}.`);
  }
  return data;
}

async function listWorkspaces(bin) {
  return checkList(await paseo(bin, ['workspace', 'ls']), 'workspace ls', ['workspaceId', 'cwd']);
}

function scratch({ dryRun }) {
  const root = tryGit(process.cwd(), 'rev-parse', '--show-toplevel');
  if (!root) throw new PaseoError(EXIT.input, 'Not inside a git checkout.');
  const dir = path.join(root, '.scratch');
  let excluded = false;
  if (tryGit(root, 'check-ignore', '-q', '.scratch/x') === null) {
    const exclude = git(root, 'rev-parse', '--path-format=absolute', '--git-path', 'info/exclude');
    if (!dryRun) {
      mkdirSync(path.dirname(exclude), { recursive: true });
      const text = existsSync(exclude) ? readFileSync(exclude, 'utf8') : '';
      appendFileSync(exclude, `${text && !text.endsWith('\n') ? '\n' : ''}.scratch/\n`);
    }
    excluded = true;
  }
  if (!dryRun) mkdirSync(dir, { recursive: true });
  return { ok: true, ...(dryRun ? { dryRun: true } : {}), path: dir, excluded };
}

const refuse = message => new PaseoError(EXIT.input, message);

function refusal(state) {
  if (state.reason === 'unsaved files') return `This workspace has unsaved work: ${state.files.join(', ')}. Save it first.`;
  if (state.reason === 'commits on no remote') return 'This workspace has commits that are not saved online yet. Save them first.';
  return 'Git could not read this workspace, so it stays open.';
}

// Throws a worktree's work away: closes the branch's open pull request, deletes the branch online, and resets
// the tree so Paseo archives it as a clean one.
function throwAway(dir, branch) {
  const notes = [];
  let pr = null;
  let remote = false;
  if (branch) {
    const open = pullRequest(dir, branch);
    if (open?.state === 'OPEN') {
      try {
        execFileSync('gh', ['pr', 'close', branch], { cwd: dir, stdio: 'ignore' });
        pr = open.number ?? branch;
      } catch { notes.push(`could not close the pull request for ${branch}`); }
    }
    if (tryGit(dir, 'ls-remote', '--exit-code', '--heads', 'origin', branch) !== null) {
      remote = tryGit(dir, 'push', '-q', 'origin', '--delete', branch) !== null;
      if (!remote) notes.push(`could not delete the branch ${branch} online`);
    }
  }
  git(dir, 'reset', '-q', '--hard');
  git(dir, 'clean', '-q', '-fd');
  return { pr, remote, ...(notes.length ? { notes } : {}) };
}

async function close({ dryRun, discard = false }, env) {
  const repo = here();
  if (!repo.linked) throw refuse('This is the main checkout, which never closes.');
  const agentId = env.PASEO_AGENT_ID?.trim();
  if (!agentId) throw refuse('This chat is not a Paseo agent, so there is no workspace to close.');
  const state = worktreeState(repo.root);
  const merged = !discard && mergedAtTip(pullRequest(repo.root, state.branch), state.tip);
  const saved = discard ? { saved: true } : savedState({ ...state, merged });
  if (!saved.saved) throw refuse(refusal(saved));
  const bin = findPaseo(env, 'TIDY_PASEO_BIN');
  const root = realpath(repo.root);
  const ws = (await listWorkspaces(bin)).find(item => realpath(item.cwd) === root);
  if (!ws) throw refuse('Paseo has no workspace for this folder.');
  const job = {
    agentId, workspaceId: ws.workspaceId, name: ws.name || path.basename(root), worktree: root,
    primary: repo.primary, commonDir: repo.commonDir, branch: state.branch, deleteBranch: discard || merged, discard,
  };
  if (dryRun) return { ok: true, dryRun: true, job };
  const discarded = discard ? throwAway(root, state.branch) : null;
  const child = spawn(process.execPath, [SELF, 'close-child'], {
    cwd: repo.primary, detached: true, stdio: 'ignore', env: { ...env, TIDY_CLOSE_JOB: JSON.stringify(job) },
  });
  child.on('error', () => {});
  child.unref();
  return {
    ok: true, workspaceId: ws.workspaceId, name: job.name, branch: job.branch, deleteBranch: job.deleteBranch,
    ...(discarded ? { discarded } : {}), message: 'Closing this workspace once this reply ends.',
  };
}

/** The detached half of `close`: wait, archive, stop leftovers, delete a merged or thrown-away branch, report. */
async function closeChild(env) {
  const job = JSON.parse(env.TIDY_CLOSE_JOB || 'null');
  if (!job?.workspaceId) throw refuse('close-child runs only from close.');
  const report = emptyReport();
  try {
    const bin = findPaseo(env, 'TIDY_PASEO_BIN');
    let waited = null;
    // A wait that throws is reported below.
    try { waited = await paseo(bin, ['wait', job.agentId, '--timeout', String(WAIT_SECONDS)]); } catch {}
    if (!waited || BUSY.has(waited.status)) {
      report.left.push({ name: job.name, reason: 'the chat never finished, so it was not closed' });
    } else {
      await paseo(bin, ['workspace', 'archive', job.workspaceId]);
      report.closed.push(job.name);
      report.stopped += stopOrphans({ roots: [job.worktree], live: [] }) ?? 0;
      if (job.branch && job.deleteBranch) {
        if (tryGit(job.primary, 'branch', '-D', job.branch) === null) report.notes.push(`kept the branch ${job.branch}: git would not delete it`);
        else if (job.discard) report.notes.push(`threw away the branch ${job.branch}`);
        else report.branches.push(job.branch);
      } else if (job.branch) {
        report.notes.push(`kept the branch ${job.branch}: its pull request did not merge at its last commit`);
      }
    }
  } catch (error) {
    report.left.push({ name: job.name, reason: `it could not be closed (${error.message})` });
  }
  addToReport(job.commonDir, report);
  return { ok: true, ...report };
}

/** Deletes old `wong-*` entries in the temp folder. */
function sweepTemp(dir, { now, dryRun, report }) {
  let names;
  try { names = readdirSync(dir).filter(name => name.startsWith('wong-')); } catch { return; }
  for (const name of names) {
    const file = path.join(dir, name);
    try {
      const stat = lstatSync(file);
      const childMtimes = stat.isDirectory() ? readdirSync(file).map(child => lstatSync(path.join(file, child)).mtimeMs) : [];
      if (!isOldTemp({ name, uid: stat.uid, mtimeMs: stat.mtimeMs, childMtimes }, { uid: uid(), now })) continue;
      const size = sizeOf(file);
      if (!dryRun) rmSync(file, { recursive: true, force: true });
      report.deleted++;
      report.freed += size;
    } catch { /* gone, or not ours to read */ }
  }
}

/** Deletes files untouched OLD_MS under `dir`, then its old empty subfolders; never `dir` itself. */
function sweepScratch(dir, { now, dryRun, report }) {
  let names;
  try { names = readdirSync(dir); } catch { return; }
  for (const name of names) {
    const file = path.join(dir, name);
    try {
      const stat = lstatSync(file);
      if (stat.isDirectory()) {
        sweepScratch(file, { now, dryRun, report });
        if (!dryRun && readdirSync(file).length === 0 && now - stat.mtimeMs > OLD_MS) rmdirSync(file);
      } else if (now - stat.mtimeMs > OLD_MS) {
        if (!dryRun) unlinkSync(file);
        report.deleted++;
        report.freed += stat.size;
      }
    } catch { /* gone meanwhile */ }
  }
}

async function sweep({ dryRun, report: printReport }, env) {
  const repo = here();
  const dir = tidyDir(repo.commonDir);
  if (printReport) return { text: takeReport(repo.commonDir) };
  const now = Date.now();
  const lock = path.join(dir, 'sweep.lock');
  const stamp = path.join(dir, 'last-sweep');
  if (!dryRun && !takeLock(lock, now)) return { ok: true, skipped: 'another tidy-up is running' };
  try {
    if (!dryRun && existsSync(stamp) && now - statSync(stamp).mtimeMs < SWEEP_EVERY_MS) {
      return { ok: true, skipped: 'the last tidy-up began under 6 hours ago' };
    }
    if (!dryRun) writeFileSync(stamp, `${new Date(now).toISOString()}\n`);
    const report = emptyReport();
    const notes = [];
    const trees = worktrees(repo, env);
    const stopped = stopOrphans({ roots: trees.parents, live: trees.live, dryRun });
    if (stopped === null) notes.push('No /proc here, so leftover processes were not checked.');
    else report.stopped += stopped;
    sweepTemp(tmpdir(), { now, dryRun, report });
    sweepScratch(path.join(repo.primary, '.scratch'), { now, dryRun, report });
    if (!dryRun) addToReport(repo.commonDir, report);
    return { ok: true, ...(dryRun ? { dryRun: true } : {}), ...report, ...(notes.length ? { skippedNotes: notes } : {}) };
  } finally {
    if (!dryRun) rmSync(lock, { force: true });
  }
}

// Flags only one command takes.
const COMMAND_FLAGS = { close: { '--discard': 'discard' }, sweep: { '--report': 'report' } };

function parseArgs(argv) {
  const booleans = { '--dry-run': 'dryRun', ...(Object.hasOwn(COMMAND_FLAGS, argv[0]) ? COMMAND_FLAGS[argv[0]] : {}) };
  const { command, flags } = parseCommand(argv, { booleans, unknown: arg => `Unknown argument ${arg}.\n${USAGE}` });
  if (!COMMANDS.includes(command)) throw new PaseoError(EXIT.input, `Unknown command "${command}". Use scratch, close, or sweep.`);
  return { command, flags };
}

async function main(argv = process.argv.slice(2), env = process.env) {
  if (argv.length === 0 || argv.includes('--help')) { process.stdout.write(`${USAGE}\n`); return EXIT.ok; }
  try {
    const { command, flags } = parseArgs(argv);
    if (command === 'sweep' && flags.report) {
      const { text } = await sweep(flags, env);
      if (text) process.stdout.write(`${text}\n`);
      return EXIT.ok;
    }
    const run = { scratch: () => scratch(flags), close: () => close(flags, env), sweep: () => sweep(flags, env), 'close-child': () => closeChild(env) };
    process.stdout.write(`${JSON.stringify(await run[command](), null, 2)}\n`);
    return EXIT.ok;
  } catch (error) {
    const code = error instanceof PaseoError ? error.code : 1;
    process.stdout.write(`${JSON.stringify({ ok: false, code, error: error.message }, null, 2)}\n`);
    return code;
  }
}

if (isMain(import.meta.url)) process.exitCode = await main();
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall