@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
110 lines • 5 kB
JavaScript
/**
* Environment context (working directory + current date) delivered to the
* model as an append-only, hidden user-role custom message instead of as
* system prompt text (senpi#2093).
*
* A date or cwd line inside the system prompt rewrites the provider-visible
* prefix every day and in every directory, so automatic prefix caches
* (OpenAI, Anthropic, and the rest) miss the whole system prompt and
* everything after it. Carrying the values in a message placed before the
* next user turn keeps the system prompt byte-stable; a new message is
* appended only when a value changes, and earlier ones are never rewritten.
*
* On the wire the message is the leading content block of the user message it
* precedes (senpi#2118), so providers whose chat templates require strict
* user/assistant alternation never see two consecutive user messages.
*/
export const ENVIRONMENT_CONTEXT_MESSAGE_TYPE = "environment-context";
/**
* Test-only date pin (senpi#4660): `SENPI_TEST_CURRENT_DATE=YYYY-MM-DD` replaces the clock's date so a harness
* driving the real CLI gets the same prompt bytes on every day. Unset (or empty) in every production launch.
*/
const CURRENT_DATE_ENV = "SENPI_TEST_CURRENT_DATE";
function pinnedCurrentDate() {
const value = process.env[CURRENT_DATE_ENV];
if (value === undefined || value === "")
return undefined;
// An out-of-range month or day parses to an invalid Date, whose toISOString() throws RangeError; check validity first.
const parsed = new Date(`${value}T00:00:00.000Z`);
const isCalendarDate = /^\d{4}-\d{2}-\d{2}$/.test(value) && !Number.isNaN(parsed.getTime()) && parsed.toISOString().startsWith(value);
if (!isCalendarDate)
throw new Error(`${CURRENT_DATE_ENV} must be a calendar date YYYY-MM-DD, got ${JSON.stringify(value)}`);
return value;
}
export function resolveEnvironmentContext(cwd, now = new Date()) {
return { cwd: cwd.replace(/\\/g, "/"), currentDate: pinnedCurrentDate() ?? now.toISOString().slice(0, 10) };
}
export function formatEnvironmentContext(context) {
return [
"<environment_context>",
` <cwd>${context.cwd}</cwd>`,
` <current_date>${context.currentDate}</current_date>`,
"</environment_context>",
].join("\n");
}
function isEnvironmentContext(value) {
if (typeof value !== "object" || value === null)
return false;
const candidate = value;
return typeof candidate.cwd === "string" && typeof candidate.currentDate === "string";
}
export function latestEnvironmentContext(messages) {
for (let index = messages.length - 1; index >= 0; index--) {
const message = messages[index];
if (message.role === "custom" &&
message.customType === ENVIRONMENT_CONTEXT_MESSAGE_TYPE &&
isEnvironmentContext(message.details)) {
return message.details;
}
}
return undefined;
}
export function createEnvironmentContextMessage(context, timestamp = Date.now()) {
return {
role: "custom",
customType: ENVIRONMENT_CONTEXT_MESSAGE_TYPE,
content: formatEnvironmentContext(context),
display: false,
details: { cwd: context.cwd, currentDate: context.currentDate },
timestamp,
};
}
/**
* A new environment context message when `current` differs from the latest
* one in `messages` (or none is visible, e.g. a fresh session or a compaction
* that summarized it away); `undefined` when the model already sees it.
*/
export function environmentContextMessageIfChanged(messages, current) {
const latest = latestEnvironmentContext(messages);
if (latest?.cwd === current.cwd && latest.currentDate === current.currentDate)
return undefined;
return createEnvironmentContextMessage(current);
}
function contentBlocks(content) {
return typeof content === "string" ? [{ type: "text", text: content }] : content;
}
/**
* Merge each converted environment-context message into the user message that
* immediately follows it, as that message's leading content block(s). One that
* no user message follows stays a standalone user message. Conversion runs on
* the persisted entries, so a resumed session produces the same bytes and a
* sent user message never changes on later requests.
*/
export function foldEnvironmentContextIntoNextUserMessage(messages, environmentMessages) {
const folded = [];
for (let index = 0; index < messages.length; index++) {
const message = messages[index];
const next = messages[index + 1];
if (environmentMessages.has(message) &&
message.role === "user" &&
next?.role === "user" &&
!environmentMessages.has(next)) {
folded.push({ ...next, content: [...contentBlocks(message.content), ...contentBlocks(next.content)] });
index++;
continue;
}
folded.push(message);
}
return folded;
}
//# sourceMappingURL=environment-context.js.map