[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-3463":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":9,"language":10,"languages":9,"totalLinesOfCode":9,"stars":11,"forks":12,"watchers":13,"openIssues":14,"contributorsCount":15,"subscribersCount":15,"size":15,"stars1d":14,"stars7d":14,"stars30d":14,"stars90d":15,"forks30d":15,"starsTrendScore":16,"compositeScore":17,"rankGlobal":9,"rankLanguage":9,"license":9,"archived":18,"fork":18,"defaultBranch":19,"hasWiki":20,"hasPages":18,"topics":21,"createdAt":9,"pushedAt":9,"updatedAt":22,"readmeContent":23,"aiSummary":24,"trendingCount":15,"starSnapshotCount":15,"syncStatus":25,"lastSyncTime":26,"discoverSource":27},3463,"go-bt","rvitorper\u002Fgo-bt","rvitorper","Minimalist BT implementation in Go",null,"Go",103,5,102,1,0,3,41.93,false,"main",true,[],"2026-06-12 04:00:17","# go-bt: Minimalist Behavior Trees for Go\n\n`go-bt` is a Behavior Tree library for Go.\n\nDesigned for background workers, game AI, mundane task automation, and asynchronous logic.\n\nInstead of `time.Sleep` or infinite `while` loops, `go-bt` uses a cooperative multitasking model. Nodes return state instantly, yielding control back to the supervisor.\n\n## Core Design Principles\n\n1. **Stateless Nodes:** Nodes (`Sequence`, `Selector`, etc.) do not hold runtime state. All temporal memory and execution states live entirely within the generic `BTContext[T]`.\n2. **The Magic Numbers:** Every node's `Run` method returns exactly one of three integers:\n   * `1` (**Success**): The task is done and successful.\n   * `0` (**Running**): The task needs more time (e.g., waiting on I\u002FO, or sleeping). It yields the thread.\n   * `-1` (**Failure**): The task failed.\n3. **First-Class Context:** `BTContext[T]` directly embeds Go's standard `context.Context`, ensuring your trees natively respect global cancellation tokens and timeouts.\n4. **Time-Travel Testing:** The engine uses an injected clock directly on the context, allowing you to instantly test a 5-minute `Timeout` or `Sleep` node in your unit tests without actually waiting.\n\n## The Node Library\n\n`go-bt` provides a minimalist set of core nodes that can be combined to create any logical control flow:\n\n* **Composites:** `Selector` (Stateless Priority \u002F Fallback), `Sequence` (Stateless AND gate), `MemSequence` (Stateful chronological execution)\n* **Decorators:** `Inverter` (NOT gate), `Optional` (Swallows failures), `Timeout`, `Retry`, `Repeat`\n* **Leaves:** `Condition` (Instant memory read), `Action` (Execution), `Sleep` (Non-blocking wait)\n\n---\n\n## Quick Start\n\n### 1. Define your Blackboard\nYour blackboard is the custom state your tree will read and manipulate. Because the library uses Go Generics, it can be any struct.\n\n```go\ntype WorkerState struct {\n    IsConnected  bool\n    PendingTasks int\n}\n```\n\n### 2. Build the Tree\nYou can use the `go-bt` nodes to assemble your logic. This example asserts that the worker is connected before attempting to process tasks:\n\n```go\npackage main\n\nimport (\n    \"time\"\n    \"context\"\n    \"go-bt\u002Fcore\"\n    \"go-bt\u002Fcomposite\"\n    \"go-bt\u002Fleaf\"\n)\n\nfunc BuildWorkerTree(cancel context.CancelFunc) core.Command[WorkerState] {\n   return composite.NewSelector(\n      composite.NewSequence(\n         composite.NewSelector(\n            leaf.NewCondition(func(blackboard *WorkerState) bool {\n               \u002F\u002F Assert connected\n               \u002F\u002F Here you'd check whether the connection is valid, etc.\n               return blackboard.IsConnected\n            }),\n            leaf.NewAction(func(ctx *core.BTContext[WorkerState]) int {\n               \u002F\u002F Make a network connection\n               \u002F\u002F Here you'd actually connect to a database\n               ctx.Blackboard.IsConnected = true\n               return 1\n            }),\n         ),\n         leaf.NewAction(func(ctx *core.BTContext[WorkerState]) int {\n            \u002F\u002F Process Tasks if connected\n            if ctx.Blackboard.PendingTasks \u003C= 0 {\n               cancel()\n               return 1\n            }\n            ctx.Blackboard.PendingTasks--\n            return 1\n         }),\n      ),\n   )\n}\n```\n\n### 3. Start the Supervisor Daemon\n`go-bt` provides a concurrent, panic-safe `Supervisor` to drive your tree. It spins up in the background and continuously ticks the logic without blocking your main application thread.\n\n```go\nfunc main() {\n   state := &WorkerState{IsConnected: false, PendingTasks: 2}\n   \n   \u002F\u002F Initialize the context with a global cancellation token\n   ctx, cancel := context.WithCancel(context.Background())\n   btCtx := core.NewBTContext(ctx, state)\n   tree := BuildWorkerTree(cancel)\n   \n   \u002F\u002F Create a Supervisor that ticks every 100ms\n   supervisor := core.NewSupervisor(tree, 100*time.Millisecond, func(err any) {\n   println(\"Tree panicked, safely recovered:\", err)\n   })\n   \n   \u002F\u002F Start the daemon in the background\n   wg := supervisor.Start(btCtx)\n   \n   \u002F\u002F Wait for the global context to be cancelled\n   wg.Wait()\n}\n```\n\nYou can check examples in the `example` directory.\n\n---\n\n## Testing & Clock Injection\n\nTesting temporal logic (like `Sleep` or `Timeout`) in CI\u002FCD pipelines usually results in slow, flaky tests. `go-bt` solves this by exposing the `Now` function directly on the `BTContext`.\n\nIn your tests, simply replace the standard clock with a mock closure. This allows you to artificially advance time by hours or days in a single microsecond, instantly triggering your `Timeout` or `Sleep` node's success\u002Ffailure states to assert structural correctness.","`go-bt` 是一个用 Go 语言编写的轻量级行为树库，适用于后台任务、游戏AI、日常任务自动化以及异步逻辑处理。其核心功能包括无状态节点设计、简洁的状态返回机制（成功、运行中、失败）和一流的上下文支持，确保行为树能够自然地响应全局取消信号和超时设置。此外，通过注入时钟到上下文中，它还支持时间旅行测试，使得在单元测试中可以快速验证长时间运行的任务而无需实际等待。该项目适合需要灵活控制流程且对性能有较高要求的应用场景，如自动化脚本执行、游戏中的NPC决策逻辑等。",2,"2026-06-11 02:54:34","CREATED_QUERY"]