teapot-coding-agent 0.37.15 → 0.37.16
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/dist/agent/agent.js +6 -0
- package/dist/master.js +92 -30
- package/dist/migrations/index.js +70 -0
- package/dist/migrations/providers.js +41 -0
- package/dist/migrations/registry.js +14 -0
- package/dist/migrations/session-titles.js +110 -0
- package/dist/migrations/sessions.js +52 -0
- package/package.json +1 -1
- package/public/assets/{index-cGRyrdfU.js → index-CxTEPeTm.js} +4 -4
- package/public/assets/{index-cGRyrdfU.js.map → index-CxTEPeTm.js.map} +1 -1
- package/public/assets/{shiki-BfdZXdbK.js → shiki-CKT56XTC.js} +2 -2
- package/public/assets/{shiki-BfdZXdbK.js.map → shiki-CKT56XTC.js.map} +1 -1
- package/public/index.html +1 -1
package/dist/agent/agent.js
CHANGED
|
@@ -553,6 +553,7 @@ export class Agent {
|
|
|
553
553
|
contextTokenBudget: 96_000,
|
|
554
554
|
contextWindowTokens: 0,
|
|
555
555
|
restoreSession: true,
|
|
556
|
+
onFirstUserPrompt: () => { },
|
|
556
557
|
globalSkillsDir: "",
|
|
557
558
|
pluginSkillRoots: [],
|
|
558
559
|
shortContext: false,
|
|
@@ -1953,6 +1954,11 @@ export class Agent {
|
|
|
1953
1954
|
...(images?.length ? { images: images.map((i) => ({ url: i.url, name: i.name })) } : {}),
|
|
1954
1955
|
...(promptId ? { promptId } : {}),
|
|
1955
1956
|
}))
|
|
1957
|
+
.then(() => {
|
|
1958
|
+
// a session's label comes from its FIRST user prompt; the host stores it once
|
|
1959
|
+
if (source === "user")
|
|
1960
|
+
this.opts.onFirstUserPrompt?.(this.currentSession);
|
|
1961
|
+
})
|
|
1956
1962
|
.then(() => bus.emit("update", { kind: "agent-update", agentId: this.opts.id }))
|
|
1957
1963
|
.catch(() => { });
|
|
1958
1964
|
return promptId;
|
package/dist/master.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* delegated to subprocesses managed by the bash tool with hard timeouts.
|
|
5
5
|
*/
|
|
6
6
|
import { closeAllTerminals, closeTerminalsFor } from "./server/terminals.js";
|
|
7
|
+
import { MIGRATIONS, runMigrations } from "./migrations/registry.js";
|
|
7
8
|
import { readFileSync, existsSync, mkdirSync, writeFileSync, readdirSync, statSync, lstatSync, renameSync, unlinkSync, openSync, closeSync, readSync, fsyncSync, rmSync, } from "node:fs";
|
|
8
9
|
import { randomUUID } from "node:crypto";
|
|
9
10
|
import { currentKey, makeKeyPool, resetPool } from "./agent/key-pool.js";
|
|
@@ -785,6 +786,32 @@ export class Master {
|
|
|
785
786
|
}
|
|
786
787
|
async start() {
|
|
787
788
|
mkdirSync(this.config.dataDir, { recursive: true });
|
|
789
|
+
/**
|
|
790
|
+
* Clear the flag BEFORE the migrations, never after.
|
|
791
|
+
*
|
|
792
|
+
* `finally` below flushes only when `configDirty` is set, so a reset placed after the migration
|
|
793
|
+
* pass erases exactly what a migration just asked to be written. MEASURED with it after: 37
|
|
794
|
+
* titles derived in memory, `"sessionTitle"` in the config file: 0.
|
|
795
|
+
*/
|
|
796
|
+
this.configDirty = false;
|
|
797
|
+
/**
|
|
798
|
+
* Run the on-disk migrations ONCE, before any agent resolves a session directory.
|
|
799
|
+
*
|
|
800
|
+
* They used to be spread across the boot path — the flat-layout move ran inside
|
|
801
|
+
* `resolveSessionDir`, i.e. per agent, where it could interleave with another agent's
|
|
802
|
+
* resolution. Ordering is the thing that matters when a machine skips several releases: an
|
|
803
|
+
* operator jumping from an old build runs EVERY step at once, and a step that assumes another
|
|
804
|
+
* has run must actually see that. See src/migrations/index.ts.
|
|
805
|
+
*/
|
|
806
|
+
await runMigrations(MIGRATIONS, {
|
|
807
|
+
configDir: path.dirname(this.configPath),
|
|
808
|
+
raw: () => this.raw,
|
|
809
|
+
touch: () => {
|
|
810
|
+
this.configDirty = true;
|
|
811
|
+
},
|
|
812
|
+
dataDir: this.config.dataDir,
|
|
813
|
+
log: (m) => console.log(`[teapot] ${m}`),
|
|
814
|
+
});
|
|
788
815
|
// ONE config write for the whole boot instead of one per agent.
|
|
789
816
|
//
|
|
790
817
|
// Every agent whose recorded session is gone mints a new one, and minting
|
|
@@ -797,7 +824,6 @@ export class Master {
|
|
|
797
824
|
// deferred write that never flushes would leave config.json describing the PREVIOUS
|
|
798
825
|
// boot, which is its own silent data loss.
|
|
799
826
|
this.deferConfigWrites = true;
|
|
800
|
-
this.configDirty = false;
|
|
801
827
|
// start every endpoint's model-catalogue lookup NOW, concurrently.
|
|
802
828
|
//
|
|
803
829
|
// `addAgent` awaits this per agent, and the endpoints are shared by many
|
|
@@ -1044,6 +1070,15 @@ export class Master {
|
|
|
1044
1070
|
const agent = new Agent({
|
|
1045
1071
|
id: ac.id,
|
|
1046
1072
|
workspace: resolveWorkspace(ac.workspace, path.dirname(this.configPath)),
|
|
1073
|
+
/**
|
|
1074
|
+
* Derive the session label when the first user prompt lands, and STORE it.
|
|
1075
|
+
*
|
|
1076
|
+
* The alternative — reading every session log while listing — was the dominant cost of
|
|
1077
|
+
* `/api/sessions`. Doing it here pays once per session instead of once per list.
|
|
1078
|
+
*/
|
|
1079
|
+
onFirstUserPrompt: (sessionId) => {
|
|
1080
|
+
void this.ensureSessionTitle(ac.id, sessionId);
|
|
1081
|
+
},
|
|
1047
1082
|
llm,
|
|
1048
1083
|
sessionDir,
|
|
1049
1084
|
progressIntervalMs: this.config.progressIntervalMs,
|
|
@@ -1205,6 +1240,15 @@ export class Master {
|
|
|
1205
1240
|
if (!entry)
|
|
1206
1241
|
return; // not a configured agent — nothing to persist yet
|
|
1207
1242
|
entry.session = sessionId;
|
|
1243
|
+
/**
|
|
1244
|
+
* A title names ONE session, so moving clears it.
|
|
1245
|
+
*
|
|
1246
|
+
* Leaving it would label the new session with the previous one's first prompt — the sidebar
|
|
1247
|
+
* would show a conversation name for a conversation it does not describe. It is re-derived on
|
|
1248
|
+
* the next prompt (see `ensureSessionTitle`).
|
|
1249
|
+
*/
|
|
1250
|
+
delete entry.sessionTitle;
|
|
1251
|
+
delete ac.sessionTitle;
|
|
1208
1252
|
// never write session pointers into a config that is describing another
|
|
1209
1253
|
// data dir — every one of them would be wrong, and the correct mapping is
|
|
1210
1254
|
// destroyed. The in-memory session above still works for this process.
|
|
@@ -1687,6 +1731,39 @@ When this task is done (or truly blocked), call finish() with a summary for @${p
|
|
|
1687
1731
|
* affordable. A torn final line is skipped rather than parsed: a chunk boundary
|
|
1688
1732
|
* can cut a JSON object in half, and that must not abort the scan.
|
|
1689
1733
|
*/
|
|
1734
|
+
/**
|
|
1735
|
+
* The stored title for a session, if the agent's config entry names THAT session.
|
|
1736
|
+
*
|
|
1737
|
+
* A single config lookup — no filesystem access — which is the point: this runs once per session
|
|
1738
|
+
* per list, and the log scan it replaced dominated the cost of listing.
|
|
1739
|
+
*/
|
|
1740
|
+
titleFor(agentId, sessionId) {
|
|
1741
|
+
const a = this.config.agents.find((x) => x.id === agentId);
|
|
1742
|
+
if (!a || a.session !== sessionId)
|
|
1743
|
+
return undefined; // the label belongs to the CURRENT session
|
|
1744
|
+
const t = a.sessionTitle?.trim();
|
|
1745
|
+
return t ? t : undefined;
|
|
1746
|
+
}
|
|
1747
|
+
/**
|
|
1748
|
+
* Derive and STORE a title for the agent's current session, once.
|
|
1749
|
+
*
|
|
1750
|
+
* Called when a session starts producing prompts, so the cost is paid per session rather than per
|
|
1751
|
+
* list. Idempotent: an existing title is never overwritten, which is also what will let the
|
|
1752
|
+
* operator rename a session by hand — a derived value must not clobber a chosen one.
|
|
1753
|
+
*/
|
|
1754
|
+
async ensureSessionTitle(agentId, sessionId) {
|
|
1755
|
+
const a = this.config.agents.find((x) => x.id === agentId);
|
|
1756
|
+
if (!a || a.session !== sessionId)
|
|
1757
|
+
return;
|
|
1758
|
+
if (a.sessionTitle?.trim())
|
|
1759
|
+
return;
|
|
1760
|
+
const dir = path.join(this.sessionsRoot(), sessionId);
|
|
1761
|
+
const title = this.sessionTitleOf(dir);
|
|
1762
|
+
if (!title)
|
|
1763
|
+
return;
|
|
1764
|
+
a.sessionTitle = title;
|
|
1765
|
+
this.saveConfig();
|
|
1766
|
+
}
|
|
1690
1767
|
sessionTitleOf(dirPath) {
|
|
1691
1768
|
let fd;
|
|
1692
1769
|
try {
|
|
@@ -1821,19 +1898,8 @@ When this task is done (or truly blocked), call finish() with a summary for @${p
|
|
|
1821
1898
|
resolveSessionDir(agentId, fresh) {
|
|
1822
1899
|
const root = this.sessionsRoot();
|
|
1823
1900
|
mkdirSync(root, { recursive: true });
|
|
1824
|
-
//
|
|
1825
|
-
//
|
|
1826
|
-
if (!fresh) {
|
|
1827
|
-
const legacy = path.join(this.config.dataDir, `${agentId}.jsonl`);
|
|
1828
|
-
if (existsSync(legacy)) {
|
|
1829
|
-
const target = path.join(root, agentId);
|
|
1830
|
-
if (!existsSync(target)) {
|
|
1831
|
-
mkdirSync(target, { recursive: true });
|
|
1832
|
-
renameSync(legacy, path.join(target, "chat.jsonl"));
|
|
1833
|
-
console.log(`[teapot] migrated session storage: ${agentId}.jsonl → sessions/${agentId}/chat.jsonl`);
|
|
1834
|
-
}
|
|
1835
|
-
}
|
|
1836
|
-
}
|
|
1901
|
+
// the ≤0.5.0 flat-layout migration runs once at boot, in src/migrations/ — doing it here meant
|
|
1902
|
+
// it ran per agent and could interleave with another agent's resolution
|
|
1837
1903
|
// Prefer the recorded session: the config says exactly which timeline this
|
|
1838
1904
|
// agent was on. Guessing by mtime let a stale-or-deleted dir whose
|
|
1839
1905
|
// chat.jsonl happened to be newer win, handing the agent a timeline the
|
|
@@ -1923,22 +1989,18 @@ When this task is done (or truly blocked), call finish() with a summary for @${p
|
|
|
1923
1989
|
size = st.size;
|
|
1924
1990
|
}
|
|
1925
1991
|
catch { /* no log yet — still listable */ }
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
// `harness` prompts, 758 of the 817, so they were what made it slow).
|
|
1939
|
-
// Those sub-sessions correctly produce no title — the sidebar already
|
|
1940
|
-
// labels them from their id via subAgentDisplayName().
|
|
1941
|
-
const title = this.sessionTitleOf(dirPath);
|
|
1992
|
+
/**
|
|
1993
|
+
* The title is STORED, not derived.
|
|
1994
|
+
*
|
|
1995
|
+
* It used to be read from each session's log here, which meant this method opened up to 830
|
|
1996
|
+
* files and allocated a buffer for each — every time it ran, on every `/api/sessions`. The
|
|
1997
|
+
* value now lives on the agent's config entry (see `AgentConfig.sessionTitle`), so listing
|
|
1998
|
+
* sessions does no I/O beyond the directory and the stat above.
|
|
1999
|
+
*
|
|
2000
|
+
* `session` is the agent's CURRENT session, so a title only names `d` when they agree; a past
|
|
2001
|
+
* session of the same agent has no stored label and falls back to its id in the sidebar.
|
|
2002
|
+
*/
|
|
2003
|
+
const title = owner ? this.titleFor(owner, d) : undefined;
|
|
1942
2004
|
out.push({ id: d, agentId: owner, mtimeMs: m, sizeBytes: size, ...(title ? { title } : {}) });
|
|
1943
2005
|
}
|
|
1944
2006
|
return out.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ordered, one-way migrations for on-disk state.
|
|
3
|
+
*
|
|
4
|
+
* ## Why order and idempotency matter more than the transformations
|
|
5
|
+
*
|
|
6
|
+
* A single upgrade path is easy. The hard case is a machine that skips several releases: an
|
|
7
|
+
* operator on v0.20 upgrading straight to v0.40 runs EVERY migration in between at once. Two
|
|
8
|
+
* migrations that each look correct can then be wrong together — the canonical example is a future
|
|
9
|
+
* `config.json` split combined with today's "store the session title in `agents[]`":
|
|
10
|
+
*
|
|
11
|
+
* split first → titles land in the new config directory, then the old file is read again and
|
|
12
|
+
* the titles are written a second time, or to the wrong file;
|
|
13
|
+
* titles first → the split then has to carry a field it never knew about.
|
|
14
|
+
*
|
|
15
|
+
* So each migration here:
|
|
16
|
+
*
|
|
17
|
+
* - is **idempotent** — running it twice is a no-op, because a crash mid-upgrade must be
|
|
18
|
+
* resumable and a re-run must not double-apply;
|
|
19
|
+
* - declares an **order** — `order` is ascending, and a later step may assume every earlier one
|
|
20
|
+
* has run;
|
|
21
|
+
* - never deletes data it does not fully replace (a rename, not a delete-then-write);
|
|
22
|
+
* - reports what it did, so a boot log shows which steps actually ran.
|
|
23
|
+
*
|
|
24
|
+
* ## What this module is NOT
|
|
25
|
+
*
|
|
26
|
+
* It does not decide WHICH migrations exist — each one lives in its own file and is registered in
|
|
27
|
+
* `index.ts`. Keeping them out of `master.ts` is the point: they were spread across the boot path,
|
|
28
|
+
* where an ordering mistake is invisible.
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* Run every registered migration in `order`, skipping nothing.
|
|
32
|
+
*
|
|
33
|
+
* There is no "already ran" gate on purpose: each step is idempotent, so the cheapest correct
|
|
34
|
+
* policy is to always run them. A recorded version would have to be validated against a config the
|
|
35
|
+
* operator may have hand-edited or restored from backup, and a wrong version claim is how a
|
|
36
|
+
* migration silently does not run.
|
|
37
|
+
*/
|
|
38
|
+
export async function runMigrations(migrations, ctx) {
|
|
39
|
+
const applied = [];
|
|
40
|
+
for (const m of [...migrations].sort((a, b) => a.order - b.order)) {
|
|
41
|
+
try {
|
|
42
|
+
if (await m.run(ctx)) {
|
|
43
|
+
applied.push(m.id);
|
|
44
|
+
ctx.log(`migrated: ${m.summary}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
// One broken step must not block the boot: the session data is still readable, and refusing
|
|
49
|
+
// to start would be worse than starting with one conversion unmade.
|
|
50
|
+
ctx.log(`migration ${m.id} FAILED (${err.message}) — continuing without it`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { applied };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* ## What is deliberately NOT here
|
|
57
|
+
*
|
|
58
|
+
* Two migrations stay in `src/agent/agent.ts` because they are per-SESSION filesystem moves that
|
|
59
|
+
* need the agent's own paths, and they already satisfy the rules above:
|
|
60
|
+
*
|
|
61
|
+
* - `migrateGoalFromWorkspace` — `<workspace>/GOAL.md` → the session's goal store. Deletes the
|
|
62
|
+
* legacy file only after a successful write, so a failure re-runs.
|
|
63
|
+
* - `migrateMemoryToAgentScope` — a session's `memory.md` → the agent-wide one. APPENDS (the
|
|
64
|
+
* agent-wide file may already hold other sessions' notes), then removes the source. A missing
|
|
65
|
+
* write leaves the session copy in place.
|
|
66
|
+
*
|
|
67
|
+
* Moving them here would mean handing this module session paths it has no business knowing. What
|
|
68
|
+
* matters is that they obey the same contract: idempotent, no data destroyed that was not
|
|
69
|
+
* replaced, and ordered after the config-level steps (they run at agent start, which is later).
|
|
70
|
+
*/
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { migrateProviderKeys } from "../master.js";
|
|
2
|
+
/**
|
|
3
|
+
* Fold a legacy single `apiKey` into an `apiKeys` array.
|
|
4
|
+
*
|
|
5
|
+
* `apiKey` is KEPT in step with `apiKeys[0]` rather than deleted: other readers still expect it (the
|
|
6
|
+
* setup wizard, `/api/providers`, hand-edited files), and a migration that removes a field the
|
|
7
|
+
* operator can see is one they will report as data loss.
|
|
8
|
+
*
|
|
9
|
+
* Idempotent because `apiKeys` is AUTHORITATIVE once present: re-running on a migrated entry
|
|
10
|
+
* produces the same list. The single `apiKey` only SEEDS the pool when there is no list — appending
|
|
11
|
+
* it as well would resurrect a credential the operator had already replaced.
|
|
12
|
+
*/
|
|
13
|
+
export const providerKeys = {
|
|
14
|
+
order: 10,
|
|
15
|
+
id: "providers-api-keys",
|
|
16
|
+
summary: "provider apiKey → apiKeys[]",
|
|
17
|
+
run(ctx) {
|
|
18
|
+
const raw = ctx.raw();
|
|
19
|
+
const providers = raw.providers;
|
|
20
|
+
if (!providers || typeof providers !== "object")
|
|
21
|
+
return false;
|
|
22
|
+
let changed = false;
|
|
23
|
+
for (const entry of Object.values(providers)) {
|
|
24
|
+
if (!entry || typeof entry !== "object")
|
|
25
|
+
continue;
|
|
26
|
+
const p = entry;
|
|
27
|
+
const before = JSON.stringify(p);
|
|
28
|
+
// ONE implementation of the folding rule, shared with the runtime normaliser — a second copy
|
|
29
|
+
// here is how the two drift and disagree about which key is authoritative.
|
|
30
|
+
const next = migrateProviderKeys(p);
|
|
31
|
+
for (const k of Object.keys(p))
|
|
32
|
+
delete p[k];
|
|
33
|
+
Object.assign(p, next);
|
|
34
|
+
if (JSON.stringify(p) !== before)
|
|
35
|
+
changed = true;
|
|
36
|
+
}
|
|
37
|
+
if (changed)
|
|
38
|
+
ctx.touch();
|
|
39
|
+
return changed;
|
|
40
|
+
},
|
|
41
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { providerKeys } from "./providers.js";
|
|
2
|
+
import { flatSessionLayout } from "./sessions.js";
|
|
3
|
+
import { sessionTitles } from "./session-titles.js";
|
|
4
|
+
/**
|
|
5
|
+
* Every migration, in application order.
|
|
6
|
+
*
|
|
7
|
+
* Add new ones with a HIGHER `order` than everything here, and never renumber an existing one — a
|
|
8
|
+
* machine that already ran it would skip it under the new number.
|
|
9
|
+
*
|
|
10
|
+
* Ordering rule: a migration may assume every lower `order` has run. That is what makes a
|
|
11
|
+
* multi-release jump safe. If two migrations must not be reordered later, say so in both files.
|
|
12
|
+
*/
|
|
13
|
+
export const MIGRATIONS = [providerKeys, flatSessionLayout, sessionTitles];
|
|
14
|
+
export { runMigrations } from "./index.js";
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { closeSync, openSync, readSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Derive each agent's session title ONCE and store it in `config.agents[].sessionTitle`.
|
|
5
|
+
*
|
|
6
|
+
* Titles used to be recomputed on every `allSessions()` — opening up to 830 session logs and
|
|
7
|
+
* allocating a 16KB buffer for each, MEASURED at 83.8ms, 8.1MB read and ~38MB of short-lived
|
|
8
|
+
* garbage per scan, on every `/api/sessions`. Storing it makes listing a config lookup.
|
|
9
|
+
*
|
|
10
|
+
* Runs ONCE PER SESSION rather than once per process: the check is `sessionTitle` on the agent's
|
|
11
|
+
* CURRENT session, so a machine that has already been through this does no file I/O at all. That
|
|
12
|
+
* is what makes it idempotent — and why it must come AFTER any step that could change `agents[]`.
|
|
13
|
+
*
|
|
14
|
+
* A session whose first prompt is a HARNESS prompt yields no title: sub-agent sessions receive only
|
|
15
|
+
* those and are labelled from their id, so inventing one would name a child after its parent's
|
|
16
|
+
* instruction.
|
|
17
|
+
*/
|
|
18
|
+
export const sessionTitles = {
|
|
19
|
+
order: 30,
|
|
20
|
+
id: "agents-session-title",
|
|
21
|
+
summary: "derive agents[].sessionTitle from each current session's first user prompt",
|
|
22
|
+
run(ctx) {
|
|
23
|
+
const raw = ctx.raw();
|
|
24
|
+
const agents = Array.isArray(raw.agents) ? raw.agents : [];
|
|
25
|
+
let changed = false;
|
|
26
|
+
for (const a of agents) {
|
|
27
|
+
const session = typeof a.session === "string" ? a.session : "";
|
|
28
|
+
if (!session)
|
|
29
|
+
continue;
|
|
30
|
+
// already stored (possibly hand-edited — a chosen name must never be overwritten)
|
|
31
|
+
if (typeof a.sessionTitle === "string" && a.sessionTitle.trim())
|
|
32
|
+
continue;
|
|
33
|
+
if (session.includes("/") || session.includes(".."))
|
|
34
|
+
continue; // never a path segment
|
|
35
|
+
const title = firstUserPromptText(path.join(ctx.dataDir, "sessions", session, "chat.jsonl"));
|
|
36
|
+
if (!title)
|
|
37
|
+
continue;
|
|
38
|
+
a.sessionTitle = title;
|
|
39
|
+
changed = true;
|
|
40
|
+
}
|
|
41
|
+
if (changed)
|
|
42
|
+
ctx.touch();
|
|
43
|
+
return changed;
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The first USER prompt in a log, trimmed to a title. `null` when there is none.
|
|
48
|
+
*
|
|
49
|
+
* Bounded (256KB) and stops at the first prompt, so one enormous log cannot dominate boot. A torn
|
|
50
|
+
* final line at a chunk boundary is skipped rather than aborting the read.
|
|
51
|
+
*/
|
|
52
|
+
function firstUserPromptText(file) {
|
|
53
|
+
let fd;
|
|
54
|
+
try {
|
|
55
|
+
fd = openSync(file, "r");
|
|
56
|
+
const CHUNK = 16 * 1024;
|
|
57
|
+
const CAP = 256 * 1024;
|
|
58
|
+
const buf = Buffer.alloc(CHUNK);
|
|
59
|
+
let offset = 0;
|
|
60
|
+
let carry = "";
|
|
61
|
+
while (offset < CAP) {
|
|
62
|
+
const n = readSync(fd, buf, 0, CHUNK, offset);
|
|
63
|
+
if (n <= 0)
|
|
64
|
+
break;
|
|
65
|
+
offset += n;
|
|
66
|
+
const lines = (carry + buf.subarray(0, n).toString("utf8")).split("\n");
|
|
67
|
+
carry = lines.pop() ?? "";
|
|
68
|
+
for (const l of lines) {
|
|
69
|
+
if (!l)
|
|
70
|
+
continue;
|
|
71
|
+
let e;
|
|
72
|
+
try {
|
|
73
|
+
e = JSON.parse(l);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
continue; // torn line at a chunk boundary
|
|
77
|
+
}
|
|
78
|
+
if (e.type !== "prompt")
|
|
79
|
+
continue;
|
|
80
|
+
if (e.data?.source !== "user")
|
|
81
|
+
return null; // a harness prompt is not a label
|
|
82
|
+
return titleOf(typeof e.data.text === "string" ? e.data.text : null);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
if (fd !== undefined) {
|
|
92
|
+
try {
|
|
93
|
+
closeSync(fd);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
/* already closed */
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** the first usable line of a prompt, clipped — the sidebar shows one short label */
|
|
102
|
+
function titleOf(text) {
|
|
103
|
+
const line = (text ?? "")
|
|
104
|
+
.split("\n")
|
|
105
|
+
.map((l) => l.trim())
|
|
106
|
+
.find((l) => l.length > 0);
|
|
107
|
+
if (!line)
|
|
108
|
+
return null;
|
|
109
|
+
return line.length > 80 ? `${line.slice(0, 79)}…` : line;
|
|
110
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, renameSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Move sessions from the ≤0.5.0 flat layout (`<dataDir>/<id>.jsonl`) into
|
|
5
|
+
* `<dataDir>/sessions/<id>/chat.jsonl`.
|
|
6
|
+
*
|
|
7
|
+
* A RENAME, not a copy-then-delete: a crash between the two would otherwise leave the history in
|
|
8
|
+
* neither place. `rename` within one filesystem is atomic, so a re-run either finds the legacy file
|
|
9
|
+
* (and moves it) or finds it already moved.
|
|
10
|
+
*
|
|
11
|
+
* Order 20: it must run before anything reads a session directory.
|
|
12
|
+
*/
|
|
13
|
+
export const flatSessionLayout = {
|
|
14
|
+
order: 20,
|
|
15
|
+
id: "sessions-flat-layout",
|
|
16
|
+
summary: "sessions/<id>.jsonl → sessions/<id>/chat.jsonl",
|
|
17
|
+
run(ctx) {
|
|
18
|
+
/**
|
|
19
|
+
* Scan the DATA DIR, not `config.agents`.
|
|
20
|
+
*
|
|
21
|
+
* A legacy log can exist for an agent that is no longer in the config (archived, removed, or a
|
|
22
|
+
* config that failed to load) — and those are exactly the histories worth rescuing. Iterating
|
|
23
|
+
* the config also made this a no-op whenever the config listed no agents, which is how a test
|
|
24
|
+
* with an empty agent list silently stopped exercising it.
|
|
25
|
+
*/
|
|
26
|
+
let names;
|
|
27
|
+
try {
|
|
28
|
+
names = readdirSync(ctx.dataDir);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const root = path.join(ctx.dataDir, "sessions");
|
|
34
|
+
let changed = false;
|
|
35
|
+
for (const name of names) {
|
|
36
|
+
if (!name.endsWith(".jsonl"))
|
|
37
|
+
continue;
|
|
38
|
+
const id = name.slice(0, -".jsonl".length);
|
|
39
|
+
if (!id || id.includes("/") || id.includes(".."))
|
|
40
|
+
continue; // never a path segment
|
|
41
|
+
const legacy = path.join(ctx.dataDir, name);
|
|
42
|
+
const target = path.join(root, id);
|
|
43
|
+
if (existsSync(target))
|
|
44
|
+
continue; // already migrated; the legacy file is a leftover
|
|
45
|
+
mkdirSync(target, { recursive: true });
|
|
46
|
+
renameSync(legacy, path.join(target, "chat.jsonl"));
|
|
47
|
+
ctx.log(`${name} → sessions/${id}/chat.jsonl`);
|
|
48
|
+
changed = true;
|
|
49
|
+
}
|
|
50
|
+
return changed;
|
|
51
|
+
},
|
|
52
|
+
};
|