-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
3342 lines (997 loc) · 55.8 KB
/
Copy pathindex.html
File metadata and controls
3342 lines (997 loc) · 55.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="generator" content="Hugo 0.164.0">
<title>chengzhycn's blog</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Better work, better life" />
<meta name="keywords" content="" />
<meta name="robots" content="noodp" />
<link rel="canonical" href="https://blog.jinzhi.site/" />
<link rel="stylesheet" href="https://blog.jinzhi.site/css/buttons.min.86f6b4c106b6c6eb690ae5203d36b442c1f66f718ff4e8164fa86cf6c61ad641.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/code.min.c4581949edab7810737609fc5568852bcd908945f14561e366dcd2300a370545.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/fonts.min.5bb7ed13e1d00d8ff39ea84af26737007eb5051b157b86fc24487c94f3dc8bbe.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/footer.min.eb8dfc2c6a7eafa36cd3ba92d63e69e849e2200e0002a228d137f236b09ecd75.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/gist.min.a751e8b0abe1ba8bc53ced52a38b19d8950fe78ca29454ea8c2595cf26aad5c0.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/header.min.75c7eb0e2872d95ff48109c6647d0223a38db52e2561dd87966eb5fc7c6bdac6.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/main.min.7984a754b43b7469d389c539410e90dff2870263de16b008dfc7e8170fcd0f5b.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/menu.min.3c17467ebeb3d38663dce68f71f519901124fa5cbb4519b2fb0667a21e9aca39.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/pagination.min.bbb986dbce00a5ce5aca0504b7925fc1c581992a4bf57f163e5d69cc1db7d836.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/post.min.e6dddd258e64c83e05cec0cd49c05216742d42fc8ecbfbe6b67083412b609bd3.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/syntax.min.a2cc694a9b481f15d41722bffc2cc54eea4c4f74fd9072b0b33253604c22606e.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/terminal.min.e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css">
<link rel="stylesheet" href="https://blog.jinzhi.site/css/terms.min.b81791663c3790e738e571cdbf802312390d30e4b1d8dc9d814a5b5454d0ac11.css">
<link rel="shortcut icon" href="https://blog.jinzhi.site/favicon.png">
<link rel="apple-touch-icon" href="https://blog.jinzhi.site/apple-touch-icon.png">
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="" />
<meta name="twitter:creator" content="" />
<meta property="og:locale" content="en" />
<meta property="og:type" content="website" />
<meta property="og:title" content="chengzhycn's blog">
<meta property="og:description" content="Better work, better life" />
<meta property="og:url" content="https://blog.jinzhi.site/" />
<meta property="og:site_name" content="chengzhycn's blog" />
<meta property="og:image" content="https://blog.jinzhi.site/og-image.png">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="627">
<link href="/index.xml" rel="alternate" type="application/rss+xml" title="chengzhycn's blog" />
<html>
<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/lxgw-wenkai-screen-web/style.css" />
<style>
body {
font-family: "LXGW WenKai Screen";
font-weight: normal;
}
</style>
</head>
<body>
</body>
</html>
</head>
<body>
<div class="page-wrapper">
<div class="container center">
<header class="header">
<div class="header__inner">
<div class="header__logo">
<a href="/">
<div class="logo">
chengzhycn's blog
</div>
</a>
</div>
<ul class="menu menu--mobile">
<li class="menu__trigger">Menu ▾</li>
<li>
<ul class="menu__dropdown">
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/categories/">Categories</a></li>
<li><a href="/tags/">Tags</a></li>
<li><a href="/archives/">Archives</a></li>
</ul>
</li>
</ul>
</div>
<nav class="navigation-menu">
<ul class="navigation-menu__inner menu--desktop">
<li><a href="/" >Home</a></li>
<li><a href="/about" >About</a></li>
<li><a href="/categories/" >Categories</a></li>
<li><a href="/tags/" >Tags</a></li>
<li><a href="/archives/" >Archives</a></li>
</ul>
</nav>
</header>
<div class="content">
<div class="posts">
<article class="post on-list">
<h2 class="post-title">
<a href="https://blog.jinzhi.site/posts/2026-07/opencode-memory-%E5%AE%9E%E7%8E%B0%E4%B8%8E%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/">opencode memory 实现与最佳实践</a>
</h2>
<div class="post-meta"><time class="post-date">2026-07-10</time></div>
<span class="post-tags">
#<a href="https://blog.jinzhi.site/tags/ai/">AI</a>
#<a href="https://blog.jinzhi.site/tags/agent/">Agent</a>
</span>
<div class="post-content">
<blockquote>
<p>注:文章由 codex 整理。</p>
</blockquote>
<h2 id="背景">背景</h2>
<p>这里的 <code>memory</code> 不是操作系统内存,也不是模型推理时的 KV cache,而是 coding agent 在多轮、多 session、多项目之间保存和复用上下文的能力。</p>
<p>截至这次分析的源码快照,<code>opencode</code> 并没有内置一个类似 <code>mem0</code>、<code>mnemopi</code> 或向量数据库的长期记忆系统。它的 memory 更接近三层组合:</p>
<ol>
<li><strong>规则型长期记忆</strong>:<code>AGENTS.md</code>、全局 <code>AGENTS.md</code>、<code>opencode.json</code> 里的 <code>instructions</code>。这些内容每轮都会进入 system prompt,是最稳定、最可审计的记忆。</li>
<li><strong>会话内压缩记忆</strong>:<code>compaction</code> agent 把长会话压缩成摘要,并保留最近若干 turn,让当前 session 可以继续推进。</li>
<li><strong>上下文型临时记忆</strong>:用户用 <code>@file</code>、MCP resource、skill、subagent 等方式临时补充上下文,主要服务当前任务。</li>
</ol>
<p>所以分析 <code>opencode memory</code> 时,关键不是找一个 <code>MemoryBackend</code>,而是看它如何把规则文件、配置、压缩摘要和临时上下文组装进模型请求。</p>
<p>本文基于:</p>
<ul>
<li>官方文档:<a href="https://opencode.ai/docs/rules/">Rules</a>、<a href="https://opencode.ai/docs/config/">Config</a>、<a href="https://opencode.ai/docs/agents/">Agents</a>、<a href="https://opencode.ai/docs/skills/">Agent Skills</a></li>
<li>源码快照:<a href="https://github.com/anomalyco/opencode/commit/251177d56cf2a8821f7e1464dd7c164e8f56036d">anomalyco/opencode@251177d56cf2a8821f7e1464dd7c164e8f56036d</a></li>
<li>重点源码文件:
<ul>
<li><code>packages/opencode/src/session/instruction.ts</code></li>
<li><code>packages/opencode/src/session/prompt.ts</code></li>
<li><code>packages/opencode/src/session/compaction.ts</code></li>
<li><code>packages/opencode/src/session/system.ts</code></li>
<li><code>packages/opencode/src/config/config.ts</code></li>
<li><code>packages/core/src/v1/config/config.ts</code></li>
</ul>
</li>
</ul>
<h2 id="核心结论">核心结论</h2>
<p><code>opencode</code> 的 memory 设计偏“显式上下文工程(context engineering)”,而不是“自动记住一切”。</p>
<p>这带来一个很重要的实践判断:<strong>应该把长期稳定的项目知识写进规则文件,把当前任务过程交给 session history 和 compaction,把低置信度、临时性、可能过期的信息留在对话里,而不是沉淀到 AGENTS.md。</strong></p>
<p>可以这样理解:</p>
<table>
<thead>
<tr>
<th>层次</th>
<th>opencode 机制</th>
<th>适合保存什么</th>
<th>不适合保存什么</th>
</tr>
</thead>
<tbody>
<tr>
<td>长期项目记忆</td>
<td>项目 <code>AGENTS.md</code></td>
<td>架构边界、测试命令、代码风格、危险操作约束</td>
<td>临时任务进度、一次性 debug 现象</td>
</tr>
<tr>
<td>长期个人记忆</td>
<td><code>~/.config/opencode/AGENTS.md</code></td>
<td>个人偏好、默认工作方式、输出习惯</td>
<td>某个项目的私有约定</td>
</tr>
<tr>
<td>可复用规则片段</td>
<td><code>opencode.json</code> 的 <code>instructions</code></td>
<td>多文件规范、共享团队标准、monorepo 分包规则</td>
<td>需要模型自己递归解析的隐式引用</td>
</tr>
<tr>
<td>会话内记忆</td>
<td><code>compaction</code></td>
<td>当前 session 的目标、决策、已完成动作、未解决问题</td>
<td>跨 session 的长期知识</td>
</tr>
<tr>
<td>临时上下文</td>
<td><code>@file</code>、MCP、skills、subagents</td>
<td>本轮要读的文件、外部资料、专项流程</td>
<td>需要每次都遵守的硬规则</td>
</tr>
</tbody>
</table>
<h2 id="实现总览">实现总览</h2>
<p>一轮模型调用前,<code>SessionPrompt</code> 会把几类 system prompt 片段拼起来:</p>
</div>
<div>
<a class="read-more button inline" href="/posts/2026-07/opencode-memory-%E5%AE%9E%E7%8E%B0%E4%B8%8E%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/">[Read more]</a>
</div>
</article>
<article class="post on-list">
<h2 class="post-title">
<a href="https://blog.jinzhi.site/posts/2026-07/loop-engineering-%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/">Loop Engineering 最佳实践</a>
</h2>
<div class="post-meta"><time class="post-date">2026-07-04</time></div>
<span class="post-tags">
#<a href="https://blog.jinzhi.site/tags/ai/">AI</a>
#<a href="https://blog.jinzhi.site/tags/agent/">Agent</a>
</span>
<div class="post-content">
<h2 id="背景">背景</h2>
<blockquote>
<p>注:文章由 codex 整理。</p>
</blockquote>
<p><code>Loop engineering</code> 讨论的不是普通代码里的 <code>for/while</code>,也不是 agent harness 内部已经存在的 <code>observe -> act -> observe</code> 工具调用循环,而是<strong>人类交给 agent harness 的外部循环规格(loop specification)</strong>。</p>
<p>arXiv 论文 <a href="https://arxiv.org/abs/2607.00038">Stop Hand-Holding Your Coding Agent: Engineering the Loops that Replace Step-by-Step Prompting</a> 给出的核心定义是:<code>loop specification</code> 是一个有边界、可复用的 artifact,包含 <code>trigger</code>、<code>goal</code>、<code>verification</code>、<code>stopping rule</code> 和 <code>memory</code>,由人类交给 Claude Code、Codex 等 agent harness,让 agent 在无需人类逐步提示的情况下追踪目标、执行、检查并停止。</p>
<p>这篇论文的价值在于把社区里比较口号化的说法收束成一个工程问题:</p>
<ul>
<li><code>prompt engineering</code> 问的是:这一轮怎么问?</li>
<li><code>context engineering</code> 问的是:agent 应该知道什么?</li>
<li><code>harness engineering</code> 问的是:agent 能在什么环境里行动?</li>
<li><code>loop engineering</code> 问的是:如何设计一个系统,让 agent 能发现工作、执行工作、验证结果、记录状态,并知道什么时候停?</li>
</ul>
<p>所以它不是“prompt engineering 已死”,而是 prompt 之外多了一层控制系统。一个 loop 的底层仍然会使用 prompt,但关键杠杆从“写一句更聪明的话”变成“设计一个带反馈、验证和刹车的闭环”。</p>
</div>
<div>
<a class="read-more button inline" href="/posts/2026-07/loop-engineering-%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5/">[Read more]</a>
</div>
</article>
<article class="post on-list">
<h2 class="post-title">
<a href="https://blog.jinzhi.site/posts/2026-07/oh-my-pi-memory-%E7%B3%BB%E7%BB%9F%E5%AE%9E%E7%8E%B0%E5%88%86%E6%9E%90/">oh-my-pi memory 系统实现分析</a>
</h2>
<div class="post-meta"><time class="post-date">2026-07-04</time></div>
<span class="post-tags">
#<a href="https://blog.jinzhi.site/tags/ai/">AI</a>
#<a href="https://blog.jinzhi.site/tags/agent/">Agent</a>
</span>
<div class="post-content">
<blockquote>
<p>注:文章由 codex 整理。</p>
</blockquote>
<h2 id="背景">背景</h2>
<p><code>oh-my-pi</code> 的 memory 系统不是一个单一模块,而是一层 <code>MemoryBackend</code> 抽象下面挂了几种不同的实现。它要解决的问题是:Agent 的上下文窗口是短期的,但用户偏好、项目约定、设计决策、踩坑经验应该跨 session 保留下来,并能在未来对话中重新进入模型上下文。</p>
<p>从实现上看,<code>oh-my-pi</code> 把 memory 拆成三个问题:</p>
<ol>
<li><strong>记忆写到哪里</strong>:本地 Markdown 摘要、本地 SQLite、还是远端 Hindsight 服务。</li>
<li><strong>什么时候写入</strong>:工具显式 <code>retain</code>,还是每 N 轮自动保留 transcript。</li>
<li><strong>什么时候读出</strong>:启动时注入摘要,首轮 prompt 前自动 recall,或者由模型主动调用 <code>recall</code> / <code>reflect</code>。</li>
</ol>
<h2 id="总体架构">总体架构</h2>
<p>memory backend 的公共接口定义在:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">packages/coding-agent/src/memory-backend/types.ts
</span></span><span class="line"><span class="cl">packages/coding-agent/src/memory-backend/resolve.ts
</span></span></code></pre></div><p><code>resolveMemoryBackend(settings)</code> 根据 <code>memory.backend</code> 选择具体实现:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">memory.backend = off -> offBackend
</span></span><span class="line"><span class="cl">memory.backend = local -> localBackend
</span></span><span class="line"><span class="cl">memory.backend = hindsight -> hindsightBackend
</span></span><span class="line"><span class="cl">memory.backend = mnemopi -> mnemopiBackend
</span></span></code></pre></div><p><code>MemoryBackend</code> 的关键 hook 是:</p>
<ul>
<li><code>start()</code>:session 启动时建立运行时状态、订阅 session event。</li>
<li><code>buildDeveloperInstructions()</code>:构造要注入系统提示词的 memory 文本。</li>
<li><code>beforeAgentStartPrompt()</code>:在当前 turn 生成前追加一次性 recall 结果,保证首轮就能吃到记忆。</li>
<li><code>enqueue()</code>:强制触发 retain / consolidation。</li>
<li><code>clear()</code>:清理当前 backend 的持久化状态。</li>
<li><code>status()</code> / <code>search()</code> / <code>save()</code> / <code>stats()</code> / <code>diagnose()</code>:给 UI、slash command、extension 使用。</li>
</ul>
<p>三种真实 backend 的定位不同:</p>
</div>
<div>
<a class="read-more button inline" href="/posts/2026-07/oh-my-pi-memory-%E7%B3%BB%E7%BB%9F%E5%AE%9E%E7%8E%B0%E5%88%86%E6%9E%90/">[Read more]</a>
</div>
</article>
<article class="post on-list">
<h2 class="post-title">
<a href="https://blog.jinzhi.site/posts/2026-07/claude-code-auto-mode-%E5%AE%9E%E7%8E%B0%E5%88%86%E6%9E%90/">Claude Code auto-mode 实现分析</a>
</h2>
<div class="post-meta"><time class="post-date">2026-07-03</time></div>
<span class="post-tags">
#<a href="https://blog.jinzhi.site/tags/ai/">AI</a>
#<a href="https://blog.jinzhi.site/tags/agent/">Agent</a>
</span>
<div class="post-content">
<blockquote>
<p>注:文章由 codex 整理。</p>
</blockquote>
<h2 id="背景">背景</h2>
<p>本文分析的是 <code>chengzhycn/claude-code</code> 在提交 <code>4b9d30f7953273e567a18eb819f4eddd45fcc877</code> 上的源码实现。</p>
<p>源码入口:</p>
<ul>
<li>仓库:https://github.com/chengzhycn/claude-code/tree/4b9d30f7953273e567a18eb819f4eddd45fcc877</li>
<li>权限模式定义:<code>src/utils/permissions/PermissionMode.ts</code></li>
<li>auto-mode 状态切换:<code>src/utils/permissions/permissionSetup.ts</code></li>
<li>工具权限主流程:<code>src/utils/permissions/permissions.ts</code></li>
<li>auto-mode classifier:<code>src/utils/permissions/yoloClassifier.ts</code></li>
<li>Bash 权限判定:<code>src/tools/BashTool/bashPermissions.ts</code></li>
<li>Bash 只读命令判定:<code>src/tools/BashTool/readOnlyValidation.ts</code></li>
<li>Bash 注入/语法安全检查:<code>src/tools/BashTool/bashSecurity.ts</code></li>
</ul>
<p>核心问题是:Claude Code 怎么在“不每一步都问用户”和“不直接 bypass permissions”之间做折中。源码里的 auto-mode 不是一个简单的全局 allow,而是一套组合机制:</p>
<ol>
<li>进入 auto-mode 时先检查 feature gate 和 opt-in。</li>
<li>临时剥离会绕过 classifier 的危险 allow rule。</li>
<li>工具调用先走原有本地权限系统。</li>
<li>对本地系统仍然需要 <code>ask</code> 的动作,再交给 auto-mode classifier 判断 allow/block。</li>
<li>classifier 不可用、输出不可解析或 transcript 太长时,按场景 fail closed 或回退到人工 permission prompt。</li>
</ol>
<h2 id="模型">模型</h2>
<p>auto-mode 可以理解成一个“permission prompt 的自动代理”,而不是新的沙箱。它的安全性来自三层:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">工具自己的权限检查
</span></span><span class="line"><span class="cl"> -> 本地静态安全规则:路径、只读命令、deny/ask/allow rule、shell 注入模式
</span></span><span class="line"><span class="cl"> -> auto-mode 快速路径:acceptEdits 可允许的操作、安全工具 allowlist
</span></span><span class="line"><span class="cl"> -> auto-mode classifier:读取 transcript + 当前 action,输出 allow/block
</span></span></code></pre></div><p>这意味着:</p>
</div>
<div>
<a class="read-more button inline" href="/posts/2026-07/claude-code-auto-mode-%E5%AE%9E%E7%8E%B0%E5%88%86%E6%9E%90/">[Read more]</a>
</div>
</article>
<article class="post on-list">
<h2 class="post-title">
<a href="https://blog.jinzhi.site/posts/2026-06/http2-%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6/">HTTP2 流量控制</a>
</h2>
<div class="post-meta"><time class="post-date">2026-06-14</time></div>
<span class="post-tags">
#<a href="https://blog.jinzhi.site/tags/http/">HTTP</a>
#<a href="https://blog.jinzhi.site/tags/network/">Network</a>
</span>
<div class="post-content">
<h2 id="核心问题">核心问题</h2>
<blockquote>
<p>注:文章由 codex 整理。</p>
</blockquote>
<p>HTTP/2 的一个核心收益是 multiplexing:多个 request/response 可以复用同一条 TCP connection,并且不同 stream 的 frame 可以交错发送。这个能力解决了 HTTP/1.x 依赖多连接并发的问题,但也引入了一个新问题:<strong>如果某个接收方、某个 stream 或某个应用层消费者处理不过来,发送方应该怎样被限制,而不至于把整个连接的内存打爆?</strong></p>
<p>HTTP/2 flow control 解决的就是这个问题。它不是拥塞控制(congestion control),也不是优先级调度(prioritization),而是一个接收方驱动的背压机制:</p>
<ul>
<li>TCP 拥塞控制关心网络路径能承受多少数据。</li>
<li>HTTP/2 flow control 关心接收端应用和协议栈愿意接收多少 <code>DATA</code> payload。</li>
<li>HTTP/2 priority 关心多个 stream 竞争发送机会时先发谁。</li>
</ul>
<p>可以把它理解为:TCP 管路决定“路上能跑多少车”,HTTP/2 flow control 决定“每个收货口和总仓库还能接多少货”。</p>
<h2 id="基本模型">基本模型</h2>
<p>HTTP/2 的流量控制基于 credit/window:</p>
<ol>
<li>接收方声明自己还能接收多少字节。</li>
<li>发送方发送 <code>DATA</code> frame 时消耗窗口。</li>
<li>接收方消费掉数据、释放缓冲后,通过 <code>WINDOW_UPDATE</code> 把窗口加回来。</li>
<li>如果窗口耗尽,发送方必须暂停发送受流控约束的数据。</li>
</ol>
<p>这里有两个层级的窗口:</p>
<table>
<thead>
<tr>
<th>窗口</th>
<th>作用范围</th>
<th>用来限制什么</th>
</tr>
</thead>
<tbody>
<tr>
<td>stream flow-control window</td>
<td>单个 stream</td>
<td>某个 request/response body 的发送量</td>
</tr>
<tr>
<td>connection flow-control window</td>
<td>整条 HTTP/2 connection</td>
<td>所有 stream 的 <code>DATA</code> frame 总发送量</td>
</tr>
</tbody>
</table>
<p>发送一个 <code>DATA</code> frame 之前,发送方必须同时检查两个窗口:<strong>frame payload 长度不能超过 stream window,也不能超过 connection window</strong>。发送后,这两个窗口都会按 payload 长度扣减。注意,HTTP/2 frame header 的 9 个字节不计入流控窗口。</p>
</div>
<div>
<a class="read-more button inline" href="/posts/2026-06/http2-%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6/">[Read more]</a>
</div>
</article>
<div class="pagination">
<div class="pagination__buttons">
<a href="/page/2/" class="button inline next">
[<span class="button__text">Older posts</span>] >
</a>
</div>
</div>
</div>
</div>
<footer class="footer">
<div class="footer__inner">
<div class="copyright">
<span>© 2026 Powered by <a href="https://gohugo.io">Hugo</a></span>
<span>:: <a href="https://github.com/panr/hugo-theme-terminal" target="_blank">Theme</a> made by <a href="https://github.com/panr" target="_blank">panr</a></span>
</div>
</div>
</footer>
<script type="text/javascript" src="/bundle.min.js"></script>
</div>
<aside class="floating-sidebar">
<div class="sidebar time-archive">
<h3>Time Archives</h3>
<div class="archive-list">
<div class="archive-item">
<a href="/posts/2026-07/" class="archive-link">
<span class="archive-date">2026-07</span>
<span class="archive-month-name">Jul</span>
<span class="archive-count">(4)</span>
</a>
</div>
<div class="archive-item">
<a href="/posts/2026-06/" class="archive-link">
<span class="archive-date">2026-06</span>
<span class="archive-month-name">Jun</span>
<span class="archive-count">(5)</span>
</a>
</div>
<div class="archive-item">
<a href="/posts/2026-05/" class="archive-link">
<span class="archive-date">2026-05</span>
<span class="archive-month-name">May</span>
<span class="archive-count">(1)</span>
</a>
</div>
<div class="archive-item">
<a href="/posts/2026-04/" class="archive-link">
<span class="archive-date">2026-04</span>
<span class="archive-month-name">Apr</span>
<span class="archive-count">(1)</span>
</a>
</div>
<div class="archive-item">