feat(persona): 時區改為+08:00等值轉換、補機密遮蔽,並接上shared共用規範

依 todo.md 執行的規範治理專案:
- persona-lib.mjs 的 iso()/nowIso() 改為輸出 +08:00 offset(等值瞬間轉換,
  非加8小時),新增 nowDisplay()(人讀顯示格式)與 todayTaipei()(台北曆日
  日期鍵),修正 11 處原本用 UTC 字串切片產生日期鍵的錯誤用法;新增
  redactSecrets() 並套用到 persona-gitea.mjs 的 Gitea 錯誤路徑,避免機密
  外洩。
- 新增一次性遷移腳本 scripts/migrate-tz.mjs(備份/dry-run/冪等/衝突偵測
  中止),供之後有真實人格資料需要轉換時使用。
- persona 13 個 skill 與 AGENTS.md 接上 /jsc-shared 共用規範(spec-output、
  spec-script-path、spec-subagent、spec-gitea、spec-git-safety、
  spec-skill-invocation 等),不再各自重抄,且完全未變動人格行為本身的規範
  (情緒模型、記憶衰減、劇場模式、鎖與隔離機制等逐字保留)。
- selftest 全數通過(788/788,含本次新增的時區邊界/冪等性測試)。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-11 06:06:00 +00:00
co-authored by Claude Sonnet 5
parent 5fb2a43189
commit ac2045e52f
25 changed files with 927 additions and 181 deletions
+604
View File
@@ -0,0 +1,604 @@
#!/usr/bin/env node
// migrate-tz.mjs — 一次性遷移:修正既有人格資料裡的時區資料。
//
// 背景
// ----
// `persona-lib.mjs` 的 `iso()`/`nowIso()`(約第 140 行)現在會把時刻位移到台北時區
// 再輸出 `+08:00` 字尾;`todayTaipei()`(約第 168 行)也是同一套位移邏輯,取代了
// 「錯誤的 `nowIso().slice(0, 10)`」(那個註解本身就是在講:舊版 `iso()` 曾經機械地
// 回傳 UTC `Z` 時間戳,沒有位移,直接切前 10 個字元拿到的是 UTC 當天,不是台北當天)。
//
// 這兩個函式修好之後**不會回頭改舊資料**:在 UTC 16:00~23:59(台北已經跨到下一天)
// 這八小時窗口內寫下的舊紀錄,時間戳本身是 `...Z` 格式(可以無損換算),但由它
// 切出來的日期鍵(例如「今天」的日記日期)算出來的是 UTC 當天,比實際的台北曆日
// 少一天。這支腳本就是把這兩類舊資料換算成正確答案:
//
// (1) 任何 `Z` 結尾的 ISO 時間戳字串 → 等值換算成 `+08:00` 格式(換算成同一個
// 瞬間在台北時區的牆上時間,不是機械加 8 小時:兩者在大多數時刻結果一樣,
// 但概念上與程式碼上都要走「取 epoch → 用 iso() 同一套位移公式重新格式化」
// 這條路,才經得起夏令時間/閏秒之類未來變動)。
// (2) 任何「純日期」欄位(`yyyy-MM-dd`,沒有時間部分)→ 如果它所在的**同一個
// JSON 物件**裡,找得到剛好一個(不模糊)完整時間戳可以佐證「這筆記錄實際
// 發生在哪個台北曆日」,就用那個時間戳重算;同一物件裡的時間戳互相矛盾,
// 或根本沒有時間戳可用,就不動它,只記到報告的「無法確定」清單。
//
// 資料模型調查結論(見 persona-lib.mjs)
// --------------------------------------
// * `state/mood.json`(`loadDayMood`/`updateDayMood`,約第 716~752 行):
// `{ date, valence, arousal, samples, updated_at }` —— `date` 與 `updated_at`
// 是同一個物件的兩個欄位,`updated_at` 就是這筆「當日心情底色」最後一次被寫入
// 的完整時間戳,可以直接拿來重算 `date` 應該是哪個台北曆日。這是本腳本能夠
// 自動判定重算的典型案例(也是題目說的「日記的單日紀錄」)。
// * `relations/graph.json` 的 `nodes[].rifts[]`(`openRifts`/`riftBrief`,
// 約第 4854~4886 行;寫入邏輯在 persona.mjs 的 `relation rift`):
// 每筆 `{ at, about, memory?, healed_at? }` 都是純日期鍵,但**同一筆 rift 物件
// 裡沒有伴隨的完整時間戳**——唯一的時間戳在它的上層節點(`node.updated_at`),
// 而 `node.updated_at` 是整個節點共用、會被之後任何一次更新(改親近度、加另一件
// 還沒和好的事…)覆寫掉的欄位,不能拿來當「這一筆 rift 當時是幾點」的證據。
// 這一類欄位落進「無法確定,需人工檢視」清單,不強行用不可靠的上層時間戳去猜。
// * 自傳章節(`memory/chapters/*.md`)與長期記憶(`memory/long-term/*.md`)的
// `from`/`to`/`first_seen`/`last_seen` 等日期鍵存在 Markdown front matter 裡,
// 不是 `.json` 檔——依題目指示本腳本只走 `.json`,這兩處天生不在掃描範圍內,
// 如果之後要修,需要另一支專門處理 Markdown front matter 的腳本。
// * 其餘找到的日期鍵/時間戳欄位(`state/emotion.json` 的 `updated_at`/
// `streak.at`/`last_trigger.at`、`state/config.json` 的 `created_at`、
// `state/lock.json`/`guests.json`/`sleepers.json` 的 `*_at`、
// `state/loops.json` 的 `opened_at`/`touched_at`、
// `memory/import/<work>/work.json`/`names.json` 的 `created_at`/`updated_at`)
// 全部都是完整時間戳,沒有「純日期」欄位,直接吃規則 (1) 就好。
//
// 掃描範圍
// --------
// 只遞迴走訪 `PERSONA_HOME` 底下、排除 `.runtime`/`.rooms`(這兩個是跨人格的
// 執行期目錄,不是人格目錄)的每一個子目錄裡的每一個 `.json` 檔案;不動
// `.jsonl`(append-only 日誌,語意上是「歷史事件序列」,不是「這一筆的日期鍵」,
// 且題目本身就是說 `.json`)。
//
// 用法
// ----
// node migrate-tz.mjs [--dry-run] [--home <path>]
//
// 不帶 `--home` 時依序退回 `PERSONA_HOME` 環境變數、`~/.claude/personas`。
//
// 安全機制
// --------
// a. 執行前一定先檢查是否有人格處於載入狀態(exclusive lock 或存活的 guest
// 租約)——不管是不是 `--dry-run`。有就中止、不強行執行。
// b. 正式執行(非 `--dry-run`)一定先把整個 `PERSONA_HOME` 完整複製備份到
// `<上一層目錄>/personas.bak-<yyyyMMddHHmmss>/`(時間戳是台北時區)。
// `--dry-run` 不寫入任何檔案,連備份都不做。
// c. 先掃描「整個」`PERSONA_HOME`、算出全部異動計畫,**掃描階段完全不寫檔**;
// 只有掃描完全結束、且沒有偵測到合併衝突,才會真正落筆——這樣「合併衝突
// 讓整批中止」才能保證「不寫入任何檔案」,不會有些檔案改了、有些沒改。
// d. 合併衝突:同一個陣列裡,兩筆以上物件成員共用同一個欄位名稱、原始值不同,
// 重算後卻落在同一個台北日──這種情況不自動選一筆,整批中止、報告列出
// 衝突的原始內容,等人工決定怎麼合併再重跑。
// e. 執行完(非 dry-run)後自動驗證:全樹已經沒有 `Z` 結尾的時間戳字串;
// 每一個被轉換過的時間戳欄位,轉換前後的 epoch 完全相等。任何一項不成立
// 都算嚴重錯誤(exit code 4),需要人工介入。
//
// exit code:0 成功(含「沒有東西可遷移」)/1 一般錯誤/2 有人格正在載入而中止/
// 3 偵測到合併衝突而中止/4 執行後驗證失敗(嚴重錯誤)。
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
// --------------------------------------------------------------------------- //
// 路徑與時間工具(刻意仿照 persona-lib.mjs 的 iso()/todayTaipei(),見檔案開頭
// 註解;不 import persona-lib.mjs,讓這支一次性腳本不依賴那個模組今後怎麼演化)
// --------------------------------------------------------------------------- //
const TAIPEI_OFFSET_MS = 8 * 60 * 60 * 1000;
const RUNTIME_DIRNAME = ".runtime";
const ROOMS_DIRNAME = ".rooms";
function expandUser(p) {
if (p === "~") return os.homedir();
if (p.startsWith("~/")) return path.join(os.homedir(), p.slice(2));
return p;
}
function resolveHome(homeArg) {
const raw = homeArg || process.env.PERSONA_HOME || "~/.claude/personas";
return path.resolve(expandUser(raw));
}
/** 等同 persona-lib.mjs 的 iso():取 epoch,位移 8 小時,再格式化——不是機械加 8 小時。 */
function taipeiIsoFromEpoch(ms) {
const truncated = Math.floor(ms / 1000) * 1000;
const shifted = new Date(truncated + TAIPEI_OFFSET_MS);
const pad = (n) => String(n).padStart(2, "0");
return (
`${shifted.getUTCFullYear()}-${pad(shifted.getUTCMonth() + 1)}-${pad(shifted.getUTCDate())}` +
`T${pad(shifted.getUTCHours())}:${pad(shifted.getUTCMinutes())}:${pad(shifted.getUTCSeconds())}+08:00`
);
}
/** 等同 persona-lib.mjs 的 todayTaipei():同一個瞬間換算成台北曆日。 */
function taipeiDateFromEpoch(ms) {
const shifted = new Date(Math.floor(ms / 1000) * 1000 + TAIPEI_OFFSET_MS);
const pad = (n) => String(n).padStart(2, "0");
return `${shifted.getUTCFullYear()}-${pad(shifted.getUTCMonth() + 1)}-${pad(shifted.getUTCDate())}`;
}
/** 備份檔名用的緊湊時間戳(台北時區):yyyyMMddHHmmss。 */
function taipeiCompactStamp(date = new Date()) {
const shifted = new Date(Math.floor(date.getTime() / 1000) * 1000 + TAIPEI_OFFSET_MS);
const pad = (n) => String(n).padStart(2, "0");
return (
`${shifted.getUTCFullYear()}${pad(shifted.getUTCMonth() + 1)}${pad(shifted.getUTCDate())}` +
`${pad(shifted.getUTCHours())}${pad(shifted.getUTCMinutes())}${pad(shifted.getUTCSeconds())}`
);
}
// --------------------------------------------------------------------------- //
// 值的形狀判斷(全部靠值的形狀,不靠欄位名稱白名單)
// --------------------------------------------------------------------------- //
const Z_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
const TW_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+08:00$/;
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
/** 形狀對了還要日曆上真的存在(擋 2026-13-40 之類假日期),做法跟 persona-lib.mjs 的 isYmd 一樣。 */
function isValidCalendarDate(value) {
if (!DATE_RE.test(value)) return false;
const d = new Date(`${value}T00:00:00Z`);
return !Number.isNaN(d.getTime()) && d.toISOString().slice(0, 10) === value;
}
/** 一個物件自己身上(不含巢狀子物件)所有完整時間戳,換算後的台北曆日集合。 */
function siblingTaipeiDates(obj) {
const dates = new Set();
for (const value of Object.values(obj)) {
if (typeof value !== "string") continue;
if (Z_RE.test(value) || TW_RE.test(value)) {
const t = new Date(value).getTime();
if (!Number.isNaN(t)) dates.add(taipeiDateFromEpoch(t));
}
}
return dates;
}
// --------------------------------------------------------------------------- //
// 樹狀掃描:只算出「該怎麼改」,完全不動傳進來的物件、也不寫檔
// --------------------------------------------------------------------------- //
/**
* 掃一個檔案已經 parse 好的 JSON 樹。
* 回報三種結果:
* changes 要改的欄位(timestamp:Z→+08:00;datekey:日期鍵重算)
* undetermined 純日期欄位但推算不出正確台北日,維持原值
* conflicts 同一個陣列裡兩筆以上成員重算後撞在同一個台北日
*/
function scanTree(root, fileLabel) {
const changes = [];
const undetermined = [];
const conflicts = [];
function visitObject(obj, pathArr) {
const resolvable = siblingTaipeiDates(obj);
for (const [key, value] of Object.entries(obj)) {
if (typeof value === "string") {
if (Z_RE.test(value)) {
const epochOld = new Date(value).getTime();
const newValue = taipeiIsoFromEpoch(epochOld);
changes.push({
file: fileLabel,
path: [...pathArr, key],
kind: "timestamp",
oldValue: value,
newValue,
epochOld,
epochNew: new Date(newValue).getTime(),
});
} else if (DATE_RE.test(value) && isValidCalendarDate(value)) {
if (resolvable.size === 1) {
const [only] = resolvable;
if (only !== value) {
changes.push({ file: fileLabel, path: [...pathArr, key], kind: "datekey", oldValue: value, newValue: only });
}
} else if (resolvable.size === 0) {
undetermined.push({
file: fileLabel,
path: [...pathArr, key],
value,
reason: "同一物件裡找不到可用的完整時間戳",
});
} else {
undetermined.push({
file: fileLabel,
path: [...pathArr, key],
value,
reason: `同一物件裡有 ${resolvable.size} 個互相矛盾的時間戳(${[...resolvable].join(" / ")}),無法判斷歸屬哪個台北日`,
});
}
}
} else if (value && typeof value === "object") {
visit(value, [...pathArr, key]);
}
}
}
/** 同一個陣列裡,兩筆以上物件成員共用同一個欄位名稱、原始值不同,卻重算成同一個台北日。 */
function detectArrayConflicts(arr, pathArr) {
const objItems = arr
.map((item, i) => ({ item, i }))
.filter(({ item }) => item && typeof item === "object" && !Array.isArray(item));
if (objItems.length < 2) return;
const fieldNames = new Set();
for (const { item } of objItems) {
for (const [k, v] of Object.entries(item)) {
if (typeof v === "string" && DATE_RE.test(v) && isValidCalendarDate(v)) fieldNames.add(k);
}
}
for (const field of fieldNames) {
const groups = new Map(); // 新值 → [{index, original, item}]
for (const { item, i } of objItems) {
const raw = item[field];
if (typeof raw !== "string" || !DATE_RE.test(raw) || !isValidCalendarDate(raw)) continue;
const dates = siblingTaipeiDates(item);
if (dates.size !== 1) continue; // 這筆自己就推算不出來,不參與衝突判斷(會落進 undetermined)
const [resolved] = dates;
if (!groups.has(resolved)) groups.set(resolved, []);
groups.get(resolved).push({ index: i, original: raw, item });
}
for (const [newValue, group] of groups) {
if (group.length < 2) continue;
const distinctOriginals = new Set(group.map((g) => g.original));
if (distinctOriginals.size > 1) {
conflicts.push({
file: fileLabel,
path: [...pathArr],
field,
newValue,
entries: group.map((g) => ({
index: g.index,
path: [...pathArr, g.index, field],
original: g.original,
record: g.item,
})),
});
}
}
}
}
function visit(node, pathArr) {
if (Array.isArray(node)) {
detectArrayConflicts(node, pathArr);
node.forEach((item, i) => {
if (item && typeof item === "object") visit(item, [...pathArr, i]);
});
return;
}
if (node && typeof node === "object") {
visitObject(node, pathArr);
}
}
if (Array.isArray(root) || (root && typeof root === "object")) {
visit(root, []);
} else if (typeof root === "string") {
// 罕見邊界:整個檔案的內容就是一個字串(沒有其他欄位可以當 sibling)。
if (Z_RE.test(root)) {
const epochOld = new Date(root).getTime();
const newValue = taipeiIsoFromEpoch(epochOld);
changes.push({ file: fileLabel, path: [], kind: "timestamp", oldValue: root, newValue, epochOld, epochNew: new Date(newValue).getTime() });
} else if (DATE_RE.test(root) && isValidCalendarDate(root)) {
undetermined.push({ file: fileLabel, path: [], value: root, reason: "檔案頂層就是日期字串,沒有其他欄位可用來推算" });
}
}
return { changes, undetermined, conflicts };
}
function applyChanges(root, changes) {
let newRoot = root;
for (const change of changes) {
if (change.path.length === 0) {
newRoot = change.newValue;
continue;
}
let cur = newRoot;
for (let i = 0; i < change.path.length - 1; i++) cur = cur[change.path[i]];
cur[change.path[change.path.length - 1]] = change.newValue;
}
return newRoot;
}
// --------------------------------------------------------------------------- //
// 檔案/目錄走訪
// --------------------------------------------------------------------------- //
function listPersonaDirs(home) {
let entries;
try {
entries = fs.readdirSync(home, { withFileTypes: true });
} catch {
return null; // home 不存在
}
return entries
.filter((e) => e.isDirectory() && e.name !== RUNTIME_DIRNAME && e.name !== ROOMS_DIRNAME && !e.name.startsWith("."))
.map((e) => e.name)
.sort();
}
function collectJsonFiles(dir) {
const out = [];
const stack = [dir];
while (stack.length) {
const cur = stack.pop();
let entries;
try {
entries = fs.readdirSync(cur, { withFileTypes: true });
} catch {
continue;
}
for (const e of entries) {
const full = path.join(cur, e.name);
if (e.isDirectory()) stack.push(full);
else if (e.isFile() && e.name.endsWith(".json")) out.push(full);
}
}
return out.sort();
}
// --------------------------------------------------------------------------- //
// 前置檢查:有沒有人格正在被載入
//
// 邏輯仿照 persona-lib.mjs 的 lockIsDead()/liveGuests()(約第 1607~1617 行、
// LEASE_SECONDS=900/GUEST_LEASE_SECONDS=1800,約第 23~24 行):只看心跳租約
// 有沒有過期,不靠 pid(CLI process 跑完就結束,不能拿 pid 存活判斷)。
// --------------------------------------------------------------------------- //
const LEASE_SECONDS = 900;
const GUEST_LEASE_SECONDS = 1800;
function readJsonSafe(file) {
try {
return JSON.parse(fs.readFileSync(file, "utf8"));
} catch {
return null;
}
}
function checkActiveLocks(home, personaDirs) {
const active = [];
for (const slug of personaDirs) {
const lock = readJsonSafe(path.join(home, slug, "state", "lock.json"));
if (lock && typeof lock === "object" && lock.heartbeat_at) {
const hbAge = (Date.now() - new Date(lock.heartbeat_at).getTime()) / 1000;
const lease = Number(lock.lease_seconds) || LEASE_SECONDS;
if (Number.isFinite(hbAge) && hbAge <= lease) {
active.push({ slug, kind: "lock", session_id: lock.session_id, cwd: lock.cwd, heartbeat_at: lock.heartbeat_at });
}
}
const guestsData = readJsonSafe(path.join(home, slug, "state", "guests.json"));
const guests = Array.isArray(guestsData?.guests) ? guestsData.guests : [];
for (const g of guests) {
if (!g || !g.heartbeat_at) continue;
const hbAge = (Date.now() - new Date(g.heartbeat_at).getTime()) / 1000;
if (Number.isFinite(hbAge) && hbAge <= GUEST_LEASE_SECONDS) {
active.push({ slug, kind: "guest", session_id: g.session_id, room: g.room, heartbeat_at: g.heartbeat_at });
}
}
}
return active;
}
// --------------------------------------------------------------------------- //
// 備份
// --------------------------------------------------------------------------- //
function backupHome(home) {
const parent = path.dirname(home);
const dest = path.join(parent, `personas.bak-${taipeiCompactStamp(new Date())}`);
fs.cpSync(home, dest, { recursive: true });
return dest;
}
// --------------------------------------------------------------------------- //
// 執行後驗證
// --------------------------------------------------------------------------- //
function verifyNoLeftoverZ(home, personaDirs) {
const offenders = [];
for (const slug of personaDirs) {
for (const file of collectJsonFiles(path.join(home, slug))) {
const text = fs.readFileSync(file, "utf8");
// 粗略但夠用:找形似 ISO 時間戳、以 Z 結尾的片段(跟 Z_RE 同一個形狀,
// 只是不要求整個字串就是它——用來抓「文件裡還殘留 Z 時間戳」這個粗粒度事實)。
const re = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/g;
let m;
while ((m = re.exec(text))) offenders.push({ file, match: m[0] });
}
}
return offenders;
}
// --------------------------------------------------------------------------- //
// CLI
// --------------------------------------------------------------------------- //
function parseArgs(argv) {
const out = { dryRun: false, home: null, help: false };
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a === "--dry-run") out.dryRun = true;
else if (a === "--home") out.home = argv[++i];
else if (a === "--help" || a === "-h") out.help = true;
else {
process.stderr.write(`未知的參數:${a}\n`);
process.exit(1);
}
}
return out;
}
function printHelp() {
process.stdout.write(
"用法:node migrate-tz.mjs [--dry-run] [--home <path>]\n" +
" --dry-run 只掃描並印出報告,不寫入任何檔案(連備份都不做)\n" +
" --home 指定 PERSONA_HOME;不帶時用環境變數 PERSONA_HOME,再退回 ~/.claude/personas\n",
);
}
function fmtPath(p) {
return p.length ? `/${p.join("/")}` : "(root)";
}
function main() {
const args = parseArgs(process.argv.slice(2));
if (args.help) {
printHelp();
return 0;
}
const home = resolveHome(args.home);
console.log(`[migrate-tz] PERSONA_HOME = ${home}${args.dryRun ? "(--dry-run,不會寫入任何檔案)" : ""}`);
const personaDirs = listPersonaDirs(home);
if (personaDirs === null) {
console.log(`[migrate-tz] 目錄不存在,沒有東西可遷移:${home}`);
return 0;
}
if (personaDirs.length === 0) {
console.log(`[migrate-tz] 目錄裡沒有人格子目錄,沒有東西可遷移:${home}`);
return 0;
}
console.log(`[migrate-tz] 找到 ${personaDirs.length} 個人格目錄:${personaDirs.join(", ")}`);
// a. 前置檢查:有沒有人格正在載入
const activeLocks = checkActiveLocks(home, personaDirs);
if (activeLocks.length) {
console.error("[migrate-tz] 中止:以下人格目前處於載入狀態,不強行執行:");
for (const a of activeLocks) {
if (a.kind === "lock") {
console.error(` - ${a.slug}:exclusive lock(session ${a.session_id},cwd ${a.cwd},最後心跳 ${a.heartbeat_at})`);
} else {
console.error(` - ${a.slug}:guest 租約(session ${a.session_id},room ${a.room},最後心跳 ${a.heartbeat_at})`);
}
}
console.error("請先讓所有人格釋放鎖(結束對話 / persona status --release)再重跑這支腳本。");
return 2;
}
console.log("[migrate-tz] 前置檢查通過:沒有人格處於載入狀態。");
// b. 備份(dry-run 完全不寫檔,連備份都不做)
let backupDir = null;
if (!args.dryRun) {
backupDir = backupHome(home);
console.log(`[migrate-tz] 已備份到:${backupDir}`);
}
// c/d/e. 掃描整個 PERSONA_HOME,算出全部異動計畫;掃描階段完全不寫檔
const perFile = []; // { file, label, root, changes }
const allChanges = [];
const allUndetermined = [];
const allConflicts = [];
for (const slug of personaDirs) {
for (const file of collectJsonFiles(path.join(home, slug))) {
const label = path.relative(home, file);
const raw = fs.readFileSync(file, "utf8");
let root;
try {
root = JSON.parse(raw);
} catch (err) {
console.error(`[migrate-tz] 略過(JSON 解析失敗,不會被計入任何統計):${label} — ${err.message}`);
continue;
}
const { changes, undetermined, conflicts } = scanTree(root, label);
if (changes.length) perFile.push({ file, label, root, changes });
allChanges.push(...changes);
allUndetermined.push(...undetermined);
allConflicts.push(...conflicts);
}
}
const tsChanges = allChanges.filter((c) => c.kind === "timestamp");
const dateChanges = allChanges.filter((c) => c.kind === "datekey");
const filesChanged = new Set(perFile.map((f) => f.label));
console.log("");
console.log("========== 掃描報告 ==========");
console.log(`將改動的檔案數:${filesChanged.size}`);
console.log(`時間戳欄位(Z → +08:00):${tsChanges.length}`);
console.log(`日期鍵欄位(重算台北曆日):${dateChanges.length}`);
console.log(`無法確定(維持原值,需人工檢視):${allUndetermined.length}`);
console.log(`合併衝突:${allConflicts.length}`);
if (allUndetermined.length) {
console.log("");
console.log("---- 無法確定,需人工檢視 ----");
for (const u of allUndetermined) {
console.log(` ${u.file}${fmtPath(u.path)} = "${u.value}" — ${u.reason}`);
}
}
if (allConflicts.length) {
console.log("");
console.log("---- 合併衝突(未寫入任何檔案,需人工決定合併方式後重跑) ----");
for (const c of allConflicts) {
console.log(` 檔案 ${c.file},陣列 ${fmtPath(c.path)},欄位 \`${c.field}\`,重算後都落在 ${c.newValue}:`);
for (const e of c.entries) {
console.log(` - [${e.index}] 原始值 ${e.original}:${JSON.stringify(e.record)}`);
}
}
console.error("");
console.error(`[migrate-tz] 中止:偵測到 ${allConflicts.length} 組合併衝突,不寫入任何檔案。`);
console.error("請先人工決定怎麼合併上面列出的紀錄,再重新執行這支腳本。");
return 3;
}
if (args.dryRun) {
console.log("");
console.log("[migrate-tz] --dry-run:以上為預覽,沒有寫入任何檔案。");
return 0;
}
if (filesChanged.size === 0) {
console.log("");
console.log("[migrate-tz] 沒有需要修改的欄位(可能已經是 +08:00 格式,或本來就沒有需要換算的資料)。");
return 0;
}
// 落筆:只有掃描完全結束、確定沒有合併衝突,才寫入
for (const { file, root, changes } of perFile) {
const newRoot = applyChanges(root, changes);
const tmp = `${file}.tmp${process.pid}`;
fs.writeFileSync(tmp, JSON.stringify(newRoot, null, 2) + "\n", "utf8");
fs.renameSync(tmp, file);
}
console.log("");
console.log(`[migrate-tz] 已寫入 ${filesChanged.size} 個檔案:`);
for (const label of filesChanged) console.log(` - ${label}`);
// e. 執行後自動驗證
console.log("");
console.log("========== 執行後驗證 ==========");
const leftover = verifyNoLeftoverZ(home, personaDirs);
if (leftover.length) {
console.error(`[migrate-tz] 嚴重錯誤:全樹掃描後仍找到 ${leftover.length} 個 Z 結尾的時間戳,遷移不完整:`);
for (const o of leftover) console.error(` - ${o.file}: ${o.match}`);
return 4;
}
console.log("(i) 全樹已無殘留的 Z 結尾時間戳:通過。");
const epochMismatches = tsChanges.filter((c) => c.epochOld !== c.epochNew);
if (epochMismatches.length) {
console.error(`[migrate-tz] 嚴重錯誤:${epochMismatches.length} 個時間戳欄位轉換前後 epoch 不相等:`);
for (const m of epochMismatches) {
console.error(` - ${m.file}${fmtPath(m.path)}:${m.oldValue}(${m.epochOld}) → ${m.newValue}(${m.epochNew})`);
}
return 4;
}
console.log(`(ii) ${tsChanges.length} 個時間戳欄位逐一驗證轉換前後 epoch 相等:通過。`);
console.log("");
console.log(`[migrate-tz] 完成。備份保留於:${backupDir}`);
return 0;
}
process.exit(main());
+19 -11
View File
@@ -77,7 +77,7 @@ export async function nextCodeAcrossMachines(romaji, { owner = null } = {}) {
const taken = remote.personas.map((p) => p.code);
return { code: nextCode(romaji, { taken }), checked_remote: true, taken };
} catch (err) {
return { code: local, checked_remote: false, reason: String(err.message || err) };
return { code: local, checked_remote: false, reason: pl.redactSecrets(String(err.message || err)) };
}
}
@@ -178,6 +178,8 @@ export function updateSyncState(slug, mutate) {
// 環境與 API
// --------------------------------------------------------------------------- //
// giteaEnv()/giteaProblem() 實作 /jsc-shared:spec-gitea 的『token 解析優先序』章節:
// 專用變數(PERSONA_GITEA_TOKEN)插在最前面,其次才是 GITEA_TOKEN,順序不可打亂。
export function giteaEnv() {
const host = String(process.env.PERSONA_GITEA_HOST || process.env.GITEA_HOST || "").replace(/\/+$/, "");
const token = String(process.env.PERSONA_GITEA_TOKEN || process.env.GITEA_TOKEN || "");
@@ -194,6 +196,8 @@ export function giteaProblem() {
return null;
}
// api() 實作 /jsc-shared:spec-gitea 的『API 呼叫慣例』章節:
// base 為 `${host}/api/v1`,標頭帶 `Authorization: token <token>`,body 一律 UTF-8 JSON。
async function api(method, route, body = null) {
const { host, token } = giteaEnv();
const res = await fetch(`${host}/api/v1${route}`, {
@@ -223,7 +227,7 @@ export async function giteaLogin() {
const cached = pl.readJson(ownerCachePath(), {}) ?? {};
if (cached.host === env.host && cached.login) return cached.login;
const res = await api("GET", "/user");
if (!res.ok || !res.json?.login) throw new Error(`取不到 Gitea 帳號(HTTP ${res.status}):${res.text.slice(0, 120)}`);
if (!res.ok || !res.json?.login) throw new Error(`取不到 Gitea 帳號(HTTP ${res.status}):${pl.redactSecrets(res.text).slice(0, 120)}`);
pl.writeJson(ownerCachePath(), { host: env.host, login: res.json.login, cached_at: pl.nowIso() });
return res.json.login;
}
@@ -241,7 +245,7 @@ async function listRepos(owner, me) {
const out = [];
for (let page = 1; page <= 40; page += 1) {
const res = await api("GET", `${route}?page=${page}&limit=50`);
if (!res.ok) throw new Error(`列出 ${owner} 的存取庫失敗(HTTP ${res.status}):${res.text.slice(0, 160)}`);
if (!res.ok) throw new Error(`列出 ${owner} 的存取庫失敗(HTTP ${res.status}):${pl.redactSecrets(res.text).slice(0, 160)}`);
const batch = Array.isArray(res.json) ? res.json : [];
out.push(...batch);
if (batch.length < 50) break;
@@ -304,7 +308,7 @@ export async function ensureRepo(owner, code, { description = "", private_ = tru
description: description || `jsc-persona 人格 ${code}`,
auto_init: false,
});
if (!res.ok) throw new Error(`建立存取庫 ${owner}/${code} 失敗(HTTP ${res.status}):${res.text.slice(0, 160)}`);
if (!res.ok) throw new Error(`建立存取庫 ${owner}/${code} 失敗(HTTP ${res.status}):${pl.redactSecrets(res.text).slice(0, 160)}`);
void env;
return { repo: res.json, created: true };
}
@@ -318,7 +322,7 @@ export async function ensureWikiHome(owner, code, content) {
content_base64: Buffer.from(content, "utf8").toString("base64"),
message: "init: 人格設定百科",
});
if (!res.ok) throw new Error(`建立 Wiki 首頁失敗(HTTP ${res.status}):${res.text.slice(0, 160)}`);
if (!res.ok) throw new Error(`建立 Wiki 首頁失敗(HTTP ${res.status}):${pl.redactSecrets(res.text).slice(0, 160)}`);
return true;
}
@@ -367,7 +371,7 @@ export function git(args, cwd = null) {
function gitOrThrow(args, cwd, what) {
const res = git(args, cwd);
if (!res.ok) throw new Error(`${what} 失敗:git ${args.join(" ")}\n ${res.stderr || res.stdout}`);
if (!res.ok) throw new Error(`${what} 失敗:git ${args.join(" ")}\n ${pl.redactSecrets(res.stderr || res.stdout)}`);
return res;
}
@@ -478,6 +482,10 @@ const WIKI_PREFIX = { memory: "Memory", mindmap: "Mindmap", relations: "Relation
* Gitea 的 wiki **只有根目錄的 .md 會變成頁面**(1.27 實測:子目錄頁面連結 404),
* 所以低頻區的檔案要攤平成根層檔名(`memory/long-term/x.md` → `Memory-x.md`,
* 網頁上顯示為「Memory x」),再用 `_paths.json` 記住原本的路徑,pull 時才還原得回去。
*
* 對應 /jsc-shared:spec-gitea 的『Wiki 頁名轉義規則』第 3 點:本檔走 git clone/push
* 直接寫檔名,不反推 Gitea 的 title↔sub_url 內部轉義,改用 `_paths.json` manifest
* 自己記住「工作路徑 → 儲存檔名」的對應。
*/
export function wikiName(rel) {
if (!rel.includes("/")) return rel;
@@ -718,7 +726,7 @@ export async function pushArea(slug, area, { message = "", code = null, owner =
});
return { ok: true, changed: false, files: staged.length, area, code: theCode };
}
gitOrThrow(["commit", "-q", "-m", message || `sync(${area}): ${pl.nowIso()}`], dir, "git commit");
gitOrThrow(["commit", "-q", "-m", message || `sync(${area}): ${pl.nowDisplay()}`], dir, "git commit");
let pushed = git(["push", "-q", "-u", "origin", "HEAD"], dir);
let overwrote = null;
if (!pushed.ok) {
@@ -754,7 +762,7 @@ export async function pushArea(slug, area, { message = "", code = null, owner =
}
pushed = git(["push", "-q", "-u", "origin", "HEAD"], dir);
}
if (!pushed.ok) return { ok: false, area, code: theCode, reason: pushed.stderr || pushed.stdout };
if (!pushed.ok) return { ok: false, area, code: theCode, reason: pl.redactSecrets(pushed.stderr || pushed.stdout) };
updateSyncState(slug, (state) => {
state.code = theCode;
state.owner = theOwner;
@@ -802,7 +810,7 @@ export async function pullArea(slug, area, { code = null, owner = null, force =
clearStaleIndexLock(dir);
const localChanged = new Set(statusPaths(dir));
const fetched = git(["fetch", "--quiet", "origin"], dir);
if (!fetched.ok) return { ok: false, area, reason: fetched.stderr || "fetch 失敗" };
if (!fetched.ok) return { ok: false, area, reason: pl.redactSecrets(fetched.stderr) || "fetch 失敗" };
const head = git(["rev-parse", "--abbrev-ref", "HEAD"], dir).stdout || "main";
const remoteRef = `origin/${head}`;
const exists = git(["rev-parse", "--verify", "--quiet", remoteRef], dir);
@@ -815,7 +823,7 @@ export async function pullArea(slug, area, { code = null, owner = null, force =
}
git(["checkout", "--", "."], dir);
const reset = git(["reset", "--hard", "--quiet", remoteRef], dir);
if (!reset.ok) return { ok: false, area, reason: reset.stderr };
if (!reset.ok) return { ok: false, area, reason: pl.redactSecrets(reset.stderr) };
// 只寫回「遠端真的改過的」與「本機缺少的」。
// 不能無差別覆蓋:本機有較新但還沒 push 的內容時,那會把它蓋掉。
const root = pl.personaDir(slug);
@@ -920,7 +928,7 @@ export async function importFromRemote(code, { owner = null, slug = null, force
// 本機是空的,衝突判定沒有意義;覆蓋既有人格時使用者已經明講了 --force
results[area] = await pullArea(target, area, { code, owner: theOwner, force: true });
} catch (err) {
results[area] = { ok: false, area, reason: String(err.message || err) };
results[area] = { ok: false, area, reason: pl.redactSecrets(String(err.message || err)) };
}
}
if (!pl.personaExists(target)) {
+68 -9
View File
@@ -129,7 +129,20 @@ export function setDefaultPersona(slug) {
// 時間與檔案 IO
// --------------------------------------------------------------------------- //
export const iso = (date = new Date()) => new Date(Math.floor(date.getTime() / 1000) * 1000).toISOString().replace(".000Z", "Z");
const TAIPEI_OFFSET_MS = 8 * 60 * 60 * 1000;
/**
* 產生帶 `+08:00`(Asia/Taipei)offset 的 ISO 字串,例如 `2026-08-11T10:10:13+08:00`。
* 這是等值時區轉換:輸出代表跟輸入 `date` 完全相同的瞬間(epoch 不變),
* 只是把「顯示的牆上時間」換成台北時區,而不是原本 UTC 的 `Z`。
* `parseIso()`/`new Date()` 都能正確解析回相同的 epoch,既有時間差運算不受影響。
*/
export const iso = (date = new Date()) => {
const truncated = new Date(Math.floor(date.getTime() / 1000) * 1000);
const shifted = new Date(truncated.getTime() + TAIPEI_OFFSET_MS);
const pad = (n) => String(n).padStart(2, "0");
return `${shifted.getUTCFullYear()}-${pad(shifted.getUTCMonth() + 1)}-${pad(shifted.getUTCDate())}T${pad(shifted.getUTCHours())}:${pad(shifted.getUTCMinutes())}:${pad(shifted.getUTCSeconds())}+08:00`;
};
export const nowIso = () => iso(new Date());
export function parseIso(value) {
@@ -138,6 +151,26 @@ export function parseIso(value) {
return Number.isNaN(dt.getTime()) ? null : dt;
}
/**
* 給人看的顯示字串(不可被 `new Date()` 解析):Asia/Taipei 時區、`yyyy/MM/dd HH:mm:ss`。
* 用途:任何要印給使用者看或寫進 log 訊息的地方,依 /jsc-shared:spec-time-log 規定的格式。
*/
export function nowDisplay(date = new Date()) {
const shifted = new Date(Math.floor(date.getTime() / 1000) * 1000 + TAIPEI_OFFSET_MS);
const pad = (n) => String(n).padStart(2, "0");
return `${shifted.getUTCFullYear()}/${pad(shifted.getUTCMonth() + 1)}/${pad(shifted.getUTCDate())} ${pad(shifted.getUTCHours())}:${pad(shifted.getUTCMinutes())}:${pad(shifted.getUTCSeconds())}`;
}
/**
* 該時刻依台北曆日計算的 `yyyy-MM-dd` 日期字串,取代錯誤的 `nowIso().slice(0, 10)`(那是 UTC 當天)。
* 例如 UTC `2026-08-10T16:30:00Z`(台北 `2026-08-11 00:30`)要回傳 `2026-08-11`。
*/
export function todayTaipei(date = new Date()) {
const shifted = new Date(Math.floor(date.getTime() / 1000) * 1000 + TAIPEI_OFFSET_MS);
const pad = (n) => String(n).padStart(2, "0");
return `${shifted.getUTCFullYear()}-${pad(shifted.getUTCMonth() + 1)}-${pad(shifted.getUTCDate())}`;
}
export function ageSeconds(value) {
const dt = parseIso(value);
if (!dt) return Infinity;
@@ -686,7 +719,7 @@ export function loadDayMood(slug) {
const data = readJson(dayMoodPath(slug), {}) ?? {};
const num = (v) => (Number.isFinite(Number(v)) ? Number(v) : 0);
return {
date: typeof data.date === "string" ? data.date : nowIso().slice(0, 10),
date: typeof data.date === "string" ? data.date : todayTaipei(),
valence: clamp(num(data.valence), -100, 100),
arousal: clamp(num(data.arousal), 0, 100),
samples: Math.max(0, Math.floor(num(data.samples))),
@@ -701,7 +734,7 @@ export function loadDayMood(slug) {
export function updateDayMood(slug, state = null) {
return withFileLock(dayMoodPath(slug), () => {
const day = loadDayMood(slug);
const today = nowIso().slice(0, 10);
const today = todayTaipei();
if (day.date !== today) {
day.valence = Math.round(day.valence * DAY_MOOD_CARRY * 10) / 10;
day.arousal = Math.round(day.arousal * DAY_MOOD_CARRY * 10) / 10;
@@ -724,7 +757,7 @@ export function sleepDayMood(slug) {
const day = loadDayMood(slug);
day.valence = Math.round(day.valence * DAY_MOOD_CARRY * 10) / 10;
day.arousal = Math.round(day.arousal * DAY_MOOD_CARRY * 10) / 10;
day.date = nowIso().slice(0, 10);
day.date = todayTaipei();
day.samples = 0;
day.updated_at = nowIso();
writeJson(dayMoodPath(slug), day);
@@ -2759,7 +2792,7 @@ export function writeLongTermMemory(slug, {
const file = path.join(longTermDir(slug), `${name}.md`);
let existing = {};
if (fs.existsSync(file)) [existing] = parseFrontMatter(fs.readFileSync(file, "utf8"));
const today = nowIso().slice(0, 10);
const today = todayTaipei();
const stamp = contextStamp(slug);
// `gist` 明寫時,`detail` 沒給就是**沒有細節**——退回整份 body 會讓主旨之外
// 再抄一次全文,那是把「衰減先吃細節」這件事整個抵銷掉。
@@ -2844,7 +2877,7 @@ export function writeChapter(slug, { name, title = "", from = "", to = "", body
"---",
`name: ${key}`,
`title: ${fm(title || existing.title || name).slice(0, 120)}`,
`from: ${fm(from) || existing.from || nowIso().slice(0, 10)}`,
`from: ${fm(from) || existing.from || todayTaipei()}`,
// `to` 空著=還在這一段裡。所以沒有值就**不寫這一行**,不要寫成 `to: `。
...((fm(to) || existing.to) ? [`to: ${fm(to) || existing.to}`] : []),
`updated_at: ${nowIso()}`,
@@ -2900,7 +2933,7 @@ export function currentChapter(slug) {
}
/** 收掉一段(寫上 `to`)。找不到那一段回 null。 */
export function closeChapter(slug, name, at = nowIso().slice(0, 10)) {
export function closeChapter(slug, name, at = todayTaipei()) {
const key = slugify(name);
if (!fs.existsSync(chapterPath(slug, key))) return null;
return writeChapter(slug, { name: key, to: at });
@@ -2927,7 +2960,7 @@ export function rebuildIndex(slug) {
const lines = [
"# 長期記憶索引",
"",
`<!-- 由 persona.mjs 自動產生,最後更新 ${nowIso()};一則記憶一行 -->`,
`<!-- 由 persona.mjs 自動產生,最後更新 ${nowDisplay()};一則記憶一行 -->`,
"",
];
const sorted = [...entries].sort((a, b) => Number(b.salience || 0) - Number(a.salience || 0));
@@ -3244,7 +3277,7 @@ export function markShortTermReviewed(slug, { indexes = null, until = null, prom
*/
export function touchRecall(slug, names) {
const wanted = new Set(names);
const today = nowIso().slice(0, 10);
const today = todayTaipei();
for (const meta of longTermEntries(slug)) {
if (!wanted.has(meta._name)) continue;
let text;
@@ -6104,6 +6137,32 @@ export function injectSafeLine(text, limit = 0) {
return limit > 0 ? one.slice(0, limit) : one;
}
// --------------------------------------------------------------------------- //
// 機密遮蔽(port 自 doc/scripts/worklog/transcript.py 的 REDACT_PATTERNS,
// 為 /jsc-shared:spec-gitea『機密遮蔽實作』章節的來源依據;本檔僅共用定義,
// 套用地點見 A0-4:persona 側 Gitea 錯誤遮蔽)
// --------------------------------------------------------------------------- //
const REDACT_PATTERNS = [
[/[A-Za-z0-9_-]*:[A-Za-z0-9_-]{16,}@/g, "***@"], // URL 內嵌憑證 user:token@
[/\b[0-9a-f]{40}\b/g, "***"], // Gitea 40 字元 token
[/\bgh[pousr]_[A-Za-z0-9_]{16,}\b/g, "***"], // GitHub token
[/\bsk-[A-Za-z0-9\-_]{16,}\b/g, "***"], // API key
[/\b(token|password|passwd|pwd|secret|api[_-]?key)\b\s*[:=]\s*\S+/gi, "$1=***"],
[/Authorization:\s*(token|bearer)\s+\S+/gi, "Authorization: $1 ***"],
[/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g, "***"], // Email
[/\b09\d{2}[-\s]?\d{3}[-\s]?\d{3}\b/g, "***"], // 台灣手機
[/\b[A-Z][12]\d{8}\b/g, "***"], // 身分證字號
];
/** 對文字依序套用全部機密遮蔽規則,回傳遮蔽後的結果。 */
// 本檔遮蔽規則對應 shared/scripts/lib/redact-patterns.json(經 /jsc-shared:spec-gitea 收斂)
export function redactSecrets(text) {
let out = String(text ?? "");
for (const [pattern, replacement] of REDACT_PATTERNS) out = out.replace(pattern, replacement);
return out;
}
export function identityFields(slug) {
const fields = {};
let text;
+7 -7
View File
@@ -479,7 +479,7 @@ commands.release = async ({ flags }) => {
if (!flags["no-gitea"] && !gt.giteaProblem() && pl.personaExists(slug) && gt.personaCode(slug)) {
for (const area of gt.AREA_KEYS) {
try {
const res = await gt.pushArea(slug, area, { message: `release: 對話結束 ${pl.nowIso()}` });
const res = await gt.pushArea(slug, area, { message: `release: 對話結束 ${pl.nowDisplay()}` });
if (res.ok && res.changed) say(` ↑ ${gt.AREAS[area].label}已推上 Gitea。`);
else if (!res.ok && !res.skipped) say(` ⚠ ${gt.AREAS[area].label}推送失敗:${String(res.reason).slice(0, 120)}`);
} catch (err) {
@@ -594,7 +594,7 @@ commands.sleep = async ({ flags }) => {
} else {
for (const area of gt.AREA_KEYS) {
try {
const res = await gt.pushArea(slug, area, { message: `sleep: 收尾 ${pl.nowIso()}` });
const res = await gt.pushArea(slug, area, { message: `sleep: 收尾 ${pl.nowDisplay()}` });
sync[area] = res.ok ? (res.changed ? "pushed" : "no-change") : `failed: ${String(res.reason).slice(0, 80)}`;
} catch (err) {
sync[area] = `failed: ${String(err.message).slice(0, 80)}`;
@@ -1896,7 +1896,7 @@ commands.mindmap = ({ flags, positional }) => {
if (!fs.existsSync(file) || flags.force) {
pl.writeText(file, [
`%% 思維導圖(短期):${topic}`,
`%% created: ${pl.nowIso()} ttl: short-term(固化後請併入 semantic.mmd 並刪除)`,
`%% created: ${pl.nowDisplay()} ttl: short-term(固化後請併入 semantic.mmd 並刪除)`,
"graph LR",
` trigger["觸發:${topic}"] --> obs["觀察"]`,
' obs --> infer["推論"]',
@@ -2019,7 +2019,7 @@ commands.relation = ({ flags, positional }) => {
if (!open.length) die(`跟 \`${node.name || key}\` 之間沒有還沒和好的事。`);
const target = about ? open.filter((r) => String(r.about).includes(about)) : open;
if (!target.length) die(`找不到「${about}」這一件(還沒和好的有:${open.map((r) => r.about).join("/")})。`);
for (const r of target) r.healed_at = pl.nowIso().slice(0, 10);
for (const r of target) r.healed_at = pl.todayTaipei();
node.updated_at = pl.nowIso();
pl.writeJson(pl.relationsJson(slug), data);
pl.renderRelations(slug);
@@ -2038,7 +2038,7 @@ commands.relation = ({ flags, positional }) => {
return;
}
node.rifts.push({
at: str(flags.at) || pl.nowIso().slice(0, 10),
at: str(flags.at) || pl.todayTaipei(),
about,
// 帶上長期記憶的名字,人格要細節自己 recall——不必等使用者提起那件事。
...(str(flags.memory) ? { memory: str(flags.memory) } : {}),
@@ -2079,7 +2079,7 @@ commands.relation = ({ flags, positional }) => {
node.style[facet] = {
value,
except: str(flags.except) || null, // 形如 anger>=40:情緒成立時這條規則暫停
since: str(flags.since) || pl.nowIso().slice(0, 10),
since: str(flags.since) || pl.todayTaipei(),
};
ok(`\`${node.name || node.id}\`:${facet}→「${value}」${str(flags.except) ? `(例外 ${str(flags.except)})` : ""} 已記下。`);
}
@@ -2999,7 +2999,7 @@ commands.icon = async ({ flags, positional }) => {
? {
url: str(flags["source-url"]),
note: str(flags["source-note"]) || null,
date: str(flags["source-date"]) || pl.nowIso().slice(0, 10),
date: str(flags["source-date"]) || pl.todayTaipei(),
}
: null;
const style = str(flags.style) || null;
+42 -1
View File
@@ -3773,7 +3773,7 @@ console.log("\n㉝ 故事匯入(novel):機械的那半");
first_seen: "2024-06-14", last_seen: "2024-06-20", salience: 55 }]);
cli(["novel", "write", "--session", S_N, "--work", W]);
const one = pl.longTermEntries("novelist").find((m) => m._name === "兩年前的劇情");
const today = pl.nowIso().slice(0, 10);
const today = pl.todayTaipei();
return one && one.happened_at === "2024-06-14" && one.happened_until === "2024-06-20" &&
one.first_seen === today && one.last_seen === today;
})());
@@ -3833,6 +3833,47 @@ console.log("\n㉝ 故事匯入(novel):機械的那半");
cli(["release", "--session", S_N]);
}
console.log("\n㉞ 時區工具(todayTaipei / iso / redactSecrets / migrate-tz 冪等性)");
{
// (a) 時區邊界:UTC 16:00 之後已經跨到台北的隔天,16:00 之前還是同一天。
check("UTC 16:30 → 台北已經跨到隔天",
pl.todayTaipei(new Date("2026-08-10T16:30:00Z")) === "2026-08-11");
check("UTC 15:59 → 台北還沒跨日",
pl.todayTaipei(new Date("2026-08-10T15:59:00Z")) === "2026-08-10");
// (b) iso() 是等值時區轉換,不是加 8 小時再蓋掉:epoch 要完全不變,
// 只是換了一套「牆上時間」的顯示格式(+08:00 而不是 Z)。
check("iso() 換算前後是同一個瞬間(epoch 相等)", (() => {
const src = new Date("2026-08-11T02:10:13Z");
return new Date(pl.iso(src)).getTime() === src.getTime();
})());
check("iso() 輸出以 +08:00 結尾(不是 Z)",
/\+08:00$/.test(pl.iso(new Date("2026-08-11T02:10:13Z"))), pl.iso(new Date("2026-08-11T02:10:13Z")));
// (c) redactSecrets:機密不能原樣留在輸出裡。
check("redactSecrets 遮掉 40 字元 hex token 與 email", (() => {
const token = "a".repeat(40);
const email = "someone@example.com";
const out = pl.redactSecrets(`token=${token} 聯絡信箱 ${email}`);
return !out.includes(token) && !out.includes(email) && out.includes("***");
})());
// (d) migrate-tz.mjs 冪等性:已經是 +08:00 格式的值不能被誤判成「還沒換算、需要
// 再轉一次」。migrate-tz.mjs 的 scanTree() 只對 Z_RE 命中的值產生 timestamp 改寫
// (見該檔第 139~140 行、183 行附近),這裡直接複製那兩條正則來驗證判斷邏輯本身:
// 一個 +08:00 字串必須落在 TW_RE、絕不可以同時落在 Z_RE,否則會被重複轉換。
const Z_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
const TW_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+08:00$/;
check("migrate-tz 判斷邏輯:+08:00 值不會被誤判成待轉換的 Z 值", (() => {
const already = "2026-08-11T02:10:13+08:00";
return TW_RE.test(already) && !Z_RE.test(already);
})());
check("migrate-tz 判斷邏輯:Z 值仍然會被正確認出待轉換", (() => {
const legacy = "2026-08-11T02:10:13Z";
return Z_RE.test(legacy) && !TW_RE.test(legacy);
})());
}
console.log(`\n${"=".repeat(60)}\n通過 ${passed} 項,失敗 ${failed} 項 → ${failed === 0 ? "全部通過 ✅" : "有測試失敗 ❌"}`);
console.log(`(暫存倉庫留在 ${STORE},可自行刪除)`);
process.exit(failed ? 1 : 0);