feat(gateway): render workspace, managed repos and operator into chat env
The chat agent had no way to know where it lives without probing the filesystem (pwd, find, git remote), wasting turns on every orientation. Per docs/architecture/identity.md the <env> block is Layer 1 invariants, so the daemon's own facts belong there, driven from live configuration. Render into the prompt's <env> block: - workspace path (chat.workspace) - repositories under management with forge host and default branch - operator identity (chat.operator, which was dead plumbing) Unconfigured values render explicitly as unknown / none configured rather than a plausible-looking default, and a repo with no forge reports forge unknown, so the model never claims a fact the daemon cannot confirm. Repos are resolved per active chat identity, mirroring chatTaskProfiles. gateway: add RepoEnv, SystemPromptConfig Workspace/Repos/Operator, TurnRunnerConfig Workspace/Repos. app: chatRepoEnv resolver wired through newChatTurnRunner. tests: prompt rendering (incl. explicit-unknown + hostile-SOUL guard) and chatRepoEnv identity resolution. go.sum gains module-graph /go.mod checksum lines.
This commit is contained in:
1 parent
003f69fef0
commit
d0079271d5
7 files changed
+237
-1
No files matched your search
@@ -0,0 +1,76 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
)
|
||||
|
||||
func TestChatRepoEnvSingleIdentity(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
Forge: config.Forge{Host: "https://github.com", Type: "github"},
|
||||
Repos: []config.Repo{
|
||||
{Owner: "my-org", Name: "frontend-app", Base: "develop"},
|
||||
{Owner: "my-org", Name: "backend-api"},
|
||||
},
|
||||
}
|
||||
got := chatRepoEnv(cfg, "")
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("expected 2 repos, got %d", len(got))
|
||||
}
|
||||
if got[0].FullName != "my-org/frontend-app" || got[0].Forge != "https://github.com" || got[0].DefaultBranch != "develop" {
|
||||
t.Errorf("repo 0 mismatch: %+v", got[0])
|
||||
}
|
||||
// Base empty -> the daemon's effective default branch (main), not a guess.
|
||||
if got[1].DefaultBranch != "main" {
|
||||
t.Errorf("expected default branch main, got %q", got[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRepoEnvMultiIdentityMatchesActive(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
Identities: []config.IdentityConfig{
|
||||
{Name: "alpha", Forge: config.Forge{Host: "https://gitea.example.com"}, Repos: []config.Repo{{Owner: "alpha", Name: "app"}}},
|
||||
{Name: "beta", Forge: config.Forge{Host: "https://github.com"}, Repos: []config.Repo{{Owner: "beta", Name: "svc"}}},
|
||||
},
|
||||
}
|
||||
got := chatRepoEnv(cfg, "beta")
|
||||
if len(got) != 1 || got[0].FullName != "beta/svc" || got[0].Forge != "https://github.com" {
|
||||
t.Fatalf("expected the active identity's scope, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRepoEnvMultiIdentityFallsBackToFirstWithRepos(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
Identities: []config.IdentityConfig{
|
||||
{Name: "a", Forge: config.Forge{}, Repos: nil},
|
||||
{Name: "b", Forge: config.Forge{Host: "https://github.com"}, Repos: []config.Repo{{Owner: "b", Name: "svc"}}},
|
||||
},
|
||||
}
|
||||
got := chatRepoEnv(cfg, "no-such-identity")
|
||||
if len(got) != 1 || got[0].FullName != "b/svc" {
|
||||
t.Fatalf("expected fallback to the first identity with repos, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRepoEnvPreservesEmptyForge(t *testing.T) {
|
||||
cfg := config.Config{
|
||||
Repos: []config.Repo{{Owner: "acme", Name: "widgets"}},
|
||||
}
|
||||
got := chatRepoEnv(cfg, "")
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("expected 1 repo, got %d", len(got))
|
||||
}
|
||||
if got[0].Forge != "" {
|
||||
t.Errorf("expected empty forge to be preserved (so the prompt renders 'forge unknown'), got %q", got[0].Forge)
|
||||
}
|
||||
if got[0].DefaultBranch != "main" {
|
||||
t.Errorf("expected default branch main, got %q", got[0].DefaultBranch)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRepoEnvEmpty(t *testing.T) {
|
||||
if got := chatRepoEnv(config.Config{}, ""); len(got) != 0 {
|
||||
t.Fatalf("expected no repos, got %+v", got)
|
||||
}
|
||||
}
|
||||
@@ -332,6 +332,8 @@ func newChatTurnRunner(
|
||||
BotUser: cfg.BotUser,
|
||||
Channel: channel,
|
||||
Operator: cfg.Chat.Operator,
|
||||
Workspace: cfg.Chat.Workspace,
|
||||
Repos: chatRepoEnv(cfg, s.DefaultChatIdentity),
|
||||
Log: s.Log,
|
||||
})
|
||||
if err := runner.Recover(ctx); err != nil && s.Log != nil {
|
||||
@@ -340,6 +342,38 @@ func newChatTurnRunner(
|
||||
return runner
|
||||
}
|
||||
|
||||
// chatRepoEnv renders the repositories the chat agent manages, with their
|
||||
// forge host and default branch, into the prompt's <env> block so the agent
|
||||
// knows its scope without probing the filesystem. It follows the same
|
||||
// identity resolution as chatTaskProfiles: when identities are configured,
|
||||
// the one matching the active chat identity wins (falling back to the first
|
||||
// identity with repositories, which is what chatTaskProfiles selects as the
|
||||
// default); otherwise the single-identity repositories are used.
|
||||
func chatRepoEnv(cfg config.Config, identity string) []gateway.RepoEnv {
|
||||
repos, forge := cfg.Repos, cfg.Forge
|
||||
if len(cfg.Identities) > 0 {
|
||||
repos, forge = nil, config.Forge{}
|
||||
for _, id := range cfg.Identities {
|
||||
if id.Name == identity {
|
||||
repos, forge = id.Repos, id.Forge
|
||||
break
|
||||
}
|
||||
if len(repos) == 0 && len(id.Repos) > 0 {
|
||||
repos, forge = id.Repos, id.Forge
|
||||
}
|
||||
}
|
||||
}
|
||||
env := make([]gateway.RepoEnv, 0, len(repos))
|
||||
for _, repo := range repos {
|
||||
env = append(env, gateway.RepoEnv{
|
||||
FullName: repo.FullName(),
|
||||
Forge: forge.Host,
|
||||
DefaultBranch: repo.BaseBranch(),
|
||||
})
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
func sendChatTurn(ctx context.Context, llm *runtime.Runtime, chatModel string, options core.GenerateOptions, turn gateway.TurnStream) (string, error) {
|
||||
if turn == nil {
|
||||
result, err := llm.Chat(ctx, chatModel, options)
|
||||
|
||||
@@ -17,6 +17,22 @@ type ToolSummary struct {
|
||||
Description string
|
||||
}
|
||||
|
||||
// RepoEnv is the runtime-metadata summary of one repository under
|
||||
// management, rendered into the chat prompt's <env> block so the agent knows
|
||||
// which checkouts it is responsible for without probing the filesystem. It is
|
||||
// built only from live configuration; it never names a repository the daemon
|
||||
// cannot confirm.
|
||||
type RepoEnv struct {
|
||||
// FullName is the configured owner/name.
|
||||
FullName string
|
||||
// Forge is the forge host the repository lives on (forge.host). Empty
|
||||
// means no forge is configured for the active identity.
|
||||
Forge string
|
||||
// DefaultBranch is the branch PRs target (repos[].base, "main" when
|
||||
// unset) -- the branch the daemon actually uses, not a guess.
|
||||
DefaultBranch string
|
||||
}
|
||||
|
||||
// SystemPromptConfig holds the inputs for one rendered system prompt.
|
||||
type SystemPromptConfig struct {
|
||||
// Persona is the active persona prompt.
|
||||
@@ -35,6 +51,19 @@ type SystemPromptConfig struct {
|
||||
Page string
|
||||
// Now stamps the prompt's date.
|
||||
Now time.Time
|
||||
// Workspace is the directory the chat agent's file and shell tools are
|
||||
// rooted at (chat.workspace). Empty means the agent has no rooted
|
||||
// workspace: a real fact the prompt must say explicitly ("unknown") so the
|
||||
// agent does not guess a path the daemon never granted it.
|
||||
Workspace string
|
||||
// Repos lists the repositories under management with their forge host and
|
||||
// default branch. Rendered from live configuration (repos entries) so the
|
||||
// agent knows its scope without probing the filesystem.
|
||||
Repos []RepoEnv
|
||||
// Operator is the display name of the person this deployment assists
|
||||
// (chat.operator). Rendered so the agent knows the identity context it
|
||||
// serves under; empty means unconfigured and must be said explicitly.
|
||||
Operator string
|
||||
}
|
||||
|
||||
// promptData is the template execution context. It exists so the template
|
||||
@@ -73,6 +102,9 @@ func BuildSystemPrompt(cfg SystemPromptConfig) string {
|
||||
Page: cfg.Page,
|
||||
Now: cfg.Now,
|
||||
Date: cfg.Now.Format("2006-01-02"),
|
||||
Workspace: cfg.Workspace,
|
||||
Repos: cfg.Repos,
|
||||
Operator: cfg.Operator,
|
||||
}
|
||||
var buf strings.Builder
|
||||
if err := archiePromptTemplate.Execute(&buf, data); err != nil {
|
||||
|
||||
@@ -120,6 +120,55 @@ func TestBuildSystemPromptSections(t *testing.T) {
|
||||
"Current page:",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "env block carries workspace, managed repositories and operator",
|
||||
cfg: SystemPromptConfig{
|
||||
Now: fixedTime(t),
|
||||
Channel: "telegram",
|
||||
Workspace: "/work/apps/archie-core",
|
||||
Operator: "Sam",
|
||||
Repos: []RepoEnv{
|
||||
{FullName: "samcharles93/archie-core", Forge: "https://github.com", DefaultBranch: "main"},
|
||||
{FullName: "my-org/frontend-app", Forge: "https://github.com", DefaultBranch: "develop"},
|
||||
},
|
||||
},
|
||||
contains: []string{
|
||||
"<env",
|
||||
"Workspace: /work/apps/archie-core",
|
||||
"Managed repositories:",
|
||||
"- samcharles93/archie-core (https://github.com, default branch main)",
|
||||
"- my-org/frontend-app (https://github.com, default branch develop)",
|
||||
"Operator: Sam",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "unset workspace, repositories and operator are explicit, not defaulted",
|
||||
cfg: SystemPromptConfig{Now: fixedTime(t)},
|
||||
contains: []string{
|
||||
"Workspace: unknown",
|
||||
"Managed repositories: none configured",
|
||||
"Operator: unknown",
|
||||
},
|
||||
// The prompt must never invent a workspace path or repository the
|
||||
// daemon did not grant it.
|
||||
absent: []string{
|
||||
"Workspace: /",
|
||||
"Managed repositories: -",
|
||||
"Operator: Sam",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "a managed repository with no forge reports it as unknown",
|
||||
cfg: SystemPromptConfig{
|
||||
Now: fixedTime(t),
|
||||
Repos: []RepoEnv{
|
||||
{FullName: "acme/widgets", DefaultBranch: "main"},
|
||||
},
|
||||
},
|
||||
contains: []string{
|
||||
"- acme/widgets (forge unknown, default branch main)",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
|
||||
@@ -61,4 +61,22 @@ Model: {{xml .}}
|
||||
{{- with .SessionID}}
|
||||
Session: {{xml .}}
|
||||
{{- end}}
|
||||
{{- with .Workspace}}
|
||||
Workspace: {{xml .}}
|
||||
{{- else}}
|
||||
Workspace: unknown
|
||||
{{- end}}
|
||||
{{- if .Repos}}
|
||||
Managed repositories:
|
||||
{{- range .Repos}}
|
||||
- {{xml .FullName}} ({{if .Forge}}{{xml .Forge}}{{else}}forge unknown{{end}}, default branch {{xml .DefaultBranch}})
|
||||
{{- end}}
|
||||
{{- else}}
|
||||
Managed repositories: none configured
|
||||
{{- end}}
|
||||
{{- with .Operator}}
|
||||
Operator: {{xml .}}
|
||||
{{- else}}
|
||||
Operator: unknown
|
||||
{{- end}}
|
||||
</env>
|
||||
@@ -90,7 +90,13 @@ type TurnRunnerConfig struct {
|
||||
BotUser string
|
||||
Channel string
|
||||
Operator string
|
||||
Log *slog.Logger
|
||||
// Workspace is the directory the chat agent's file and shell tools are
|
||||
// rooted at (chat.workspace). Passed into the prompt's <env> block.
|
||||
Workspace string
|
||||
// Repos lists the repositories under management with their forge host and
|
||||
// default branch, rendered into the prompt's <env> block.
|
||||
Repos []RepoEnv
|
||||
Log *slog.Logger
|
||||
}
|
||||
|
||||
// TurnRunner owns one chat turn from session resolution through model
|
||||
@@ -313,6 +319,9 @@ func (r *TurnRunner) prepareTurn(ctx context.Context, sessionID string, msg Mess
|
||||
SessionID: sessionID,
|
||||
Now: time.Now(),
|
||||
Page: msg.Page,
|
||||
Workspace: r.Workspace,
|
||||
Repos: r.Repos,
|
||||
Operator: r.Operator,
|
||||
})
|
||||
compression, err := CompressionConfigForModel(
|
||||
modelDetails,
|
||||
|
||||
Reference in new issue
Block a user