

























@@ -127,6 +127,16 @@ function makeCliResult(text: string): EmbeddedAgentRunResult {
127127};
128128}
129129130+async function persistCliTranscriptEntry(
131+params: Parameters<typeof persistCliTurnTranscript>[0],
132+): Promise<SessionEntry | undefined> {
133+const result = await persistCliTurnTranscript(params);
134+if (result.kind !== "persisted") {
135+throw new Error("expected CLI transcript persistence to keep the current session");
136+}
137+return result.sessionEntry;
138+}
139+130140async function readSessionMessages(sessionFile: string) {
131141return (await readSessionFileJsonLines<{ type?: string; message?: unknown }>(sessionFile))
132142.filter((entry) => entry.type === "message")
@@ -1143,7 +1153,7 @@ describe("CLI attempt execution", () => {
11431153});
11441154let updatedEntry: SessionEntry | undefined;
11451155try {
1146-updatedEntry = await persistCliTurnTranscript({
1156+updatedEntry = await persistCliTranscriptEntry({
11471157body: "persist this",
11481158result: makeCliResult("hello from cli"),
11491159sessionId: sessionEntry.sessionId,
@@ -1204,6 +1214,40 @@ describe("CLI attempt execution", () => {
12041214expect(sessionStore[sessionKey]?.updatedAt).toBe(persisted[sessionKey]?.updatedAt);
12051215});
120612161217+it("does not append a CLI transcript after the session is deleted", async () => {
1218+const sessionKey = "agent:main:subagent:cli-transcript-deleted";
1219+const staleSessionFile = path.join(tmpDir, "session-cli-stale.jsonl");
1220+const staleEntry: SessionEntry = {
1221+sessionId: "session-cli-stale",
1222+sessionFile: staleSessionFile,
1223+updatedAt: 1,
1224+};
1225+const sessionStore: Record<string, SessionEntry> = { [sessionKey]: staleEntry };
1226+await fs.writeFile(storePath, JSON.stringify({}, null, 2), "utf-8");
1227+clearSessionStoreCacheForTest();
1228+1229+const result = await persistCliTurnTranscript({
1230+body: "late prompt",
1231+result: makeCliResult("late reply"),
1232+sessionId: staleEntry.sessionId,
1233+ sessionKey,
1234+sessionEntry: staleEntry,
1235+ sessionStore,
1236+ storePath,
1237+sessionAgentId: "main",
1238+sessionCwd: tmpDir,
1239+config: {},
1240+});
1241+1242+expect(result).toEqual({ kind: "session-rebound", sessionEntry: undefined });
1243+await expect(fs.stat(staleSessionFile)).rejects.toMatchObject({ code: "ENOENT" });
1244+const persisted = JSON.parse(await fs.readFile(storePath, "utf-8")) as Record<
1245+string,
1246+SessionEntry
1247+>;
1248+expect(persisted[sessionKey]).toBeUndefined();
1249+});
1250+12071251it("embedded assistant gap-fill skips user mirror and dedupes identical assistant tails", async () => {
12081252const sessionKey = "agent:main:subagent:embedded-gap-fill";
12091253const sessionEntry: SessionEntry = {
@@ -1221,7 +1265,7 @@ describe("CLI attempt execution", () => {
12211265runner: "embedded",
12221266};
122312671224-const updatedFirst = await persistCliTurnTranscript({
1268+const updatedFirst = await persistCliTranscriptEntry({
12251269body: "ignored for gap fill",
12261270transcriptBody: "also ignored",
12271271 result,
@@ -1278,7 +1322,7 @@ describe("CLI attempt execution", () => {
12781322runner: "embedded",
12791323};
128013241281-const updatedFirst = await persistCliTurnTranscript({
1325+const updatedFirst = await persistCliTranscriptEntry({
12821326body: "ignored for gap fill",
12831327 result,
12841328sessionId: sessionEntry.sessionId,
@@ -1344,7 +1388,7 @@ describe("CLI attempt execution", () => {
13441388runner: "embedded",
13451389};
134613901347-const updatedFirst = await persistCliTurnTranscript({
1391+const updatedFirst = await persistCliTranscriptEntry({
13481392body: "ignored for gap fill",
13491393 result,
13501394sessionId: sessionEntry.sessionId,
@@ -1415,7 +1459,7 @@ describe("CLI attempt execution", () => {
14151459runner: "embedded",
14161460};
141714611418-const updatedFirst = await persistCliTurnTranscript({
1462+const updatedFirst = await persistCliTranscriptEntry({
14191463body: "ignored for gap fill",
14201464 result,
14211465sessionId: sessionEntry.sessionId,
@@ -1484,7 +1528,7 @@ describe("CLI attempt execution", () => {
14841528const sessionStore: Record<string, SessionEntry> = { [sessionKey]: sessionEntry };
14851529await fs.writeFile(storePath, JSON.stringify(sessionStore, null, 2), "utf-8");
148615301487-const updatedEntry = await persistCliTurnTranscript({
1531+const updatedEntry = await persistCliTranscriptEntry({
14881532body: [
14891533"<<<BEGIN_OPENCLAW_INTERNAL_CONTEXT>>>",
14901534"secret runtime context",
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。