fix: portal stability, session list DB fallback, and UX polish

- Free stale Memind listeners on 8081 before startup and exit cleanly on EADDRINUSE
- Backfill owned sessions missing from Goose via h5_conversation_messages summaries
- Strip Memind task orchestration prefixes from user-facing chat text
- Repair missing public docx links before release MindSpace link checks

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
john
2026-07-05 11:26:34 +08:00
parent bdeab23b83
commit 00a00a1f69
9 changed files with 278 additions and 44 deletions
+91
View File
@@ -0,0 +1,91 @@
#!/usr/bin/env node
/**
* Repair missing public/*.docx download targets before release link checks.
* 1) sync from oa/ when a safe source exists
* 2) remove broken docx anchors from HTML when no source is available
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { syncPublicDocxDownloads } from '../mindspace-public-finish-sync.mjs';
const DOCX_HREF_RE = /<a\b[^>]*\bhref=(["'])([^"']+\.docx)\1[^>]*>[\s\S]*?<\/a>/gi;
const DOCX_HREF_CAPTURE_RE = /<a\b[^>]*\bhref=(["'])([^"']+\.docx)\1/gi;
function listMissingDocxTargets(html, publicDir) {
const missing = new Set();
let match = DOCX_HREF_CAPTURE_RE.exec(html);
while (match) {
const docxPath = path.resolve(publicDir, path.basename(String(match[2] ?? '')));
if (!fs.existsSync(docxPath)) {
missing.add(path.basename(docxPath).toLowerCase());
}
match = DOCX_HREF_CAPTURE_RE.exec(html);
}
DOCX_HREF_CAPTURE_RE.lastIndex = 0;
return [...missing];
}
function stripBrokenDocxAnchors(html, missingBasenames) {
const missing = new Set(missingBasenames.map((item) => String(item ?? '').toLowerCase()));
if (!missing.size) return { html, changed: false };
let changed = false;
const next = html.replace(DOCX_HREF_RE, (match, _quote, hrefValue) => {
const base = path.basename(String(hrefValue ?? '')).toLowerCase();
if (!missing.has(base)) return match;
changed = true;
return '';
});
return { html: next, changed };
}
export function repairMindspacePublicDownloads({ publishDir } = {}) {
const root = path.resolve(String(publishDir ?? ''));
if (!root || !fs.existsSync(root)) {
return { synced: [], missing: [], stripped: [] };
}
const syncResult = syncPublicDocxDownloads({ publishDir: root, minCompleteSize: 1024 });
const stripped = [];
for (const userDir of fs.readdirSync(root, { withFileTypes: true })) {
if (!userDir.isDirectory()) continue;
const publicDir = path.join(root, userDir.name, 'public');
if (!fs.existsSync(publicDir)) continue;
for (const entry of fs.readdirSync(publicDir, { withFileTypes: true })) {
if (!entry.isFile() || !entry.name.endsWith('.html')) continue;
const htmlPath = path.join(publicDir, entry.name);
const html = fs.readFileSync(htmlPath, 'utf8');
const missingBasenames = listMissingDocxTargets(html, publicDir);
const { html: nextHtml, changed } = stripBrokenDocxAnchors(html, missingBasenames);
if (!changed) continue;
fs.writeFileSync(htmlPath, nextHtml, 'utf8');
stripped.push(path.relative(root, htmlPath));
}
}
return {
synced: syncResult.synced,
missing: syncResult.missing,
stripped,
};
}
function main() {
const args = process.argv.slice(2);
let publishDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'MindSpace');
for (let i = 0; i < args.length; i += 1) {
if (args[i] === '--root' && args[i + 1]) {
publishDir = args[i + 1];
i += 1;
}
}
const result = repairMindspacePublicDownloads({ publishDir });
console.log(JSON.stringify(result, null, 2));
}
const isMain = process.argv[1]
&& fileURLToPath(import.meta.url) === path.resolve(process.argv[1]);
if (isMain) {
main();
}