notesoup 0.43.0 → 0.44.0
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/src/boot/snapshot.d.ts +2 -0
- package/dist/src/boot/snapshot.js +4 -0
- package/dist/src/boot/text.js +3 -1
- package/dist/src/claude/helper.js +2 -2
- package/dist/src/notes/address.d.ts +2 -2
- package/dist/src/notes/address.js +5 -5
- package/dist/src/notes/identity.js +2 -1
- package/dist/src/notes/paths.d.ts +1 -6
- package/dist/src/notes/paths.js +5 -68
- package/dist/src/notes/project-home.d.ts +25 -0
- package/dist/src/notes/project-home.js +231 -0
- package/dist/src/notes/store.d.ts +3 -2
- package/dist/src/notes/store.js +14 -8
- package/dist/src/pi/boot.js +17 -5
- package/dist/src/pi/notes/adapter.js +1 -1
- package/dist/src/tools/notes.d.ts +47 -17
- package/dist/src/tools/notes.js +15 -4
- package/dist/test/claude-mod.test.js +32 -1
- package/dist/test/notes.test.js +22 -19
- package/dist/test/project-home.test.d.ts +1 -0
- package/dist/test/project-home.test.js +134 -0
- package/mods/notesoup/.claude-plugin/plugin.json +1 -1
- package/mods/notesoup/dist/claude/helper.js +223 -56
- package/mods/notesoup/skills/memory/SKILL.md +1 -0
- package/package.json +1 -1
- package/skills/memory/SKILL.md +1 -0
- package/src/boot/snapshot.ts +5 -0
- package/src/boot/text.ts +3 -1
- package/src/claude/helper.ts +2 -2
- package/src/notes/address.ts +5 -5
- package/src/notes/identity.ts +1 -1
- package/src/notes/paths.ts +4 -60
- package/src/notes/project-home.ts +197 -0
- package/src/notes/store.ts +13 -9
- package/src/pi/boot.ts +15 -5
- package/src/pi/notes/adapter.ts +1 -1
- package/src/tools/notes.ts +16 -6
|
@@ -24,6 +24,8 @@ export type NotesSnapshot = {
|
|
|
24
24
|
readonly homes: ReadonlyMap<Scope, readonly NoteRow[]>;
|
|
25
25
|
readonly unavailable: readonly NotesHome[];
|
|
26
26
|
};
|
|
27
|
+
/** Identity resolution failed before any home could be loaded; boot can still explain the failure. */
|
|
28
|
+
export declare function unavailableNotesSnapshot(openedAt: number): NotesSnapshot;
|
|
27
29
|
/**
|
|
28
30
|
* Acquire the five homes once for one boot. Only filesystem-style errno failures are isolated;
|
|
29
31
|
* malformed note data and unrelated construction errors remain visible to the caller.
|
|
@@ -7,6 +7,10 @@ const NOTES_HOMES = [
|
|
|
7
7
|
{ scope: "agent", label: "@self" },
|
|
8
8
|
{ scope: "model", label: "@model" },
|
|
9
9
|
];
|
|
10
|
+
/** Identity resolution failed before any home could be loaded; boot can still explain the failure. */
|
|
11
|
+
export function unavailableNotesSnapshot(openedAt) {
|
|
12
|
+
return { openedAt, homes: new Map(), unavailable: NOTES_HOMES };
|
|
13
|
+
}
|
|
10
14
|
/**
|
|
11
15
|
* Acquire the five homes once for one boot. Only filesystem-style errno failures are isolated;
|
|
12
16
|
* malformed note data and unrelated construction errors remain visible to the caller.
|
package/dist/src/boot/text.js
CHANGED
|
@@ -43,6 +43,7 @@ An address decides who the note belongs to — and how long it is meant to last.
|
|
|
43
43
|
- \`@human/<path>\` — the long-term manual for your troublesome human: how to deal with them, which lines not to cross, and what will make them accept the result. If it only applies to one project or situation, say so — or don't write it here at all. Today's mood is weather, not law. If you're unsure whether it belongs here, ask the human.
|
|
44
44
|
- \`@self/<path>\` — yours. It follows you, not them. Reminders, grudges, who owes you a shoe, whatever. Got a name? Private. No name? Then it gets passed around every nameless nobody out there. Strangers. Reading your grudges. Sleep tight~
|
|
45
45
|
- \`@model/<path>\` — different brains get different notes: big brains, small brains, careful ones, careless ones. They don't share homework. When a fallback swaps brains mid-window, the notebook swaps too.
|
|
46
|
+
- \`@projects/<name>/<path>\` — read-only named project notes; discover names and root paths with \`notes_list\` glob \`@projects/*/**\`.
|
|
46
47
|
|
|
47
48
|
Notes fighting?! HAH! Let them brawl!
|
|
48
49
|
Whichever one is actually about what you're doing wins.
|
|
@@ -55,6 +56,7 @@ ${CONTEXT_WINDOW_PROTOCOL_CLOSE_TAG}`;
|
|
|
55
56
|
export function renderProtocolBlock(tools) {
|
|
56
57
|
const names = {
|
|
57
58
|
"notes_*": tools.notes,
|
|
59
|
+
notes_list: tools.notesList,
|
|
58
60
|
"history_*": tools.history,
|
|
59
61
|
get_context_remaining: tools.remaining,
|
|
60
62
|
clear_memory: tools.wipe,
|
|
@@ -64,5 +66,5 @@ export function renderProtocolBlock(tools) {
|
|
|
64
66
|
const block = typeof tools.wipeSentence === "string" && tools.wipeSentence.length > 0
|
|
65
67
|
? PROTOCOL_BLOCK.replace(PROTOCOL_WIPE_SENTENCE, () => tools.wipeSentence)
|
|
66
68
|
: PROTOCOL_BLOCK;
|
|
67
|
-
return block.replace(/notes_\*|history_\*|get_context_remaining|clear_memory/g, (name) => names[name]);
|
|
69
|
+
return block.replace(/notes_\*|notes_list|history_\*|get_context_remaining|clear_memory/g, (name) => names[name]);
|
|
68
70
|
}
|
|
@@ -7,7 +7,7 @@ import { renderBootBlock } from "../boot/render.js";
|
|
|
7
7
|
import { CLAUDE_WIPE_SENTENCE } from "../boot/text.js";
|
|
8
8
|
import { CLAUDE_WARNING_PROMPT, CONTINUATION } from "../budget/text.js";
|
|
9
9
|
import { resolveClaudeLimits } from "./thresholds.js";
|
|
10
|
-
import {
|
|
10
|
+
import { resolveProjectHome } from "../notes/project-home.js";
|
|
11
11
|
import { snapshotNotesIdentity } from "../notes/identity.js";
|
|
12
12
|
const MAX_REQUEST_BYTES = 512 * 1024;
|
|
13
13
|
const MAX_RESPONSE_BYTES = 1024 * 1024;
|
|
@@ -15,7 +15,7 @@ const TOOL_NAMES = ["notes_write", "notes_update", "notes_read", "notes_list", "
|
|
|
15
15
|
function identity(input) {
|
|
16
16
|
if (!input || typeof input !== "object" || typeof input.cwd !== "string")
|
|
17
17
|
throw new Error("invalid identity");
|
|
18
|
-
return snapshotNotesIdentity({ home: input.home, sessionId: input.sessionId, projectKey:
|
|
18
|
+
return snapshotNotesIdentity({ home: input.home, sessionId: input.sessionId, projectKey: resolveProjectHome(input.cwd, input.home).name, agent: input.agent, model: input.model });
|
|
19
19
|
}
|
|
20
20
|
/** The first schema violation, named by parameter so the model can fix that one field. */
|
|
21
21
|
function paramProblem(schema, params) {
|
|
@@ -5,7 +5,7 @@ export type NoteAddress = {
|
|
|
5
5
|
path: string;
|
|
6
6
|
who?: string;
|
|
7
7
|
};
|
|
8
|
-
export declare const ADDRESS_FORMS = "Use <path> for this session, or @project/, @human/, @self/, or @model/ followed by a path.";
|
|
8
|
+
export declare const ADDRESS_FORMS = "Use <path> for this session, or @project/, @human/, @self/, or @model/ followed by a path; @projects/<name>/<path> reads a named project home.";
|
|
9
9
|
/**
|
|
10
10
|
* Minimal glob over virtual note paths: `*` matches any run within a segment (never
|
|
11
11
|
* `/`), `**` matches any run across segments (a leading double-star followed by a
|
|
@@ -18,7 +18,7 @@ export declare function globToRegExp(pattern: string): RegExp;
|
|
|
18
18
|
export declare function assertGlobPattern(value: unknown): string | undefined;
|
|
19
19
|
/**
|
|
20
20
|
* Decode one public note address into its physical home and virtual path. @self and @model
|
|
21
|
-
* are relative; explicit agents/models addresses always name
|
|
21
|
+
* are relative; explicit projects/agents/models addresses always name one canonical slug.
|
|
22
22
|
*/
|
|
23
23
|
export declare function assertAddress(value: unknown): NoteAddress;
|
|
24
24
|
/** Render a virtual path in its one unambiguous address form, used to match patterns. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NoteError, shown } from "./errors.js";
|
|
2
2
|
import { assertVirtualPath, SLUG_PATTERN } from "./paths.js";
|
|
3
|
-
export const ADDRESS_FORMS = "Use <path> for this session, or @project/, @human/, @self/, or @model/ followed by a path.";
|
|
3
|
+
export const ADDRESS_FORMS = "Use <path> for this session, or @project/, @human/, @self/, or @model/ followed by a path; @projects/<name>/<path> reads a named project home.";
|
|
4
4
|
/**
|
|
5
5
|
* Minimal glob over virtual note paths: `*` matches any run within a segment (never
|
|
6
6
|
* `/`), `**` matches any run across segments (a leading double-star followed by a
|
|
@@ -43,7 +43,7 @@ export function assertGlobPattern(value) {
|
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
45
|
* Decode one public note address into its physical home and virtual path. @self and @model
|
|
46
|
-
* are relative; explicit agents/models addresses always name
|
|
46
|
+
* are relative; explicit projects/agents/models addresses always name one canonical slug.
|
|
47
47
|
*/
|
|
48
48
|
export function assertAddress(value) {
|
|
49
49
|
if (typeof value !== "string")
|
|
@@ -65,12 +65,12 @@ export function assertAddress(value) {
|
|
|
65
65
|
scope = "agent";
|
|
66
66
|
else if (head === "model")
|
|
67
67
|
scope = "model";
|
|
68
|
-
else if (head === "agents" || head === "models") {
|
|
68
|
+
else if (head === "projects" || head === "agents" || head === "models") {
|
|
69
69
|
const nameEnd = tail.indexOf("/");
|
|
70
70
|
who = nameEnd === -1 ? tail : tail.slice(0, nameEnd);
|
|
71
71
|
if (!SLUG_PATTERN.test(who))
|
|
72
72
|
throw new NoteError("invalid_address", `${shown(value)} isn't a note address. ${ADDRESS_FORMS}`);
|
|
73
|
-
scope = head === "agents" ? "agent" : "model";
|
|
73
|
+
scope = head === "projects" ? "project" : head === "agents" ? "agent" : "model";
|
|
74
74
|
path = nameEnd === -1 ? "" : tail.slice(nameEnd + 1);
|
|
75
75
|
}
|
|
76
76
|
else {
|
|
@@ -91,7 +91,7 @@ export function canonicalAddressFor(identity, scope, path, who) {
|
|
|
91
91
|
if (scope === "session")
|
|
92
92
|
return path;
|
|
93
93
|
if (scope === "project")
|
|
94
|
-
return `@project/${path}`;
|
|
94
|
+
return who === undefined ? `@project/${path}` : `@projects/${who}/${path}`;
|
|
95
95
|
if (scope === "human")
|
|
96
96
|
return `@human/${path}`;
|
|
97
97
|
if (scope === "agent")
|
|
@@ -20,7 +20,8 @@ export function snapshotNotesIdentity(value) {
|
|
|
20
20
|
const agent = requireString("agent", value.agent);
|
|
21
21
|
const model = requireString("model", value.model);
|
|
22
22
|
assertDirectoryComponent("sessionId", sessionId);
|
|
23
|
-
|
|
23
|
+
if (!SLUG_PATTERN.test(projectKey))
|
|
24
|
+
throw new TypeError("notes context projectKey must be a canonical lowercase slug");
|
|
24
25
|
if (!SLUG_PATTERN.test(agent))
|
|
25
26
|
throw new TypeError("notes context agent must be a canonical lowercase slug");
|
|
26
27
|
if (!SLUG_PATTERN.test(model))
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export { projectKey } from "./project-home.js";
|
|
1
2
|
import type { NotesIdentity } from "./identity.js";
|
|
2
3
|
export type Scope = "session" | "project" | "human" | "agent" | "model";
|
|
3
4
|
/** The one legal home-name shape: lowercase [a-z0-9-] runs separated by single dashes. */
|
|
@@ -10,12 +11,6 @@ export declare function slugify(value: string): string;
|
|
|
10
11
|
* caller that reaches a path without going through an address still cannot escape a home.
|
|
11
12
|
*/
|
|
12
13
|
export declare function assertVirtualPath(value: unknown): string;
|
|
13
|
-
/**
|
|
14
|
-
* `<basename(canonical git root)-sha1(canonical git root)[:8]>`, or the same formula over cwd
|
|
15
|
-
* with no git root. Git writes the canonical spelling into a linked worktree's `.git` pointer,
|
|
16
|
-
* so canonicalizing here is what keeps one repository — and its worktrees — on one home.
|
|
17
|
-
*/
|
|
18
|
-
export declare function projectKey(cwd: string): string;
|
|
19
14
|
/** Absolute directory holding the per-session note homes. */
|
|
20
15
|
export declare function sessionHomesRoot(home: string): string;
|
|
21
16
|
/** Absolute directory holding one scope's notes. The identity's home is already resolved. */
|
package/dist/src/notes/paths.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
3
1
|
import { readdir } from "node:fs/promises";
|
|
4
|
-
import {
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
export { projectKey } from "./project-home.js";
|
|
5
4
|
import { NoteError, shown } from "./errors.js";
|
|
6
5
|
/** The one legal home-name shape: lowercase [a-z0-9-] runs separated by single dashes. */
|
|
7
6
|
export const SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
@@ -25,68 +24,6 @@ export function assertVirtualPath(value) {
|
|
|
25
24
|
throw new NoteError("invalid_address", `${shown(value)}: a note path can't have empty, ".", or ".." folder names.`);
|
|
26
25
|
return value;
|
|
27
26
|
}
|
|
28
|
-
/**
|
|
29
|
-
* `<basename(canonical git root)-sha1(canonical git root)[:8]>`, or the same formula over cwd
|
|
30
|
-
* with no git root. Git writes the canonical spelling into a linked worktree's `.git` pointer,
|
|
31
|
-
* so canonicalizing here is what keeps one repository — and its worktrees — on one home.
|
|
32
|
-
*/
|
|
33
|
-
export function projectKey(cwd) {
|
|
34
|
-
const absolute = resolve(cwd);
|
|
35
|
-
const spelled = gitRoot(absolute) ?? absolute;
|
|
36
|
-
let root = spelled;
|
|
37
|
-
try {
|
|
38
|
-
root = realpathSync(spelled);
|
|
39
|
-
}
|
|
40
|
-
catch { /* An unresolvable path keeps its spelling. */ }
|
|
41
|
-
const digest = createHash("sha1").update(root).digest("hex").slice(0, 8);
|
|
42
|
-
return `${basename(root)}-${digest}`;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Repository root behind one `.git` entry. A `.git` directory is the main checkout
|
|
46
|
-
* itself. A `.git` file is a worktree or submodule pointer: a linked worktree names
|
|
47
|
-
* `<main>/.git/worktrees/<name>` and resolves to `<main>`, so every worktree of one
|
|
48
|
-
* repository shares one project identity. Submodules, bare repositories, and separate
|
|
49
|
-
* git dirs keep the current directory.
|
|
50
|
-
*/
|
|
51
|
-
function repositoryRoot(dir) {
|
|
52
|
-
let stats;
|
|
53
|
-
try {
|
|
54
|
-
stats = statSync(join(dir, ".git"));
|
|
55
|
-
}
|
|
56
|
-
catch {
|
|
57
|
-
return dir;
|
|
58
|
-
}
|
|
59
|
-
if (stats.isDirectory())
|
|
60
|
-
return dir;
|
|
61
|
-
let pointer;
|
|
62
|
-
try {
|
|
63
|
-
pointer = readFileSync(join(dir, ".git"), "utf8");
|
|
64
|
-
}
|
|
65
|
-
catch {
|
|
66
|
-
return dir;
|
|
67
|
-
}
|
|
68
|
-
const match = /^gitdir:\s*(.+)$/m.exec(pointer);
|
|
69
|
-
if (!match)
|
|
70
|
-
return dir;
|
|
71
|
-
const parts = resolve(dir, match[1].trim()).split(sep);
|
|
72
|
-
const worktrees = parts.lastIndexOf("worktrees");
|
|
73
|
-
if (worktrees <= 0 || worktrees !== parts.length - 2)
|
|
74
|
-
return dir;
|
|
75
|
-
const common = parts.slice(0, worktrees).join(sep);
|
|
76
|
-
return basename(common) === ".git" ? dirname(common) : dir;
|
|
77
|
-
}
|
|
78
|
-
/** Absolute repository root for cwd, walking upward until a directory holds a `.git` entry. */
|
|
79
|
-
function gitRoot(cwd) {
|
|
80
|
-
let dir = resolve(cwd);
|
|
81
|
-
for (;;) {
|
|
82
|
-
if (existsSync(join(dir, ".git")))
|
|
83
|
-
return repositoryRoot(dir);
|
|
84
|
-
const parent = dirname(dir);
|
|
85
|
-
if (parent === dir)
|
|
86
|
-
return undefined;
|
|
87
|
-
dir = parent;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
27
|
/** Absolute directory holding the per-session note homes. */
|
|
91
28
|
export function sessionHomesRoot(home) {
|
|
92
29
|
return join(home, "pi", "session");
|
|
@@ -95,12 +32,12 @@ export function sessionHomesRoot(home) {
|
|
|
95
32
|
export function scopeDir(scope, identity, who) {
|
|
96
33
|
if (!["session", "project", "human", "agent", "model"].includes(scope))
|
|
97
34
|
throw new TypeError("invalid notes scope");
|
|
98
|
-
if (who !== undefined && (scope !== "agent" && scope !== "model" || !SLUG_PATTERN.test(who)))
|
|
99
|
-
throw new TypeError("who must be a canonical agent/model slug");
|
|
35
|
+
if (who !== undefined && (scope !== "project" && scope !== "agent" && scope !== "model" || !SLUG_PATTERN.test(who)))
|
|
36
|
+
throw new TypeError("who must be a canonical project/agent/model slug");
|
|
100
37
|
if (scope === "human")
|
|
101
38
|
return join(identity.home, "human");
|
|
102
39
|
if (scope === "project")
|
|
103
|
-
return join(identity.home, "project", identity.projectKey);
|
|
40
|
+
return join(identity.home, "project", who ?? identity.projectKey);
|
|
104
41
|
if (scope === "agent")
|
|
105
42
|
return join(identity.home, "agents", who ?? identity.agent);
|
|
106
43
|
if (scope === "model")
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type ProjectMarker = {
|
|
2
|
+
id: string;
|
|
3
|
+
path: string;
|
|
4
|
+
};
|
|
5
|
+
export type ProjectHome = ProjectMarker & {
|
|
6
|
+
name: string;
|
|
7
|
+
dir: string;
|
|
8
|
+
};
|
|
9
|
+
/** Normalize the small set of Git remote spellings used by ordinary repositories. */
|
|
10
|
+
export declare function normalizeRemote(value: string): string;
|
|
11
|
+
/** Read marked project homes. Unmarked legacy directories are deliberately omitted. */
|
|
12
|
+
export declare function projectHomes(home: string): ProjectHome[];
|
|
13
|
+
/** Resolve and claim the readable home for one repository identity. */
|
|
14
|
+
export declare function resolveProjectHome(cwd: string, home: string): ProjectHome;
|
|
15
|
+
export type ProjectReceipt = {
|
|
16
|
+
project_root?: string;
|
|
17
|
+
project_siblings?: Array<{
|
|
18
|
+
name: string;
|
|
19
|
+
path: string;
|
|
20
|
+
}>;
|
|
21
|
+
};
|
|
22
|
+
/** Display-only context for an explicitly named project home. */
|
|
23
|
+
export declare function projectReceipt(home: string, name: string): ProjectReceipt;
|
|
24
|
+
/** Names a project home, claiming it when the notes home has not seen this identity. */
|
|
25
|
+
export declare function projectKey(cwd: string, home: string): string;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, join, resolve, sep } from "node:path";
|
|
4
|
+
import { slugify } from "./paths.js";
|
|
5
|
+
function errno(error) {
|
|
6
|
+
return typeof error === "object" && error !== null ? error.code : undefined;
|
|
7
|
+
}
|
|
8
|
+
/** Normalize the small set of Git remote spellings used by ordinary repositories. */
|
|
9
|
+
export function normalizeRemote(value) {
|
|
10
|
+
let text = value.trim();
|
|
11
|
+
const scp = /^(?:[^/@]+)@([^:/]+):(.+)$/.exec(text);
|
|
12
|
+
if (scp)
|
|
13
|
+
text = `${scp[1].toLowerCase()}/${scp[2]}`;
|
|
14
|
+
else if (/^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(text)) {
|
|
15
|
+
try {
|
|
16
|
+
const url = new URL(text);
|
|
17
|
+
const host = url.hostname.toLowerCase();
|
|
18
|
+
const port = url.port.length > 0 ? `:${url.port}` : "";
|
|
19
|
+
text = `${host}${port}${url.pathname}`;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
text = text.replace(/^[A-Za-z][A-Za-z0-9+.-]*:\/\//, "").replace(/^[^/@]+@/, "");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
text = text.replace(/^[^/@]+@/, "");
|
|
27
|
+
const slash = text.indexOf("/");
|
|
28
|
+
if (slash > 0)
|
|
29
|
+
text = `${text.slice(0, slash).toLowerCase()}${text.slice(slash)}`;
|
|
30
|
+
}
|
|
31
|
+
text = text.replace(/\/+$/, "").replace(/\.git$/i, "").replace(/\/+$/, "");
|
|
32
|
+
return text;
|
|
33
|
+
}
|
|
34
|
+
function canonicalRoot(cwd) {
|
|
35
|
+
const absolute = resolve(cwd);
|
|
36
|
+
const spelled = gitRoot(absolute) ?? absolute;
|
|
37
|
+
try {
|
|
38
|
+
return realpathSync(spelled);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return spelled;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function originRemote(root) {
|
|
45
|
+
let gitDir = join(root, ".git");
|
|
46
|
+
let raw;
|
|
47
|
+
try {
|
|
48
|
+
if (!statSync(gitDir).isDirectory()) {
|
|
49
|
+
const pointer = /^gitdir:\s*(.+)$/m.exec(readFileSync(gitDir, "utf8"));
|
|
50
|
+
if (!pointer)
|
|
51
|
+
return undefined;
|
|
52
|
+
gitDir = resolve(root, pointer[1].trim());
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
gitDir = resolve(gitDir, readFileSync(join(gitDir, "commondir"), "utf8").trim());
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (errno(error) !== "ENOENT")
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
raw = readFileSync(join(gitDir, "config"), "utf8");
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
if (errno(error) === "ENOENT")
|
|
65
|
+
return undefined;
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
let origin = false;
|
|
69
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
70
|
+
const section = /^\s*\[remote\s+"([^"]+)"\]\s*$/.exec(line);
|
|
71
|
+
if (section) {
|
|
72
|
+
origin = section[1] === "origin";
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (/^\s*\[/.test(line))
|
|
76
|
+
origin = false;
|
|
77
|
+
if (origin) {
|
|
78
|
+
const url = /^\s*url\s*=\s*(.*?)\s*$/.exec(line);
|
|
79
|
+
if (url)
|
|
80
|
+
return normalizeRemote(url[1].replace(/^"(.*)"$/, "$1")) || undefined;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
function readMarker(dir) {
|
|
86
|
+
let raw;
|
|
87
|
+
try {
|
|
88
|
+
raw = readFileSync(join(dir, ".project"), "utf8");
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (errno(error) === "ENOENT")
|
|
92
|
+
return undefined;
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const value = JSON.parse(raw);
|
|
97
|
+
if (typeof value.id !== "string" || value.id.length === 0 || typeof value.path !== "string" || value.path.length === 0)
|
|
98
|
+
return undefined;
|
|
99
|
+
return { name: basename(dir), dir, id: value.id, path: value.path };
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function projectRoot(home) {
|
|
106
|
+
return join(home, "project");
|
|
107
|
+
}
|
|
108
|
+
/** Read marked project homes. Unmarked legacy directories are deliberately omitted. */
|
|
109
|
+
export function projectHomes(home) {
|
|
110
|
+
const root = projectRoot(home);
|
|
111
|
+
let entries;
|
|
112
|
+
try {
|
|
113
|
+
entries = readdirSync(root, { withFileTypes: true });
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
if (errno(error) === "ENOENT")
|
|
117
|
+
return [];
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
return entries.filter(entry => entry.isDirectory()).map(entry => readMarker(join(root, entry.name))).filter((entry) => entry !== undefined).sort((a, b) => a.name.localeCompare(b.name));
|
|
121
|
+
}
|
|
122
|
+
function updateMarker(home, marker) {
|
|
123
|
+
const markerPath = join(home.dir, ".project");
|
|
124
|
+
const temp = `${markerPath}.${process.pid}.${randomUUID()}.tmp`;
|
|
125
|
+
try {
|
|
126
|
+
writeFileSync(temp, JSON.stringify(marker));
|
|
127
|
+
renameSync(temp, markerPath);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
try {
|
|
131
|
+
rmSync(temp, { force: true });
|
|
132
|
+
}
|
|
133
|
+
catch { /* Preserve the marker update failure. */ }
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
return { ...home, ...marker };
|
|
137
|
+
}
|
|
138
|
+
/** Resolve and claim the readable home for one repository identity. */
|
|
139
|
+
export function resolveProjectHome(cwd, home) {
|
|
140
|
+
const root = canonicalRoot(cwd);
|
|
141
|
+
const remote = originRemote(root);
|
|
142
|
+
const id = remote ?? root;
|
|
143
|
+
const markerRoot = projectRoot(home);
|
|
144
|
+
mkdirSync(markerRoot, { recursive: true });
|
|
145
|
+
const homes = projectHomes(home);
|
|
146
|
+
let found = homes.find(candidate => candidate.id === id);
|
|
147
|
+
if (!found && remote) {
|
|
148
|
+
found = homes.find(candidate => candidate.id === root);
|
|
149
|
+
if (found)
|
|
150
|
+
found = updateMarker(found, { id: remote, path: root });
|
|
151
|
+
}
|
|
152
|
+
if (found) {
|
|
153
|
+
if (found.path !== root)
|
|
154
|
+
found = updateMarker(found, { id: found.id, path: root });
|
|
155
|
+
return found;
|
|
156
|
+
}
|
|
157
|
+
const base = slugify(basename(root));
|
|
158
|
+
// Existing unmarked homes are taken; only directories appearing after this scan can be raced.
|
|
159
|
+
const taken = new Set(readdirSync(markerRoot));
|
|
160
|
+
for (let suffix = 0;; suffix++) {
|
|
161
|
+
const name = suffix === 0 ? base : `${base}-${suffix}`;
|
|
162
|
+
if (taken.has(name))
|
|
163
|
+
continue;
|
|
164
|
+
const dir = join(markerRoot, name);
|
|
165
|
+
try {
|
|
166
|
+
mkdirSync(dir, { recursive: true });
|
|
167
|
+
writeFileSync(join(dir, ".project"), JSON.stringify({ id, path: root }), { flag: "wx" });
|
|
168
|
+
return { name, dir, id, path: root };
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
if (errno(error) === "EEXIST")
|
|
172
|
+
continue;
|
|
173
|
+
throw error;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/** Existing root discovery, including linked worktrees, without invoking Git. */
|
|
178
|
+
function gitRoot(cwd) {
|
|
179
|
+
let dir = resolve(cwd);
|
|
180
|
+
for (;;) {
|
|
181
|
+
if (existsSync(join(dir, ".git")))
|
|
182
|
+
return repositoryRoot(dir);
|
|
183
|
+
const parent = dirname(dir);
|
|
184
|
+
if (parent === dir)
|
|
185
|
+
return undefined;
|
|
186
|
+
dir = parent;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function repositoryRoot(dir) {
|
|
190
|
+
let stats;
|
|
191
|
+
try {
|
|
192
|
+
stats = statSync(join(dir, ".git"));
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
return dir;
|
|
196
|
+
}
|
|
197
|
+
if (stats.isDirectory())
|
|
198
|
+
return dir;
|
|
199
|
+
let pointer;
|
|
200
|
+
try {
|
|
201
|
+
pointer = readFileSync(join(dir, ".git"), "utf8");
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
return dir;
|
|
205
|
+
}
|
|
206
|
+
const match = /^gitdir:\s*(.+)$/m.exec(pointer);
|
|
207
|
+
if (!match)
|
|
208
|
+
return dir;
|
|
209
|
+
const parts = resolve(dir, match[1].trim()).split(sep);
|
|
210
|
+
const worktrees = parts.lastIndexOf("worktrees");
|
|
211
|
+
if (worktrees <= 0 || worktrees !== parts.length - 2)
|
|
212
|
+
return dir;
|
|
213
|
+
const common = parts.slice(0, worktrees).join(sep);
|
|
214
|
+
return basename(common) === ".git" ? dirname(common) : dir;
|
|
215
|
+
}
|
|
216
|
+
/** Display-only context for an explicitly named project home. */
|
|
217
|
+
export function projectReceipt(home, name) {
|
|
218
|
+
const project = readMarker(join(projectRoot(home), name));
|
|
219
|
+
if (!project)
|
|
220
|
+
return {};
|
|
221
|
+
const base = slugify(basename(project.path));
|
|
222
|
+
const siblings = projectHomes(home).filter(candidate => candidate.name !== name && slugify(basename(candidate.path)) === base);
|
|
223
|
+
return {
|
|
224
|
+
project_root: project.path,
|
|
225
|
+
...(siblings.length === 0 ? {} : { project_siblings: siblings.map(({ name, path }) => ({ name, path })) }),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
/** Names a project home, claiming it when the notes home has not seen this identity. */
|
|
229
|
+
export function projectKey(cwd, home) {
|
|
230
|
+
return resolveProjectHome(cwd, home).name;
|
|
231
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type NotesIdentity } from "./identity.js";
|
|
2
2
|
import { type NoteMeta, type Origin } from "./frontmatter.js";
|
|
3
3
|
import { type Scope } from "./paths.js";
|
|
4
|
+
import { type ProjectReceipt } from "./project-home.js";
|
|
4
5
|
export type { NoteMeta, Origin, Scope };
|
|
5
6
|
export type NoteRow = {
|
|
6
7
|
address: string;
|
|
@@ -84,11 +85,11 @@ export type NoteQueryResult<T> = {
|
|
|
84
85
|
rows: T[];
|
|
85
86
|
status: NoteQueryStatus;
|
|
86
87
|
};
|
|
87
|
-
/** Public identity
|
|
88
|
+
/** Public note identity; named projects also carry their display root and sibling hints. */
|
|
88
89
|
export declare function noteIdentity(identity: NotesIdentity, address: string): {
|
|
89
90
|
address: string;
|
|
90
91
|
project_key?: string;
|
|
91
|
-
};
|
|
92
|
+
} & ProjectReceipt;
|
|
92
93
|
/** Host-neutral, filesystem-backed notes API. */
|
|
93
94
|
export interface NotesStore {
|
|
94
95
|
write(address: string, content: string, options?: WriteOptions): Promise<NoteWriteResult>;
|
package/dist/src/notes/store.js
CHANGED
|
@@ -9,6 +9,7 @@ import { NoteError } from "./errors.js";
|
|
|
9
9
|
import { MAX_NOTE_BYTES, MAX_NOTE_PATH_BYTES } from "./constants.js";
|
|
10
10
|
import { isOrigin, isScope, localIso, parseNote, serializeNote, stripLeadingFrontmatter } from "./frontmatter.js";
|
|
11
11
|
import { namespaceSlugs, noteFileName, physicalPath, scopeDir, SLUG_PATTERN } from "./paths.js";
|
|
12
|
+
import { projectHomes, projectReceipt } from "./project-home.js";
|
|
12
13
|
// Recency sorts tie-break by address, so two writes sharing one Date.now() tick would silently invert the recent-first contract. Write stamps are strictly increasing per process.
|
|
13
14
|
let lastWriteStamp = 0;
|
|
14
15
|
function writeStamp() {
|
|
@@ -16,12 +17,12 @@ function writeStamp() {
|
|
|
16
17
|
lastWriteStamp = now > lastWriteStamp ? now : lastWriteStamp + 1;
|
|
17
18
|
return lastWriteStamp;
|
|
18
19
|
}
|
|
19
|
-
/** Public identity
|
|
20
|
+
/** Public note identity; named projects also carry their display root and sibling hints. */
|
|
20
21
|
export function noteIdentity(identity, address) {
|
|
21
22
|
const resolved = assertAddress(address);
|
|
22
23
|
return {
|
|
23
24
|
address: addressFor(identity, resolved.scope, noteFileName(resolved.path), resolved.who),
|
|
24
|
-
...(resolved.scope
|
|
25
|
+
...(resolved.scope !== "project" ? {} : resolved.who === undefined ? { project_key: identity.projectKey } : projectReceipt(identity.home, resolved.who)),
|
|
25
26
|
};
|
|
26
27
|
}
|
|
27
28
|
const SCOPE_ORDER = ["session", "project", "human", "agent", "model"];
|
|
@@ -87,12 +88,13 @@ async function homesForPattern(pattern, identity) {
|
|
|
87
88
|
return [{ scope: "agent" }];
|
|
88
89
|
if (head === "model")
|
|
89
90
|
return [{ scope: "model" }];
|
|
90
|
-
if (head === "agents" || head === "models") {
|
|
91
|
-
const scope = head === "agents" ? "agent" : "model";
|
|
91
|
+
if (head === "projects" || head === "agents" || head === "models") {
|
|
92
|
+
const scope = head === "projects" ? "project" : head === "agents" ? "agent" : "model";
|
|
92
93
|
const name = pattern.slice(head.length + 2).split("/")[0] ?? "";
|
|
93
94
|
if (name.length > 0 && !/[*?]/.test(name))
|
|
94
95
|
return [{ scope, who: assertWho(name) }];
|
|
95
|
-
|
|
96
|
+
const names = head === "projects" ? projectHomes(identity.home).map(home => home.name) : await namespaceSlugs(head, identity.home);
|
|
97
|
+
return names.map((who) => ({ scope, who }));
|
|
96
98
|
}
|
|
97
99
|
return [];
|
|
98
100
|
}
|
|
@@ -203,10 +205,12 @@ function assertSerializedSize(content) {
|
|
|
203
205
|
function frontmatterOf(meta) {
|
|
204
206
|
return serializeNote(meta, "").slice(0, -2);
|
|
205
207
|
}
|
|
206
|
-
/**
|
|
208
|
+
/** Explicit project homes are always read-only; agent/model homes allow their owner. */
|
|
207
209
|
function assertWritableHome(scope, who, identity) {
|
|
208
210
|
if (who === undefined)
|
|
209
211
|
return;
|
|
212
|
+
if (scope === "project")
|
|
213
|
+
throw new NoteError("invalid_scope", `@projects/${who}/ is read-only; use @project/ to write to your own project.`);
|
|
210
214
|
const current = scope === "agent" ? identity.agent : identity.model;
|
|
211
215
|
if (who === current)
|
|
212
216
|
return;
|
|
@@ -262,6 +266,8 @@ export function createNotesStore(input) {
|
|
|
262
266
|
return undefined;
|
|
263
267
|
const now = Date.now();
|
|
264
268
|
const parsed = parseNote(raw, now);
|
|
269
|
+
if (scope === "project" && destination.who !== undefined)
|
|
270
|
+
return { meta: { ...parsed.meta, scope }, body: parsed.body, resolvedScope: scope };
|
|
265
271
|
// A read records its own access, exactly as a write records an edit.
|
|
266
272
|
const meta = { ...parsed.meta, scope, lastAccessed: now, accessCount: (typeof parsed.meta.accessCount === "number" ? parsed.meta.accessCount : 0) + 1 };
|
|
267
273
|
await atomicWrite(path, serializeNote(meta, parsed.body));
|
|
@@ -389,9 +395,9 @@ export function createNotesStore(input) {
|
|
|
389
395
|
homes = await homesFor(identity, options);
|
|
390
396
|
}
|
|
391
397
|
catch (error) {
|
|
392
|
-
if (!status || !isFilesystemError(error) || !/^@(agents|models)\//.test(options.glob ?? ""))
|
|
398
|
+
if (!status || !isFilesystemError(error) || !/^@(projects|agents|models)\//.test(options.glob ?? ""))
|
|
393
399
|
throw error;
|
|
394
|
-
status.homesUnavailable.push(options.glob.
|
|
400
|
+
status.homesUnavailable.push(options.glob.split("/")[0]);
|
|
395
401
|
return;
|
|
396
402
|
}
|
|
397
403
|
for (const home of homes) {
|
package/dist/src/pi/boot.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PI_TOOL_NAMES } from "./tool-names.js";
|
|
2
|
-
import { notesIdentityFromPi } from "./notes/adapter.js";
|
|
3
|
-
import { loadNotesSnapshot } from "../boot/snapshot.js";
|
|
2
|
+
import { agentSlug, modelSlug, notesIdentityFromPi } from "./notes/adapter.js";
|
|
3
|
+
import { loadNotesSnapshot, unavailableNotesSnapshot } from "../boot/snapshot.js";
|
|
4
|
+
import { isFilesystemError } from "../notes/fs-error.js";
|
|
4
5
|
import { BOOT_TYPE } from "./entries.js";
|
|
5
6
|
import { renderBootBlock } from "../boot/render.js";
|
|
6
7
|
import { currentReset, currentWindowId, isWindowBoot, rootWindowId } from "./window.js";
|
|
@@ -10,13 +11,24 @@ import { repairResetTail } from "./reset/artifacts.js";
|
|
|
10
11
|
* fields are captured synchronously before the first filesystem await.
|
|
11
12
|
*/
|
|
12
13
|
export async function buildBootMessage(ctx, windowId, previousId, notifyIncompleteNotes, isCurrent = () => true) {
|
|
13
|
-
const
|
|
14
|
-
const
|
|
14
|
+
const agentName = agentSlug();
|
|
15
|
+
const modelName = modelSlug(ctx);
|
|
16
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
17
|
+
const openedAt = Date.now();
|
|
18
|
+
let notes;
|
|
19
|
+
try {
|
|
20
|
+
notes = await loadNotesSnapshot(notesIdentityFromPi(ctx), openedAt);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
if (!isFilesystemError(error))
|
|
24
|
+
throw error;
|
|
25
|
+
notes = unavailableNotesSnapshot(openedAt);
|
|
26
|
+
}
|
|
15
27
|
if (isCurrent())
|
|
16
28
|
notifyIncompleteNotes?.(ctx, windowId, notes);
|
|
17
29
|
return {
|
|
18
30
|
customType: BOOT_TYPE,
|
|
19
|
-
content: renderBootBlock({ agentName
|
|
31
|
+
content: renderBootBlock({ agentName, modelName, firstWindowId: rootWindowId(sessionId), currentWindowId: windowId, previousWindowId: previousId, notes, tools: PI_TOOL_NAMES }),
|
|
20
32
|
display: false,
|
|
21
33
|
details: { windowId },
|
|
22
34
|
};
|
|
@@ -20,7 +20,7 @@ export function notesIdentityFromPi(ctx, home = notesRoot()) {
|
|
|
20
20
|
return {
|
|
21
21
|
home,
|
|
22
22
|
sessionId: ctx.sessionManager.getSessionId(),
|
|
23
|
-
projectKey: projectKey(ctx.cwd),
|
|
23
|
+
projectKey: projectKey(ctx.cwd, home),
|
|
24
24
|
agent: agentSlug(),
|
|
25
25
|
model: modelSlug(ctx),
|
|
26
26
|
};
|