feat(gitea): clone —— 從 Gitea 匯入一個本機還沒有的人格
底層本來就走得通(`pullArea` 的 restore 會把本機缺少的檔案全部補進來), 擋住的是上層的雞生蛋:`sync` 先走 `requireOwner`,而 `requireOwner` 第一件事 就是「本機沒有這個人格就 die」。本機沒有它 → load 不了它 → sync pull 被擋 → 永遠拉不回來。所以照 `import` 的模式另開一個只驗 session、不驗 host 的入口。 * `clone --code <編號>`:兩區都拉回來(Wiki 區給身分與長期記憶,檔案區給活狀態), 然後補上 `pullArea` 不管的那幾件事——驗 IDENTITY.md(`validateBundle` 明文的 人格最低要件,Gitea 這條路上原本不存在)、補寫 config.code/來歷、 `rebuildIndex()`、`renderRelations()`。拉回來不成人格就中止並清掉半成品。 * `clone`(不帶 --code):列出遠端有哪些人格、哪些本機還沒有。 整個 codebase 原本沒有任何「列出 owner 底下的存取庫」的呼叫,新增 `listRemotePersonas()`:分頁打 `GET /user/repos`(他人/組織走 `/users/<owner>/repos`), 用編號格式過濾——存取庫名稱就是人格編號,所以那份清單就是遠端的人格清單。 * 本機已有同名人格時**預設不覆蓋**;`--force` 才蓋(沿用 `import` 的兩道保護: 不得覆寫別人、不得覆寫正被其他程序載入的人格),`--persona` 可並存兩份。 * 加進 `OWNER_EXEMPT_SUBCOMMANDS`,否則已載入其他人格時會被 hook deny。 * 編號衝突:`nextCode()` 只掃本機,換機器會重複發號。新增 `nextCodeAcrossMachines()`,發號前先問遠端已經用掉哪些編號;Gitea 連不上 就退回本機答案並在輸出明講「只對過本機」。`create` 與 `code assign/next` 都改用它。 * 順手修正 `ensureRepo` 的建庫路由:`me` 取自 `resolveOwner()`,而它在有 `PERSONA_GITEA_OWNER` 時只會把那個值原封不動還回來,於是組織永遠走成 `/user/repos`(建到 token 本人底下)。改用不受該環境變數影響的 `giteaLogin()`。 測試:selftest 新增第 ㉒ 區,用 file:// 的裸倉庫當「假的 Gitea」跑完整往返 (推兩區 → 刪掉本機人格 → clone 回來 → 驗身分/長期記憶/活狀態/索引/關係圖), 並涵蓋前兩個修正(子資料夾與非 ASCII 檔名真的進了存取庫、push 覆蓋遠端的回報 與 Stop hook 只吵一次)。345 → 373 項全過。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+131
-9
@@ -233,6 +233,7 @@ commands.create = async ({ flags }) => {
|
||||
const session = requireSession(flags);
|
||||
// 人格編號 = 英文名全大寫 + 兩位索引(同名才遞增)。編號就是 Gitea 存取庫的名稱。
|
||||
let code = str(flags.code);
|
||||
let codeNote = "";
|
||||
if (code && !gt.validCode(code)) die(`編號 \`${code}\` 不合法,格式是「英文名全大寫-兩位數」,例如 \`ASUNA-01\`。`);
|
||||
if (!code) {
|
||||
const base = gt.normalizeRomaji(str(flags.romaji) || str(flags.persona));
|
||||
@@ -242,8 +243,12 @@ commands.create = async ({ flags }) => {
|
||||
"中文名請先轉成羅馬拼音並跟使用者確認拼法,再帶進來。",
|
||||
);
|
||||
}
|
||||
code = gt.nextCode(base);
|
||||
// 發號前先問遠端:`nextCode` 只掃本機,換一台機器就會把同一個號再發一次
|
||||
const next = flags["no-gitea"] ? { code: gt.nextCode(base), checked_remote: false, reason: "--no-gitea" }
|
||||
: await gt.nextCodeAcrossMachines(base, { owner: str(flags.owner) || null });
|
||||
code = next.code;
|
||||
if (!code) die(`\`${base}\` 的編號已經用到 99,請換一個英文名。`);
|
||||
if (!next.checked_remote) codeNote = ` ⚠ 編號只對過本機,沒對過 Gitea(${next.reason})——別台機器可能已經用掉這個號。`;
|
||||
}
|
||||
// 沒指定 --persona 就用編號當目錄名(一個識別走到底);指定了就沿用(相容既有人格)
|
||||
const slug = str(flags.persona) || code;
|
||||
@@ -288,6 +293,7 @@ commands.create = async ({ flags }) => {
|
||||
pl.acquireLock(slug, session, { cwd: str(flags.cwd) || null });
|
||||
pl.bindHost(session, slug, { cwd: str(flags.cwd) || null });
|
||||
ok(`人格 \`${slug}\`(編號 \`${code}\`)建立於 ${root},已取得載入鎖並綁定本 session。`);
|
||||
if (codeNote) say(codeNote);
|
||||
say(` 下一步:補完 ${root}/IDENTITY.md 與 SOUL.md,再用 /jsc-persona:persona-chat 開始對話。`);
|
||||
// Gitea 上的存取庫名稱就是編號。身分還沒補完,這裡只開庫;內容之後由各時機自動 push。
|
||||
if (!flags["no-gitea"] && !gt.giteaProblem()) {
|
||||
@@ -1520,6 +1526,104 @@ commands.import = ({ flags }) => {
|
||||
emit({ ...result, source: bundle.persona, checksum_ok: checksumOk }, flags.json, lines);
|
||||
};
|
||||
|
||||
/**
|
||||
* 從 Gitea 匯入一個**本機還沒有**的人格(換一台機器時的第一步)。
|
||||
*
|
||||
* 這是唯一不需要「先載入該人格」的同步入口,而且非如此不可:`sync` 的每個動作都要
|
||||
* `requireOwner`,而 `requireOwner` 的第一件事是「本機沒有這個人格就 die」——本機沒有它,
|
||||
* 就 load 不了它,就永遠拉不回來。所以走 `import` 那條路:只驗 session,不驗 host。
|
||||
*/
|
||||
commands.clone = async ({ flags, positional }) => {
|
||||
const session = requireSession(flags);
|
||||
const problem = gt.giteaProblem();
|
||||
if (problem) die(`Gitea 尚未設定:${problem}(設 GITEA_HOST 與 GITEA_TOKEN,或用 PERSONA_GITEA_* 覆寫)`);
|
||||
const owner = str(flags.owner) || null;
|
||||
const code = str(flags.code) || positional[0] || "";
|
||||
|
||||
// 不指定編號 → 列出遠端有哪些人格讓使用者挑
|
||||
if (!code) {
|
||||
let listed;
|
||||
try {
|
||||
listed = await gt.listRemotePersonas({ owner });
|
||||
} catch (err) {
|
||||
die(`列不出 Gitea 上的人格:${err.message}`);
|
||||
}
|
||||
const rows = listed.personas;
|
||||
const missing = rows.filter((r) => !r.local);
|
||||
emit({ owner: listed.owner, personas: rows }, flags.json, [
|
||||
`Gitea \`${listed.owner}\` 底下的人格:${rows.length} 個,其中 ${missing.length} 個本機還沒有。`,
|
||||
...(rows.length
|
||||
? rows.map((r) =>
|
||||
`${r.local ? " ✔" : " ⬇"} \`${r.code}\`` +
|
||||
(r.local ? ` 本機已有${r.local === r.code ? "" : `(目錄 ${r.local})`}` : " 本機還沒有") +
|
||||
`|${r.private ? "私有" : "公開"}|最後更新 ${String(r.updated_at || "").slice(0, 10) || "—"}` +
|
||||
(r.description ? `|${r.description}` : ""))
|
||||
: ["(一個都沒有——存取庫名稱要是人格編號才算得上人格,例如 `ASUNA-01`)"]),
|
||||
"",
|
||||
"匯入:`persona.mjs clone --code <編號> --session <id>`(本機還沒有的那些)",
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!gt.validCode(code)) die(`編號 \`${code}\` 不合法(格式:ASUNA-01)。`);
|
||||
const target = str(flags.persona) || code;
|
||||
const data = pl.loadSession(session);
|
||||
const exists = pl.personaExists(target);
|
||||
if (exists && !flags.force) {
|
||||
die(
|
||||
`人格 \`${target}\` 本機已經有了。要以遠端覆蓋本機請加 --force(本機還沒推上去的改動會不見),` +
|
||||
"或用 `--persona <另一個目錄名>` 拉成第二份;只是想更新的話請 `load` 之後跑 `sync pull`。",
|
||||
);
|
||||
}
|
||||
if (exists) {
|
||||
// 覆寫既有人格:跟 `import` 一樣的兩道保護
|
||||
if (data.host && data.host !== target) {
|
||||
die(`本 session 載入的是 \`${data.host}\`,不得覆寫另一個既有人格 \`${target}\`;請先 release 再匯入。`);
|
||||
}
|
||||
const lock = pl.readJson(pl.lockPath(target)) ?? {};
|
||||
if (Object.keys(lock).length && lock.session_id !== session && !pl.lockIsDead(lock)) {
|
||||
die(
|
||||
`人格 \`${target}\` 正被另一個程序載入(session ${String(lock.session_id).slice(0, 8)}…,cwd ${lock.cwd}),` +
|
||||
"不能覆寫它的資料。",
|
||||
);
|
||||
}
|
||||
}
|
||||
let result;
|
||||
try {
|
||||
result = await gt.importFromRemote(code, { owner, slug: target, force: Boolean(flags.force) });
|
||||
} catch (err) {
|
||||
die(err.message);
|
||||
}
|
||||
const lines = [
|
||||
`✔ 人格 \`${result.persona}\`(\`${result.code}\`)已從 Gitea 匯入${exists ? ",覆寫本機既有資料" : ""}。`,
|
||||
` 來源:${result.owner}/${result.code}|${result.written.length} 個檔案|${pl.identityBrief(result.persona) || "(無身分欄位)"}`,
|
||||
...gt.AREA_KEYS.map((key) => {
|
||||
const res = result.results[key] || {};
|
||||
return ` ${gt.AREAS[key].label}:` +
|
||||
(res.ok ? `${res.written?.length ?? 0} 個檔案${res.empty ? "(遠端是空的)" : ""}`
|
||||
: `⚠ ${String(res.reason || "失敗").slice(0, 120)}`);
|
||||
}),
|
||||
` 長期記憶 ${pl.longTermEntries(result.persona).length} 則|短期 ${pl.readJsonl(pl.shortTermPath(result.persona)).length} 筆` +
|
||||
`|關係人 ${pl.loadRelations(result.persona).nodes.length} 位`,
|
||||
];
|
||||
if (flags.load) {
|
||||
if (data.host && data.host !== result.persona) {
|
||||
lines.push(` ⚠ 本 session 已載入 \`${data.host}\`,未自動載入;要用它請先 release。`);
|
||||
} else {
|
||||
try {
|
||||
pl.acquireLock(result.persona, session, { cwd: str(flags.cwd) || null });
|
||||
pl.bindHost(session, result.persona, { cwd: str(flags.cwd) || null });
|
||||
lines.push(` 已載入 \`${result.persona}\`,可以直接開始聊。`);
|
||||
} catch (err) {
|
||||
lines.push(` ⚠ 自動載入失敗:${err.message}`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
lines.push(` 下一步:/jsc-persona:persona-chat ${result.persona}`);
|
||||
}
|
||||
emit(result, flags.json, lines);
|
||||
};
|
||||
|
||||
/**
|
||||
* 人格編號:英文名全大寫 + 兩位索引(同名才遞增),也就是 Gitea 存取庫的名稱。
|
||||
* 既有人格用 `code assign --romaji <英文名>` 補編號,加 `--rename` 連目錄名一起改成編號。
|
||||
@@ -1530,9 +1634,14 @@ commands.code = async ({ flags, positional }) => {
|
||||
if (action === "next") {
|
||||
const base = gt.normalizeRomaji(str(flags.romaji));
|
||||
if (!base) die("需要 `--romaji <英文名>`(只能是拉丁字母與數字)。");
|
||||
const next = gt.nextCode(base);
|
||||
if (!next) die(`\`${base}\` 的編號已經用到 99。`);
|
||||
emit({ romaji: base, code: next }, flags.json, [`\`${base}\` 的下一個可用編號:\`${next}\``]);
|
||||
const next = await gt.nextCodeAcrossMachines(base, { owner: str(flags.owner) || null });
|
||||
if (!next.code) die(`\`${base}\` 的編號已經用到 99。`);
|
||||
emit({ romaji: base, ...next }, flags.json, [
|
||||
`\`${base}\` 的下一個可用編號:\`${next.code}\``,
|
||||
next.checked_remote
|
||||
? ` (已對過 Gitea 上的 ${next.taken.length} 個編號)`
|
||||
: ` ⚠ 只看了本機,沒對過 Gitea(${next.reason})——換機器可能會撞號。`,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
const slug = hostOf(flags, session);
|
||||
@@ -1551,12 +1660,18 @@ commands.code = async ({ flags, positional }) => {
|
||||
const existing = gt.personaCode(slug);
|
||||
if (existing && !flags.force) die(`人格 \`${slug}\` 已有編號 \`${existing}\`。要重新指派請加 --force。`);
|
||||
let code = str(flags.code);
|
||||
let codeNote = "";
|
||||
if (code && !gt.validCode(code)) die(`編號 \`${code}\` 不合法(格式:ASUNA-01)。`);
|
||||
if (!code) {
|
||||
const base = gt.normalizeRomaji(str(flags.romaji) || slug);
|
||||
if (!base) die("需要 `--romaji <英文名>`(中文名請先轉羅馬拼音並跟使用者確認拼法)。");
|
||||
code = gt.nextCode(base);
|
||||
// 發號前先問遠端:本機看不到別台機器已經發出去的編號
|
||||
const next = await gt.nextCodeAcrossMachines(base, { owner: str(flags.owner) || null });
|
||||
code = next.code;
|
||||
if (!code) die(`\`${base}\` 的編號已經用到 99。`);
|
||||
codeNote = next.checked_remote
|
||||
? ` (已對過 Gitea 上的 ${next.taken.length} 個編號,不會跟別台機器撞號)`
|
||||
: ` ⚠ 編號只對過本機,沒對過 Gitea(${next.reason})——別台機器可能已經用掉這個號。`;
|
||||
}
|
||||
const config = pl.loadConfig(slug);
|
||||
config.persona = slug;
|
||||
@@ -1564,7 +1679,7 @@ commands.code = async ({ flags, positional }) => {
|
||||
config.romaji = gt.codePrefix(code);
|
||||
config.schema = 2;
|
||||
pl.writeJson(pl.configPath(slug), config);
|
||||
const lines = [`✔ 人格 \`${slug}\` 的編號指派為 \`${code}\`。`];
|
||||
const lines = [`✔ 人格 \`${slug}\` 的編號指派為 \`${code}\`。`, ...(codeNote ? [codeNote] : [])];
|
||||
let current = slug;
|
||||
if (flags.rename && slug !== code) {
|
||||
if (pl.personaExists(code)) die(`目錄 \`${code}\` 已存在,無法改名。`);
|
||||
@@ -2061,7 +2176,13 @@ const HELP = `persona.mjs — jsc-persona 人格 / 記憶 / 情緒 / 關係圖 C
|
||||
產出 icon.svg + icon.png,設為 Gitea 存取庫頭像並同步到 Wiki 區。
|
||||
|
||||
編號與 Gitea(存取庫名稱 = 人格編號):
|
||||
code show|assign|next --session <id> [--romaji <英文名> --code <ASUNA-01> --rename --force --public]
|
||||
code show|assign|next --session <id> [--romaji <英文名> --code <ASUNA-01> --rename --force --public --owner]
|
||||
發新編號前會先問遠端有哪些編號(避免換機器撞號);Gitea 連不上就只看本機並明講
|
||||
clone --session <id> [--code <ASUNA-01>|<ASUNA-01>] [--persona <目錄名> --owner --force --load --json]
|
||||
把一個**本機還沒有**的人格從 Gitea 整個拉回來(換機器接續同一個人格的第一步)。
|
||||
不帶 --code 就列出遠端有哪些人格、哪些本機還沒有。
|
||||
唯一不用先載入該人格的同步入口(本機沒有它就 load 不了它)。
|
||||
本機已有同名人格時預設不覆蓋:要蓋加 --force,或用 --persona 拉成第二份。
|
||||
sync status|init|push|pull|verify --session <id> [--area files|wiki|all --if-due --force --message --owner]
|
||||
verify 會確認「本機 = Gitea」,不一致就以非零結束(形象圖必須同步)
|
||||
檔案區=高頻活狀態(情緒/短期記憶/心裡話/逐字),每輪對話後背景 push
|
||||
@@ -2071,9 +2192,10 @@ const HELP = `persona.mjs — jsc-persona 人格 / 記憶 / 情緒 / 關係圖 C
|
||||
環境變數:GITEA_HOST / GITEA_TOKEN(或 PERSONA_GITEA_HOST / _TOKEN / _OWNER),
|
||||
PERSONA_GITEA=off 可整個關掉,PERSONA_SYNC_MIN_SECONDS 調 push 間隔
|
||||
|
||||
搬家(離線檔案):
|
||||
搬家:
|
||||
export --session <id> [--out <檔案> --with-journal --gzip --force] 匯出目前載入的人格
|
||||
import --session <id> --file <檔案> [--persona <新 slug> --force --load]
|
||||
import --session <id> --file <檔案> [--persona <新 slug> --force --load] 從 bundle 檔匯入
|
||||
clone --session <id> [--code <ASUNA-01>] 從 Gitea 匯入(見上)
|
||||
|
||||
維護:
|
||||
gc 清理死鎖與過期租約
|
||||
|
||||
Reference in New Issue
Block a user