Files
ARTEX/third_party/norma/noaadapter/inherit.go
T
wangdefa f782b612ec build: 内置 norma 模块,构建不再依赖上游仓库
把 github.com/Autumn-27/norma v0.4.3(commit 320ae32)的源码复制到
third_party/norma,并用 go.mod 的 replace 指向本地副本。上游删除仓库或
改写 tag 都不再影响本仓库构建。

- 已验证 GOPROXY=off 下可完整编译并打出发行包
- go.sum 中该模块的两条校验记录随之删除(本地 replace 不需要哈希)
- 升级方法与授权注意事项见 third_party/README.md
2026-10-20 21:10:41 -07:00

85 lines
3.1 KiB
Go

package noaadapter
import (
"path/filepath"
"github.com/Autumn-27/norma/noa"
)
// maxInheritDepth bounds the walk up the parent chain. Deep enough for any real
// nesting, shallow enough that a cycle or a mis-linked session cannot spin.
const maxInheritDepth = 8
// DeriveChildState hands a subagent the parent's compression ledger.
//
// What carries over and what does not follows from what each field means:
//
// - Blocks, MessageRefs, TokenSnapshot, NextBlockID are the ledger. Without
// them a child that sees inherited context could not address it, and would
// allocate block ids that collide with the parent's.
// - Nudge is reset. Pacing describes one conversation's pressure history; the
// child starts its own, and inheriting a mid-session cadence would either
// silence it or make it nag immediately.
// - Stats is reset so the child reports its own work.
//
// Inherited blocks keep their original ArchivePath. It is absolute and the file
// is still there, so the child can follow a compression back to its originals
// exactly as the parent could — while its own new archives land in its own
// directory.
func DeriveChildState(parent noa.CompressionState, childSessionID, childArchiveRoot string) noa.CompressionState {
child := noa.CloneState(parent)
child.SessionID = childSessionID
child.ArchiveRoot = childArchiveRoot
child.Nudge = noa.NudgeState{LastShownByTier: map[noa.Tier]int{}}
child.Stats = noa.Stats{}
return child
}
// SubagentArchiveRoot is where a subagent's own archives live.
func SubagentArchiveRoot(base, sessionID, agentID string) string {
return filepath.Join(base, sessionID, "subagents", agentID)
}
// InheritOptions describes one link in a parent chain.
type InheritOptions struct {
// ArchiveBaseDir is shared by every session in the chain.
ArchiveBaseDir string
// SessionID is the session being opened.
SessionID string
// ParentIDs are candidate ancestors, nearest first. The walk stops at the
// first one with blocks.
ParentIDs []string
}
// InheritState finds a usable ledger for a session that has none of its own.
//
// A session can arrive empty for ordinary reasons — a fork, a subagent, a
// transcript copied without its sidecar — and starting from zero would make the
// model re-compress everything the ancestor already compressed. So the chain is
// walked until a state with blocks turns up.
//
// Returns ok=false when nothing was found, which is the normal case for a
// genuinely new session.
func InheritState(o InheritOptions) (noa.CompressionState, string, bool) {
root := filepath.Join(o.ArchiveBaseDir, o.SessionID)
for i, parentID := range o.ParentIDs {
if i >= maxInheritDepth {
break
}
if parentID == "" || parentID == o.SessionID {
continue
}
parentRoot := filepath.Join(o.ArchiveBaseDir, parentID)
store := NewStateStore(parentRoot)
if !store.Exists() {
continue
}
st, err := store.Load(parentID, parentRoot)
if err != nil || len(st.Blocks) == 0 {
continue
}
return DeriveChildState(st, o.SessionID, root), parentID, true
}
return noa.CreateInitialState(o.SessionID, root), "", false
}