Files
ai_academy/internal/application/service/workspace_checkpointer.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

217 lines
7.8 KiB
Go

// Package service - per-turn /workspace git checkpoints.
//
// WorkspaceCheckpointer commits the session sandbox's /workspace at the end of
// every agent turn so session fork and rewind can later roll a sandbox back to
// a specific turn. Git metadata lives at sandbox.SessionGitDir, outside
// /workspace, so wiping the work tree cannot drop checkpoint history. It
// mirrors ArtifactCollector's shape deliberately: a narrow injectable
// interface for the sandbox side, and strict best-effort semantics so an
// auxiliary feature can never break a reply.
//
// Contract:
// - Never returns an error. A failed checkpoint yields nil, the message
// simply carries no checkpoint, and that fork point degrades.
// - Never lazy-creates a sandbox: an empty sandboxID short-circuits.
// - Idempotent inside the sandbox: the script initialises the repository
// only when absent, so pause/resume and sandbox rebuilds self-heal.
package service
import (
"context"
"fmt"
"regexp"
"strings"
"time"
"academy/internal/logger"
"academy/internal/sandbox"
"academy/internal/types"
"academy/internal/types/interfaces"
)
// SandboxShellRunner is the narrow subset of sandbox behaviour the
// checkpointer needs. *sandbox.SessionBoundManager satisfies it in production;
// tests use a fake to assert on the emitted script without a real sandbox.
type SandboxShellRunner interface {
ExecShellCommand(
ctx context.Context,
sessionID, command, workDir string,
timeout time.Duration,
env map[string]string,
) (*sandbox.ExecuteResult, error)
}
// workspaceCheckpointTimeout bounds the git round-trip. `git add -A` walks the
// whole working tree, so this is generous relative to a normal commit but
// still short enough that a wedged sandbox cannot stall the completion path.
const workspaceCheckpointTimeout = 30 * time.Second
// gitSHAPattern matches a full 40-character hex object name. The script's last
// stdout line must look like one; anything else means git printed a warning or
// the script diverged, and we refuse to record a checkpoint we cannot trust.
var gitSHAPattern = regexp.MustCompile(`^[0-9a-f]{40}$`)
// WorkspaceCheckpointer commits /workspace after each agent turn.
type WorkspaceCheckpointer struct {
runner SandboxShellRunner
activity interfaces.ActivityRegistry
}
// NewWorkspaceCheckpointer wires a checkpointer. A nil runner yields a
// checkpointer that degrades to "no checkpoint", matching the graceful
// degradation contract used by ArtifactCollector.
func NewWorkspaceCheckpointer(runner SandboxShellRunner) *WorkspaceCheckpointer {
return &WorkspaceCheckpointer{runner: runner}
}
// WithActivityRegistry makes Checkpoint flag commits taken while background
// work is live. Without a registry every checkpoint is unflagged.
func (c *WorkspaceCheckpointer) WithActivityRegistry(activity interfaces.ActivityRegistry) *WorkspaceCheckpointer {
if c != nil {
c.activity = activity
}
return c
}
// backgroundActive reports whether background work is live in the session. A
// failed lookup counts as live: an unusable fork point is recoverable, a rewind
// onto a half-written tree is not.
func (c *WorkspaceCheckpointer) backgroundActive(ctx context.Context, sessionID string) bool {
if c.activity == nil {
return false
}
tenantID, ok := types.SandboxTenantIDFromContext(ctx)
if !ok || tenantID == 0 {
return false
}
live, err := c.activity.List(ctx, interfaces.SessionRef{TenantID: tenantID, SessionID: sessionID})
if err != nil {
logger.Warnf(ctx, "[WorkspaceCheckpointer] background lookup failed session=%s: %v", sessionID, err)
return true
}
return len(live) > 0
}
// checkpointScript renders the idempotent init-and-commit script.
//
// Ordering matters in two places:
//
// - `safe.directory` is passed per-invocation via git_ws. /workspace is
// owned by uid 1000 while shell_exec runs as root by default, and git
// refuses to touch a dubiously-owned repository — including from the
// rev-parse probe below. -c avoids writing ~/.gitconfig every turn.
//
// - `--allow-empty` is mandatory. A turn that only answered a question
// without touching a file would otherwise produce no commit, leaving that
// message with no checkpoint and making it unusable as a fork point. With
// it, every turn has exactly one checkpoint and fork boundaries are always
// resolvable.
func checkpointScriptFor(l sandbox.WorkspaceLayout, messageID string) string {
return fmt.Sprintf(`set -e
%s
%s
git_ws add -A
git_ws commit -q --allow-empty -m 'turn:%s'
git_ws rev-parse HEAD`, gitWorkspacePreambleFor(l), gitWorkspaceEnsureRepo(), messageID)
}
func checkpointScript(workspace, gitDir, messageID string) string {
return checkpointScriptFor(sandbox.WorkspaceLayout{
Origin: sandbox.WorkspaceOriginRemote, Root: workspace, GitDir: gitDir,
}, messageID)
}
// Checkpoint commits /workspace and returns the resulting checkpoint, or nil
// when anything at all went wrong. It never returns an error by design: the
// caller runs on the turn-completion path and must not be blocked here.
func (c *WorkspaceCheckpointer) Checkpoint(
ctx context.Context, sessionID, sandboxID, messageID string,
) *types.SandboxCheckpoint {
if c == nil || c.runner == nil {
return nil
}
if strings.TrimSpace(sessionID) == "" || strings.TrimSpace(messageID) == "" {
return nil
}
// No bound sandbox means there is nothing to snapshot, and a checkpoint
// without a sandbox ID could never be validated at fork time anyway.
if strings.TrimSpace(sandboxID) == "" {
return nil
}
if v, ok := c.runner.(WorkspaceVersioning); ok && !v.VersionsWorkspace(ctx, sessionID) {
return nil
}
layout, err := workspaceLayoutFor(ctx, c.runner, sessionID)
if err != nil {
logger.Warnf(ctx, "[WorkspaceCheckpointer] layout unavailable session=%s: %v", sessionID, err)
return nil
}
command := checkpointScriptFor(layout, messageID)
// Read before committing: no tool runs while the turn is finishing, so no
// background work can start between this read and the commit.
backgroundActive := c.backgroundActive(ctx, sessionID)
var result *sandbox.ExecuteResult
if runner, ok := c.runner.(sandbox.SessionInstallShellExecutor); ok {
// Git operates on the existing workspace. Do not prepare artifact/input
// directories or let a late checkpoint provision a replacement sandbox.
result, err = runner.ExecShellCommandWithOptions(ctx, sessionID, command, sandbox.ShellExecOptions{
WorkDir: layout.Root, Timeout: workspaceCheckpointTimeout,
SkipWorkspacePrep: true, ExpectedSandboxID: sandboxID,
})
} else {
result, err = c.runner.ExecShellCommand(ctx, sessionID, command,
layout.Root, workspaceCheckpointTimeout, nil)
}
if err != nil {
logger.Warnf(ctx, "[WorkspaceCheckpointer] exec failed session=%s message=%s: %v",
sessionID, messageID, err)
return nil
}
if result == nil {
logger.Warnf(ctx, "[WorkspaceCheckpointer] nil result session=%s message=%s",
sessionID, messageID)
return nil
}
if result.ExitCode != 0 {
logger.Warnf(ctx,
"[WorkspaceCheckpointer] git exited %d session=%s message=%s stderr=%s",
result.ExitCode, sessionID, messageID, truncateForLog(result.Stderr))
return nil
}
sha := lastNonEmptyLine(result.Stdout)
if !gitSHAPattern.MatchString(sha) {
logger.Warnf(ctx,
"[WorkspaceCheckpointer] unexpected stdout tail session=%s message=%s tail=%q",
sessionID, messageID, truncateForLog(sha))
return nil
}
return &types.SandboxCheckpoint{
SandboxID: sandboxID,
CommitSHA: sha,
CommittedAt: time.Now().UTC(),
BackgroundActive: backgroundActive,
}
}
func lastNonEmptyLine(s string) string {
lines := strings.Split(strings.TrimRight(s, "\n"), "\n")
for i := len(lines) - 1; i >= 0; i-- {
if line := strings.TrimSpace(lines[i]); line != "" {
return line
}
}
return ""
}
func truncateForLog(s string) string {
const limit = 256
s = strings.TrimSpace(s)
if len(s) <= limit {
return s
}
return s[:limit] + "…"
}