Skip to content

Commit 48cd2b2

Browse files
committed
fix(core): add project_id to task
1 parent d61f82e commit 48cd2b2

6 files changed

Lines changed: 83 additions & 10 deletions

File tree

src/server/core/acontext_core/llm/tool/task_lib/append_planning.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async def _append_messages_to_planning_section_handler(
2626
)
2727
r = await TD.append_messages_to_planning_section(
2828
ctx.db_session,
29+
ctx.project_id,
2930
ctx.session_id,
3031
actually_message_ids,
3132
)

src/server/core/acontext_core/llm/tool/task_lib/insert.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
async def insert_task_handler(ctx: TaskCtx, llm_arguments: dict) -> Result[str]:
1111
r = await TD.insert_task(
1212
ctx.db_session,
13+
ctx.project_id,
1314
ctx.session_id,
1415
after_order=llm_arguments["after_task_order"],
1516
data={

src/server/core/acontext_core/schema/orm/project.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
if TYPE_CHECKING:
1010
from .space import Space
1111
from .session import Session
12+
from .task import Task
1213
from .tool_reference import ToolReference
1314

1415

@@ -49,6 +50,15 @@ class Project(CommonMixin):
4950
},
5051
)
5152

53+
tasks: List["Task"] = field(
54+
default_factory=list,
55+
metadata={
56+
"db": relationship(
57+
"Task", back_populates="project", cascade="all, delete-orphan"
58+
)
59+
},
60+
)
61+
5262
tool_references: List["ToolReference"] = field(
5363
default_factory=list,
5464
metadata={

src/server/core/acontext_core/schema/orm/task.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from ..utils import asUUID
2020

2121
if TYPE_CHECKING:
22+
from .project import Project
2223
from .session import Session
2324
from .message import Message
2425

@@ -55,6 +56,16 @@ class Task(CommonMixin):
5556
}
5657
)
5758

59+
project_id: asUUID = field(
60+
metadata={
61+
"db": Column(
62+
UUID(as_uuid=True),
63+
ForeignKey("projects.id", ondelete="CASCADE"),
64+
nullable=False,
65+
)
66+
}
67+
)
68+
5869
order: int = field(metadata={"db": Column(Integer, nullable=False)})
5970

6071
data: dict = field(metadata={"db": Column(JSONB, nullable=False)})
@@ -94,3 +105,8 @@ class Task(CommonMixin):
94105
init=False,
95106
metadata={"db": relationship("Session", back_populates="tasks")},
96107
)
108+
109+
project: "Project" = field(
110+
init=False,
111+
metadata={"db": relationship("Project", back_populates="tasks")},
112+
)

src/server/core/acontext_core/service/data/task.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ async def set_task_space_digested(
154154

155155
async def insert_task(
156156
db_session: AsyncSession,
157+
project_id: asUUID,
157158
session_id: asUUID,
158159
after_order: int,
159160
data: dict,
@@ -192,6 +193,7 @@ async def insert_task(
192193
# Step 3: Create new task
193194
task = Task(
194195
session_id=session_id,
196+
project_id=project_id,
195197
order=after_order + 1,
196198
data=data,
197199
status=status,
@@ -223,6 +225,7 @@ async def append_messages_to_task(
223225

224226
async def append_messages_to_planning_section(
225227
db_session: AsyncSession,
228+
project_id: asUUID,
226229
session_id: asUUID,
227230
message_ids: list[asUUID],
228231
) -> Result[None]:
@@ -237,6 +240,7 @@ async def append_messages_to_planning_section(
237240
if planning_task is None:
238241
# add planning section
239242
planning_task = Task(
243+
project_id=project_id,
240244
session_id=session_id,
241245
order=0,
242246
data={},

src/server/core/tests/service/test_task_data.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def test_fetch_all_tasks_success(self):
6060
]
6161

6262
for data in tasks_data:
63-
task = Task(**data)
63+
task = Task(project_id=project.id, **data)
6464
session.add(task)
6565

6666
await session.flush()
@@ -106,12 +106,14 @@ async def test_fetch_tasks_with_status_filter(self):
106106
# Create sample tasks with different statuses
107107
task1 = Task(
108108
session_id=test_session.id,
109+
project_id=project.id,
109110
order=1,
110111
data={"name": "task1"},
111112
status="pending",
112113
)
113114
task2 = Task(
114115
session_id=test_session.id,
116+
project_id=project.id,
115117
order=2,
116118
data={"name": "task2"},
117119
status="running",
@@ -174,6 +176,7 @@ async def test_update_status_success(self):
174176

175177
task = Task(
176178
session_id=test_session.id,
179+
project_id=project.id,
177180
order=1,
178181
data={"name": "test_task"},
179182
status="pending",
@@ -217,6 +220,7 @@ async def test_update_order_success(self):
217220

218221
task = Task(
219222
session_id=test_session.id,
223+
project_id=project.id,
220224
order=1,
221225
data={"name": "test_task"},
222226
status="pending",
@@ -261,6 +265,7 @@ async def test_update_data_success(self):
261265

262266
task = Task(
263267
session_id=test_session.id,
268+
project_id=project.id,
264269
order=1,
265270
data={"name": "original_task"},
266271
status="pending",
@@ -303,6 +308,7 @@ async def test_update_multiple_fields(self):
303308

304309
task = Task(
305310
session_id=test_session.id,
311+
project_id=project.id,
306312
order=1,
307313
data={"name": "original_task"},
308314
status="pending",
@@ -367,6 +373,7 @@ async def test_update_task_with_none_values(self):
367373

368374
task = Task(
369375
session_id=test_session.id,
376+
project_id=project.id,
370377
order=1,
371378
data={"name": "test_task"},
372379
status="pending",
@@ -423,6 +430,7 @@ async def test_update_task_patch_data_success(self):
423430
}
424431
task = Task(
425432
session_id=test_session.id,
433+
project_id=project.id,
426434
order=1,
427435
data=initial_data,
428436
status="pending",
@@ -524,6 +532,7 @@ async def test_update_task_patch_data_with_status_and_order(self):
524532

525533
task = Task(
526534
session_id=test_session.id,
535+
project_id=project.id,
527536
order=1,
528537
data={"task_description": "original", "step": "init"},
529538
status="pending",
@@ -579,7 +588,9 @@ async def test_insert_task_success(self):
579588
data = {"name": "new_task", "description": "A new task"}
580589
after_order = 0 # Insert after position 0 (will become position 1)
581590

582-
result = await insert_task(session, test_session.id, after_order, data)
591+
result = await insert_task(
592+
session, project.id, test_session.id, after_order, data
593+
)
583594

584595
t_data, error = result.unpack()
585596
assert error is None
@@ -617,7 +628,12 @@ async def test_insert_task_with_custom_status(self):
617628
custom_status = "running"
618629

619630
result = await insert_task(
620-
session, test_session.id, after_order, data, status=custom_status
631+
session,
632+
project.id,
633+
test_session.id,
634+
after_order,
635+
data,
636+
status=custom_status,
621637
)
622638

623639
t_data, error = result.unpack()
@@ -655,7 +671,9 @@ async def test_insert_task_default_status(self):
655671
data = {"name": "default_status_task"}
656672
after_order = 2 # Insert after position 2 (will become position 3)
657673

658-
result = await insert_task(session, test_session.id, after_order, data)
674+
result = await insert_task(
675+
session, project.id, test_session.id, after_order, data
676+
)
659677

660678
data, error = result.unpack()
661679
assert error is None
@@ -701,7 +719,9 @@ async def test_insert_task_complex_data(self):
701719
],
702720
}
703721

704-
result = await insert_task(session, test_session.id, 0, complex_data)
722+
result = await insert_task(
723+
session, project.id, test_session.id, 0, complex_data
724+
)
705725

706726
data, error = result.unpack()
707727
assert error is None
@@ -737,18 +757,21 @@ async def test_insert_order_increment(self):
737757
# Create initial tasks with orders 1, 2, 3
738758
task1 = Task(
739759
session_id=test_session.id,
760+
project_id=project.id,
740761
order=1,
741762
data={"name": "task1"},
742763
status="pending",
743764
)
744765
task2 = Task(
745766
session_id=test_session.id,
767+
project_id=project.id,
746768
order=2,
747769
data={"name": "task2"},
748770
status="pending",
749771
)
750772
task3 = Task(
751773
session_id=test_session.id,
774+
project_id=project.id,
752775
order=3,
753776
data={"name": "task3"},
754777
status="pending",
@@ -758,7 +781,9 @@ async def test_insert_order_increment(self):
758781

759782
# Insert a new task after position 1 (should become position 2)
760783
new_data = {"name": "inserted_task"}
761-
result = await insert_task(session, test_session.id, 1, new_data)
784+
result = await insert_task(
785+
session, project.id, test_session.id, 1, new_data
786+
)
762787

763788
new_task, error = result.unpack()
764789
assert error is None
@@ -811,6 +836,7 @@ async def test_delete_task_success(self):
811836

812837
task = Task(
813838
session_id=test_session.id,
839+
project_id=project.id,
814840
order=1,
815841
data={"name": "task_to_delete"},
816842
status="pending",
@@ -875,18 +901,21 @@ async def test_delete_task_cascade_behavior(self):
875901
# Create multiple tasks
876902
task1 = Task(
877903
session_id=test_session.id,
904+
project_id=project.id,
878905
order=1,
879906
data={"name": "task1"},
880907
status="pending",
881908
)
882909
task2 = Task(
883910
session_id=test_session.id,
911+
project_id=project.id,
884912
order=2,
885913
data={"name": "task2"},
886914
status="running",
887915
)
888916
task3 = Task(
889917
session_id=test_session.id,
918+
project_id=project.id,
890919
order=3,
891920
data={"name": "task3"},
892921
status="success",
@@ -939,7 +968,9 @@ async def test_full_task_lifecycle(self):
939968

940969
# 1. Create a task
941970
initial_data = {"name": "lifecycle_task", "step": "created"}
942-
create_result = await insert_task(session, test_session.id, 0, initial_data)
971+
create_result = await insert_task(
972+
session, project.id, test_session.id, 0, initial_data
973+
)
943974
created_task, _ = create_result.unpack()
944975
assert created_task is not None
945976
task_id = created_task.id
@@ -997,9 +1028,15 @@ async def test_multiple_sessions_isolation(self):
9971028
await session.flush()
9981029

9991030
# Create tasks in each session
1000-
await insert_task(session, session1.id, 0, {"session": "1", "task": "A"})
1001-
await insert_task(session, session1.id, 1, {"session": "1", "task": "B"})
1002-
await insert_task(session, session2.id, 0, {"session": "2", "task": "A"})
1031+
await insert_task(
1032+
session, project.id, session1.id, 0, {"session": "1", "task": "A"}
1033+
)
1034+
await insert_task(
1035+
session, project.id, session1.id, 1, {"session": "1", "task": "B"}
1036+
)
1037+
await insert_task(
1038+
session, project.id, session2.id, 0, {"session": "2", "task": "A"}
1039+
)
10031040

10041041
# Fetch tasks for each session
10051042
session1_tasks_result = await fetch_current_tasks(session, session1.id)
@@ -1041,18 +1078,21 @@ async def test_ordering_after_updates(self):
10411078
# Create initial tasks in order
10421079
task1_result = await insert_task(
10431080
session,
1081+
project.id,
10441082
test_session.id,
10451083
0,
10461084
{"name": "task1"}, # Insert after 0 -> position 1
10471085
)
10481086
task2_result = await insert_task(
10491087
session,
1088+
project.id,
10501089
test_session.id,
10511090
1,
10521091
{"name": "task2"}, # Insert after 1 -> position 2
10531092
)
10541093
task3_result = await insert_task(
10551094
session,
1095+
project.id,
10561096
test_session.id,
10571097
2,
10581098
{"name": "task3"}, # Insert after 2 -> position 3
@@ -1065,6 +1105,7 @@ async def test_ordering_after_updates(self):
10651105
# Insert a new task in the middle (after position 1)
10661106
middle_task_result = await insert_task(
10671107
session,
1108+
project.id,
10681109
test_session.id,
10691110
1,
10701111
{"name": "middle_task"}, # Insert after 1 -> position 2

0 commit comments

Comments
 (0)