chore: v2.8.23 — workspace groups, inner repos in Changes (opt-in), sub-agent tokens counted
Workspace groups: named, foldable, ordered sidebar sections kept per account (agenthub.workspace.groups); a workspace joins one from its menu, a drop on the header, a drag under a row, or Alt+Up/Down past a group's edge; headers drag to reorder; a folded header shows its most urgent state; New workspace in this group; fold/unfold all. Inner repos: GET /api/lanes/:id/repos + ?repo= on git/fs routes; the Changes panel shows a parent folder's repos only when Settings -> Git -> Changes panel turns it on (agenthub.changes.innerRepos, off by default). Cost: the hook writer adds a session's sub-agent transcripts to its tokens and hours (server/lib/subagent-usage.js) — a sub-agent on the session's own model was never counted live (~8% of a week here); an imported session is re-raised on every pass, so a claude -p run first seen with an empty transcript no longer stays at $0 (~10%). A one-time raise-only catch-up (user_version 5) re-reads ~/.claude/projects. Also: omp install script may redirect to its exact install.redirect URL (#85, repo-links test exempts it), native paste in the terminal.
This commit is contained in:
@@ -63,12 +63,14 @@ async function stepsFor(id, action) {
|
||||
if (d.installed) throw makeErr("EINSTALLED", `${tool.label} is already installed`);
|
||||
return tool.install.kind === "npm"
|
||||
? [{ argv: npmArgv(tool.install.package, await versions.installPrefixArgs()) }]
|
||||
: [{ script: tool.install.url }];
|
||||
: [{ script: tool.install.url, redirect: tool.install.redirect }];
|
||||
}
|
||||
if (!tool.update) throw makeErr("ENOUPDATE", `${tool.label} has no update command`);
|
||||
if (!d.installed) throw makeErr("ENOTINSTALLED", `${tool.label} is not installed`);
|
||||
if (tool.update.kind === "self") return [{ argv: [d.path, ...tool.update.args] }];
|
||||
if (tool.update.kind === "script") return [{ script: tool.install.url }];
|
||||
if (tool.update.kind === "script") {
|
||||
return [{ script: tool.install.url, redirect: tool.install.redirect }];
|
||||
}
|
||||
const by = await versions.managedBy(id);
|
||||
if (!by) {
|
||||
throw makeErr(
|
||||
@@ -85,12 +87,16 @@ function append(j, text) {
|
||||
if (j.log.length > LOG_CAP) j.log = j.log.slice(-LOG_CAP);
|
||||
}
|
||||
|
||||
/** Download a vendor script to a 0600 temp file; the final URL must stay on its site. */
|
||||
async function download(url) {
|
||||
/**
|
||||
* Download a vendor script to a 0600 temp file; the final URL must stay on its
|
||||
* site, or be exactly the catalog's `redirect`.
|
||||
*/
|
||||
async function download(url, redirect) {
|
||||
const res = await fetch(url, { signal: AbortSignal.timeout(15_000), redirect: "follow" });
|
||||
const final = new URL(res.url || url);
|
||||
const site = aiTools.siteOf(new URL(url).hostname);
|
||||
if (final.protocol !== "https:" || aiTools.siteOf(final.hostname) !== site) {
|
||||
const onSite = final.protocol === "https:" && aiTools.siteOf(final.hostname) === site;
|
||||
if (!onSite && final.href !== redirect) {
|
||||
throw new Error(`script redirected off ${site}: ${final.href}`);
|
||||
}
|
||||
if (!res.ok) throw new Error(`script download failed: HTTP ${res.status}`);
|
||||
@@ -169,7 +175,7 @@ async function runSteps(j, steps, onStepOk) {
|
||||
let tmp = null;
|
||||
if (step.script) {
|
||||
append(j, `$ download ${step.script}\n`);
|
||||
tmp = await download(step.script);
|
||||
tmp = await download(step.script, step.redirect);
|
||||
argv = ["bash", tmp];
|
||||
}
|
||||
try {
|
||||
|
||||
+17
-4
@@ -44,18 +44,24 @@ const isStrArray = (v, re) =>
|
||||
// wrong for co.uk-style hosts (none in the catalog); swap in a PSL if one comes.
|
||||
const siteOf = (host) => host.toLowerCase().split(".").slice(-2).join(".");
|
||||
|
||||
/** The script URL, when it is https on the homepage's site; else null. */
|
||||
function scriptUrl(url, homepageUrl) {
|
||||
/** A plain https URL (no userinfo, no port), normalised; else null. */
|
||||
function httpsUrl(url) {
|
||||
try {
|
||||
const u = new URL(url);
|
||||
const home = new URL(homepageUrl);
|
||||
if (u.protocol !== "https:" || u.username || u.password || u.port) return null;
|
||||
return siteOf(u.hostname) === siteOf(home.hostname) ? u.href : null;
|
||||
return u;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** The script URL, when it is https on the homepage's site; else null. */
|
||||
function scriptUrl(url, homepageUrl) {
|
||||
const u = httpsUrl(url);
|
||||
const home = httpsUrl(homepageUrl);
|
||||
return u && home && siteOf(u.hostname) === siteOf(home.hostname) ? u.href : null;
|
||||
}
|
||||
|
||||
/** `{install, update}` normalised, or null when either is malformed. */
|
||||
function installFields(doc) {
|
||||
let install = null;
|
||||
@@ -67,6 +73,13 @@ function installFields(doc) {
|
||||
const url = scriptUrl(i.url, doc.homepageUrl);
|
||||
if (!url) return null;
|
||||
install = { kind: "script", url };
|
||||
// The ONE exact URL the download may redirect to off the homepage's site
|
||||
// (omp.sh/install → raw.githubusercontent.com); never a site wildcard.
|
||||
if (i.redirect !== undefined) {
|
||||
const to = typeof i.redirect === "string" && httpsUrl(i.redirect);
|
||||
if (!to) return null;
|
||||
install.redirect = to.href;
|
||||
}
|
||||
} else return null;
|
||||
}
|
||||
let update = null;
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* @file nested-repos.js
|
||||
* @description The git repositories INSIDE a workspace folder — a parent
|
||||
* folder holding several repos side by side (`~/work/shop/{api,web,admin}`),
|
||||
* each with its own `.git` — so the Changes panel can show one section per
|
||||
* repo, like VS Code's Source Control. Found by reading the disk only (no git
|
||||
* call): a directory holding a `.git` entry (a directory, or the file a
|
||||
* submodule or linked worktree has) is a repo, and the walk does not go
|
||||
* inside one. At most `MAX_DEPTH` levels below the workspace folder, never
|
||||
* through a symlink, never into a dot directory or `node_modules`/`vendor`,
|
||||
* at most `MAX_REPOS` repos and `MAX_ENTRIES` directory entries read, so a
|
||||
* folder with a huge tree (or a member flooding theirs) costs a bounded read.
|
||||
* Cached per folder for `TTL_MS`; `refresh` reads again.
|
||||
*
|
||||
* `repoFolder` is the git routes' `?repo=<path>`: the request names a path
|
||||
* RELATIVE to the workspace folder, and it is accepted only when it is one of
|
||||
* the repos this walk found — never a path the walk did not produce, so the
|
||||
* request can no more point the git routes at an arbitrary directory than
|
||||
* `?source=` can.
|
||||
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
||||
*/
|
||||
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const MAX_DEPTH = 2;
|
||||
const MAX_REPOS = 50;
|
||||
const MAX_ENTRIES = 5000;
|
||||
const TTL_MS = 30_000;
|
||||
const REREAD_MS = 2_000;
|
||||
const SKIP = new Set(["node_modules", "vendor", "dist", "build", "target", "__pycache__"]);
|
||||
|
||||
const cache = new Map();
|
||||
|
||||
function fail(code, message) {
|
||||
return Object.assign(new Error(message), { code });
|
||||
}
|
||||
|
||||
async function hasGit(dir) {
|
||||
try {
|
||||
await fs.promises.lstat(path.join(dir, ".git"));
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether `dir` or a folder above it holds a `.git` — the folder is itself in a repo. */
|
||||
async function insideRepo(dir) {
|
||||
for (let d = dir; ;) {
|
||||
if (await hasGit(d)) return true;
|
||||
const up = path.dirname(d);
|
||||
if (up === d) return false;
|
||||
d = up;
|
||||
}
|
||||
}
|
||||
|
||||
async function walk(root) {
|
||||
const repos = [];
|
||||
let budget = MAX_ENTRIES;
|
||||
let truncated = false;
|
||||
const visit = async (dir, depth) => {
|
||||
let handle;
|
||||
try {
|
||||
handle = await fs.promises.opendir(dir);
|
||||
} catch {
|
||||
return; // unreadable: not ours to report
|
||||
}
|
||||
const children = [];
|
||||
try {
|
||||
for await (const ent of handle) {
|
||||
if (--budget < 0) {
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
// `isDirectory()` is false for a symlink: a link is never followed.
|
||||
if (!ent.isDirectory() || ent.name.startsWith(".") || SKIP.has(ent.name)) continue;
|
||||
children.push(ent.name);
|
||||
}
|
||||
} catch {
|
||||
/* a directory that vanished mid-read */
|
||||
}
|
||||
children.sort((a, b) => a.localeCompare(b));
|
||||
for (const name of children) {
|
||||
if (repos.length >= MAX_REPOS) {
|
||||
truncated = true;
|
||||
return;
|
||||
}
|
||||
const abs = path.join(dir, name);
|
||||
if (await hasGit(abs)) {
|
||||
const rel = path.relative(root, abs).split(path.sep).join("/");
|
||||
repos.push({ path: rel, name: rel });
|
||||
} else if (depth < MAX_DEPTH) {
|
||||
await visit(abs, depth + 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
await visit(root, 1);
|
||||
return { items: repos, truncated };
|
||||
}
|
||||
|
||||
/**
|
||||
* The repos below `cwd`, plus whether `cwd` itself is in a repo (`root`).
|
||||
* @param {string} cwd The workspace folder (absolute).
|
||||
* @param {{refresh?: boolean}} [opts]
|
||||
* @returns {Promise<{root: boolean, items: {path: string, name: string}[], truncated: boolean}>}
|
||||
*/
|
||||
async function nestedRepos(cwd, { refresh = false } = {}) {
|
||||
const hit = cache.get(cwd);
|
||||
if (!refresh && hit && Date.now() - hit.at < TTL_MS) return hit.value;
|
||||
const pending = (async () => {
|
||||
const [root, found] = await Promise.all([insideRepo(cwd), walk(cwd)]);
|
||||
return { root, ...found };
|
||||
})();
|
||||
cache.set(cwd, { at: Date.now(), value: pending });
|
||||
try {
|
||||
return await pending;
|
||||
} catch (err) {
|
||||
cache.delete(cwd);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The directory `?repo=<rel>` names: only a repo `nestedRepos` found under
|
||||
* the workspace folder (re-read if the cached list does not have it — a repo
|
||||
* cloned in the last 30 s — but at most once per `REREAD_MS`, so a stream of
|
||||
* bad names cannot keep the disk walking).
|
||||
* @throws ENOREPO otherwise.
|
||||
*/
|
||||
async function repoFolder(cwd, rel) {
|
||||
const want = String(rel);
|
||||
const find = (list) => list.items.find((r) => r.path === want);
|
||||
let hit = find(await nestedRepos(cwd));
|
||||
if (!hit && Date.now() - (cache.get(cwd)?.at ?? 0) >= REREAD_MS) {
|
||||
hit = find(await nestedRepos(cwd, { refresh: true }));
|
||||
}
|
||||
if (!hit) throw fail("ENOREPO", "that folder is not a repository inside this workspace");
|
||||
return path.join(cwd, ...hit.path.split("/"));
|
||||
}
|
||||
|
||||
/** Tests only. */
|
||||
function clearNestedRepoCache() {
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
module.exports = { nestedRepos, repoFolder, clearNestedRepoCache, MAX_DEPTH, MAX_REPOS };
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* @file subagent-usage — the token usage of a Claude session's sub-agents (the
|
||||
* `agent-*.jsonl` transcripts Claude Code writes beside the session's own, in
|
||||
* `<dir>/<session id>/subagents/` or `<dir>/subagents/<session id>/` — the
|
||||
* same places `findSessionSubagents` in scripts/import-history.js looks),
|
||||
* summed per pricing bucket and per UTC hour. The hook writer adds it to the
|
||||
* main transcript's usage, so a session's total and its hours count every
|
||||
* sub-agent, the same total the importer's `combineSessionTokens` writes
|
||||
* (both go through the high-water `replaceTokenUsage`, so neither can inflate
|
||||
* the other). Workflow-tool agents (`subagents/workflows/…`) are left out:
|
||||
* `workflow-ingest.js` books those under their own service tier. Parsing goes
|
||||
* through a TranscriptCache of its own (stat-cached, incremental), so the main
|
||||
* transcripts' cache is never crowded out.
|
||||
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
||||
*/
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const TranscriptCache = require("./transcript-cache");
|
||||
const { emptyBucket, accumulateBucket } = require("./token-usage");
|
||||
|
||||
const cache = new TranscriptCache(500);
|
||||
|
||||
/** The session's sub-agent transcripts, flat ones only. */
|
||||
function subagentFiles(transcriptPath) {
|
||||
const dir = path.dirname(transcriptPath);
|
||||
const sessionId = path.basename(transcriptPath, ".jsonl");
|
||||
const out = [];
|
||||
for (const d of [
|
||||
path.join(dir, sessionId, "subagents"),
|
||||
path.join(dir, "subagents", sessionId),
|
||||
]) {
|
||||
let names;
|
||||
try {
|
||||
names = fs.readdirSync(d);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
for (const n of names) if (n.endsWith(".jsonl")) out.push(path.join(d, n));
|
||||
}
|
||||
return out.sort();
|
||||
}
|
||||
|
||||
/** `a` plus `b`, bucket by bucket (an hour bucket keeps its `hour`); never mutates. */
|
||||
function addTokens(a, b) {
|
||||
const out = {};
|
||||
for (const src of [a, b]) {
|
||||
for (const [key, t] of Object.entries(src || {})) {
|
||||
if (!out[key]) {
|
||||
out[key] = emptyBucket(t.model, t.speed, t.geo, t.tier);
|
||||
if (t.hour) out[key].hour = t.hour;
|
||||
}
|
||||
accumulateBucket(out[key], t);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The sub-agents' usage of the session whose transcript is `transcriptPath`:
|
||||
* `{tokensByModel, tokensByHour, parts}` (`parts` = each file's cached parse
|
||||
* result, the same object while that file is unchanged), or null when it has
|
||||
* no sub-agent with usage.
|
||||
*/
|
||||
function subagentUsage(transcriptPath) {
|
||||
if (typeof transcriptPath !== "string" || !transcriptPath.endsWith(".jsonl")) return null;
|
||||
const parts = [];
|
||||
for (const f of subagentFiles(transcriptPath)) {
|
||||
let r = null;
|
||||
try {
|
||||
r = cache.extract(f);
|
||||
} catch {
|
||||
/* unreadable or vanished: skipped this time */
|
||||
}
|
||||
if (r && r.tokensByModel) parts.push(r);
|
||||
}
|
||||
if (!parts.length) return null;
|
||||
let tokensByModel = {};
|
||||
let tokensByHour = {};
|
||||
for (const r of parts) {
|
||||
tokensByModel = addTokens(tokensByModel, r.tokensByModel);
|
||||
tokensByHour = addTokens(tokensByHour, r.tokensByHour);
|
||||
}
|
||||
return { tokensByModel, tokensByHour, parts };
|
||||
}
|
||||
|
||||
/** Whether two `parts` lists are the same parses (nothing grew in between). */
|
||||
function sameParts(a, b) {
|
||||
const x = a || [];
|
||||
const y = b || [];
|
||||
return x.length === y.length && x.every((r, i) => r === y[i]);
|
||||
}
|
||||
|
||||
module.exports = { subagentUsage, subagentFiles, addTokens, sameParts };
|
||||
Reference in New Issue
Block a user