asyncmode

package
v2.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package asyncmode 是「异步对话模式」的纯逻辑:有效模式的判定、降级原因、写给智能体的回执文案。

这里不碰数据库也不碰网络:Store 与 MCP / REST 层把当前状态(用户开关、hook 最近出现的时间)传进来, 拿回判定结果与要写进回执的行。这样判定规则与文案可以脱离其余系统穷举测试,也只有一处定义。

Index

Constants

View Source
const (
	Sync  = "sync"
	Async = "async"
)

模式名。

View Source
const (
	// DefaultWindowAdd 是智能体每发一条新消息、回复窗口增加的时长。
	DefaultWindowAdd = 5 * time.Minute
	// DefaultWaitSlice 是智能体每次用计时器连续等待的最长时间,到点后回头读收件箱。
	DefaultWaitSlice = 10 * time.Minute
	// WindowMax 是回复窗口从此刻起最多能累加到的长度,防止无限续命。
	WindowMax = 30 * time.Minute
)
View Source
const AnsweredNote = "答复已在收件箱 / 确认项里:按答复接着做并置进行中,或在进展里写明为什么仍要暂缓"

AnsweredNote 是「确认卡已答复、任务还在暂缓」的软提醒正文(不含编号):回执 tasks: 行与 task_board overview 共用。

View Source
const AuthLineMax = 300

AuthLineMax 是 auth: 行的字数上限(含前缀、档位名与等级)。

View Source
const Contract = `` /* 1704-byte string literal not displayed */

Contract 是异步生效时回执里的 async: 契约(不超过 760 字,测试里卡死)。 它是智能体在异步模式下唯一的行为说明,所以把「不可逆操作(L2)批准前绝不执行」(INV-3)写成独立的一条: 用 async_ask 发 L2 确认卡、不阻断其余工作,没有答复就是不批准(#217,Q68 方案 A)。 上限从 600 放宽到 760,是为了写进两条工作方式(#221,用户 #859 / #834,确认项 Q73 选 a): 等用户的事一律发成确认卡、不只写在汇报里((1) 的后半句);长耗时命令默认后台跑、等待用计时器分片((7))。

View Source
const GateListMax = 8

GateListMax 是回执里最多逐个点名的未完成任务数;更多的只给总数,避免一个积压很多的会话把每条回执撑得很长。

View Source
const GateViewMax = 20

GateViewMax 是 Web 界面里最多列出的未完成任务数(界面可以比回执长得多)。

View Source
const HookFreshness = 60 * time.Minute

HookFreshness 是「最近见过 hook」的有效窗口:异步模式靠 hook 把追加指令与答复在工作期间送到智能体手里, hook 太久没出现,就说明这条通路已经不可靠,此时继续让智能体「发了就走」会让用户的话没人听见,所以自动降级。

View Source
const ShelveNote = "等用户拍板的先发确认卡(async_ask),进展里写 Q<N>;等别的条件写明等什么、预计何时"

ShelveNote 是「暂缓要写明缘由」的软提醒正文(不含任务编号):回执 tasks: 行与 task_board overview 共用同一句话。

Variables

This section is empty.

Functions

func AnsweredMarker added in v2.1.0

func AnsweredMarker(items []AnsweredTask, total int) string

AnsweredMarker 是回执 tasks: 行末尾的反向软提醒:用户已经答复了确认卡,挂着它的任务却还停在「暂缓」。 它防的是答复被漏处理(上下文被压缩、收件箱已读已确认却没接着做);没有这样的任务时返回空串。

func AnsweredNames added in v2.1.0

func AnsweredNames(items []AnsweredTask, total int) string

AnsweredNames 把这类任务写成「Q89→#164 Q90→#170 等共 5 个」:最多点名 GateListMax 个,其余只给总数。

func AuthLine

func AuthLine(p Profile) string

AuthLine 把档位写成回执里的 auth: 行;超过 AuthLineMax 时截断规则并以省略号收尾。

func ExtendWindow

func ExtendWindow(until *time.Time, now time.Time, add time.Duration) time.Time

ExtendWindow 在当前截止时间上累加 add(已过期或没有窗口时从 now 起算),整体不超过 now+WindowMax。

func FormatClock

func FormatClock(d time.Duration) string

FormatClock 把时长写成 mm:ss(超过 1 小时写成 h:mm:ss)。

func GateMarker added in v2.1.0

func GateMarker(tasks []GateTask, total int) string

GateMarker 是回执 tasks: 行末尾的收尾门提示。没有挡着的任务(或门没开 / 已放行)时返回空串。 它与 window: 行互补:窗口为空(从没起算或已用完)时没有 window: 行,这条仍然告诉智能体「现在还不能收尾」。

func GateNames added in v2.1.0

func GateNames(tasks []GateTask, total int) string

GateNames 把挡着收尾的任务写成「#12 #15 #18 等共 11 个」:最多点名 GateListMax 个,其余只给总数。 total 是未完成任务总数(≥ len(tasks));tasks 为空时返回空串。

func ReasonText

func ReasonText(r Reason) string

ReasonText 是降级原因的人话(回执与界面共用)。

func ReceiptLines

func ReceiptLines(st State, p Profile) (mode []string, auth string)

ReceiptLines 返回回执头里的 mode 相关行与 auth 行。

  • 没开启过异步的工作流:全部为空(INV-2,回执与升级之前逐字一致);
  • 异步生效:mode: async + 契约,auth 行取代阶段提示词;
  • 开启过、但现在是同步:一行 mode: sync(原因),阶段提示词照旧。

func ShelveMarker added in v2.1.0

func ShelveMarker(tasks []GateTask, total int) string

ShelveMarker 是回执 tasks: 行末尾的软提醒:刚置「暂缓」、进展里却没写在等哪张确认卡 / 等什么的任务。 没有这样的任务时返回空串。

func Signature

func Signature(st State, profileID uint, p Profile) string

Signature 把「智能体该知道的模式状态」压成一个短串:有效模式、降级原因、实际生效的档位(编号 + 内容摘要)。 它变了、而智能体还没被告知,就在下一次工具回执里补一段说明(见 mcp 层的 modeNoticeBlock)。 档位内容也算在内:用户改了档位的规则、换了默认档位、删掉了工作流选用的档位(回落到默认),智能体都该知道。

func WindowLine

func WindowLine(w WindowState, now time.Time, slice time.Duration) string

WindowLine 是回执里的 window: 行。没有窗口时返回空串。

  • 有任务在做:窗口暂停,告诉智能体继续做任务,别为了等而停下来;必须等用户拍板的任务该置「暂缓」, 暂缓的不算「在做」,所以不挡窗口也不挡收尾(「非阻断任务做完才能结束会话」,#124);
  • 窗口还剩时间:手头没别的事就用计时器等 min(剩余, slice),到点重读收件箱;仍无消息且未到期再按剩余等;
  • 窗口已到期:最后读一轮收件箱,仍为空即可收尾。

func WindowRemaining

func WindowRemaining(until *time.Time, now time.Time) (time.Duration, bool)

WindowRemaining 返回窗口剩余时长;没有窗口返回 (0, false);已到期返回 (0, true)。

Types

type AnsweredTask added in v2.1.0

type AnsweredTask struct {
	Seq   int    `json:"seq"`   // 任务编号(#12)
	Title string `json:"title"` // 任务标题
	Ref   string `json:"ref"`   // 已答复的确认卡(Q89)
}

AnsweredTask 是「反向」软提醒里的一项:确认卡已经答复,挂着它的任务却还在暂缓、答复之后没人动过它。

type GateTask added in v2.1.0

type GateTask struct {
	// ID 是任务的全局主键(REST 用,Web 上的「暂缓」快捷按钮要它);回执里只用 Seq(#12)。
	ID    uint   `json:"id"`
	Seq   int    `json:"seq"`
	State string `json:"state"`
	Title string `json:"title"`
}

GateTask 是挡着收尾的一个任务。

type Input

type Input struct {
	Enabled  bool      // 用户当前是否开着
	Ever     bool      // 是否曾经开启过
	HookSeen time.Time // 最近一次见到 hook 的时间;零值表示没见过
	Now      time.Time
}

Input 是判定所需的全部事实。

type Profile

type Profile struct {
	Name  string
	Level string
	Rules string
}

Profile 是回执 auth: 行需要的档位信息。

type Reason

type Reason string

Reason 是异步没有生效时的原因(生效时为空)。

const (
	ReasonNone      Reason = ""
	ReasonDisabled  Reason = "disabled"   // 用户开启过,现在关了
	ReasonNoHook    Reason = "no_hook"    // 开着,但这个工作流还没见过 hook
	ReasonHookStale Reason = "hook_stale" // 开着,但 hook 太久没出现
)

type State

type State struct {
	Enabled   bool   // 用户当前是否开着
	Ever      bool   // 是否曾经开启过(开着一定算开启过)
	Effective bool   // 真正生效:开着 且 hook 在线
	Reason    Reason // 没生效的原因;从未开启过的工作流为空
}

State 是判定结果。

func Evaluate

func Evaluate(in Input) State

Evaluate 判定有效模式:用户开启 且 最近一小时内见过 hook 才是异步;否则同步,并给出原因。 从未开启过的工作流不产生原因(Reason 为空)——它没有「降级」可言。

func (State) Mode

func (s State) Mode() string

Mode 返回有效模式名。

type WindowState

type WindowState struct {
	// Until 是走动中的窗口截止时间;暂停(有任务)或没有窗口时为 nil。
	Until *time.Time
	// BankSec 是暂停时攒着的剩余秒数。
	BankSec int
	// ActiveTasks 是挡着窗口的任务数:> 0 时窗口不走。收尾门开着时是门口径的未完成任务数(待办 / 进行中 / 验证中,
	// 不按时间截断);门关着时是老口径(进行中 / 验证中、且 3 小时内有更新)。被「本次放行」后为 0。
	ActiveTasks int
	// Strict 表示这个会话的收尾门开着(用户没有关掉)。
	Strict bool
	// Released 表示用户点了「本次放行」:窗口不再被任务挡住、照常倒计时。
	Released bool
	// Gate 是挡着收尾的任务(Strict 时有值,至多 GateViewMax 个、按编号升序);GateTotal 是总数(≥ len(Gate))。
	// 放行后这两项仍如实给出(界面要显示「放行中,仍有 N 个未完成」),只是不再让窗口暂停。
	Gate      []GateTask
	GateTotal int
}

WindowState 是回复窗口此刻的全部事实。

func (WindowState) Empty

func (w WindowState) Empty() bool

Empty 当前没有任何回复窗口(既不在走,也没有攒着)。

func (WindowState) Paused

func (w WindowState) Paused() bool

Paused 窗口里还攒着时间、但因为有任务在做而没有在走。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL