mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-19 03:40:50 +00:00
refactor(hub,cli,web): move team memory into the repo (runner side)
Team memory now lives with the code, like CLAUDE.md: <main repo>/.hapi/team/ on the machine where the agent runs. Worktrees resolve to the main repo so every member shares one directory. The CLI creates charter.md + handoffs/ at session start (team env only), points the agent at it in the system prompt, and reports the path in session metadata; the web reads it through the session file API, so remote runners work too. Hub keeps only coordination data (teams.db): members/tasks/messages/status. Its memory-dir management and the /memory API are removed.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
|
||||
import { ensureTeamMemory, resolveMainRepoRoot, resolveTeamMemoryDir } from './teamMemory'
|
||||
|
||||
function makeRepos() {
|
||||
const root = mkdtempSync(join(tmpdir(), 'hapi-team-memory-'))
|
||||
const main = join(root, 'repo')
|
||||
const worktree = join(root, 'wt')
|
||||
mkdirSync(join(main, '.git', 'worktrees', 'feat'), { recursive: true })
|
||||
mkdirSync(worktree, { recursive: true })
|
||||
writeFileSync(join(worktree, '.git'), `gitdir: ${join(main, '.git', 'worktrees', 'feat')}\n`, 'utf8')
|
||||
return { root, main, worktree }
|
||||
}
|
||||
|
||||
describe('teamMemory', () => {
|
||||
it('resolves the repo root for a plain checkout and a linked worktree', () => {
|
||||
const { root, main, worktree } = makeRepos()
|
||||
try {
|
||||
expect(resolveMainRepoRoot(main)).toBe(main)
|
||||
expect(resolveMainRepoRoot(worktree)).toBe(main)
|
||||
expect(resolveTeamMemoryDir(worktree)).toBe(join(main, '.hapi', 'team'))
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('creates charter.md + handoffs only for team sessions', () => {
|
||||
const { root, worktree } = makeRepos()
|
||||
try {
|
||||
expect(ensureTeamMemory({}, worktree)).toBeNull()
|
||||
|
||||
const dir = ensureTeamMemory({ HAPI_TEAM_ID: 'team-1', HAPI_TEAM_NAME: 'Auth 重构' }, worktree)
|
||||
expect(dir).toBe(join(root, 'repo', '.hapi', 'team'))
|
||||
const charter = readFileSync(join(dir!, 'charter.md'), 'utf8')
|
||||
expect(charter).toContain('Auth 重构')
|
||||
|
||||
// Idempotent: an existing charter is not overwritten.
|
||||
writeFileSync(join(dir!, 'charter.md'), 'custom', 'utf8')
|
||||
ensureTeamMemory({ HAPI_TEAM_ID: 'team-1' }, worktree)
|
||||
expect(readFileSync(join(dir!, 'charter.md'), 'utf8')).toBe('custom')
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,80 @@
|
||||
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
||||
import { join, resolve } from 'node:path'
|
||||
|
||||
/**
|
||||
* Team memory lives with the code, like CLAUDE.md / project docs:
|
||||
* `<main repo root>/.hapi/team/` on the machine where the agent runs.
|
||||
*
|
||||
* Worktrees point at the main repository so every member's worktree shares one
|
||||
* memory directory (a linked worktree's `.git` is a file: `gitdir: <main>/.git/worktrees/<name>`).
|
||||
*/
|
||||
export function resolveMainRepoRoot(cwd: string = process.cwd()): string {
|
||||
const dotGit = join(cwd, '.git')
|
||||
let isDirectory = false
|
||||
try {
|
||||
isDirectory = statSync(dotGit).isDirectory()
|
||||
} catch {
|
||||
isDirectory = false
|
||||
}
|
||||
if (isDirectory) {
|
||||
return cwd
|
||||
}
|
||||
try {
|
||||
const content = readFileSync(dotGit, 'utf8')
|
||||
const match = content.match(/^gitdir:\s*(.+)\s*$/m)
|
||||
if (match?.[1]) {
|
||||
// <main>/.git/worktrees/<name> -> <main>
|
||||
return resolve(match[1].trim(), '..', '..', '..')
|
||||
}
|
||||
} catch {
|
||||
// not a worktree
|
||||
}
|
||||
return cwd
|
||||
}
|
||||
|
||||
export function resolveTeamMemoryDir(cwd: string = process.cwd()): string {
|
||||
return join(resolveMainRepoRoot(cwd), '.hapi', 'team')
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the team memory dir + charter.md exist for a team member session.
|
||||
* Returns the directory, or null when the session is not in a team.
|
||||
*/
|
||||
export function ensureTeamMemory(env: NodeJS.ProcessEnv = process.env, cwd: string = process.cwd()): string | null {
|
||||
const teamId = env.HAPI_TEAM_ID?.trim()
|
||||
if (!teamId) {
|
||||
return null
|
||||
}
|
||||
const dir = resolveTeamMemoryDir(cwd)
|
||||
try {
|
||||
mkdirSync(join(dir, 'handoffs'), { recursive: true })
|
||||
const charter = join(dir, 'charter.md')
|
||||
if (!existsSync(charter)) {
|
||||
const name = env.HAPI_TEAM_NAME?.trim() || teamId.slice(0, 8)
|
||||
writeFileSync(charter, charterTemplate(name), 'utf8')
|
||||
}
|
||||
} catch {
|
||||
// Best effort: the agent can still create the files itself.
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
function charterTemplate(teamName: string): string {
|
||||
return [
|
||||
`# ${teamName} — 团队规约`,
|
||||
'',
|
||||
'> 团队记忆与代码放在一起,成员用普通文件工具读写;提交与否由团队决定。',
|
||||
'',
|
||||
'## 目标',
|
||||
'(lead 或人类补充这个团队要达成什么)',
|
||||
'',
|
||||
'## 协作规则',
|
||||
'- 广播消息不会主动推送:开工前用 team_read 拉取,完成后用 team_send 汇报。',
|
||||
'- 需要人类决策时用 team_send 的 to="human" 或 kind="decision"。',
|
||||
'- 交接产物写到 handoffs/ 目录,文件名用任务 id 或简短主题。',
|
||||
'',
|
||||
'## 决策记录',
|
||||
'(重要取舍追加到这里,说明日期、背景、结论)',
|
||||
''
|
||||
].join('\n')
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { trimIdent } from '@/utils/trimIdent'
|
||||
import { ensureTeamMemory } from './teamMemory'
|
||||
|
||||
/**
|
||||
* System-prompt block injected into team member sessions.
|
||||
@@ -15,12 +16,18 @@ export function getTeamPromptBlock(toolPrefix: string, env: NodeJS.ProcessEnv =
|
||||
}
|
||||
const name = env.HAPI_TEAM_NAME?.trim() || teamId.slice(0, 8)
|
||||
const role = env.HAPI_TEAM_ROLE?.trim() || 'member'
|
||||
// Memory lives with the code: <repo>/.hapi/team/ (worktrees share the main repo).
|
||||
const memoryDir = ensureTeamMemory(env)
|
||||
const memoryLine = memoryDir
|
||||
? `- Team memory lives with the code: ${memoryDir}/ (charter.md = team charter, handoffs/ = handoff notes). Read/write it with your normal file tools.`
|
||||
: null
|
||||
return trimIdent(`
|
||||
You are a member of HAPI agent team "${name}" (your role: ${role}).
|
||||
- Start by calling ${toolPrefix}team_status to pick up your assignment, and ${toolPrefix}team_read to pull team messages (broadcasts are not pushed to you).
|
||||
- Report progress, completion, and blockers with ${toolPrefix}team_send (kind=status or task-update). Batch updates; do not chat back and forth.
|
||||
- Your normal replies to the human are synced into the team group chat automatically - do not use team_send just to answer the human. Use ${toolPrefix}team_send with to="human" (or kind="decision") only when you genuinely need a human decision; it notifies them out-of-band.
|
||||
- Only use ${toolPrefix}spawn_peer when the human or the lead explicitly asks you to.
|
||||
${memoryLine ?? ''}
|
||||
`)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user