feat(cli): Cursor emits AGENT_NOTIFY_SUMMARY when status-summary toggle is on (#2354)

* feat(cli): Cursor emits AGENT_NOTIFY_SUMMARY via rules overlay

When Emit status summaries is on, install a transient workspace
.cursor/rules overlay for Cursor ACP sessions and clean it up on
teardown, closing the flavor hole left after #1376.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): harden Cursor notify-rule overlay ownership and exclude

Address review on the emit overlay: clear ownership state under lock,
refresh crash-recovery snapshots after user replacement, fall back to
an alternate rule filename when the primary path is tracked, and lease
.git/info/exclude so git add -A cannot stage the transient rule.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): path-scope notify overlay ownership and reap excludes

Refresh restore snapshots on live user replacements, key ownership by
selected rule path, and prune dead PID-stamped .git/info/exclude leases
left by crashed Cursor sessions.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): harden Cursor notify overlay against symlink and path traps

Refuse rule writes through symlinks, canonicalize workspace paths for
ownership, escape literal Git exclude patterns, and qualify settings
copy as Cursor ACP.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): preserve unreadable rules and file modes in notify overlay

Skip install when an existing rule cannot be backed up, restore original
permission bits on cleanup, and stop retrying ownership-lock callback
failures as lock contention.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): keep notify overlay restore snapshot on unreadable cleanup

When teardown cannot read the generated rule, leave the file alone but
retain the private ownership snapshot so a later session can restore the
user's original content; write state.json as 0600.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): restore notify overlay snapshot even if install never wrote

Last-owner cleanup now restores from the shared snapshot whenever the
on-disk rule is still HAPI-owned, including handles that registered then
failed before writing the generated rule.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): write notify overlay ownership state atomically

Replace in-place state.json writes with temp+rename so a partial failure
cannot destroy the only backup of a user rule, and harden the failed-
install recovery test to actually chmod the exclude file.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): fail closed on abandoned notify overlay locks

Publish pid+token via link(2) like the MCP overlay so a crashed holder
leaves an identifiable lock, and surface an actionable rm path instead of
silently spinning for five seconds.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): retry notify overlay lock when holder already released

After link EEXIST, a missing lock means the contender freed it before our
owner read - retry acquire instead of misreporting the path as abandoned.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): treat notify lock ENOENT during owner read as retry

Distinguish missing vs invalid lock contents so a holder that releases
between existsSync and readFile is not misclassified as abandoned.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): recreate notify overlay stateDir under the ownership lock

Last-owner cleanup no longer rmdirs stateDir, and install mkdir+write runs
inside the lock so a waiting installer cannot hit ENOENT on state.json.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
HeavyGee
2026-11-10 15:12:06 +08:00
committed by GitHub
co-authored by Cursor
parent ce6d074a54
commit 39da0be557
9 changed files with 1657 additions and 11 deletions
@@ -44,6 +44,8 @@ const harness = vi.hoisted(() => ({
stderrErrorHandler: null as ((error: { type: string; message: string; raw?: string }) => void) | null,
disconnectError: null as Error | null,
overlayCleanup: null as ReturnType<typeof vi.fn> | null,
notifyRuleInstallCalls: [] as Array<{ cwd: string; project?: string | null; agentId?: string | null }>,
notifyRuleCleanup: null as ReturnType<typeof vi.fn> | null,
agentActivityListener: null as ((thinking: boolean, source?: string) => void) | null,
titleSyncControllers: [] as unknown[],
titleSyncRegistered: [] as unknown[],
@@ -285,6 +287,18 @@ vi.mock('./utils/cursorMcpOverlay', () => ({
},
}));
vi.mock('./utils/cursorNotifyRuleOverlay', () => ({
installCursorNotifyRuleOverlay: (opts: {
cwd: string
project?: string | null
agentId?: string | null
}) => {
harness.notifyRuleInstallCalls.push(opts);
harness.notifyRuleCleanup = vi.fn();
return { rulePath: `${opts.cwd}/.cursor/rules/hapi-session.mdc`, cleanup: harness.notifyRuleCleanup };
},
}));
vi.mock('@/ui/ink/OpencodeDisplay', () => ({
OpencodeDisplay: () => null
}));
@@ -298,6 +312,10 @@ import { createCursorAcpBackend } from './utils/cursorAcpBackend';
import { RPC_METHODS } from '@hapi/protocol/rpcMethods';
import { CursorSession } from './session';
import { ApiSessionClient } from '@/api/apiSession';
import {
applyHubSessionSummaryContract,
resetSessionSummaryContractForTests
} from '@/modules/common/sessionSummaryInstruction';
import {
_resetSharedCursorModelsCacheForTests,
writeSharedCursorModelsCache
@@ -394,7 +412,11 @@ describe('cursorAcpRemoteLauncher', () => {
harness.stderrErrorHandler = null;
harness.disconnectError = null;
harness.overlayCleanup = null;
harness.notifyRuleInstallCalls = [];
harness.notifyRuleCleanup = null;
harness.agentActivityListener = null;
resetSessionSummaryContractForTests();
delete process.env.HAPI_SESSION_SUMMARY_CONTRACT;
harness.titleSyncControllers = [];
harness.titleSyncRegistered = [];
harness.onChangeTitle = null;
@@ -412,6 +434,8 @@ describe('cursorAcpRemoteLauncher', () => {
afterEach(() => {
vi.clearAllMocks();
_resetSharedCursorModelsCacheForTests();
resetSessionSummaryContractForTests();
delete process.env.HAPI_SESSION_SUMMARY_CONTRACT;
});
it('ends the launcher when a soft steer outlives an abort', async () => {
@@ -1152,6 +1176,35 @@ describe('cursorAcpRemoteLauncher', () => {
expect(harness.overlayCleanup).toHaveBeenCalled();
});
it('installs the notify-rule overlay when emit status summaries is on', async () => {
applyHubSessionSummaryContract(true);
const session = makeSession(null);
await cursorAcpRemoteLauncher(session);
expect(harness.notifyRuleInstallCalls).toEqual([
{ cwd: '/tmp/project', project: 'project', agentId: 'test-session-id' }
]);
expect(harness.notifyRuleCleanup).toHaveBeenCalled();
});
it('skips the notify-rule overlay when emit status summaries is off', async () => {
applyHubSessionSummaryContract(false);
const session = makeSession(null);
await cursorAcpRemoteLauncher(session);
expect(harness.notifyRuleInstallCalls).toEqual([]);
expect(harness.notifyRuleCleanup).toBeNull();
});
it('removes the notify-rule overlay even when backend.disconnect rejects', async () => {
applyHubSessionSummaryContract(true);
harness.disconnectError = new Error('disconnect failed');
const session = makeSession(null);
await expect(cursorAcpRemoteLauncher(session)).rejects.toThrow('disconnect failed');
expect(harness.notifyRuleCleanup).toHaveBeenCalled();
});
it('drops post-done harness speech after the HAPI prompt ends (#2267)', async () => {
const doneFooter =
'AGENT_NOTIFY_SUMMARY {"version":1,"agent":"a","project":"hapi","status":"done","summary":"Topic closed"}';