mirror of
https://github.com/dtzp555-max/memory-continuity.git
synced 2026-07-19 09:42:42 +00:00
fix: session log token estimation handles array content, fix TOCTOU race
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -210,9 +210,13 @@ function writeSessionLog(workspaceDir, messages, config = {}) {
|
|||||||
// Count messages by role
|
// Count messages by role
|
||||||
const userCount = messages.filter(m => m?.role === "user").length;
|
const userCount = messages.filter(m => m?.role === "user").length;
|
||||||
const assistantCount = messages.filter(m => m?.role === "assistant").length;
|
const assistantCount = messages.filter(m => m?.role === "assistant").length;
|
||||||
const totalTokens = estimateTokens(
|
const contentToString = (m) => {
|
||||||
messages.map(m => typeof m?.content === "string" ? m.content : "").join("")
|
if (typeof m?.content === "string") return m.content;
|
||||||
);
|
if (Array.isArray(m?.content))
|
||||||
|
return m.content.filter(b => b?.type === "text").map(b => b.text).join(" ");
|
||||||
|
return "";
|
||||||
|
};
|
||||||
|
const totalTokens = estimateTokens(messages.map(contentToString).join(" "));
|
||||||
|
|
||||||
// Build log entry
|
// Build log entry
|
||||||
const entry = [
|
const entry = [
|
||||||
@@ -223,13 +227,10 @@ function writeSessionLog(workspaceDir, messages, config = {}) {
|
|||||||
"",
|
"",
|
||||||
].join("\n");
|
].join("\n");
|
||||||
|
|
||||||
// Append to daily log (create with header if new)
|
// Append-safe: use appendFileSync for both new and existing files
|
||||||
if (!fs.existsSync(logFile)) {
|
const isNew = !fs.existsSync(logFile);
|
||||||
const header = `# Session Log — ${dateStr}\n\n`;
|
const prefix = isNew ? `# Session Log — ${dateStr}\n\n` : "";
|
||||||
fs.writeFileSync(logFile, header + entry, "utf8");
|
fs.appendFileSync(logFile, prefix + entry, "utf8");
|
||||||
} else {
|
|
||||||
fs.appendFileSync(logFile, entry, "utf8");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractStateFromMessages(messages) {
|
function extractStateFromMessages(messages) {
|
||||||
|
|||||||
Reference in New Issue
Block a user