feat(agent): add nested agents.md support across backend and webui (#309)

* feat(agent): add nested agents.md support across backend and webui

* fix(webui): update agentsmd route paths and unnest folder agentsmd route

* fix(agent): flush agents.md autosave on close and address review feedback

- Flush pending debounced AGENTS.md autosave on close/unmount so edits
  made within 750ms of navigating away are no longer silently lost
- Serialize autosaves through a promise chain so an older in-flight
  request can never overwrite newer content on the server
- Internationalize the hardcoded "Failed to save" and
  "Loading AGENTS.md..." strings (en + zh)
- Validate that AGENTS.md is only stored on folder/root assets in
  AssetService, preventing bogus virtual paths for file assets
- Cap updateAgentsMd content at 100k chars via Zod validation
- Remove unused getAgentsMd/updateAgentsMd response DTO schemas
- Drop unused teamId prop from AgentsMdEditor
- Add tests: non-folder asset rejection, oversized content 400,
  and agent-chat workflow assetId passthrough

* refactor(db): isolate AGENTS.md content in dedicated 1:1 AssetAgentMd table

* style(webui): unify top bar button hover styles and add wand icon to agentsmd button

* style(webui): update agentsmd button state colors and remove presence dot

* fix(webui): fix close button overflow and stabilize debounced autosave in agentsmd editor

* fix(webui): update debounce to 1s and await pending save before closing agentsmd editor

* feat(webui): add terminal AGENTS.md breadcrumb on agentsmd editor page

* fix(webui): use ScrollArea for markdown editor scrolling
This commit is contained in:
Yiling-J
2026-08-15 10:25:46 +08:00
committed by GitHub
parent 3c3b6880e4
commit 1cee103b1c
56 changed files with 3529 additions and 242 deletions
+110
View File
@@ -18,6 +18,7 @@ import {
generateSessionNameActivity,
type GenerateSessionNameParams,
getUserTeamInfoActivity,
formatProjectContextPrompt,
} from './agent'
import * as piAgent from '../index'
import { type AgentHarness, type Session } from '@earendil-works/pi-agent-core'
@@ -2041,4 +2042,113 @@ describe('Agent Database Activities Integration', () => {
expect(piAgent.createAgentSession).not.toHaveBeenCalled()
})
})
describe('Nested AGENTS.md prompt injection', () => {
it('formatProjectContextPrompt formats context files into XML project_context block', () => {
expect(formatProjectContextPrompt([])).toBe('')
const formatted = formatProjectContextPrompt([
{ path: '/AGENTS.md', content: 'Root rules' },
{ path: '/subfolder/AGENTS.md', content: 'Subfolder rules' },
])
expect(formatted).toBe(
'\n\n<project_context>\n\nProject-specific instructions and guidelines:\n\n<project_instructions path="/AGENTS.md">\nRoot rules\n</project_instructions>\n\n<project_instructions path="/subfolder/AGENTS.md">\nSubfolder rules\n</project_instructions>\n\n</project_context>',
)
})
it('agentChatActivity injects nested AGENTS.md instructions into systemPrompt', async () => {
const user = await prisma.user.create({
data: { name: 'User_Chat_Md', email: `chat-md-${Date.now()}@example.com` },
})
const team = await prisma.team.create({
data: { name: 'Team_Chat_Md_' + Date.now() },
})
const project = await prisma.project.create({
data: { name: 'Project_Chat_Md', teamId: team.id },
})
const rootFolder = await prisma.asset.create({
data: {
name: 'Root',
type: AssetType.root,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
await prisma.assetAgentMd.create({
data: {
assetId: rootFolder.id,
content: '# Root Policy',
},
})
const subfolder = await prisma.asset.create({
data: {
name: 'subfolder',
type: AssetType.folder,
parentId: rootFolder.id,
projectId: project.id,
status: AssetStatus.uploaded,
},
})
await prisma.assetAgentMd.create({
data: {
assetId: subfolder.id,
content: '# Subfolder Policy',
},
})
const mockHarness = {
subscribe: vi.fn(),
prompt: vi.fn().mockResolvedValue({
content: [{ type: 'text', text: 'Chat reply' }],
usage: { input: 10, output: 20 },
}),
}
const mockSession = {
getEntries: vi.fn().mockResolvedValue([]),
getStorage: vi.fn().mockReturnValue({ sessionId: 'mock-session-id' }),
}
vi.mocked(piAgent.createAgentSession).mockResolvedValue({
session: mockSession as unknown as Session<DatabaseSessionMetadata>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Mock AgentHarness instance for activity test
harness: mockHarness as unknown as AgentHarness<any, any, any, any>,
})
const context = {
agent: { id: 'agent-1', provider: { name: 'google' }, modelRef: { modelId: 'gemini' } },
dbProviders: [],
teamSkills: [],
allowedDomains: [],
} as unknown as AgentExecutionContext
await agentChatActivity({
teamId: team.id,
agentId: 'agent-1',
message: 'Hello',
imageUrls: [],
projectId: project.id,
folderId: subfolder.id,
assetId: subfolder.id,
sessionId: 'mock-session-id',
userId: user.id,
context,
})
expect(piAgent.createAgentSession).toHaveBeenCalledWith(
expect.objectContaining({
systemPrompt: expect.stringContaining('<project_context>'),
}),
)
const lastCall = vi.mocked(piAgent.createAgentSession).mock.calls.at(-1)
const systemPromptArg = lastCall?.[0].systemPrompt as string
expect(systemPromptArg).toContain(
'<project_instructions path="/AGENTS.md">\n# Root Policy\n</project_instructions>',
)
expect(systemPromptArg).toContain(
'<project_instructions path="/subfolder/AGENTS.md">\n# Subfolder Policy\n</project_instructions>',
)
})
})
})