| #!/usr/bin/env node
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 'use strict';
|
| const fs = require('fs');
|
| const path = require('path');
|
| const os = require('os');
|
| const emit = require('./lib/emit');
|
| const cfg = require('./lib/config');
|
| const sessionLock = require('./lib/session-lock');
|
| const messages = require('./lib/messages');
|
|
|
| const STALE_THRESHOLD_BYTES = 9000;
|
| const STATE_FILE_PREFIX = 'nomad_stop_';
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| function newestMtimeMs(dirPath) {
|
| let newest = 0;
|
| function walk(dir) {
|
| let entries;
|
| try {
|
| entries = fs.readdirSync(dir, { withFileTypes: true });
|
| } catch (e) {
|
| return;
|
| }
|
| for (const ent of entries) {
|
| const full = path.join(dir, ent.name);
|
| try {
|
| if (ent.isDirectory()) {
|
| walk(full);
|
| } else if (ent.isFile()) {
|
| const mt = Math.floor(fs.statSync(full).mtimeMs);
|
| if (mt > newest) newest = mt;
|
| }
|
| } catch (e) {
|
|
|
| }
|
| }
|
| }
|
| try {
|
| if (fs.existsSync(dirPath)) walk(dirPath);
|
| } catch (e) {
|
|
|
| }
|
| return newest;
|
| }
|
|
|
| function fileSizeBytes(filePath) {
|
| try {
|
| if (filePath && fs.existsSync(filePath)) return fs.statSync(filePath).size;
|
| } catch (e) {
|
|
|
| }
|
| return 0;
|
| }
|
|
|
| |
| |
| |
| |
| |
| |
|
|
| function decide(prevState, tlen, memNewest) {
|
| if (!prevState.exists) {
|
| return { remind: true, newT: tlen, newM: memNewest };
|
| }
|
| const lastT = prevState.lastT || 0;
|
| const lastM = prevState.lastM || 0;
|
| if (memNewest > lastM) {
|
| return { remind: false, newT: tlen, newM: memNewest };
|
| }
|
| if (tlen - lastT > STALE_THRESHOLD_BYTES) {
|
| return { remind: true, newT: tlen, newM: memNewest };
|
| }
|
| return { remind: false, newT: lastT, newM: lastM };
|
| }
|
|
|
| function readPrevState(stateFile) {
|
| if (!fs.existsSync(stateFile)) return { exists: false, lastT: 0, lastM: 0 };
|
| try {
|
| const raw = fs.readFileSync(stateFile, 'utf8');
|
| const parts = raw.split('|');
|
| const lastT = parts.length >= 1 ? parseInt(parts[0], 10) : NaN;
|
| const lastM = parts.length >= 2 ? parseInt(parts[1], 10) : NaN;
|
| return { exists: true, lastT: Number.isFinite(lastT) ? lastT : 0, lastM: Number.isFinite(lastM) ? lastM : 0 };
|
| } catch (e) {
|
| return { exists: false, lastT: 0, lastM: 0 };
|
| }
|
| }
|
|
|
| function writeState(stateFile, newT, newM) {
|
| fs.writeFileSync(stateFile, String(newT) + '|' + String(newM), 'utf8');
|
| }
|
|
|
|
|
| function buildAppendLogHint(teamRoot, config) {
|
| const appendLogScript = path.join(__dirname, 'append-log.js');
|
| const teamLogPath = cfg.resolveSharedPath(teamRoot, config, 'team_log');
|
| const target = teamLogPath || '<team_log path from team-config.json shared_paths.team_log>';
|
| return 'node "' + appendLogScript + '" "- <your entry>" --target "' + target + '"';
|
| }
|
|
|
| async function main() {
|
| const platform = emit.getPlatform();
|
| try {
|
| const raw = await emit.readStdin();
|
| let j = {};
|
| try {
|
| j = raw && raw.trim() ? JSON.parse(raw) : {};
|
| } catch (e) {
|
| j = {};
|
| }
|
| const sid = typeof j.session_id === 'string' ? j.session_id : '';
|
| if (!sid) {
|
| process.exit(0);
|
| return;
|
| }
|
|
|
| if (emit.isDuplicateInvocation('Stop', sid)) {
|
| process.exit(0);
|
| return;
|
| }
|
|
|
| const cwd = emit.resolveCwd(raw, platform);
|
| const tp = typeof j.transcript_path === 'string' ? j.transcript_path : '';
|
|
|
| const agentHome = cfg.resolveAgentHome(cwd);
|
| const memDir = agentHome ? cfg.firstExisting(agentHome, ['记忆', 'memory']) : null;
|
| const memNewest = memDir ? newestMtimeMs(memDir) : 0;
|
| const tlen = fileSizeBytes(tp);
|
|
|
| const key = sid.replace(/[^A-Za-z0-9]/g, '');
|
| const stateFile = path.join(os.tmpdir(), STATE_FILE_PREFIX + key + '.txt');
|
|
|
| const prevState = readPrevState(stateFile);
|
| const result = decide(prevState, tlen, memNewest);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| try {
|
| writeState(stateFile, result.newT, result.newM);
|
| } catch (e) {
|
| process.stderr.write('[session-stop] could not persist reminder state (non-fatal, ' +
|
| 'lock release continues): ' + ((e && e.message) || e) + '\n');
|
| }
|
|
|
| if (result.remind) {
|
| const teamRoot = cfg.resolveTeamRoot(cwd || process.cwd());
|
| const config = cfg.loadTeamConfig(teamRoot);
|
| const lang = cfg.resolveLanguage(config);
|
| const msg = messages.get(lang);
|
| const reason = msg.stopReason(buildAppendLogHint(teamRoot, config));
|
| emit.writeJson(emit.buildStopBlockPayload(reason, platform));
|
| }
|
|
|
|
|
| if (agentHome) {
|
| try {
|
| sessionLock.release(agentHome);
|
| } catch (e) {
|
|
|
| }
|
| }
|
| } catch (e) {
|
|
|
|
|
|
|
|
|
| process.stderr.write('[session-stop] non-fatal error, no reminder emitted (close-out not blocked): ' + ((e && e.stack) || e) + '\n');
|
| }
|
| process.exit(0);
|
| }
|
|
|
| if (require.main === module) {
|
| main();
|
| }
|
|
|
| module.exports = {
|
| decide,
|
| newestMtimeMs,
|
| fileSizeBytes,
|
| readPrevState,
|
| writeState,
|
| buildAppendLogHint,
|
| STALE_THRESHOLD_BYTES,
|
| };
|
|
|