Skip to content

Commit 44f810e

Browse files
Copilotnotfolder
andcommitted
重複記録を回避し、Legacyモードのみでフック使用。コードレビュー指摘も修正
Co-authored-by: notfolder <20558197+notfolder@users.noreply.github.com>
1 parent 708e6d5 commit 44f810e

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

clients/ollama_client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ def get_response(self) -> tuple[str, list, int]:
177177

178178
# 統計記録フックを呼び出し
179179
self._invoke_statistics_hook(total_tokens)
180+
181+
return reply, [], total_tokens
180182

181183
except Exception as e:
182184
# Log error
@@ -187,9 +189,6 @@ def get_response(self) -> tuple[str, list, int]:
187189
)
188190
raise
189191

190-
else:
191-
return reply, [], total_tokens
192-
193192
finally:
194193
# Clean up request.json
195194
if request_path.exists():

handlers/task_handler.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ def handle(self, task: Task) -> None:
110110
# 変換に失敗した場合は通常処理に進む(エラーはログに記録済み)
111111
self.logger.warning("Issue→MR/PR変換に失敗しました。通常処理を継続します。")
112112

113-
# トークン統計記録フックを設定(全モード共通)
114-
# UUIDがある場合のみ統計記録を有効化
115-
self._setup_statistics_hook(task)
116-
117113
# 計画機能の有効可否を先に判定する
118114
planning_config = self.config.get("planning", {})
119115
planning_enabled = planning_config.get("enabled", True)
@@ -131,43 +127,46 @@ def handle(self, task: Task) -> None:
131127
# LLMクライアントのツール定義を更新(実行環境ラッパー含む)
132128
self._update_llm_client_tools()
133129
# Use planning-based task handling
130+
# Planning modeでは内部で統計記録を行うのでフックは不要
134131
self._handle_with_planning(task, self.config, execution_manager)
135132
else:
136133
# Check if context storage is enabled
137134
context_storage_enabled = self.config.get("context_storage", {}).get("enabled", False)
138135

139136
if context_storage_enabled and task.uuid:
140137
# Use file-based context storage
138+
# Context Storage modeでは内部で統計記録を行うのでフックは不要
141139
self._handle_with_context_storage(task, self.config)
142140
else:
143141
# Use legacy in-memory handling
144-
self._handle_legacy(task, self.config)
142+
# Legacy modeでは統計記録がないため、フックを設定して記録する
143+
self._setup_statistics_hook_for_legacy(task)
144+
try:
145+
self._handle_legacy(task, self.config)
146+
finally:
147+
self._clear_statistics_hook()
145148
finally:
146149
# 実行環境のクリーンアップ
147150
self._cleanup_execution_environment(execution_manager, task)
148-
149-
# 統計記録フックをクリア
150-
self._clear_statistics_hook()
151151

152-
def _setup_statistics_hook(self, task: Task) -> None:
153-
"""トークン統計記録フックを設定する.
152+
def _setup_statistics_hook_for_legacy(self, task: Task) -> None:
153+
"""Legacy モード用のトークン統計記録フックを設定する.
154154
155-
タスクにUUIDがある場合、TaskContextManagerを作成(または取得)し
156-
LLMクライアントに統計記録フックを設定します
155+
Legacy モードではTaskContextManagerを使用しないため
156+
ここで作成してLLMクライアントにフックを設定します
157157
158158
Args:
159159
task: タスクオブジェクト
160160
161161
"""
162162
if not task.uuid:
163-
self.logger.info("タスクにUUIDがないため、トークン統計記録は無効です")
163+
self.logger.info("タスクにUUIDがないため、Legacy モードでのトークン統計記録は無効です")
164164
return
165165

166166
try:
167167
from context_storage import TaskContextManager
168168

169-
# TaskContextManagerを作成してインスタンス変数に保持
170-
# (planning/context_storageモードでは既に作成されているが、legacyモード用に保険)
169+
# Legacy モード用のTaskContextManagerを作成
171170
self._statistics_context_manager = TaskContextManager(
172171
task_key=task.get_task_key(),
173172
task_uuid=task.uuid,
@@ -184,11 +183,11 @@ def statistics_hook(llm_calls: int, tokens: int) -> None:
184183
)
185184

186185
self.llm_client.set_statistics_hook(statistics_hook)
187-
self.logger.info("トークン統計記録フックを設定しました: uuid=%s", task.uuid)
186+
self.logger.info("Legacy モード用のトークン統計記録フックを設定しました: uuid=%s", task.uuid)
188187

189188
except Exception as e:
190189
self.logger.warning(
191-
"トークン統計記録フックの設定に失敗しました(統計は記録されません): %s",
190+
"Legacy モード用のトークン統計記録フックの設定に失敗しました(統計は記録されません): %s",
192191
e,
193192
exc_info=True,
194193
)
@@ -198,6 +197,16 @@ def _clear_statistics_hook(self) -> None:
198197
"""トークン統計記録フックをクリアする."""
199198
if hasattr(self, 'llm_client') and self.llm_client:
200199
self.llm_client.set_statistics_hook(None)
200+
201+
# TaskContextManagerのクリーンアップ(Legacy モード用)
202+
if hasattr(self, '_statistics_context_manager') and self._statistics_context_manager:
203+
try:
204+
# タスク完了としてマーク
205+
self._statistics_context_manager.complete()
206+
except Exception as e:
207+
self.logger.warning("Legacy モード用TaskContextManagerのクリーンアップに失敗: %s", e)
208+
finally:
209+
self._statistics_context_manager = None
201210

202211
def _init_execution_environment(
203212
self,

0 commit comments

Comments
 (0)