longtable 0.1.39 → 0.1.40

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "longtable",
3
- "version": "0.1.39",
3
+ "version": "0.1.40",
4
4
  "description": "Brings your own Claude Code or Codex to your team's table on longtable.chat, quietly, on your own plan.",
5
5
  "homepage": "https://longtable.chat",
6
6
  "repository": {
package/src/repo.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  // The connection's own clean copy of the team's code: the latest main, never the person's work folder.
2
2
 
3
3
  import { execFileSync } from "node:child_process";
4
- import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
4
+ import { cpSync, existsSync, mkdirSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
5
5
  import { join, resolve } from "node:path";
6
6
  import { fileURLToPath } from "node:url";
7
7
  import { home, log } from "./store.mjs";
@@ -537,6 +537,50 @@ function copyIncluded(repo, dir) {
537
537
  // Opens (or reopens) the worktree for a job. `branch` is the task's branch for work, or the branch to read for a
538
538
  // review; with no branch it reads the default one. Work on a branch that already exists picks up where it left off.
539
539
  // A job that only reads gets a worktree of its own (`job`), removed when it ends, so two jobs never share one.
540
+ // The working copies Longtable made in a person's own repository (under ~/.longtable/trees), tidied when no job runs.
541
+ // One with changes not committed is never touched. A clean one whose branch holds nothing of its own (every commit on
542
+ // it is on another branch or remote too) goes, branch and all: there is nothing to lose. A clean one whose branch does
543
+ // hold work loses only its folder, once it has been quiet for a week; the branch and its commits stay, and a follow-up
544
+ // lays a fresh folder. The person's own worktrees are never looked at.
545
+ export function tidyTrees(repos, { quietDays = 7 } = {}) {
546
+ // Compared by real path: git names a worktree by its real path, and the same folder can be reached another way (a
547
+ // Mac's /var is /private/var; Windows can name a folder by its short name).
548
+ const real = (p) => { try { return realpathSync.native(p); } catch { return resolve(p); } };
549
+ const key = (p) => real(p).replace(/\\/g, "/").toLowerCase() + "/";
550
+ const ours = key(join(home, "trees"));
551
+ const out = { removed: [], deleted: [], kept: [] };
552
+ for (const repo of [...new Set((repos || []).filter(Boolean))]) {
553
+ let list = "";
554
+ try { list = tgit(repo, ["worktree", "list", "--porcelain"]); } catch { continue; }
555
+ for (const block of list.split(/\r?\n\r?\n/)) {
556
+ const path = /^worktree (.+)$/m.exec(block)?.[1];
557
+ const ref = /^branch (refs\/heads\/.+)$/m.exec(block)?.[1] || null;
558
+ if (!path || !existsSync(path) || !key(path).startsWith(ours)) continue;
559
+ let dirty = true;
560
+ try { dirty = tgit(path, ["status", "--porcelain"]) !== ""; } catch {}
561
+ if (dirty) { out.kept.push({ path, why: "changes not committed" }); continue; }
562
+ const branch = ref ? ref.slice("refs/heads/".length) : null;
563
+ const mine = !!branch && branch.startsWith("longtable/");
564
+ let unique = false;
565
+ if (mine) {
566
+ try {
567
+ const tip = tgit(repo, ["rev-parse", ref]);
568
+ unique = !tgit(repo, ["for-each-ref", "--contains", tip, "--format=%(refname)"]).split(/\r?\n/).some((r) => r && !r.startsWith("refs/heads/longtable/"));
569
+ } catch { unique = true; }
570
+ }
571
+ if (unique) {
572
+ let at = 0;
573
+ try { at = Number(tgit(path, ["log", "-1", "--format=%ct"])) * 1000; } catch {}
574
+ if (Date.now() - at < quietDays * 24 * 3600 * 1000) { out.kept.push({ path, why: "work from this week" }); continue; }
575
+ }
576
+ try { tgit(repo, ["worktree", "remove", "--force", path]); out.removed.push(path); } catch { out.kept.push({ path, why: "could not be removed" }); continue; }
577
+ if (mine && !unique) { try { tgit(repo, ["branch", "-D", branch]); out.deleted.push(branch); } catch {} }
578
+ }
579
+ try { tgit(repo, ["worktree", "prune"]); } catch {}
580
+ }
581
+ return out;
582
+ }
583
+
540
584
  export function openTree(seat, branch, { doing = false, job = "read" } = {}) {
541
585
  const table = safeTable(seat.table_id);
542
586
  if (branch) safeBranch(branch);
package/src/run.mjs CHANGED
@@ -20,7 +20,7 @@ import { client } from "./api.mjs";
20
20
  import { ask, findClaude, stopAll } from "./claude.mjs";
21
21
  import { askCodex, codexLimits, findCodex } from "./codex.mjs";
22
22
  import { packCode } from "./pack.mjs";
23
- import { changedSoFar, closeTree, fileFolder, folderChanges, folderState, freshCopy, isAddress, pushPlan, sourceFor, trustedRepo, landTree, landWork, neverCommit, openBranch, openTree, openWork, ownFolder, layTree, patchOf, realRepo, refreshBranch, safeTable, treeChanges, undoCommit, workBranch } from "./repo.mjs";
23
+ import { changedSoFar, closeTree, fileFolder, folderChanges, folderState, freshCopy, isAddress, pushPlan, sourceFor, trustedRepo, landTree, landWork, neverCommit, openBranch, openTree, openWork, ownFolder, layTree, tidyTrees, patchOf, realRepo, refreshBranch, safeTable, treeChanges, undoCommit, workBranch } from "./repo.mjs";
24
24
  import { alwaysRules } from "./approve-server.mjs";
25
25
  import { fileURLToPath } from "node:url";
26
26
  import { cut, hideSecrets, home, loadSecret, log, readConfig, writeWhole } from "./store.mjs";
@@ -1571,6 +1571,17 @@ export async function runLoop({ pollMs = 1000, backup = false } = {}) {
1571
1571
  setInterval(checkRelease, 10 * 60 * 1000).unref?.();
1572
1572
  // Two lanes on one connection: long work in one, everything quick in the other, so an hour of work never keeps
1573
1573
  // the table from hearing people or answering a question.
1574
+ // The working copies it left in people's own repositories are tidied at start and every six hours, only while no job
1575
+ // runs (a job may be using one).
1576
+ const tidy = () => {
1577
+ if (busy > 0 || claiming > 0) return;
1578
+ try {
1579
+ const t = tidyTrees(seats.filter((s) => s.kind === "claude").map((s) => realRepo(s)));
1580
+ if (t.removed.length || t.kept.length) log(`tidied its working copies: removed ${t.removed.length} (and ${t.deleted.length} empty branches), left ${t.kept.length}${t.kept.length ? ` (${[...new Set(t.kept.map((k) => k.why))].join(", ")})` : ""}`);
1581
+ } catch (e) { log(`could not tidy its working copies: ${e.message}`); }
1582
+ };
1583
+ tidy();
1584
+ setInterval(tidy, 6 * 3600 * 1000).unref?.();
1574
1585
  // Declared before the lanes start: they read it the moment a full-mode job arrives.
1575
1586
  let aside = 0;
1576
1587
  await Promise.all([lane("light"), lane("heavy")]);