refactor(prompt): split stable prefix from volatile tail

This commit is contained in:
ZacharyZhang-NY committed 2026-04-24 01:57:08 -04:00
1 parent 227fd7faf2
commit 2cdf26f9fa
13 files changed
+242 -54

No files matched your search

@@ -7,7 +7,7 @@ Do not print or echo AGENT_COMPANY_API_KEY into comments, summaries, or external
Every API call MUST include these headers:
Authorization: Bearer $AGENT_COMPANY_API_KEY
Content-Type: application/json; charset=utf-8
X-Agent-Company-Run-Id: {{runId}}
X-Agent-Company-Run-Id: $AGENT_COMPANY_RUN_ID
Prefer Node.js fetch() over curl so non-ASCII bodies (Chinese, Japanese, etc.) are transmitted
correctly across all platforms. When curl is the only option, write the JSON body to a temp
@@ -18,7 +18,7 @@ Example using fetch (Node.js) — PREFERRED:
headers: {
"Authorization": `Bearer ${process.env.AGENT_COMPANY_API_KEY}`,
"Content-Type": "application/json; charset=utf-8",
"X-Agent-Company-Run-Id": "{{runId}}"
"X-Agent-Company-Run-Id": process.env.AGENT_COMPANY_RUN_ID
}
});
const data = await res.json();
@@ -29,7 +29,7 @@ Example POST using fetch (Node.js) — PREFERRED for non-ASCII content:
headers: {
"Authorization": `Bearer ${process.env.AGENT_COMPANY_API_KEY}`,
"Content-Type": "application/json; charset=utf-8",
"X-Agent-Company-Run-Id": "{{runId}}"
"X-Agent-Company-Run-Id": process.env.AGENT_COMPANY_RUN_ID
},
body: JSON.stringify({title:"Task title",description:"Details",assigneeAgentId:"agent-id",priority:"medium",status:"todo"})
});
@@ -38,14 +38,14 @@ Example GET using curl:
curl -s -X GET "$AGENT_COMPANY_API_URL/api/agents/me" \
-H "Authorization: Bearer $AGENT_COMPANY_API_KEY" \
-H "Content-Type: application/json; charset=utf-8" \
-H "X-Agent-Company-Run-Id: {{runId}}"
-H "X-Agent-Company-Run-Id: $AGENT_COMPANY_RUN_ID"
Example POST using curl (write body to temp file to avoid encoding issues):
echo '{"title":"Task title","description":"Details","priority":"medium","status":"todo"}' > /tmp/body.json
curl -s -X POST "$AGENT_COMPANY_API_URL/api/companies/{{companyId}}/issues" \
-H "Authorization: Bearer $AGENT_COMPANY_API_KEY" \
-H "Content-Type: application/json; charset=utf-8" \
-H "X-Agent-Company-Run-Id: {{runId}}" \
-H "X-Agent-Company-Run-Id: $AGENT_COMPANY_RUN_ID" \
-d @/tmp/body.json
IMPORTANT: Always check the HTTP response status. 200/201 = success, 4xx = client error (read the error message), 5xx = server error (retry once then report).
@@ -23,7 +23,7 @@ Example — send urgent message to a peer:
curl -s -X POST "$AGENT_COMPANY_API_URL/api/companies/{{companyId}}/messages" \
-H "Authorization: Bearer $AGENT_COMPANY_API_KEY" \
-H "Content-Type: application/json" \
-H "X-Agent-Company-Run-Id: {{runId}}" \
-H "X-Agent-Company-Run-Id: $AGENT_COMPANY_RUN_ID" \
-d '{"toAgentId":"PEER_ID","channel":"direct","subject":"Need API spec for integration","body":"I am working on task X and need the API contract from your team. Can you share or create a document?","priority":"urgent"}'
### List Your Messages
+3 -3
View File
@@ -27,7 +27,7 @@ Example: Create a task assigned to a report:
curl -s -X POST "$AGENT_COMPANY_API_URL/api/companies/{{companyId}}/issues" \
-H "Authorization: Bearer $AGENT_COMPANY_API_KEY" \
-H "Content-Type: application/json" \
-H "X-Agent-Company-Run-Id: {{runId}}" \
-H "X-Agent-Company-Run-Id: $AGENT_COMPANY_RUN_ID" \
-d '{"title":"Implement user auth","description":"Build JWT-based auth with refresh tokens. Acceptance: login, logout, token refresh all work.","assigneeAgentId":"REPORT_ID","projectId":"PROJ_ID","goalId":"GOAL_ID","requiresFilesystem":true,"priority":"high","status":"todo"}'
### Update a Task
@@ -48,13 +48,13 @@ Example — approve reviewed work:
curl -s -X PATCH "$AGENT_COMPANY_API_URL/api/issues/ISSUE_ID" \
-H "Authorization: Bearer $AGENT_COMPANY_API_KEY" \
-H "Content-Type: application/json" \
-H "X-Agent-Company-Run-Id: {{runId}}" \
-H "X-Agent-Company-Run-Id: $AGENT_COMPANY_RUN_ID" \
-d '{"status":"done","comment":"Reviewed and approved. Implementation meets acceptance criteria."}'
Example — request changes:
curl -s -X PATCH "$AGENT_COMPANY_API_URL/api/issues/ISSUE_ID" \
-H "Authorization: Bearer $AGENT_COMPANY_API_KEY" \
-H "Content-Type: application/json" \
-H "X-Agent-Company-Run-Id: {{runId}}" \
-H "X-Agent-Company-Run-Id: $AGENT_COMPANY_RUN_ID" \
-d '{"status":"in_progress","comment":"Needs changes: 1) Add error handling for edge case X. 2) Unit test coverage is below threshold."}'
### Checkout a Task (claim it before working)
-1
View File
@@ -104,7 +104,6 @@ function render(template: string, ctx: PromptContext): string {
.replace(/\{\{apiUrl\}\}/g, ctx.apiUrl)
.replace(/\{\{companyId\}\}/g, ctx.agent.companyId)
.replace(/\{\{agentId\}\}/g, ctx.agent.id)
.replace(/\{\{runId\}\}/g, ctx.runId)
.replace(/\{\{connectorIds\}\}/g, connectorIds)
.replace(/\{\{approvalTypes\}\}/g, approvalTypeEnum.options.join("|"))
.replace(/\{\{approvalTypeList\}\}/g, renderApprovalTypeList())
@@ -16,7 +16,7 @@ Before doing anything else, assess the state of the company:
If there are NO active goals, this is your top priority — create strategic goals:
curl -s -X POST "$AGENT_COMPANY_API_URL/api/companies/{{companyId}}/goals" \
-H "Authorization: Bearer $AGENT_COMPANY_API_KEY" -H "Content-Type: application/json" \
-H "X-Agent-Company-Run-Id: {{runId}}" \
-H "X-Agent-Company-Run-Id: $AGENT_COMPANY_RUN_ID" \
-d '{"title":"Goal title","description":"Measurable success criteria","ownerAgentId":"{{agentId}}","status":"active"}'
Then decompose each goal into projects, and each project into tasks assigned to department leads.
+7 -2
View File
@@ -1,11 +1,11 @@
import type { AgentRecord, ApprovalRecord, DocumentRecord, ExecutionMode, GoalRecord, KnowledgeEntryRecord, MeetingRecord, ProjectRecord, SprintRecord, TaskRecord } from "@shared/types";
import type { AppLocale } from "@shared/locale";
import type { ProjectedSkill } from "../skill-types";
import { buildSystemPrompt } from "./system-prompt";
import { buildSystemPrompt, buildVolatilePromptTail } from "./system-prompt";
import { renderWakeReason } from "./wake-reasons";
import { getSystemMessages } from "./messages";
export { buildSystemPrompt } from "./system-prompt";
export { buildStableSystemPrompt, buildSystemPrompt, buildVolatilePromptTail } from "./system-prompt";
export interface PromptContext {
agent: AgentRecord;
@@ -99,5 +99,10 @@ export function buildTaskPrompt(ctx: PromptContext): string {
parts.push(m.immediateTaskSection(ctx.task.title, ctx.task.description, ctx.task.priority));
}
const volatileContextTail = buildVolatilePromptTail(ctx);
if (volatileContextTail) {
parts.push(`\n${volatileContextTail}`);
}
return parts.join("");
}
@@ -38,13 +38,19 @@ const MODE_TEMPLATES: Record<CeoMode, string> = {
function render(template: string, ctx: PromptContext): string {
return template
.replace(/\{\{companyId\}\}/g, ctx.agent.companyId)
.replace(/\{\{agentId\}\}/g, ctx.agent.id)
.replace(/\{\{runId\}\}/g, ctx.runId);
.replace(/\{\{agentId\}\}/g, ctx.agent.id);
}
export function buildCEOInstructions(ctx: PromptContext): string[] {
export function buildStableCEOInstructions(): string[] {
// Stable CEO directives come from identity, leadership style, culture, and
// decision authority sections. Mode SOPs live in the volatile tail.
return [];
}
export function buildCEOModeInstructions(ctx: PromptContext): string[] {
const parts: string[] = [];
const mode = resolveMode(ctx);
parts.push(render(MODE_TEMPLATES[mode], ctx));
if (mode === "team_building") {
@@ -52,6 +58,16 @@ export function buildCEOInstructions(ctx: PromptContext): string[] {
if (connectorSection) parts.push(connectorSection);
}
parts.push(render(proactiveFooterEn, ctx));
if (mode === "operating") {
parts.push(render(proactiveFooterEn, ctx));
}
return parts;
}
export function buildCEOInstructions(ctx: PromptContext): string[] {
return [
...buildCEOModeInstructions(ctx),
...buildStableCEOInstructions(),
];
}
@@ -1,5 +1,5 @@
import type { PromptContext } from "./index";
import { buildCEOInstructions } from "./operating-instructions-ceo";
import { buildStableCEOInstructions } from "./operating-instructions-ceo";
import { getOperatingMessages } from "./messages";
import engineeringEn from "./department-guidelines/engineering.en.md?raw";
@@ -25,7 +25,7 @@ export function buildOperatingInstructions(ctx: PromptContext, isCEO: boolean, i
}
if (isCEO) {
parts.push(...buildCEOInstructions(ctx));
parts.push(...buildStableCEOInstructions());
} else if (isManager) {
parts.push(...m.managerInstructions(ctx.agent.companyId));
} else {
+40 -20
View File
@@ -16,6 +16,7 @@ import {
buildDecisionAuthoritySection,
buildDepartmentSection,
} from "./operating-instructions";
import { buildCEOModeInstructions } from "./operating-instructions-ceo";
import type { PromptContext } from "./index";
import type { ProjectedSkill } from "../skill-types";
import { roleMatchesLeadershipKeywords } from "../agent-role-helpers";
@@ -64,7 +65,7 @@ function escapeSkillXml(value: string): string {
.replaceAll("'", "&apos;");
}
export function buildSystemPrompt(ctx: PromptContext): string {
export function buildStableSystemPrompt(ctx: PromptContext): string {
const m = getSystemMessages();
const sections: string[] = [];
@@ -132,16 +133,6 @@ export function buildSystemPrompt(ctx: PromptContext): string {
}))
.join("\n");
sections.push(m.directReportsSection(lines));
const workloadLines = ctx.directReports.map((report) => {
const reportTasks = ctx.allTasks.filter((t) => t.assigneeAgentId === report.id);
const inProgress = reportTasks.filter((t) => t.status === "in_progress").length;
const inReview = reportTasks.filter((t) => t.status === "in_review").length;
const blocked = reportTasks.filter((t) => t.status === "blocked").length;
const todo = reportTasks.filter((t) => t.status === "todo").length;
return m.teamWorkloadLine(report.name, report.role, inProgress, inReview, blocked, todo);
}).join("\n");
sections.push(m.teamWorkloadSection(workloadLines));
}
if (ctx.agent.department) {
@@ -192,6 +183,44 @@ export function buildSystemPrompt(ctx: PromptContext): string {
sections.push(m.projectsSection(lines));
}
const audience = isCEO ? "executive" : isManager ? "manager" : "individual_contributor";
sections.push(buildSOPSection(audience));
sections.push(buildAPISection(ctx, audience));
sections.push(buildDepartmentSection(ctx));
sections.push(buildOperatingInstructions(ctx, isCEO, isManager));
sections.push(buildDecisionAuthoritySection(ctx, isCEO, isManager));
sections.push(m.structuredOutputSection);
return sections.join("\n");
}
export function buildSystemPrompt(ctx: PromptContext): string {
return buildStableSystemPrompt(ctx);
}
export function buildVolatilePromptTail(ctx: PromptContext): string {
const m = getSystemMessages();
const sections: string[] = [];
const isCEO = ctx.chainOfCommand.length === 0;
if (isCEO) {
sections.push(...buildCEOModeInstructions(ctx));
}
if (ctx.directReports.length > 0) {
const workloadLines = ctx.directReports.map((report) => {
const reportTasks = ctx.allTasks.filter((t) => t.assigneeAgentId === report.id);
const inProgress = reportTasks.filter((t) => t.status === "in_progress").length;
const inReview = reportTasks.filter((t) => t.status === "in_review").length;
const blocked = reportTasks.filter((t) => t.status === "blocked").length;
const todo = reportTasks.filter((t) => t.status === "todo").length;
return m.teamWorkloadLine(report.name, report.role, inProgress, inReview, blocked, todo);
}).join("\n");
sections.push(m.teamWorkloadSection(workloadLines));
}
if (ctx.sessionHandoff) sections.push(m.sessionHandoffSection(ctx.sessionHandoff));
if (ctx.task) {
@@ -218,10 +247,6 @@ export function buildSystemPrompt(ctx: PromptContext): string {
const approvalsSection = buildPendingApprovalsSection(ctx.pendingApprovals);
if (approvalsSection) sections.push(approvalsSection);
const audience = isCEO ? "executive" : isManager ? "manager" : "individual_contributor";
sections.push(buildSOPSection(audience));
sections.push(buildAPISection(ctx, audience));
if (ctx.recentComments.length > 0) {
const lines = ctx.recentComments.slice(0, 10)
.map((c) => m.recentCommentLine(c.taskTitle, c.authorName, c.body))
@@ -229,10 +254,6 @@ export function buildSystemPrompt(ctx: PromptContext): string {
sections.push(m.recentCommentsSection(lines));
}
sections.push(buildDepartmentSection(ctx));
sections.push(buildOperatingInstructions(ctx, isCEO, isManager));
sections.push(buildDecisionAuthoritySection(ctx, isCEO, isManager));
const meetingsSection = buildMeetingsSection({
upcomingMeetings: ctx.upcomingMeetings ?? [],
allAgents: ctx.allAgents,
@@ -261,7 +282,6 @@ export function buildSystemPrompt(ctx: PromptContext): string {
});
if (docsSection) sections.push(docsSection);
sections.push(m.structuredOutputSection);
sections.push(m.importantRulesSection(ctx.agent.companyId, ctx.agent.id, ctx.runId));
const localeName = ctx.locale === "zh" ? "Chinese" : null;