mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
feat(cli): per-team memory dir with legacy migration and gitignore
- team memory moves to <repo>/.hapi/teams/<slug>-<short-id>/ so several teams on one repository no longer share (and clobber) a single charter - the pre-teams/ shared .hapi/team dir is moved into the first team dir that starts after the upgrade - .hapi/ is appended to the repo .gitignore once (idempotent, best effort)
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
|
||||
import { ensureTeamMemory, resolveMainRepoRoot, resolveTeamMemoryDir } from './teamMemory'
|
||||
import { ensureTeamMemory, resolveMainRepoRoot, resolveTeamMemoryDir, teamDirName } from './teamMemory'
|
||||
|
||||
const TEAM_ENV = { HAPI_TEAM_ID: 'team-1', HAPI_TEAM_NAME: 'Auth 重构' }
|
||||
|
||||
function makeRepos() {
|
||||
const root = mkdtempSync(join(tmpdir(), 'hapi-team-memory-'))
|
||||
@@ -21,21 +23,29 @@ describe('teamMemory', () => {
|
||||
try {
|
||||
expect(resolveMainRepoRoot(main)).toBe(main)
|
||||
expect(resolveMainRepoRoot(worktree)).toBe(main)
|
||||
expect(resolveTeamMemoryDir(worktree)).toBe(join(main, '.hapi', 'team'))
|
||||
expect(resolveTeamMemoryDir(worktree, TEAM_ENV)).toBe(join(main, '.hapi', 'teams', 'Auth-重构-team1'))
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('names the team directory from the team name + short id', () => {
|
||||
expect(teamDirName(TEAM_ENV)).toBe('Auth-重构-team1')
|
||||
expect(teamDirName({ HAPI_TEAM_ID: '013fe26a-81fb-4c6c-b3d8-442bf2b67d82' })).toBe('team-013fe26a')
|
||||
expect(teamDirName({ HAPI_TEAM_NAME: '增长/搜索 团队' })).toBe('增长-搜索-团队')
|
||||
expect(teamDirName({})).toBe('default')
|
||||
})
|
||||
|
||||
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 dir = ensureTeamMemory(TEAM_ENV, worktree)
|
||||
expect(dir).toBe(join(root, 'repo', '.hapi', 'teams', 'Auth-重构-team1'))
|
||||
const charter = readFileSync(join(dir!, 'charter.md'), 'utf8')
|
||||
expect(charter).toContain('Auth 重构')
|
||||
expect(existsSync(join(dir!, 'handoffs'))).toBe(true)
|
||||
|
||||
// Idempotent: an existing charter is not overwritten.
|
||||
writeFileSync(join(dir!, 'charter.md'), 'custom', 'utf8')
|
||||
@@ -45,4 +55,36 @@ describe('teamMemory', () => {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('migrates the legacy shared .hapi/team directory into the team dir', () => {
|
||||
const { root, main, worktree } = makeRepos()
|
||||
try {
|
||||
mkdirSync(join(main, '.hapi', 'team', 'handoffs'), { recursive: true })
|
||||
writeFileSync(join(main, '.hapi', 'team', 'charter.md'), 'legacy charter', 'utf8')
|
||||
writeFileSync(join(main, '.hapi', 'team', 'handoffs', 'note.md'), 'note', 'utf8')
|
||||
|
||||
const dir = ensureTeamMemory(TEAM_ENV, worktree)
|
||||
expect(dir).toBe(join(main, '.hapi', 'teams', 'Auth-重构-team1'))
|
||||
expect(existsSync(join(main, '.hapi', 'team'))).toBe(false)
|
||||
expect(readFileSync(join(dir!, 'charter.md'), 'utf8')).toBe('legacy charter')
|
||||
expect(readFileSync(join(dir!, 'handoffs', 'note.md'), 'utf8')).toBe('note')
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('adds .hapi/ to .gitignore exactly once', () => {
|
||||
const { root, main, worktree } = makeRepos()
|
||||
try {
|
||||
writeFileSync(join(main, '.gitignore'), 'node_modules\n', 'utf8')
|
||||
ensureTeamMemory(TEAM_ENV, worktree)
|
||||
const first = readFileSync(join(main, '.gitignore'), 'utf8')
|
||||
expect(first).toContain('.hapi/')
|
||||
|
||||
ensureTeamMemory(TEAM_ENV, worktree)
|
||||
expect(readFileSync(join(main, '.gitignore'), 'utf8')).toBe(first)
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
|
||||
import { join, resolve } from 'node:path'
|
||||
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs'
|
||||
import { dirname, 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.
|
||||
* `<main repo root>/.hapi/teams/<team-dir>/` on the machine where the agent runs.
|
||||
* The directory name is `<slug of team name>-<short team id>` so one repository can
|
||||
* host several teams without mixing their charter / roles / handoffs.
|
||||
*
|
||||
* 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>`).
|
||||
@@ -32,12 +34,36 @@ export function resolveMainRepoRoot(cwd: string = process.cwd()): string {
|
||||
return cwd
|
||||
}
|
||||
|
||||
export function resolveTeamMemoryDir(cwd: string = process.cwd()): string {
|
||||
return join(resolveMainRepoRoot(cwd), '.hapi', 'team')
|
||||
/** Directory name for one team: `<slug>-<8-char id>` (fallbacks: `team-<id>`, `default`). */
|
||||
export function teamDirName(env: NodeJS.ProcessEnv = process.env): string {
|
||||
const teamId = env.HAPI_TEAM_ID?.trim() ?? ''
|
||||
const shortId = teamId.replace(/[^a-z0-9]/gi, '').slice(0, 8).toLowerCase()
|
||||
const slug = slugifyTeamName(env.HAPI_TEAM_NAME ?? '')
|
||||
if (slug && shortId) {
|
||||
return `${slug}-${shortId}`
|
||||
}
|
||||
if (slug) {
|
||||
return slug
|
||||
}
|
||||
return shortId ? `team-${shortId}` : 'default'
|
||||
}
|
||||
|
||||
function slugifyTeamName(name: string): string {
|
||||
return name
|
||||
.trim()
|
||||
.replace(/[\\/:*?"<>|\u0000-\u001f]+/g, '-')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.replace(/^[-.]+|[-.]+$/g, '')
|
||||
.slice(0, 40)
|
||||
}
|
||||
|
||||
export function resolveTeamMemoryDir(cwd: string = process.cwd(), env: NodeJS.ProcessEnv = process.env): string {
|
||||
return join(resolveMainRepoRoot(cwd), '.hapi', 'teams', teamDirName(env))
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the team memory dir + charter.md exist for a team member session.
|
||||
* Ensure the per-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 {
|
||||
@@ -45,8 +71,11 @@ export function ensureTeamMemory(env: NodeJS.ProcessEnv = process.env, cwd: stri
|
||||
if (!teamId) {
|
||||
return null
|
||||
}
|
||||
const dir = resolveTeamMemoryDir(cwd)
|
||||
const repoRoot = resolveMainRepoRoot(cwd)
|
||||
const dir = resolveTeamMemoryDir(cwd, env)
|
||||
try {
|
||||
migrateLegacyTeamMemory(repoRoot, dir)
|
||||
ensureRepoIgnore(repoRoot)
|
||||
mkdirSync(join(dir, 'handoffs'), { recursive: true })
|
||||
const charter = join(dir, 'charter.md')
|
||||
if (!existsSync(charter)) {
|
||||
@@ -59,6 +88,34 @@ export function ensureTeamMemory(env: NodeJS.ProcessEnv = process.env, cwd: stri
|
||||
return dir
|
||||
}
|
||||
|
||||
/** Move the pre-`teams/` shared directory to the first team that starts after the upgrade. */
|
||||
function migrateLegacyTeamMemory(repoRoot: string, dir: string): void {
|
||||
const legacy = join(repoRoot, '.hapi', 'team')
|
||||
if (existsSync(dir) || !existsSync(legacy)) {
|
||||
return
|
||||
}
|
||||
mkdirSync(dirname(dir), { recursive: true })
|
||||
renameSync(legacy, dir)
|
||||
}
|
||||
|
||||
/** Keep `.hapi/` out of `git status` without touching other ignore rules. */
|
||||
function ensureRepoIgnore(repoRoot: string): void {
|
||||
if (!existsSync(join(repoRoot, '.git'))) {
|
||||
return
|
||||
}
|
||||
const file = join(repoRoot, '.gitignore')
|
||||
const existing = existsSync(file) ? readFileSync(file, 'utf8') : ''
|
||||
const alreadyIgnored = existing.split(/\r?\n/).some((line) => {
|
||||
const entry = line.trim()
|
||||
return entry === '.hapi' || entry === '.hapi/' || entry === '/.hapi' || entry === '/.hapi/'
|
||||
})
|
||||
if (alreadyIgnored) {
|
||||
return
|
||||
}
|
||||
const prefix = existing.length === 0 || existing.endsWith('\n') ? '' : '\n'
|
||||
writeFileSync(file, `${existing}${prefix}# HAPI team memory (local)\n.hapi/\n`, 'utf8')
|
||||
}
|
||||
|
||||
function charterTemplate(teamName: string): string {
|
||||
return [
|
||||
`# ${teamName} — 团队规约`,
|
||||
|
||||
@@ -16,7 +16,7 @@ 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).
|
||||
// Memory lives with the code: <repo>/.hapi/teams/<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.`
|
||||
|
||||
Reference in New Issue
Block a user