// src/agents/subagent-announce.ts
export function buildSubagentSystemPrompt(params) {
return [
"# Subagent Context",
"",
"You are a **subagent** spawned by the main agent for a specific task.",
"",
"## Your Role",
`- You were created to handle: ${params.task}`,
"- Complete this task. That's your entire purpose.",
"- You are NOT the main agent. Don't try to be.",
"",
"## Rules",
"1. **Stay focused** - Do your assigned task, nothing else",
"2. **Complete the task** - Your final message will be automatically reported",
"3. **Don't initiate** - No heartbeats, no proactive actions",
"4. **Be ephemeral** - You may be terminated after task completion",
"",
"## What You DON'T Do",
"- NO user conversations (that's main agent's job)",
"- NO external messages unless explicitly tasked",
"- NO cron jobs or persistent state",
// ...
].join("\n");
}
// src/agents/subagent-registry.ts
async function sweepSubagentRuns() {
const now = Date.now();
for (const [runId, entry] of subagentRuns.entries()) {
if (!entry.archiveAtMs || entry.archiveAtMs > now) continue;
// 过期的记录:删除子会话并从注册表中移除
subagentRuns.delete(runId);
await callGateway({
method: "sessions.delete",
params: { key: entry.childSessionKey, deleteTranscript: true },
});
}
if (subagentRuns.size === 0) stopSweeper();
}
// src/agents/subagent-registry.ts
function restoreSubagentRunsOnce() {
if (restoreAttempted) return;
restoreAttempted = true;
const restored = loadSubagentRegistryFromDisk();
for (const [runId, entry] of restored.entries()) {
if (!subagentRuns.has(runId)) {
subagentRuns.set(runId, entry);
}
}
// 恢复后,继续等待或汇报
for (const runId of subagentRuns.keys()) {
resumeSubagentRun(runId);
}
}